@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(); }
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; }
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); }
/** * 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); } } } }
protected double normInf() { double max = 0; for (VectorEntry e : this) max = Math.max(Math.abs(e.get()), max); return max; }
protected double norm2() { double norm = 0; for (VectorEntry e : this) norm += e.get() * e.get(); return Math.sqrt(norm); }
protected double norm1() { double sum = 0; for (VectorEntry e : this) sum += Math.abs(e.get()); return sum; }