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 scale(double alpha) {
    if (alpha == 0) return zero();
    else if (alpha == 1) return this;

    for (VectorEntry e : this) e.set(alpha * e.get());

    return this;
  }
  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;
  }
Example #5
0
  /**
   * Check whether an entry is set.
   *
   * @param entry The entry.
   * @return {@code true} if the entry is set in this vector.
   * @throws IllegalArgumentException if the entry is not from this vector or another vector sharing
   *     the same key domain. Only vectors and their side channels share key domains for the
   *     purposes of this check.
   */
  public boolean isSet(VectorEntry entry) {
    final SparseVector evec = entry.getVector();
    final int eind = entry.getIndex();

    if (evec == null) {
      throw new IllegalArgumentException("entry is not associated with a vector");
    } else if (!keys.isCompatibleWith(evec.keys)) {
      throw new IllegalArgumentException("entry does not have safe key domain");
    }
    assert entry.getKey() == keys.getKey(eind);
    return keys.indexIsActive(eind);
  }
  @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();
  }
 protected double norm2_robust() {
   double scale = 0, ssq = 1;
   for (VectorEntry e : this) {
     double xval = e.get();
     if (xval != 0) {
       double absxi = Math.abs(xval);
       if (scale < absxi) {
         ssq = 1 + ssq * Math.pow(scale / absxi, 2);
         scale = absxi;
       } else ssq = ssq + Math.pow(absxi / scale, 2);
     }
   }
   return scale * Math.sqrt(ssq);
 }
Example #8
0
  /**
   * Creates a new square matrix whose number of rows and columns match the dimensionality of the
   * given vector. It also places the values of the vector on the diagonal of the matrix.
   *
   * @param diagonal The vector of diagonal values.
   * @return A new, square matrix with the diagonal elements equal to the elements of the given
   *     vector.
   */
  public MatrixType createDiagonal(final Vectorizable diagonal) {
    final Vector vector = diagonal.convertToVector();
    final int dimensionality = vector.getDimensionality();

    // Create the matrix.
    final MatrixType result = this.createMatrix(dimensionality, dimensionality);

    // Set the diagonal values.
    for (VectorEntry entry : vector) {
      final int i = entry.getIndex();
      result.setElement(i, i, entry.getValue());
    }
    return result;
  }
Example #9
0
  /**
   * Get the value for the entry's key.
   *
   * @param entry A {@code VectorEntry} with the key to look up
   * @return the key's value
   * @throws IllegalArgumentException if the entry is unset, or if it is not from this vector or
   *     another vector sharing the same key domain. Only vectors and their side channels share key
   *     domains for the purposes of this check.
   */
  public double get(VectorEntry entry) {
    final SparseVector evec = entry.getVector();
    final int eind = entry.getIndex();

    if (evec == null) {
      throw new IllegalArgumentException("entry is not associated with a vector");
    } else if (!evec.keys.isCompatibleWith(keys)) {
      throw new IllegalArgumentException("entry does not have safe key domain");
    }
    assert entry.getKey() == keys.getKey(eind);
    if (keys.indexIsActive(eind)) {
      return values[eind];
    } else {
      throw new IllegalArgumentException("Key " + entry.getKey() + " is not set");
    }
  }
Example #10
0
 @Override
 @Nonnull
 public VectorEntry next() {
   int pos = iter.nextInt();
   boolean isSet = state == VectorEntry.State.SET || keys.indexIsActive(pos);
   double v = isSet ? values[pos] : Double.NaN;
   entry.set(pos, keys.getKey(pos), v, isSet);
   return entry;
 }
Example #11
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);
        }
      }
    }
  }
Example #12
0
  /**
   * Count the common keys between two vectors.
   *
   * @param o The other vector.
   * @return The number of keys appearing in both this and the other vector.
   */
  public int countCommonKeys(SparseVector o) {
    int count = 0;
    Iterator<VectorEntry> i1 = fastIterator();
    Iterator<VectorEntry> i2 = o.fastIterator();

    VectorEntry e1 = i1.hasNext() ? i1.next() : null;
    VectorEntry e2 = i2.hasNext() ? i2.next() : null;

    while (e1 != null && e2 != null) {
      final long k1 = e1.getKey();
      final long k2 = e2.getKey();
      if (k1 < k2) {
        e1 = i1.hasNext() ? i1.next() : null;
      } else if (k2 < k1) {
        e2 = i2.hasNext() ? i2.next() : null;
      } else {
        count += 1;
        e1 = i1.hasNext() ? i1.next() : null;
        e2 = i2.hasNext() ? i2.next() : null;
      }
    }
    return count;
  }
Example #13
0
  /**
   * Compute the dot product between two vectors.
   *
   * @param o The other vector.
   * @return The dot (inner) product between this vector and {@var o}.
   */
  public double dot(SparseVector o) {
    double dot = 0;

    Iterator<VectorEntry> i1 = fastIterator();
    Iterator<VectorEntry> i2 = o.fastIterator();

    VectorEntry e1 = i1.hasNext() ? i1.next() : null;
    VectorEntry e2 = i2.hasNext() ? i2.next() : null;

    while (e1 != null && e2 != null) {
      final long k1 = e1.getKey();
      final long k2 = e2.getKey();
      if (k1 < k2) {
        e1 = i1.hasNext() ? i1.next() : null;
      } else if (k2 < k1) {
        e2 = i2.hasNext() ? i2.next() : null;
      } else {
        dot += e1.getValue() * e2.getValue();
        e1 = i1.hasNext() ? i1.next() : null;
        e2 = i2.hasNext() ? i2.next() : null;
      }
    }
    return dot;
  }
Example #14
0
 @Override
 public Entry<Double> next() {
   VectorEntry e = delegate.next();
   return new BasicEntry<>(e.getKey(), (Double) e.getValue());
 }
 protected double norm1() {
   double sum = 0;
   for (VectorEntry e : this) sum += Math.abs(e.get());
   return sum;
 }
 protected double norm2() {
   double norm = 0;
   for (VectorEntry e : this) norm += e.get() * e.get();
   return Math.sqrt(norm);
 }
 protected double normInf() {
   double max = 0;
   for (VectorEntry e : this) max = Math.max(Math.abs(e.get()), max);
   return max;
 }
Example #18
0
 @Override
 public Long getKey() {
   return entry.getKey();
 }
Example #19
0
 @Override
 public Double getValue() {
   return entry.getValue();
 }
 public Vector zero() {
   for (VectorEntry e : this) e.set(0);
   return this;
 }