/** * Returns all elements in the wrapped matrix in a row-major array. */ @Override public double[] getElements() { if (matrix instanceof MatrixSIS) { return ((MatrixSIS) matrix).getElements(); } else { return super.getElements(); } }
/** * Returns all elements in the wrapped matrix in a row-major array. */ @Override public double[] getElements() { if (matrix instanceof MatrixSIS) { return ((MatrixSIS) matrix).getElements(); } else { return super.getElements(); } }
/** * Returns a hash code value based on the data values in this matrix. * * @return a hash code value for this matrix. */ @Override public int hashCode() { return Arrays.hashCode(getElements()) ^ (int) serialVersionUID; }
/** * Returns a hash code value based on the data values in this matrix. * * @return a hash code value for this matrix. */ @Override public int hashCode() { return Arrays.hashCode(getElements()) ^ (int) serialVersionUID; }
/** * Stores all matrix elements in the given flat array. This method does not verify the array length. * All subclasses in this {@code org.apache.sis.referencing.operation.matrix} package override this * method with a more efficient implementation. * * @param dest the destination array. May be longer than necessary (this happen when the caller needs to * append {@link org.apache.sis.internal.util.DoubleDouble#error} values after the elements). */ void getElements(final double[] dest) { final double[] elements = getElements(); System.arraycopy(elements, 0, dest, 0, elements.length); }
/** * Stores all matrix elements in the given flat array. This method does not verify the array length. * All subclasses in this {@code org.apache.sis.referencing.operation.matrix} package override this * method with a more efficient implementation. * * @param dest the destination array. May be longer than necessary (this happen when the caller needs to * append {@link org.apache.sis.internal.util.DoubleDouble#error} values after the elements). */ void getElements(final double[] dest) { final double[] elements = getElements(); System.arraycopy(elements, 0, dest, 0, elements.length); }
/** * Copies the elements of the given matrix in the given array. * This method ignores the error terms, if any. * * @param matrix the matrix to copy. * @param numRow {@code matrix.getNumRow()}. * @param numCol {@code matrix.getNumCol()}. * @param elements where to copy the elements. */ static void getElements(final Matrix matrix, final int numRow, final int numCol, final double[] elements) { if (matrix instanceof MatrixSIS) { ((MatrixSIS) matrix).getElements(elements); } else { for (int k=0,j=0; j<numRow; j++) { for (int i=0; i<numCol; i++) { elements[k++] = matrix.getElement(j, i); } } } }
/** * Copies the elements of the given matrix in the given array. * This method ignores the error terms, if any. * * @param matrix the matrix to copy. * @param numRow {@code matrix.getNumRow()}. * @param numCol {@code matrix.getNumCol()}. * @param elements where to copy the elements. */ static void getElements(final Matrix matrix, final int numRow, final int numCol, final double[] elements) { if (matrix instanceof MatrixSIS) { ((MatrixSIS) matrix).getElements(elements); } else { for (int k=0,j=0; j<numRow; j++) { for (int i=0; i<numCol; i++) { elements[k++] = matrix.getElement(j, i); } } } }
/** * Returns the elements of the given matrix, together with error terms if available. */ private static double[] getExtendedElements(final Matrix matrix) { if (matrix instanceof ExtendedPrecisionMatrix) { return ((ExtendedPrecisionMatrix) matrix).getExtendedElements(); } else { return MatrixSIS.castOrCopy(matrix).getElements(); } }
/** * Returns the elements of the given matrix, together with error terms if available. */ private static double[] getExtendedElements(final Matrix matrix) { if (matrix instanceof ExtendedPrecisionMatrix) { return ((ExtendedPrecisionMatrix) matrix).getExtendedElements(); } else { return MatrixSIS.castOrCopy(matrix).getElements(); } }
getElements(matrix, numRow, numCol, elements);
getElements(matrix, numRow, numCol, elements);
/** * Asserts that the given matrix is equals to the given expected values, up to the given tolerance threshold. * This method compares the elements values in two slightly redundant ways. */ static void assertEqualsElements(final double[] expected, final int numRow, final int numCol, final MatrixSIS actual, final double tolerance) { assertEquals("numRow", numRow, actual.getNumRow()); assertEquals("numCol", numCol, actual.getNumCol()); assertArrayEquals(expected, actual.getElements(), tolerance); // First because more informative in case of failure. assertTrue(Matrices.create(numRow, numCol, expected).equals(actual, tolerance)); }
/** * Verifies our claim that {@code A.solve(B)} is equivalent to {@code A.inverse().multiply(B)}. * This claim is documented in {@link MatrixSIS#solve(Matrix)} javadoc. * * @throws NoninvertibleMatrixException should not happen. */ @Test public void testSolveEquivalence() throws NoninvertibleMatrixException { final Matrix3 A = new Matrix3( 0.5, 0, 0, 0, 0.5, 0, 0, 0, 1); final Matrix3 B = new Matrix3( 0, 3, 0, 3, 0, 0, 0, 0, 1); // Verify the result of A.inverse().multiply(B) as a matter of principle. final double[] expected = A.inverse().multiply(B).getElements(); assertArrayEquals(new double[] { 0, 6, 0, 6, 0, 0, 0, 0, 1}, expected, TOLERANCE); // Now the actual test. assertEqualsElements(expected, SIZE, SIZE, A.solve(B), TOLERANCE); }
/** * Sets the {@link #transform} field to the {@link TranslationTransform} instance to test. * * @param dimensions expected number of source and target dimensions. * @param matrix the data to use for creating the transform. */ private void create(final int dimensions, final MatrixSIS matrix) { final double[] elements = matrix.getElements(); final TranslationTransform tr = new TranslationTransform(matrix.getNumRow(), elements); assertEquals("sourceDimensions", dimensions, tr.getSourceDimensions()); assertEquals("targetDimensions", dimensions, tr.getTargetDimensions()); assertMatrixEquals("matrix", matrix, tr.getMatrix(), 0.0); assertArrayEquals("elements", elements, tr.getExtendedElements(), 0.0); transform = tr; validate(); }
/** * Sets the {@link #transform} field to the {@link ScaleTransform} instance to test. * * @param sourceDimensions expected number of source dimensions. * @param targetDimensions expected number of source dimensions. * @param matrix the data to use for creating the transform. */ private void create(final int sourceDimensions, final int targetDimensions, final MatrixSIS matrix) { final double[] elements = matrix.getElements(); final ScaleTransform tr = new ScaleTransform(matrix.getNumRow(), matrix.getNumCol(), elements); assertEquals("sourceDimensions", sourceDimensions, tr.getSourceDimensions()); assertEquals("targetDimensions", targetDimensions, tr.getTargetDimensions()); Assert.assertMatrixEquals("matrix", matrix, tr.getMatrix(), 0.0); assertArrayEquals("elements", elements, tr.getExtendedElements(), 0.0); transform = tr; validate(); }
/** * Creates a matrix initialized with a random array of element values, * then tests the {@link MatrixSIS#getElement(int, int)} method for each element. * This test will use {@link Matrices#create(int, int, double[])} for creating the matrix. * * <p>If this test fails, then all other tests in this class will be skipped since it would * not be possible to verify the result of any matrix operation.</p> */ @Test public void testGetElements() { initialize(3812872376135347328L); prepareNewMatrixSize(random); final int numRow = getNumRow(); final int numCol = getNumCol(); final double[] elements = createRandomPositiveValues(numRow * numCol); final MatrixSIS matrix = Matrices.create(numRow, numCol, elements); validate(matrix); /* * The JAMA constructor uses column-major array (FORTRAN convention), while SIS uses * row-major array. So we have to transpose the JAMA matrix after construction. */ assertEqualsJAMA(new Matrix(elements, numCol).transpose(), matrix, STRICT); assertArrayEquals("getElements", elements, matrix.getElements(), STRICT); }