/** * Returns the result of postmultiplying this by m. * * @param m matrix to postmultiply by * @return this * m * @throws IllegalArgumentException if columnDimension(this) != rowDimension(m) */ public OpenMapRealMatrix multiply(OpenMapRealMatrix m) throws IllegalArgumentException { // safety check MatrixUtils.checkMultiplicationCompatible(this, m); final int outCols = m.getColumnDimension(); OpenMapRealMatrix out = new OpenMapRealMatrix(rowDimension, outCols); for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext(); ) { iterator.advance(); final double value = iterator.value(); final int key = iterator.key(); final int i = key / columnDimension; final int k = key % columnDimension; for (int j = 0; j < outCols; ++j) { final int rightKey = m.computeKey(k, j); if (m.entries.containsKey(rightKey)) { final int 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; }
private OpenMapRealMatrix createSparseMatrix(double[][] data) { OpenMapRealMatrix matrix = new OpenMapRealMatrix(data.length, data[0].length); for (int row = 0; row < data.length; row++) { for (int col = 0; col < data[row].length; col++) { matrix.setEntry(row, col, data[row][col]); } } return matrix; }
/** test add failure */ public void testAddFail() { OpenMapRealMatrix m = createSparseMatrix(testData); OpenMapRealMatrix m2 = createSparseMatrix(testData2); try { m.add(m2); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException ex) { // ignored } }
/** test m-n = m + -n */ public void testPlusMinus() { OpenMapRealMatrix m = createSparseMatrix(testData); OpenMapRealMatrix n = createSparseMatrix(testDataInv); assertClose("m-n = m + -n", m.subtract(n), n.scalarMultiply(-1d).add(m), entryTolerance); try { m.subtract(createSparseMatrix(testData2)); fail("Expecting illegalArgumentException"); } catch (IllegalArgumentException ex) { // ignored } }
/** * Optimized method to compute the outer product. * * @param v The vector to comput the outer product on * @return The outer product of <code>this</code> and <code>v</code> * @throws IllegalArgumentException If the dimensions don't match */ public OpenMapRealMatrix outerproduct(OpenMapRealVector v) throws IllegalArgumentException { checkVectorDimensions(v.getDimension()); OpenMapRealMatrix res = new OpenMapRealMatrix(virtualSize, virtualSize); Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); Iterator iter2 = v.getEntries().iterator(); while (iter2.hasNext()) { iter2.advance(); res.setEntry(iter.key(), iter2.key(), iter.value() * iter2.value()); } } return res; }
/** * 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 OpenMapRealMatrix subtract(OpenMapRealMatrix m) throws IllegalArgumentException { // safety check MatrixUtils.checkAdditionCompatible(this, m); final OpenMapRealMatrix out = new OpenMapRealMatrix(this); for (OpenIntToDoubleHashMap.Iterator iterator = m.entries.iterator(); iterator.hasNext(); ) { iterator.advance(); final int row = iterator.key() / columnDimension; final int col = iterator.key() - row * columnDimension; out.setEntry(row, col, getEntry(row, col) - iterator.value()); } return out; }
/** test dimensions */ public void testDimensions() { OpenMapRealMatrix m = createSparseMatrix(testData); OpenMapRealMatrix m2 = createSparseMatrix(testData2); assertEquals("testData row dimension", 3, m.getRowDimension()); assertEquals("testData column dimension", 3, m.getColumnDimension()); assertTrue("testData is square", m.isSquare()); assertEquals("testData2 row dimension", m2.getRowDimension(), 2); assertEquals("testData2 column dimension", m2.getColumnDimension(), 3); assertTrue("testData2 is not square", !m2.isSquare()); }
/** test copy functions */ public void testCopyFunctions() { OpenMapRealMatrix m1 = createSparseMatrix(testData); RealMatrix m2 = m1.copy(); assertEquals(m1.getClass(), m2.getClass()); assertEquals((m2), m1); OpenMapRealMatrix m3 = createSparseMatrix(testData); RealMatrix m4 = m3.copy(); assertEquals(m3.getClass(), m4.getClass()); assertEquals((m4), m3); }
public void testPremultiply() { RealMatrix m3 = createSparseMatrix(d3); RealMatrix m4 = createSparseMatrix(d4); RealMatrix m5 = createSparseMatrix(d5); assertClose("m3*m4=m5", m4.preMultiply(m3), m5, entryTolerance); OpenMapRealMatrix m = createSparseMatrix(testData); OpenMapRealMatrix mInv = createSparseMatrix(testDataInv); OpenMapRealMatrix identity = createSparseMatrix(id); assertClose("inverse multiply", m.preMultiply(mInv), identity, entryTolerance); assertClose("inverse multiply", mInv.preMultiply(m), identity, entryTolerance); assertClose("identity multiply", m.preMultiply(identity), m, entryTolerance); assertClose("identity multiply", identity.preMultiply(mInv), mInv, entryTolerance); try { m.preMultiply(createSparseMatrix(bigSingular)); fail("Expecting illegalArgumentException"); } catch (IllegalArgumentException ex) { // ignored } }
/** test add */ public void testAdd() { OpenMapRealMatrix m = createSparseMatrix(testData); OpenMapRealMatrix mInv = createSparseMatrix(testDataInv); OpenMapRealMatrix mDataPlusInv = createSparseMatrix(testDataPlusInv); RealMatrix mPlusMInv = m.add(mInv); for (int row = 0; row < m.getRowDimension(); row++) { for (int col = 0; col < m.getColumnDimension(); col++) { assertEquals( "sum entry entry", mDataPlusInv.getEntry(row, col), mPlusMInv.getEntry(row, col), entryTolerance); } } }
public void testSetSubMatrix() throws Exception { OpenMapRealMatrix m = createSparseMatrix(testData); m.setSubMatrix(detData2, 1, 1); RealMatrix expected = createSparseMatrix(new double[][] {{1.0, 2.0, 3.0}, {2.0, 1.0, 3.0}, {1.0, 2.0, 4.0}}); assertEquals(expected, m); m.setSubMatrix(detData2, 0, 0); expected = createSparseMatrix(new double[][] {{1.0, 3.0, 3.0}, {2.0, 4.0, 3.0}, {1.0, 2.0, 4.0}}); assertEquals(expected, m); m.setSubMatrix(testDataPlus2, 0, 0); expected = createSparseMatrix(new double[][] {{3.0, 4.0, 5.0}, {4.0, 7.0, 5.0}, {3.0, 2.0, 10.0}}); assertEquals(expected, m); // javadoc example OpenMapRealMatrix matrix = createSparseMatrix(new double[][] {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 0, 1, 2}}); matrix.setSubMatrix(new double[][] {{3, 4}, {5, 6}}, 1, 1); expected = createSparseMatrix(new double[][] {{1, 2, 3, 4}, {5, 3, 4, 8}, {9, 5, 6, 2}}); assertEquals(expected, matrix); // dimension overflow try { m.setSubMatrix(testData, 1, 1); fail("expecting MatrixIndexException"); } catch (MatrixIndexException e) { // expected } // dimension underflow try { m.setSubMatrix(testData, -1, 1); fail("expecting MatrixIndexException"); } catch (MatrixIndexException e) { // expected } try { m.setSubMatrix(testData, 1, -1); fail("expecting MatrixIndexException"); } catch (MatrixIndexException e) { // expected } // null try { m.setSubMatrix(null, 1, 1); fail("expecting NullPointerException"); } catch (NullPointerException e) { // expected } try { new OpenMapRealMatrix(0, 0); fail("expecting IllegalArgumentException"); } catch (IllegalArgumentException e) { // expected } // ragged try { m.setSubMatrix(new double[][] {{1}, {2, 3}}, 0, 0); fail("expecting IllegalArgumentException"); } catch (IllegalArgumentException e) { // expected } // empty try { m.setSubMatrix(new double[][] {{}}, 0, 0); fail("expecting IllegalArgumentException"); } catch (IllegalArgumentException e) { // expected } }
public void testToString() { OpenMapRealMatrix m = createSparseMatrix(testData); assertEquals("OpenMapRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}", m.toString()); m = new OpenMapRealMatrix(1, 1); assertEquals("OpenMapRealMatrix{{0.0}}", m.toString()); }
public void testEqualsAndHashCode() { OpenMapRealMatrix m = createSparseMatrix(testData); OpenMapRealMatrix m1 = m.copy(); OpenMapRealMatrix mt = (OpenMapRealMatrix) m.transpose(); assertTrue(m.hashCode() != mt.hashCode()); assertEquals(m.hashCode(), m1.hashCode()); assertEquals(m, m); assertEquals(m, m1); assertFalse(m.equals(null)); assertFalse(m.equals(mt)); assertFalse(m.equals(createSparseMatrix(bigSingular))); }
/** test multiply */ public void testMultiply() { OpenMapRealMatrix m = createSparseMatrix(testData); OpenMapRealMatrix mInv = createSparseMatrix(testDataInv); OpenMapRealMatrix identity = createSparseMatrix(id); OpenMapRealMatrix m2 = createSparseMatrix(testData2); assertClose("inverse multiply", m.multiply(mInv), identity, entryTolerance); assertClose( "inverse multiply", m.multiply(new BlockRealMatrix(testDataInv)), identity, entryTolerance); assertClose("inverse multiply", mInv.multiply(m), identity, entryTolerance); assertClose("identity multiply", m.multiply(identity), m, entryTolerance); assertClose("identity multiply", identity.multiply(mInv), mInv, entryTolerance); assertClose("identity multiply", m2.multiply(identity), m2, entryTolerance); try { m.multiply(createSparseMatrix(bigSingular)); fail("Expecting illegalArgumentException"); } catch (IllegalArgumentException ex) { // ignored } }
/** test norm */ public void testNorm() { OpenMapRealMatrix m = createSparseMatrix(testData); OpenMapRealMatrix m2 = createSparseMatrix(testData2); assertEquals("testData norm", 14d, m.getNorm(), entryTolerance); assertEquals("testData2 norm", 7d, m2.getNorm(), entryTolerance); }