List of usage examples for org.apache.commons.math3.linear MatrixUtils checkMultiplicationCompatible
public static void checkMultiplicationCompatible(final AnyMatrix left, final AnyMatrix right) throws DimensionMismatchException
From source file:lirmm.inria.fr.math.BigSparseRealMatrix.java
/** * Postmultiply this matrix by {@code m}. * * @param m Matrix to postmultiply by.//from w w w .j a v a2 s .c om * @return {@code this} * {@code m}. * @throws DimensionMismatchException if the number of rows of {@code m} * differ from the number of columns of {@code this} matrix. * @throws NumberIsTooLargeException if the total number of entries of the * product is larger than {@code Integer.MAX_VALUE}. */ public BigSparseRealMatrix multiply(BigSparseRealMatrix m) throws DimensionMismatchException, NumberIsTooLargeException { // Safety check. MatrixUtils.checkMultiplicationCompatible(this, m); final int outCols = m.getColumnDimension(); BigSparseRealMatrix out = new BigSparseRealMatrix(getRowDimension(), outCols); for (OpenLongToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) { iterator.advance(); final double value = iterator.value(); final long key = iterator.key(); final int i, k; if (isTransposed) { k = (int) (key / columns); i = (int) (key % columns); } else { i = (int) (key / columns); k = (int) (key % columns); } for (int j = 0; j < outCols; ++j) { final long rightKey; if (m.isTransposed) { rightKey = m.computeKey(j, k); } else { rightKey = m.computeKey(k, j); } if (m.entries.containsKey(rightKey)) { final long outKey = out.computeKey(i, j); final double outValue = out.entries.get(outKey) + value * m.entries.get(rightKey); if (outValue == 0.0) { out.entries.remove(outKey); } else { out.entries.put(outKey, outValue); } } } } return out; }
From source file:lirmm.inria.fr.math.BigSparseRealMatrix.java
/** * {@inheritDoc}/*from w w w.j ava2 s.c o m*/ * * @throws NumberIsTooLargeException if {@code m} is an * {@code OpenMapRealMatrix}, and the total number of entries of the product * is larger than {@code Integer.MAX_VALUE}. */ @Override public RealMatrix multiply(final RealMatrix m) throws DimensionMismatchException, NumberIsTooLargeException { try { return multiply((BigSparseRealMatrix) m); } catch (ClassCastException cce) { MatrixUtils.checkMultiplicationCompatible(this, m); final int outCols = m.getColumnDimension(); final BigSparseRealMatrix out = new BigSparseRealMatrix(rows, outCols); for (OpenLongToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) { iterator.advance(); final double value = iterator.value(); final long key = iterator.key(); final int i, k; if (isTransposed) { k = (int) (key / columns); i = (int) (key % columns); } else { i = (int) (key / columns); k = (int) (key % columns); } for (int j = 0; j < outCols; ++j) { out.addToEntry(i, j, value * m.getEntry(k, j)); } } return out; } }
From source file:lirmm.inria.fr.math.BigSparseRealMatrix.java
/** * Subtract {@code A*B} from this matrix. * * @param A Latent feature matrix 1/* ww w. ja v a 2 s.c o m*/ * @param B Latent feature matrix 2 * @return {@code this} - {@code A} * {@code B}. */ public BigSparseRealMatrix specialOperation(BigSparseRealMatrix A, BigSparseRealMatrix B) { MatrixUtils.checkMultiplicationCompatible(A, B); MatrixUtils.checkAdditionCompatible(this, new BigSparseRealMatrix(A.getRowDimension(), B.getColumnDimension())); BigSparseRealMatrix out = new BigSparseRealMatrix(getRowDimension(), getColumnDimension()); for (OpenLongToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) { iterator.advance(); final double value = iterator.value(); final long key = iterator.key(); final int i, j; if (isTransposed) { j = (int) (key / columns); i = (int) (key % columns); } else { i = (int) (key / columns); j = (int) (key % columns); } double v = value - A.getRowVector(i).dotProduct(B.getColumnVector(j)); out.setEntry(i, j, v); } return out; }