public double dot(Vector y) {
    checkSize(y);

    double ret = 0;
    for (VectorEntry e : this) ret += e.get() * y.get(e.index());
    return ret;
  }
  public Vector add(double alpha, Vector y) {
    checkSize(y);

    if (alpha == 0) return this;

    for (VectorEntry e : y) add(e.index(), alpha * e.get());

    return this;
  }
  public Vector set(double alpha, Vector y) {
    checkSize(y);

    if (alpha == 0) return zero();

    zero();
    for (VectorEntry e : y) set(e.index(), alpha * e.get());

    return this;
  }
  @Override
  public String toString() {
    // Output into coordinate format. Indices start from 1 instead of 0
    Formatter out = new Formatter();

    out.format("%10d %19d\n", size, Matrices.cardinality(this));

    for (VectorEntry e : this)
      if (e.get() != 0) out.format("%10d % .12e\n", e.index() + 1, e.get());

    return out.toString();
  }
Exemplo n.º 5
0
  /**
   * Standardize the matrix entries by row- or column-wise z-scores (z=(x-u)/sigma)
   *
   * @param isByRow standardize by row if true; otherwise by column
   */
  public void standardize(boolean isByRow) {

    int iters = isByRow ? numRows : numColumns;
    for (int iter = 0; iter < iters; iter++) {
      SparseVector vec = isByRow ? row(iter) : column(iter);

      if (vec.getCount() > 0) {

        double[] data = vec.getData();
        double mu = Stats.mean(data);
        double sigma = Stats.sd(data, mu);

        for (VectorEntry ve : vec) {
          int idx = ve.index();
          double val = ve.get();
          double z = (val - mu) / sigma;

          if (isByRow) this.set(iter, idx, z);
          else this.set(idx, iter, z);
        }
      }
    }
  }