mathnet / mathnet-numerics

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

Cholesky does not throw error on asymmetric matrix #1029

Open antonott opened 10 months ago

antonott commented 10 months ago

The matrix { { 2, 0 }, { 1, 2 } } is not symmetric, hence not positive definite. Yet the code

using MathNet.Numerics.LinearAlgebra;
var m = Matrix<double>.Build.DenseOfArray(new double[,] { { 2, 0 }, { 1, 2 } });
m.Cholesky();

does not produce an error.

This seems like a bug to me, since the Cholesky decomposition is typically only defined for (symmetric) positive definite matrices, and especially since the documentation for the Cholesky<T> class says:

The computation of the Cholesky factorization is done at construction time. If the matrix is not symmetric or positive definite, the constructor will throw an exception.

I am not sure what precise algorithm is used for computing the factorization, but apparently it does not (always) catch non-symmetry. Maybe an explicit symmetry check is needed?