1 ;;; calc-mtx.el --- matrix functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This file is autoloaded from calc-ext.el.
33 (defun calc-mdet (arg)
36 (calc-unary-op "mdet" 'calcFunc-det arg
)))
38 (defun calc-mtrace (arg)
41 (calc-unary-op "mtr" 'calcFunc-tr arg
)))
43 (defun calc-mlud (arg)
46 (calc-unary-op "mlud" 'calcFunc-lud arg
)))
49 ;;; Coerce row vector A to be a matrix. [V V]
50 (defun math-row-matrix (a)
51 (if (and (Math-vectorp a
)
52 (not (math-matrixp a
)))
56 ;;; Coerce column vector A to be a matrix. [V V]
57 (defun math-col-matrix (a)
58 (if (and (Math-vectorp a
)
59 (not (math-matrixp a
)))
60 (cons 'vec
(mapcar (function (lambda (x) (list 'vec x
))) (cdr a
)))
65 ;;; Multiply matrices A and B. [V V V]
66 (defun math-mul-mats (a b
)
68 (cols (length (nth 1 b
)))
70 (while (setq a
(cdr a
))
73 (while (> (setq col
(1- col
)) 0)
74 (setq ap
(cdr (car a
))
76 accum
(math-mul (car ap
) (nth col
(car bp
))))
77 (while (setq ap
(cdr ap
) bp
(cdr bp
))
78 (setq accum
(math-add accum
(math-mul (car ap
) (nth col
(car bp
))))))
79 (setq row
(cons accum row
)))
80 (setq mat
(cons (cons 'vec row
) mat
)))
81 (cons 'vec
(nreverse mat
))))
83 (defun math-mul-mat-vec (a b
)
84 (cons 'vec
(mapcar (function (lambda (row)
85 (math-dot-product row b
)))
90 (defun calcFunc-tr (mat) ; [Public]
91 (if (math-square-matrixp mat
)
92 (math-matrix-trace-step 2 (1- (length mat
)) mat
(nth 1 (nth 1 mat
)))
93 (math-reject-arg mat
'square-matrixp
)))
95 (defun math-matrix-trace-step (n size mat sum
)
97 (math-matrix-trace-step (1+ n
) size mat
98 (math-add sum
(nth n
(nth n mat
))))
102 ;;; Matrix inverse and determinant.
103 (defun math-matrix-inv-raw (m)
104 (let ((n (1- (length m
))))
106 (let ((det (math-det-raw m
)))
107 (and (not (math-zerop det
))
114 (math-neg (nth 2 (nth 1 m
))))
116 (math-neg (nth 1 (nth 2 m
)))
121 (math-sub (math-mul (nth 3 (nth 3 m
))
123 (math-mul (nth 3 (nth 2 m
))
125 (math-sub (math-mul (nth 3 (nth 1 m
))
127 (math-mul (nth 3 (nth 3 m
))
129 (math-sub (math-mul (nth 3 (nth 2 m
))
131 (math-mul (nth 3 (nth 1 m
))
134 (math-sub (math-mul (nth 3 (nth 2 m
))
136 (math-mul (nth 3 (nth 3 m
))
138 (math-sub (math-mul (nth 3 (nth 3 m
))
140 (math-mul (nth 3 (nth 1 m
))
142 (math-sub (math-mul (nth 3 (nth 1 m
))
144 (math-mul (nth 3 (nth 2 m
))
147 (math-sub (math-mul (nth 2 (nth 3 m
))
149 (math-mul (nth 2 (nth 2 m
))
151 (math-sub (math-mul (nth 2 (nth 1 m
))
153 (math-mul (nth 2 (nth 3 m
))
155 (math-sub (math-mul (nth 2 (nth 2 m
))
157 (math-mul (nth 2 (nth 1 m
))
158 (nth 1 (nth 2 m
))))))))
160 (let ((lud (math-matrix-lud m
)))
162 (math-lud-solve lud
(calcFunc-idn 1 n
)))))))
164 (defun calcFunc-det (m)
165 (if (math-square-matrixp m
)
166 (math-with-extra-prec 2 (math-det-raw m
))
167 (if (and (eq (car-safe m
) 'calcFunc-idn
)
168 (or (math-zerop (nth 1 m
))
169 (math-equal-int (nth 1 m
) 1)))
171 (math-reject-arg m
'square-matrixp
))))
173 ;; The variable math-det-lu is local to math-det-raw, but is
174 ;; used by math-det-step, which is called by math-det-raw.
177 (defun math-det-raw (m)
178 (let ((n (1- (length m
))))
182 (math-sub (math-mul (nth 1 (nth 1 m
))
184 (math-mul (nth 2 (nth 1 m
))
192 (math-mul (nth 1 (nth 1 m
))
193 (math-mul (nth 2 (nth 2 m
))
195 (math-mul (nth 2 (nth 1 m
))
196 (math-mul (nth 3 (nth 2 m
))
198 (math-mul (nth 3 (nth 1 m
))
199 (math-mul (nth 1 (nth 2 m
))
201 (math-mul (nth 3 (nth 1 m
))
202 (math-mul (nth 2 (nth 2 m
))
204 (math-mul (nth 1 (nth 1 m
))
205 (math-mul (nth 3 (nth 2 m
))
207 (math-mul (nth 2 (nth 1 m
))
208 (math-mul (nth 1 (nth 2 m
))
209 (nth 3 (nth 3 m
))))))
210 (t (let ((lud (math-matrix-lud m
)))
212 (let ((math-det-lu (car lud
)))
213 (math-det-step n
(nth 2 lud
)))
216 (defun math-det-step (n prod
)
218 (math-det-step (1- n
) (math-mul prod
(nth n
(nth n math-det-lu
))))
221 ;;; This returns a list (LU index d), or nil if not possible.
222 ;;; Argument M must be a square matrix.
223 (defvar math-lud-cache nil
)
224 (defun math-matrix-lud (m)
225 (let ((old (assoc m math-lud-cache
))
226 (context (list calc-internal-prec calc-prefer-frac
)))
227 (if (and old
(equal (nth 1 old
) context
))
229 (let* ((lud (catch 'singular
(math-do-matrix-lud m
)))
230 (entry (cons context lud
)))
233 (setq math-lud-cache
(cons (cons m entry
) math-lud-cache
)))
236 ;;; Numerical Recipes section 2.3; implicit pivoting omitted.
237 (defun math-do-matrix-lud (m)
238 (let* ((lu (math-copy-matrix m
))
240 i
(j 1) k imax sum big
247 (math-working "LUD step" (format "%d/%d" j i
))
248 (setq sum
(nth j
(nth i lu
))
251 (setq sum
(math-sub sum
(math-mul (nth k
(nth i lu
))
254 (setcar (nthcdr j
(nth i lu
)) sum
)
257 (math-working "LUD step" (format "%d/%d" j i
))
258 (setq sum
(nth j
(nth i lu
))
261 (setq sum
(math-sub sum
(math-mul (nth k
(nth i lu
))
264 (setcar (nthcdr j
(nth i lu
)) sum
)
265 (let ((dum (math-abs-approx sum
)))
266 (if (Math-lessp big dum
)
271 (setq lu
(math-swap-rows lu j imax
)
273 (setq index
(cons imax index
))
274 (let ((pivot (nth j
(nth j lu
))))
275 (if (math-zerop pivot
)
276 (throw 'singular nil
)
278 (while (<= (setq i
(1+ i
)) n
)
279 (setcar (nthcdr j
(nth i lu
))
280 (math-div (nth j
(nth i lu
)) pivot
)))))
282 (list lu
(nreverse index
) d
)))
284 (defun math-swap-rows (m r1 r2
)
286 (let* ((r1prev (nthcdr (1- r1
) m
))
288 (r2prev (nthcdr (1- r2
) m
))
293 (setcdr row2
(cdr row1
))
294 (setcdr row1 r2next
)))
298 (defun math-lud-solve (lud b
&optional need
)
300 (let* ((x (math-copy-matrix b
))
302 (m (1- (length (nth 1 x
))))
307 (math-working "LUD solver step" col
)
314 sum
(nth col
(nth ip x
)))
315 (setcar (nthcdr col
(nth ip x
)) (nth col
(nth i x
)))
321 (setq sum
(math-sub sum
(math-mul (nth j
(nth i lu
))
322 (nth col
(nth j x
))))
324 (setcar (nthcdr col
(nth i x
)) sum
)
326 (while (>= (setq i
(1- i
)) 1)
327 (setq sum
(nth col
(nth i x
))
329 (while (<= (setq j
(1+ j
)) n
)
330 (setq sum
(math-sub sum
(math-mul (nth j
(nth i lu
))
331 (nth col
(nth j x
))))))
332 (setcar (nthcdr col
(nth i x
))
333 (math-div sum
(nth i
(nth i lu
)))))
337 (math-reject-arg need
"*Singular matrix"))))
339 (defun calcFunc-lud (m)
340 (if (math-square-matrixp m
)
341 (or (math-with-extra-prec 2
342 (let ((lud (math-matrix-lud m
)))
344 (let* ((lmat (math-copy-matrix (car lud
)))
345 (umat (math-copy-matrix (car lud
)))
346 (n (1- (length (car lud
))))
347 (perm (calcFunc-idn 1 n
))
352 (setcar (nthcdr j
(nth i lmat
)) 0)
354 (setcar (nthcdr j
(nth j lmat
)) 1)
355 (while (<= (setq i
(1+ i
)) n
)
356 (setcar (nthcdr j
(nth i umat
)) 0))
358 (while (>= (setq j
(1- j
)) 1)
359 (let ((pos (nth (1- j
) (nth 1 lud
))))
361 (setq perm
(math-swap-rows perm j pos
)))))
362 (list 'vec perm lmat umat
)))))
363 (math-reject-arg m
"*Singular matrix"))
364 (math-reject-arg m
'square-matrixp
)))
368 ;; arch-tag: fc0947b1-90e1-4a23-8950-d8ead9c3a306
369 ;;; calc-mtx.el ends here