/* This Program is taken from the following site (without permission) solely for the experimentation of cost analysis of practial java programs http://www.physics.unlv.edu/~pang/cp2_j.html */ /////////////////////////////////////////////////////////////////////////// // // // Program file name: Det.java // // // // Tao Pang 2006 // // // // Last modified: January 18, 2006 // // // // (1) This Java program is part of the book, "An Introduction to // // Computational Physics, 2nd Edition," written by Tao Pang and // // published by Cambridge University Press on January 19, 2006. // // // // (2) No warranties, express or implied, are made for this program. // // // /////////////////////////////////////////////////////////////////////////// // An example of evaluating the determinant of a matrix // via the partial-pivoting Gaussian elimination. import java.lang.*; public class Det { public static void main(String argv[]) { double a[][]= {{ 1, -3, 2, -1, -2}, {-2, 2, -1, 2, 3}, { 3, -3, -2, 1, -1}, { 1, -2, 1, -3, 2}, {-3, -1, 2, 1, -3}}; double d = det(a); System.out.println("The determinant is: " + d); } // Method to evaluate the determinant of a matrix. public static double det(double a[][]) { int n = a.length; int index[] = new int[n]; // Transform the matrix into an upper triangle gaussian(a, index); // Take the product of the diagonal elements double d = 1; for (int i=0; iindex[i])) { sgn = -sgn; int j = index[i]; index[i] = index[j]; index[j] = j; } } return sgn*d; } // Method to carry out the partial-pivoting Gaussian // elimination. Here index[] stores pivoting order. public static void gaussian(double a[][], int index[]) { int n = index.length; double c[] = new double[n]; // Initialize the index for (int i=0; i c1) c1 = c0; } c[i] = c1; } // Search the pivoting element from each column int k = 0; for (int j=0; j pi1) { pi1 = pi0; k = i; } } // Interchange rows according to the pivoting order int itmp = index[j]; index[j] = index[k]; index[k] = itmp; for (int i=j+1; i