1 ;;; calc-mtx.el --- matrix functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file is autoloaded from calc-ext.el.
32 (defun calc-mdet (arg)
35 (calc-unary-op "mdet" 'calcFunc-det arg)))
37 (defun calc-mtrace (arg)
40 (calc-unary-op "mtr" 'calcFunc-tr arg)))
42 (defun calc-mlud (arg)
45 (calc-unary-op "mlud" 'calcFunc-lud arg)))
48 ;;; Coerce row vector A to be a matrix. [V V]
49 (defun math-row-matrix (a)
50 (if (and (Math-vectorp a)
51 (not (math-matrixp a)))
55 ;;; Coerce column vector A to be a matrix. [V V]
56 (defun math-col-matrix (a)
57 (if (and (Math-vectorp a)
58 (not (math-matrixp a)))
59 (cons 'vec (mapcar (function (lambda (x) (list 'vec x))) (cdr a)))
64 ;;; Multiply matrices A and B. [V V V]
65 (defun math-mul-mats (a b)
67 (cols (length (nth 1 b)))
69 (while (setq a (cdr a))
72 (while (> (setq col (1- col)) 0)
73 (setq ap (cdr (car a))
75 accum (math-mul (car ap) (nth col (car bp))))
76 (while (setq ap (cdr ap) bp (cdr bp))
77 (setq accum (math-add accum (math-mul (car ap) (nth col (car bp))))))
78 (setq row (cons accum row)))
79 (setq mat (cons (cons 'vec row) mat)))
80 (cons 'vec (nreverse mat))))
82 (defun math-mul-mat-vec (a b)
83 (cons 'vec (mapcar (function (lambda (row)
84 (math-dot-product row b)))
89 (defun calcFunc-tr (mat) ; [Public]
90 (if (math-square-matrixp mat)
91 (math-matrix-trace-step 2 (1- (length mat)) mat (nth 1 (nth 1 mat)))
92 (math-reject-arg mat 'square-matrixp)))
94 (defun math-matrix-trace-step (n size mat sum)
96 (math-matrix-trace-step (1+ n) size mat
97 (math-add sum (nth n (nth n mat))))
101 ;;; Matrix inverse and determinant.
102 (defun math-matrix-inv-raw (m)
103 (let ((n (1- (length m))))
105 (let ((det (math-det-raw m)))
106 (and (not (math-zerop det))
113 (math-neg (nth 2 (nth 1 m))))
115 (math-neg (nth 1 (nth 2 m)))
120 (math-sub (math-mul (nth 3 (nth 3 m))
122 (math-mul (nth 3 (nth 2 m))
124 (math-sub (math-mul (nth 3 (nth 1 m))
126 (math-mul (nth 3 (nth 3 m))
128 (math-sub (math-mul (nth 3 (nth 2 m))
130 (math-mul (nth 3 (nth 1 m))
133 (math-sub (math-mul (nth 3 (nth 2 m))
135 (math-mul (nth 3 (nth 3 m))
137 (math-sub (math-mul (nth 3 (nth 3 m))
139 (math-mul (nth 3 (nth 1 m))
141 (math-sub (math-mul (nth 3 (nth 1 m))
143 (math-mul (nth 3 (nth 2 m))
146 (math-sub (math-mul (nth 2 (nth 3 m))
148 (math-mul (nth 2 (nth 2 m))
150 (math-sub (math-mul (nth 2 (nth 1 m))
152 (math-mul (nth 2 (nth 3 m))
154 (math-sub (math-mul (nth 2 (nth 2 m))
156 (math-mul (nth 2 (nth 1 m))
157 (nth 1 (nth 2 m))))))))
159 (let ((lud (math-matrix-lud m)))
161 (math-lud-solve lud (calcFunc-idn 1 n)))))))
163 (defun calcFunc-det (m)
164 (if (math-square-matrixp m)
165 (math-with-extra-prec 2 (math-det-raw m))
166 (if (and (eq (car-safe m) 'calcFunc-idn)
167 (or (math-zerop (nth 1 m))
168 (math-equal-int (nth 1 m) 1)))
170 (math-reject-arg m 'square-matrixp))))
172 ;; The variable math-det-lu is local to math-det-raw, but is
173 ;; used by math-det-step, which is called by math-det-raw.
176 (defun math-det-raw (m)
177 (let ((n (1- (length m))))
181 (math-sub (math-mul (nth 1 (nth 1 m))
183 (math-mul (nth 2 (nth 1 m))
191 (math-mul (nth 1 (nth 1 m))
192 (math-mul (nth 2 (nth 2 m))
194 (math-mul (nth 2 (nth 1 m))
195 (math-mul (nth 3 (nth 2 m))
197 (math-mul (nth 3 (nth 1 m))
198 (math-mul (nth 1 (nth 2 m))
200 (math-mul (nth 3 (nth 1 m))
201 (math-mul (nth 2 (nth 2 m))
203 (math-mul (nth 1 (nth 1 m))
204 (math-mul (nth 3 (nth 2 m))
206 (math-mul (nth 2 (nth 1 m))
207 (math-mul (nth 1 (nth 2 m))
208 (nth 3 (nth 3 m))))))
209 (t (let ((lud (math-matrix-lud m)))
211 (let ((math-det-lu (car lud)))
212 (math-det-step n (nth 2 lud)))
215 (defun math-det-step (n prod)
217 (math-det-step (1- n) (math-mul prod (nth n (nth n math-det-lu))))
220 ;;; This returns a list (LU index d), or nil if not possible.
221 ;;; Argument M must be a square matrix.
222 (defvar math-lud-cache nil)
223 (defun math-matrix-lud (m)
224 (let ((old (assoc m math-lud-cache))
225 (context (list calc-internal-prec calc-prefer-frac)))
226 (if (and old (equal (nth 1 old) context))
228 (let* ((lud (catch 'singular (math-do-matrix-lud m)))
229 (entry (cons context lud)))
232 (setq math-lud-cache (cons (cons m entry) math-lud-cache)))
235 ;;; Numerical Recipes section 2.3; implicit pivoting omitted.
236 (defun math-do-matrix-lud (m)
237 (let* ((lu (math-copy-matrix m))
239 i (j 1) k imax sum big
246 (math-working "LUD step" (format "%d/%d" j i))
247 (setq sum (nth j (nth i lu))
250 (setq sum (math-sub sum (math-mul (nth k (nth i lu))
253 (setcar (nthcdr j (nth i lu)) sum)
256 (math-working "LUD step" (format "%d/%d" j i))
257 (setq sum (nth j (nth i lu))
260 (setq sum (math-sub sum (math-mul (nth k (nth i lu))
263 (setcar (nthcdr j (nth i lu)) sum)
264 (let ((dum (math-abs-approx sum)))
265 (if (Math-lessp big dum)
270 (setq lu (math-swap-rows lu j imax)
272 (setq index (cons imax index))
273 (let ((pivot (nth j (nth j lu))))
274 (if (math-zerop pivot)
275 (throw 'singular nil)
277 (while (<= (setq i (1+ i)) n)
278 (setcar (nthcdr j (nth i lu))
279 (math-div (nth j (nth i lu)) pivot)))))
281 (list lu (nreverse index) d)))
283 (defun math-swap-rows (m r1 r2)
285 (let* ((r1prev (nthcdr (1- r1) m))
287 (r2prev (nthcdr (1- r2) m))
292 (setcdr row2 (cdr row1))
293 (setcdr row1 r2next)))
297 (defun math-lud-solve (lud b &optional need)
299 (let* ((x (math-copy-matrix b))
301 (m (1- (length (nth 1 x))))
306 (math-working "LUD solver step" col)
313 sum (nth col (nth ip x)))
314 (setcar (nthcdr col (nth ip x)) (nth col (nth i x)))
320 (setq sum (math-sub sum (math-mul (nth j (nth i lu))
321 (nth col (nth j x))))
323 (setcar (nthcdr col (nth i x)) sum)
325 (while (>= (setq i (1- i)) 1)
326 (setq sum (nth col (nth i x))
328 (while (<= (setq j (1+ j)) n)
329 (setq sum (math-sub sum (math-mul (nth j (nth i lu))
330 (nth col (nth j x))))))
331 (setcar (nthcdr col (nth i x))
332 (math-div sum (nth i (nth i lu)))))
336 (math-reject-arg need "*Singular matrix"))))
338 (defun calcFunc-lud (m)
339 (if (math-square-matrixp m)
340 (or (math-with-extra-prec 2
341 (let ((lud (math-matrix-lud m)))
343 (let* ((lmat (math-copy-matrix (car lud)))
344 (umat (math-copy-matrix (car lud)))
345 (n (1- (length (car lud))))
346 (perm (calcFunc-idn 1 n))
351 (setcar (nthcdr j (nth i lmat)) 0)
353 (setcar (nthcdr j (nth j lmat)) 1)
354 (while (<= (setq i (1+ i)) n)
355 (setcar (nthcdr j (nth i umat)) 0))
357 (while (>= (setq j (1- j)) 1)
358 (let ((pos (nth (1- j) (nth 1 lud))))
360 (setq perm (math-swap-rows perm j pos)))))
361 (list 'vec perm lmat umat)))))
362 (math-reject-arg m "*Singular matrix"))
363 (math-reject-arg m 'square-matrixp)))
367 ;;; calc-mtx.el ends here