/** 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());
 }
Exemple #2
0
  /**
   * 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;
  }
 /** 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);
     }
   }
 }