diff --git a/src/main/java/org/apache/commons/math/linear/Array2DRowRealMatrix.java b/src/main/java/org/apache/commons/math/linear/Array2DRowRealMatrix.java index 981d2c72b..8d448dc50 100644 --- a/src/main/java/org/apache/commons/math/linear/Array2DRowRealMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/Array2DRowRealMatrix.java @@ -212,21 +212,31 @@ public class Array2DRowRealMatrix extends AbstractRealMatrix implements Serializ final int nRows = this.getRowDimension(); final int nCols = m.getColumnDimension(); final int nSum = this.getColumnDimension(); + final double[][] outData = new double[nRows][nCols]; - for (int row = 0; row < nRows; row++) { - final double[] dataRow = data[row]; - final double[] outDataRow = outData[row]; - for (int col = 0; col < nCols; col++) { + // Will hold a column of "m". + final double[] mCol = new double[nSum]; + final double[][] mData = m.data; + + // Multiply. + for (int col = 0; col < nCols; col++) { + // Copy all elements of column "col" of "m" so that + // will be in contiguous memory. + for (int mRow = 0; mRow < nSum; mRow++) { + mCol[mRow] = mData[mRow][col]; + } + + for (int row = 0; row < nRows; row++) { + final double[] dataRow = data[row]; double sum = 0; for (int i = 0; i < nSum; i++) { - sum += dataRow[i] * m.data[i][col]; + sum += dataRow[i] * mCol[i]; } - outDataRow[col] = sum; + outData[row][col] = sum; } } return new Array2DRowRealMatrix(outData, false); - } /** {@inheritDoc} */ diff --git a/src/site/xdoc/changes.xml b/src/site/xdoc/changes.xml index 0ee562178..a1e8e04f6 100644 --- a/src/site/xdoc/changes.xml +++ b/src/site/xdoc/changes.xml @@ -52,6 +52,10 @@ The type attribute can be add,update,fix,remove. If the output is not quite correct, check for invisible trailing spaces! --> + + Faster "multiply" method in "Array2DRowRealMatrix". Code inspired + from the Jama project. + Replaced package.html with package-info.java for package documentation.