Esempio n. 1
0
  /**
   * get a col sparse vector of a matrix
   *
   * @param col col id
   * @return a sparse vector of {index, value}
   */
  public SparseVector column(int col) {

    SparseVector sv = new SparseVector(numRows);

    if (isCCSUsed) {
      for (int j = colPtr[col]; j < colPtr[col + 1]; j++) {
        int row = rowInd[j];
        double val = get(row, col);
        if (val != 0.0) sv.set(row, val);
      }
    } else {
      for (int row = 0; row < numRows; row++) {
        double val = get(row, col);
        if (val != 0.0) sv.set(row, val);
      }
    }

    return sv;
  }
Esempio n. 2
0
  /**
   * get a row sparse vector of a matrix
   *
   * @param row row id
   * @return a sparse vector of {index, value}
   */
  public SparseVector row(int row) {

    SparseVector sv = new SparseVector(numColumns);

    for (int j = rowPtr[row]; j < rowPtr[row + 1]; j++) {
      int col = colInd[j];
      double val = get(row, col);
      if (val != 0.0) sv.set(col, val);
    }

    return sv;
  }
  @Test
  public void testGetRawData() {
    SparseVector vector = new SparseVector(Integer.MAX_VALUE);
    double[] data = vector.getRawData();
    assertTrue(data != null);
    assertTrue(data.length == 0);
    assertSame(data, vector.data);
    assertEquals(data.length, vector.getRawIndex().length);

    vector.set(2, 1.0);
    vector.set(1, 0.0);
    vector.set(4, 2.0);

    data = vector.getRawData();
    assertSame(data, vector.data);
    assertEquals(data.length, vector.getRawIndex().length);

    // In this case, the raw index is larger than the used, so the raw
    // indices have more entries than the other one.
    assertTrue(data.length > vector.getUsed());
    assertTrue(data.length > vector.getIndex().length);
  }
  /**
   * Unit test checking that the sparse vector does not end up ever using more than "size" elements.
   */
  @Test
  public void testOverAllocation() {
    for (int d = 0; d < 10; d++) {
      SparseVector v = new SparseVector(d, 0);
      assertEquals(0, v.index.length);
      assertEquals(0, v.data.length);

      // Fill with non-zero elements.
      for (int i = 0; i < d; i++) {
        v.set(i, 1.0 + i);
      }

      assertEquals(d, v.index.length);
      assertEquals(d, v.data.length);
    }
  }
Esempio n. 5
0
  @Override
  public Vector slice(int from, int to) {

    SparseVector res = new SparseVector();
    int fi = find(from);
    if (fi < 0) {
      fi = -fi - 1;
    }
    int ti = find(to);
    if (ti < 0) {
      ti = -ti - 1;
    }
    for (int idx = fi; idx < ti; idx++) {
      res.set(index(idx) - from, value(idx));
    }
    return res;
  }