mathnet-numerics icon indicating copy to clipboard operation
mathnet-numerics copied to clipboard

Performance of OuterProduct for SparseVectors

Open sebhofer opened this issue 6 years ago • 3 comments

OuterProduct seems to be rather slow for large SparseVectors. Consider, for example,

let bar = SparseVector.init 5000 (fun i -> if i%100=0 then float i else 0.)
let mat1 = bar.OuterProduct(bar)
// Real: 00:00:00.809, CPU: 00:00:00.812, GC gen0: 0, gen1: 0, gen2: 0
let mat2 = (bar.ToColumnMatrix())*(bar.ToRowMatrix())
// Real: 00:00:00.000, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
mat1 = mat2

Things get worse when the vector gets denser:

let foo = SparseVector.init 200 (fun i -> float i)
let mat1 = foo.OuterProduct(foo)
//  Real: 00:00:00.410, CPU: 00:00:00.390, GC gen0: 0, gen1: 0, gen2: 0
let mat2 = (foo.ToColumnMatrix())*(foo.ToRowMatrix())
//  Real: 00:00:00.001, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
mat1 = mat2

Using matrix multiplication seems like a straightforward workaround. Am I missing something?

sebhofer avatar May 11 '18 15:05 sebhofer

Yes, OuterProduct needs a special implementation for sparse vectors but does not have one yet.

cdrnet avatar May 13 '18 10:05 cdrnet

Ok, thanks for the clarification. Is there an easy way to find out which functions have a native implementation for sparse matrices?

sebhofer avatar May 14 '18 08:05 sebhofer

Also, would it make sense to try to implement the SparseVector implementation of OuterProduct myself? I don't really know C# but some of the other linalg routines I looked at look straightforward enough. So I could at least try to come up with something useful...

sebhofer avatar May 14 '18 12:05 sebhofer