sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1523103 - in /sis/branches/JDK7: ./ core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/ core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/ core/sis-referencing/src/test/java/org/apac...
Date Fri, 13 Sep 2013 21:02:39 GMT
Author: desruisseaux
Date: Fri Sep 13 21:02:39 2013
New Revision: 1523103

URL: http://svn.apache.org/r1523103
Log:
Initial port of JAMA code for matrix inversion.

Added:
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Solver.java
  (with props)
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/SolverTest.java
  (with props)
Modified:
    sis/branches/JDK7/NOTICE
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/GeneralMatrix.java
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix3.java
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix4.java
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrix.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/GeneralMatrixTest.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix1Test.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix2Test.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix3Test.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix4Test.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatrixTestCase.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrixTest.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java

Modified: sis/branches/JDK7/NOTICE
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/NOTICE?rev=1523103&r1=1523102&r2=1523103&view=diff
==============================================================================
--- sis/branches/JDK7/NOTICE (original)
+++ sis/branches/JDK7/NOTICE Fri Sep 13 21:02:39 2013
@@ -8,6 +8,9 @@ The Javadoc contains documentation from 
 (OGC®) specifications (http://www.opengeospatial.org/standards/), also
 known as OpenGIS.
 
+The matrix package contains code adapted from JAMA Java Matrix Package
+(http://math.nist.gov/javanumerics/jama/)
+
 Some modules use the Esri Geometry API developed by ESRI
 (http://github.com/Esri/geometry-api-java/)
 

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/GeneralMatrix.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/GeneralMatrix.java?rev=1523103&r1=1523102&r2=1523103&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/GeneralMatrix.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/GeneralMatrix.java
[UTF-8] Fri Sep 13 21:02:39 2013
@@ -278,7 +278,7 @@ class GeneralMatrix extends MatrixSIS {
      */
     @Override
     public MatrixSIS inverse() throws NoninvertibleMatrixException {
-        throw new UnsupportedOperationException(); // TODO
+        return Solver.inverse(this);
     }
 
     /**

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix3.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix3.java?rev=1523103&r1=1523102&r2=1523103&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix3.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix3.java
[UTF-8] Fri Sep 13 21:02:39 2013
@@ -296,7 +296,7 @@ public final class Matrix3 extends Matri
      */
     @Override
     public MatrixSIS inverse() throws NoninvertibleMatrixException {
-        throw new UnsupportedOperationException(); // TODO
+        return Solver.inverse(this);
     }
 
     /**

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix4.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix4.java?rev=1523103&r1=1523102&r2=1523103&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix4.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix4.java
[UTF-8] Fri Sep 13 21:02:39 2013
@@ -347,7 +347,7 @@ public final class Matrix4 extends Matri
      */
     @Override
     public MatrixSIS inverse() throws NoninvertibleMatrixException {
-        throw new UnsupportedOperationException(); // TODO
+        return Solver.inverse(this);
     }
 
     /**

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrix.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrix.java?rev=1523103&r1=1523102&r2=1523103&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrix.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrix.java
[UTF-8] Fri Sep 13 21:02:39 2013
@@ -122,6 +122,15 @@ final class NonSquareMatrix extends Gene
      * {@inheritDoc}
      */
     @Override
+    public MatrixSIS inverse() throws NoninvertibleMatrixException {
+        // TODO: This is where we will need a special treatment different than what JAMA
do (because different needs).
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
     public MatrixSIS clone() {
         return new NonSquareMatrix(this);
     }

Added: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Solver.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Solver.java?rev=1523103&view=auto
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Solver.java
(added)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Solver.java
[UTF-8] Fri Sep 13 21:02:39 2013
@@ -0,0 +1,238 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.referencing.operation.matrix;
+
+import org.opengis.referencing.operation.Matrix;
+import org.apache.sis.util.ArraysExt;
+
+
+/**
+ * Computes the value of <var>U</var> which solves {@code X} × <var>U</var>
= {@code Y}.
+ * The {@link #solve(MatrixSIS, Matrix, int)} method in this class is adapted from the {@code
LUDecomposition}
+ * class of the <a href="http://math.nist.gov/javanumerics/jama">JAMA matrix package</a>.
JAMA is provided in
+ * the public domain.
+ *
+ * <p>This class implements the {@link Matrix} interface as an implementation convenience.
+ * This implementation details can be ignored.</p>
+ *
+ * @since   0.4
+ * @version 0.4
+ * @module
+ */
+final class Solver implements Matrix {
+    /**
+     * A immutable identity matrix without defined size.
+     * This is used only for computing the inverse.
+     */
+    private static final Matrix IDENTITY = new Solver();
+
+    /**
+     * For the {@link #IDENTITY} constant only.
+     */
+    private Solver() {
+    }
+
+    /**
+     * Returns {@code true} since this matrix is the identity matrix.
+     */
+    @Override
+    public boolean isIdentity() {
+        return true;
+    }
+
+    /**
+     * Returns 1 for elements on the diagonal, 0 otherwise.
+     * This method never thrown exception.
+     */
+    @Override
+    public double getElement(final int j, final int i) {
+        return (j == i) ? 1 : 0;
+    }
+
+    /**
+     * Unsupported operation since this matrix is immutable.
+     */
+    @Override
+    public void setElement(int j, int i, double d) {
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * Returns {@code this} since this matrix is immutable.
+     */
+    @Override
+    public Matrix clone() {
+        return this;
+    }
+
+    /**
+     * Arbitrarily returns 0. The actual value does not matter for the purpose of {@code
Solver}.
+     */
+    @Override
+    public int getNumRow() {
+        return 0;
+    }
+
+    /**
+     * Arbitrarily returns 0. The actual value does not matter for the purpose of {@code
Solver}.
+     */
+    @Override
+    public int getNumCol() {
+        return 0;
+    }
+
+    /**
+     * Computes the inverse of the given matrix. This method shall be invoked only for square
matrices
+     * (this is <strong>not</strong> verified by this method).
+     */
+    static MatrixSIS inverse(final MatrixSIS X) throws NoninvertibleMatrixException {
+        return solve(X, IDENTITY, X.getNumRow());
+    }
+
+    /**
+     * Solves {@code X} × <var>U</var> = {@code Y}.
+     * This method is an adaptation of the {@code LUDecomposition} class of the JAMA matrix
package.
+     *
+     * <p>This method does <strong>not</strong> checks the matrix size.
+     * Check for matrix size shall be performed by the caller like below:</p>
+     *
+     * {@preformat java
+     *     final int size = X.getNumRow();
+     *     if (X.getNumCol() != size) {
+     *         throw new NoninvertibleTransformException("Matrix must be square.");
+     *     }
+     *     if (Y.getNumRow() != size) {
+     *         throw new MismatchedMatrixSizeException("Matrix row dimensions must agree.");
+     *     }
+     * }
+     *
+     * @param  X The matrix to invert.
+     * @param  innerSize The value of {@code Y.getNumCol()}.
+     * @throws NoninvertibleMatrixException If the {@code X} matrix is singular.
+     */
+    static MatrixSIS solve(final MatrixSIS X, final Matrix Y, final int innerSize)
+            throws NoninvertibleMatrixException
+    {
+        final int size = X.getNumRow();
+        /*
+         * Use a "left-looking", dot-product, Crout/Doolittle algorithm.
+         */
+        final double[] LU = X.getElements();
+        final int[] pivot = new int[size];
+        for (int j=0; j<size; j++) {
+           pivot[j] = j;
+        }
+        final double[] column = new double[size];
+        for (int i=0; i<size; i++) {
+            /*
+             * Make a copy of the i-th column.
+             */
+            for (int j=0; j<size; j++) {
+                column[j] = LU[j*size + i];
+            }
+            /*
+             * Apply previous transformations.
+             */
+            for (int j=0; j<size; j++) {
+                final int rowOffset = j*size;
+                final int kmax = Math.min(j,i);
+                double s = 0.0;
+                for (int k=0; k<kmax; k++) {
+                   s += LU[rowOffset + k] * column[k];
+                }
+                LU[rowOffset + i] = (column[j] -= s);
+            }
+            /*
+             * Find pivot and exchange if necessary.
+             */
+            int p = i;
+            for (int j=i; ++j < size;) {
+                if (Math.abs(column[j]) > Math.abs(column[p])) {
+                    p = j;
+                }
+            }
+            if (p != i) {
+                final int pRow = p*size;
+                final int iRow = i*size;
+                for (int k=0; k<size; k++) { // Swap two full rows.
+                    ArraysExt.swap(LU, pRow + k, iRow + k);
+                }
+                ArraysExt.swap(pivot, p, i);
+            }
+            /*
+             * Compute multipliers.
+             */
+            final double d = LU[i*size + i];
+            if (d != 0.0) {
+                for (int j=i; ++j < size;) {
+                    LU[j*size + i] /= d;
+                }
+            }
+        }
+        /*
+         * At this point, we are done computing LU.
+         * Ensure that the matrix is not singular.
+         */
+        for (int j=0; j<size; j++) {
+            if (LU[j*size + j] == 0) {
+                throw new NoninvertibleMatrixException();
+            }
+        }
+        /*
+         * Copy right hand side with pivoting.
+         * We will write the result of this method directly in the elements array.
+         */
+        final double[] elements = new double[size * innerSize];
+        for (int k=0,j=0; j<size; j++) {
+            final int p = pivot[j];
+            for (int i=0; i<innerSize; i++) {
+                elements[k++] = Y.getElement(p, i);
+            }
+        }
+        /*
+         * Solve L*Y = B(pivot, :)
+         */
+        for (int k=0; k<size; k++) {
+            final int rowOffset = k*innerSize;          // Offset of row computed by current
iteration.
+            for (int j=k; ++j < size;) {
+                final int loRowOffset = j*innerSize;    // Offset of a row after (locate
lower) the current row.
+                final int luRowOffset = j*size;  // Offset of the corresponding row in the
LU matrix.
+                for (int i=0; i<innerSize; i++) {
+                    elements[loRowOffset + i] -= (elements[rowOffset + i] * LU[luRowOffset
+ k]);
+                }
+            }
+        }
+        /*
+         * Solve U*X = Y
+         */
+        for (int k=size; --k >= 0;) {
+            final int rowOffset = k*innerSize;          // Offset of row computed by current
iteration.
+            final double d = LU[k*size + k];     // A diagonal element on the current row.
+            for (int i=0; i<innerSize; i++) {           // Apply to all columns in the
current row.
+                elements[rowOffset + i] /= d;
+            }
+            for (int j=0; j<k; j++) {
+                final int upRowOffset = j*innerSize;    // Offset of a row before (locate
upper) the current row.
+                final double c = LU[j*size + k]; // Same column than the diagonal element,
but in the upper row.
+                for (int i=0; i<innerSize; i++) {       // Apply to all columns in the
upper row.
+                    elements[upRowOffset + i] -= (elements[rowOffset + i] * c);
+                }
+            }
+        }
+        return Matrices.create(size, innerSize, elements);
+    }
+}

Propchange: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Solver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Solver.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/GeneralMatrixTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/GeneralMatrixTest.java?rev=1523103&r1=1523102&r2=1523103&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/GeneralMatrixTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/GeneralMatrixTest.java
[UTF-8] Fri Sep 13 21:02:39 2013
@@ -16,6 +16,8 @@
  */
 package org.apache.sis.referencing.operation.matrix;
 
+import org.apache.sis.test.DependsOn;
+
 import static org.junit.Assert.*;
 
 
@@ -28,6 +30,7 @@ import static org.junit.Assert.*;
  * @version 0.4
  * @module
  */
+@DependsOn(SolverTest.class)
 public final strictfp class GeneralMatrixTest extends MatrixTestCase {
     /**
      * Number of rows and columns.

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix1Test.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix1Test.java?rev=1523103&r1=1523102&r2=1523103&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix1Test.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix1Test.java
[UTF-8] Fri Sep 13 21:02:39 2013
@@ -17,6 +17,7 @@
 package org.apache.sis.referencing.operation.matrix;
 
 import org.junit.Test;
+import org.apache.sis.test.DependsOn;
 
 import static org.junit.Assert.*;
 import static org.apache.sis.referencing.operation.matrix.Matrix1.SIZE;
@@ -31,6 +32,7 @@ import static org.apache.sis.referencing
  * @version 0.4
  * @module
  */
+@DependsOn(SolverTest.class)
 public final strictfp class Matrix1Test extends MatrixTestCase {
     /**
      * Returns the size of the matrix of interest for this test class.

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix2Test.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix2Test.java?rev=1523103&r1=1523102&r2=1523103&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix2Test.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix2Test.java
[UTF-8] Fri Sep 13 21:02:39 2013
@@ -16,6 +16,7 @@
  */
 package org.apache.sis.referencing.operation.matrix;
 
+import org.apache.sis.test.DependsOn;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
@@ -31,6 +32,7 @@ import static org.apache.sis.referencing
  * @version 0.4
  * @module
  */
+@DependsOn(SolverTest.class)
 public final strictfp class Matrix2Test extends MatrixTestCase {
     /**
      * Returns the size of the matrix of interest for this test class.

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix3Test.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix3Test.java?rev=1523103&r1=1523102&r2=1523103&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix3Test.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix3Test.java
[UTF-8] Fri Sep 13 21:02:39 2013
@@ -16,6 +16,7 @@
  */
 package org.apache.sis.referencing.operation.matrix;
 
+import org.apache.sis.test.DependsOn;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
@@ -31,6 +32,7 @@ import static org.apache.sis.referencing
  * @version 0.4
  * @module
  */
+@DependsOn(SolverTest.class)
 public final strictfp class Matrix3Test extends MatrixTestCase {
     /**
      * Returns the size of the matrix of interest for this test class.

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix4Test.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix4Test.java?rev=1523103&r1=1523102&r2=1523103&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix4Test.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix4Test.java
[UTF-8] Fri Sep 13 21:02:39 2013
@@ -16,6 +16,7 @@
  */
 package org.apache.sis.referencing.operation.matrix;
 
+import org.apache.sis.test.DependsOn;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
@@ -31,6 +32,7 @@ import static org.apache.sis.referencing
  * @version 0.4
  * @module
  */
+@DependsOn(SolverTest.class)
 public final strictfp class Matrix4Test extends MatrixTestCase {
     /**
      * Returns the size of the matrix of interest for this test class.

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatrixTestCase.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatrixTestCase.java?rev=1523103&r1=1523102&r2=1523103&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatrixTestCase.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatrixTestCase.java
[UTF-8] Fri Sep 13 21:02:39 2013
@@ -63,7 +63,7 @@ public abstract strictfp class MatrixTes
     /**
      * Number of random matrices to try in arithmetic operation tests.
      */
-    private static final int NUM_TRIES = 10;
+    static final int NUMBER_OF_REPETITIONS = 10;
 
     /**
      * The threshold in matrix determinant for attempting to compute the inverse.
@@ -99,7 +99,7 @@ public abstract strictfp class MatrixTes
     /**
      * Verifies that the SIS matrix is equals to the JAMA one, up to the given tolerance
value.
      */
-    private static void assertMatrixEquals(final Matrix expected, final MatrixSIS actual,
final double tolerance) {
+    static void assertMatrixEquals(final Matrix expected, final MatrixSIS actual, final double
tolerance) {
         final int numRow = actual.getNumRow();
         final int numCol = actual.getNumCol();
         assertEquals("numRow", expected.getRowDimension(),    numRow);
@@ -285,7 +285,7 @@ public abstract strictfp class MatrixTes
     public void testMultiply() {
         final int numRow = getNumRow();
         final int numCol = getNumCol();
-        for (int n=0; n<NUM_TRIES; n++) {
+        for (int n=0; n<NUMBER_OF_REPETITIONS; n++) {
             double[] elements = createRandomPositiveValues(numRow * numCol);
             final MatrixSIS matrix = Matrices.create(numRow, numCol, elements);
             final Matrix reference = new Matrix(elements, numCol).transpose();
@@ -321,9 +321,9 @@ public abstract strictfp class MatrixTes
         final int numRow = getNumRow();
         final int numCol = getNumCol();
 
-        org.junit.Assume.assumeTrue(numRow == 1 && numCol == 1); // Temporary limitation.
+        if (numRow != 1 || numCol != 1) return; // Temporary limitation.
 
-        for (int n=0; n<NUM_TRIES; n++) {
+        for (int n=0; n<NUMBER_OF_REPETITIONS; n++) {
             double[] elements = createRandomPositiveValues(numRow * numCol);
             final Matrix reference = new Matrix(elements, numCol).transpose();
             if (!(reference.det() >= DETERMINANT_THRESHOLD)) {
@@ -362,10 +362,7 @@ public abstract strictfp class MatrixTes
     public void testInverse() throws NoninvertibleMatrixException {
         final int numRow = getNumRow();
         final int numCol = getNumCol();
-
-        org.junit.Assume.assumeTrue(numRow == 1 && numCol == 1); // Temporary limitation.
-
-        for (int n=0; n<NUM_TRIES; n++) {
+        for (int n=0; n<NUMBER_OF_REPETITIONS; n++) {
             final double[] elements = createRandomPositiveValues(numRow * numCol);
             final Matrix reference = new Matrix(elements, numCol).transpose();
             if (!(reference.det() >= DETERMINANT_THRESHOLD)) {

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrixTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrixTest.java?rev=1523103&r1=1523102&r2=1523103&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrixTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrixTest.java
[UTF-8] Fri Sep 13 21:02:39 2013
@@ -59,4 +59,12 @@ public final strictfp class NonSquareMat
         super.validate(matrix);
         assertEquals(NonSquareMatrix.class, matrix.getClass());
     }
+
+    /**
+     * TODO: inverse transform not yet implemented for non-square matrix.
+     */
+    @Override
+    @org.junit.Ignore
+    public void testInverse() throws NoninvertibleMatrixException {
+    }
 }

Added: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/SolverTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/SolverTest.java?rev=1523103&view=auto
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/SolverTest.java
(added)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/SolverTest.java
[UTF-8] Fri Sep 13 21:02:39 2013
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.referencing.operation.matrix;
+
+import java.util.Random;
+import Jama.Matrix;
+import org.apache.sis.test.TestUtilities;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+
+/**
+ * Tests the {@link Solver} class using <a href="http://math.nist.gov/javanumerics/jama">JAMA</a>
+ * as the reference implementation.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.4
+ * @version 0.4
+ * @module
+ */
+public final strictfp class SolverTest extends TestCase {
+    /**
+     * The matrix to test.
+     */
+    private MatrixSIS matrix;
+
+    /**
+     * A matrix to use as the reference implementation.
+     * Contains the same value than {@link #matrix}.
+     */
+    private Matrix reference;
+
+    /**
+     * Initializes the {@link #matrix} and {@link #reference} matrices to random values.
+     */
+    private void createMatrices(final int numRow, final int numCol, final Random random)
{
+        matrix = new GeneralMatrix(numRow, numCol, false);
+        reference = new Matrix(numRow, numCol);
+        for (int j=0; j<numRow; j++) {
+            for (int i=0; i<numCol; i++) {
+                final double e = random.nextDouble() * 1000;
+                matrix.setElement(j, i, e);
+                reference.set(j, i, e);
+            }
+        }
+    }
+
+    /**
+     * Tests the {@code Solver.solve(MatrixSIS, Matrix, int)} method.
+     *
+     * @throws NoninvertibleMatrixException Should never happen.
+     */
+    @Test
+    public void testSolve() throws NoninvertibleMatrixException {
+        final Random random = TestUtilities.createRandomNumberGenerator();
+        for (int k=0; k<MatrixTestCase.NUMBER_OF_REPETITIONS; k++) {
+            final int size = random.nextInt(16) + 1;
+            createMatrices(size, random.nextInt(16) + 1, random);
+            final Matrix referenceArg = this.reference;
+            final MatrixSIS matrixArg = this.matrix;
+            createMatrices(size, size, random);
+            final Matrix jama;
+            try {
+                jama = reference.solve(referenceArg);
+            } catch (RuntimeException e) {
+                out.println(e); // "Matrix is singular."
+                continue;
+            }
+            final MatrixSIS U = Solver.solve(matrix, matrixArg, matrixArg.getNumCol());
+            MatrixTestCase.assertMatrixEquals(jama, U, MatrixTestCase.TOLERANCE);
+        }
+    }
+}

Propchange: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/SolverTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/SolverTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java?rev=1523103&r1=1523102&r2=1523103&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java
[UTF-8] Fri Sep 13 21:02:39 2013
@@ -31,6 +31,7 @@ import org.junit.BeforeClass;
  */
 @Suite.SuiteClasses({
     // Test matrix first because they may be used in about every SIS corners.
+    org.apache.sis.referencing.operation.matrix.SolverTest.class,
     org.apache.sis.referencing.operation.matrix.Matrix1Test.class,
     org.apache.sis.referencing.operation.matrix.Matrix2Test.class,
     org.apache.sis.referencing.operation.matrix.Matrix3Test.class,



Mime
View raw message