/** * Return the value at the given index, without checking bounds * * @param index an int index * @return the double at the index */ @Override public double getQuick(int index) { return vector.getQuick(pivot[index]); }
static Matrix createMiIi(Iterable<Vector> featureVectors, int numFeatures) { double[][] MiIi = new double[numFeatures][Iterables.size(featureVectors)]; int n = 0; for (Vector featureVector : featureVectors) { for (int m = 0; m < numFeatures; m++) { MiIi[m][n] = featureVector.getQuick(m); } n++; } return new DenseMatrix(MiIi, true); }
@Override public double getQuick(int row, int column) { Vector r = rowVectors.get(row); return r == null ? 0.0 : r.getQuick(column); }
@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 Vector assign(Vector x, Vector y, DoubleDoubleFunction f) { for (Element xe : x.nonZeroes()) { xe.set(f.apply(xe.get(), y.getQuick(xe.index()))); } return x; } }
@Override public Vector assign(Vector x, Vector y, DoubleDoubleFunction f) { for (Element ye : y.nonZeroes()) { x.setQuick(ye.index(), f.apply(x.getQuick(ye.index()), ye.get())); } return x; } }
@Override public Vector assign(Vector x, Vector y, DoubleDoubleFunction f) { for (Element xe : x.all()) { x.setQuick(xe.index(), f.apply(xe.get(), y.getQuick(xe.index()))); } return x; } }
@Override public Vector assign(Vector x, Vector y, DoubleDoubleFunction f) { for (Element ye : y.all()) { x.setQuick(ye.index(), f.apply(x.getQuick(ye.index()), ye.get())); } return x; } }
@Override public Vector assign(Vector x, Vector y, DoubleDoubleFunction f) { OrderedIntDoubleMapping updates = new OrderedIntDoubleMapping(false); for (Element xe : x.all()) { updates.set(xe.index(), f.apply(xe.get(), y.getQuick(xe.index()))); } x.mergeUpdates(updates); return x; } }
@Override public Vector assign(Vector x, Vector y, DoubleDoubleFunction f) { OrderedIntDoubleMapping updates = new OrderedIntDoubleMapping(false); for (Element ye : y.all()) { updates.set(ye.index(), f.apply(x.getQuick(ye.index()), ye.get())); } x.mergeUpdates(updates); return x; } }
@Override public Vector assign(Vector x, Vector y, DoubleDoubleFunction f) { OrderedIntDoubleMapping updates = new OrderedIntDoubleMapping(false); for (Element ye : y.nonZeroes()) { updates.set(ye.index(), f.apply(x.getQuick(ye.index()), ye.get())); } x.mergeUpdates(updates); return x; } }
@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 testCrossProduct() { Matrix result = test.cross(test); assertEquals("row size", test.size(), result.rowSize()); assertEquals("col size", test.size(), result.columnSize()); for (int row = 0; row < result.rowSize(); row++) { for (int col = 0; col < result.columnSize(); col++) { assertEquals("cross[" + row + "][" + col + ']', test.getQuick(row) * test.getQuick(col), result.getQuick(row, col), 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); }