mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

Better documentation of usolve limitations, or possibly error message when input is not upper triangular?

Open Asqiir opened this issue 3 years ago • 4 comments

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!

Asqiir avatar Oct 06 '21 09:10 Asqiir

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"

Asqiir avatar Oct 06 '21 19:10 Asqiir

Thanks for reporting. I'll change the title to reflect improving the error message. Anyone able to help here?

josdejong avatar Oct 23 '21 09:10 josdejong

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?

gwhitney avatar Oct 03 '23 02:10 gwhitney

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.

josdejong avatar Oct 04 '23 09:10 josdejong