コード例 #1
0
  @Test
  public void testMultiplyRandom() {
    Random r = new Random();
    DOK2Dint dok = new DOK2Dint(10 + r.nextInt(10), 10 + r.nextInt(10));

    // random matrix
    for (int l = 0; l < dok.nlines(); l++) {
      for (int c = 0; c < dok.ncols(); c++) {
        // 50% chance of random element
        if (r.nextBoolean()) {
          dok.set(l, c, r.nextInt());
        }
      }
    }

    DOK1Dint vec = new DOK1Dint(dok.ncols());

    // random vec
    for (int l = 0; l < vec.nlines(); l++) {
      if (r.nextBoolean()) {
        vec.set(l, r.nextInt());
      }
    }

    // expected result
    DOK1Dint expected = dok.multiply(vec);

    assertEquals(expected, new CSC2Dint(dok).multiply(vec));
  }
コード例 #2
0
  @Test
  public void testGet() {
    assertEquals(1, m.get(1, 1), 0);

    DOK2Dint dok = new DOK2Dint(10, 10);
    // first line == [0 1 2 3 4 5 ... ]
    for (int c = 0; c < dok.ncols(); c++) {
      dok.set(0, c, c);
    }
    // first col == [0 1.5 2.5 3.5 ...]
    for (int l = 1; l < dok.nlines(); l++) {
      dok.set(l, 0, (int) (l + 0.5));
    }

    Matrix2Dint m = new CSC2Dint(dok);
    for (int l = 0; l < m.nlines(); l++) {
      for (int c = 0; c < m.ncols(); c++) {
        // print line and column number on error
        assertArrayEquals(
            new Integer[] {(int) l, (int) c, dok.get(l, c)},
            new Integer[] {(int) l, (int) c, m.get(l, c)});
      }
    }
  }