public static ISolverArrayDecomposition<decimal> Decompose(this decimal[][] matrix, bool leastSquares = false)
{
int rows = matrix.Rows();
int cols = matrix.Columns();
if (leastSquares)
{
return new JaggedSingularValueDecompositionD(matrix,
computeLeftSingularVectors: true,
computeRightSingularVectors: true,
autoTranspose: true);
}
if (rows == cols)
{
// Solve by LU Decomposition if matrix is square.
return new JaggedLuDecompositionD(matrix);
}
else
{
if (cols < rows)
{
// Solve by QR Decomposition if not.
return new JaggedQrDecompositionD(matrix);
}
else
{
return new JaggedSingularValueDecompositionD(matrix,
computeLeftSingularVectors: true,
computeRightSingularVectors: true,
autoTranspose: true);
}
}
}