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
)))
236 (defun math-lud-pivot-check (a)
237 "Determine a useful value for checking the size of potential pivots
238 in LUD decomposition."
239 (cond ((eq (car-safe a
) 'mod
)
240 (if (and (math-integerp (nth 1 a
))
241 (math-integerp (nth 2 a
))
242 (eq (math-gcd (nth 1 a
) (nth 2 a
)) 1))
246 (math-abs-approx a
))))
249 ;;; Numerical Recipes section 2.3; implicit pivoting omitted.
250 (defun math-do-matrix-lud (m)
251 (let* ((lu (math-copy-matrix m
))
253 i
(j 1) k imax sum big
260 (math-working "LUD step" (format "%d/%d" j i
))
261 (setq sum
(nth j
(nth i lu
))
264 (setq sum
(math-sub sum
(math-mul (nth k
(nth i lu
))
267 (setcar (nthcdr j
(nth i lu
)) sum
)
270 (math-working "LUD step" (format "%d/%d" j i
))
271 (setq sum
(nth j
(nth i lu
))
274 (setq sum
(math-sub sum
(math-mul (nth k
(nth i lu
))
277 (setcar (nthcdr j
(nth i lu
)) sum
)
278 (let ((dum (math-lud-pivot-check sum
)))
279 (if (Math-lessp big dum
)
284 (setq lu
(math-swap-rows lu j imax
)
286 (setq index
(cons imax index
))
287 (let ((pivot (nth j
(nth j lu
))))
288 (if (math-zerop pivot
)
289 (throw 'singular nil
)
291 (while (<= (setq i
(1+ i
)) n
)
292 (setcar (nthcdr j
(nth i lu
))
293 (math-div (nth j
(nth i lu
)) pivot
)))))
295 (list lu
(nreverse index
) d
)))
297 (defun math-swap-rows (m r1 r2
)
299 (let* ((r1prev (nthcdr (1- r1
) m
))
301 (r2prev (nthcdr (1- r2
) m
))
306 (setcdr row2
(cdr row1
))
307 (setcdr row1 r2next
)))
311 (defun math-lud-solve (lud b
&optional need
)
313 (let* ((x (math-copy-matrix b
))
315 (m (1- (length (nth 1 x
))))
320 (math-working "LUD solver step" col
)
327 sum
(nth col
(nth ip x
)))
328 (setcar (nthcdr col
(nth ip x
)) (nth col
(nth i x
)))
334 (setq sum
(math-sub sum
(math-mul (nth j
(nth i lu
))
335 (nth col
(nth j x
))))
337 (setcar (nthcdr col
(nth i x
)) sum
)
339 (while (>= (setq i
(1- i
)) 1)
340 (setq sum
(nth col
(nth i x
))
342 (while (<= (setq j
(1+ j
)) n
)
343 (setq sum
(math-sub sum
(math-mul (nth j
(nth i lu
))
344 (nth col
(nth j x
))))))
345 (setcar (nthcdr col
(nth i x
))
346 (math-div sum
(nth i
(nth i lu
)))))
350 (math-reject-arg need
"*Singular matrix"))))
352 (defun calcFunc-lud (m)
353 (if (math-square-matrixp m
)
354 (or (math-with-extra-prec 2
355 (let ((lud (math-matrix-lud m
)))
357 (let* ((lmat (math-copy-matrix (car lud
)))
358 (umat (math-copy-matrix (car lud
)))
359 (n (1- (length (car lud
))))
360 (perm (calcFunc-idn 1 n
))
365 (setcar (nthcdr j
(nth i lmat
)) 0)
367 (setcar (nthcdr j
(nth j lmat
)) 1)
368 (while (<= (setq i
(1+ i
)) n
)
369 (setcar (nthcdr j
(nth i umat
)) 0))
371 (while (>= (setq j
(1- j
)) 1)
372 (let ((pos (nth (1- j
) (nth 1 lud
))))
374 (setq perm
(math-swap-rows perm j pos
)))))
375 (list 'vec perm lmat umat
)))))
376 (math-reject-arg m
"*Singular matrix"))
377 (math-reject-arg m
'square-matrixp
)))
381 ;;; calc-mtx.el ends here