1 ;;; calc-mtx.el --- matrix functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainers: D. Goel <deego@gnufans.org>
7 ;; Colin Walters <walters@debian.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
31 ;; This file is autoloaded from calc-ext.el.
36 (defun calc-Need-calc-mat () nil)
39 (defun calc-mdet (arg)
42 (calc-unary-op "mdet" 'calcFunc-det arg)))
44 (defun calc-mtrace (arg)
47 (calc-unary-op "mtr" 'calcFunc-tr arg)))
49 (defun calc-mlud (arg)
52 (calc-unary-op "mlud" 'calcFunc-lud arg)))
55 ;;; Coerce row vector A to be a matrix. [V V]
56 (defun math-row-matrix (a)
57 (if (and (Math-vectorp a)
58 (not (math-matrixp a)))
62 ;;; Coerce column vector A to be a matrix. [V V]
63 (defun math-col-matrix (a)
64 (if (and (Math-vectorp a)
65 (not (math-matrixp a)))
66 (cons 'vec (mapcar (function (lambda (x) (list 'vec x))) (cdr a)))
71 ;;; Multiply matrices A and B. [V V V]
72 (defun math-mul-mats (a b)
74 (cols (length (nth 1 b)))
76 (while (setq a (cdr a))
79 (while (> (setq col (1- col)) 0)
80 (setq ap (cdr (car a))
82 accum (math-mul (car ap) (nth col (car bp))))
83 (while (setq ap (cdr ap) bp (cdr bp))
84 (setq accum (math-add accum (math-mul (car ap) (nth col (car bp))))))
85 (setq row (cons accum row)))
86 (setq mat (cons (cons 'vec row) mat)))
87 (cons 'vec (nreverse mat))))
89 (defun math-mul-mat-vec (a b)
90 (cons 'vec (mapcar (function (lambda (row)
91 (math-dot-product row b)))
96 (defun calcFunc-tr (mat) ; [Public]
97 (if (math-square-matrixp mat)
98 (math-matrix-trace-step 2 (1- (length mat)) mat (nth 1 (nth 1 mat)))
99 (math-reject-arg mat 'square-matrixp)))
101 (defun math-matrix-trace-step (n size mat sum)
103 (math-matrix-trace-step (1+ n) size mat
104 (math-add sum (nth n (nth n mat))))
108 ;;; Matrix inverse and determinant.
109 (defun math-matrix-inv-raw (m)
110 (let ((n (1- (length m))))
112 (let ((det (math-det-raw m)))
113 (and (not (math-zerop det))
120 (math-neg (nth 2 (nth 1 m))))
122 (math-neg (nth 1 (nth 2 m)))
127 (math-sub (math-mul (nth 3 (nth 3 m))
129 (math-mul (nth 3 (nth 2 m))
131 (math-sub (math-mul (nth 3 (nth 1 m))
133 (math-mul (nth 3 (nth 3 m))
135 (math-sub (math-mul (nth 3 (nth 2 m))
137 (math-mul (nth 3 (nth 1 m))
140 (math-sub (math-mul (nth 3 (nth 2 m))
142 (math-mul (nth 3 (nth 3 m))
144 (math-sub (math-mul (nth 3 (nth 3 m))
146 (math-mul (nth 3 (nth 1 m))
148 (math-sub (math-mul (nth 3 (nth 1 m))
150 (math-mul (nth 3 (nth 2 m))
153 (math-sub (math-mul (nth 2 (nth 3 m))
155 (math-mul (nth 2 (nth 2 m))
157 (math-sub (math-mul (nth 2 (nth 1 m))
159 (math-mul (nth 2 (nth 3 m))
161 (math-sub (math-mul (nth 2 (nth 2 m))
163 (math-mul (nth 2 (nth 1 m))
164 (nth 1 (nth 2 m))))))))
166 (let ((lud (math-matrix-lud m)))
168 (math-lud-solve lud (calcFunc-idn 1 n)))))))
170 (defun calcFunc-det (m)
171 (if (math-square-matrixp m)
172 (math-with-extra-prec 2 (math-det-raw m))
173 (if (and (eq (car-safe m) 'calcFunc-idn)
174 (or (math-zerop (nth 1 m))
175 (math-equal-int (nth 1 m) 1)))
177 (math-reject-arg m 'square-matrixp))))
179 (defun math-det-raw (m)
180 (let ((n (1- (length m))))
184 (math-sub (math-mul (nth 1 (nth 1 m))
186 (math-mul (nth 2 (nth 1 m))
194 (math-mul (nth 1 (nth 1 m))
195 (math-mul (nth 2 (nth 2 m))
197 (math-mul (nth 2 (nth 1 m))
198 (math-mul (nth 3 (nth 2 m))
200 (math-mul (nth 3 (nth 1 m))
201 (math-mul (nth 1 (nth 2 m))
203 (math-mul (nth 3 (nth 1 m))
204 (math-mul (nth 2 (nth 2 m))
206 (math-mul (nth 1 (nth 1 m))
207 (math-mul (nth 3 (nth 2 m))
209 (math-mul (nth 2 (nth 1 m))
210 (math-mul (nth 1 (nth 2 m))
211 (nth 3 (nth 3 m))))))
212 (t (let ((lud (math-matrix-lud m)))
214 (let ((lu (car lud)))
215 (math-det-step n (nth 2 lud)))
218 (defun math-det-step (n prod)
220 (math-det-step (1- n) (math-mul prod (nth n (nth n lu))))
223 ;;; This returns a list (LU index d), or nil if not possible.
224 ;;; Argument M must be a square matrix.
225 (defvar math-lud-cache nil)
226 (defun math-matrix-lud (m)
227 (let ((old (assoc m math-lud-cache))
228 (context (list calc-internal-prec calc-prefer-frac)))
229 (if (and old (equal (nth 1 old) context))
231 (let* ((lud (catch 'singular (math-do-matrix-lud m)))
232 (entry (cons context lud)))
235 (setq math-lud-cache (cons (cons m entry) math-lud-cache)))
238 ;;; Numerical Recipes section 2.3; implicit pivoting omitted.
239 (defun math-do-matrix-lud (m)
240 (let* ((lu (math-copy-matrix m))
242 i (j 1) k imax sum big
249 (math-working "LUD step" (format "%d/%d" j i))
250 (setq sum (nth j (nth i lu))
253 (setq sum (math-sub sum (math-mul (nth k (nth i lu))
256 (setcar (nthcdr j (nth i lu)) sum)
259 (math-working "LUD step" (format "%d/%d" j i))
260 (setq sum (nth j (nth i lu))
263 (setq sum (math-sub sum (math-mul (nth k (nth i lu))
266 (setcar (nthcdr j (nth i lu)) sum)
267 (let ((dum (math-abs-approx sum)))
268 (if (Math-lessp big dum)
273 (setq lu (math-swap-rows lu j imax)
275 (setq index (cons imax index))
276 (let ((pivot (nth j (nth j lu))))
277 (if (math-zerop pivot)
278 (throw 'singular nil)
280 (while (<= (setq i (1+ i)) n)
281 (setcar (nthcdr j (nth i lu))
282 (math-div (nth j (nth i lu)) pivot)))))
284 (list lu (nreverse index) d)))
286 (defun math-swap-rows (m r1 r2)
288 (let* ((r1prev (nthcdr (1- r1) m))
290 (r2prev (nthcdr (1- r2) m))
295 (setcdr row2 (cdr row1))
296 (setcdr row1 r2next)))
300 (defun math-lud-solve (lud b &optional need)
302 (let* ((x (math-copy-matrix b))
304 (m (1- (length (nth 1 x))))
309 (math-working "LUD solver step" col)
316 sum (nth col (nth ip x)))
317 (setcar (nthcdr col (nth ip x)) (nth col (nth i x)))
323 (setq sum (math-sub sum (math-mul (nth j (nth i lu))
324 (nth col (nth j x))))
326 (setcar (nthcdr col (nth i x)) sum)
328 (while (>= (setq i (1- i)) 1)
329 (setq sum (nth col (nth i x))
331 (while (<= (setq j (1+ j)) n)
332 (setq sum (math-sub sum (math-mul (nth j (nth i lu))
333 (nth col (nth j x))))))
334 (setcar (nthcdr col (nth i x))
335 (math-div sum (nth i (nth i lu)))))
339 (math-reject-arg need "*Singular matrix"))))
341 (defun calcFunc-lud (m)
342 (if (math-square-matrixp m)
343 (or (math-with-extra-prec 2
344 (let ((lud (math-matrix-lud m)))
346 (let* ((lmat (math-copy-matrix (car lud)))
347 (umat (math-copy-matrix (car lud)))
348 (n (1- (length (car lud))))
349 (perm (calcFunc-idn 1 n))
354 (setcar (nthcdr j (nth i lmat)) 0)
356 (setcar (nthcdr j (nth j lmat)) 1)
357 (while (<= (setq i (1+ i)) n)
358 (setcar (nthcdr j (nth i umat)) 0))
360 (while (>= (setq j (1- j)) 1)
361 (let ((pos (nth (1- j) (nth 1 lud))))
363 (setq perm (math-swap-rows perm j pos)))))
364 (list 'vec perm lmat umat)))))
365 (math-reject-arg m "*Singular matrix"))
366 (math-reject-arg m 'square-matrixp)))
368 ;;; calc-mtx.el ends here