diff --git a/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java b/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java index b14bb0257..c0c3ba310 100644 --- a/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java +++ b/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java @@ -32,7 +32,7 @@ import org.apache.commons.math.util.MathUtils; */ 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 @@ public abstract class AbstractRealMatrix implements RealMatrix, Serializable { /** {@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; } - maxColSum = Math.max(maxColSum, sum); - } - return maxColSum; + + /** {@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; + } + } + + /** {@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 @@ public abstract class AbstractRealMatrix implements RealMatrix, Serializable { 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 @@ public abstract class AbstractRealMatrix implements RealMatrix, Serializable { } /** {@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 @@ public abstract class AbstractRealMatrix implements RealMatrix, Serializable { } } 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 @@ public abstract class AbstractRealMatrix implements RealMatrix, Serializable { } } 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 { - walkInRowOrder(visitor, startRow, endRow, startColumn, endColumn); + 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 { + return walkInRowOrder(visitor, startRow, endRow, startColumn, endColumn); } /** {@inheritDoc} */ diff --git a/src/java/org/apache/commons/math/linear/DenseRealMatrix.java b/src/java/org/apache/commons/math/linear/DenseRealMatrix.java index 2aa51768e..9ef60f653 100644 --- a/src/java/org/apache/commons/math/linear/DenseRealMatrix.java +++ b/src/java/org/apache/commons/math/linear/DenseRealMatrix.java @@ -1284,8 +1284,9 @@ public class DenseRealMatrix extends AbstractRealMatrix implements Serializable } /** {@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 @@ public class DenseRealMatrix extends AbstractRealMatrix implements Serializable } } } + 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 @@ public class DenseRealMatrix extends AbstractRealMatrix implements Serializable } } } + 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 @@ public class DenseRealMatrix extends AbstractRealMatrix implements Serializable } } } + 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 @@ public class DenseRealMatrix extends AbstractRealMatrix implements Serializable } } } + 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 @@ public class DenseRealMatrix extends AbstractRealMatrix implements Serializable } } } + 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 @@ public class DenseRealMatrix extends AbstractRealMatrix implements Serializable } } } + 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 @@ public class DenseRealMatrix extends AbstractRealMatrix implements Serializable } } } + 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 @@ public class DenseRealMatrix extends AbstractRealMatrix implements Serializable } } } + return visitor.end(); } /** diff --git a/src/java/org/apache/commons/math/linear/RealMatrix.java b/src/java/org/apache/commons/math/linear/RealMatrix.java index c0c44acd6..31e916396 100644 --- a/src/java/org/apache/commons/math/linear/RealMatrix.java +++ b/src/java/org/apache/commons/math/linear/RealMatrix.java @@ -500,34 +500,57 @@ public interface RealMatrix extends Serializable { /** * Visit (and possibly change) all matrix entries in row order. + *
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.
* @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. + *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.
* @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. + *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.
* @param visitor visitor used to process all matrix entries * @param startRow Initial row index * @param endRow Final row index (inclusive) @@ -538,17 +561,26 @@ public interface RealMatrix extends Serializable { * @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. + *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.
* @param visitor visitor used to process all matrix entries * @param startRow Initial row index * @param endRow Final row index (inclusive) @@ -559,57 +591,179 @@ public interface RealMatrix extends Serializable { * @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. - *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.
+ * Visit (and possibly change) all matrix entries in column order. + *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.
* @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. - *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.
+ * Visit (but don't change) all matrix entries in column order. + *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.
* @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. - *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.
+ * Visit (and possibly change) some matrix entries in column order. + *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.
+ * @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 + * @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(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 + */ + double walkInColumnOrder(RealMatrixChangingVisitor visitor, + int startRow, int endRow, int startColumn, int endColumn) + throws MatrixIndexException, MatrixVisitorException; + + /** + * Visit (but don't change) some matrix entries in column order. + *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.
+ * @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 + * @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 #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 + */ + 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. + *The fastest walking order depends on the exact matrix class. It may be + * different from traditional row or column orders.
+ * @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. + *The fastest walking order depends on the exact matrix class. It may be + * different from traditional row or column orders.
+ * @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. + *The fastest walking order depends on the exact matrix class. It may be + * different from traditional row or column orders.
* @param visitor visitor used to process all matrix entries * @param startRow Initial row index * @param endRow Final row index (inclusive) @@ -621,21 +775,24 @@ public interface RealMatrix extends Serializable { * @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(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 */ - void walkInInternalOrder(RealMatrixChangingVisitor visitor, - int startRow, int endRow, int startColumn, int endColumn) + double walkInOptimizedOrder(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. - *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.
+ * Visit (but don't change) some matrix entries using the fastest possible order. + *The fastest walking order depends on the exact matrix class. It may be + * different from traditional row or column orders.
* @param visitor visitor used to process all matrix entries * @param startRow Initial row index * @param endRow Final row index (inclusive) @@ -647,12 +804,18 @@ public interface RealMatrix extends Serializable { * @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 #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 */ - void walkInInternalOrder(RealMatrixPreservingVisitor visitor, - int startRow, int endRow, int startColumn, int endColumn) + double walkInOptimizedOrder(RealMatrixPreservingVisitor visitor, + int startRow, int endRow, int startColumn, int endColumn) throws MatrixIndexException, MatrixVisitorException; /** diff --git a/src/java/org/apache/commons/math/linear/RealMatrixChangingVisitor.java b/src/java/org/apache/commons/math/linear/RealMatrixChangingVisitor.java index ea9eb7a18..6ec1dd5f8 100644 --- a/src/java/org/apache/commons/math/linear/RealMatrixChangingVisitor.java +++ b/src/java/org/apache/commons/math/linear/RealMatrixChangingVisitor.java @@ -27,6 +27,19 @@ import java.io.Serializable; */ public interface RealMatrixChangingVisitor extends Serializable { + /** + * Start visiting a matrix. + *This method is called once before any entry of the matrix is visited.
+ * @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 @@ -38,4 +51,11 @@ public interface RealMatrixChangingVisitor extends Serializable { double visit(int row, int column, double value) throws MatrixVisitorException; + /** + * End visiting a matrix. + *This method is called once after all entries of the matrix have been visited.
+ * @return the value that thewalkInXxxOrder
must return
+ */
+ double end();
+
}
diff --git a/src/java/org/apache/commons/math/linear/RealMatrixImpl.java b/src/java/org/apache/commons/math/linear/RealMatrixImpl.java
index 55b53f5c4..5c4a94112 100644
--- a/src/java/org/apache/commons/math/linear/RealMatrixImpl.java
+++ b/src/java/org/apache/commons/math/linear/RealMatrixImpl.java
@@ -460,57 +460,129 @@ public class RealMatrixImpl extends AbstractRealMatrix implements Serializable {
}
/** {@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();
}
/**
diff --git a/src/java/org/apache/commons/math/linear/RealMatrixPreservingVisitor.java b/src/java/org/apache/commons/math/linear/RealMatrixPreservingVisitor.java
index 93db993b5..8bbc2714f 100644
--- a/src/java/org/apache/commons/math/linear/RealMatrixPreservingVisitor.java
+++ b/src/java/org/apache/commons/math/linear/RealMatrixPreservingVisitor.java
@@ -27,6 +27,19 @@ import java.io.Serializable;
*/
public interface RealMatrixPreservingVisitor extends Serializable {
+ /**
+ * Start visiting a matrix.
+ * This method is called once before any entry of the matrix is visited.
+ * @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 @@ -37,4 +50,11 @@ public interface RealMatrixPreservingVisitor extends Serializable { void visit(int row, int column, double value) throws MatrixVisitorException; + /** + * End visiting a matrix. + *This method is called once after all entries of the matrix have been visited.
+ * @return the value that thewalkInXxxOrder
must return
+ */
+ double end();
+
}
diff --git a/src/site/xdoc/changes.xml b/src/site/xdoc/changes.xml
index e493e7373..8e0dc75e2 100644
--- a/src/site/xdoc/changes.xml
+++ b/src/site/xdoc/changes.xml
@@ -41,9 +41,10 @@ The