@Override public void set(int index, double value) { delegate.set(index, value); }
@Override public void set(int index, double value) { delegate.set(index, value); }
@Override public void setQuick(int row, int column, double value) { if (row == column) { diagonal.set(row, value); } else { throw new UnsupportedOperationException("Can't set off-diagonal element"); } }
@Override public void set(int index, double value) { if (index == this.index) { diagonal.set(index, value); } else { throw new IllegalArgumentException("Can't set off-diagonal element of diagonal matrix"); } }
@Override public void setQuick(int index, double value) { if (index == this.index) { diagonal.set(this.index, value); } else { throw new IllegalArgumentException("Can't set off-diagonal element of DiagonalMatrix"); } }
private static void updateTrainingProjectionsVector(TrainingState state, Vector trainingVector, int previousEigenIndex) { Vector previousEigen = state.mostRecentEigen(); Vector currentTrainingVectorProjection = state.currentTrainingProjection(); double projection = previousEigen.dot(trainingVector); currentTrainingVectorProjection.set(previousEigenIndex, projection); }
@Test public void testDotSuperBig() { Vector w = new SequentialAccessSparseVector(Integer.MAX_VALUE, 12); w.set(1, 0.4); w.set(2, 0.4); w.set(3, -0.666666667); Vector v = new SequentialAccessSparseVector(Integer.MAX_VALUE, 12); v.set(3, 1); assertEquals("super-big", -0.666666667, v.dot(w), EPSILON); }
public void testToString() { Vector w; w = generateTestVector(20); w.set(0, 1.1); w.set(13, 100500.); w.set(19, 3.141592); assertEquals("{0:1.1,13:100500.0,19:3.141592}", w.toString()); w = generateTestVector(12); w.set(10, 0.1); assertEquals("{10:0.1}", w.toString()); w = generateTestVector(12); assertEquals("{}", w.toString()); } }
@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); }
/** * Collects the results of a function applied to each column of a matrix. * * @param f The function to be applied to each column. * @return The vector of results. */ @Override public Vector aggregateColumns(VectorFunction f) { Vector r = new DenseVector(numCols()); for (int col = 0; col < numCols(); col++) { r.set(col, f.apply(viewColumn(col))); } return r; }
/** * Collects the results of a function applied to each row of a matrix. * * @param f The function to be applied to each row. * @return The vector of results. */ @Override public Vector aggregateRows(VectorFunction f) { Vector r = new DenseVector(numRows()); int n = numRows(); for (int row = 0; row < n; row++) { r.set(row, f.apply(viewRow(row))); } return r; }
@Override @Before public void setUp() throws Exception { super.setUp(); test = generateTestVector(2 * values.length + 1); for (int i = 0; i < values.length; i++) { test.set(2 * i + 1, values[i]); } }
@Test public void testSet() throws Exception { test.set(2, 4.5); for (int i = 0; i < test.size(); i++) { assertEquals("set [" + i + ']', i == 2 ? 4.5 : values[OFFSET + i], test.get(i), EPSILON); } }
public static Vector mult(Matrix m, Vector v) { if (m.numRows() != v.size()) { throw new CardinalityException(m.numRows(), v.size()); } // Use a Dense Vector for the moment, Vector result = new DenseVector(m.numRows()); for (int i = 0; i < m.numRows(); i++) { result.set(i, m.viewRow(i).dot(v)); } return result; }
public static Matrix randomHierarchicalMatrix(int numRows, int numCols, boolean symmetric) { Matrix matrix = new DenseMatrix(numRows, numCols); // TODO rejigger tests so that it doesn't expect this particular seed Random r = new Random(1234L); for (int row = 0; row < numRows; row++) { Vector v = new DenseVector(numCols); for (int col = 0; col < numCols; col++) { double val = r.nextGaussian(); v.set(col, val); } v.assign(Functions.MULT, 1/((row + 1) * v.norm(2))); matrix.assignRow(row, v); } if (symmetric) { return matrix.times(matrix.transpose()); } return matrix; }
public void createRandom(Vector v) { int size = randomInt(v.size() - 1); for (int i = 0; i < size; ++i) { v.set(randomInt(v.size() - 1), randomDouble()); } int zeros = Math.max(2, size / 4); for (Element e : v.nonZeroes()) { if (e.index() % zeros == 0) { e.set(0.0); } } }
@Test public void testViewBasics() { Vector v = randomVector(); int[] pivot = pivot(); Vector pvv = new PermutedVectorView(v, pivot); // verify the view has the same contents for (int i = 0; i < 20; i++) { assertEquals("Element " + i, v.get(pivot[i]), pvv.get(i), 0); } // change a view element or two on each side pvv.set(6, 321); v.set(9, 512); // verify again for (int i = 0; i < 20; i++) { assertEquals("Element " + i, v.get(pivot[i]), pvv.get(i), 0); } }
@Test public void testSet() { test.set(3, 4.5); for (int i = 0; i < test.size(); i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 0.0, test.get(i), EPSILON); } else if (i == 3) { assertEquals("set [" + i + ']', 4.5, test.get(i), EPSILON); } else { assertEquals("set [" + i + ']', values[i/2], test.get(i), EPSILON); } } }
@Test public void testColumnView() { assertEquals(test.rowSize(), test.viewColumn(0).size()); assertEquals(test.rowSize(), test.viewColumn(1).size()); Random gen = RandomUtils.getRandom(); for (int col = 0; col < test.columnSize(); col++) { int j = gen.nextInt(test.columnSize()); double old = test.get(col, j); double v = gen.nextGaussian(); test.viewColumn(col).set(j, v); assertEquals(v, test.get(j, col), 0); assertEquals(v, test.viewColumn(col).get(j), 0); test.set(j, col, old); assertEquals(old, test.get(j, col), 0); assertEquals(old, test.viewColumn(col).get(j), 0); } }