예제 #1
0
  /** @return the cardinary of current matrix */
  public int size() {
    int size = 0;

    for (MatrixEntry me : this) if (me.get() != 0) size++;

    return size;
  }
예제 #2
0
  /**
   * Normalize the matrix entries to (0, 1) by (x-min)/(max-min)
   *
   * @param min minimum value
   * @param max maximum value
   */
  public void normalize(double min, double max) {
    assert max > min;

    for (MatrixEntry me : this) {
      double entry = me.get();
      if (entry != 0) me.set((entry - min) / (max - min));
    }
  }
예제 #3
0
  /** @return the data table of this matrix as (row, column, value) cells */
  public Table<Integer, Integer, Double> getDataTable() {
    Table<Integer, Integer, Double> res = HashBasedTable.create();

    for (MatrixEntry me : this) {
      if (me.get() != 0) res.put(me.row(), me.column(), me.get());
    }

    return res;
  }
예제 #4
0
  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(String.format("%d\t%d\t%d\n", new Object[] {numRows, numColumns, size()}));

    for (MatrixEntry me : this)
      if (me.get() != 0)
        sb.append(
            String.format("%d\t%d\t%f\n", new Object[] {me.row() + 1, me.column() + 1, me.get()}));

    return sb.toString();
  }
예제 #5
0
  /** @return the transpose of current matrix */
  public SparseMatrix transpose() {
    if (isCCSUsed) {
      SparseMatrix tr = new SparseMatrix(numColumns, numRows);

      tr.copyCRS(this.rowData, this.rowPtr, this.colInd);
      tr.copyCCS(this.colData, this.colPtr, this.rowInd);

      return tr;
    } else {
      Table<Integer, Integer, Double> dataTable = HashBasedTable.create();
      for (MatrixEntry me : this) dataTable.put(me.column(), me.row(), me.get());
      return new SparseMatrix(numColumns, numRows, dataTable);
    }
  }
예제 #6
0
  @Override
  public RQ factor(DenseMatrix A) {

    if (Q.numRows() != A.numRows())
      throw new IllegalArgumentException("Q.numRows() != A.numRows()");
    else if (Q.numColumns() != A.numColumns())
      throw new IllegalArgumentException("Q.numColumns() != A.numColumns()");
    else if (R == null) throw new IllegalArgumentException("R == null");

    /*
     * Calculate factorisation, and extract the triangular factor
     */
    intW info = new intW(0);
    LAPACK.getInstance().dgerqf(m, n, A.getData(), Matrices.ld(m), tau, work, work.length, info);

    if (info.val < 0) throw new IllegalArgumentException();

    R.zero();
    for (MatrixEntry e : A)
      if (e.column() >= (n - m) + e.row()) R.set(e.row(), e.column() - (n - m), e.get());

    /*
     * Generate the orthogonal matrix
     */
    info.val = 0;
    LAPACK
        .getInstance()
        .dorgrq(m, n, k, A.getData(), Matrices.ld(m), tau, workGen, workGen.length, info);

    if (info.val < 0) throw new IllegalArgumentException();

    Q.set(A);

    return this;
  }
 @Override
 void copy(Matrix A) {
   for (MatrixEntry e : A) if (e.row() >= e.column()) set(e.row(), e.column(), e.get());
 }
  @Test
  public void testIterator() throws Exception {
    File f = getSparseBinarySVDLIBCFile();
    Iterator<MatrixEntry> it = new SvdlibcSparseBinaryFileIterator(f);
    MatrixEntry me = it.next();
    // Col 0
    assertEquals(0, me.column());
    assertEquals(0, me.row());
    me = it.next();
    assertEquals(0, me.column());
    assertEquals(2, me.row());
    me = it.next();
    // Col 1
    assertEquals(1, me.column());
    assertEquals(1, me.row());
    me = it.next();
    // Col 2
    assertEquals(2, me.column());
    assertEquals(0, me.row());
    me = it.next();
    assertEquals(2, me.column());
    assertEquals(1, me.row());
    me = it.next();
    assertEquals(2, me.column());
    assertEquals(2, me.row());

    assertFalse(it.hasNext());
  }