mathjs
mathjs copied to clipboard
Better documentation of usolve limitations, or possibly error message when input is not upper triangular?
Minimal code:
window.onload = function() {
const U = math.matrix([[0.10452846326765346,-120],[-0.9945218953682733,-0]])
const b = math.matrix([-22.163070207475485, -59.419761706007094])
result = math.usolve(U,b)
console.log(result)
}
The error I get: Uncaught Error: Linear system cannot be solved since matrix is singular
.
I entered the very same equation into GNU Octave which was able to solve it: it is no unsolvable system. When switching the lines, it even becomes an upper triangular matrix with nonzero values on the diagonal line. So its determinant is not zero, which implicates that the matrix is not even singular!
Ok, the problem became clear: I should have used "lsolve" instead of "usolve". I think it would be helpfull if the error message represented the problem in a way of "U is not a lower triangular matrix"
Thanks for reporting. I'll change the title to reflect improving the error message. Anyone able to help here?
My two cents: usolve currently does not check its argument is upper triangular. It simply assumes that its input is upper triangular, and produces nonsense results if that assumption is not true. So the only way to issue an error message if the input is not upper triangular is to add code to explicitly check. There are two issues (a) For large matrices, that check is potentially as expensive as the solution itself, so the clients that are using the function as intended might not appreciate it being slowed down; and (b) there is a precision issue inherent in checking whether below-diagonal entries are 0, so it might be best to just take the caller's word for it that all of those entries should be considered 0. So perhaps it would be best to simply treat this as a documentation issue, emphasizing that usolve only works with upper triangular matrices and that its output is meaningless if the input does not satisfy that condition?
It makes sense at least as a first step to document this assumption, that's little work.
It may be worth doing a small experiment to see how big of an performance impact there is on validating whether the matrix is upper triangular. Maybe it is not that bad, in that case if would be worth adding such a check.