Mercurial > emacs
annotate lisp/calc/calc-mtx.el @ 41215:e4e7371d61e9
(minibuffer-history-sexp-flag): Doc fix.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sat, 17 Nov 2001 22:28:30 +0000 |
parents | 73f364fd8aaa |
children | fcd507927105 |
rev | line source |
---|---|
40785 | 1 ;; Calculator for GNU Emacs, part II [calc-mat.el] |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
40785 | 3 ;; Written by Dave Gillespie, daveg@synaptics.com. |
4 | |
5 ;; This file is part of GNU Emacs. | |
6 | |
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. | |
13 | |
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. | |
21 | |
22 | |
23 | |
24 ;; This file is autoloaded from calc-ext.el. | |
25 (require 'calc-ext) | |
26 | |
27 (require 'calc-macs) | |
28 | |
29 (defun calc-Need-calc-mat () nil) | |
30 | |
31 | |
32 (defun calc-mdet (arg) | |
33 (interactive "P") | |
34 (calc-slow-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
35 (calc-unary-op "mdet" 'calcFunc-det arg))) |
40785 | 36 |
37 (defun calc-mtrace (arg) | |
38 (interactive "P") | |
39 (calc-slow-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
40 (calc-unary-op "mtr" 'calcFunc-tr arg))) |
40785 | 41 |
42 (defun calc-mlud (arg) | |
43 (interactive "P") | |
44 (calc-slow-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
45 (calc-unary-op "mlud" 'calcFunc-lud arg))) |
40785 | 46 |
47 | |
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))) | |
52 (list 'vec a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
53 a)) |
40785 | 54 |
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))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
60 a)) |
40785 | 61 |
62 | |
63 | |
64 ;;; Multiply matrices A and B. [V V V] | |
65 (defun math-mul-mats (a b) | |
66 (let ((mat nil) | |
67 (cols (length (nth 1 b))) | |
68 row col ap bp accum) | |
69 (while (setq a (cdr a)) | |
70 (setq col cols | |
71 row nil) | |
72 (while (> (setq col (1- col)) 0) | |
73 (setq ap (cdr (car a)) | |
74 bp (cdr b) | |
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))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
80 (cons 'vec (nreverse mat)))) |
40785 | 81 |
82 (defun math-mul-mat-vec (a b) | |
83 (cons 'vec (mapcar (function (lambda (row) | |
84 (math-dot-product row b))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
85 (cdr a)))) |
40785 | 86 |
87 | |
88 | |
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))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
92 (math-reject-arg mat 'square-matrixp))) |
40785 | 93 |
94 (defun math-matrix-trace-step (n size mat sum) | |
95 (if (<= n size) | |
96 (math-matrix-trace-step (1+ n) size mat | |
97 (math-add sum (nth n (nth n mat)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
98 sum)) |
40785 | 99 |
100 | |
101 ;;; Matrix inverse and determinant. | |
102 (defun math-matrix-inv-raw (m) | |
103 (let ((n (1- (length m)))) | |
104 (if (<= n 3) | |
105 (let ((det (math-det-raw m))) | |
106 (and (not (math-zerop det)) | |
107 (math-div | |
108 (cond ((= n 1) 1) | |
109 ((= n 2) | |
110 (list 'vec | |
111 (list 'vec | |
112 (nth 2 (nth 2 m)) | |
113 (math-neg (nth 2 (nth 1 m)))) | |
114 (list 'vec | |
115 (math-neg (nth 1 (nth 2 m))) | |
116 (nth 1 (nth 1 m))))) | |
117 ((= n 3) | |
118 (list 'vec | |
119 (list 'vec | |
120 (math-sub (math-mul (nth 3 (nth 3 m)) | |
121 (nth 2 (nth 2 m))) | |
122 (math-mul (nth 3 (nth 2 m)) | |
123 (nth 2 (nth 3 m)))) | |
124 (math-sub (math-mul (nth 3 (nth 1 m)) | |
125 (nth 2 (nth 3 m))) | |
126 (math-mul (nth 3 (nth 3 m)) | |
127 (nth 2 (nth 1 m)))) | |
128 (math-sub (math-mul (nth 3 (nth 2 m)) | |
129 (nth 2 (nth 1 m))) | |
130 (math-mul (nth 3 (nth 1 m)) | |
131 (nth 2 (nth 2 m))))) | |
132 (list 'vec | |
133 (math-sub (math-mul (nth 3 (nth 2 m)) | |
134 (nth 1 (nth 3 m))) | |
135 (math-mul (nth 3 (nth 3 m)) | |
136 (nth 1 (nth 2 m)))) | |
137 (math-sub (math-mul (nth 3 (nth 3 m)) | |
138 (nth 1 (nth 1 m))) | |
139 (math-mul (nth 3 (nth 1 m)) | |
140 (nth 1 (nth 3 m)))) | |
141 (math-sub (math-mul (nth 3 (nth 1 m)) | |
142 (nth 1 (nth 2 m))) | |
143 (math-mul (nth 3 (nth 2 m)) | |
144 (nth 1 (nth 1 m))))) | |
145 (list 'vec | |
146 (math-sub (math-mul (nth 2 (nth 3 m)) | |
147 (nth 1 (nth 2 m))) | |
148 (math-mul (nth 2 (nth 2 m)) | |
149 (nth 1 (nth 3 m)))) | |
150 (math-sub (math-mul (nth 2 (nth 1 m)) | |
151 (nth 1 (nth 3 m))) | |
152 (math-mul (nth 2 (nth 3 m)) | |
153 (nth 1 (nth 1 m)))) | |
154 (math-sub (math-mul (nth 2 (nth 2 m)) | |
155 (nth 1 (nth 1 m))) | |
156 (math-mul (nth 2 (nth 1 m)) | |
157 (nth 1 (nth 2 m)))))))) | |
158 det))) | |
159 (let ((lud (math-matrix-lud m))) | |
160 (and lud | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
161 (math-lud-solve lud (calcFunc-idn 1 n))))))) |
40785 | 162 |
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))) | |
169 (nth 1 m) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
170 (math-reject-arg m 'square-matrixp)))) |
40785 | 171 |
172 (defun math-det-raw (m) | |
173 (let ((n (1- (length m)))) | |
174 (cond ((= n 1) | |
175 (nth 1 (nth 1 m))) | |
176 ((= n 2) | |
177 (math-sub (math-mul (nth 1 (nth 1 m)) | |
178 (nth 2 (nth 2 m))) | |
179 (math-mul (nth 2 (nth 1 m)) | |
180 (nth 1 (nth 2 m))))) | |
181 ((= n 3) | |
182 (math-sub | |
183 (math-sub | |
184 (math-sub | |
185 (math-add | |
186 (math-add | |
187 (math-mul (nth 1 (nth 1 m)) | |
188 (math-mul (nth 2 (nth 2 m)) | |
189 (nth 3 (nth 3 m)))) | |
190 (math-mul (nth 2 (nth 1 m)) | |
191 (math-mul (nth 3 (nth 2 m)) | |
192 (nth 1 (nth 3 m))))) | |
193 (math-mul (nth 3 (nth 1 m)) | |
194 (math-mul (nth 1 (nth 2 m)) | |
195 (nth 2 (nth 3 m))))) | |
196 (math-mul (nth 3 (nth 1 m)) | |
197 (math-mul (nth 2 (nth 2 m)) | |
198 (nth 1 (nth 3 m))))) | |
199 (math-mul (nth 1 (nth 1 m)) | |
200 (math-mul (nth 3 (nth 2 m)) | |
201 (nth 2 (nth 3 m))))) | |
202 (math-mul (nth 2 (nth 1 m)) | |
203 (math-mul (nth 1 (nth 2 m)) | |
204 (nth 3 (nth 3 m)))))) | |
205 (t (let ((lud (math-matrix-lud m))) | |
206 (if lud | |
207 (let ((lu (car lud))) | |
208 (math-det-step n (nth 2 lud))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
209 0)))))) |
40785 | 210 |
211 (defun math-det-step (n prod) | |
212 (if (> n 0) | |
213 (math-det-step (1- n) (math-mul prod (nth n (nth n lu)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
214 prod)) |
40785 | 215 |
216 ;;; This returns a list (LU index d), or NIL if not possible. | |
217 ;;; Argument M must be a square matrix. | |
218 (defun math-matrix-lud (m) | |
219 (let ((old (assoc m math-lud-cache)) | |
220 (context (list calc-internal-prec calc-prefer-frac))) | |
221 (if (and old (equal (nth 1 old) context)) | |
222 (cdr (cdr old)) | |
223 (let* ((lud (catch 'singular (math-do-matrix-lud m))) | |
224 (entry (cons context lud))) | |
225 (if old | |
226 (setcdr old entry) | |
227 (setq math-lud-cache (cons (cons m entry) math-lud-cache))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
228 lud)))) |
40785 | 229 (defvar math-lud-cache nil) |
230 | |
231 ;;; Numerical Recipes section 2.3; implicit pivoting omitted. | |
232 (defun math-do-matrix-lud (m) | |
233 (let* ((lu (math-copy-matrix m)) | |
234 (n (1- (length lu))) | |
235 i (j 1) k imax sum big | |
236 (d 1) (index nil)) | |
237 (while (<= j n) | |
238 (setq i 1 | |
239 big 0 | |
240 imax j) | |
241 (while (< i j) | |
242 (math-working "LUD step" (format "%d/%d" j i)) | |
243 (setq sum (nth j (nth i lu)) | |
244 k 1) | |
245 (while (< k i) | |
246 (setq sum (math-sub sum (math-mul (nth k (nth i lu)) | |
247 (nth j (nth k lu)))) | |
248 k (1+ k))) | |
249 (setcar (nthcdr j (nth i lu)) sum) | |
250 (setq i (1+ i))) | |
251 (while (<= i n) | |
252 (math-working "LUD step" (format "%d/%d" j i)) | |
253 (setq sum (nth j (nth i lu)) | |
254 k 1) | |
255 (while (< k j) | |
256 (setq sum (math-sub sum (math-mul (nth k (nth i lu)) | |
257 (nth j (nth k lu)))) | |
258 k (1+ k))) | |
259 (setcar (nthcdr j (nth i lu)) sum) | |
260 (let ((dum (math-abs-approx sum))) | |
261 (if (Math-lessp big dum) | |
262 (setq big dum | |
263 imax i))) | |
264 (setq i (1+ i))) | |
265 (if (> imax j) | |
266 (setq lu (math-swap-rows lu j imax) | |
267 d (- d))) | |
268 (setq index (cons imax index)) | |
269 (let ((pivot (nth j (nth j lu)))) | |
270 (if (math-zerop pivot) | |
271 (throw 'singular nil) | |
272 (setq i j) | |
273 (while (<= (setq i (1+ i)) n) | |
274 (setcar (nthcdr j (nth i lu)) | |
275 (math-div (nth j (nth i lu)) pivot))))) | |
276 (setq j (1+ j))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
277 (list lu (nreverse index) d))) |
40785 | 278 |
279 (defun math-swap-rows (m r1 r2) | |
280 (or (= r1 r2) | |
281 (let* ((r1prev (nthcdr (1- r1) m)) | |
282 (row1 (cdr r1prev)) | |
283 (r2prev (nthcdr (1- r2) m)) | |
284 (row2 (cdr r2prev)) | |
285 (r2next (cdr row2))) | |
286 (setcdr r2prev row1) | |
287 (setcdr r1prev row2) | |
288 (setcdr row2 (cdr row1)) | |
289 (setcdr row1 r2next))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
290 m) |
40785 | 291 |
292 | |
293 (defun math-lud-solve (lud b &optional need) | |
294 (if lud | |
295 (let* ((x (math-copy-matrix b)) | |
296 (n (1- (length x))) | |
297 (m (1- (length (nth 1 x)))) | |
298 (lu (car lud)) | |
299 (col 1) | |
300 i j ip ii index sum) | |
301 (while (<= col m) | |
302 (math-working "LUD solver step" col) | |
303 (setq i 1 | |
304 ii nil | |
305 index (nth 1 lud)) | |
306 (while (<= i n) | |
307 (setq ip (car index) | |
308 index (cdr index) | |
309 sum (nth col (nth ip x))) | |
310 (setcar (nthcdr col (nth ip x)) (nth col (nth i x))) | |
311 (if (null ii) | |
312 (or (math-zerop sum) | |
313 (setq ii i)) | |
314 (setq j ii) | |
315 (while (< j i) | |
316 (setq sum (math-sub sum (math-mul (nth j (nth i lu)) | |
317 (nth col (nth j x)))) | |
318 j (1+ j)))) | |
319 (setcar (nthcdr col (nth i x)) sum) | |
320 (setq i (1+ i))) | |
321 (while (>= (setq i (1- i)) 1) | |
322 (setq sum (nth col (nth i x)) | |
323 j i) | |
324 (while (<= (setq j (1+ j)) n) | |
325 (setq sum (math-sub sum (math-mul (nth j (nth i lu)) | |
326 (nth col (nth j x)))))) | |
327 (setcar (nthcdr col (nth i x)) | |
328 (math-div sum (nth i (nth i lu))))) | |
329 (setq col (1+ col))) | |
330 x) | |
331 (and need | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
332 (math-reject-arg need "*Singular matrix")))) |
40785 | 333 |
334 (defun calcFunc-lud (m) | |
335 (if (math-square-matrixp m) | |
336 (or (math-with-extra-prec 2 | |
337 (let ((lud (math-matrix-lud m))) | |
338 (and lud | |
339 (let* ((lmat (math-copy-matrix (car lud))) | |
340 (umat (math-copy-matrix (car lud))) | |
341 (n (1- (length (car lud)))) | |
342 (perm (calcFunc-idn 1 n)) | |
343 i (j 1)) | |
344 (while (<= j n) | |
345 (setq i 1) | |
346 (while (< i j) | |
347 (setcar (nthcdr j (nth i lmat)) 0) | |
348 (setq i (1+ i))) | |
349 (setcar (nthcdr j (nth j lmat)) 1) | |
350 (while (<= (setq i (1+ i)) n) | |
351 (setcar (nthcdr j (nth i umat)) 0)) | |
352 (setq j (1+ j))) | |
353 (while (>= (setq j (1- j)) 1) | |
354 (let ((pos (nth (1- j) (nth 1 lud)))) | |
355 (or (= pos j) | |
356 (setq perm (math-swap-rows perm j pos))))) | |
357 (list 'vec perm lmat umat))))) | |
358 (math-reject-arg m "*Singular matrix")) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
359 (math-reject-arg m 'square-matrixp))) |
40785 | 360 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
361 ;;; calc-mtx.el ends here |