@Override public Vector assign(Vector other) { delegate.assign(other); return this; }
@Override public Vector assign(DoubleFunction function) { delegate.assign(function); return this; }
@Override public Vector assign(Vector other, DoubleDoubleFunction function) { delegate.assign(other, function); return this; }
@SuppressWarnings("CloneDoesntCallSuperClone") @Override public Vector clone() { Vector v = new DenseVector(size()); v.assign(this, Functions.PLUS); return v; }
private static Vector createOptimizedCopy(Vector vector) { Vector result; if (vector.isDense()) { result = vector.like().assign(vector, Functions.SECOND_LEFT_ZERO); } else { result = vector.clone(); } return result; }
@Override public Vector times(Vector that) { if (size != that.size()) { throw new CardinalityException(size, that.size()); } if (this.getNumNondefaultElements() <= that.getNumNondefaultElements()) { return createOptimizedCopy(this).assign(that, Functions.MULT); } else { return createOptimizedCopy(that).assign(this, Functions.MULT); } }
@Override public Vector plus(Vector that) { if (size != that.size()) { throw new CardinalityException(size, that.size()); } return createOptimizedCopy().assign(that, Functions.PLUS); }
private static Vector randomVector() { Vector v = new DenseVector(20); v.assign(new DoubleFunction() { private final Random gen = RandomUtils.getRandom(); @Override public double apply(double arg1) { return gen.nextDouble(); } }); return v; } }
@Test(expected = CardinalityException.class) public void testAssignDoubleArrayCardinality() { double[] array = new double[test.size() + 1]; test.assign(array); }
@Test(expected = CardinalityException.class) public void testAssignVectorCardinality() { Vector other = new DenseVector(test.size() - 1); test.assign(other); }
@Test(expected = CardinalityException.class) public void testAssignVectorCardinality() { Vector other = new DenseVector(test.size() - 1); test.assign(other); }
@Test public void testAssignDoubleArray() { double[] array = new double[test.size()]; test.assign(array); for (int i = 0; i < values.length; i++) { assertEquals("value[" + i + ']', 0.0, test.getQuick(i), EPSILON); } }
@Test public void testAssignDouble() { test.assign(0); for (int i = 0; i < test.size(); i++) { assertEquals("value[" + i + ']', 0.0, test.getQuick(i), EPSILON); } }
@Test public void testAssignBinaryFunction2() throws Exception { test.assign(Functions.PLUS, 4); for (int i = 0; i < test.size(); i++) { assertEquals("value[" + i + ']', values[i + 1] + 4, test.getQuick(i), EPSILON); } }
@Test public void testAssignBinaryFunction3() throws Exception { test.assign(new TimesFunction(), 4); for (int i = 0; i < test.size(); i++) { assertEquals("value[" + i + ']', values[i + 1] * 4, test.getQuick(i), EPSILON); } }
@Test public void testAssignBinaryFunction() { test.assign(test, Functions.PLUS); for (int i = 0; i < values.length; i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 0.0, test.get(i), EPSILON); } else { assertEquals("value[" + i + ']', 2 * values[i - 1], test.getQuick(i), EPSILON); } } }
@Test public void testAssignBinaryFunction2() { test.assign(Functions.plus(4)); for (int i = 0; i < values.length; i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 4.0, test.get(i), EPSILON); } else { assertEquals("value[" + i + ']', values[i - 1] + 4, test.getQuick(i), EPSILON); } } }
@Test public void testAssignBinaryFunction3() { test.assign(Functions.mult(4)); for (int i = 0; i < values.length; i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 0.0, test.get(i), EPSILON); } else { assertEquals("value[" + i + ']', values[i - 1] * 4, test.getQuick(i), EPSILON); } } }
@Test public void testViewRow() { Vector row = test.viewRow(1); assertEquals("row size", 2, row.getNumNondefaultElements()); //create a matrix with an unassigned row 0 Matrix matrix = new SparseMatrix(1, 1); Vector view = matrix.viewRow(0); final double value = 1.23; view.assign(value); //test whether the update in the view is reflected in the matrix assertEquals("Matrix value", view.getQuick(0), matrix.getQuick(0, 0), EPSILON); }
@Test public void randomMatrix() { Matrix a = new DenseMatrix(60, 60).assign(Functions.random()); QRDecomposition qr = new QRDecomposition(a); // how close is Q to actually being orthornormal? double maxIdent = qr.getQ().transpose().times(qr.getQ()).viewDiagonal().assign(Functions.plus(-1)).norm(1); assertEquals(0, maxIdent, 1.0e-13); // how close is Q R to the original value of A? Matrix z = qr.getQ().times(qr.getR()).minus(a); double maxError = z.aggregate(Functions.MIN, Functions.ABS); assertEquals(0, maxError, 1.0e-13); }