commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r731231 - in /commons/proper/math/trunk/src/java/org/apache/commons/math/linear: AbstractRealMatrix.java RealMatrix.java
Date Sun, 04 Jan 2009 12:07:43 GMT
Author: luc
Date: Sun Jan  4 04:07:43 2009
New Revision: 731231

URL: http://svn.apache.org/viewvc?rev=731231&view=rev
Log:
added copySubmatrix methods
use matrix visitors to implement some existing methods more efficiently

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrix.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java?rev=731231&r1=731230&r2=731231&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java
Sun Jan  4 04:07:43 2009
@@ -285,24 +285,106 @@
     }
 
     /** {@inheritDoc} */
-    public RealMatrix getSubMatrix(int[] selectedRows, int[] selectedColumns)
+    public RealMatrix getSubMatrix(final int[] selectedRows, final int[] selectedColumns)
         throws MatrixIndexException {
 
+        // safety checks
         checkSubMatrixIndex(selectedRows, selectedColumns);
 
+        // copy entries
         final RealMatrix subMatrix =
             createMatrix(selectedRows.length, selectedColumns.length);
-        for (int i = 0; i < selectedRows.length; i++) {
-            for (int j = 0; j < selectedColumns.length; j++) {
-                subMatrix.setEntry(i, j, getEntry(selectedRows[i], selectedColumns[j]));
+        subMatrix.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
+
+            /** Serializable version identifier. */
+            private static final long serialVersionUID = 4572851009041214720L;
+
+            /** {@inheritDoc} */
+            public double visit(final int row, final int column, final double value) {
+                return getEntry(selectedRows[row], selectedColumns[column]);
             }
-        }
+
+        });
 
         return subMatrix;
 
     } 
 
     /** {@inheritDoc} */
+    public void copySubMatrix(final int startRow, final int endRow,
+                              final int startColumn, final int endColumn,
+                              final double[][] destination)
+        throws MatrixIndexException, IllegalArgumentException {
+
+        // safety checks
+        checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
+        final int rowsCount    = endRow + 1 - startRow;
+        final int columnsCount = endColumn + 1 - startColumn;
+        if ((destination.length < rowsCount) || (destination[0].length < columnsCount))
{
+            throw MathRuntimeException.createIllegalArgumentException(
+                    "dimensions mismatch: got {0}x{1} but expected {2}x{3}",
+                    new Object[] {
+                        destination.length, destination[0].length,
+                        rowsCount, columnsCount
+                    });
+        }
+
+        // copy entries
+        walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
+
+            /** Serializable version identifier. */
+            private static final long serialVersionUID = -6302162622577015104L;
+
+            /** Initial row index. */
+            private int startRow;
+
+            /** Initial column index. */
+            private int startColumn;
+
+            /** {@inheritDoc} */
+            public void start(final int rows, final int columns,
+                              final int startRow, final int endRow,
+                              final int startColumn, final int endColumn) {
+                this.startRow    = startRow;
+                this.startColumn = startColumn;
+            }
+
+            /** {@inheritDoc} */
+            public void visit(final int row, final int column, final double value) {
+                destination[row - startRow][column - startColumn] = value;
+            }
+
+        }, startRow, endRow, startColumn, endColumn);
+
+    }
+
+    /** {@inheritDoc} */
+    public void copySubMatrix(int[] selectedRows, int[] selectedColumns, double[][] destination)
+        throws MatrixIndexException, IllegalArgumentException {
+
+        // safety checks
+        checkSubMatrixIndex(selectedRows, selectedColumns);
+        if ((destination.length < selectedRows.length) ||
+            (destination[0].length < selectedColumns.length)) {
+            throw MathRuntimeException.createIllegalArgumentException(
+                    "dimensions mismatch: got {0}x{1} but expected {2}x{3}",
+                    new Object[] {
+                        destination.length, destination[0].length,
+                        selectedRows.length, selectedColumns.length
+                    });
+        }
+
+        // copy entries
+        for (int i = 0; i < selectedRows.length; i++) {
+            final double[] destinationI = destination[i];
+            for (int j = 0; j < selectedColumns.length; j++) {
+                destinationI[j] = getEntry(selectedRows[i], selectedColumns[j]);
+            }
+        }
+
+    }
+
+    /** {@inheritDoc} */
     public void setSubMatrix(final double[][] subMatrix, final int row, final int column)

         throws MatrixIndexException {
 
@@ -554,27 +636,16 @@
         final int nRows = getRowDimension();
         final int nCols = getColumnDimension();
         final RealMatrix out = createMatrix(nCols, nRows);
-        walkInOptimizedOrder(new RealMatrixPreservingVisitor() {
+        walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
 
-            /** Serializable version identifier */
-            private static final long serialVersionUID = 3807296710038754174L;
-
-            /** {@inheritDoc} */
-            public void start(final int rows, final int columns,
-                              final int startRow, final int endRow,
-                              final int startColumn, final int endColumn) {
-            }
+            /** Serializable version identifier. */
+            private static final long serialVersionUID = 2482589609486637597L;
 
             /** {@inheritDoc} */
             public void visit(final int row, final int column, final double value) {
                 out.setEntry(column, row, value);
             }
 
-            /** {@inheritDoc} */
-            public double end() {
-                return 0;
-            }
-
         });
 
         return out;

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrix.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrix.java?rev=731231&r1=731230&r2=731231&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrix.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrix.java Sun
Jan  4 04:07:43 2009
@@ -136,8 +136,8 @@
      *         specified rows and columns
      * @exception MatrixIndexException  if the indices are not valid
      */
-   RealMatrix getSubMatrix(int startRow, int endRow, int startColumn,
-            int endColumn) throws MatrixIndexException;
+   RealMatrix getSubMatrix(int startRow, int endRow, int startColumn, int endColumn)
+       throws MatrixIndexException;
    
    /**
     * Gets a submatrix. Rows and columns are indicated
@@ -150,9 +150,40 @@
     * @exception MatrixIndexException if row or column selections are not valid
     */
    RealMatrix getSubMatrix(int[] selectedRows, int[] selectedColumns)
-   throws MatrixIndexException;
+       throws MatrixIndexException;
 
    /**
+    * Copy a submatrix. Rows and columns are indicated
+    * counting from 0 to n-1.
+    *
+    * @param startRow Initial row index
+    * @param endRow Final row index (inclusive)
+    * @param startColumn Initial column index
+    * @param endColumn Final column index (inclusive)
+    * @param destination The arrays where the submatrix data should be copied
+    * (if larger than rows/columns counts, only the upper-left part will be used)
+    * @exception MatrixIndexException if the indices are not valid
+    * @exception IllegalArgumentException if the destination array is too small
+    */
+  void copySubMatrix(int startRow, int endRow, int startColumn, int endColumn,
+                     double[][] destination)
+      throws MatrixIndexException, IllegalArgumentException;
+  
+  /**
+   * Copy a submatrix. Rows and columns are indicated
+   * counting from 0 to n-1.
+   *
+    * @param selectedRows Array of row indices.
+    * @param selectedColumns Array of column indices.
+   * @param destination The arrays where the submatrix data should be copied
+   * (if larger than rows/columns counts, only the upper-left part will be used)
+   * @exception MatrixIndexException if the indices are not valid
+   * @exception IllegalArgumentException if the destination array is too small
+   */
+  void copySubMatrix(int[] selectedRows, int[] selectedColumns, double[][] destination)
+      throws MatrixIndexException, IllegalArgumentException;
+ 
+   /**
     * Replace the submatrix starting at <code>row, column</code> using data in
     * the input <code>subMatrix</code> array. Indexes are 0-based.
     * <p> 



Mime
View raw message