@Override public Vector precondition(Vector v) { return v.times(inverseDiagonal); }
@Override public Vector times(double x) { return delegate.times(x); }
@Override public Vector times(Vector x) { return delegate.times(x); }
@Override public Vector times(Vector x) { return delegate.times(x); }
@Override public Vector times(double x) { return delegate.times(x); }
private Vector localVOrtho(Vector v) { for (Vector old : localV) { if (old != null) { double x = v.dot(old); v = v.minus(old.times(x)); } } return v; }
@Override public Matrix cross(Vector other) { Matrix result = matrixLike(size, other.size()); Iterator<Vector.Element> it = iterateNonZero(); while (it.hasNext()) { Vector.Element e = it.next(); int row = e.index(); result.assignRow(row, other.times(getQuick(row))); } return result; }
@Override public Matrix timesRight(Matrix that) { if (that.numRows() != diagonal.size()) { throw new IllegalArgumentException("Incompatible number of rows in the right operand of matrix multiplication."); } Matrix m = that.like(); for (int row = 0; row < diagonal.size(); row++) { m.assignRow(row, that.viewRow(row).times(diagonal.getQuick(row))); } return m; }
@Override public Matrix timesLeft(Matrix that) { if (that.numCols() != diagonal.size()) { throw new IllegalArgumentException( "Incompatible number of rows in the left operand of matrix-matrix multiplication."); } Matrix m = that.like(); for (int col = 0; col < diagonal.size(); col++) { m.assignColumn(col, that.viewColumn(col).times(diagonal.getQuick(col))); } return m; }
/** Y' Cu p(u) */ private Matrix getYtransponseCuPu(Vector userRatings) { Preconditions.checkArgument(userRatings.isSequentialAccess(), "need sequential access to ratings!"); Vector YtransponseCuPu = new DenseVector(numFeatures); for (Element e : userRatings.nonZeroes()) { YtransponseCuPu.assign(Y.get(e.index()).times(confidence(e.get())), Functions.PLUS); } return columnVectorAsMatrix(YtransponseCuPu); }
u = A.times(v).minus(u.times(alpha)); beta = u.norm(2); if (beta > 0) { localVEnqueue(v); v = transposedA.times(u).minus(v.times(beta)); hbar = h.minus(hbar.times(thetabar * rho / (rhoold * rhobarold))); x.assign(hbar.times(zeta / (rho * rhobar)), Functions.PLUS); h = v.minus(h.times(thetanew / rho));
@Test(expected = CardinalityException.class) public void testTimesVectorCardinality() { test.times(new DenseVector(test.size() + 1)); }
@Test(expected = CardinalityException.class) public void testTimesVectorCardinality() { test.times(new DenseVector(test.size() + 1)); }
/** Y' (Cu - I) Y + λ I */ private Matrix getYtransponseCuMinusIYPlusLambdaI(Vector userRatings) { Preconditions.checkArgument(userRatings.isSequentialAccess(), "need sequential access to ratings!"); /* (Cu -I) Y */ OpenIntObjectHashMap<Vector> CuMinusIY = new OpenIntObjectHashMap<>(userRatings.getNumNondefaultElements()); for (Element e : userRatings.nonZeroes()) { CuMinusIY.put(e.index(), Y.get(e.index()).times(confidence(e.get()) - 1)); } Matrix YtransponseCuMinusIY = new DenseMatrix(numFeatures, numFeatures); /* Y' (Cu -I) Y by outer products */ for (Element e : userRatings.nonZeroes()) { for (Vector.Element feature : Y.get(e.index()).all()) { Vector partial = CuMinusIY.get(e.index()).times(feature.get()); YtransponseCuMinusIY.viewRow(feature.index()).assign(partial, Functions.PLUS); } } /* Y' (Cu - I) Y + λ I add lambda on the diagonal */ for (int feature = 0; feature < numFeatures; feature++) { YtransponseCuMinusIY.setQuick(feature, feature, YtransponseCuMinusIY.getQuick(feature, feature) + lambda); } return YtransponseCuMinusIY; }
@Test public void testTimesVector() throws Exception { Vector val = test.times(test); assertEquals("size", 3, val.size()); for (int i = 0; i < test.size(); i++) { assertEquals("get [" + i + ']', values[OFFSET + i] * values[OFFSET + i], val.get(i), EPSILON); } }
@Test public void testTimesDouble() throws Exception { Vector val = test.times(3); assertEquals("size", 3, val.size()); for (int i = 0; i < test.size(); i++) { assertEquals("get [" + i + ']', values[OFFSET + i] * 3, val.get(i), EPSILON); } }
@Test public void testTimesVector() { Vector val = test.times(test); assertEquals("size", test.size(), val.size()); for (int i = 0; i < test.size(); i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 0.0, val.get(i), EPSILON); } else { assertEquals("get [" + i + ']', values[i/2] * values[i/2], val.get(i), EPSILON); } } }
@Test public void testTimesDouble() { Vector val = test.times(3); assertEquals("size", test.size(), val.size()); for (int i = 0; i < test.size(); i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 0.0, val.get(i), EPSILON); } else { assertEquals("get [" + i + ']', values[i/2] * 3.0, val.get(i), EPSILON); } } }
assertEquals(0, dv1.plus(dv2).getDistanceSquared(sv1.plus(v2)), FUZZ); assertEquals(0, dv1.times(dv2).getDistanceSquared(v1.times(v2)), FUZZ); assertEquals(0, dv1.times(dv2).getDistanceSquared(v1.times(dv2)), FUZZ); assertEquals(0, dv1.times(dv2).getDistanceSquared(v1.times(sv2)), FUZZ); assertEquals(0, dv1.times(dv2).getDistanceSquared(sv1.times(v2)), FUZZ); assertEquals(0, dv1.times(z).getDistanceSquared(v1.times(z)), 1.0e-12); assertEquals(0, dv1.plus(z).getDistanceSquared(v1.plus(z)), 1.0e-12);
assertEquals("mutation via normalize(double) fails to change lengthSquared", expected, v.getLengthSquared(), EPSILON); v.times(2.0); expected = lengthSquaredSlowly(v); assertEquals("mutation via times(double) fails to change lengthSquared", expected, v.getLengthSquared(), EPSILON); v.times(v); expected = lengthSquaredSlowly(v); assertEquals("mutation via times(vector) fails to change lengthSquared", expected, v.getLengthSquared(), EPSILON);