⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 realmatriximpl.java

📁 Apache的common math数学软件包
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        double[][] 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(double[][] 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 double[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 RealMatrix getRowMatrix(int row) throws MatrixIndexException {        if ( !isValidCoordinate( row, 0)) {            throw new MatrixIndexException("illegal row argument");        }        int ncols = this.getColumnDimension();        double[][] out = new double[1][ncols];         System.arraycopy(data[row], 0, out[0], 0, ncols);        return new RealMatrixImpl(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 RealMatrix getColumnMatrix(int column) throws MatrixIndexException {        if ( !isValidCoordinate( 0, column)) {            throw new MatrixIndexException("illegal column argument");        }        int nRows = this.getRowDimension();        double[][] out = new double[nRows][1];         for (int row = 0; row < nRows; row++) {            out[row][0] = data[row][column];        }        return new RealMatrixImpl(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 double[] getRow(int row) throws MatrixIndexException {        if ( !isValidCoordinate( row, 0 ) ) {            throw new MatrixIndexException("illegal row argument");        }        int ncols = this.getColumnDimension();        double[] out = new double[ncols];        System.arraycopy(data[row], 0, out, 0, ncols);        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 double[] getColumn(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 row = 0; row < nRows; row++) {            out[row] = data[row][col];        }        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 double 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 transpose matrix.     *     * @return transpose matrix     */    public RealMatrix transpose() {        int nRows = this.getRowDimension();        int nCols = this.getColumnDimension();        RealMatrixImpl out = new RealMatrixImpl(nCols, nRows);        double[][] 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 RealMatrix inverse() throws InvalidMatrixException {        return solve(MatrixUtils.createRealIdentityMatrix                (this.getRowDimension()));    }    /**     * @return determinant     * @throws InvalidMatrixException if matrix is not square     */    public double 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 0d;        } else {            double det = parity;            for (int i = 0; i < this.getRowDimension(); i++) {                det *= lu[i][i];            }            return det;        }    }    /**     * @return true if the matrix is square (rowDimension = columnDimension)     */    public boolean isSquare() {        return (this.getColumnDimension() == this.getRowDimension());    }    /**     * @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        }    }    /**     * @return rowDimension     */    public int getRowDimension() {        return data.length;    }    /**     * @return columnDimension     */    public int getColumnDimension() {        return data[0].length;    }    /**     * @return trace     * @throws IllegalArgumentException if the matrix is not square     */    public double getTrace() throws IllegalArgumentException {        if (!isSquare()) {            throw new IllegalArgumentException("matrix is not square");        }        double trace = data[0][0];        for (int i = 1; i < this.getRowDimension(); i++) {            trace += data[i][i];        }        return trace;    }    /**     * @param v vector to operate on     * @throws IllegalArgumentException if columnDimension != v.length     * @return resulting vector     */    public double[] operate(double[] v) throws IllegalArgumentException {        if (v.length != this.getColumnDimension()) {            throw new IllegalArgumentException("vector has wrong length");        }        int nRows = this.getRowDimension();        int nCols = this.getColumnDimension();        double[] out = new double[v.length];        for (int row = 0; row < nRows; row++) {            double sum = 0;            for (int i = 0; i < nCols; i++) {                sum += data[row][i] * v[i];            }            out[row] = sum;        }        return out;    }    /**     * @param v vector to premultiply by     * @throws IllegalArgumentException if rowDimension != v.length     * @return resulting matrix     */    public double[] preMultiply(double[] v) throws IllegalArgumentException {        int nRows = this.getRowDimension();        if (v.length != nRows) {            throw new IllegalArgumentException("vector has wrong length");        }        int nCols = this.getColumnDimension();        double[] out = new double[nCols];        for (int col = 0; col < nCols; col++) {            double sum = 0;            for (int i = 0; i < nRows; i++) {                sum += data[i][col] * 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 constant 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 double[] solve(double[] b) throws IllegalArgumentException, InvalidMatrixException {        int nRows = this.getRowDimension();        if (b.length != nRows) {            throw new IllegalArgumentException("constant vector has wrong length");        }        RealMatrix bMatrix = new RealMatrixImpl(b);        double[][] solution = ((RealMatrixImpl) (solve(bMatrix))).getDataRef();        double[] out = new double[nRows];        for (int row = 0; row < nRows; row++) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -