mathnet-numerics
mathnet-numerics copied to clipboard
Performance of OuterProduct for SparseVectors
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?
Yes, OuterProduct needs a special implementation for sparse vectors but does not have one yet.
Ok, thanks for the clarification. Is there an easy way to find out which functions have a native implementation for sparse matrices?
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...