Element-wise sparse-matrix multiplication using Colt

1.4k Views Asked by At

What am I doing wrong here? I want to element-wise multiply two sparse matrices using Colt. Here's an example of how I'm attempting to do this:

DoubleMatrix2D A = new SparseDoubleMatrix2D(2, 2);
A.set(0, 0, 2.0);

DoubleMatrix2D B = new SparseDoubleMatrix2D(2, 2);
B.set(0, 0, 3.0);

A.assign(B, Functions.mult);

Instead of the expected result of a matrix with 6 as the top left element, I'm getting this:

2 x 2 matrix
18 0
0 0

Changing A to DenseDoubleMatrix2D yields the correct result. Changing B to DenseDoubleMatrix2D does not change the result. Element-wise multiplying two vectors in this fashion always yielded the correct results, no matter whether I used SparseDoubleMatrix1D or DenseDoubleMatrix1D.

1

There are 1 best solutions below

0
On

"assign" mutates the object, so you may have used it twice.
E.g. See the following code using Parallel Colt 0.10.0 with the Scala REPL.

scala> import cern.colt.matrix.tdouble._
import cern.colt.matrix.tdouble._

scala> import cern.jet.math.tdouble.DoubleFunctions
import cern.jet.math.tdouble.DoubleFunctions

scala> val A = new SparseDoubleMatrix2D(2, 2);
A: cern.colt.matrix.tdouble.impl.SparseDoubleMatrix2D = 
2 x 2 sparse matrix, nnz = 0


scala> A.set(0, 0, 2.0)

scala> val B = new SparseDoubleMatrix2D(2, 2);
B: cern.colt.matrix.tdouble.impl.SparseDoubleMatrix2D = 
2 x 2 sparse matrix, nnz = 0


scala> B.set(0, 0, 3.0)

scala> A.assign(B, DoubleFunctions.mult)
res11: cern.colt.matrix.tdouble.DoubleMatrix2D = 
2 x 2 sparse matrix, nnz = 1
(0,0)   6.0


scala> A.assign(B, DoubleFunctions.mult)
res12: cern.colt.matrix.tdouble.DoubleMatrix2D = 
2 x 2 sparse matrix, nnz = 1
(0,0)   18.0


scala> A.assign(B, DoubleFunctions.mult)
res13: cern.colt.matrix.tdouble.DoubleMatrix2D = 
2 x 2 sparse matrix, nnz = 1
(0,0)   54.0

Or, it could be a bug in a different version you are using of Colt.