commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r730787 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/linear/ site/xdoc/ test/org/apache/commons/math/linear/
Date Fri, 02 Jan 2009 17:07:43 GMT
Author: luc
Date: Fri Jan  2 09:07:42 2009
New Revision: 730787

URL: http://svn.apache.org/viewvc?rev=730787&view=rev
Log:
added column walking order
renamed walkInInternalOrder into walkInOptimizedOrder

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/DenseRealMatrix.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrix.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrixChangingVisitor.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrixImpl.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrixPreservingVisitor.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/org/apache/commons/math/linear/DenseRealMatrixTest.java
    commons/proper/math/trunk/src/test/org/apache/commons/math/linear/RealMatrixImplTest.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=730787&r1=730786&r2=730787&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 Fri Jan  2 09:07:42 2009
@@ -32,7 +32,7 @@
  */
 public abstract class AbstractRealMatrix implements RealMatrix, Serializable {
     
-    /** Serializable version identifier */
+    /** Serializable version identifier. */
     private static final long serialVersionUID = -3665653040524315561L;
 
     /** Cached LU solver.
@@ -195,31 +195,74 @@
 
     /** {@inheritDoc} */
     public double getNorm() {
-        final int rowCount    = getRowDimension();
-        final int columnCount = getColumnDimension();
-        double maxColSum = 0;
-        for (int col = 0; col < columnCount; ++col) {
-            double sum = 0;
-            for (int row = 0; row < rowCount; ++row) {
-                sum += Math.abs(getEntry(row, col));
+        return walkInColumnOrder(new RealMatrixPreservingVisitor() {
+
+            /** Serializable version identifier */
+            private static final long serialVersionUID = -2452270856202894168L;
+
+            /** Last row index. */
+            private double endRow;
+
+            /** Sum of absolute values on one column. */
+            private double columnSum;
+
+            /** Maximal sum across all columns. */
+            private double maxColSum;
+
+            /** {@inheritDoc} */
+            public void start(final int rows, final int columns,
+                              final int startRow, final int endRow,
+                              final int startColumn, final int endColumn) {
+                this.endRow = endRow;
+                columnSum   = 0;
+                maxColSum   = 0;
+            }
+
+            /** {@inheritDoc} */
+            public void visit(final int row, final int column, final double value) {
+                columnSum += Math.abs(value);
+                if (row == endRow) {
+                    maxColSum = Math.max(maxColSum, columnSum);
+                    columnSum = 0;
+                }
             }
-            maxColSum = Math.max(maxColSum, sum);
-        }
-        return maxColSum;
+
+            /** {@inheritDoc} */
+            public double end() {
+                return maxColSum;
+            }
+
+        });
     }
     
     /** {@inheritDoc} */
     public double getFrobeniusNorm() {
-        final int rowCount    = getRowDimension();
-        final int columnCount = getColumnDimension();
-        double sum2 = 0;
-        for (int col = 0; col < columnCount; ++col) {
-            for (int row = 0; row < rowCount; ++row) {
-                final double mij = getEntry(row, col);
-                sum2 += mij * mij;
+        return walkInOptimizedOrder(new RealMatrixPreservingVisitor() {
+
+            /** Serializable version identifier */
+            private static final long serialVersionUID = -6065411033772300640L;
+
+            /** Sum of squared entries. */
+            private double sum;
+
+            /** {@inheritDoc} */
+            public void start(final int rows, final int columns,
+                              final int startRow, final int endRow,
+                              final int startColumn, final int endColumn) {
+                sum = 0;
             }
-        }
-        return Math.sqrt(sum2);
+
+            /** {@inheritDoc} */
+            public void visit(final int row, final int column, final double value) {
+                sum += value * value;
+            }
+
+            /** {@inheritDoc} */
+            public double end() {
+                return Math.sqrt(sum);
+            }
+
+        });
     }
     
     /** {@inheritDoc} */
@@ -511,11 +554,28 @@
         final int nRows = getRowDimension();
         final int nCols = getColumnDimension();
         final RealMatrix out = createMatrix(nCols, nRows);
-        for (int row = 0; row < nRows; ++row) {
-            for (int col = 0; col < nCols; ++col) {
-                out.setEntry(col, row, getEntry(row, col));
+        walkInOptimizedOrder(new RealMatrixPreservingVisitor() {
+
+            /** 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) {
+            }
+
+            /** {@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;
 
@@ -691,10 +751,11 @@
     }
 
     /** {@inheritDoc} */
-    public void walkInRowOrder(final RealMatrixChangingVisitor visitor)
+    public double walkInRowOrder(final RealMatrixChangingVisitor visitor)
         throws MatrixVisitorException {
         final int rows    = getRowDimension();
         final int columns = getColumnDimension();
+        visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
         for (int row = 0; row < rows; ++row) {
             for (int column = 0; column < columns; ++column) {
                 final double oldValue = getEntry(row, column);
@@ -703,26 +764,31 @@
             }
         }
         lu = null;
+        return visitor.end();
     }
 
     /** {@inheritDoc} */
-    public void walkInRowOrder(final RealMatrixPreservingVisitor visitor)
+    public double walkInRowOrder(final RealMatrixPreservingVisitor visitor)
         throws MatrixVisitorException {
         final int rows    = getRowDimension();
         final int columns = getColumnDimension();
+        visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
         for (int row = 0; row < rows; ++row) {
             for (int column = 0; column < columns; ++column) {
                 visitor.visit(row, column, getEntry(row, column));
             }
         }
+        return visitor.end();
     }
 
     /** {@inheritDoc} */
-    public void walkInRowOrder(final RealMatrixChangingVisitor visitor,
-                               final int startRow, final int endRow,
-                               final int startColumn, final int endColumn)
+    public double walkInRowOrder(final RealMatrixChangingVisitor visitor,
+                                 final int startRow, final int endRow,
+                                 final int startColumn, final int endColumn)
         throws MatrixIndexException, MatrixVisitorException {
         checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
+        visitor.start(getRowDimension(), getColumnDimension(),
+                      startRow, endRow, startColumn, endColumn);
         for (int row = startRow; row <= endRow; ++row) {
             for (int column = startColumn; column <= endColumn; ++column) {
                 final double oldValue = getEntry(row, column);
@@ -731,47 +797,116 @@
             }
         }
         lu = null;
+        return visitor.end();
     }
 
     /** {@inheritDoc} */
-    public void walkInRowOrder(final RealMatrixPreservingVisitor visitor,
-                               final int startRow, final int endRow,
-                               final int startColumn, final int endColumn)
+    public double walkInRowOrder(final RealMatrixPreservingVisitor visitor,
+                                 final int startRow, final int endRow,
+                                 final int startColumn, final int endColumn)
         throws MatrixIndexException, MatrixVisitorException {
         checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
+        visitor.start(getRowDimension(), getColumnDimension(),
+                      startRow, endRow, startColumn, endColumn);
         for (int row = startRow; row <= endRow; ++row) {
             for (int column = startColumn; column <= endColumn; ++column) {
                 visitor.visit(row, column, getEntry(row, column));
             }
         }
+        return visitor.end();
     }
 
-    /** {@inheritDoc} */
-    public void walkInInternalOrder(final RealMatrixChangingVisitor visitor)
+    public double walkInColumnOrder(final RealMatrixChangingVisitor visitor)
         throws MatrixVisitorException {
-        walkInRowOrder(visitor);
+        final int rows    = getRowDimension();
+        final int columns = getColumnDimension();
+        visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
+        for (int column = 0; column < columns; ++column) {
+            for (int row = 0; row < rows; ++row) {
+                final double oldValue = getEntry(row, column);
+                final double newValue = visitor.visit(row, column, oldValue);
+                setEntry(row, column, newValue);
+            }
+        }
+        lu = null;
+        return visitor.end();
     }
 
     /** {@inheritDoc} */
-    public void walkInInternalOrder(final RealMatrixPreservingVisitor visitor)
+    public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor)
         throws MatrixVisitorException {
-        walkInRowOrder(visitor);
+        final int rows    = getRowDimension();
+        final int columns = getColumnDimension();
+        visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
+        for (int column = 0; column < columns; ++column) {
+            for (int row = 0; row < rows; ++row) {
+                visitor.visit(row, column, getEntry(row, column));
+            }
+        }
+        return visitor.end();
     }
 
     /** {@inheritDoc} */
-    public void walkInInternalOrder(final RealMatrixChangingVisitor visitor,
+    public double walkInColumnOrder(final RealMatrixChangingVisitor visitor,
                                     final int startRow, final int endRow,
                                     final int startColumn, final int endColumn)
-        throws MatrixIndexException, MatrixVisitorException {
-        walkInRowOrder(visitor, startRow, endRow, startColumn, endColumn);
+    throws MatrixIndexException, MatrixVisitorException {
+        checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
+        visitor.start(getRowDimension(), getColumnDimension(),
+                      startRow, endRow, startColumn, endColumn);
+        for (int column = startColumn; column <= endColumn; ++column) {
+            for (int row = startRow; row <= endRow; ++row) {
+                final double oldValue = getEntry(row, column);
+                final double newValue = visitor.visit(row, column, oldValue);
+                setEntry(row, column, newValue);
+            }
+        }
+        lu = null;
+        return visitor.end();
     }
 
     /** {@inheritDoc} */
-    public void walkInInternalOrder(final RealMatrixPreservingVisitor visitor,
+    public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor,
                                     final int startRow, final int endRow,
                                     final int startColumn, final int endColumn)
+    throws MatrixIndexException, MatrixVisitorException {
+        checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
+        visitor.start(getRowDimension(), getColumnDimension(),
+                      startRow, endRow, startColumn, endColumn);
+        for (int column = startColumn; column <= endColumn; ++column) {
+            for (int row = startRow; row <= endRow; ++row) {
+                visitor.visit(row, column, getEntry(row, column));
+            }
+        }
+        return visitor.end();
+    }
+
+    /** {@inheritDoc} */
+    public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor)
+        throws MatrixVisitorException {
+        return walkInRowOrder(visitor);
+    }
+
+    /** {@inheritDoc} */
+    public double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor)
+        throws MatrixVisitorException {
+        return walkInRowOrder(visitor);
+    }
+
+    /** {@inheritDoc} */
+    public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor,
+                                       final int startRow, final int endRow,
+                                       final int startColumn, final int endColumn)
+        throws MatrixIndexException, MatrixVisitorException {
+        return walkInRowOrder(visitor, startRow, endRow, startColumn, endColumn);
+    }
+
+    /** {@inheritDoc} */
+    public double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor,
+                                       final int startRow, final int endRow,
+                                       final int startColumn, final int endColumn)
         throws MatrixIndexException, MatrixVisitorException {
-        walkInRowOrder(visitor, startRow, endRow, startColumn, endColumn);
+        return walkInRowOrder(visitor, startRow, endRow, startColumn, endColumn);
     }
 
     /** {@inheritDoc} */

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/DenseRealMatrix.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/DenseRealMatrix.java?rev=730787&r1=730786&r2=730787&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/linear/DenseRealMatrix.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/linear/DenseRealMatrix.java Fri Jan  2 09:07:42 2009
@@ -1284,8 +1284,9 @@
     }
 
     /** {@inheritDoc} */
-    public void walkInRowOrder(final RealMatrixChangingVisitor visitor)
+    public double walkInRowOrder(final RealMatrixChangingVisitor visitor)
         throws MatrixVisitorException {
+        visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
         for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
             final int pStart = iBlock * BLOCK_SIZE;
             final int pEnd   = Math.min(pStart + BLOCK_SIZE, rows);
@@ -1301,11 +1302,13 @@
                 }
              }
         }
+        return visitor.end();
     }
 
     /** {@inheritDoc} */
-    public void walkInRowOrder(final RealMatrixPreservingVisitor visitor)
+    public double walkInRowOrder(final RealMatrixPreservingVisitor visitor)
         throws MatrixVisitorException {
+        visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
         for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
             final int pStart = iBlock * BLOCK_SIZE;
             final int pEnd   = Math.min(pStart + BLOCK_SIZE, rows);
@@ -1321,14 +1324,16 @@
                 }
              }
         }
+        return visitor.end();
     }
 
     /** {@inheritDoc} */
-    public void walkInRowOrder(final RealMatrixChangingVisitor visitor,
-                               final int startRow, final int endRow,
-                               final int startColumn, final int endColumn)
+    public double walkInRowOrder(final RealMatrixChangingVisitor visitor,
+                                 final int startRow, final int endRow,
+                                 final int startColumn, final int endColumn)
         throws MatrixIndexException, MatrixVisitorException {
         checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
+        visitor.start(rows, columns, startRow, endRow, startColumn, endColumn);
         for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) {
             final int p0     = iBlock * BLOCK_SIZE;
             final int pStart = Math.max(startRow, p0);
@@ -1346,14 +1351,16 @@
                 }
              }
         }
+        return visitor.end();
     }
 
     /** {@inheritDoc} */
-    public void walkInRowOrder(final RealMatrixPreservingVisitor visitor,
-                               final int startRow, final int endRow,
-                               final int startColumn, final int endColumn)
+    public double walkInRowOrder(final RealMatrixPreservingVisitor visitor,
+                                 final int startRow, final int endRow,
+                                 final int startColumn, final int endColumn)
         throws MatrixIndexException, MatrixVisitorException {
         checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
+        visitor.start(rows, columns, startRow, endRow, startColumn, endColumn);
         for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) {
             final int p0     = iBlock * BLOCK_SIZE;
             final int pStart = Math.max(startRow, p0);
@@ -1371,11 +1378,13 @@
                 }
              }
         }
+        return visitor.end();
     }
 
     /** {@inheritDoc} */
-    public void walkInInternalOrder(final RealMatrixChangingVisitor visitor)
+    public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor)
         throws MatrixVisitorException {
+        visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
         for (int iBlock = 0, blockIndex = 0; iBlock < blockRows; ++iBlock) {
             final int pStart = iBlock * BLOCK_SIZE;
             final int pEnd   = Math.min(pStart + BLOCK_SIZE, rows);
@@ -1390,11 +1399,13 @@
                 }
             }
         }
+        return visitor.end();
     }
 
     /** {@inheritDoc} */
-    public void walkInInternalOrder(final RealMatrixPreservingVisitor visitor)
+    public double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor)
         throws MatrixVisitorException {
+        visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
         for (int iBlock = 0, blockIndex = 0; iBlock < blockRows; ++iBlock) {
             final int pStart = iBlock * BLOCK_SIZE;
             final int pEnd   = Math.min(pStart + BLOCK_SIZE, rows);
@@ -1409,14 +1420,16 @@
                 }
             }
         }
+        return visitor.end();
     }
 
     /** {@inheritDoc} */
-    public void walkInInternalOrder(final RealMatrixChangingVisitor visitor,
-                                    final int startRow, final int endRow,
-                                    final int startColumn, final int endColumn)
+    public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor,
+                                       final int startRow, final int endRow,
+                                       final int startColumn, final int endColumn)
         throws MatrixIndexException, MatrixVisitorException {
         checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
+        visitor.start(rows, columns, startRow, endRow, startColumn, endColumn);
         for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) {
             final int p0     = iBlock * BLOCK_SIZE;
             final int pStart = Math.max(startRow, p0);
@@ -1434,14 +1447,16 @@
                 }
             }
         }
+        return visitor.end();
     }
 
     /** {@inheritDoc} */
-    public void walkInInternalOrder(final RealMatrixPreservingVisitor visitor,
-                                    final int startRow, final int endRow,
-                                    final int startColumn, final int endColumn)
+    public double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor,
+                                       final int startRow, final int endRow,
+                                       final int startColumn, final int endColumn)
         throws MatrixIndexException, MatrixVisitorException {
         checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
+        visitor.start(rows, columns, startRow, endRow, startColumn, endColumn);
         for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) {
             final int p0     = iBlock * BLOCK_SIZE;
             final int pStart = Math.max(startRow, p0);
@@ -1459,6 +1474,7 @@
                 }
             }
         }
+        return visitor.end();
     }
 
     /**

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=730787&r1=730786&r2=730787&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 Fri Jan  2 09:07:42 2009
@@ -500,34 +500,57 @@
 
     /**
      * Visit (and possibly change) all matrix entries in row order.
+     * <p>Row order starts at upper left and iterating through all elements
+     * of a row from left to right before going to the leftmost element
+     * of the next row.</p>
      * @param visitor visitor used to process all matrix entries
      * @exception  MatrixVisitorException if the visitor cannot process an entry
      * @see #walkInRowOrder(RealMatrixPreservingVisitor)
      * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int)
      * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int)
-     * @see #walkInInternalOrder(RealMatrixChangingVisitor)
-     * @see #walkInInternalOrder(RealMatrixPreservingVisitor)
-     * @see #walkInInternalOrder(RealMatrixChangingVisitor, int, int, int, int)
-     * @see #walkInInternalOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @return the value returned by {@link RealMatrixChangingVisitor#end()} at the end
+     * of the walk
      */
-    void walkInRowOrder(RealMatrixChangingVisitor visitor) throws MatrixVisitorException;
+    double walkInRowOrder(RealMatrixChangingVisitor visitor)
+        throws MatrixVisitorException;
 
     /**
      * Visit (but don't change) all matrix entries in row order.
+     * <p>Row order starts at upper left and iterating through all elements
+     * of a row from left to right before going to the leftmost element
+     * of the next row.</p>
      * @param visitor visitor used to process all matrix entries
      * @exception  MatrixVisitorException if the visitor cannot process an entry
      * @see #walkInRowOrder(RealMatrixChangingVisitor)
      * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int)
      * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int)
-     * @see #walkInInternalOrder(RealMatrixChangingVisitor)
-     * @see #walkInInternalOrder(RealMatrixPreservingVisitor)
-     * @see #walkInInternalOrder(RealMatrixChangingVisitor, int, int, int, int)
-     * @see #walkInInternalOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @return the value returned by {@link RealMatrixPreservingVisitor#end()} at the end
+     * of the walk
      */
-    void walkInRowOrder(RealMatrixPreservingVisitor visitor) throws MatrixVisitorException;
+    double walkInRowOrder(RealMatrixPreservingVisitor visitor)
+        throws MatrixVisitorException;
 
     /**
-     * Visit (and possibly change) all matrix entries in row order.
+     * Visit (and possibly change) some matrix entries in row order.
+     * <p>Row order starts at upper left and iterating through all elements
+     * of a row from left to right before going to the leftmost element
+     * of the next row.</p>
      * @param visitor visitor used to process all matrix entries
      * @param startRow Initial row index
      * @param endRow Final row index (inclusive)
@@ -538,17 +561,26 @@
      * @see #walkInRowOrder(RealMatrixChangingVisitor)
      * @see #walkInRowOrder(RealMatrixPreservingVisitor)
      * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int)
-     * @see #walkInInternalOrder(RealMatrixChangingVisitor)
-     * @see #walkInInternalOrder(RealMatrixPreservingVisitor)
-     * @see #walkInInternalOrder(RealMatrixChangingVisitor, int, int, int, int)
-     * @see #walkInInternalOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @return the value returned by {@link RealMatrixChangingVisitor#end()} at the end
+     * of the walk
      */
-    void walkInRowOrder(RealMatrixChangingVisitor visitor,
-                        int startRow, int endRow, int startColumn, int endColumn)
+    double walkInRowOrder(RealMatrixChangingVisitor visitor,
+                          int startRow, int endRow, int startColumn, int endColumn)
         throws MatrixIndexException, MatrixVisitorException;
 
     /**
-     * Visit (but don't change) all matrix entries in row order.
+     * Visit (but don't change) some matrix entries in row order.
+     * <p>Row order starts at upper left and iterating through all elements
+     * of a row from left to right before going to the leftmost element
+     * of the next row.</p>
      * @param visitor visitor used to process all matrix entries
      * @param startRow Initial row index
      * @param endRow Final row index (inclusive)
@@ -559,103 +591,234 @@
      * @see #walkInRowOrder(RealMatrixChangingVisitor)
      * @see #walkInRowOrder(RealMatrixPreservingVisitor)
      * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int)
-     * @see #walkInInternalOrder(RealMatrixChangingVisitor)
-     * @see #walkInInternalOrder(RealMatrixPreservingVisitor)
-     * @see #walkInInternalOrder(RealMatrixChangingVisitor, int, int, int, int)
-     * @see #walkInInternalOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @return the value returned by {@link RealMatrixPreservingVisitor#end()} at the end
+     * of the walk
      */
-    void walkInRowOrder(RealMatrixPreservingVisitor visitor,
-                        int startRow, int endRow, int startColumn, int endColumn)
+    double walkInRowOrder(RealMatrixPreservingVisitor visitor,
+                          int startRow, int endRow, int startColumn, int endColumn)
         throws MatrixIndexException, MatrixVisitorException;
 
     /**
-     * Visit (and possibly change) all matrix entries in row order.
-     * <p>The matrix internal order depends on the exact matrix class. It may be
-     * different from traditional row order, but is generally faster. If there is no need
-     * for an explicit walk order, this method should be preferred to the {@link
-     * #walkInRowOrder(RealMatrixChangingVisitor)} one.</p>
+     * Visit (and possibly change) all matrix entries in column order.
+     * <p>Column order starts at upper left and iterating through all elements
+     * of a column from top to bottom before going to the topmost element
+     * of the next column.</p>
      * @param visitor visitor used to process all matrix entries
      * @exception  MatrixVisitorException if the visitor cannot process an entry
      * @see #walkInRowOrder(RealMatrixChangingVisitor)
      * @see #walkInRowOrder(RealMatrixPreservingVisitor)
      * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int)
      * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int)
-     * @see #walkInInternalOrder(RealMatrixPreservingVisitor)
-     * @see #walkInInternalOrder(RealMatrixChangingVisitor, int, int, int, int)
-     * @see #walkInInternalOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @return the value returned by {@link RealMatrixChangingVisitor#end()} at the end
+     * of the walk
      */
-    void walkInInternalOrder(RealMatrixChangingVisitor visitor) throws MatrixVisitorException;
+    double walkInColumnOrder(RealMatrixChangingVisitor visitor)
+        throws MatrixVisitorException;
 
     /**
-     * Visit (but don't change) all matrix entries in row order.
-     * <p>The matrix internal order depends on the exact matrix class. It may be
-     * different from traditional row order, but is generally faster. If there is no need
-     * for an explicit walk order, this method should be preferred to the {@link
-     * #walkInRowOrder(RealMatrixPreservingVisitor)} one.</p>
+     * Visit (but don't change) all matrix entries in column order.
+     * <p>Column order starts at upper left and iterating through all elements
+     * of a column from top to bottom before going to the topmost element
+     * of the next column.</p>
      * @param visitor visitor used to process all matrix entries
      * @exception  MatrixVisitorException if the visitor cannot process an entry
      * @see #walkInRowOrder(RealMatrixChangingVisitor)
      * @see #walkInRowOrder(RealMatrixPreservingVisitor)
      * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int)
      * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int)
-     * @see #walkInInternalOrder(RealMatrixChangingVisitor)
-     * @see #walkInInternalOrder(RealMatrixChangingVisitor, int, int, int, int)
-     * @see #walkInInternalOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @return the value returned by {@link RealMatrixPreservingVisitor#end()} at the end
+     * of the walk
      */
-    void walkInInternalOrder(RealMatrixPreservingVisitor visitor) throws MatrixVisitorException;
+    double walkInColumnOrder(RealMatrixPreservingVisitor visitor)
+        throws MatrixVisitorException;
 
     /**
-     * Visit (and possibly change) all matrix entries in row order.
-     * <p>The matrix internal order depends on the exact matrix class. It may be
-     * different from traditional row order, but is generally faster. If there is no need
-     * for an explicit walk order, this method should be preferred to the {@link
-     * #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int)} one.</p>
+     * Visit (and possibly change) some matrix entries in column order.
+     * <p>Column order starts at upper left and iterating through all elements
+     * of a column from top to bottom before going to the topmost element
+     * of the next column.</p>
      * @param visitor visitor used to process all matrix entries
      * @param startRow Initial row index
      * @param endRow Final row index (inclusive)
      * @param startColumn Initial column index
-     * @param endColumn Final column index (inclusive)
+     * @param endColumn Final column index
      * @exception  MatrixVisitorException if the visitor cannot process an entry
      * @exception MatrixIndexException  if the indices are not valid
      * @see #walkInRowOrder(RealMatrixChangingVisitor)
      * @see #walkInRowOrder(RealMatrixPreservingVisitor)
      * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int)
      * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int)
-     * @see #walkInInternalOrder(RealMatrixChangingVisitor)
-     * @see #walkInInternalOrder(RealMatrixPreservingVisitor)
-     * @see #walkInInternalOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @return the value returned by {@link RealMatrixChangingVisitor#end()} at the end
+     * of the walk
      */
-    void walkInInternalOrder(RealMatrixChangingVisitor visitor,
+    double walkInColumnOrder(RealMatrixChangingVisitor visitor,
                              int startRow, int endRow, int startColumn, int endColumn)
         throws MatrixIndexException, MatrixVisitorException;
 
     /**
-     * Visit (but don't change) all matrix entries in row order.
-     * Visit (and possibly change) all matrix entries in row order.
-     * <p>The matrix internal order depends on the exact matrix class. It may be
-     * different from traditional row order, but is generally faster. If there is no need
-     * for an explicit walk order, this method should be preferred to the {@link
-     * #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int)} one.</p>
+     * Visit (but don't change) some matrix entries in column order.
+     * <p>Column order starts at upper left and iterating through all elements
+     * of a column from top to bottom before going to the topmost element
+     * of the next column.</p>
      * @param visitor visitor used to process all matrix entries
      * @param startRow Initial row index
      * @param endRow Final row index (inclusive)
      * @param startColumn Initial column index
-     * @param endColumn Final column index (inclusive)
+     * @param endColumn Final column index
      * @exception  MatrixVisitorException if the visitor cannot process an entry
      * @exception MatrixIndexException  if the indices are not valid
      * @see #walkInRowOrder(RealMatrixChangingVisitor)
      * @see #walkInRowOrder(RealMatrixPreservingVisitor)
      * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int)
      * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int)
-     * @see #walkInInternalOrder(RealMatrixChangingVisitor)
-     * @see #walkInInternalOrder(RealMatrixPreservingVisitor)
-     * @see #walkInInternalOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @return the value returned by {@link RealMatrixPreservingVisitor#end()} at the end
+     * of the walk
      */
-    void walkInInternalOrder(RealMatrixPreservingVisitor visitor,
+    double walkInColumnOrder(RealMatrixPreservingVisitor visitor,
                              int startRow, int endRow, int startColumn, int endColumn)
         throws MatrixIndexException, MatrixVisitorException;
 
     /**
+     * Visit (and possibly change) all matrix entries using the fastest possible order.
+     * <p>The fastest walking order depends on the exact matrix class. It may be
+     * different from traditional row or column orders.</p>
+     * @param visitor visitor used to process all matrix entries
+     * @exception  MatrixVisitorException if the visitor cannot process an entry
+     * @see #walkInRowOrder(RealMatrixChangingVisitor)
+     * @see #walkInRowOrder(RealMatrixPreservingVisitor)
+     * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @return the value returned by {@link RealMatrixChangingVisitor#end()} at the end
+     * of the walk
+     */
+    double walkInOptimizedOrder(RealMatrixChangingVisitor visitor)
+        throws MatrixVisitorException;
+
+    /**
+     * Visit (but don't change) all matrix entries using the fastest possible order.
+     * <p>The fastest walking order depends on the exact matrix class. It may be
+     * different from traditional row or column orders.</p>
+     * @param visitor visitor used to process all matrix entries
+     * @exception  MatrixVisitorException if the visitor cannot process an entry
+     * @see #walkInRowOrder(RealMatrixChangingVisitor)
+     * @see #walkInRowOrder(RealMatrixPreservingVisitor)
+     * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @return the value returned by {@link RealMatrixPreservingVisitor#end()} at the end
+     * of the walk
+     */
+    double walkInOptimizedOrder(RealMatrixPreservingVisitor visitor)
+        throws MatrixVisitorException;
+
+    /**
+     * Visit (and possibly change) some matrix entries using the fastest possible order.
+     * <p>The fastest walking order depends on the exact matrix class. It may be
+     * different from traditional row or column orders.</p>
+     * @param visitor visitor used to process all matrix entries
+     * @param startRow Initial row index
+     * @param endRow Final row index (inclusive)
+     * @param startColumn Initial column index
+     * @param endColumn Final column index (inclusive)
+     * @exception  MatrixVisitorException if the visitor cannot process an entry
+     * @exception MatrixIndexException  if the indices are not valid
+     * @see #walkInRowOrder(RealMatrixChangingVisitor)
+     * @see #walkInRowOrder(RealMatrixPreservingVisitor)
+     * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @return the value returned by {@link RealMatrixChangingVisitor#end()} at the end
+     * of the walk
+     */
+    double walkInOptimizedOrder(RealMatrixChangingVisitor visitor,
+                                int startRow, int endRow, int startColumn, int endColumn)
+        throws MatrixIndexException, MatrixVisitorException;
+
+    /**
+     * Visit (but don't change) some matrix entries using the fastest possible order.
+     * <p>The fastest walking order depends on the exact matrix class. It may be
+     * different from traditional row or column orders.</p>
+     * @param visitor visitor used to process all matrix entries
+     * @param startRow Initial row index
+     * @param endRow Final row index (inclusive)
+     * @param startColumn Initial column index
+     * @param endColumn Final column index (inclusive)
+     * @exception  MatrixVisitorException if the visitor cannot process an entry
+     * @exception MatrixIndexException  if the indices are not valid
+     * @see #walkInRowOrder(RealMatrixChangingVisitor)
+     * @see #walkInRowOrder(RealMatrixPreservingVisitor)
+     * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor)
+     * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor)
+     * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int)
+     * @return the value returned by {@link RealMatrixPreservingVisitor#end()} at the end
+     * of the walk
+     */
+    double walkInOptimizedOrder(RealMatrixPreservingVisitor visitor,
+                                int startRow, int endRow, int startColumn, int endColumn)
+        throws MatrixIndexException, MatrixVisitorException;
+
+    /**
      * Returns the solution vector for a linear system with coefficient
      * matrix = this and constant vector = <code>b</code>.
      *

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrixChangingVisitor.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrixChangingVisitor.java?rev=730787&r1=730786&r2=730787&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrixChangingVisitor.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrixChangingVisitor.java Fri Jan  2 09:07:42 2009
@@ -28,6 +28,19 @@
 public interface RealMatrixChangingVisitor extends Serializable {
 
     /**
+     * Start visiting a matrix.
+     * <p>This method is called once before any entry of the matrix is visited.</p>
+     * @param rows number of rows of the matrix
+     * @param columns number of columns of the matrix
+     * @param startRow Initial row index
+     * @param endRow Final row index (inclusive)
+     * @param startColumn Initial column index
+     * @param endColumn Final column index (inclusive)
+     */
+    void start(int rows, int columns,
+               int startRow, int endRow, int startColumn, int endColumn);
+
+    /**
      * Visit one matrix entry.
      * @param row row index of the entry
      * @param column column index of the entry
@@ -38,4 +51,11 @@
     double visit(int row, int column, double value)
         throws MatrixVisitorException;
 
+    /**
+     * End visiting a matrix.
+     * <p>This method is called once after all entries of the matrix have been visited.</p>
+     * @return the value that the <code>walkInXxxOrder</code> must return
+     */
+    double end();
+
 }

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrixImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrixImpl.java?rev=730787&r1=730786&r2=730787&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrixImpl.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrixImpl.java Fri Jan  2 09:07:42 2009
@@ -460,57 +460,129 @@
     }
 
     /** {@inheritDoc} */
-    public void walkInRowOrder(final RealMatrixChangingVisitor visitor)
+    public double walkInRowOrder(final RealMatrixChangingVisitor visitor)
         throws MatrixVisitorException {
         final int rows    = getRowDimension();
         final int columns = getColumnDimension();
+        visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
         for (int i = 0; i < rows; ++i) {
             final double[] rowI = data[i];
             for (int j = 0; j < columns; ++j) {
                 rowI[j] = visitor.visit(i, j, rowI[j]);
             }
         }
+        return visitor.end();
     }
 
     /** {@inheritDoc} */
-    public void walkInRowOrder(final RealMatrixPreservingVisitor visitor)
+    public double walkInRowOrder(final RealMatrixPreservingVisitor visitor)
         throws MatrixVisitorException {
         final int rows    = getRowDimension();
         final int columns = getColumnDimension();
+        visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
         for (int i = 0; i < rows; ++i) {
             final double[] rowI = data[i];
             for (int j = 0; j < columns; ++j) {
                 visitor.visit(i, j, rowI[j]);
             }
         }
+        return visitor.end();
     }
 
     /** {@inheritDoc} */
-    public void walkInRowOrder(final RealMatrixChangingVisitor visitor,
-                               final int startRow, final int endRow,
-                               final int startColumn, final int endColumn)
+    public double walkInRowOrder(final RealMatrixChangingVisitor visitor,
+                                 final int startRow, final int endRow,
+                                 final int startColumn, final int endColumn)
         throws MatrixIndexException, MatrixVisitorException {
         checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
+        visitor.start(getRowDimension(), getColumnDimension(),
+                      startRow, endRow, startColumn, endColumn);
         for (int i = startRow; i <= endRow; ++i) {
             final double[] rowI = data[i];
             for (int j = startColumn; j <= endColumn; ++j) {
                 rowI[j] = visitor.visit(i, j, rowI[j]);
             }
         }
+        return visitor.end();
     }
 
     /** {@inheritDoc} */
-    public void walkInRowOrder(final RealMatrixPreservingVisitor visitor,
-                               final int startRow, final int endRow,
-                               final int startColumn, final int endColumn)
+    public double walkInRowOrder(final RealMatrixPreservingVisitor visitor,
+                                 final int startRow, final int endRow,
+                                 final int startColumn, final int endColumn)
         throws MatrixIndexException, MatrixVisitorException {
         checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
+        visitor.start(getRowDimension(), getColumnDimension(),
+                      startRow, endRow, startColumn, endColumn);
         for (int i = startRow; i <= endRow; ++i) {
             final double[] rowI = data[i];
             for (int j = startColumn; j <= endColumn; ++j) {
                 visitor.visit(i, j, rowI[j]);
             }
         }
+        return visitor.end();
+    }
+
+    /** {@inheritDoc} */
+    public double walkInColumnOrder(final RealMatrixChangingVisitor visitor)
+        throws MatrixVisitorException {
+        final int rows    = getRowDimension();
+        final int columns = getColumnDimension();
+        visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
+        for (int j = 0; j < columns; ++j) {
+            for (int i = 0; i < rows; ++i) {
+                final double[] rowI = data[i];
+                rowI[j] = visitor.visit(i, j, rowI[j]);
+            }
+        }
+        return visitor.end();
+    }
+
+    /** {@inheritDoc} */
+    public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor)
+        throws MatrixVisitorException {
+        final int rows    = getRowDimension();
+        final int columns = getColumnDimension();
+        visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
+        for (int j = 0; j < columns; ++j) {
+            for (int i = 0; i < rows; ++i) {
+                visitor.visit(i, j, data[i][j]);
+            }
+        }
+        return visitor.end();
+    }
+
+    /** {@inheritDoc} */
+    public double walkInColumnOrder(final RealMatrixChangingVisitor visitor,
+                                    final int startRow, final int endRow,
+                                    final int startColumn, final int endColumn)
+        throws MatrixIndexException, MatrixVisitorException {
+        checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
+        visitor.start(getRowDimension(), getColumnDimension(),
+                      startRow, endRow, startColumn, endColumn);
+        for (int j = startColumn; j <= endColumn; ++j) {
+            for (int i = startRow; i <= endRow; ++i) {
+                final double[] rowI = data[i];
+                rowI[j] = visitor.visit(i, j, rowI[j]);
+            }
+        }
+        return visitor.end();
+    }
+
+    /** {@inheritDoc} */
+    public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor,
+                                    final int startRow, final int endRow,
+                                    final int startColumn, final int endColumn)
+        throws MatrixIndexException, MatrixVisitorException {
+        checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
+        visitor.start(getRowDimension(), getColumnDimension(),
+                      startRow, endRow, startColumn, endColumn);
+        for (int j = startColumn; j <= endColumn; ++j) {
+            for (int i = startRow; i <= endRow; ++i) {
+                visitor.visit(i, j, data[i][j]);
+            }
+        }
+        return visitor.end();
     }
 
     /**

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrixPreservingVisitor.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrixPreservingVisitor.java?rev=730787&r1=730786&r2=730787&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrixPreservingVisitor.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrixPreservingVisitor.java Fri Jan  2 09:07:42 2009
@@ -28,6 +28,19 @@
 public interface RealMatrixPreservingVisitor extends Serializable {
 
     /**
+     * Start visiting a matrix.
+     * <p>This method is called once before any entry of the matrix is visited.</p>
+     * @param rows number of rows of the matrix
+     * @param columns number of columns of the matrix
+     * @param startRow Initial row index
+     * @param endRow Final row index (inclusive)
+     * @param startColumn Initial column index
+     * @param endColumn Final column index (inclusive)
+     */
+    void start(int rows, int columns,
+               int startRow, int endRow, int startColumn, int endColumn);
+
+    /**
      * Visit one matrix entry.
      * @param row row index of the entry
      * @param column column index of the entry
@@ -37,4 +50,11 @@
     void visit(int row, int column, double value)
         throws MatrixVisitorException;
 
+    /**
+     * End visiting a matrix.
+     * <p>This method is called once after all entries of the matrix have been visited.</p>
+     * @return the value that the <code>walkInXxxOrder</code> must return
+     */
+    double end();
+
 }

Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=730787&r1=730786&r2=730787&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Fri Jan  2 09:07:42 2009
@@ -41,9 +41,10 @@
     <release version="2.0" date="TBD" description="TBD">
       <action dev="luc" type="add" >
         Added method to walk matrix entries with or without changing them in the
-        visitor design pattern sense. Two different orders can be used, row by row
-        of following internal storage. Internal order should be preferred when no
-        specific order is needed, because it will be more cache efficient.
+        visitor design pattern sense. Three different orders can be used, row by row,
+        column by column or optimized order according to internal storage. Optimized
+        order should be preferred when no specific order is needed, because it will be
+        more cache efficient.
       </action>
       <action dev="luc" type="add" issue="MATH-215" due-to="Bernhard Grünewaldt">
         Added Fast Hadamard Transform.

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/DenseRealMatrixTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/linear/DenseRealMatrixTest.java?rev=730787&r1=730786&r2=730787&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/linear/DenseRealMatrixTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/linear/DenseRealMatrixTest.java Fri Jan  2 09:07:42 2009
@@ -374,8 +374,8 @@
     /** test transpose */
     public void testTranspose() {
         RealMatrix m = new DenseRealMatrix(testData); 
-        RealMatrix mIT = new LUSolver(new LUDecompositionImpl(m)).getInverse().transpose();
-        RealMatrix mTI = new LUSolver(new LUDecompositionImpl(m.transpose())).getInverse();
+        RealMatrix mIT = new LUDecompositionImpl(m).getSolver().getInverse().transpose();
+        RealMatrix mTI = new LUDecompositionImpl(m.transpose()).getSolver().getInverse();
         assertClose(mIT, mTI, normTolerance);
         m = new DenseRealMatrix(testData2);
         RealMatrix mt = new DenseRealMatrix(testData2T);
@@ -1027,13 +1027,13 @@
         RealMatrix m = new DenseRealMatrix(rows, columns);
         m.walkInRowOrder(new SetVisitor());
         GetVisitor getVisitor = new GetVisitor();
-        m.walkInInternalOrder(getVisitor);
+        m.walkInOptimizedOrder(getVisitor);
         assertEquals(rows * columns, getVisitor.getCount());
 
         m = new DenseRealMatrix(rows, columns);
         m.walkInRowOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
         getVisitor = new GetVisitor();
-        m.walkInInternalOrder(getVisitor, 1, rows - 2, 1, columns - 2);
+        m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
         assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
         for (int i = 0; i < rows; ++i) {
             assertEquals(0.0, m.getEntry(i, 0), 0);                    
@@ -1044,15 +1044,34 @@
             assertEquals(0.0, m.getEntry(rows - 1, j), 0);
         }
 
+        m = new DenseRealMatrix(rows, columns);
+        m.walkInColumnOrder(new SetVisitor());
+        getVisitor = new GetVisitor();
+        m.walkInOptimizedOrder(getVisitor);
+        assertEquals(rows * columns, getVisitor.getCount());
+
+        m = new DenseRealMatrix(rows, columns);
+        m.walkInColumnOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
+        getVisitor = new GetVisitor();
+        m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
+        assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
+        for (int i = 0; i < rows; ++i) {
+            assertEquals(0.0, m.getEntry(i, 0), 0);                    
+            assertEquals(0.0, m.getEntry(i, columns - 1), 0);
+        }
+        for (int j = 0; j < columns; ++j) {
+            assertEquals(0.0, m.getEntry(0, j), 0);                    
+            assertEquals(0.0, m.getEntry(rows - 1, j), 0);
+        }
 
         m = new DenseRealMatrix(rows, columns);
-        m.walkInInternalOrder(new SetVisitor());
+        m.walkInOptimizedOrder(new SetVisitor());
         getVisitor = new GetVisitor();
         m.walkInRowOrder(getVisitor);
         assertEquals(rows * columns, getVisitor.getCount());
 
         m = new DenseRealMatrix(rows, columns);
-        m.walkInInternalOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
+        m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
         getVisitor = new GetVisitor();
         m.walkInRowOrder(getVisitor, 1, rows - 2, 1, columns - 2);
         assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
@@ -1065,18 +1084,47 @@
             assertEquals(0.0, m.getEntry(rows - 1, j), 0);
         }
 
+        m = new DenseRealMatrix(rows, columns);
+        m.walkInOptimizedOrder(new SetVisitor());
+        getVisitor = new GetVisitor();
+        m.walkInColumnOrder(getVisitor);
+        assertEquals(rows * columns, getVisitor.getCount());
+
+        m = new DenseRealMatrix(rows, columns);
+        m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
+        getVisitor = new GetVisitor();
+        m.walkInColumnOrder(getVisitor, 1, rows - 2, 1, columns - 2);
+        assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
+        for (int i = 0; i < rows; ++i) {
+            assertEquals(0.0, m.getEntry(i, 0), 0);                    
+            assertEquals(0.0, m.getEntry(i, columns - 1), 0);
+        }
+        for (int j = 0; j < columns; ++j) {
+            assertEquals(0.0, m.getEntry(0, j), 0);                    
+            assertEquals(0.0, m.getEntry(rows - 1, j), 0);
+        }
+
     }
     
     private static class SetVisitor implements RealMatrixChangingVisitor {
         private static final long serialVersionUID = -5724808764099124932L;
+        public void start(int rows, int columns, int startRow, int endRow,
+                int startColumn, int endColumn) {
+        }
         public double visit(int i, int j, double value) {
             return i + j / 1024.0;
         }
+        public double end() {
+            return 0;
+        }
     }
 
     private static class GetVisitor implements RealMatrixPreservingVisitor {
         private static final long serialVersionUID = 1299771253908695242L;
         int count = 0;
+        public void start(int rows, int columns, int startRow, int endRow,
+                int startColumn, int endColumn) {
+        }
         public void visit(int i, int j, double value) {
             ++count;
             assertEquals(i + j / 1024.0, value, 0.0);
@@ -1084,6 +1132,9 @@
         public int getCount() {
             return count;
         }
+        public double end() {
+            return 0;
+        }
     };
 
     //--------------- -----------------Protected methods

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java?rev=730787&r1=730786&r2=730787&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java Fri Jan  2 09:07:42 2009
@@ -813,13 +813,13 @@
         RealMatrix m = new RealMatrixImpl(rows, columns);
         m.walkInRowOrder(new SetVisitor());
         GetVisitor getVisitor = new GetVisitor();
-        m.walkInInternalOrder(getVisitor);
+        m.walkInOptimizedOrder(getVisitor);
         assertEquals(rows * columns, getVisitor.getCount());
 
         m = new RealMatrixImpl(rows, columns);
         m.walkInRowOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
         getVisitor = new GetVisitor();
-        m.walkInInternalOrder(getVisitor, 1, rows - 2, 1, columns - 2);
+        m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
         assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
         for (int i = 0; i < rows; ++i) {
             assertEquals(0.0, m.getEntry(i, 0), 0);                    
@@ -830,15 +830,34 @@
             assertEquals(0.0, m.getEntry(rows - 1, j), 0);
         }
 
+        m = new RealMatrixImpl(rows, columns);
+        m.walkInColumnOrder(new SetVisitor());
+        getVisitor = new GetVisitor();
+        m.walkInOptimizedOrder(getVisitor);
+        assertEquals(rows * columns, getVisitor.getCount());
+
+        m = new RealMatrixImpl(rows, columns);
+        m.walkInColumnOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
+        getVisitor = new GetVisitor();
+        m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
+        assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
+        for (int i = 0; i < rows; ++i) {
+            assertEquals(0.0, m.getEntry(i, 0), 0);                    
+            assertEquals(0.0, m.getEntry(i, columns - 1), 0);
+        }
+        for (int j = 0; j < columns; ++j) {
+            assertEquals(0.0, m.getEntry(0, j), 0);                    
+            assertEquals(0.0, m.getEntry(rows - 1, j), 0);
+        }
 
         m = new RealMatrixImpl(rows, columns);
-        m.walkInInternalOrder(new SetVisitor());
+        m.walkInOptimizedOrder(new SetVisitor());
         getVisitor = new GetVisitor();
         m.walkInRowOrder(getVisitor);
         assertEquals(rows * columns, getVisitor.getCount());
 
         m = new RealMatrixImpl(rows, columns);
-        m.walkInInternalOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
+        m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
         getVisitor = new GetVisitor();
         m.walkInRowOrder(getVisitor, 1, rows - 2, 1, columns - 2);
         assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
@@ -851,18 +870,47 @@
             assertEquals(0.0, m.getEntry(rows - 1, j), 0);
         }
 
+        m = new RealMatrixImpl(rows, columns);
+        m.walkInOptimizedOrder(new SetVisitor());
+        getVisitor = new GetVisitor();
+        m.walkInColumnOrder(getVisitor);
+        assertEquals(rows * columns, getVisitor.getCount());
+
+        m = new RealMatrixImpl(rows, columns);
+        m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
+        getVisitor = new GetVisitor();
+        m.walkInColumnOrder(getVisitor, 1, rows - 2, 1, columns - 2);
+        assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
+        for (int i = 0; i < rows; ++i) {
+            assertEquals(0.0, m.getEntry(i, 0), 0);                    
+            assertEquals(0.0, m.getEntry(i, columns - 1), 0);
+        }
+        for (int j = 0; j < columns; ++j) {
+            assertEquals(0.0, m.getEntry(0, j), 0);                    
+            assertEquals(0.0, m.getEntry(rows - 1, j), 0);
+        }
+
     }
     
     private static class SetVisitor implements RealMatrixChangingVisitor {
         private static final long serialVersionUID = -5724808764099124932L;
+        public void start(int rows, int columns, int startRow, int endRow,
+                int startColumn, int endColumn) {
+        }
         public double visit(int i, int j, double value) {
             return i + j / 1024.0;
         }
+        public double end() {
+            return 0;
+        }
     }
 
     private static class GetVisitor implements RealMatrixPreservingVisitor {
         private static final long serialVersionUID = 1299771253908695242L;
         int count = 0;
+        public void start(int rows, int columns, int startRow, int endRow,
+                int startColumn, int endColumn) {
+        }
         public void visit(int i, int j, double value) {
             ++count;
             assertEquals(i + j / 1024.0, value, 0.0);
@@ -870,6 +918,9 @@
         public int getCount() {
             return count;
         }
+        public double end() {
+            return 0;
+        }
     };
 
     //--------------- -----------------Protected methods



Mime
View raw message