Mercurial > emacs
annotate lisp/calc/calc-vec.el @ 41220:0a6ed29cea73
Comment change.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sat, 17 Nov 2001 22:41:26 +0000 |
parents | 73f364fd8aaa |
children | fcd507927105 |
rev | line source |
---|---|
40785 | 1 ;; Calculator for GNU Emacs, part II [calc-vec.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-vec () nil) | |
30 | |
31 | |
32 (defun calc-display-strings (n) | |
33 (interactive "P") | |
34 (calc-wrapper | |
35 (message (if (calc-change-mode 'calc-display-strings n t t) | |
36 "Displaying vectors of integers as quoted strings." | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
37 "Displaying vectors of integers normally.")))) |
40785 | 38 |
39 | |
40 (defun calc-pack (n) | |
41 (interactive "P") | |
42 (calc-wrapper | |
43 (let* ((nn (if n 1 2)) | |
44 (mode (if n (prefix-numeric-value n) (calc-top-n 1))) | |
45 (mode (if (and (Math-vectorp mode) (cdr mode)) (cdr mode) | |
46 (if (integerp mode) mode | |
47 (error "Packing mode must be an integer or vector of integers")))) | |
48 (num (calc-pack-size mode)) | |
49 (items (calc-top-list num nn))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
50 (calc-enter-result (+ nn num -1) "pack" (calc-pack-items mode items))))) |
40785 | 51 |
52 (defun calc-pack-size (mode) | |
53 (cond ((consp mode) | |
54 (let ((size 1)) | |
55 (while mode | |
56 (or (integerp (car mode)) (error "Vector of integers expected")) | |
57 (setq size (* size (calc-pack-size (car mode))) | |
58 mode (cdr mode))) | |
59 (if (= size 0) | |
60 (error "Zero dimensions not allowed") | |
61 size))) | |
62 ((>= mode 0) mode) | |
63 (t (or (cdr (assq mode '((-3 . 3) (-13 . 1) (-14 . 3) (-15 . 6)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
64 2)))) |
40785 | 65 |
66 (defun calc-pack-items (mode items) | |
67 (cond ((consp mode) | |
68 (if (cdr mode) | |
69 (let* ((size (calc-pack-size (cdr mode))) | |
70 (len (length items)) | |
71 (new nil) | |
72 p row) | |
73 (while (> len 0) | |
74 (setq p (nthcdr (1- size) items) | |
75 row items | |
76 items (cdr p) | |
77 len (- len size)) | |
78 (setcdr p nil) | |
79 (setq new (cons (calc-pack-items (cdr mode) row) new))) | |
80 (calc-pack-items (car mode) (nreverse new))) | |
81 (calc-pack-items (car mode) items))) | |
82 ((>= mode 0) | |
83 (cons 'vec items)) | |
84 ((= mode -3) | |
85 (if (and (math-objvecp (car items)) | |
86 (math-objvecp (nth 1 items)) | |
87 (math-objvecp (nth 2 items))) | |
88 (if (and (math-num-integerp (car items)) | |
89 (math-num-integerp (nth 1 items))) | |
90 (if (math-realp (nth 2 items)) | |
91 (cons 'hms items) | |
92 (error "Seconds must be real")) | |
93 (error "Hours and minutes must be integers")) | |
94 (math-normalize (list '+ | |
95 (list '+ | |
96 (if (eq calc-angle-mode 'rad) | |
97 (list '* (car items) | |
98 '(hms 1 0 0)) | |
99 (car items)) | |
100 (list '* (nth 1 items) '(hms 0 1 0))) | |
101 (list '* (nth 2 items) '(hms 0 0 1)))))) | |
102 ((= mode -13) | |
103 (if (math-realp (car items)) | |
104 (cons 'date items) | |
105 (if (eq (car-safe (car items)) 'date) | |
106 (car items) | |
107 (if (math-objvecp (car items)) | |
108 (error "Date value must be real") | |
109 (cons 'calcFunc-date items))))) | |
110 ((memq mode '(-14 -15)) | |
111 (let ((p items)) | |
112 (while (and p (math-objvecp (car p))) | |
113 (or (math-integerp (car p)) | |
114 (error "Components must be integers")) | |
115 (setq p (cdr p))) | |
116 (if p | |
117 (cons 'calcFunc-date items) | |
118 (list 'date (math-dt-to-date items))))) | |
119 ((or (eq (car-safe (car items)) 'vec) | |
120 (eq (car-safe (nth 1 items)) 'vec)) | |
121 (let* ((x (car items)) | |
122 (vx (eq (car-safe x) 'vec)) | |
123 (y (nth 1 items)) | |
124 (vy (eq (car-safe y) 'vec)) | |
125 (z nil) | |
126 (n (1- (length (if vx x y))))) | |
127 (and vx vy | |
128 (/= n (1- (length y))) | |
129 (error "Vectors must be the same length")) | |
130 (while (>= (setq n (1- n)) 0) | |
131 (setq z (cons (calc-pack-items | |
132 mode | |
133 (list (if vx (car (setq x (cdr x))) x) | |
134 (if vy (car (setq y (cdr y))) y))) | |
135 z))) | |
136 (cons 'vec (nreverse z)))) | |
137 ((= mode -1) | |
138 (if (and (math-realp (car items)) (math-realp (nth 1 items))) | |
139 (cons 'cplx items) | |
140 (if (and (math-objectp (car items)) (math-objectp (nth 1 items))) | |
141 (error "Components must be real")) | |
142 (math-normalize (list '+ (car items) | |
143 (list '* (nth 1 items) '(cplx 0 1)))))) | |
144 ((= mode -2) | |
145 (if (and (math-realp (car items)) (math-anglep (nth 1 items))) | |
146 (cons 'polar items) | |
147 (if (and (math-objectp (car items)) (math-objectp (nth 1 items))) | |
148 (error "Components must be real")) | |
149 (math-normalize (list '* (car items) | |
150 (if (math-anglep (nth 1 items)) | |
151 (list 'polar 1 (nth 1 items)) | |
152 (list 'calcFunc-exp | |
153 (list '* | |
154 (math-to-radians-2 | |
155 (nth 1 items)) | |
156 (list 'polar | |
157 1 | |
158 (math-quarter-circle | |
159 nil))))))))) | |
160 ((= mode -4) | |
161 (let ((x (car items)) | |
162 (sigma (nth 1 items))) | |
163 (if (or (math-scalarp x) (not (math-objvecp x))) | |
164 (if (or (math-anglep sigma) (not (math-objvecp sigma))) | |
165 (math-make-sdev x sigma) | |
166 (error "Error component must be real")) | |
167 (error "Mean component must be real or complex")))) | |
168 ((= mode -5) | |
169 (let ((a (car items)) | |
170 (m (nth 1 items))) | |
171 (if (and (math-anglep a) (math-anglep m)) | |
172 (if (math-posp m) | |
173 (math-make-mod a m) | |
174 (error "Modulus must be positive")) | |
175 (if (and (math-objectp a) (math-objectp m)) | |
176 (error "Components must be real")) | |
177 (list 'calcFunc-makemod a m)))) | |
178 ((memq mode '(-6 -7 -8 -9)) | |
179 (let ((lo (car items)) | |
180 (hi (nth 1 items))) | |
181 (if (and (or (math-anglep lo) (eq (car lo) 'date) | |
182 (not (math-objvecp lo))) | |
183 (or (math-anglep hi) (eq (car hi) 'date) | |
184 (not (math-objvecp hi)))) | |
185 (math-make-intv (+ mode 9) lo hi) | |
186 (error "Components must be real")))) | |
187 ((eq mode -10) | |
188 (if (math-zerop (nth 1 items)) | |
189 (error "Denominator must not be zero") | |
190 (if (and (math-integerp (car items)) (math-integerp (nth 1 items))) | |
191 (math-normalize (cons 'frac items)) | |
192 (if (and (math-objectp (car items)) (math-objectp (nth 1 items))) | |
193 (error "Components must be integers")) | |
194 (cons 'calcFunc-fdiv items)))) | |
195 ((memq mode '(-11 -12)) | |
196 (if (and (math-realp (car items)) (math-integerp (nth 1 items))) | |
197 (calcFunc-scf (math-float (car items)) (nth 1 items)) | |
198 (if (and (math-objectp (car items)) (math-objectp (nth 1 items))) | |
199 (error "Components must be integers")) | |
200 (math-normalize | |
201 (list 'calcFunc-scf | |
202 (list 'calcFunc-float (car items)) | |
203 (nth 1 items))))) | |
204 (t | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
205 (error "Invalid packing mode: %d" mode)))) |
40785 | 206 |
207 (defun calc-unpack (mode) | |
208 (interactive "P") | |
209 (calc-wrapper | |
210 (let ((calc-unpack-with-type t)) | |
211 (calc-pop-push-record-list 1 "unpk" (calc-unpack-item | |
212 (and mode | |
213 (prefix-numeric-value mode)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
214 (calc-top)))))) |
40785 | 215 |
216 (defun calc-unpack-type (item) | |
217 (cond ((eq (car-safe item) 'vec) | |
218 (1- (length item))) | |
219 ((eq (car-safe item) 'intv) | |
220 (- (nth 1 item) 9)) | |
221 (t | |
222 (or (cdr (assq (car-safe item) '( (cplx . -1) (polar . -2) | |
223 (hms . -3) (sdev . -4) (mod . -5) | |
224 (frac . -10) (float . -11) | |
225 (date . -13) ))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
226 (error "Argument must be a composite object"))))) |
40785 | 227 |
228 (defun calc-unpack-item (mode item) | |
229 (cond ((not mode) | |
230 (if (or (and (not (memq (car-safe item) '(frac float cplx polar vec | |
231 hms date sdev mod | |
232 intv))) | |
233 (math-objvecp item)) | |
234 (eq (car-safe item) 'var)) | |
235 (error "Argument must be a composite object or function call")) | |
236 (if (eq (car item) 'intv) | |
237 (cdr (cdr item)) | |
238 (cdr item))) | |
239 ((> mode 0) | |
240 (let ((dims nil) | |
241 type new row) | |
242 (setq item (list item)) | |
243 (while (> mode 0) | |
244 (setq type (calc-unpack-type (car item)) | |
245 dims (cons type dims) | |
246 new (calc-unpack-item nil (car item))) | |
247 (while (setq item (cdr item)) | |
248 (or (= (calc-unpack-type (car item)) type) | |
249 (error "Inconsistent types or dimensions in vector elements")) | |
250 (setq new (append new (calc-unpack-item nil (car item))))) | |
251 (setq item new | |
252 mode (1- mode))) | |
253 (if (cdr dims) (setq dims (list (cons 'vec (nreverse dims))))) | |
254 (cond ((eq calc-unpack-with-type 'pair) | |
255 (list (car dims) (cons 'vec item))) | |
256 (calc-unpack-with-type | |
257 (append item dims)) | |
258 (t item)))) | |
259 ((eq calc-unpack-with-type 'pair) | |
260 (let ((calc-unpack-with-type nil)) | |
261 (list mode (cons 'vec (calc-unpack-item mode item))))) | |
262 ((= mode -3) | |
263 (if (eq (car-safe item) 'hms) | |
264 (cdr item) | |
265 (error "Argument must be an HMS form"))) | |
266 ((= mode -13) | |
267 (if (eq (car-safe item) 'date) | |
268 (cdr item) | |
269 (error "Argument must be a date form"))) | |
270 ((= mode -14) | |
271 (if (eq (car-safe item) 'date) | |
272 (math-date-to-dt (math-floor (nth 1 item))) | |
273 (error "Argument must be a date form"))) | |
274 ((= mode -15) | |
275 (if (eq (car-safe item) 'date) | |
276 (append (math-date-to-dt (nth 1 item)) | |
277 (and (not (math-integerp (nth 1 item))) | |
278 (list 0 0 0))) | |
279 (error "Argument must be a date form"))) | |
280 ((eq (car-safe item) 'vec) | |
281 (let ((x nil) | |
282 (y nil) | |
283 res) | |
284 (while (setq item (cdr item)) | |
285 (setq res (calc-unpack-item mode (car item)) | |
286 x (cons (car res) x) | |
287 y (cons (nth 1 res) y))) | |
288 (list (cons 'vec (nreverse x)) | |
289 (cons 'vec (nreverse y))))) | |
290 ((= mode -1) | |
291 (if (eq (car-safe item) 'cplx) | |
292 (cdr item) | |
293 (if (eq (car-safe item) 'polar) | |
294 (cdr (math-complex item)) | |
295 (if (Math-realp item) | |
296 (list item 0) | |
297 (error "Argument must be a complex number"))))) | |
298 ((= mode -2) | |
299 (if (or (memq (car-safe item) '(cplx polar)) | |
300 (Math-realp item)) | |
301 (cdr (math-polar item)) | |
302 (error "Argument must be a complex number"))) | |
303 ((= mode -4) | |
304 (if (eq (car-safe item) 'sdev) | |
305 (cdr item) | |
306 (list item 0))) | |
307 ((= mode -5) | |
308 (if (eq (car-safe item) 'mod) | |
309 (cdr item) | |
310 (error "Argument must be a modulo form"))) | |
311 ((memq mode '(-6 -7 -8 -9)) | |
312 (if (eq (car-safe item) 'intv) | |
313 (cdr (cdr item)) | |
314 (list item item))) | |
315 ((= mode -10) | |
316 (if (eq (car-safe item) 'frac) | |
317 (cdr item) | |
318 (if (Math-integerp item) | |
319 (list item 1) | |
320 (error "Argument must be a rational number")))) | |
321 ((= mode -11) | |
322 (if (eq (car-safe item) 'float) | |
323 (list (nth 1 item) (math-normalize (nth 2 item))) | |
324 (error "Expected a floating-point number"))) | |
325 ((= mode -12) | |
326 (if (eq (car-safe item) 'float) | |
327 (list (calcFunc-mant item) (calcFunc-xpon item)) | |
328 (error "Expected a floating-point number"))) | |
329 (t | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
330 (error "Invalid unpacking mode: %d" mode)))) |
40785 | 331 (setq calc-unpack-with-type nil) |
332 | |
333 (defun calc-diag (n) | |
334 (interactive "P") | |
335 (calc-wrapper | |
336 (calc-enter-result 1 "diag" (if n | |
337 (list 'calcFunc-diag (calc-top-n 1) | |
338 (prefix-numeric-value n)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
339 (list 'calcFunc-diag (calc-top-n 1)))))) |
40785 | 340 |
341 (defun calc-ident (n) | |
342 (interactive "NDimension of identity matrix = ") | |
343 (calc-wrapper | |
344 (calc-enter-result 0 "idn" (if (eq n 0) | |
345 '(calcFunc-idn 1) | |
346 (list 'calcFunc-idn 1 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
347 (prefix-numeric-value n)))))) |
40785 | 348 |
349 (defun calc-index (n &optional stack) | |
350 (interactive "NSize of vector = \nP") | |
351 (calc-wrapper | |
352 (if (consp stack) | |
353 (calc-enter-result 3 "indx" (cons 'calcFunc-index (calc-top-list-n 3))) | |
354 (calc-enter-result 0 "indx" (list 'calcFunc-index | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
355 (prefix-numeric-value n)))))) |
40785 | 356 |
357 (defun calc-build-vector (n) | |
358 (interactive "NSize of vector = ") | |
359 (calc-wrapper | |
360 (calc-enter-result 1 "bldv" (list 'calcFunc-cvec | |
361 (calc-top-n 1) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
362 (prefix-numeric-value n))))) |
40785 | 363 |
364 (defun calc-cons (arg) | |
365 (interactive "P") | |
366 (calc-wrapper | |
367 (if (calc-is-hyperbolic) | |
368 (calc-binary-op "rcns" 'calcFunc-rcons arg) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
369 (calc-binary-op "cons" 'calcFunc-cons arg)))) |
40785 | 370 |
371 | |
372 (defun calc-head (arg) | |
373 (interactive "P") | |
374 (calc-wrapper | |
375 (if (calc-is-inverse) | |
376 (if (calc-is-hyperbolic) | |
377 (calc-unary-op "rtai" 'calcFunc-rtail arg) | |
378 (calc-unary-op "tail" 'calcFunc-tail arg)) | |
379 (if (calc-is-hyperbolic) | |
380 (calc-unary-op "rhed" 'calcFunc-rhead arg) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
381 (calc-unary-op "head" 'calcFunc-head arg))))) |
40785 | 382 |
383 (defun calc-tail (arg) | |
384 (interactive "P") | |
385 (calc-invert-func) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
386 (calc-head arg)) |
40785 | 387 |
388 (defun calc-vlength (arg) | |
389 (interactive "P") | |
390 (calc-wrapper | |
391 (if (calc-is-hyperbolic) | |
392 (calc-unary-op "dims" 'calcFunc-mdims arg) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
393 (calc-unary-op "len" 'calcFunc-vlen arg)))) |
40785 | 394 |
395 (defun calc-arrange-vector (n) | |
396 (interactive "NNumber of columns = ") | |
397 (calc-wrapper | |
398 (calc-enter-result 1 "arng" (list 'calcFunc-arrange (calc-top-n 1) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
399 (prefix-numeric-value n))))) |
40785 | 400 |
401 (defun calc-vector-find (arg) | |
402 (interactive "P") | |
403 (calc-wrapper | |
404 (let ((func (cons 'calcFunc-find (calc-top-list-n 2)))) | |
405 (calc-enter-result | |
406 2 "find" | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
407 (if arg (append func (list (prefix-numeric-value arg))) func))))) |
40785 | 408 |
409 (defun calc-subvector () | |
410 (interactive) | |
411 (calc-wrapper | |
412 (if (calc-is-inverse) | |
413 (calc-enter-result 3 "rsvc" (cons 'calcFunc-rsubvec | |
414 (calc-top-list-n 3))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
415 (calc-enter-result 3 "svec" (cons 'calcFunc-subvec (calc-top-list-n 3)))))) |
40785 | 416 |
417 (defun calc-reverse-vector (arg) | |
418 (interactive "P") | |
419 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
420 (calc-unary-op "rev" 'calcFunc-rev arg))) |
40785 | 421 |
422 (defun calc-mask-vector (arg) | |
423 (interactive "P") | |
424 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
425 (calc-binary-op "vmsk" 'calcFunc-vmask arg))) |
40785 | 426 |
427 (defun calc-expand-vector (arg) | |
428 (interactive "P") | |
429 (calc-wrapper | |
430 (if (calc-is-hyperbolic) | |
431 (calc-enter-result 3 "vexp" (cons 'calcFunc-vexp (calc-top-list-n 3))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
432 (calc-binary-op "vexp" 'calcFunc-vexp arg)))) |
40785 | 433 |
434 (defun calc-sort () | |
435 (interactive) | |
436 (calc-slow-wrapper | |
437 (if (calc-is-inverse) | |
438 (calc-enter-result 1 "rsrt" (list 'calcFunc-rsort (calc-top-n 1))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
439 (calc-enter-result 1 "sort" (list 'calcFunc-sort (calc-top-n 1)))))) |
40785 | 440 |
441 (defun calc-grade () | |
442 (interactive) | |
443 (calc-slow-wrapper | |
444 (if (calc-is-inverse) | |
445 (calc-enter-result 1 "rgrd" (list 'calcFunc-rgrade (calc-top-n 1))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
446 (calc-enter-result 1 "grad" (list 'calcFunc-grade (calc-top-n 1)))))) |
40785 | 447 |
448 (defun calc-histogram (n) | |
449 (interactive "NNumber of bins: ") | |
450 (calc-slow-wrapper | |
451 (if calc-hyperbolic-flag | |
452 (calc-enter-result 2 "hist" (list 'calcFunc-histogram | |
453 (calc-top-n 2) | |
454 (calc-top-n 1) | |
455 (prefix-numeric-value n))) | |
456 (calc-enter-result 1 "hist" (list 'calcFunc-histogram | |
457 (calc-top-n 1) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
458 (prefix-numeric-value n)))))) |
40785 | 459 |
460 (defun calc-transpose (arg) | |
461 (interactive "P") | |
462 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
463 (calc-unary-op "trn" 'calcFunc-trn arg))) |
40785 | 464 |
465 (defun calc-conj-transpose (arg) | |
466 (interactive "P") | |
467 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
468 (calc-unary-op "ctrn" 'calcFunc-ctrn arg))) |
40785 | 469 |
470 (defun calc-cross (arg) | |
471 (interactive "P") | |
472 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
473 (calc-binary-op "cros" 'calcFunc-cross arg))) |
40785 | 474 |
475 (defun calc-remove-duplicates (arg) | |
476 (interactive "P") | |
477 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
478 (calc-unary-op "rdup" 'calcFunc-rdup arg))) |
40785 | 479 |
480 (defun calc-set-union (arg) | |
481 (interactive "P") | |
482 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
483 (calc-binary-op "unio" 'calcFunc-vunion arg '(vec) 'calcFunc-rdup))) |
40785 | 484 |
485 (defun calc-set-intersect (arg) | |
486 (interactive "P") | |
487 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
488 (calc-binary-op "intr" 'calcFunc-vint arg '(vec) 'calcFunc-rdup))) |
40785 | 489 |
490 (defun calc-set-difference (arg) | |
491 (interactive "P") | |
492 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
493 (calc-binary-op "diff" 'calcFunc-vdiff arg '(vec) 'calcFunc-rdup))) |
40785 | 494 |
495 (defun calc-set-xor (arg) | |
496 (interactive "P") | |
497 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
498 (calc-binary-op "xor" 'calcFunc-vxor arg '(vec) 'calcFunc-rdup))) |
40785 | 499 |
500 (defun calc-set-complement (arg) | |
501 (interactive "P") | |
502 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
503 (calc-unary-op "cmpl" 'calcFunc-vcompl arg))) |
40785 | 504 |
505 (defun calc-set-floor (arg) | |
506 (interactive "P") | |
507 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
508 (calc-unary-op "vflr" 'calcFunc-vfloor arg))) |
40785 | 509 |
510 (defun calc-set-enumerate (arg) | |
511 (interactive "P") | |
512 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
513 (calc-unary-op "enum" 'calcFunc-venum arg))) |
40785 | 514 |
515 (defun calc-set-span (arg) | |
516 (interactive "P") | |
517 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
518 (calc-unary-op "span" 'calcFunc-vspan arg))) |
40785 | 519 |
520 (defun calc-set-cardinality (arg) | |
521 (interactive "P") | |
522 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
523 (calc-unary-op "card" 'calcFunc-vcard arg))) |
40785 | 524 |
525 (defun calc-unpack-bits (arg) | |
526 (interactive "P") | |
527 (calc-wrapper | |
528 (if (calc-is-inverse) | |
529 (calc-unary-op "bpck" 'calcFunc-vpack arg) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
530 (calc-unary-op "bupk" 'calcFunc-vunpack arg)))) |
40785 | 531 |
532 (defun calc-pack-bits (arg) | |
533 (interactive "P") | |
534 (calc-invert-func) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
535 (calc-unpack-bits arg)) |
40785 | 536 |
537 | |
538 (defun calc-rnorm (arg) | |
539 (interactive "P") | |
540 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
541 (calc-unary-op "rnrm" 'calcFunc-rnorm arg))) |
40785 | 542 |
543 (defun calc-cnorm (arg) | |
544 (interactive "P") | |
545 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
546 (calc-unary-op "cnrm" 'calcFunc-cnorm arg))) |
40785 | 547 |
548 (defun calc-mrow (n &optional nn) | |
549 (interactive "NRow number: \nP") | |
550 (calc-wrapper | |
551 (if (consp nn) | |
552 (calc-enter-result 2 "mrow" (cons 'calcFunc-mrow (calc-top-list-n 2))) | |
553 (setq n (prefix-numeric-value n)) | |
554 (if (= n 0) | |
555 (calc-enter-result 1 "getd" (list 'calcFunc-getdiag (calc-top-n 1))) | |
556 (if (< n 0) | |
557 (calc-enter-result 1 "rrow" (list 'calcFunc-mrrow | |
558 (calc-top-n 1) (- n))) | |
559 (calc-enter-result 1 "mrow" (list 'calcFunc-mrow | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
560 (calc-top-n 1) n))))))) |
40785 | 561 |
562 (defun calc-mcol (n &optional nn) | |
563 (interactive "NColumn number: \nP") | |
564 (calc-wrapper | |
565 (if (consp nn) | |
566 (calc-enter-result 2 "mcol" (cons 'calcFunc-mcol (calc-top-list-n 2))) | |
567 (setq n (prefix-numeric-value n)) | |
568 (if (= n 0) | |
569 (calc-enter-result 1 "getd" (list 'calcFunc-getdiag (calc-top-n 1))) | |
570 (if (< n 0) | |
571 (calc-enter-result 1 "rcol" (list 'calcFunc-mrcol | |
572 (calc-top-n 1) (- n))) | |
573 (calc-enter-result 1 "mcol" (list 'calcFunc-mcol | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
574 (calc-top-n 1) n))))))) |
40785 | 575 |
576 | |
577 ;;;; Vectors. | |
578 | |
579 (defun calcFunc-mdims (m) | |
580 (or (math-vectorp m) | |
581 (math-reject-arg m 'vectorp)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
582 (cons 'vec (math-mat-dimens m))) |
40785 | 583 |
584 | |
585 ;;; Apply a function elementwise to vector A. [V X V; N X N] [Public] | |
586 (defun math-map-vec (f a) | |
587 (if (math-vectorp a) | |
588 (cons 'vec (mapcar f (cdr a))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
589 (funcall f a))) |
40785 | 590 |
591 (defun math-dimension-error () | |
592 (calc-record-why "*Dimension error") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
593 (signal 'wrong-type-argument nil)) |
40785 | 594 |
595 | |
596 ;;; Build a vector out of a list of objects. [Public] | |
597 (defun calcFunc-vec (&rest objs) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
598 (cons 'vec objs)) |
40785 | 599 |
600 | |
601 ;;; Build a constant vector or matrix. [Public] | |
602 (defun calcFunc-cvec (obj &rest dims) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
603 (math-make-vec-dimen obj dims)) |
40785 | 604 |
605 (defun math-make-vec-dimen (obj dims) | |
606 (if dims | |
607 (if (natnump (car dims)) | |
608 (if (or (cdr dims) | |
609 (not (math-numberp obj))) | |
610 (cons 'vec (copy-sequence | |
611 (make-list (car dims) | |
612 (math-make-vec-dimen obj (cdr dims))))) | |
613 (cons 'vec (make-list (car dims) obj))) | |
614 (math-reject-arg (car dims) 'fixnatnump)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
615 obj)) |
40785 | 616 |
617 (defun calcFunc-head (vec) | |
618 (if (and (Math-vectorp vec) | |
619 (cdr vec)) | |
620 (nth 1 vec) | |
621 (calc-record-why 'vectorp vec) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
622 (list 'calcFunc-head vec))) |
40785 | 623 |
624 (defun calcFunc-tail (vec) | |
625 (if (and (Math-vectorp vec) | |
626 (cdr vec)) | |
627 (cons 'vec (cdr (cdr vec))) | |
628 (calc-record-why 'vectorp vec) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
629 (list 'calcFunc-tail vec))) |
40785 | 630 |
631 (defun calcFunc-cons (head tail) | |
632 (if (Math-vectorp tail) | |
633 (cons 'vec (cons head (cdr tail))) | |
634 (calc-record-why 'vectorp tail) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
635 (list 'calcFunc-cons head tail))) |
40785 | 636 |
637 (defun calcFunc-rhead (vec) | |
638 (if (and (Math-vectorp vec) | |
639 (cdr vec)) | |
640 (let ((vec (copy-sequence vec))) | |
641 (setcdr (nthcdr (- (length vec) 2) vec) nil) | |
642 vec) | |
643 (calc-record-why 'vectorp vec) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
644 (list 'calcFunc-rhead vec))) |
40785 | 645 |
646 (defun calcFunc-rtail (vec) | |
647 (if (and (Math-vectorp vec) | |
648 (cdr vec)) | |
649 (nth (1- (length vec)) vec) | |
650 (calc-record-why 'vectorp vec) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
651 (list 'calcFunc-rtail vec))) |
40785 | 652 |
653 (defun calcFunc-rcons (head tail) | |
654 (if (Math-vectorp head) | |
655 (append head (list tail)) | |
656 (calc-record-why 'vectorp head) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
657 (list 'calcFunc-rcons head tail))) |
40785 | 658 |
659 | |
660 | |
661 ;;; Apply a function elementwise to vectors A and B. [O X O O] [Public] | |
662 (defun math-map-vec-2 (f a b) | |
663 (if (math-vectorp a) | |
664 (if (math-vectorp b) | |
665 (let ((v nil)) | |
666 (while (setq a (cdr a)) | |
667 (or (setq b (cdr b)) | |
668 (math-dimension-error)) | |
669 (setq v (cons (funcall f (car a) (car b)) v))) | |
670 (if a (math-dimension-error)) | |
671 (cons 'vec (nreverse v))) | |
672 (let ((v nil)) | |
673 (while (setq a (cdr a)) | |
674 (setq v (cons (funcall f (car a) b) v))) | |
675 (cons 'vec (nreverse v)))) | |
676 (if (math-vectorp b) | |
677 (let ((v nil)) | |
678 (while (setq b (cdr b)) | |
679 (setq v (cons (funcall f a (car b)) v))) | |
680 (cons 'vec (nreverse v))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
681 (funcall f a b)))) |
40785 | 682 |
683 | |
684 | |
685 ;;; "Reduce" a function over a vector (left-associatively). [O X V] [Public] | |
686 (defun math-reduce-vec (f a) | |
687 (if (math-vectorp a) | |
688 (if (cdr a) | |
689 (let ((accum (car (setq a (cdr a))))) | |
690 (while (setq a (cdr a)) | |
691 (setq accum (funcall f accum (car a)))) | |
692 accum) | |
693 0) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
694 a)) |
40785 | 695 |
696 ;;; Reduce a function over the columns of matrix A. [V X V] [Public] | |
697 (defun math-reduce-cols (f a) | |
698 (if (math-matrixp a) | |
699 (cons 'vec (math-reduce-cols-col-step f (cdr a) 1 (length (nth 1 a)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
700 a)) |
40785 | 701 |
702 (defun math-reduce-cols-col-step (f a col cols) | |
703 (and (< col cols) | |
704 (cons (math-reduce-cols-row-step f (nth col (car a)) col (cdr a)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
705 (math-reduce-cols-col-step f a (1+ col) cols)))) |
40785 | 706 |
707 (defun math-reduce-cols-row-step (f tot col a) | |
708 (if a | |
709 (math-reduce-cols-row-step f | |
710 (funcall f tot (nth col (car a))) | |
711 col | |
712 (cdr a)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
713 tot)) |
40785 | 714 |
715 | |
716 | |
717 (defun math-dot-product (a b) | |
718 (if (setq a (cdr a) b (cdr b)) | |
719 (let ((accum (math-mul (car a) (car b)))) | |
720 (while (setq a (cdr a) b (cdr b)) | |
721 (setq accum (math-add accum (math-mul (car a) (car b))))) | |
722 accum) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
723 0)) |
40785 | 724 |
725 | |
726 ;;; Return the number of elements in vector V. [Public] | |
727 (defun calcFunc-vlen (v) | |
728 (if (math-vectorp v) | |
729 (1- (length v)) | |
730 (if (math-objectp v) | |
731 0 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
732 (list 'calcFunc-vlen v)))) |
40785 | 733 |
734 ;;; Get the Nth row of a matrix. | |
735 (defun calcFunc-mrow (mat n) ; [Public] | |
736 (if (Math-vectorp n) | |
737 (math-map-vec (function (lambda (x) (calcFunc-mrow mat x))) n) | |
738 (if (and (eq (car-safe n) 'intv) (math-constp n)) | |
739 (calcFunc-subvec mat | |
740 (math-add (nth 2 n) (if (memq (nth 1 n) '(2 3)) 0 1)) | |
741 (math-add (nth 3 n) (if (memq (nth 1 n) '(1 3)) 1 0))) | |
742 (or (and (integerp (setq n (math-check-integer n))) | |
743 (> n 0)) | |
744 (math-reject-arg n 'fixposintp)) | |
745 (or (Math-vectorp mat) | |
746 (math-reject-arg mat 'vectorp)) | |
747 (or (nth n mat) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
748 (math-reject-arg n "*Index out of range"))))) |
40785 | 749 |
750 (defun calcFunc-subscr (mat n &optional m) | |
751 (setq mat (calcFunc-mrow mat n)) | |
752 (if m | |
753 (if (math-num-integerp n) | |
754 (calcFunc-mrow mat m) | |
755 (calcFunc-mcol mat m)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
756 mat)) |
40785 | 757 |
758 ;;; Get the Nth column of a matrix. | |
759 (defun math-mat-col (mat n) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
760 (cons 'vec (mapcar (function (lambda (x) (elt x n))) (cdr mat)))) |
40785 | 761 |
762 (defun calcFunc-mcol (mat n) ; [Public] | |
763 (if (Math-vectorp n) | |
764 (calcFunc-trn | |
765 (math-map-vec (function (lambda (x) (calcFunc-mcol mat x))) n)) | |
766 (if (and (eq (car-safe n) 'intv) (math-constp n)) | |
767 (if (math-matrixp mat) | |
768 (math-map-vec (function (lambda (x) (calcFunc-mrow x n))) mat) | |
769 (calcFunc-mrow mat n)) | |
770 (or (and (integerp (setq n (math-check-integer n))) | |
771 (> n 0)) | |
772 (math-reject-arg n 'fixposintp)) | |
773 (or (Math-vectorp mat) | |
774 (math-reject-arg mat 'vectorp)) | |
775 (or (if (math-matrixp mat) | |
776 (and (< n (length (nth 1 mat))) | |
777 (math-mat-col mat n)) | |
778 (nth n mat)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
779 (math-reject-arg n "*Index out of range"))))) |
40785 | 780 |
781 ;;; Remove the Nth row from a matrix. | |
782 (defun math-mat-less-row (mat n) | |
783 (if (<= n 0) | |
784 (cdr mat) | |
785 (cons (car mat) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
786 (math-mat-less-row (cdr mat) (1- n))))) |
40785 | 787 |
788 (defun calcFunc-mrrow (mat n) ; [Public] | |
789 (and (integerp (setq n (math-check-integer n))) | |
790 (> n 0) | |
791 (< n (length mat)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
792 (math-mat-less-row mat n))) |
40785 | 793 |
794 ;;; Remove the Nth column from a matrix. | |
795 (defun math-mat-less-col (mat n) | |
796 (cons 'vec (mapcar (function (lambda (x) (math-mat-less-row x n))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
797 (cdr mat)))) |
40785 | 798 |
799 (defun calcFunc-mrcol (mat n) ; [Public] | |
800 (and (integerp (setq n (math-check-integer n))) | |
801 (> n 0) | |
802 (if (math-matrixp mat) | |
803 (and (< n (length (nth 1 mat))) | |
804 (math-mat-less-col mat n)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
805 (math-mat-less-row mat n)))) |
40785 | 806 |
807 (defun calcFunc-getdiag (mat) ; [Public] | |
808 (if (math-square-matrixp mat) | |
809 (cons 'vec (math-get-diag-step (cdr mat) 1)) | |
810 (calc-record-why 'square-matrixp mat) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
811 (list 'calcFunc-getdiag mat))) |
40785 | 812 |
813 (defun math-get-diag-step (row n) | |
814 (and row | |
815 (cons (nth n (car row)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
816 (math-get-diag-step (cdr row) (1+ n))))) |
40785 | 817 |
818 (defun math-transpose (mat) ; [Public] | |
819 (let ((m nil) | |
820 (col (length (nth 1 mat)))) | |
821 (while (> (setq col (1- col)) 0) | |
822 (setq m (cons (math-mat-col mat col) m))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
823 (cons 'vec m))) |
40785 | 824 |
825 (defun calcFunc-trn (mat) | |
826 (if (math-vectorp mat) | |
827 (if (math-matrixp mat) | |
828 (math-transpose mat) | |
829 (math-col-matrix mat)) | |
830 (if (math-numberp mat) | |
831 mat | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
832 (math-reject-arg mat 'matrixp)))) |
40785 | 833 |
834 (defun calcFunc-ctrn (mat) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
835 (calcFunc-conj (calcFunc-trn mat))) |
40785 | 836 |
837 (defun calcFunc-pack (mode els) | |
838 (or (Math-vectorp els) (math-reject-arg els 'vectorp)) | |
839 (if (and (Math-vectorp mode) (cdr mode)) | |
840 (setq mode (cdr mode)) | |
841 (or (integerp mode) (math-reject-arg mode 'fixnump))) | |
842 (condition-case err | |
843 (if (= (calc-pack-size mode) (1- (length els))) | |
844 (calc-pack-items mode (cdr els)) | |
845 (math-reject-arg els "*Wrong number of elements")) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
846 (error (math-reject-arg els (nth 1 err))))) |
40785 | 847 |
848 (defun calcFunc-unpack (mode thing) | |
849 (or (integerp mode) (math-reject-arg mode 'fixnump)) | |
850 (condition-case err | |
851 (cons 'vec (calc-unpack-item mode thing)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
852 (error (math-reject-arg thing (nth 1 err))))) |
40785 | 853 |
854 (defun calcFunc-unpackt (mode thing) | |
855 (let ((calc-unpack-with-type 'pair)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
856 (calcFunc-unpack mode thing))) |
40785 | 857 |
858 (defun calcFunc-arrange (vec cols) ; [Public] | |
859 (setq cols (math-check-fixnum cols t)) | |
860 (if (math-vectorp vec) | |
861 (let* ((flat (math-flatten-vector vec)) | |
862 (mat (list 'vec)) | |
863 next) | |
864 (if (<= cols 0) | |
865 (nconc mat flat) | |
866 (while (>= (length flat) cols) | |
867 (setq next (nthcdr cols flat)) | |
868 (setcdr (nthcdr (1- cols) flat) nil) | |
869 (setq mat (nconc mat (list (cons 'vec flat))) | |
870 flat next)) | |
871 (if flat | |
872 (setq mat (nconc mat (list (cons 'vec flat))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
873 mat)))) |
40785 | 874 |
875 (defun math-flatten-vector (vec) ; [L V] | |
876 (if (math-vectorp vec) | |
877 (apply 'append (mapcar 'math-flatten-vector (cdr vec))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
878 (list vec))) |
40785 | 879 |
880 (defun calcFunc-vconcat (a b) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
881 (math-normalize (list '| a b))) |
40785 | 882 |
883 (defun calcFunc-vconcatrev (a b) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
884 (math-normalize (list '| b a))) |
40785 | 885 |
886 (defun calcFunc-append (v1 v2) | |
887 (if (and (math-vectorp v1) (math-vectorp v2)) | |
888 (append v1 (cdr v2)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
889 (list 'calcFunc-append v1 v2))) |
40785 | 890 |
891 (defun calcFunc-appendrev (v1 v2) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
892 (calcFunc-append v2 v1)) |
40785 | 893 |
894 | |
895 ;;; Copy a matrix. [Public] | |
896 (defun math-copy-matrix (m) | |
897 (if (math-vectorp (nth 1 m)) | |
898 (cons 'vec (mapcar 'copy-sequence (cdr m))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
899 (copy-sequence m))) |
40785 | 900 |
901 ;;; Convert a scalar or vector into an NxN diagonal matrix. [Public] | |
902 (defun calcFunc-diag (a &optional n) | |
903 (and n (not (integerp n)) | |
904 (setq n (math-check-fixnum n))) | |
905 (if (math-vectorp a) | |
906 (if (and n (/= (length a) (1+ n))) | |
907 (list 'calcFunc-diag a n) | |
908 (if (math-matrixp a) | |
909 (if (and n (/= (length (elt a 1)) (1+ n))) | |
910 (list 'calcFunc-diag a n) | |
911 a) | |
912 (cons 'vec (math-diag-step (cdr a) 0 (1- (length a)))))) | |
913 (if n | |
914 (cons 'vec (math-diag-step (make-list n a) 0 n)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
915 (list 'calcFunc-diag a)))) |
40785 | 916 |
917 (defun calcFunc-idn (a &optional n) | |
918 (if n | |
919 (if (math-vectorp a) | |
920 (math-reject-arg a 'numberp) | |
921 (calcFunc-diag a n)) | |
922 (if (integerp calc-matrix-mode) | |
923 (calcFunc-idn a calc-matrix-mode) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
924 (list 'calcFunc-idn a)))) |
40785 | 925 |
926 (defun math-mimic-ident (a m) | |
927 (if (math-square-matrixp m) | |
928 (calcFunc-idn a (1- (length m))) | |
929 (if (math-vectorp m) | |
930 (if (math-zerop a) | |
931 (cons 'vec (mapcar (function (lambda (x) | |
932 (if (math-vectorp x) | |
933 (math-mimic-ident a x) | |
934 a))) | |
935 (cdr m))) | |
936 (math-dimension-error)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
937 (calcFunc-idn a)))) |
40785 | 938 |
939 (defun math-diag-step (a n m) | |
940 (if (< n m) | |
941 (cons (cons 'vec | |
942 (nconc (make-list n 0) | |
943 (cons (car a) | |
944 (make-list (1- (- m n)) 0)))) | |
945 (math-diag-step (cdr a) (1+ n) m)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
946 nil)) |
40785 | 947 |
948 ;;; Create a vector of consecutive integers. [Public] | |
949 (defun calcFunc-index (n &optional start incr) | |
950 (if (math-messy-integerp n) | |
951 (math-float (calcFunc-index (math-trunc n) start incr)) | |
952 (and (not (integerp n)) | |
953 (setq n (math-check-fixnum n))) | |
954 (let ((vec nil)) | |
955 (if start | |
956 (progn | |
957 (if (>= n 0) | |
958 (while (>= (setq n (1- n)) 0) | |
959 (setq vec (cons start vec) | |
960 start (math-add start (or incr 1)))) | |
961 (while (<= (setq n (1+ n)) 0) | |
962 (setq vec (cons start vec) | |
963 start (math-mul start (or incr 2))))) | |
964 (setq vec (nreverse vec))) | |
965 (if (>= n 0) | |
966 (while (> n 0) | |
967 (setq vec (cons n vec) | |
968 n (1- n))) | |
969 (let ((i -1)) | |
970 (while (>= i n) | |
971 (setq vec (cons i vec) | |
972 i (1- i)))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
973 (cons 'vec vec)))) |
40785 | 974 |
975 ;;; Find an element in a vector. | |
976 (defun calcFunc-find (vec x &optional start) | |
977 (setq start (if start (math-check-fixnum start t) 1)) | |
978 (if (< start 1) (math-reject-arg start 'posp)) | |
979 (setq vec (nthcdr start vec)) | |
980 (let ((n start)) | |
981 (while (and vec (not (Math-equal x (car vec)))) | |
982 (setq n (1+ n) | |
983 vec (cdr vec))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
984 (if vec n 0))) |
40785 | 985 |
986 ;;; Return a subvector of a vector. | |
987 (defun calcFunc-subvec (vec start &optional end) | |
988 (setq start (math-check-fixnum start t) | |
989 end (math-check-fixnum (or end 0) t)) | |
990 (or (math-vectorp vec) (math-reject-arg vec 'vectorp)) | |
991 (let ((len (1- (length vec)))) | |
992 (if (<= start 0) | |
993 (setq start (+ len start 1))) | |
994 (if (<= end 0) | |
995 (setq end (+ len end 1))) | |
996 (if (or (> start len) | |
997 (<= end start)) | |
998 '(vec) | |
999 (setq vec (nthcdr start vec)) | |
1000 (if (<= end len) | |
1001 (let ((chop (nthcdr (- end start 1) (setq vec (copy-sequence vec))))) | |
1002 (setcdr chop nil))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1003 (cons 'vec vec)))) |
40785 | 1004 |
1005 ;;; Remove a subvector from a vector. | |
1006 (defun calcFunc-rsubvec (vec start &optional end) | |
1007 (setq start (math-check-fixnum start t) | |
1008 end (math-check-fixnum (or end 0) t)) | |
1009 (or (math-vectorp vec) (math-reject-arg vec 'vectorp)) | |
1010 (let ((len (1- (length vec)))) | |
1011 (if (<= start 0) | |
1012 (setq start (+ len start 1))) | |
1013 (if (<= end 0) | |
1014 (setq end (+ len end 1))) | |
1015 (if (or (> start len) | |
1016 (<= end start)) | |
1017 vec | |
1018 (let ((tail (nthcdr end vec)) | |
1019 (chop (nthcdr (1- start) (setq vec (copy-sequence vec))))) | |
1020 (setcdr chop nil) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1021 (append vec tail))))) |
40785 | 1022 |
1023 ;;; Reverse the order of the elements of a vector. | |
1024 (defun calcFunc-rev (vec) | |
1025 (if (math-vectorp vec) | |
1026 (cons 'vec (reverse (cdr vec))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1027 (math-reject-arg vec 'vectorp))) |
40785 | 1028 |
1029 ;;; Compress a vector according to a mask vector. | |
1030 (defun calcFunc-vmask (mask vec) | |
1031 (if (math-numberp mask) | |
1032 (if (math-zerop mask) | |
1033 '(vec) | |
1034 vec) | |
1035 (or (math-vectorp mask) (math-reject-arg mask 'vectorp)) | |
1036 (or (math-constp mask) (math-reject-arg mask 'constp)) | |
1037 (or (math-vectorp vec) (math-reject-arg vec 'vectorp)) | |
1038 (or (= (length mask) (length vec)) (math-dimension-error)) | |
1039 (let ((new nil)) | |
1040 (while (setq mask (cdr mask) vec (cdr vec)) | |
1041 (or (math-zerop (car mask)) | |
1042 (setq new (cons (car vec) new)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1043 (cons 'vec (nreverse new))))) |
40785 | 1044 |
1045 ;;; Expand a vector according to a mask vector. | |
1046 (defun calcFunc-vexp (mask vec &optional filler) | |
1047 (or (math-vectorp mask) (math-reject-arg mask 'vectorp)) | |
1048 (or (math-constp mask) (math-reject-arg mask 'constp)) | |
1049 (or (math-vectorp vec) (math-reject-arg vec 'vectorp)) | |
1050 (let ((new nil) | |
1051 (fvec (and filler (math-vectorp filler)))) | |
1052 (while (setq mask (cdr mask)) | |
1053 (if (math-zerop (car mask)) | |
1054 (setq new (cons (or (if fvec | |
1055 (car (setq filler (cdr filler))) | |
1056 filler) | |
1057 (car mask)) new)) | |
1058 (setq vec (cdr vec) | |
1059 new (cons (or (car vec) (car mask)) new)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1060 (cons 'vec (nreverse new)))) |
40785 | 1061 |
1062 | |
1063 ;;; Compute the row and column norms of a vector or matrix. [Public] | |
1064 (defun calcFunc-rnorm (a) | |
1065 (if (and (Math-vectorp a) | |
1066 (math-constp a)) | |
1067 (if (math-matrixp a) | |
1068 (math-reduce-vec 'math-max (math-map-vec 'calcFunc-cnorm a)) | |
1069 (math-reduce-vec 'math-max (math-map-vec 'math-abs a))) | |
1070 (calc-record-why 'vectorp a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1071 (list 'calcFunc-rnorm a))) |
40785 | 1072 |
1073 (defun calcFunc-cnorm (a) | |
1074 (if (and (Math-vectorp a) | |
1075 (math-constp a)) | |
1076 (if (math-matrixp a) | |
1077 (math-reduce-vec 'math-max | |
1078 (math-reduce-cols 'math-add-abs a)) | |
1079 (math-reduce-vec 'math-add-abs a)) | |
1080 (calc-record-why 'vectorp a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1081 (list 'calcFunc-cnorm a))) |
40785 | 1082 |
1083 (defun math-add-abs (a b) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1084 (math-add (math-abs a) (math-abs b))) |
40785 | 1085 |
1086 | |
1087 ;;; Sort the elements of a vector into increasing order. | |
1088 (defun calcFunc-sort (vec) ; [Public] | |
1089 (if (math-vectorp vec) | |
1090 (cons 'vec (sort (copy-sequence (cdr vec)) 'math-beforep)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1091 (math-reject-arg vec 'vectorp))) |
40785 | 1092 |
1093 (defun calcFunc-rsort (vec) ; [Public] | |
1094 (if (math-vectorp vec) | |
1095 (cons 'vec (nreverse (sort (copy-sequence (cdr vec)) 'math-beforep))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1096 (math-reject-arg vec 'vectorp))) |
40785 | 1097 |
1098 (defun calcFunc-grade (grade-vec) | |
1099 (if (math-vectorp grade-vec) | |
1100 (let* ((len (1- (length grade-vec)))) | |
1101 (cons 'vec (sort (cdr (calcFunc-index len)) 'math-grade-beforep))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1102 (math-reject-arg grade-vec 'vectorp))) |
40785 | 1103 |
1104 (defun calcFunc-rgrade (grade-vec) | |
1105 (if (math-vectorp grade-vec) | |
1106 (let* ((len (1- (length grade-vec)))) | |
1107 (cons 'vec (nreverse (sort (cdr (calcFunc-index len)) | |
1108 'math-grade-beforep)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1109 (math-reject-arg grade-vec 'vectorp))) |
40785 | 1110 |
1111 (defun math-grade-beforep (i j) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1112 (math-beforep (nth i grade-vec) (nth j grade-vec))) |
40785 | 1113 |
1114 | |
1115 ;;; Compile a histogram of data from a vector. | |
1116 (defun calcFunc-histogram (vec wts &optional n) | |
1117 (or n (setq n wts wts 1)) | |
1118 (or (Math-vectorp vec) | |
1119 (math-reject-arg vec 'vectorp)) | |
1120 (if (Math-vectorp wts) | |
1121 (or (= (length vec) (length wts)) | |
1122 (math-dimension-error))) | |
1123 (or (natnump n) | |
1124 (math-reject-arg n 'fixnatnump)) | |
1125 (let ((res (make-vector n 0)) | |
1126 (vp vec) | |
1127 (wvec (Math-vectorp wts)) | |
1128 (wp wts) | |
1129 bin) | |
1130 (while (setq vp (cdr vp)) | |
1131 (setq bin (car vp)) | |
1132 (or (natnump bin) | |
1133 (setq bin (math-floor bin))) | |
1134 (and (natnump bin) | |
1135 (< bin n) | |
1136 (aset res bin (math-add (aref res bin) | |
1137 (if wvec (car (setq wp (cdr wp))) wts))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1138 (cons 'vec (append res nil)))) |
40785 | 1139 |
1140 | |
1141 ;;; Set operations. | |
1142 | |
1143 (defun calcFunc-vunion (a b) | |
1144 (if (Math-objectp a) | |
1145 (setq a (list 'vec a)) | |
1146 (or (math-vectorp a) (math-reject-arg a 'vectorp))) | |
1147 (if (Math-objectp b) | |
1148 (setq b (list b)) | |
1149 (or (math-vectorp b) (math-reject-arg b 'vectorp)) | |
1150 (setq b (cdr b))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1151 (calcFunc-rdup (append a b))) |
40785 | 1152 |
1153 (defun calcFunc-vint (a b) | |
1154 (if (and (math-simple-set a) (math-simple-set b)) | |
1155 (progn | |
1156 (setq a (cdr (calcFunc-rdup a))) | |
1157 (setq b (cdr (calcFunc-rdup b))) | |
1158 (let ((vec (list 'vec))) | |
1159 (while (and a b) | |
1160 (if (math-beforep (car a) (car b)) | |
1161 (setq a (cdr a)) | |
1162 (if (Math-equal (car a) (car b)) | |
1163 (setq vec (cons (car a) vec) | |
1164 a (cdr a))) | |
1165 (setq b (cdr b)))) | |
1166 (nreverse vec))) | |
1167 (calcFunc-vcompl (calcFunc-vunion (calcFunc-vcompl a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1168 (calcFunc-vcompl b))))) |
40785 | 1169 |
1170 (defun calcFunc-vdiff (a b) | |
1171 (if (and (math-simple-set a) (math-simple-set b)) | |
1172 (progn | |
1173 (setq a (cdr (calcFunc-rdup a))) | |
1174 (setq b (cdr (calcFunc-rdup b))) | |
1175 (let ((vec (list 'vec))) | |
1176 (while a | |
1177 (while (and b (math-beforep (car b) (car a))) | |
1178 (setq b (cdr b))) | |
1179 (if (and b (Math-equal (car a) (car b))) | |
1180 (setq a (cdr a) | |
1181 b (cdr b)) | |
1182 (setq vec (cons (car a) vec) | |
1183 a (cdr a)))) | |
1184 (nreverse vec))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1185 (calcFunc-vcompl (calcFunc-vunion (calcFunc-vcompl a) b)))) |
40785 | 1186 |
1187 (defun calcFunc-vxor (a b) | |
1188 (if (and (math-simple-set a) (math-simple-set b)) | |
1189 (progn | |
1190 (setq a (cdr (calcFunc-rdup a))) | |
1191 (setq b (cdr (calcFunc-rdup b))) | |
1192 (let ((vec (list 'vec))) | |
1193 (while (or a b) | |
1194 (if (and a | |
1195 (or (not b) | |
1196 (math-beforep (car a) (car b)))) | |
1197 (setq vec (cons (car a) vec) | |
1198 a (cdr a)) | |
1199 (if (and a (Math-equal (car a) (car b))) | |
1200 (setq a (cdr a)) | |
1201 (setq vec (cons (car b) vec))) | |
1202 (setq b (cdr b)))) | |
1203 (nreverse vec))) | |
1204 (let ((ca (calcFunc-vcompl a)) | |
1205 (cb (calcFunc-vcompl b))) | |
1206 (calcFunc-vunion (calcFunc-vcompl (calcFunc-vunion ca b)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1207 (calcFunc-vcompl (calcFunc-vunion a cb)))))) |
40785 | 1208 |
1209 (defun calcFunc-vcompl (a) | |
1210 (setq a (math-prepare-set a)) | |
1211 (let ((vec (list 'vec)) | |
1212 (prev '(neg (var inf var-inf))) | |
1213 (closed 2)) | |
1214 (while (setq a (cdr a)) | |
1215 (or (and (equal (nth 2 (car a)) '(neg (var inf var-inf))) | |
1216 (memq (nth 1 (car a)) '(2 3))) | |
1217 (setq vec (cons (list 'intv | |
1218 (+ closed | |
1219 (if (memq (nth 1 (car a)) '(0 1)) 1 0)) | |
1220 prev | |
1221 (nth 2 (car a))) | |
1222 vec))) | |
1223 (setq prev (nth 3 (car a)) | |
1224 closed (if (memq (nth 1 (car a)) '(0 2)) 2 0))) | |
1225 (or (and (equal prev '(var inf var-inf)) | |
1226 (= closed 0)) | |
1227 (setq vec (cons (list 'intv (+ closed 1) | |
1228 prev '(var inf var-inf)) | |
1229 vec))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1230 (math-clean-set (nreverse vec)))) |
40785 | 1231 |
1232 (defun calcFunc-vspan (a) | |
1233 (setq a (math-prepare-set a)) | |
1234 (if (cdr a) | |
1235 (let ((last (nth (1- (length a)) a))) | |
1236 (math-make-intv (+ (logand (nth 1 (nth 1 a)) 2) | |
1237 (logand (nth 1 last) 1)) | |
1238 (nth 2 (nth 1 a)) | |
1239 (nth 3 last))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1240 '(intv 2 0 0))) |
40785 | 1241 |
1242 (defun calcFunc-vfloor (a &optional always-vec) | |
1243 (setq a (math-prepare-set a)) | |
1244 (let ((vec (list 'vec)) (p a) (prev nil) b mask) | |
1245 (while (setq p (cdr p)) | |
1246 (setq mask (nth 1 (car p)) | |
1247 a (nth 2 (car p)) | |
1248 b (nth 3 (car p))) | |
1249 (and (memq mask '(0 1)) | |
1250 (not (math-infinitep a)) | |
1251 (setq mask (logior mask 2)) | |
1252 (math-num-integerp a) | |
1253 (setq a (math-add a 1))) | |
1254 (setq a (math-ceiling a)) | |
1255 (and (memq mask '(0 2)) | |
1256 (not (math-infinitep b)) | |
1257 (setq mask (logior mask 1)) | |
1258 (math-num-integerp b) | |
1259 (setq b (math-sub b 1))) | |
1260 (setq b (math-floor b)) | |
1261 (if (and prev (Math-equal (math-sub a 1) (nth 3 prev))) | |
1262 (setcar (nthcdr 3 prev) b) | |
1263 (or (Math-lessp b a) | |
1264 (setq vec (cons (setq prev (list 'intv mask a b)) vec))))) | |
1265 (setq vec (nreverse vec)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1266 (math-clean-set vec always-vec))) |
40785 | 1267 |
1268 (defun calcFunc-vcard (a) | |
1269 (setq a (calcFunc-vfloor a t)) | |
1270 (or (math-constp a) (math-reject-arg a "*Set must be finite")) | |
1271 (let ((count 0)) | |
1272 (while (setq a (cdr a)) | |
1273 (if (eq (car-safe (car a)) 'intv) | |
1274 (setq count (math-add count (math-sub (nth 3 (car a)) | |
1275 (nth 2 (car a)))))) | |
1276 (setq count (math-add count 1))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1277 count)) |
40785 | 1278 |
1279 (defun calcFunc-venum (a) | |
1280 (setq a (calcFunc-vfloor a t)) | |
1281 (or (math-constp a) (math-reject-arg a "*Set must be finite")) | |
1282 (let ((p a) next) | |
1283 (while (cdr p) | |
1284 (setq next (cdr p)) | |
1285 (if (eq (car-safe (nth 1 p)) 'intv) | |
1286 (setcdr p (nconc (cdr (calcFunc-index (math-add | |
1287 (math-sub (nth 3 (nth 1 p)) | |
1288 (nth 2 (nth 1 p))) | |
1289 1) | |
1290 (nth 2 (nth 1 p)))) | |
1291 (cdr (cdr p))))) | |
1292 (setq p next)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1293 a)) |
40785 | 1294 |
1295 (defun calcFunc-vpack (a) | |
1296 (setq a (calcFunc-vfloor a t)) | |
1297 (if (and (cdr a) | |
1298 (math-negp (if (eq (car-safe (nth 1 a)) 'intv) | |
1299 (nth 2 (nth 1 a)) | |
1300 (nth 1 a)))) | |
1301 (math-reject-arg (nth 1 a) 'posp)) | |
1302 (let ((accum 0)) | |
1303 (while (setq a (cdr a)) | |
1304 (if (eq (car-safe (car a)) 'intv) | |
1305 (if (equal (nth 3 (car a)) '(var inf var-inf)) | |
1306 (setq accum (math-sub accum | |
1307 (math-power-of-2 (nth 2 (car a))))) | |
1308 (setq accum (math-add accum | |
1309 (math-sub | |
1310 (math-power-of-2 (1+ (nth 3 (car a)))) | |
1311 (math-power-of-2 (nth 2 (car a))))))) | |
1312 (setq accum (math-add accum (math-power-of-2 (car a)))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1313 accum)) |
40785 | 1314 |
1315 (defun calcFunc-vunpack (a &optional w) | |
1316 (or (math-num-integerp a) (math-reject-arg a 'integerp)) | |
1317 (if w (setq a (math-clip a w))) | |
1318 (if (math-messy-integerp a) (setq a (math-trunc a))) | |
1319 (let* ((calc-number-radix 2) | |
1320 (neg (math-negp a)) | |
1321 (aa (if neg (math-sub -1 a) a)) | |
1322 (str (if (eq aa 0) | |
1323 "" | |
1324 (if (consp aa) | |
1325 (math-format-bignum-binary (cdr aa)) | |
1326 (math-format-binary aa)))) | |
1327 (zero (if neg ?1 ?0)) | |
1328 (one (if neg ?0 ?1)) | |
1329 (len (length str)) | |
1330 (vec (list 'vec)) | |
1331 (pos (1- len)) pos2) | |
1332 (while (>= pos 0) | |
1333 (if (eq (aref str pos) zero) | |
1334 (setq pos (1- pos)) | |
1335 (setq pos2 pos) | |
1336 (while (and (>= pos 0) (eq (aref str pos) one)) | |
1337 (setq pos (1- pos))) | |
1338 (setq vec (cons (if (= pos (1- pos2)) | |
1339 (- len pos2 1) | |
1340 (list 'intv 3 (- len pos2 1) (- len pos 2))) | |
1341 vec)))) | |
1342 (if neg | |
1343 (setq vec (cons (list 'intv 2 len '(var inf var-inf)) vec))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1344 (math-clean-set (nreverse vec)))) |
40785 | 1345 |
1346 (defun calcFunc-rdup (a) | |
1347 (if (math-simple-set a) | |
1348 (progn | |
1349 (and (Math-objectp a) (setq a (list 'vec a))) | |
1350 (or (math-vectorp a) (math-reject-arg a 'vectorp)) | |
1351 (setq a (sort (copy-sequence (cdr a)) 'math-beforep)) | |
1352 (let ((p a)) | |
1353 (while (cdr p) | |
1354 (if (Math-equal (car p) (nth 1 p)) | |
1355 (setcdr p (cdr (cdr p))) | |
1356 (setq p (cdr p))))) | |
1357 (cons 'vec a)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1358 (math-clean-set (math-prepare-set a)))) |
40785 | 1359 |
1360 (defun math-prepare-set (a) | |
1361 (if (Math-objectp a) | |
1362 (setq a (list 'vec a)) | |
1363 (or (math-vectorp a) (math-reject-arg a 'vectorp)) | |
1364 (setq a (cons 'vec (sort (copy-sequence (cdr a)) 'math-beforep)))) | |
1365 (let ((p a) res) | |
1366 | |
1367 ;; Convert all elements to non-empty intervals. | |
1368 (while (cdr p) | |
1369 (if (eq (car-safe (nth 1 p)) 'intv) | |
1370 (if (math-intv-constp (nth 1 p)) | |
1371 (if (and (memq (nth 1 (nth 1 p)) '(0 1 2)) | |
1372 (Math-equal (nth 2 (nth 1 p)) (nth 3 (nth 1 p)))) | |
1373 (setcdr p (cdr (cdr p))) | |
1374 (setq p (cdr p))) | |
1375 (math-reject-arg (nth 1 p) 'constp)) | |
1376 (or (Math-anglep (nth 1 p)) | |
1377 (eq (car (nth 1 p)) 'date) | |
1378 (equal (nth 1 p) '(var inf var-inf)) | |
1379 (equal (nth 1 p) '(neg (var inf var-inf))) | |
1380 (math-reject-arg (nth 1 p) 'realp)) | |
1381 (setcar (cdr p) (list 'intv 3 (nth 1 p) (nth 1 p))) | |
1382 (setq p (cdr p)))) | |
1383 | |
1384 ;; Combine redundant intervals. | |
1385 (setq p a) | |
1386 (while (cdr (cdr p)) | |
1387 (if (or (memq (setq res (math-compare (nth 3 (nth 1 p)) | |
1388 (nth 2 (nth 2 p)))) | |
1389 '(-1 2)) | |
1390 (and (eq res 0) | |
1391 (memq (nth 1 (nth 1 p)) '(0 2)) | |
1392 (memq (nth 1 (nth 2 p)) '(0 1)))) | |
1393 (setq p (cdr p)) | |
1394 (setq res (math-compare (nth 3 (nth 1 p)) (nth 3 (nth 2 p)))) | |
1395 (setcdr p (cons (list 'intv | |
1396 (+ (logand (logior (nth 1 (nth 1 p)) | |
1397 (if (Math-equal | |
1398 (nth 2 (nth 1 p)) | |
1399 (nth 2 (nth 2 p))) | |
1400 (nth 1 (nth 2 p)) | |
1401 0)) | |
1402 2) | |
1403 (logand (logior (if (memq res '(1 0 2)) | |
1404 (nth 1 (nth 1 p)) 0) | |
1405 (if (memq res '(-1 0 2)) | |
1406 (nth 1 (nth 2 p)) 0)) | |
1407 1)) | |
1408 (nth 2 (nth 1 p)) | |
1409 (if (eq res 1) | |
1410 (nth 3 (nth 1 p)) | |
1411 (nth 3 (nth 2 p)))) | |
1412 (cdr (cdr (cdr p)))))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1413 a) |
40785 | 1414 |
1415 (defun math-clean-set (a &optional always-vec) | |
1416 (let ((p a) res) | |
1417 (while (cdr p) | |
1418 (if (and (eq (car-safe (nth 1 p)) 'intv) | |
1419 (Math-equal (nth 2 (nth 1 p)) (nth 3 (nth 1 p)))) | |
1420 (setcar (cdr p) (nth 2 (nth 1 p)))) | |
1421 (setq p (cdr p))) | |
1422 (if (and (not (cdr (cdr a))) | |
1423 (eq (car-safe (nth 1 a)) 'intv) | |
1424 (not always-vec)) | |
1425 (nth 1 a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1426 a))) |
40785 | 1427 |
1428 (defun math-simple-set (a) | |
1429 (or (and (Math-objectp a) | |
1430 (not (eq (car-safe a) 'intv))) | |
1431 (and (Math-vectorp a) | |
1432 (progn | |
1433 (while (and (setq a (cdr a)) | |
1434 (not (eq (car-safe (car a)) 'intv)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1435 (null a))))) |
40785 | 1436 |
1437 | |
1438 | |
1439 | |
1440 ;;; Compute a right-handed vector cross product. [O O O] [Public] | |
1441 (defun calcFunc-cross (a b) | |
1442 (if (and (eq (car-safe a) 'vec) | |
1443 (= (length a) 4)) | |
1444 (if (and (eq (car-safe b) 'vec) | |
1445 (= (length b) 4)) | |
1446 (list 'vec | |
1447 (math-sub (math-mul (nth 2 a) (nth 3 b)) | |
1448 (math-mul (nth 3 a) (nth 2 b))) | |
1449 (math-sub (math-mul (nth 3 a) (nth 1 b)) | |
1450 (math-mul (nth 1 a) (nth 3 b))) | |
1451 (math-sub (math-mul (nth 1 a) (nth 2 b)) | |
1452 (math-mul (nth 2 a) (nth 1 b)))) | |
1453 (math-reject-arg b "*Three-vector expected")) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1454 (math-reject-arg a "*Three-vector expected"))) |
40785 | 1455 |
1456 | |
1457 | |
1458 | |
1459 | |
1460 (defun math-read-brackets (space-sep close) | |
1461 (and space-sep (setq space-sep (not (math-check-for-commas)))) | |
1462 (math-read-token) | |
1463 (while (eq exp-token 'space) | |
1464 (math-read-token)) | |
1465 (if (or (equal exp-data close) | |
1466 (eq exp-token 'end)) | |
1467 (progn | |
1468 (math-read-token) | |
1469 '(vec)) | |
1470 (let ((save-exp-pos exp-pos) | |
1471 (save-exp-old-pos exp-old-pos) | |
1472 (save-exp-token exp-token) | |
1473 (save-exp-data exp-data) | |
1474 (vals (let ((exp-keep-spaces space-sep)) | |
1475 (if (or (equal exp-data "\\dots") | |
1476 (equal exp-data "\\ldots")) | |
1477 '(vec (neg (var inf var-inf))) | |
1478 (catch 'syntax (math-read-vector)))))) | |
1479 (if (stringp vals) | |
1480 (if space-sep | |
1481 (let ((error-exp-pos exp-pos) | |
1482 (error-exp-old-pos exp-old-pos) | |
1483 vals2) | |
1484 (setq exp-pos save-exp-pos | |
1485 exp-old-pos save-exp-old-pos | |
1486 exp-token save-exp-token | |
1487 exp-data save-exp-data) | |
1488 (let ((exp-keep-spaces nil)) | |
1489 (setq vals2 (catch 'syntax (math-read-vector)))) | |
1490 (if (and (not (stringp vals2)) | |
1491 (or (assoc exp-data '(("\\ldots") ("\\dots") (";"))) | |
1492 (equal exp-data close) | |
1493 (eq exp-token 'end))) | |
1494 (setq space-sep nil | |
1495 vals vals2) | |
1496 (setq exp-pos error-exp-pos | |
1497 exp-old-pos error-exp-old-pos) | |
1498 (throw 'syntax vals))) | |
1499 (throw 'syntax vals))) | |
1500 (if (or (equal exp-data "\\dots") | |
1501 (equal exp-data "\\ldots")) | |
1502 (progn | |
1503 (math-read-token) | |
1504 (setq vals (if (> (length vals) 2) | |
1505 (cons 'calcFunc-mul (cdr vals)) (nth 1 vals))) | |
1506 (let ((exp2 (if (or (equal exp-data close) | |
1507 (equal exp-data ")") | |
1508 (eq exp-token 'end)) | |
1509 '(var inf var-inf) | |
1510 (math-read-expr-level 0)))) | |
1511 (setq vals | |
1512 (list 'intv | |
1513 (if (equal exp-data ")") 2 3) | |
1514 vals | |
1515 exp2))) | |
1516 (if (not (or (equal exp-data close) | |
1517 (equal exp-data ")") | |
1518 (eq exp-token 'end))) | |
1519 (throw 'syntax "Expected `]'"))) | |
1520 (if (equal exp-data ";") | |
1521 (let ((exp-keep-spaces space-sep)) | |
1522 (setq vals (cons 'vec (math-read-matrix (list vals)))))) | |
1523 (if (not (or (equal exp-data close) | |
1524 (eq exp-token 'end))) | |
1525 (throw 'syntax "Expected `]'"))) | |
1526 (or (eq exp-token 'end) | |
1527 (math-read-token)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1528 vals))) |
40785 | 1529 |
1530 (defun math-check-for-commas (&optional balancing) | |
1531 (let ((count 0) | |
1532 (pos (1- exp-pos))) | |
1533 (while (and (>= count 0) | |
1534 (setq pos (string-match | |
1535 (if balancing "[],[{}()<>]" "[],[{}()]") | |
1536 exp-str (1+ pos))) | |
1537 (or (/= (aref exp-str pos) ?,) (> count 0) balancing)) | |
1538 (cond ((memq (aref exp-str pos) '(?\[ ?\{ ?\( ?\<)) | |
1539 (setq count (1+ count))) | |
1540 ((memq (aref exp-str pos) '(?\] ?\} ?\) ?\>)) | |
1541 (setq count (1- count))))) | |
1542 (if balancing | |
1543 pos | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1544 (and pos (= (aref exp-str pos) ?,))))) |
40785 | 1545 |
1546 (defun math-read-vector () | |
1547 (let* ((val (list (math-read-expr-level 0))) | |
1548 (last val)) | |
1549 (while (progn | |
1550 (while (eq exp-token 'space) | |
1551 (math-read-token)) | |
1552 (and (not (eq exp-token 'end)) | |
1553 (not (equal exp-data ";")) | |
1554 (not (equal exp-data close)) | |
1555 (not (equal exp-data "\\dots")) | |
1556 (not (equal exp-data "\\ldots")))) | |
1557 (if (equal exp-data ",") | |
1558 (math-read-token)) | |
1559 (while (eq exp-token 'space) | |
1560 (math-read-token)) | |
1561 (let ((rest (list (math-read-expr-level 0)))) | |
1562 (setcdr last rest) | |
1563 (setq last rest))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1564 (cons 'vec val))) |
40785 | 1565 |
1566 (defun math-read-matrix (mat) | |
1567 (while (equal exp-data ";") | |
1568 (math-read-token) | |
1569 (while (eq exp-token 'space) | |
1570 (math-read-token)) | |
1571 (setq mat (nconc mat (list (math-read-vector))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1572 mat) |
40785 | 1573 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1574 ;;; calc-vec.el ends here |