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