mathnet / mathnet-numerics

Math.NET Numerics
http://numerics.mathdotnet.com
MIT License
3.47k stars 893 forks source link

Performance problem of Solve or Inverse #227

Open heavenwing opened 10 years ago

heavenwing commented 10 years ago

I create a sparse matrix with 5000 size, when I solve or inverse, after I wait for a long time, it still running. code is var sw = Stopwatch.StartNew(); var a = Matrix.Build.SparseDiagonal(5000, 5000, 1); var f = Vector.Build.Sparse(5000); f[4999] = 1; var s = a.Solve(f); sw.Stop(); Debug.WriteLine(sw.ElapsedMilliseconds / 1000);

cdrnet commented 10 years ago

The matrix decompositions are currently only implemented for dense matrices. For other structures they fall back to a generic implementation which is particularly inefficient for sparse matrices.

The long term fix is obviously to provide optimized sparse implementations for all the matrix decompositions (and also native bindings). Now that v3 has been released we can finally move forward in this direction. A partial sparse implementation of the LU decomposition (that would be used in this case) is already implemented in the MILU(0) preconditioner (takes ~10ms on this example in a quick test).

A system of this size may still be solvable as dense when using the MKL native provider (~1s in a quick test). Usually you'd use the iterative solvers for sparse matrices (SolveIterative, e.g. using the MILU(0) preconditioner), but they don't seem to work well on this synthetic example.

Until the sparse direct methods are ready we may want consider throwing on sparse matrices, or automatically try use iterative methods.

adriaanpfeiffer commented 9 years ago

Hi, Are you aware of this project https://csparse.codeplex.com which has implementations for solving sparse systems with direct methods ?

wo80 commented 8 years ago

@a5rGithub: the code is now hosted here at GitHub (https://github.com/wo80/CSparse.NET). Also be aware that it is released under LGPL, so it won't be part of Math.NET Numerics.

I've written an example showing how both libraries can be used together: https://github.com/wo80/CSparse.NET/wiki/Math.NET-Numerics-and-CSparse

dgrimmin commented 5 months ago

https://github.com/wo80/CSparse.NET/wiki/Math.NET-Numerics-and-CSparse