1 ;; Calculator for GNU Emacs, part II [calc-mat.el]
2 ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
24 ;; This file is autoloaded from calc-ext.el.
29 (defun calc-Need-calc-mat () nil)
32 (defun calc-mdet (arg)
35 (calc-unary-op "mdet" 'calcFunc-det arg))
38 (defun calc-mtrace (arg)
41 (calc-unary-op "mtr" 'calcFunc-tr arg))
44 (defun calc-mlud (arg)
47 (calc-unary-op "mlud" 'calcFunc-lud arg))
51 ;;; Coerce row vector A to be a matrix. [V V]
52 (defun math-row-matrix (a)
53 (if (and (Math-vectorp a)
54 (not (math-matrixp a)))
59 ;;; Coerce column vector A to be a matrix. [V V]
60 (defun math-col-matrix (a)
61 (if (and (Math-vectorp a)
62 (not (math-matrixp a)))
63 (cons 'vec (mapcar (function (lambda (x) (list 'vec x))) (cdr a)))
69 ;;; Multiply matrices A and B. [V V V]
70 (defun math-mul-mats (a b)
72 (cols (length (nth 1 b)))
74 (while (setq a (cdr a))
77 (while (> (setq col (1- col)) 0)
78 (setq ap (cdr (car a))
80 accum (math-mul (car ap) (nth col (car bp))))
81 (while (setq ap (cdr ap) bp (cdr bp))
82 (setq accum (math-add accum (math-mul (car ap) (nth col (car bp))))))
83 (setq row (cons accum row)))
84 (setq mat (cons (cons 'vec row) mat)))
85 (cons 'vec (nreverse mat)))
88 (defun math-mul-mat-vec (a b)
89 (cons 'vec (mapcar (function (lambda (row)
90 (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))
102 (defun math-matrix-trace-step (n size mat sum)
104 (math-matrix-trace-step (1+ n) size mat
105 (math-add sum (nth n (nth n mat))))
110 ;;; Matrix inverse and determinant.
111 (defun math-matrix-inv-raw (m)
112 (let ((n (1- (length m))))
114 (let ((det (math-det-raw m)))
115 (and (not (math-zerop det))
122 (math-neg (nth 2 (nth 1 m))))
124 (math-neg (nth 1 (nth 2 m)))
129 (math-sub (math-mul (nth 3 (nth 3 m))
131 (math-mul (nth 3 (nth 2 m))
133 (math-sub (math-mul (nth 3 (nth 1 m))
135 (math-mul (nth 3 (nth 3 m))
137 (math-sub (math-mul (nth 3 (nth 2 m))
139 (math-mul (nth 3 (nth 1 m))
142 (math-sub (math-mul (nth 3 (nth 2 m))
144 (math-mul (nth 3 (nth 3 m))
146 (math-sub (math-mul (nth 3 (nth 3 m))
148 (math-mul (nth 3 (nth 1 m))
150 (math-sub (math-mul (nth 3 (nth 1 m))
152 (math-mul (nth 3 (nth 2 m))
155 (math-sub (math-mul (nth 2 (nth 3 m))
157 (math-mul (nth 2 (nth 2 m))
159 (math-sub (math-mul (nth 2 (nth 1 m))
161 (math-mul (nth 2 (nth 3 m))
163 (math-sub (math-mul (nth 2 (nth 2 m))
165 (math-mul (nth 2 (nth 1 m))
166 (nth 1 (nth 2 m))))))))
168 (let ((lud (math-matrix-lud m)))
170 (math-lud-solve lud (calcFunc-idn 1 n))))))
173 (defun calcFunc-det (m)
174 (if (math-square-matrixp m)
175 (math-with-extra-prec 2 (math-det-raw m))
176 (if (and (eq (car-safe m) 'calcFunc-idn)
177 (or (math-zerop (nth 1 m))
178 (math-equal-int (nth 1 m) 1)))
180 (math-reject-arg m 'square-matrixp)))
183 (defun math-det-raw (m)
184 (let ((n (1- (length m))))
188 (math-sub (math-mul (nth 1 (nth 1 m))
190 (math-mul (nth 2 (nth 1 m))
198 (math-mul (nth 1 (nth 1 m))
199 (math-mul (nth 2 (nth 2 m))
201 (math-mul (nth 2 (nth 1 m))
202 (math-mul (nth 3 (nth 2 m))
204 (math-mul (nth 3 (nth 1 m))
205 (math-mul (nth 1 (nth 2 m))
207 (math-mul (nth 3 (nth 1 m))
208 (math-mul (nth 2 (nth 2 m))
210 (math-mul (nth 1 (nth 1 m))
211 (math-mul (nth 3 (nth 2 m))
213 (math-mul (nth 2 (nth 1 m))
214 (math-mul (nth 1 (nth 2 m))
215 (nth 3 (nth 3 m))))))
216 (t (let ((lud (math-matrix-lud m)))
218 (let ((lu (car lud)))
219 (math-det-step n (nth 2 lud)))
223 (defun math-det-step (n prod)
225 (math-det-step (1- n) (math-mul prod (nth n (nth n lu))))
229 ;;; This returns a list (LU index d), or NIL if not possible.
230 ;;; Argument M must be a square matrix.
231 (defun math-matrix-lud (m)
232 (let ((old (assoc m math-lud-cache))
233 (context (list calc-internal-prec calc-prefer-frac)))
234 (if (and old (equal (nth 1 old) context))
236 (let* ((lud (catch 'singular (math-do-matrix-lud m)))
237 (entry (cons context lud)))
240 (setq math-lud-cache (cons (cons m entry) math-lud-cache)))
243 (defvar math-lud-cache nil)
245 ;;; Numerical Recipes section 2.3; implicit pivoting omitted.
246 (defun math-do-matrix-lud (m)
247 (let* ((lu (math-copy-matrix m))
249 i (j 1) k imax sum big
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)
266 (math-working "LUD step" (format "%d/%d" j i))
267 (setq sum (nth j (nth i lu))
270 (setq sum (math-sub sum (math-mul (nth k (nth i lu))
273 (setcar (nthcdr j (nth i lu)) sum)
274 (let ((dum (math-abs-approx sum)))
275 (if (Math-lessp big dum)
280 (setq lu (math-swap-rows lu j imax)
282 (setq index (cons imax index))
283 (let ((pivot (nth j (nth j lu))))
284 (if (math-zerop pivot)
285 (throw 'singular nil)
287 (while (<= (setq i (1+ i)) n)
288 (setcar (nthcdr j (nth i lu))
289 (math-div (nth j (nth i lu)) pivot)))))
291 (list lu (nreverse index) d))
294 (defun math-swap-rows (m r1 r2)
296 (let* ((r1prev (nthcdr (1- r1) m))
298 (r2prev (nthcdr (1- r2) m))
303 (setcdr row2 (cdr row1))
304 (setcdr row1 r2next)))
309 (defun math-lud-solve (lud b &optional need)
311 (let* ((x (math-copy-matrix b))
313 (m (1- (length (nth 1 x))))
318 (math-working "LUD solver step" col)
325 sum (nth col (nth ip x)))
326 (setcar (nthcdr col (nth ip x)) (nth col (nth i x)))
332 (setq sum (math-sub sum (math-mul (nth j (nth i lu))
333 (nth col (nth j x))))
335 (setcar (nthcdr col (nth i x)) sum)
337 (while (>= (setq i (1- i)) 1)
338 (setq sum (nth col (nth i x))
340 (while (<= (setq j (1+ j)) n)
341 (setq sum (math-sub sum (math-mul (nth j (nth i lu))
342 (nth col (nth j x))))))
343 (setcar (nthcdr col (nth i x))
344 (math-div sum (nth i (nth i lu)))))
348 (math-reject-arg need "*Singular matrix")))
351 (defun calcFunc-lud (m)
352 (if (math-square-matrixp m)
353 (or (math-with-extra-prec 2
354 (let ((lud (math-matrix-lud m)))
356 (let* ((lmat (math-copy-matrix (car lud)))
357 (umat (math-copy-matrix (car lud)))
358 (n (1- (length (car lud))))
359 (perm (calcFunc-idn 1 n))
364 (setcar (nthcdr j (nth i lmat)) 0)
366 (setcar (nthcdr j (nth j lmat)) 1)
367 (while (<= (setq i (1+ i)) n)
368 (setcar (nthcdr j (nth i umat)) 0))
370 (while (>= (setq j (1- j)) 1)
371 (let ((pos (nth (1- j) (nth 1 lud))))
373 (setq perm (math-swap-rows perm j pos)))))
374 (list 'vec perm lmat umat)))))
375 (math-reject-arg m "*Singular matrix"))
376 (math-reject-arg m 'square-matrixp))