/** {@inheritDoc} */ public void setRowMatrix(final int row, final FieldMatrix<T> matrix) throws OutOfRangeException, MatrixDimensionMismatchException { checkRowIndex(row); final int nCols = getColumnDimension(); if ((matrix.getRowDimension() != 1) || (matrix.getColumnDimension() != nCols)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, matrix.getEntry(0, i)); } }
/** * Generic copy constructor. * * @param other Instance to copy. */ public SparseFieldMatrix(FieldMatrix<T> other){ super(other.getField(), other.getRowDimension(), other.getColumnDimension()); rows = other.getRowDimension(); columns = other.getColumnDimension(); entries = new OpenIntToFieldHashMap<T>(getField()); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { setEntry(i, j, other.getEntry(i, j)); } } }
/** {@inheritDoc} */ public void setColumnMatrix(final int column, final FieldMatrix<T> matrix) throws OutOfRangeException, MatrixDimensionMismatchException { checkColumnIndex(column); final int nRows = getRowDimension(); if ((matrix.getRowDimension() != nRows) || (matrix.getColumnDimension() != 1)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), nRows, 1); } for (int i = 0; i < nRows; ++i) { setEntry(i, column, matrix.getEntry(i, 0)); } }
final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (m.getColumnDimension() != nCols || m.getRowDimension() != nRows) { return false;
/** * Check if a matrix is addition compatible with the instance. * * @param m Matrix to check. * @throws MatrixDimensionMismatchException if the matrix is not * addition-compatible with instance. */ protected void checkAdditionCompatible(final FieldMatrix<T> m) throws MatrixDimensionMismatchException { if ((getRowDimension() != m.getRowDimension()) || (getColumnDimension() != m.getColumnDimension())) { throw new MatrixDimensionMismatchException(m.getRowDimension(), m.getColumnDimension(), getRowDimension(), getColumnDimension()); } }
/** * Check if a matrix is subtraction compatible with the instance. * * @param m Matrix to check. * @throws MatrixDimensionMismatchException if the matrix is not * subtraction-compatible with instance. */ protected void checkSubtractionCompatible(final FieldMatrix<T> m) throws MatrixDimensionMismatchException { if ((getRowDimension() != m.getRowDimension()) || (getColumnDimension() != m.getColumnDimension())) { throw new MatrixDimensionMismatchException(m.getRowDimension(), m.getColumnDimension(), getRowDimension(), getColumnDimension()); } }
if (!matrix.isSquare()) { throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension()); final int m = matrix.getColumnDimension(); field = matrix.getField(); lu = matrix.getData();
final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), rows, m.getColumnDimension()); final T zero = getField().getZero(); final int qEnd = FastMath.min(qStart + BLOCK_SIZE, m.getColumnDimension());
/** {@inheritDoc} */ public FieldMatrix<T> multiply(final FieldMatrix<T> m) throws DimensionMismatchException { // safety check checkMultiplicationCompatible(m); final int nRows = getRowDimension(); final int nCols = m.getColumnDimension(); final int nSum = getColumnDimension(); final FieldMatrix<T> out = createMatrix(nRows, nCols); for (int row = 0; row < nRows; ++row) { for (int col = 0; col < nCols; ++col) { T sum = field.getZero(); for (int i = 0; i < nSum; ++i) { sum = sum.add(getEntry(row, i).multiply(m.getEntry(i, col))); } out.setEntry(row, col, sum); } } return out; }
final int nColB = b.getColumnDimension();
new Array2DRowFieldMatrix<T>(field, x.getRowDimension() - 1, x.getColumnDimension()); for (int i = 0; i < truncatedX.getRowDimension(); ++i) { for (int j = 0; j < truncatedX.getColumnDimension(); ++j) {
/** {@inheritDoc} */ public void setRowMatrix(final int row, final FieldMatrix<T> matrix) throws OutOfRangeException, MatrixDimensionMismatchException { checkRowIndex(row); final int nCols = getColumnDimension(); if ((matrix.getRowDimension() != 1) || (matrix.getColumnDimension() != nCols)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, matrix.getEntry(0, i)); } }
/** {@inheritDoc} */ public void setColumnMatrix(final int column, final FieldMatrix<T> matrix) throws OutOfRangeException, MatrixDimensionMismatchException { checkColumnIndex(column); final int nRows = getRowDimension(); if ((matrix.getRowDimension() != nRows) || (matrix.getColumnDimension() != 1)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), nRows, 1); } for (int i = 0; i < nRows; ++i) { setEntry(i, column, matrix.getEntry(i, 0)); } }
/** * Generic copy constructor. * * @param other Instance to copy. */ public SparseFieldMatrix(FieldMatrix<T> other){ super(other.getField(), other.getRowDimension(), other.getColumnDimension()); rows = other.getRowDimension(); columns = other.getColumnDimension(); entries = new OpenIntToFieldHashMap<T>(getField()); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { setEntry(i, j, other.getEntry(i, j)); } } }
final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (m.getColumnDimension() != nCols || m.getRowDimension() != nRows) { return false;
/** * Check if a matrix is addition compatible with the instance. * * @param m Matrix to check. * @throws MatrixDimensionMismatchException if the matrix is not * addition-compatible with instance. */ protected void checkAdditionCompatible(final FieldMatrix<T> m) throws MatrixDimensionMismatchException { if ((getRowDimension() != m.getRowDimension()) || (getColumnDimension() != m.getColumnDimension())) { throw new MatrixDimensionMismatchException(m.getRowDimension(), m.getColumnDimension(), getRowDimension(), getColumnDimension()); } }
/** * Check if a matrix is subtraction compatible with the instance. * * @param m Matrix to check. * @throws MatrixDimensionMismatchException if the matrix is not * subtraction-compatible with instance. */ protected void checkSubtractionCompatible(final FieldMatrix<T> m) throws MatrixDimensionMismatchException { if ((getRowDimension() != m.getRowDimension()) || (getColumnDimension() != m.getColumnDimension())) { throw new MatrixDimensionMismatchException(m.getRowDimension(), m.getColumnDimension(), getRowDimension(), getColumnDimension()); } }
/** {@inheritDoc} */ public FieldMatrix<T> multiply(final FieldMatrix<T> m) throws DimensionMismatchException { // safety check checkMultiplicationCompatible(m); final int nRows = getRowDimension(); final int nCols = m.getColumnDimension(); final int nSum = getColumnDimension(); final FieldMatrix<T> out = createMatrix(nRows, nCols); for (int row = 0; row < nRows; ++row) { for (int col = 0; col < nCols; ++col) { T sum = field.getZero(); for (int i = 0; i < nSum; ++i) { sum = sum.add(getEntry(row, i).multiply(m.getEntry(i, col))); } out.setEntry(row, col, sum); } } return out; }
/** * Converts a FieldMatrix to the list expression representation. * * @param matrix * @return */ public static IAST matrix2List(final FieldMatrix<ExprFieldElement> matrix) { if (matrix == null) { return null; } final int rowSize = matrix.getRowDimension(); final int colSize = matrix.getColumnDimension(); final IAST out = F.List(); IAST currOutRow; for (int i = 0; i < rowSize; i++) { currOutRow = F.List(); out.add(currOutRow); for (int j = 0; j < colSize; j++) { IExpr expr = matrix.getEntry(i, j).getExpr(); if (expr instanceof INumber) { currOutRow.add(expr); } else { currOutRow.add(F.eval(F.Together(expr))); } } } out.addEvalFlags(IAST.IS_MATRIX); return out; }
@Override public ExprFieldElement matrixEval(final FieldMatrix<ExprFieldElement> matrix) { if (matrix.getRowDimension() == 2 && matrix.getColumnDimension() == 2) { // 2x2 matrix ExprFieldElement[] row1 = matrix.getRow(0); ExprFieldElement[] row2 = matrix.getRow(1); return row1[0].multiply(row2[1]).subtract((row1[1].multiply(row2[0]))); } if (matrix.getRowDimension() == 3 && matrix.getColumnDimension() == 3) { // 3x3 matrix ExprFieldElement[] row1 = matrix.getRow(0); ExprFieldElement[] row2 = matrix.getRow(1); ExprFieldElement[] row3 = matrix.getRow(2); return row1[0].multiply(row2[1].multiply(row3[2])).subtract( (row1[0].multiply(row2[2].multiply(row3[1])))).subtract( (row1[1].multiply(row2[0].multiply(row3[2])))).add( (row1[1].multiply(row2[2].multiply(row3[0])))).add( (row1[2].multiply(row2[0].multiply(row3[1])))).subtract( (row1[2].multiply(row2[1].multiply(row3[0])))); } final FieldLUDecomposition<ExprFieldElement> lu = new FieldLUDecomposition<ExprFieldElement>(matrix); return lu.getDeterminant(); }