/** {@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 FieldMatrix<T> preMultiply(final FieldMatrix<T> m) throws DimensionMismatchException { return m.multiply(this); }
/** {@inheritDoc} */ @Override public void visit(final int row, final int column, final T value) { out.setEntry(column, row, value); } });
/** * 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)); } } }
/** * 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()); } }
if (!matrix.isSquare()) { throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension()); final int m = matrix.getColumnDimension(); field = matrix.getField(); lu = matrix.getData(); pivot = new int[m]; cachedL = null;
resultMatrix = new BlockFieldMatrix<ExprFieldElement>(ExprField.CONST, matrix.getRowDimension(), matrix.getColumnDimension()); int min = matrix.getRowDimension(); if (min > matrix.getColumnDimension()) { min = matrix.getColumnDimension(); resultMatrix.setEntry(i, i, ExprField.CONST.getOne()); resultMatrix = resultMatrix.multiply(matrix);
/** {@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; }
/** {@inheritDoc} */ public FieldMatrix<T> subtract(final FieldMatrix<T> m) throws MatrixDimensionMismatchException { // safety check checkSubtractionCompatible(m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final FieldMatrix<T> out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col).subtract(m.getEntry(row, col))); } } return out; }
final int m = HBigFraction.getRowDimension(); H.setEntry(i, j, HBigFraction.getEntry(i, j).doubleValue());
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()); int r = rStart; for (int l = lStart; l < lEnd; ++l) { sum = sum.add(tBlock[l].multiply(m.getEntry(r, q))); ++r;
/** * Calculates the exact value of {@code P(D_n < d)} using the method described in [1] (reference * in class javadoc above) and {@link org.apache.commons.math3.fraction.BigFraction} (see * above). * * @param d statistic * @param n sample size * @return the two-sided probability of \(P(D_n < d)\) * @throws MathArithmeticException if algorithm fails to convert {@code h} to a * {@link org.apache.commons.math3.fraction.BigFraction} in expressing {@code d} as \((k * - h) / m\) for integer {@code k, m} and \(0 \le h < 1\). */ private double exactK(double d, int n) throws MathArithmeticException { final int k = (int) Math.ceil(n * d); final FieldMatrix<BigFraction> H = this.createExactH(d, n); final FieldMatrix<BigFraction> Hpower = H.power(n); BigFraction pFrac = Hpower.getEntry(k - 1, k - 1); for (int i = 1; i <= n; ++i) { pFrac = pFrac.multiply(i).divide(n); } /* * BigFraction.doubleValue converts numerator to double and the denominator to double and * divides afterwards. That gives NaN quite easy. This does not (scale is the number of * digits): */ return pFrac.bigDecimalValue(20, BigDecimal.ROUND_HALF_UP).doubleValue(); }
@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(); }
/** * Convert a {@link FieldMatrix}/{@link BigFraction} matrix to a {@link RealMatrix}. * * @param m Matrix to convert. * @return the converted matrix. */ public static Array2DRowRealMatrix bigFractionMatrixToRealMatrix(final FieldMatrix<BigFraction> m) { final BigFractionMatrixConverter converter = new BigFractionMatrixConverter(); m.walkInOptimizedOrder(converter); return converter.getConvertedMatrix(); }
for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { outBlock[k] = tBlock[k].add(m.getEntry(p, q)); ++k;
/** * Check if a matrix is multiplication compatible with the instance. * * @param m Matrix to check. * @throws DimensionMismatchException if the matrix is not * multiplication-compatible with instance. */ protected void checkMultiplicationCompatible(final FieldMatrix<T> m) throws DimensionMismatchException { if (getColumnDimension() != m.getRowDimension()) { throw new DimensionMismatchException(m.getRowDimension(), getColumnDimension()); } } }
/** Simple constructor. * @param field field to which the time and state vector elements belong * @param n number of steps of the multistep method * (excluding the one being computed) */ private AdamsNordsieckFieldTransformer(final Field<T> field, final int n) { this.field = field; final int rows = n - 1; // compute coefficients FieldMatrix<T> bigP = buildP(rows); FieldDecompositionSolver<T> pSolver = new FieldLUDecomposition<T>(bigP).getSolver(); T[] u = MathArrays.buildArray(field, rows); Arrays.fill(u, field.getOne()); c1 = pSolver.solve(new ArrayFieldVector<T>(u, false)).toArray(); // update coefficients are computed by combining transform from // Nordsieck to multistep, then shifting rows to represent step advance // then applying inverse transform T[][] shiftedP = bigP.getData(); for (int i = shiftedP.length - 1; i > 0; --i) { // shift rows shiftedP[i] = shiftedP[i - 1]; } shiftedP[0] = MathArrays.buildArray(field, rows); Arrays.fill(shiftedP[0], field.getZero()); update = new Array2DRowFieldMatrix<T>(pSolver.solve(new Array2DRowFieldMatrix<T>(shiftedP, false)).getData()); }
if (!matrix.isSquare()) { throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension()); final int m = matrix.getColumnDimension(); field = matrix.getField(); lu = matrix.getData(); pivot = new int[m]; cachedL = null;
/** * 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)); } } }