sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1522485 - in /sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix: GeneralMatrix.java Matrices.java MatrixSIS.java NonSquareMatrix.java
Date Thu, 12 Sep 2013 08:41:22 GMT
Author: desruisseaux
Date: Thu Sep 12 08:41:22 2013
New Revision: 1522485

URL: http://svn.apache.org/r1522485
Log:
Added a package-private subtype for non-square matrix. The 'inverse()' method of that subtype
will perform some additional check for independent axes before to attempt the matrix inversion.

Added:
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrix.java
  (with props)
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/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=1522485&r1=1522484&r2=1522485&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] Thu Sep 12 08:41:22 2013
@@ -43,12 +43,13 @@ class GeneralMatrix extends MatrixSIS {
      * All matrix elements in a flat, row-major (column indices vary fastest) array.
      * The array length is <code>{@linkplain #numRow} * {@linkplain #numCol}</code>.
      */
-    private final double[] elements;
+    final double[] elements;
 
     /**
      * Number of rows and columns.
+     * This is non-final only for {@link NonSquareMatrix#setToTranspose()} purpose.
      */
-    private final short numRow, numCol;
+    short numRow, numCol;
 
     /**
      * Creates a matrix of size {@code numRow} × {@code numCol}.
@@ -60,7 +61,7 @@ class GeneralMatrix extends MatrixSIS {
      * @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, final boolean setToIdentity)
{
+    GeneralMatrix(final int numRow, final int numCol, final boolean setToIdentity) {
         ensureValidSize(numRow, numCol);
         this.numRow = (short) numRow;
         this.numCol = (short) numCol;
@@ -81,7 +82,7 @@ class GeneralMatrix extends MatrixSIS {
      * @param numCol Number of columns.
      * @param elements Initial values.
      */
-    public GeneralMatrix(final int numRow, final int numCol, final double[] elements) {
+    GeneralMatrix(final int numRow, final int numCol, final double[] elements) {
         ensureValidSize(numRow, numCol);
         ensureLengthMatch(numRow*numCol, elements);
         this.numRow = (short) numRow;
@@ -94,7 +95,7 @@ class GeneralMatrix extends MatrixSIS {
      *
      * @param matrix The matrix to copy.
      */
-    public GeneralMatrix(final Matrix matrix) {
+    GeneralMatrix(final Matrix matrix) {
         final int numRow = matrix.getNumRow();
         final int numCol = matrix.getNumCol();
         ensureValidSize(numRow, numCol);
@@ -195,6 +196,8 @@ class GeneralMatrix extends MatrixSIS {
      */
     @Override
     public final boolean isAffine() {
+        final int numRow = this.numRow; // Protection against accidental changes.
+        final int numCol = this.numCol;
         if (numRow == numCol) {
             int i = elements.length;
             if (elements[--i] == 1) {
@@ -215,6 +218,8 @@ class GeneralMatrix extends MatrixSIS {
      */
     @Override
     public final boolean isIdentity() {
+        final int numRow = this.numRow; // Protection against accidental changes.
+        final int numCol = this.numCol;
         if (numRow != numCol) {
             return false;
         }
@@ -236,6 +241,7 @@ class GeneralMatrix extends MatrixSIS {
      */
     @Override
     public final void setToIdentity() {
+        final int numCol = this.numCol; // Protection against accidental changes.
         Arrays.fill(elements, 0);
         for (int i=0; i<elements.length; i += numCol+1) {
             elements[i] = 1;
@@ -244,9 +250,14 @@ class GeneralMatrix extends MatrixSIS {
 
     /**
      * {@inheritDoc}
+     *
+     * The implementation provided by {@code GeneralMatrix} is valid only for square matrix.
+     * {@link NonSquareMatrix} must override.
      */
     @Override
-    public final void setToTranspose() {
+    public void setToTranspose() {
+        final int numRow = this.numRow; // Protection against accidental changes.
+        final int numCol = this.numCol;
         for (int j=0; j<numRow; j++) {
             for (int i=0; i<j; i++) {
                 final int lowerLeft  = j*numCol + i;
@@ -263,6 +274,8 @@ class GeneralMatrix extends MatrixSIS {
      */
     @Override
     public final void normalizeColumns() {
+        final int numRow = this.numRow; // Protection against accidental changes.
+        final int numCol = this.numCol;
         final double[] column = new double[numRow];
         for (int i=0; i<numCol; i++) {
             for (int j=0; j<numRow; j++) {
@@ -288,6 +301,8 @@ class GeneralMatrix extends MatrixSIS {
      */
     @Override
     public MatrixSIS multiply(final Matrix matrix) {
+        final int numRow = this.numRow; // Protection against accidental changes.
+        final int numCol = this.numCol;
         final int nc = matrix.getNumCol();
         if (matrix.getNumRow() != numCol) {
             throw new MismatchedMatrixSizeException(Errors.format(

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=1522485&r1=1522484&r2=1522485&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] Thu Sep 12 08:41:22 2013
@@ -16,6 +16,7 @@
  */
 package org.apache.sis.referencing.operation.matrix;
 
+import java.awt.geom.AffineTransform;
 import org.opengis.referencing.operation.Matrix;
 import org.opengis.referencing.operation.MathTransform;
 import org.apache.sis.util.Static;
@@ -26,7 +27,6 @@ import org.apache.sis.internal.util.Nume
 
 // Related to JDK7
 import java.util.Objects;
-import java.awt.geom.AffineTransform;
 
 
 /**
@@ -53,26 +53,36 @@ public final class Matrices extends Stat
      * Creates a square identity matrix of size {@code size} × {@code size}.
      * Elements on the diagonal (<var>j</var> == <var>i</var>) are
set to 1.
      *
-     * <p>For an affine transform, the {@code size} is the number of
-     * {@linkplain MathTransform#getSourceDimensions() source} and
-     * {@linkplain MathTransform#getTargetDimensions() target} dimensions + 1.</p>
-     *
-     * @param size Numbers of row and columns.
+     * {@section Implementation note}
+     * For sizes between {@value org.apache.sis.referencing.operation.matrix.Matrix1#SIZE}
and
+     * {@value org.apache.sis.referencing.operation.matrix.Matrix4#SIZE} inclusive, the matrix
+     * is guaranteed to be an instance of one of {@link Matrix1} … {@link Matrix4} subtypes.
+     *
+     * @param  size Numbers of row and columns. For an affine transform, this is the number
of
+     *         {@linkplain MathTransform#getSourceDimensions() source} and
+     *         {@linkplain MathTransform#getTargetDimensions() target} dimensions + 1.
      * @return An identity matrix of the given 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();
+            case 1:  return new Matrix1();
+            case 2:  return new Matrix2();
+            case 3:  return new Matrix3();
+            case 4:  return new Matrix4();
+            default: return new GeneralMatrix(size, size, true);
         }
-        return new GeneralMatrix(size, size, true);
     }
 
     /**
      * Creates a matrix of size {@code numRow} × {@code numCol}.
      * Elements on the diagonal (<var>j</var> == <var>i</var>) are
set to 1.
+     * The result is an identity matrix if {@code numRow} = {@code numCol}.
+     *
+     * {@section Implementation note}
+     * For {@code numRow} == {@code numCol} with a value between
+     * {@value org.apache.sis.referencing.operation.matrix.Matrix1#SIZE} and
+     * {@value org.apache.sis.referencing.operation.matrix.Matrix4#SIZE} inclusive, the matrix
+     * is guaranteed to be an instance of one of {@link Matrix1} … {@link Matrix4} subtypes.
      *
      * @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.
@@ -82,7 +92,7 @@ public final class Matrices extends Stat
         if (numRow == numCol) {
             return createIdentity(numRow);
         } else {
-            return new GeneralMatrix(numRow, numCol, true);
+            return new NonSquareMatrix(numRow, numCol, true);
         }
     }
 
@@ -90,6 +100,12 @@ public final class Matrices extends Stat
      * 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.
      *
+     * {@section Implementation note}
+     * For {@code numRow} == {@code numCol} with a value between
+     * {@value org.apache.sis.referencing.operation.matrix.Matrix1#SIZE} and
+     * {@value org.apache.sis.referencing.operation.matrix.Matrix4#SIZE} inclusive, the matrix
+     * is guaranteed to be an instance of one of {@link Matrix1} … {@link Matrix4} subtypes.
+     *
      * @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.
@@ -97,19 +113,26 @@ public final class Matrices extends Stat
     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);
+                case 1:  return new Matrix1(false);
+                case 2:  return new Matrix2(false);
+                case 3:  return new Matrix3(false);
+                case 4:  return new Matrix4(false);
+                default: return new GeneralMatrix(numRow, numCol, false);
             }
         }
-        return new GeneralMatrix(numRow, numCol, false);
+        return new NonSquareMatrix(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.
      *
+     * {@section Implementation note}
+     * For {@code numRow} == {@code numCol} with a value between
+     * {@value org.apache.sis.referencing.operation.matrix.Matrix1#SIZE} and
+     * {@value org.apache.sis.referencing.operation.matrix.Matrix4#SIZE} inclusive, the matrix
+     * is guaranteed to be an instance of one of {@link Matrix1} … {@link Matrix4} subtypes.
+     *
      * @param  numRow   Number of rows.
      * @param  numCol   Number of columns.
      * @param  elements The matrix elements in a row-major array. Column indices vary fastest.
@@ -130,8 +153,15 @@ public final class Matrices extends Stat
     /**
      * Creates a new matrix which is a copy of the given matrix.
      *
+     * {@section Implementation note}
+     * For square matrix with a size between {@value org.apache.sis.referencing.operation.matrix.Matrix1#SIZE}
+     * and {@value org.apache.sis.referencing.operation.matrix.Matrix4#SIZE} inclusive, the
returned matrix is
+     * guaranteed to be an instance of one of {@link Matrix1} … {@link Matrix4} subtypes.
+     *
      * @param matrix The matrix to copy, or {@code null}.
      * @return A copy of the given matrix, or {@code null} if the given matrix was null.
+     *
+     * @see MatrixSIS#clone()
      */
     public static MatrixSIS copy(final Matrix matrix) {
         if (matrix == null) {

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=1522485&r1=1522484&r2=1522485&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
Thu Sep 12 08:41:22 2013
@@ -162,6 +162,9 @@ public abstract class MatrixSIS implemen
      *     }
      * }
      *
+     * Be aware that in the particular case of datum shifts, the matrix may be very close
to an identity matrix.
+     * Such matrices for datum shift may look like spurious results of rounding errors, but
are not.
+     *
      * @see #isIdentity()
      * @see java.awt.geom.AffineTransform#setToIdentity()
      */
@@ -265,6 +268,8 @@ public abstract class MatrixSIS implemen
      * Returns a clone of this matrix.
      *
      * @return A new matrix of the same class and with the same values than this matrix.
+     *
+     * @see Matrices#copy(Matrix)
      */
     @Override
     public MatrixSIS clone() {

Added: 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=1522485&view=auto
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrix.java
(added)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrix.java
[UTF-8] Thu Sep 12 08:41:22 2013
@@ -0,0 +1,106 @@
+/*
+ * 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;
+
+
+/**
+ * A matrix which is not square.
+ *
+ * @author  Martin Desruisseaux
+ * @since   0.4
+ * @version 0.4
+ * @module
+ *
+ * @see Matrices#create(int, int)
+ */
+final class NonSquareMatrix extends GeneralMatrix {
+    /**
+     * Serial number for inter-operability with different versions.
+     */
+    private static final long serialVersionUID = 5481206711680231697L;
+
+    /**
+     * Creates a matrix of size {@code numRow} × {@code numCol}.
+     * 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.
+     */
+    NonSquareMatrix(final int numRow, final int numCol, final boolean setToIdentity) {
+        super(numRow, numCol, setToIdentity);
+    }
+
+    /**
+     * Constructs a {@code numRow} × {@code numCol} matrix initialized to the values in
the {@code elements} array.
+     * The array values are copied in one row at a time in row major fashion.
+     * The array shall be exactly {@code numRow*numCol} in length.
+     *
+     * @param numRow Number of rows.
+     * @param numCol Number of columns.
+     * @param elements Initial values.
+     */
+    NonSquareMatrix(final int numRow, final int numCol, final double[] elements) {
+        super(numRow, numCol, elements);
+    }
+
+    /**
+     * Constructs a new matrix and copies the initial values from the given matrix.
+     *
+     * @param matrix The matrix to copy.
+     */
+    NonSquareMatrix(final Matrix matrix) {
+        super(matrix);
+    }
+
+    /**
+     * Creates a clone of the given matrix, for {@link #clone()} usage only.
+     */
+    private NonSquareMatrix(final GeneralMatrix matrix) {
+        super(matrix);
+    }
+
+    /**
+     * Sets the value of this matrix to its transpose.
+     */
+    @Override
+    public void setToTranspose() {
+        final short numRow = this.numRow; // Protection against accidental changes before
we are done.
+        final short numCol = this.numCol;
+        final double[] copy = elements.clone();
+        int k = 0;
+        for (int j=0; j<numRow; j++) {
+            for (int i=0; i<numCol; i++) {
+                elements[i*numCol + j] = copy[k++];
+            }
+        }
+        this.numRow = numCol;
+        this.numCol = numRow;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public MatrixSIS clone() {
+        return new NonSquareMatrix(this);
+    }
+}

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

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



Mime
View raw message