/** * Return the value at the given indexes, without checking bounds * * @param row an int row index * @param column an int column index * @return the double at the index */ @Override public double getQuick(int row, int column) { return base.getQuick(rowPivot[row], columnPivot[column]); }
/** * 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 matrix.getQuick(row + rowStride * index, column + columnStride * index); }
@Test public void createMiIi() { Vector f1 = new DenseVector(new double[] { 1, 2, 3 }); Vector f2 = new DenseVector(new double[] { 4, 5, 6 }); Matrix miIi = AlternatingLeastSquaresSolver.createMiIi(Arrays.asList(f1, f2), 3); assertEquals(1.0, miIi.getQuick(0, 0), EPSILON); assertEquals(2.0, miIi.getQuick(1, 0), EPSILON); assertEquals(3.0, miIi.getQuick(2, 0), EPSILON); assertEquals(4.0, miIi.getQuick(0, 1), EPSILON); assertEquals(5.0, miIi.getQuick(1, 1), EPSILON); assertEquals(6.0, miIi.getQuick(2, 1), EPSILON); }
public JacobiConditioner(Matrix a) { if (a.numCols() != a.numRows()) { throw new IllegalArgumentException("Matrix must be square."); } inverseDiagonal = new DenseVector(a.numCols()); for (int i = 0; i < a.numCols(); ++i) { inverseDiagonal.setQuick(i, 1.0 / a.getQuick(i, i)); } }
@Test public void testAssignColumn() { double[] data = {2.1, 3.2, 4.3}; test.assignColumn(1, new DenseVector(data)); assertEquals("test[0][1]", 2.1, test.getQuick(0, 1), EPSILON); assertEquals("test[1][1]", 3.2, test.getQuick(1, 1), EPSILON); assertEquals("test[2][1]", 4.3, test.getQuick(2, 1), EPSILON); }
@Test public void testAssignColumn() { double[] data = {2.1, 3.2, 4.3}; test.assignColumn(1, new DenseVector(data)); assertEquals("test[0][1]", 2.1, test.getQuick(0, 1), EPSILON); assertEquals("test[1][1]", 3.2, test.getQuick(1, 1), EPSILON); assertEquals("test[2][1]", 4.3, test.getQuick(2, 1), EPSILON); }
@Test public void testCopy() { Matrix copy = test.clone(); assertTrue("wrong class", copy instanceof MatrixView); for (int row = 0; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { assertEquals("value[" + row + "][" + col + ']', test.getQuick(row, col), copy.getQuick(row, col), EPSILON); } } }
@Test public void testGetQuick() { for (int row = 0; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { assertEquals("value[" + row + "][" + col + ']', values[row][col], test .getQuick(row, col), EPSILON); } } }
@Test public void testAssignDouble() { test.assign(4.53); for (int row = 0; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { assertEquals("value[" + row + "][" + col + ']', 4.53, test.getQuick( row, col), EPSILON); } } }
@Test public void testAssignDoubleArrayArray() { test.assign(new double[3][2]); for (int row = 0; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { assertEquals("value[" + row + "][" + col + ']', 0.0, test.getQuick(row, col), EPSILON); } } }
@Test public void testAssignUnaryFunction() { test.assign(Functions.NEGATE); for (int row = 0; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { assertEquals("value[" + row + "][" + col + ']', -values[row + 1][col + 1], test.getQuick(row, col), EPSILON); } } }
@Test public void testAssignDoubleArrayArray() { test.assign(new double[3][2]); for (int row = 0; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { assertEquals("value[" + row + "][" + col + ']', 0.0, test.getQuick(row, col), EPSILON); } } }
@Test public void testMinus() { Matrix value = test.minus(test); for (int row = 0; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { assertEquals("value[" + row + "][" + col + ']', 0.0, value.getQuick( row, col), EPSILON); } } }
@Test public void testViewPart() { int[] offset = {1, 1}; int[] size = {2, 1}; Matrix view = test.viewPart(offset, size); for (int row = 0; row < view.rowSize(); row++) { for (int col = 0; col < view.columnSize(); col++) { assertEquals("value[" + row + "][" + col + ']', values[row + 2][col + 2], view.getQuick(row, col), EPSILON); } } }
@Test public void testAssignDouble() { test.assign(4.53); for (int row = 0; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { assertEquals("value[" + row + "][" + col + ']', 4.53, test.getQuick( row, col), EPSILON); } } }
@Test public void testPlusMatrix() { Matrix value = test.plus(test); for (int row = 0; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { assertEquals("value[" + row + "][" + col + ']', values[row][col] * 2, value.getQuick(row, col), EPSILON); } } }
@Test public void testTranspose() { Matrix transpose = test.transpose(); assertEquals("rows", test.columnSize(), transpose.rowSize()); assertEquals("cols", test.rowSize(), transpose.columnSize()); for (int row = 0; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { assertEquals("value[" + row + "][" + col + ']', test.getQuick(row, col), transpose.getQuick(col, row), 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 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); } } }