@Override public double aggregate(Vector other, DoubleDoubleFunction aggregator, DoubleDoubleFunction combiner) { Preconditions.checkArgument(size == other.size(), "Vector sizes differ"); if (size == 0) { return 0; } return VectorBinaryAggregate.aggregateBest(this, other, aggregator, combiner); }
@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; }
public static void writeVector(DataOutput out, Vector vector, boolean laxPrecision) throws IOException { byte flags = flags(vector, laxPrecision); writeVectorFlagsAndSize(out, flags, vector.size()); writeVectorContents(out, vector, flags); }
@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); } }
public RandomAccessSparseVector(Vector other) { this(other.size(), other.getNumNondefaultElements()); for (Element e : other.nonZeroes()) { values.put(e.index(), e.get()); } }
@Test(expected = CardinalityException.class) public void testAssignDoubleArrayCardinality() { double[] array = new double[test.size() + 1]; test.assign(array); }
@Test public void testPlusDouble() { Vector val = test.plus(1); assertEquals("size", test.size(), val.size()); for (int i = 0; i < test.size(); i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 1.0, val.get(i), EPSILON); } else { assertEquals("get [" + i + ']', values[i/2] + 1.0, 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(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 testCopy() { Vector copy = test.clone(); for (int i = 0; i < test.size(); i++) { assertEquals("copy [" + i + ']', test.get(i), copy.get(i), EPSILON); } }
@Test public void testViewPart() { Vector part = test.viewPart(1, 2); assertEquals("part size", 2, part.getNumNondefaultElements()); for (int i = 0; i < part.size(); i++) { assertEquals("part[" + i + ']', test.get(i+1), part.get(i), EPSILON); } }
@Test public void testGetDistanceSquared() { Vector other = new RandomAccessSparseVector(test.size()); other.set(1, -2); other.set(2, -5); other.set(3, -9); other.set(4, 1); double expected = test.minus(other).getLengthSquared(); assertTrue("a.getDistanceSquared(b) != a.minus(b).getLengthSquared", Math.abs(expected - test.getDistanceSquared(other)) < 10.0E-7); }