public static decimal[,] Divide(this decimal[,] a, decimal[,] b, bool leastSquares = false)
{
int rows = b.Rows();
int cols = b.Columns();
if (leastSquares)
{
return new SingularValueDecompositionD(b.Transpose(),
computeLeftSingularVectors: true,
computeRightSingularVectors: true,
autoTranspose: true).Solve(a.Transpose()).Transpose();
}
if (rows == cols && cols == a.Rows())
{
// Solve by LU Decomposition if matrix is square.
return new LuDecompositionD(b, true).SolveTranspose(a);
}
else
{
if (rows <= cols)
{
// Solve by QR Decomposition if not.
return new QrDecompositionD(b, true).SolveTranspose(a);
}
else
{
return new SingularValueDecompositionD(b.Transpose(),
computeLeftSingularVectors: true,
computeRightSingularVectors: true,
autoTranspose: true).Solve(a.Transpose()).Transpose();
}
}
}