📄 bigmatriximpl.java
字号:
* <p> * Makes a fresh copy of the underlying data.</p> * * @return 2-dimensional array of entries */ public BigDecimal[][] getData() { return copyOut(); } /** * Returns matrix entries as a two-dimensional array. * <p> * Makes a fresh copy of the underlying data converted to * <code>double</code> values.</p> * * @return 2-dimensional array of entries */ public double[][] getDataAsDoubleArray() { int nRows = getRowDimension(); int nCols = getColumnDimension(); double d[][] = new double[nRows][nCols]; for (int i = 0; i < nRows; i++) { for (int j=0; j<nCols;j++) { d[i][j] = data[i][j].doubleValue(); } } return d; } /** * Returns a reference to the underlying data array. * <p> * Does not make a fresh copy of the underlying data.</p> * * @return 2-dimensional array of entries */ public BigDecimal[][] getDataRef() { return data; } /*** * Gets the rounding mode for division operations * The default is {@link java.math.BigDecimal#ROUND_HALF_UP} * @see BigDecimal * @return the rounding mode. */ public int getRoundingMode() { return roundingMode; } /*** * Sets the rounding mode for decimal divisions. * @see BigDecimal * @param roundingMode rounding mode for decimal divisions */ public void setRoundingMode(int roundingMode) { this.roundingMode = roundingMode; } /*** * Sets the scale for division operations. * The default is 64 * @see BigDecimal * @return the scale */ public int getScale() { return scale; } /*** * Sets the scale for division operations. * @see BigDecimal * @param scale scale for division operations */ public void setScale(int scale) { this.scale = scale; } /** * Returns the <a href="http://mathworld.wolfram.com/MaximumAbsoluteRowSumNorm.html"> * maximum absolute row sum norm</a> of the matrix. * * @return norm */ public BigDecimal getNorm() { BigDecimal maxColSum = ZERO; for (int col = 0; col < this.getColumnDimension(); col++) { BigDecimal sum = ZERO; for (int row = 0; row < this.getRowDimension(); row++) { sum = sum.add(data[row][col].abs()); } maxColSum = maxColSum.max(sum); } return maxColSum; } /** * Gets a submatrix. Rows and columns are indicated * counting from 0 to n-1. * * @param startRow Initial row index * @param endRow Final row index * @param startColumn Initial column index * @param endColumn Final column index * @return The subMatrix containing the data of the * specified rows and columns * @exception MatrixIndexException if row or column selections are not valid */ public BigMatrix getSubMatrix(int startRow, int endRow, int startColumn, int endColumn) throws MatrixIndexException { if (startRow < 0 || startRow > endRow || endRow > data.length || startColumn < 0 || startColumn > endColumn || endColumn > data[0].length ) { throw new MatrixIndexException( "invalid row or column index selection"); } BigMatrixImpl subMatrix = new BigMatrixImpl(endRow - startRow+1, endColumn - startColumn+1); BigDecimal[][] subMatrixData = subMatrix.getDataRef(); for (int i = startRow; i <= endRow; i++) { for (int j = startColumn; j <= endColumn; j++) { subMatrixData[i - startRow][j - startColumn] = data[i][j]; } } return subMatrix; } /** * Gets a submatrix. Rows and columns are indicated * counting from 0 to n-1. * * @param selectedRows Array of row indices must be non-empty * @param selectedColumns Array of column indices must be non-empty * @return The subMatrix containing the data in the * specified rows and columns * @exception MatrixIndexException if supplied row or column index arrays * are not valid */ public BigMatrix getSubMatrix(int[] selectedRows, int[] selectedColumns) throws MatrixIndexException { if (selectedRows.length * selectedColumns.length == 0) { throw new MatrixIndexException( "selected row and column index arrays must be non-empty"); } BigMatrixImpl subMatrix = new BigMatrixImpl(selectedRows.length, selectedColumns.length); BigDecimal[][] subMatrixData = subMatrix.getDataRef(); try { for (int i = 0; i < selectedRows.length; i++) { for (int j = 0; j < selectedColumns.length; j++) { subMatrixData[i][j] = data[selectedRows[i]][selectedColumns[j]]; } } } catch (ArrayIndexOutOfBoundsException e) { throw new MatrixIndexException("matrix dimension mismatch"); } return subMatrix; } /** * Replace the submatrix starting at <code>row, column</code> using data in * the input <code>subMatrix</code> array. Indexes are 0-based. * <p> * Example:<br> * Starting with <pre> * 1 2 3 4 * 5 6 7 8 * 9 0 1 2 * </pre> * and <code>subMatrix = {{3, 4} {5,6}}</code>, invoking * <code>setSubMatrix(subMatrix,1,1))</code> will result in <pre> * 1 2 3 4 * 5 3 4 8 * 9 5 6 2 * </pre></p> * * @param subMatrix array containing the submatrix replacement data * @param row row coordinate of the top, left element to be replaced * @param column column coordinate of the top, left element to be replaced * @throws MatrixIndexException if subMatrix does not fit into this * matrix from element in (row, column) * @throws IllegalArgumentException if <code>subMatrix</code> is not rectangular * (not all rows have the same length) or empty * @throws NullPointerException if <code>subMatrix</code> is null * @since 1.1 */ public void setSubMatrix(BigDecimal[][] subMatrix, int row, int column) throws MatrixIndexException { if ((row < 0) || (column < 0)){ throw new MatrixIndexException ("invalid row or column index selection"); } int nRows = subMatrix.length; if (nRows == 0) { throw new IllegalArgumentException( "Matrix must have at least one row."); } int nCols = subMatrix[0].length; if (nCols == 0) { throw new IllegalArgumentException( "Matrix must have at least one column."); } for (int r = 1; r < nRows; r++) { if (subMatrix[r].length != nCols) { throw new IllegalArgumentException( "All input rows must have the same length."); } } if (data == null) { if ((row > 0)||(column > 0)) throw new MatrixIndexException ("matrix must be initialized to perfom this method"); data = new BigDecimal[nRows][nCols]; System.arraycopy(subMatrix, 0, data, 0, subMatrix.length); } if (((nRows + row) > this.getRowDimension()) || (nCols + column > this.getColumnDimension())) throw new MatrixIndexException( "invalid row or column index selection"); for (int i = 0; i < nRows; i++) { System.arraycopy(subMatrix[i], 0, data[row + i], column, nCols); } lu = null; } /** * Returns the entries in row number <code>row</code> * as a row matrix. Row indices start at 0. * * @param row the row to be fetched * @return row matrix * @throws MatrixIndexException if the specified row index is invalid */ public BigMatrix getRowMatrix(int row) throws MatrixIndexException { if ( !isValidCoordinate( row, 0)) { throw new MatrixIndexException("illegal row argument"); } int ncols = this.getColumnDimension(); BigDecimal[][] out = new BigDecimal[1][ncols]; System.arraycopy(data[row], 0, out[0], 0, ncols); return new BigMatrixImpl(out); } /** * Returns the entries in column number <code>column</code> * as a column matrix. Column indices start at 0. * * @param column the column to be fetched * @return column matrix * @throws MatrixIndexException if the specified column index is invalid */ public BigMatrix getColumnMatrix(int column) throws MatrixIndexException { if ( !isValidCoordinate( 0, column)) { throw new MatrixIndexException("illegal column argument"); } int nRows = this.getRowDimension(); BigDecimal[][] out = new BigDecimal[nRows][1]; for (int row = 0; row < nRows; row++) { out[row][0] = data[row][column]; } return new BigMatrixImpl(out); } /** * Returns the entries in row number <code>row</code> as an array. * <p> * Row indices start at 0. A <code>MatrixIndexException</code> is thrown * unless <code>0 <= row < rowDimension.</code></p> * * @param row the row to be fetched * @return array of entries in the row * @throws MatrixIndexException if the specified row index is not valid */ public BigDecimal[] getRow(int row) throws MatrixIndexException { if ( !isValidCoordinate( row, 0 ) ) { throw new MatrixIndexException("illegal row argument"); } int ncols = this.getColumnDimension(); BigDecimal[] out = new BigDecimal[ncols]; System.arraycopy(data[row], 0, out, 0, ncols); return out; } /** * Returns the entries in row number <code>row</code> as an array * of double values. * <p> * Row indices start at 0. A <code>MatrixIndexException</code> is thrown * unless <code>0 <= row < rowDimension.</code></p> * * @param row the row to be fetched * @return array of entries in the row * @throws MatrixIndexException if the specified row index is not valid */ public double[] getRowAsDoubleArray(int row) throws MatrixIndexException { if ( !isValidCoordinate( row, 0 ) ) { throw new MatrixIndexException("illegal row argument"); } int ncols = this.getColumnDimension(); double[] out = new double[ncols]; for (int i=0;i<ncols;i++) { out[i] = data[row][i].doubleValue(); } return out; } /** * Returns the entries in column number <code>col</code> as an array. * <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 BigDecimal[] getColumn(int col) throws MatrixIndexException { if ( !isValidCoordinate(0, col) ) { throw new MatrixIndexException("illegal column argument"); } int nRows = this.getRowDimension(); BigDecimal[] out = new BigDecimal[nRows]; for (int i = 0; i < nRows; i++) { out[i] = data[i][col]; } return out; } /** * Returns the entries in column number <code>col</code> as an array * of double values.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -