sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1528555 - in /sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix: GeneralMatrix.java Matrix3.java Matrix4.java MatrixSIS.java
Date Wed, 02 Oct 2013 16:35:34 GMT
Author: desruisseaux
Date: Wed Oct  2 16:35:33 2013
New Revision: 1528555

URL: http://svn.apache.org/r1528555
Log:
Revert the previous commit that removed 'equals' and 'hashCode' from Matrix3/4 (more investigation
has show that it was not a so good idea),
and simplify in an other way by removing 'MatrixSIS.getExtendedElements()'. Instead, we perform
that work in GeneralMatrix in a way that put
unconditionally the error terms. Tthis simplify the actual computation by removing a bunch
of '(foo == 0) ? foo : foo' operations. This is
more apparent in the Solver class (not yet committed).

Modified:
    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/MatrixSIS.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=1528555&r1=1528554&r2=1528555&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] Wed Oct  2 16:35:33 2013
@@ -105,7 +105,9 @@ class GeneralMatrix extends MatrixSIS {
         this.numRow = (short) numRow;
         this.numCol = (short) numCol;
         this.elements = Arrays.copyOf(elements, length * precision);
-        inferErrors();
+        if (precision != 1) {
+            inferErrors(this.elements);
+        }
     }
 
     /**
@@ -123,7 +125,9 @@ class GeneralMatrix extends MatrixSIS {
         this.numCol = (short) numCol;
         elements = new double[numRow * numCol * precision];
         getElements(matrix, numRow, numCol, elements);
-        inferErrors();
+        if (precision != 1) {
+            inferErrors(elements);
+        }
     }
 
     /**
@@ -158,8 +162,8 @@ class GeneralMatrix extends MatrixSIS {
      * intend was to specify the {@link Math#PI} value, in which case this method will infer
that we would
      * need to add 1.2246467991473532E-16 in order to get a value closer to π.
      */
-    private void inferErrors() {
-        final int length = numRow * numCol;
+    private static void inferErrors(final double[] elements) {
+        final int length = elements.length / 2;
         for (int i=length; i<elements.length; i++) {
             elements[i] = DoubleDouble.errorForWellKnownValue(elements[i - length]);
         }
@@ -237,27 +241,26 @@ class GeneralMatrix extends MatrixSIS {
     }
 
     /**
-     * Returns all elements of the given matrix, possibly including the error terms for extended-precision
arithmetic.
-     * If the returned array contains error terms, then the array will have twice the normal
length.
-     * See {@link #elements} for more discussion.
+     * Returns all elements of the given matrix followed by the error terms for extended-precision
arithmetic.
+     * The array will have twice the normal length. See {@link #elements} for more discussion.
      *
      * <p>This method may return a direct reference to the internal array. <strong>Do
not modify.</strong></p>
      */
-    private static double[] getExtendedElements(final Matrix matrix, final int numRow, final
int numCol) {
-        if (matrix instanceof MatrixSIS) {
-            return ((MatrixSIS) matrix).getExtendedElements();
+    static double[] getExtendedElements(final Matrix matrix, final int numRow, final int
numCol) {
+        double[] elements;
+        final int length = numRow * numCol * 2;
+        if (matrix instanceof GeneralMatrix) {
+            elements = ((GeneralMatrix) matrix).elements;
+            if (elements.length == length) {
+                return elements; // Internal array already uses extended precision.
+            } else {
+                elements = Arrays.copyOf(elements, length);
+            }
+        } else {
+            elements = new double[length];
+            getElements(matrix, numRow, numCol, elements);
         }
-        final double[] elements = new double[numRow * numCol];
-        getElements(matrix, numRow, numCol, elements);
-        return elements;
-    }
-
-    /**
-     * Returns a direct reference to the internal array, which may or may not contains error
values
-     * for extended precision arithmetic.
-     */
-    @Override
-    final double[] getExtendedElements() {
+        inferErrors(elements);
         return elements;
     }
 
@@ -273,10 +276,12 @@ class GeneralMatrix extends MatrixSIS {
      * {@inheritDoc}
      */
     @Override
-    public final void setElements(final double[] elements) {
-        ensureLengthMatch(numRow*numCol, elements);
-        System.arraycopy(elements, 0, this.elements, 0, elements.length);
-        inferErrors();
+    public final void setElements(final double[] newValues) {
+        ensureLengthMatch(numRow*numCol, newValues);
+        System.arraycopy(newValues, 0, elements, 0, newValues.length);
+        if (elements.length != newValues.length) {
+            inferErrors(newValues);
+        }
     }
 
     /**
@@ -424,11 +429,11 @@ class GeneralMatrix extends MatrixSIS {
          * Get the matrix element values, together with the error terms if the matrix
          * use extended precision (double-double arithmetic).
          */
-        final double[] eltA   = A.getExtendedElements();
+        final double[] eltA   = getExtendedElements(A, numRow, nc);
         final double[] eltB   = getExtendedElements(B, nc, numCol);
-        final int      errors = indexOfErrors(numRow, numCol, elements); // Where error values
start, or 0 if none.
-        final int      errA   = indexOfErrors(numRow, nc, eltA);
-        final int      errB   = indexOfErrors(nc, numCol, eltB);
+        final int      errors = numRow * numCol; // Where error values start, or 0 if none.
+        final int      errA   = numRow * nc;
+        final int      errB   = nc * numCol;
         /*
          * Compute the product, to be stored directly in 'this'.
          */
@@ -442,8 +447,8 @@ class GeneralMatrix extends MatrixSIS {
                 final int nextRow = iA + nc;
                 while (iA < nextRow) {
                     dot.value = eltA[iA];
-                    dot.error = (errA != 0) ? eltA[iA + errA] : 0;
-                    dot.multiply(eltB[iB], (errB != 0) ? eltB[iB + errB] : 0);
+                    dot.error = eltA[iA + errA];
+                    dot.multiply(eltB[iB], eltB[iB + errB]);
                     sum.add(dot);
                     iB += numCol; // Move to next row of B.
                     iA++;         // Move to next column of A.
@@ -455,6 +460,32 @@ class GeneralMatrix extends MatrixSIS {
     }
 
     /**
+     * Returns {@code true} if the specified object is of type {@code GeneralMatrix} and
+     * all of the data members are equal to the corresponding data members in this matrix.
+     *
+     * @param object The object to compare with this matrix for equality.
+     * @return {@code true} if the given object is equal to this matrix.
+     */
+    @Override
+    public final boolean equals(final Object object) {
+        if (object instanceof GeneralMatrix) {
+            final GeneralMatrix that = (GeneralMatrix) object;
+            return numRow == that.numRow &&
+                   numCol == that.numCol &&
+                   Arrays.equals(elements, that.elements);
+        }
+        return false;
+    }
+
+    /**
+     * Returns a hash code value based on the data values in this object.
+     */
+    @Override
+    public final int hashCode() {
+        return ((numRow << Short.SIZE) | numCol) ^ Arrays.hashCode(elements) ^ (int)
serialVersionUID;
+    }
+
+    /**
      * {@inheritDoc}
      */
     @Override

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=1528555&r1=1528554&r2=1528555&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] Wed Oct  2 16:35:33 2013
@@ -16,7 +16,9 @@
  */
 package org.apache.sis.referencing.operation.matrix;
 
+import java.util.Arrays;
 import org.opengis.referencing.operation.Matrix;
+import org.apache.sis.internal.util.Numerics;
 import org.apache.sis.math.MathFunctions;
 
 
@@ -319,4 +321,36 @@ public final class Matrix3 extends Matri
     public MatrixSIS solve(final Matrix matrix) throws MismatchedMatrixSizeException, NoninvertibleMatrixException
{
         throw new UnsupportedOperationException(); // TODO
     }
+
+    /**
+     * Returns {@code true} if the specified object is of type {@code Matrix3} and
+     * all of the data members are equal to the corresponding data members in this matrix.
+     *
+     * @param object The object to compare with this matrix for equality.
+     * @return {@code true} if the given object is equal to this matrix.
+     */
+    @Override
+    public boolean equals(final Object object) {
+        if (object instanceof Matrix3) {
+            final Matrix3 that = (Matrix3) object;
+            return Numerics.equals(this.m00, that.m00) &&
+                   Numerics.equals(this.m01, that.m01) &&
+                   Numerics.equals(this.m02, that.m02) &&
+                   Numerics.equals(this.m10, that.m10) &&
+                   Numerics.equals(this.m11, that.m11) &&
+                   Numerics.equals(this.m12, that.m12) &&
+                   Numerics.equals(this.m20, that.m20) &&
+                   Numerics.equals(this.m21, that.m21) &&
+                   Numerics.equals(this.m22, that.m22);
+        }
+        return false;
+    }
+
+    /**
+     * Returns a hash code value based on the data values in this object.
+     */
+    @Override
+    public int hashCode() {
+        return Arrays.hashCode(getElements()) ^ (int) serialVersionUID;
+    }
 }

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=1528555&r1=1528554&r2=1528555&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] Wed Oct  2 16:35:33 2013
@@ -16,7 +16,9 @@
  */
 package org.apache.sis.referencing.operation.matrix;
 
+import java.util.Arrays;
 import org.opengis.referencing.operation.Matrix;
+import org.apache.sis.internal.util.Numerics;
 import org.apache.sis.math.MathFunctions;
 
 
@@ -370,4 +372,43 @@ public final class Matrix4 extends Matri
     public MatrixSIS solve(final Matrix matrix) throws MismatchedMatrixSizeException, NoninvertibleMatrixException
{
         throw new UnsupportedOperationException(); // TODO
     }
+
+    /**
+     * Returns {@code true} if the specified object is of type {@code Matrix4} and
+     * all of the data members are equal to the corresponding data members in this matrix.
+     *
+     * @param object The object to compare with this matrix for equality.
+     * @return {@code true} if the given object is equal to this matrix.
+     */
+    @Override
+    public boolean equals(final Object object) {
+        if (object instanceof Matrix4) {
+            final Matrix4 that = (Matrix4) object;
+            return Numerics.equals(this.m00, that.m00) &&
+                   Numerics.equals(this.m01, that.m01) &&
+                   Numerics.equals(this.m02, that.m02) &&
+                   Numerics.equals(this.m03, that.m03) &&
+                   Numerics.equals(this.m10, that.m10) &&
+                   Numerics.equals(this.m11, that.m11) &&
+                   Numerics.equals(this.m12, that.m12) &&
+                   Numerics.equals(this.m13, that.m13) &&
+                   Numerics.equals(this.m20, that.m20) &&
+                   Numerics.equals(this.m21, that.m21) &&
+                   Numerics.equals(this.m22, that.m22) &&
+                   Numerics.equals(this.m23, that.m23) &&
+                   Numerics.equals(this.m30, that.m30) &&
+                   Numerics.equals(this.m31, that.m31) &&
+                   Numerics.equals(this.m32, that.m32) &&
+                   Numerics.equals(this.m33, that.m33);
+        }
+        return false;
+    }
+
+    /**
+     * Returns a hash code value based on the data values in this object.
+     */
+    @Override
+    public int hashCode() {
+        return Arrays.hashCode(getElements()) ^ (int) serialVersionUID;
+    }
 }

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/MatrixSIS.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/MatrixSIS.java?rev=1528555&r1=1528554&r2=1528555&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/MatrixSIS.java
(original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/MatrixSIS.java
Wed Oct  2 16:35:33 2013
@@ -16,7 +16,6 @@
  */
 package org.apache.sis.referencing.operation.matrix;
 
-import java.util.Arrays;
 import java.io.Serializable;
 import org.opengis.referencing.operation.Matrix;
 import org.apache.sis.util.ArgumentChecks;
@@ -127,17 +126,6 @@ public abstract class MatrixSIS implemen
     }
 
     /**
-     * Returns all matrix elements, possibly including the error terms for extended-precision
arithmetic.
-     * If the returned array contains error terms, then the array will have twice the normal
length.
-     * See {@link GeneralMatrix#elements} for more discussion.
-     *
-     * <p>This method may return a direct reference to the internal array. <strong>Do
not modify.</strong></p>
-     */
-    double[] getExtendedElements() {
-        return getElements();
-    }
-
-    /**
      * Returns a copy of all matrix elements in a flat, row-major (column indices vary fastest)
array.
      * The array length is <code>{@linkplain #getNumRow()} * {@linkplain #getNumCol()}</code>.
      *
@@ -304,33 +292,6 @@ public abstract class MatrixSIS implemen
     }
 
     /**
-     * Returns {@code true} if the specified object is of same type than {@code this} and
-     * all of the data members are equal to the corresponding data members in this matrix.
-     *
-     * @param object The object to compare with this matrix for equality.
-     * @return {@code true} if the given object is equal to this matrix.
-     */
-    @Override
-    public boolean equals(final Object object) {
-        if (object != null && object.getClass() == getClass()) {
-            final MatrixSIS that = (MatrixSIS) object;
-            return getNumRow() == that.getNumRow() &&
-                   getNumCol() == that.getNumCol() &&
-                   Arrays.equals(getExtendedElements(), that.getExtendedElements());
-        }
-        return false;
-    }
-
-    /**
-     * Returns a hash code value based on the data values in this object.
-     */
-    @Override
-    public int hashCode() {
-        return (getNumRow() << Short.SIZE) ^ getNumCol() ^
-                Arrays.hashCode(getExtendedElements()) ^ (int) serialVersionUID;
-    }
-
-    /**
      * Returns a clone of this matrix.
      *
      * @return A new matrix of the same class and with the same values than this matrix.



Mime
View raw message