📄 matrix.java
字号:
// assign the input matrix to lower triangular matrix // l_a.assign(this_a); l_a.m_d.sqrt(); l_a.changeType(Integral::LOWER_TRIANGULAR); } // type: FULL, SYMMETRIC, LOWER_TRIANGULAR, UPPER_TRIANGULAR, SPARSE // else { */ // get number of rows of current matrix // int nrows = getNumRows(); // make a copy of the current matrix // Matrix a_M = new Matrix(); a_M.copyMatrix(this); // loop over all rows // for (int i = 0; i < nrows; i++) { // constructs a lower triangular matrix directly iterating // over columns. remember an upper triangular matrix is // the transpose matrix of a lower triangular matrix in // our storage format. // for (int j = i; j < nrows; j++) { // accumulate the sum // double sum = a_M.getValue(i, j); for (int k = i - 1; k >= 0; k--) { sum -= a_M.getValue(i, k) * a_M.getValue(j, k); } // handle diagonal elements separately // if (i == j) { // if the sum is not greater than zero, the matrix is // not positive definite // if (sum <= 0.0) { // System.out.println("decompositionCholesky()"); return false; } // assign the value // a_M.setValue(i, i, Math.sqrt(sum)); } // other elements are handled with the normal calculation // else { a_M.setValue(j, i, sum / a_M.getValue(i, i)); } } } // copy the lower triangular part of the result matrix into l_a and // set the diagonal elements of l_a matrix to (TIntegral)1 // l_a.initMatrixValue(nrows, nrows, 0.0, LOWER_TRIANGULAR); l_a.copyLowerMatrix(a_M); return true; } /** * This routine computes the inverse matrix using the gauss jordan method * see numerical recipes, the are of scientific computing, second edition, * cambridge university press. pages 36 - 41 * * @param a input matrix as 2-d double array * @param n number of rows and columns in the input matrix * @param b output matrix as 2-d double array * @param m number of rows and columns in the output matrix */ public void gaussj(double a[][], int n, double b[][], int m) { int indxc[] = null; int indxr[] = null; int ipiv[] = null; int i = 0; int icol = 0; int irow = 0; int j = 0; int k = 0; int l = 0; int ll = 0; double big = 0; double dum = 0; double pivinv = 0; double tmp = 0; indxc = new int[n]; indxr = new int[n]; ipiv = new int[n]; for (j = 0; j < n; j++) ipiv[j] = 0; for (i = 0; i < n; i++) { big = 0.0; for (j = 0; j < n; j++) if (ipiv[j] != 1) for (k = 0; k < n; k++) { if (ipiv[k] == 0) { if (Math.abs(a[j][k]) >= big) { big = Math.abs(a[j][k]); irow = j; icol = k; } } else if (ipiv[k] > 1) { return; } } ++(ipiv[icol]); if (irow != icol) { for (l = 0; l < n; l++) { tmp = a[irow][l]; a[irow][l] = a[icol][l]; a[icol][l] = tmp; } for (l = 0; l < m; l++) { tmp = b[irow][l]; b[irow][l] = b[icol][l]; b[icol][l] = tmp; } } indxr[i] = irow; indxc[i] = icol; if (a[icol][icol] == 0.0) { return; } pivinv = 1.0 / a[icol][icol]; a[icol][icol] = 1.0; for (l = 0; l < n; l++) a[icol][l] *= pivinv; for (l = 0; l < m; l++) b[icol][l] *= pivinv; for (ll = 0; ll < n; ll++) if (ll != icol) { dum = a[ll][icol]; a[ll][icol] = 0.0; for (l = 0; l < n; l++) a[ll][l] -= a[icol][l] * dum; for (l = 0; l < m; l++) b[ll][l] -= b[icol][l] * dum; } } for (l = n - 1; l >= 0; l--) { if (indxr[l] != indxc[l]) { for (k = 0; k < n; k++) { tmp = a[k][indxr[l]]; a[k][indxr[l]] = a[k][indxc[l]]; a[k][indxc[l]] = tmp; } } } } /** * this routine computes the inverse of a matrix * * @param A inverse matrix */ public void invertMatrix(Matrix A) { if ((A == null) || (row != col)) { return; } // declare a unit matrix // Matrix T = new Matrix(); T.Elem = new double[row][row]; T.row = T.col = row; T.identityMatrix(); // allocate memory // A.row = A.col = row; A.Elem = new double[row][row]; // make a local copy of the matrix // A.copyMatrix(this); // find the inverse of the matrix // gaussj(A.Elem, row, T.Elem, row); } /** * This routine takes a matrix as an argument and copies it to the * the matrix object that invoked it * */ public void resetMatrix() { for(int i = 0; i < row; i++ ) { for(int j = 0; j < col; j++) { Elem[i][j] = 0.0; } } } /** * This routine adds two matrices * * @param A matrix to be added to object evoked * */ public void addMatrix(Matrix A) { if (A == null) { return; } if( (row != A.row) || (col != A.col) ) { return; } else { for(int i = 0; i < row; i++ ) { for(int j = 0; j < col; j++) { Elem[i][j] += A.Elem[i][j]; } } } } /** * This routine subtracts two matrices * * @param A input matrix * @param B difference matrix */ public void subtractMatrix(Matrix A, Matrix B) { if(A == null || B == null) { return; } if( (row != A.row) || (col != A.col) ) { return; } else { B.row = row; B.col = col; B.Elem = new double[row][col]; for(int i = 0; i < row; i++ ) { for(int j = 0; j < col; j++) { B.Elem[i][j] = Elem[i][j] - A.Elem[i][j]; } } } } /** * This routine multiplys a matrix with a scalar * * @param scalar matrix double scalar value * */ public void scalarMultMatrix(double scalar) { for(int i = 0; i < row; i++ ) { for(int j = 0; j < col; j++) { Elem[i][j] *= scalar; } } } /** * This routine takes the square root of the matrix object * */ public void normMatrix() { for(int i = 0; i < row; i++ ) { for(int j = 0; j < col; j++) { Elem[i][j] = Math.sqrt(Elem[i][j]); } } } /** * This routine takes every element to its exponent with Math.exp() * */ public void expMatrix() { for(int i = 0; i < row; i++ ) { for(int j = 0; j < col; j++) { Elem[i][j] = Math.exp(Elem[i][j]); } } } /** * This routine inverses every element of the matrix * */ public void inverseMatrixElements() { for(int i = 0; i < row; i++ ) { for(int j = 0; j < col; j++) { Elem[i][j] = 1/Elem[i][j]; } } } /** * This routine adds a double value to every element in a matrix * * @param val_a double value to be added to every element */ public void addMatrixElements(double val_a) { for(int i = 0; i < row; i++ ) { for(int j = 0; j < col; j++) { Elem[i][j] += val_a; } } } /** * This routine multiplys two matrices * * @param A input matrix * @param B product matrix */ public void multMatrix(Matrix A, Matrix B) { if(A == null || B == null) { // System.out.println(" A == null || B == null"); Exception e = new Exception(); e.printStackTrace(); return; } if(col != A.row ) { // System.out.println(" col != A.row "); Exception e = new Exception(); e.printStackTrace(); return; } else { B.row = row; B.col = A.col; B.Elem = new double[row][A.col]; for(int i = 0; i < B.row; i++ ) { for(int j = 0; j < B.col; j++) { B.Elem[i][j] = 0.0; for(int k = 0; k < col; k++ ) { B.Elem[i][j] += Elem[i][k] * A.Elem[k][j]; } } } } } /** * This routine performs the transpose of a matrix * * @param B matrix transpose * */ public void transposeMatrix(Matrix B) { if(B == null) { return; } B.row = col; B.col = row; B.Elem = new double[col][row]; for(int i = 0; i < row; i++ ) { for(int j = 0; j < col; j++) { B.Elem[j][i] = Elem[i][j]; } } } /** * Prints the matrix contents to STDOUT * */ public void printMatrix() { for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { // System.out.print(Elem[i][j] + " " ); } // System.out.print("\n"); } // System.out.print("\n"); } /** * Puts the DoubleVector contents in a vector and calls printMatrix() * * @param vec_a Vector to be printed */ public static void printDoubleVector(Vector<Double> vec_a) { Matrix temp_M = new Matrix(); temp_M.initMatrix(vec_a); temp_M.printMatrix(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -