0

in my Java source i must execute following lines very often:

vecX = EigenMat.multiply(vecX);
vecY = EigenMat.multiply(vecY);

EigenMat is a N x N Matrix with N~40 vecX/vecY is a N x 1 vector (intern a RealMatrix to)

I used the "Sampler" from VisualFM to find some hotspots in my code and

org.apache.commons.math3.linear.Array2DRowRealMatrix.<init>()
org.apache.commons.math3.linear.Array2DRowRealMatrix.multiply()

have a very high runtime. I'm not a java professional but i think every multiplication a new vector is created. Can i reassign the old one?

Maybe i should switch to JBLAS to speed it up?

Matyro

Edit: Single core only

Hotspot2 Hotspot1

Matyro
  • 151
  • 1
  • 9
  • Is a GUI involved? [Profile](http://stackoverflow.com/q/2064427/230513) to guide your decision. – trashgod Oct 30 '14 at 21:00
  • No Gui involved and added hotspot/profile table from VisualVM – Matyro Oct 30 '14 at 21:24
  • How does [jblas](http://mikiobraun.github.io/jblas/) [compare](http://jccaicedo.blogspot.com/2012_05_01_archive.html)? Also consider [tag:jscience], which leverages [tag:javolution]. – trashgod Oct 30 '14 at 21:36

1 Answers1

0

i think every multiplication a new vector is created

Yes, it is. Source code of multiply():

public Array2DRowRealMatrix multiply(final Array2DRowRealMatrix m) {
        // Safety check.
        MatrixUtils.checkMultiplicationCompatible(this, m);

        final int nRows = this.getRowDimension();
        final int nCols = m.getColumnDimension();
        final int nSum = this.getColumnDimension();

        final double[][] outData = new double[nRows][nCols];
        // 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] * mCol[i];
                }
                outData[row][col] = sum;
            }
        }

        return new Array2DRowRealMatrix(outData, false);
    }

Input vector m is copied, as stated in a comment Copy all elements of column "col" of "m" so that will be in contiguous memory.

Can i reassign the old one?

Yes, you can perform the multiplication by yourself, writing two loops. Use getData() to get a reference to the underlying double[][] data.

Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110