コード例 #1
0
  public double elementSum() {
    double sum = 0d;

    MatrixCursor cursor = this.cursor();
    while (cursor.next()) {
      sum += cursor.val();
    }

    return sum;
  }
コード例 #2
0
  /*
   * simple generic implementation, can take advantage of sparsity structor
   * of this matrix, but not the given one.
   * @see org.genemania.engine.matricks.Matrix#elementMultiplySum(org.genemania.engine.matricks.Matrix)
   */
  public double elementMultiplySum(Matrix m) {
    double sum = 0d;

    MatrixCursor cursor = this.cursor();
    while (cursor.next()) {
      sum += cursor.val() * m.get(cursor.row(), cursor.col());
    }

    return sum;
  }
コード例 #3
0
  public Vector columnSums() throws MatricksException {
    Vector sums = new DenseDoubleVector(numCols());

    MatrixCursor cursor = cursor();
    while (cursor.next()) {
      sums.set(cursor.col(), cursor.val() + sums.get(cursor.col()));
    }

    return sums;
  }
コード例 #4
0
  public void add(double a, Matrix B) throws MatricksException {
    if (B.numRows() != this.numRows() && B.numCols() != this.numCols()) {
      throw new MatricksException("incompatible size for addition");
    }

    MatrixCursor cursor = B.cursor();
    while (cursor.next()) {
      final int row = cursor.row();
      final int col = cursor.col();
      double v = a * cursor.val() + this.get(row, col);
      this.set(row, col, v);
    }
  }
コード例 #5
0
  public Vector rowSums() throws MatricksException {
    // TODO: can change this to a lower level interface,
    // taking in an array of doubles and not allocating ... ?
    Vector sums = new DenseDoubleVector(numRows());

    // TODO: optimize out row() with local var? microopt, measure speed
    MatrixCursor cursor = cursor();
    while (cursor.next()) {
      sums.set(cursor.row(), cursor.val() + sums.get(cursor.row()));
    }

    return sums;
  }
コード例 #6
0
  /**
   * a <- max(a,a')
   *
   * @param a
   * @param b
   */
  public void setToMaxTranspose() throws MatricksException {
    //        throw new RuntimeException("not implemented");
    // TODO: bail if not square

    MatrixCursor cursor = cursor();
    while (cursor.next()) {
      double u = cursor.val();
      double v = get(cursor.col(), cursor.row());
      if (u > v) {
        set(cursor.col(), cursor.row(), u);
      } else if (v > u) {
        cursor.set(v);
      }
    }
  }
コード例 #7
0
  public void mmwrite(String filename) throws IOException {

    BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
    try {
      writer.write(MM_COORDINATE_MATRIX_HEADER + "\n");
      int nnz = nnz();
      writer.write(String.format("%d %d %d\n", numRows(), numCols(), nnz));

      MatrixCursor cursor = cursor();
      int total = 0;
      while (cursor.next()) {
        total += 1;
        writer.write(String.format("%d %d %f\n", cursor.row() + 1, cursor.col() + 1, cursor.val()));
      }

      // safety check for internal data consistency
      if (total != nnz) {
        throw new MatricksException(
            String.format("expected %d elements, only found %d", nnz, total));
      }
    } finally {
      writer.close();
    }
  }
コード例 #8
0
 public void setAll(final double a) throws MatricksException {
   MatrixCursor cursor = this.cursor();
   while (cursor.next()) {
     this.set(cursor.row(), cursor.col(), a);
   }
 }
コード例 #9
0
 /*
  * A = A ./ (x*x')
  */
 public void dotDivOuterProd(Vector x) {
   MatrixCursor mCursor = cursor();
   while (mCursor.next()) {
     mCursor.set(mCursor.val() / (x.get(mCursor.row()) * x.get(mCursor.col())));
   }
 }