1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-27 07:37:33 +00:00

Calc: fix LU decomposition for non-numeric matrices (bug#41223)

Computing determinant and inverse for on some matrices containing
non-numeric elements failed or gave the wrong result.
Reported by Mauro Aranda.

* lisp/calc/calc-mtx.el (math-do-matrix-lud): Don't use zero as pivot.
* test/lisp/calc/calc-tests.el (calc-matrix-determinant): New test.
This commit is contained in:
Mattias Engdegård 2020-05-13 15:17:10 +02:00
parent a4671733b7
commit 1d559581b3
2 changed files with 25 additions and 1 deletions

View File

@ -275,7 +275,7 @@ in LUD decomposition."
k (1+ k)))
(setcar (nthcdr j (nth i lu)) sum)
(let ((dum (math-lud-pivot-check sum)))
(if (Math-lessp big dum)
(if (or (math-zerop big) (Math-lessp big dum))
(setq big dum
imax i)))
(setq i (1+ i)))

View File

@ -345,6 +345,30 @@ An existing calc stack is reused, otherwise a new one is created."
(should (Math-num-integerp '(float 1 0)))
(should-not (Math-num-integerp nil)))
(ert-deftest calc-matrix-determinant ()
(should (equal (calcFunc-det '(vec (vec 3)))
3))
(should (equal (calcFunc-det '(vec (vec 2 3) (vec 6 7)))
-4))
(should (equal (calcFunc-det '(vec (vec 1 2 3) (vec 4 5 7) (vec 9 6 2)))
15))
(should (equal (calcFunc-det '(vec (vec 0 5 7 3)
(vec 0 0 2 0)
(vec 1 2 3 4)
(vec 0 0 0 3)))
30))
(should (equal (calcFunc-det '(vec (vec (var a var-a))))
'(var a var-a)))
(should (equal (calcFunc-det '(vec (vec 2 (var a var-a))
(vec 7 (var a var-a))))
'(* -5 (var a var-a))))
(should (equal (calcFunc-det '(vec (vec 1 0 0 0)
(vec 0 1 0 0)
(vec 0 0 0 1)
(vec 0 0 (var a var-a) 0)))
'(neg (var a var-a)))))
(provide 'calc-tests)
;;; calc-tests.el ends here