From a08f9179d33966cc61cc42a91538bc04499ce265 Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Sun, 2 Oct 2011 13:00:41 +0000 Subject: [PATCH] MATH-676 Faster "multiply" method. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1178186 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/linear/Array2DRowRealMatrix.java | 24 +++++++++++++------ src/site/xdoc/changes.xml | 4 ++++ 2 files changed, 21 insertions(+), 7 deletions(-) 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.