Open istathar opened 8 years ago
Thanks for the report.
Yes, I do think the inverse needs a review, or even a rewrite. It is not intentional that inverse
is partial. It should always return a result. So this is a bug. However, I am not sure that calculating the determinant before calculating the inverse is a good approach. While calculating the inverse, it should become apparent at some step that the inverse can't be calculated, and therefore the input matrix was not invertible. If this is the case, the computation should stop there and return a Left
value.
I added a test for this case that is currently failing.
Given that the type signature of
inverse
isI was a bit surprised that a matrix that isn't invertable didn't result in a
Left
:Boom.
Oh.
I realize that large matrices don't suffer from this problem statistically, but smaller ones can bork pretty easily. So can we check? Calculating the determinant isn't free, but in the way you have a few different cases for matrix multiplication, can we have a safer
inverse
function that checks? You've already got the type signature to support it.(I note in passing that there seem to be inverse methods that use the determinant in the calculation; Cayley–Hamilton and Cramer's Rule so if either of those were suitable for the implementation of
inverse
then the safety check calculating the determinant would contribute to the overall computation)Anyway, I'd like inverse to not blow up if handed a non-invertable matrix. The other approach is to
catch
errors being thrown, but that's a bit ugly.AfC