/** * Returns the transpose matrix. * * @return transpose matrix */ public BigMatrix transpose() { final int nRows = this.getRowDimension(); final int nCols = this.getColumnDimension(); final BigDecimal[][] outData = new BigDecimal[nCols][nRows]; for (int row = 0; row < nRows; row++) { final BigDecimal[] dataRow = data[row]; for (int col = 0; col < nCols; col++) { outData[col][row] = dataRow[col]; } } return new BigMatrixImpl(outData, false); }
/** * Create a new BigMatrix using <code>d</code> as the underlying * data array. * <p>The input array is copied, not referenced. This constructor has * the same effect as calling {@link #BigMatrixImpl(BigDecimal[][], boolean)} * with the second argument set to <code>true</code>.</p> * * @param d data for new matrix * @throws IllegalArgumentException if <code>d</code> is not rectangular * (not all rows have the same length) or empty * @throws NullPointerException if <code>d</code> is null */ public BigMatrixImpl(BigDecimal[][] d) { this.copyIn(d); lu = null; }
/** * Create a new BigMatrix which is a copy of this. * * @return the cloned matrix */ public BigMatrix copy() { return new BigMatrixImpl(this.copyOut(), false); }
/** * Is this a square matrix? * @return true if the matrix is square (rowDimension = columnDimension) */ public boolean isSquare() { return (this.getColumnDimension() == 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 NonSquareMatrixException(getRowDimension(), getColumnDimension()); } 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 < getRowDimension(); i++) { det = det.multiply(lu[i][i]); } return det; } }
/** * 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; }
/** * Compute this minus <code>m</code>. * * @param m matrix to be subtracted * @return this + m * @throws IllegalArgumentException if m is not the same size as this */ public BigMatrix subtract(BigMatrix m) throws IllegalArgumentException { try { return subtract((BigMatrixImpl) m); } catch (ClassCastException cce) { // safety check MatrixUtils.checkSubtractionCompatible(this, m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final BigDecimal[][] outData = new BigDecimal[rowCount][columnCount]; for (int row = 0; row < rowCount; row++) { final BigDecimal[] dataRow = data[row]; final BigDecimal[] outDataRow = outData[row]; for (int col = 0; col < columnCount; col++) { outDataRow[col] = dataRow[col].subtract(getEntry(row, col)); } } return new BigMatrixImpl(outData, false); } }
/** * 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; }
final int nRows = this.getRowDimension(); final int nCols = this.getColumnDimension(); if (nRows != nCols) { throw new NonSquareMatrixException(getRowDimension(), getColumnDimension()); lu = this.getData();
/** * Returns a {@link BigMatrix} whose entries are the the values in the * the input array. The input array is copied, not referenced. * * @param data input array * @return RealMatrix containing the values of the array * @throws IllegalArgumentException if <code>data</code> is not rectangular * (not all rows have the same length) or empty * @throws NullPointerException if data is null */ public static BigMatrix createBigMatrix(BigDecimal[][] data) { return new BigMatrixImpl(data); }
/** * 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 NonSquareMatrixException(getRowDimension(), getColumnDimension()); } BigDecimal trace = data[0][0]; for (int i = 1; i < this.getRowDimension(); i++) { trace = trace.add(data[i][i]); } return trace; }
/** * 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; } }
/** * 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 <code>dimension x dimension</code> identity matrix. * * @param dimension dimension of identity matrix to generate * @return identity matrix * @throws IllegalArgumentException if dimension is not positive * @since 1.1 */ public static BigMatrix createBigIdentityMatrix(int dimension) { BigMatrixImpl out = new BigMatrixImpl(dimension, dimension); BigDecimal[][] d = out.getDataRef(); for (int row = 0; row < dimension; row++) { for (int col = 0; col < dimension; col++) { d[row][col] = row == col ? BigMatrixImpl.ONE : BigMatrixImpl.ZERO; } } return out; }
/** * 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 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 { MatrixUtils.checkRowIndex(this, row); final int ncols = this.getColumnDimension(); final BigDecimal[][] out = new BigDecimal[1][ncols]; System.arraycopy(data[row], 0, out[0], 0, ncols); return new BigMatrixImpl(out, false); }
/** * 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 { MatrixUtils.checkColumnIndex(this, column); final int nRows = this.getRowDimension(); final BigDecimal[][] out = new BigDecimal[nRows][1]; for (int row = 0; row < nRows; row++) { out[row][0] = data[row][column]; } return new BigMatrixImpl(out, false); }
/** * 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 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 { MatrixUtils.checkRowIndex(this, row); final int ncols = this.getColumnDimension(); final BigDecimal[] out = new BigDecimal[ncols]; System.arraycopy(data[row], 0, out, 0, ncols); return out; }
/** * Returns matrix entries as a two-dimensional array. * <p> * Makes a fresh copy of the underlying data.</p> * * @return 2-dimensional array of entries */ public BigDecimal[][] getData() { return copyOut(); }