Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package math2605; import org.apache.commons.math.linear.AbstractRealMatrix; import org.apache.commons.math.linear.Array2DRowRealMatrix; import org.apache.commons.math.linear.RealMatrix; /** * * @author anjali */ public class MatrixMethods { public RealMatrix m; public MatrixMethods(RealMatrix m) { this.m = m; } public RealMatrix inverseMatrix() { RealMatrix inverse = new Array2DRowRealMatrix(m.getRowDimension(), m.getColumnDimension()); //RealMatrix inverse = new AbstractRealMatrix(m.getRowDimension(), m.getColumnDimension()); double[][] r = invert(m.getData()); int row = r[0].length; int col = r.length; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { inverse.setEntry(i, j, r[i][j]); } } return inverse; } public double determinant() { return determinant(m.getData(), m.getColumnDimension()); } public double determinant(double A[][], int N) { double det = 0; if (N == 1) { det = A[0][0]; } else if (N == 2) { det = A[0][0] * A[1][1] - A[1][0] * A[0][1]; } else { det = 0; for (int j1 = 0; j1 < N; j1++) { double[][] m = new double[N - 1][]; for (int k = 0; k < (N - 1); k++) { m[k] = new double[N - 1]; } for (int i = 1; i < N; i++) { int j2 = 0; for (int j = 0; j < N; j++) { if (j == j1) continue; m[i - 1][j2] = A[i][j]; j2++; } } det += Math.pow(-1.0, 1.0 + j1 + 1.0) * A[0][j1] * determinant(m, N - 1); } } return det; } public static double[][] invert(double a[][]) { int n = a.length; double x[][] = new double[n][n]; double b[][] = new double[n][n]; int index[] = new int[n]; for (int i = 0; i < n; ++i) b[i][i] = 1; upperTriangular(a, index); for (int i = 0; i < n - 1; ++i) { for (int j = i + 1; j < n; ++j) { for (int k = 0; k < n; ++k) { b[index[j]][k] -= a[index[j]][i] * b[index[i]][k]; } } } for (int i = 0; i < n; ++i) { x[n - 1][i] = b[index[n - 1]][i] / a[index[n - 1]][n - 1]; for (int j = n - 2; j >= 0; --j) { x[j][i] = b[index[j]][i]; for (int k = j + 1; k < n; ++k) { x[j][i] -= a[index[j]][k] * x[k][i]; } x[j][i] /= a[index[j]][j]; } } return x; } public static void upperTriangular(double a[][], int index[]) { int n = index.length; double c[] = new double[n]; for (int i = 0; i < n; ++i) index[i] = i; for (int i = 0; i < n; ++i) { double c1 = 0; for (int j = 0; j < n; ++j) { double c0 = Math.abs(a[i][j]); if (c0 > c1) c1 = c0; } c[i] = c1; } int k = 0; for (int j = 0; j < n - 1; ++j) { double pi1 = 0; for (int i = j; i < n; ++i) { double pi0 = Math.abs(a[index[i]][j]); pi0 /= c[index[i]]; if (pi0 > 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 < n; ++i) { double pj = a[index[i]][j] / a[index[j]][j]; a[index[i]][j] = pj; for (int l = j + 1; l < n; ++l) { a[index[i]][l] -= pj * a[index[j]][l]; } } } } public double trace() { double trace = 0; for (int i = 0; i < m.getRowDimension(); i++) { for (int j = 0; j < m.getColumnDimension(); j++) { if (j == i) { trace += m.getEntry(i, j); } } } return trace; } }