/** {@inheritDoc} */ @Override public void visit(final int row, final int column, final T value) { out.setEntry(column, row, value); } });
/** * Returns the matrix U of the decomposition. * <p>U is an upper-triangular matrix</p> * @return the U matrix (or null if decomposed matrix is singular) */ public FieldMatrix<T> getU() { if ((cachedU == null) && !singular) { final int m = pivot.length; cachedU = new Array2DRowFieldMatrix<T>(field, m, m); for (int i = 0; i < m; ++i) { final T[] luI = lu[i]; for (int j = i; j < m; ++j) { cachedU.setEntry(i, j, luI[j]); } } } return cachedU; }
/** * Returns the matrix L of the decomposition. * <p>L is a lower-triangular matrix</p> * @return the L matrix (or null if decomposed matrix is singular) */ public FieldMatrix<T> getL() { if ((cachedL == null) && !singular) { final int m = pivot.length; cachedL = new Array2DRowFieldMatrix<T>(field, m, m); for (int i = 0; i < m; ++i) { final T[] luI = lu[i]; for (int j = 0; j < i; ++j) { cachedL.setEntry(i, j, luI[j]); } cachedL.setEntry(i, i, field.getOne()); } } return cachedL; }
/** * Compute the outer product. * @param v vector with which outer product should be computed * @return the matrix outer product between instance and v */ public FieldMatrix<T> outerProduct(ArrayFieldVector<T> v) { final int m = data.length; final int n = v.data.length; final FieldMatrix<T> out = new Array2DRowFieldMatrix<T>(field, m, n); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { out.setEntry(i, j, data[i].multiply(v.data[j])); } } return out; }
/** * Returns a diagonal matrix with specified elements. * * @param <T> the type of the field elements * @param diagonal diagonal elements of the matrix (the array elements * will be copied) * @return diagonal matrix * @since 2.0 */ public static <T extends FieldElement<T>> FieldMatrix<T> createFieldDiagonalMatrix(final T[] diagonal) { final FieldMatrix<T> m = createFieldMatrix(diagonal[0].getField(), diagonal.length, diagonal.length); for (int i = 0; i < diagonal.length; ++i) { m.setEntry(i, i, diagonal[i]); } return m; }
/** * Returns the P rows permutation matrix. * <p>P is a sparse matrix with exactly one element set to 1.0 in * each row and each column, all other elements being set to 0.0.</p> * <p>The positions of the 1 elements are given by the {@link #getPivot() * pivot permutation vector}.</p> * @return the P rows permutation matrix (or null if decomposed matrix is singular) * @see #getPivot() */ public FieldMatrix<T> getP() { if ((cachedP == null) && !singular) { final int m = pivot.length; cachedP = new Array2DRowFieldMatrix<T>(field, m, m); for (int i = 0; i < m; ++i) { cachedP.setEntry(i, pivot[i], field.getOne()); } } return cachedP; }
/** {@inheritDoc} */ public FieldMatrix<T> getInverse() { final int m = pivot.length; final T one = field.getOne(); FieldMatrix<T> identity = new Array2DRowFieldMatrix<T>(field, m, m); for (int i = 0; i < m; ++i) { identity.setEntry(i, i, one); } return solve(identity); } }
/** {@inheritDoc} */ public FieldMatrix<T> getSubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); final FieldMatrix<T> subMatrix = createMatrix(endRow - startRow + 1, endColumn - startColumn + 1); for (int i = startRow; i <= endRow; ++i) { for (int j = startColumn; j <= endColumn; ++j) { subMatrix.setEntry(i - startRow, j - startColumn, getEntry(i, j)); } } return subMatrix; }
/** {@inheritDoc} */ public FieldMatrix<T> getColumnMatrix(final int column) throws OutOfRangeException { checkColumnIndex(column); final int nRows = getRowDimension(); final FieldMatrix<T> out = createMatrix(nRows, 1); for (int i = 0; i < nRows; ++i) { out.setEntry(i, 0, getEntry(i, column)); } return out; }
/** {@inheritDoc} */ public FieldMatrix<T> getRowMatrix(final int row) throws OutOfRangeException { checkRowIndex(row); final int nCols = getColumnDimension(); final FieldMatrix<T> out = createMatrix(1, nCols); for (int i = 0; i < nCols; ++i) { out.setEntry(0, i, getEntry(row, i)); } return out; }
/** {@inheritDoc} */ public FieldMatrix<T> scalarAdd(final T d) { 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).add(d)); } } return out; }
/** {@inheritDoc} */ public FieldMatrix<T> scalarMultiply(final T d) { 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).multiply(d)); } } return out; }
/** * Create a row {@link FieldMatrix} using the data from the input * array. * * @param <T> the type of the field elements * @param rowData the input row data * @return a 1 x rowData.length FieldMatrix * @throws NoDataException if {@code rowData} is empty. * @throws NullArgumentException if {@code rowData} is {@code null}. */ public static <T extends FieldElement<T>> FieldMatrix<T> createRowFieldMatrix(final T[] rowData) throws NoDataException, NullArgumentException { if (rowData == null) { throw new NullArgumentException(); } final int nCols = rowData.length; if (nCols == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } final FieldMatrix<T> m = createFieldMatrix(rowData[0].getField(), 1, nCols); for (int i = 0; i < nCols; ++i) { m.setEntry(0, i, rowData[i]); } return m; }
/** * Creates a column {@link FieldMatrix} using the data from the input * array. * * @param <T> the type of the field elements * @param columnData the input column data * @return a columnData x 1 FieldMatrix * @throws NoDataException if {@code data} is empty. * @throws NullArgumentException if {@code columnData} is {@code null}. */ public static <T extends FieldElement<T>> FieldMatrix<T> createColumnFieldMatrix(final T[] columnData) throws NoDataException, NullArgumentException { if (columnData == null) { throw new NullArgumentException(); } final int nRows = columnData.length; if (nRows == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } final FieldMatrix<T> m = createFieldMatrix(columnData[0].getField(), nRows, 1); for (int i = 0; i < nRows; ++i) { m.setEntry(i, 0, columnData[i]); } return m; }
/** {@inheritDoc} */ public FieldMatrix<T> outerProduct(FieldVector<T> v) { try { return outerProduct((ArrayFieldVector<T>) v); } catch (ClassCastException cce) { final int m = data.length; final int n = v.getDimension(); final FieldMatrix<T> out = new Array2DRowFieldMatrix<T>(field, m, n); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { out.setEntry(i, j, data[i].multiply(v.getEntry(j))); } } 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; }
/** {@inheritDoc} */ public FieldMatrix<T> add(FieldMatrix<T> m) throws MatrixDimensionMismatchException { // safety check checkAdditionCompatible(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).add(m.getEntry(row, col))); } } return out; }
/** {@inheritDoc} */ public FieldMatrix<T> outerProduct(FieldVector<T> v) { if (v instanceof SparseFieldVector<?>) { return outerProduct((SparseFieldVector<T>)v); } else { final int n = v.getDimension(); FieldMatrix<T> res = new SparseFieldMatrix<T>(field, virtualSize, n); OpenIntToFieldHashMap<T>.Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); int row = iter.key(); FieldElement<T>value = iter.value(); for (int col = 0; col < n; col++) { res.setEntry(row, col, value.multiply(v.getEntry(col))); } } return res; } }
/** {@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> scalarMultiply(final T d) { 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).multiply(d)); } } return out; }