sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1522019 - in /sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix: GeneralMatrix.java Matrices.java Matrix1.java Matrix2.java Matrix3.java Matrix4.java MatrixSIS.java
Date Wed, 11 Sep 2013 20:18:43 GMT
Author: desruisseaux
Date: Wed Sep 11 20:18:42 2013
New Revision: 1522019

URL: http://svn.apache.org/r1522019
Log:
Remove the 'setToZero()' method, replaced by a 'createZero' constructor.
This is because search in existing code base has shown that 'setToZero()'
was used only for clearing a newly allocated matrix.

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/Matrices.java
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix1.java
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix2.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=1522019&r1=1522018&r2=1522019&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 Sep 11 20:18:42 2013
@@ -18,6 +18,7 @@ package org.apache.sis.referencing.opera
 
 import java.util.Arrays;
 import org.opengis.referencing.operation.Matrix;
+import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.math.MathFunctions;
 
@@ -51,18 +52,23 @@ class GeneralMatrix extends MatrixSIS {
 
     /**
      * Creates a matrix of size {@code numRow} × {@code numCol}.
-     * Elements on the diagonal (<var>j</var> == <var>i</var>) are
set to 1.
+     * If {@code setToIdentity} is {@code true}, then the elements
+     * on the diagonal (<var>j</var> == <var>i</var>) are set to
1.
      *
      * @param numRow Number of rows.
      * @param numCol Number of columns.
+     * @param setToIdentity {@code true} for initializing the matrix to the identity matrix,
+     *        or {@code false} for leaving it initialized to zero.
      */
-    public GeneralMatrix(final int numRow, final int numCol) {
+    public GeneralMatrix(final int numRow, final int numCol, final boolean setToIdentity)
{
         ensureValidSize(numRow, numCol);
         this.numRow = (short) numRow;
         this.numCol = (short) numCol;
         elements = new double[numRow * numCol];
-        for (int i=0; i<elements.length; i += numCol+1) {
-            elements[i] = 1;
+        if (setToIdentity) {
+            for (int i=0; i<elements.length; i += numCol+1) {
+                elements[i] = 1;
+            }
         }
     }
 
@@ -80,7 +86,7 @@ class GeneralMatrix extends MatrixSIS {
         ensureLengthMatch(numRow*numCol, elements);
         this.numRow = (short) numRow;
         this.numCol = (short) numCol;
-        this.elements = elements;
+        this.elements = elements.clone();
     }
 
     /**
@@ -230,7 +236,7 @@ class GeneralMatrix extends MatrixSIS {
      */
     @Override
     public final void setToIdentity() {
-        setToZero();
+        Arrays.fill(elements, 0);
         for (int i=0; i<elements.length; i += numCol+1) {
             elements[i] = 1;
         }
@@ -240,14 +246,6 @@ class GeneralMatrix extends MatrixSIS {
      * {@inheritDoc}
      */
     @Override
-    public final void setToZero() {
-        Arrays.fill(elements, 0);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
     public final void negate() {
         for (int i=0; i<elements.length; i++) {
             elements[i] = -elements[i];
@@ -300,7 +298,23 @@ class GeneralMatrix extends MatrixSIS {
      */
     @Override
     public MatrixSIS multiply(final Matrix matrix) {
-        throw new UnsupportedOperationException(); // TODO
+        final int nc = matrix.getNumCol();
+        if (matrix.getNumRow() != numCol) {
+            throw new MismatchedMatrixSizeException(Errors.format(
+                    Errors.Keys.MismatchedMatrixSize_4, numCol, nc, matrix.getNumRow(), nc));
+        }
+        final MatrixSIS result = Matrices.createZero(numRow, nc);
+        for (int j=0; j<numRow; j++) {
+            final int srcOff = j * numCol;
+            for (int i=0; i<nc; i++) {
+                double sum = 0;
+                for (int k=0; k<numCol; k++) {
+                    sum += elements[srcOff + k] * matrix.getElement(k, i);
+                }
+                result.setElement(j, i, sum);
+            }
+        }
+        return result;
     }
 
     /**

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java?rev=1522019&r1=1522018&r2=1522019&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
[UTF-8] Wed Sep 11 20:18:42 2013
@@ -59,18 +59,18 @@ public final class Matrices extends Stat
      * @param size Numbers of row and columns.
      * @return An identity matrix of the given size.
      */
-    public static MatrixSIS create(final int size) {
+    public static MatrixSIS createIdentity(final int size) {
         switch (size) {
             case 1: return new Matrix1();
             case 2: return new Matrix2();
             case 3: return new Matrix3();
             case 4: return new Matrix4();
         }
-        return new GeneralMatrix(size, size);
+        return new GeneralMatrix(size, size, true);
     }
 
     /**
-     * Creates an identity matrix of size {@code numRow} × {@code numCol}.
+     * Creates a matrix of size {@code numRow} × {@code numCol}.
      * Elements on the diagonal (<var>j</var> == <var>i</var>) are
set to 1.
      *
      * @param numRow For an affine transform, this is the number of {@linkplain MathTransform#getTargetDimensions()
target dimensions} + 1.
@@ -79,13 +79,33 @@ public final class Matrices extends Stat
      */
     public static MatrixSIS create(final int numRow, final int numCol) {
         if (numRow == numCol) {
-            return create(numRow);
+            return createIdentity(numRow);
         } else {
-            return new GeneralMatrix(numRow, numCol);
+            return new GeneralMatrix(numRow, numCol, true);
         }
     }
 
     /**
+     * Creates a matrix of size {@code numRow} × {@code numCol} filled with zero values.
+     * This constructor is convenient when the caller want to initialize the matrix elements
himself.
+     *
+     * @param numRow For an affine transform, this is the number of {@linkplain MathTransform#getTargetDimensions()
target dimensions} + 1.
+     * @param numCol For an affine transform, this is the number of {@linkplain MathTransform#getSourceDimensions()
source dimensions} + 1.
+     * @return A matrix of the given size with only zero values.
+     */
+    public static MatrixSIS createZero(final int numRow, final int numCol) {
+        if (numRow == numCol) {
+            switch (numRow) {
+                case 1: return new Matrix1(false);
+                case 2: return new Matrix2(false);
+                case 3: return new Matrix3(false);
+                case 4: return new Matrix4(false);
+            }
+        }
+        return new GeneralMatrix(numRow, numCol, false);
+    }
+
+    /**
      * Creates a matrix of size {@code numRow} × {@code numCol} initialized to the given
elements.
      * The elements array size must be equals to {@code numRow*numCol}. Column indices vary
fastest.
      *

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix1.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix1.java?rev=1522019&r1=1522018&r2=1522019&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix1.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix1.java
[UTF-8] Wed Sep 11 20:18:42 2013
@@ -64,6 +64,14 @@ public final class Matrix1 extends Matri
     }
 
     /**
+     * Creates a new matrix filled with only zero values.
+     *
+     * @param ignore Shall always be {@code false} in current version.
+     */
+    Matrix1(final boolean ignore) {
+    }
+
+    /**
      * Creates a new matrix initialized to the specified value.
      *
      * @param m00 The element in this matrix.
@@ -216,14 +224,6 @@ public final class Matrix1 extends Matri
      * {@inheritDoc}
      */
     @Override
-    public final void setToZero() {
-        m00 = 0;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
     public void negate() {
         m00 = -m00;
     }

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix2.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix2.java?rev=1522019&r1=1522018&r2=1522019&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix2.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix2.java
[UTF-8] Wed Sep 11 20:18:42 2013
@@ -65,6 +65,14 @@ public final class Matrix2 extends Matri
     }
 
     /**
+     * Creates a new matrix filled with only zero values.
+     *
+     * @param ignore Shall always be {@code false} in current version.
+     */
+    Matrix2(final boolean ignore) {
+    }
+
+    /**
      * Creates a new matrix initialized to the specified values.
      *
      * @param m00 The first matrix element in the first row.
@@ -233,14 +241,6 @@ public final class Matrix2 extends Matri
      * {@inheritDoc}
      */
     @Override
-    public final void setToZero() {
-        m00 = m01 = m10 = m11 = 0;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
     public void negate() {
         m00 = -m00;
         m01 = -m01;

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=1522019&r1=1522018&r2=1522019&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 Sep 11 20:18:42 2013
@@ -73,6 +73,14 @@ public final class Matrix3 extends Matri
     }
 
     /**
+     * Creates a new matrix filled with only zero values.
+     *
+     * @param ignore Shall always be {@code false} in current version.
+     */
+    Matrix3(final boolean ignore) {
+    }
+
+    /**
      * Creates a new matrix initialized to the specified values.
      *
      * @param m00 The first matrix element in the first row.
@@ -270,10 +278,9 @@ public final class Matrix3 extends Matri
     }
 
     /**
-     * {@inheritDoc}
+     * Sets all the values in this matrix to zero.
      */
-    @Override
-    public final void setToZero() {
+    private void setToZero() {
         m00 = m01 = m02 = 0;
         m10 = m11 = m12 = 0;
         m20 = m21 = m22 = 0;

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=1522019&r1=1522018&r2=1522019&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 Sep 11 20:18:42 2013
@@ -81,6 +81,14 @@ public final class Matrix4 extends Matri
     }
 
     /**
+     * Creates a new matrix filled with only zero values.
+     *
+     * @param ignore Shall always be {@code false} in current version.
+     */
+    Matrix4(final boolean ignore) {
+    }
+
+    /**
      * Creates a new matrix initialized to the specified values.
      *
      * @param m00 The first matrix element in the first row.
@@ -317,10 +325,9 @@ public final class Matrix4 extends Matri
     }
 
     /**
-     * {@inheritDoc}
+     * Sets all the values in this matrix to zero.
      */
-    @Override
-    public final void setToZero() {
+    private void setToZero() {
         m00 = m01 = m02 = m03 = 0;
         m10 = m11 = m12 = m13 = 0;
         m20 = m21 = m22 = m23 = 0;

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=1522019&r1=1522018&r2=1522019&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 Sep 11 20:18:42 2013
@@ -166,11 +166,6 @@ public abstract class MatrixSIS implemen
     public abstract void setToIdentity();
 
     /**
-     * Sets all the values in this matrix to zero.
-     */
-    public abstract void setToZero();
-
-    /**
      * Negates the values of this matrix: {@code this} = {@code -this}.
      */
     public abstract void negate();



Mime
View raw message