📄 bigmatriximpl.java
字号:
* <p> * Column indices start at 0. A <code>MatrixIndexException</code> is thrown * unless <code>0 <= column < columnDimension.</code></p> * * @param col the column to be fetched * @return array of entries in the column * @throws MatrixIndexException if the specified column index is not valid */ public double[] getColumnAsDoubleArray(int col) throws MatrixIndexException { if ( !isValidCoordinate( 0, col ) ) { throw new MatrixIndexException("illegal column argument"); } int nrows = this.getRowDimension(); double[] out = new double[nrows]; for (int i=0;i<nrows;i++) { out[i] = data[i][col].doubleValue(); } return out; } /** * Returns the entry in the specified row and column. * <p> * Row and column indices start at 0 and must satisfy * <ul> * <li><code>0 <= row < rowDimension</code></li> * <li><code> 0 <= column < columnDimension</code></li> * </ul> * otherwise a <code>MatrixIndexException</code> is thrown.</p> * * @param row row location of entry to be fetched * @param column column location of entry to be fetched * @return matrix entry in row,column * @throws MatrixIndexException if the row or column index is not valid */ public BigDecimal getEntry(int row, int column) throws MatrixIndexException { if (!isValidCoordinate(row,column)) { throw new MatrixIndexException("matrix entry does not exist"); } return data[row][column]; } /** * Returns the entry in the specified row and column as a double. * <p> * Row and column indices start at 0 and must satisfy * <ul> * <li><code>0 <= row < rowDimension</code></li> * <li><code> 0 <= column < columnDimension</code></li> * </ul> * otherwise a <code>MatrixIndexException</code> is thrown.</p> * * @param row row location of entry to be fetched * @param column column location of entry to be fetched * @return matrix entry in row,column * @throws MatrixIndexException if the row * or column index is not valid */ public double getEntryAsDouble(int row, int column) throws MatrixIndexException { return getEntry(row,column).doubleValue(); } /** * Returns the transpose matrix. * * @return transpose matrix */ public BigMatrix transpose() { int nRows = this.getRowDimension(); int nCols = this.getColumnDimension(); BigMatrixImpl out = new BigMatrixImpl(nCols, nRows); BigDecimal[][] outData = out.getDataRef(); for (int row = 0; row < nRows; row++) { for (int col = 0; col < nCols; col++) { outData[col][row] = data[row][col]; } } return out; } /** * Returns the inverse matrix if this matrix is invertible. * * @return inverse matrix * @throws InvalidMatrixException if this is not invertible */ public BigMatrix inverse() throws InvalidMatrixException { return solve(MatrixUtils.createBigIdentityMatrix (this.getRowDimension())); } /** * Returns the determinant of this matrix. * * @return determinant * @throws InvalidMatrixException if matrix is not square */ public BigDecimal getDeterminant() throws InvalidMatrixException { if (!isSquare()) { throw new InvalidMatrixException("matrix is not square"); } if (isSingular()) { // note: this has side effect of attempting LU decomp if lu == null return ZERO; } else { BigDecimal det = (parity == 1) ? ONE : ONE.negate(); for (int i = 0; i < this.getRowDimension(); i++) { det = det.multiply(lu[i][i]); } return det; } } /** * Is this a square matrix? * @return true if the matrix is square (rowDimension = columnDimension) */ public boolean isSquare() { return (this.getColumnDimension() == this.getRowDimension()); } /** * Is this a singular matrix? * @return true if the matrix is singular */ public boolean isSingular() { if (lu == null) { try { luDecompose(); return false; } catch (InvalidMatrixException ex) { return true; } } else { // LU decomp must have been successfully performed return false; // so the matrix is not singular } } /** * Returns the number of rows in the matrix. * * @return rowDimension */ public int getRowDimension() { return data.length; } /** * Returns the number of columns in the matrix. * * @return columnDimension */ public int getColumnDimension() { return data[0].length; } /** * Returns the <a href="http://mathworld.wolfram.com/MatrixTrace.html"> * trace</a> of the matrix (the sum of the elements on the main diagonal). * * @return trace * * @throws IllegalArgumentException if this matrix is not square. */ public BigDecimal getTrace() throws IllegalArgumentException { if (!isSquare()) { throw new IllegalArgumentException("matrix is not square"); } BigDecimal trace = data[0][0]; for (int i = 1; i < this.getRowDimension(); i++) { trace = trace.add(data[i][i]); } return trace; } /** * Returns the result of multiplying this by the vector <code>v</code>. * * @param v the vector to operate on * @return this*v * @throws IllegalArgumentException if columnDimension != v.size() */ public BigDecimal[] operate(BigDecimal[] v) throws IllegalArgumentException { if (v.length != this.getColumnDimension()) { throw new IllegalArgumentException("vector has wrong length"); } int nRows = this.getRowDimension(); int nCols = this.getColumnDimension(); BigDecimal[] out = new BigDecimal[v.length]; for (int row = 0; row < nRows; row++) { BigDecimal sum = ZERO; for (int i = 0; i < nCols; i++) { sum = sum.add(data[row][i].multiply(v[i])); } out[row] = sum; } return out; } /** * Returns the result of multiplying this by the vector <code>v</code>. * * @param v the vector to operate on * @return this*v * @throws IllegalArgumentException if columnDimension != v.size() */ public BigDecimal[] operate(double[] v) throws IllegalArgumentException { BigDecimal bd[] = new BigDecimal[v.length]; for (int i=0;i<bd.length;i++) { bd[i] = new BigDecimal(v[i]); } return operate(bd); } /** * Returns the (row) vector result of premultiplying this by the vector <code>v</code>. * * @param v the row vector to premultiply by * @return v*this * @throws IllegalArgumentException if rowDimension != v.size() */ public BigDecimal[] preMultiply(BigDecimal[] v) throws IllegalArgumentException { int nRows = this.getRowDimension(); if (v.length != nRows) { throw new IllegalArgumentException("vector has wrong length"); } int nCols = this.getColumnDimension(); BigDecimal[] out = new BigDecimal[nCols]; for (int col = 0; col < nCols; col++) { BigDecimal sum = ZERO; for (int i = 0; i < nRows; i++) { sum = sum.add(data[i][col].multiply(v[i])); } out[col] = sum; } return out; } /** * Returns a matrix of (column) solution vectors for linear systems with * coefficient matrix = this and constant vectors = columns of * <code>b</code>. * * @param b array of constants forming RHS of linear systems to * to solve * @return solution array * @throws IllegalArgumentException if this.rowDimension != row dimension * @throws InvalidMatrixException if this matrix is not square or is singular */ public BigDecimal[] solve(BigDecimal[] b) throws IllegalArgumentException, InvalidMatrixException { int nRows = this.getRowDimension(); if (b.length != nRows) { throw new IllegalArgumentException("constant vector has wrong length"); } BigMatrix bMatrix = new BigMatrixImpl(b); BigDecimal[][] solution = ((BigMatrixImpl) (solve(bMatrix))).getDataRef(); BigDecimal[] out = new BigDecimal[nRows]; for (int row = 0; row < nRows; row++) { out[row] = solution[row][0]; } return out; } /** * Returns a matrix of (column) solution vectors for linear systems with * coefficient matrix = this and constant vectors = columns of * <code>b</code>. * * @param b array of constants forming RHS of linear systems to * to solve * @return solution array * @throws IllegalArgumentException if this.rowDimension != row dimension * @throws InvalidMatrixException if this matrix is not square or is singular */ public BigDecimal[] solve(double[] b) throws IllegalArgumentException, InvalidMatrixException { BigDecimal bd[] = new BigDecimal[b.length]; for (int i=0;i<bd.length;i++) { bd[i] = new BigDecimal(b[i]); } return solve(bd); } /** * Returns a matrix of (column) solution vectors for linear systems with * coefficient matrix = this and constant vectors = columns of * <code>b</code>. * * @param b matrix of constant vectors forming RHS of linear systems to * to solve * @return matrix of solution vectors * @throws IllegalArgumentException if this.rowDimension != row dimension * @throws InvalidMatrixException if this matrix is not square or is singular */ public BigMatrix solve(BigMatrix b) throws IllegalArgumentException, InvalidMatrixException { if (b.getRowDimension() != this.getRowDimension()) { throw new IllegalArgumentException("Incorrect row dimension"); } if (!this.isSquare()) { throw new InvalidMatrixException("coefficient matrix is not square"); } if (this.isSingular()) { // side effect: compute LU decomp throw new InvalidMatrixException("Matrix is singular."); } int nCol = this.getColumnDimension(); int nColB = b.getColumnDimension(); int nRowB = b.getRowDimension(); // Apply permutations to b BigDecimal[][] bp = new BigDecimal[nRowB][nColB]; for (int row = 0; row < nRowB; row++) { for (int col = 0; col < nColB; col++) { bp[row][col] = b.getEntry(permutation[row], col); } } // Solve LY = b for (int col = 0; col < nCol; col++) { for (int i = col + 1; i < nCol; i++) { for (int j = 0; j < nColB; j++) { bp[i][j] = bp[i][j].subtract(bp[col][j].multiply(lu[i][col])); } } } // Solve UX = Y for (int col = nCol - 1; col >= 0; col--) { for (int j = 0; j < nColB; j++) { bp[col][j] = bp[col][j].divide(lu[col][col], scale, roundingMode); } for (int i = 0; i < col; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -