Add a provide statement.
[bpt/emacs.git] / lisp / calc / calc-map.el
CommitLineData
3132f345
CW
1;;; calc-map.el --- higher-order functions for Calc
2
ab5a1765 3;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2004 Free Software Foundation, Inc.
3132f345
CW
4
5;; Author: David Gillespie <daveg@synaptics.com>
8b31b519 6;; Maintainer: Jay Belanger <belanger@truman.edu>
136211a9
EZ
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is distributed in the hope that it will be useful,
11;; but WITHOUT ANY WARRANTY. No author or distributor
12;; accepts responsibility to anyone for the consequences of using it
13;; or for whether it serves any particular purpose or works at all,
14;; unless he says so in writing. Refer to the GNU Emacs General Public
15;; License for full details.
16
17;; Everyone is granted permission to copy, modify and redistribute
18;; GNU Emacs, but only under the conditions described in the
19;; GNU Emacs General Public License. A copy of this license is
20;; supposed to have been given to you along with GNU Emacs so you
21;; can know your rights and responsibilities. It should be in a
22;; file named COPYING. Among other things, the copyright notice
23;; and this notice must be preserved on all copies.
24
3132f345 25;;; Commentary:
136211a9 26
3132f345 27;;; Code:
136211a9
EZ
28
29;; This file is autoloaded from calc-ext.el.
30(require 'calc-ext)
31
32(require 'calc-macs)
33
34(defun calc-Need-calc-map () nil)
35
36
37(defun calc-apply (&optional oper)
38 (interactive)
39 (calc-wrapper
40 (let* ((sel-mode nil)
41 (calc-dollar-values (mapcar 'calc-get-stack-element
42 (nthcdr calc-stack-top calc-stack)))
43 (calc-dollar-used 0)
44 (oper (or oper (calc-get-operator "Apply"
45 (if (math-vectorp (calc-top 1))
46 (1- (length (calc-top 1)))
47 -1))))
48 (expr (calc-top-n (1+ calc-dollar-used))))
49 (message "Working...")
50 (calc-set-command-flag 'clear-message)
51 (calc-enter-result (1+ calc-dollar-used)
52 (concat (substring "apl" 0 (- 4 (length (nth 2 oper))))
53 (nth 2 oper))
54 (list 'calcFunc-apply
55 (math-calcFunc-to-var (nth 1 oper))
bf77c646 56 expr)))))
136211a9
EZ
57
58(defun calc-reduce (&optional oper accum)
59 (interactive)
60 (calc-wrapper
61 (let* ((sel-mode nil)
62 (nest (calc-is-hyperbolic))
63 (rev (calc-is-inverse))
64 (nargs (if (and nest (not rev)) 2 1))
65 (calc-dollar-values (mapcar 'calc-get-stack-element
66 (nthcdr calc-stack-top calc-stack)))
67 (calc-dollar-used 0)
68 (calc-mapping-dir (and (not accum) (not nest) ""))
69 (oper (or oper (calc-get-operator
70 (if nest
71 (concat (if accum "Accumulate " "")
72 (if rev "Fixed Point" "Nest"))
73 (concat (if rev "Inv " "")
74 (if accum "Accumulate" "Reduce")))
75 (if nest 1 2)))))
76 (message "Working...")
77 (calc-set-command-flag 'clear-message)
78 (calc-enter-result (+ calc-dollar-used nargs)
79 (concat (substring (if nest
80 (if rev "fxp" "nst")
81 (if accum "acc" "red"))
82 0 (- 4 (length (nth 2 oper))))
83 (nth 2 oper))
84 (if nest
85 (cons (if rev
86 (if accum 'calcFunc-afixp 'calcFunc-fixp)
87 (if accum 'calcFunc-anest 'calcFunc-nest))
88 (cons (math-calcFunc-to-var (nth 1 oper))
89 (calc-top-list-n
90 nargs (1+ calc-dollar-used))))
91 (list (if accum
92 (if rev 'calcFunc-raccum 'calcFunc-accum)
93 (intern (concat "calcFunc-"
94 (if rev "r" "")
95 "reduce"
96 calc-mapping-dir)))
97 (math-calcFunc-to-var (nth 1 oper))
bf77c646 98 (calc-top-n (1+ calc-dollar-used))))))))
136211a9
EZ
99
100(defun calc-accumulate (&optional oper)
101 (interactive)
bf77c646 102 (calc-reduce oper t))
136211a9
EZ
103
104(defun calc-map (&optional oper)
105 (interactive)
106 (calc-wrapper
107 (let* ((sel-mode nil)
108 (calc-dollar-values (mapcar 'calc-get-stack-element
109 (nthcdr calc-stack-top calc-stack)))
110 (calc-dollar-used 0)
111 (calc-mapping-dir "")
112 (oper (or oper (calc-get-operator "Map")))
113 (nargs (car oper)))
114 (message "Working...")
115 (calc-set-command-flag 'clear-message)
116 (calc-enter-result (+ nargs calc-dollar-used)
117 (concat (substring "map" 0 (- 4 (length (nth 2 oper))))
118 (nth 2 oper))
119 (cons (intern (concat "calcFunc-map" calc-mapping-dir))
120 (cons (math-calcFunc-to-var (nth 1 oper))
121 (calc-top-list-n
122 nargs
bf77c646 123 (1+ calc-dollar-used))))))))
136211a9
EZ
124
125(defun calc-map-equation (&optional oper)
126 (interactive)
127 (calc-wrapper
128 (let* ((sel-mode nil)
129 (calc-dollar-values (mapcar 'calc-get-stack-element
130 (nthcdr calc-stack-top calc-stack)))
131 (calc-dollar-used 0)
132 (oper (or oper (calc-get-operator "Map-equation")))
133 (nargs (car oper)))
134 (message "Working...")
135 (calc-set-command-flag 'clear-message)
136 (calc-enter-result (+ nargs calc-dollar-used)
137 (concat (substring "map" 0 (- 4 (length (nth 2 oper))))
138 (nth 2 oper))
139 (cons (if (calc-is-inverse)
140 'calcFunc-mapeqr
141 (if (calc-is-hyperbolic)
142 'calcFunc-mapeqp 'calcFunc-mapeq))
143 (cons (math-calcFunc-to-var (nth 1 oper))
144 (calc-top-list-n
145 nargs
bf77c646 146 (1+ calc-dollar-used))))))))
136211a9 147
3132f345
CW
148(defvar calc-verify-arglist t)
149(defvar calc-mapping-dir nil)
136211a9
EZ
150(defun calc-map-stack ()
151 "This is meant to be called by calc-keypad mode."
152 (interactive)
153 (let ((calc-verify-arglist nil))
154 (calc-unread-command ?\$)
bf77c646 155 (calc-map)))
136211a9
EZ
156
157(defun calc-outer-product (&optional oper)
158 (interactive)
159 (calc-wrapper
160 (let* ((sel-mode nil)
161 (calc-dollar-values (mapcar 'calc-get-stack-element
162 (nthcdr calc-stack-top calc-stack)))
163 (calc-dollar-used 0)
164 (oper (or oper (calc-get-operator "Outer" 2))))
165 (message "Working...")
166 (calc-set-command-flag 'clear-message)
167 (calc-enter-result (+ 2 calc-dollar-used)
168 (concat (substring "out" 0 (- 4 (length (nth 2 oper))))
169 (nth 2 oper))
170 (cons 'calcFunc-outer
171 (cons (math-calcFunc-to-var (nth 1 oper))
172 (calc-top-list-n
bf77c646 173 2 (1+ calc-dollar-used))))))))
136211a9
EZ
174
175(defun calc-inner-product (&optional mul-oper add-oper)
176 (interactive)
177 (calc-wrapper
178 (let* ((sel-mode nil)
179 (calc-dollar-values (mapcar 'calc-get-stack-element
180 (nthcdr calc-stack-top calc-stack)))
181 (calc-dollar-used 0)
182 (mul-oper (or mul-oper (calc-get-operator "Inner (Mult)" 2)))
183 (mul-used calc-dollar-used)
184 (calc-dollar-values (if (> mul-used 0)
185 (cdr calc-dollar-values)
186 calc-dollar-values))
187 (calc-dollar-used 0)
188 (add-oper (or add-oper (calc-get-operator "Inner (Add)" 2))))
189 (message "Working...")
190 (calc-set-command-flag 'clear-message)
191 (calc-enter-result (+ 2 mul-used calc-dollar-used)
192 (concat "in"
193 (substring (nth 2 mul-oper) 0 1)
194 (substring (nth 2 add-oper) 0 1))
195 (nconc (list 'calcFunc-inner
196 (math-calcFunc-to-var (nth 1 mul-oper))
197 (math-calcFunc-to-var (nth 1 add-oper)))
198 (calc-top-list-n
bf77c646 199 2 (+ 1 mul-used calc-dollar-used)))))))
136211a9 200
136211a9
EZ
201(defconst calc-oper-keys '( ( ( ?+ 2 calcFunc-add )
202 ( ?- 2 calcFunc-sub )
203 ( ?* 2 calcFunc-mul )
204 ( ?/ 2 calcFunc-div )
205 ( ?^ 2 calcFunc-pow )
206 ( ?| 2 calcFunc-vconcat )
207 ( ?% 2 calcFunc-mod )
208 ( ?\\ 2 calcFunc-idiv )
209 ( ?! 1 calcFunc-fact )
210 ( ?& 1 calcFunc-inv )
211 ( ?n 1 calcFunc-neg )
212 ( ?x user )
213 ( ?z user )
214 ( ?A 1 calcFunc-abs )
215 ( ?J 1 calcFunc-conj )
216 ( ?G 1 calcFunc-arg )
217 ( ?Q 1 calcFunc-sqrt )
218 ( ?N 2 calcFunc-min )
219 ( ?X 2 calcFunc-max )
220 ( ?F 1 calcFunc-floor )
221 ( ?R 1 calcFunc-round )
222 ( ?S 1 calcFunc-sin )
223 ( ?C 1 calcFunc-cos )
224 ( ?T 1 calcFunc-tan )
225 ( ?L 1 calcFunc-ln )
226 ( ?E 1 calcFunc-exp )
227 ( ?B 2 calcFunc-log ) )
228 ( ( ?F 1 calcFunc-ceil ) ; inverse
229 ( ?R 1 calcFunc-trunc )
230 ( ?Q 1 calcFunc-sqr )
231 ( ?S 1 calcFunc-arcsin )
232 ( ?C 1 calcFunc-arccos )
233 ( ?T 1 calcFunc-arctan )
234 ( ?L 1 calcFunc-exp )
235 ( ?E 1 calcFunc-ln )
236 ( ?B 2 calcFunc-alog )
237 ( ?^ 2 calcFunc-nroot )
238 ( ?| 2 calcFunc-vconcatrev ) )
239 ( ( ?F 1 calcFunc-ffloor ) ; hyperbolic
240 ( ?R 1 calcFunc-fround )
241 ( ?S 1 calcFunc-sinh )
242 ( ?C 1 calcFunc-cosh )
243 ( ?T 1 calcFunc-tanh )
244 ( ?L 1 calcFunc-log10 )
245 ( ?E 1 calcFunc-exp10 )
246 ( ?| 2 calcFunc-append ) )
247 ( ( ?F 1 calcFunc-fceil ) ; inverse-hyperbolic
248 ( ?R 1 calcFunc-ftrunc )
249 ( ?S 1 calcFunc-arcsinh )
250 ( ?C 1 calcFunc-arccosh )
251 ( ?T 1 calcFunc-arctanh )
252 ( ?L 1 calcFunc-exp10 )
253 ( ?E 1 calcFunc-log10 )
3132f345
CW
254 ( ?| 2 calcFunc-appendrev ) )))
255
136211a9
EZ
256(defconst calc-a-oper-keys '( ( ( ?a 3 calcFunc-apart )
257 ( ?b 3 calcFunc-subst )
258 ( ?c 2 calcFunc-collect )
259 ( ?d 2 calcFunc-deriv )
260 ( ?e 1 calcFunc-esimplify )
261 ( ?f 2 calcFunc-factor )
262 ( ?g 2 calcFunc-pgcd )
263 ( ?i 2 calcFunc-integ )
264 ( ?m 2 calcFunc-match )
265 ( ?n 1 calcFunc-nrat )
266 ( ?r 2 calcFunc-rewrite )
267 ( ?s 1 calcFunc-simplify )
268 ( ?t 3 calcFunc-taylor )
269 ( ?x 1 calcFunc-expand )
270 ( ?M 2 calcFunc-mapeq )
271 ( ?N 3 calcFunc-minimize )
272 ( ?P 2 calcFunc-roots )
273 ( ?R 3 calcFunc-root )
274 ( ?S 2 calcFunc-solve )
275 ( ?T 4 calcFunc-table )
276 ( ?X 3 calcFunc-maximize )
277 ( ?= 2 calcFunc-eq )
278 ( ?\# 2 calcFunc-neq )
279 ( ?< 2 calcFunc-lt )
280 ( ?> 2 calcFunc-gt )
281 ( ?\[ 2 calcFunc-leq )
282 ( ?\] 2 calcFunc-geq )
283 ( ?{ 2 calcFunc-in )
284 ( ?! 1 calcFunc-lnot )
285 ( ?& 2 calcFunc-land )
286 ( ?\| 2 calcFunc-lor )
287 ( ?: 3 calcFunc-if )
288 ( ?. 2 calcFunc-rmeq )
289 ( ?+ 4 calcFunc-sum )
290 ( ?- 4 calcFunc-asum )
291 ( ?* 4 calcFunc-prod )
292 ( ?_ 2 calcFunc-subscr )
293 ( ?\\ 2 calcFunc-pdiv )
294 ( ?% 2 calcFunc-prem )
295 ( ?/ 2 calcFunc-pdivrem ) )
296 ( ( ?m 2 calcFunc-matchnot )
297 ( ?M 2 calcFunc-mapeqr )
298 ( ?S 2 calcFunc-finv ) )
299 ( ( ?d 2 calcFunc-tderiv )
300 ( ?f 2 calcFunc-factors )
301 ( ?M 2 calcFunc-mapeqp )
302 ( ?N 3 calcFunc-wminimize )
303 ( ?R 3 calcFunc-wroot )
304 ( ?S 2 calcFunc-fsolve )
305 ( ?X 3 calcFunc-wmaximize )
306 ( ?/ 2 calcFunc-pdivide ) )
3132f345
CW
307 ( ( ?S 2 calcFunc-ffinv ) )))
308
136211a9
EZ
309(defconst calc-b-oper-keys '( ( ( ?a 2 calcFunc-and )
310 ( ?o 2 calcFunc-or )
311 ( ?x 2 calcFunc-xor )
312 ( ?d 2 calcFunc-diff )
313 ( ?n 1 calcFunc-not )
314 ( ?c 1 calcFunc-clip )
315 ( ?l 2 calcFunc-lsh )
316 ( ?r 2 calcFunc-rsh )
317 ( ?L 2 calcFunc-ash )
318 ( ?R 2 calcFunc-rash )
319 ( ?t 2 calcFunc-rot )
320 ( ?p 1 calcFunc-vpack )
321 ( ?u 1 calcFunc-vunpack )
322 ( ?D 4 calcFunc-ddb )
323 ( ?F 3 calcFunc-fv )
324 ( ?I 1 calcFunc-irr )
325 ( ?M 3 calcFunc-pmt )
326 ( ?N 2 calcFunc-npv )
327 ( ?P 3 calcFunc-pv )
328 ( ?S 3 calcFunc-sln )
329 ( ?T 3 calcFunc-rate )
330 ( ?Y 4 calcFunc-syd )
331 ( ?\# 3 calcFunc-nper )
332 ( ?\% 2 calcFunc-relch ) )
333 ( ( ?F 3 calcFunc-fvb )
334 ( ?I 1 calcFunc-irrb )
335 ( ?M 3 calcFunc-pmtb )
336 ( ?N 2 calcFunc-npvb )
337 ( ?P 3 calcFunc-pvb )
338 ( ?T 3 calcFunc-rateb )
339 ( ?\# 3 calcFunc-nperb ) )
340 ( ( ?F 3 calcFunc-fvl )
341 ( ?M 3 calcFunc-pmtl )
342 ( ?P 3 calcFunc-pvl )
343 ( ?T 3 calcFunc-ratel )
3132f345
CW
344 ( ?\# 3 calcFunc-nperl ) )))
345
136211a9
EZ
346(defconst calc-c-oper-keys '( ( ( ?d 1 calcFunc-deg )
347 ( ?r 1 calcFunc-rad )
348 ( ?h 1 calcFunc-hms )
349 ( ?f 1 calcFunc-float )
3132f345
CW
350 ( ?F 1 calcFunc-frac ) )))
351
136211a9
EZ
352(defconst calc-f-oper-keys '( ( ( ?b 2 calcFunc-beta )
353 ( ?e 1 calcFunc-erf )
354 ( ?g 1 calcFunc-gamma )
355 ( ?h 2 calcFunc-hypot )
356 ( ?i 1 calcFunc-im )
357 ( ?j 2 calcFunc-besJ )
358 ( ?n 2 calcFunc-min )
359 ( ?r 1 calcFunc-re )
360 ( ?s 1 calcFunc-sign )
361 ( ?x 2 calcFunc-max )
362 ( ?y 2 calcFunc-besY )
363 ( ?A 1 calcFunc-abssqr )
364 ( ?B 3 calcFunc-betaI )
365 ( ?E 1 calcFunc-expm1 )
366 ( ?G 2 calcFunc-gammaP )
367 ( ?I 2 calcFunc-ilog )
368 ( ?L 1 calcFunc-lnp1 )
369 ( ?M 1 calcFunc-mant )
370 ( ?Q 1 calcFunc-isqrt )
371 ( ?S 1 calcFunc-scf )
372 ( ?T 2 calcFunc-arctan2 )
373 ( ?X 1 calcFunc-xpon )
374 ( ?\[ 2 calcFunc-decr )
375 ( ?\] 2 calcFunc-incr ) )
376 ( ( ?e 1 calcFunc-erfc )
377 ( ?E 1 calcFunc-lnp1 )
378 ( ?G 2 calcFunc-gammaQ )
379 ( ?L 1 calcFunc-expm1 ) )
380 ( ( ?B 3 calcFunc-betaB )
381 ( ?G 2 calcFunc-gammag) )
3132f345
CW
382 ( ( ?G 2 calcFunc-gammaG ) )))
383
136211a9
EZ
384(defconst calc-k-oper-keys '( ( ( ?b 1 calcFunc-bern )
385 ( ?c 2 calcFunc-choose )
386 ( ?d 1 calcFunc-dfact )
387 ( ?e 1 calcFunc-euler )
388 ( ?f 1 calcFunc-prfac )
389 ( ?g 2 calcFunc-gcd )
390 ( ?h 2 calcFunc-shuffle )
391 ( ?l 2 calcFunc-lcm )
392 ( ?m 1 calcFunc-moebius )
393 ( ?n 1 calcFunc-nextprime )
394 ( ?r 1 calcFunc-random )
395 ( ?s 2 calcFunc-stir1 )
396 ( ?t 1 calcFunc-totient )
397 ( ?B 3 calcFunc-utpb )
398 ( ?C 2 calcFunc-utpc )
399 ( ?F 3 calcFunc-utpf )
400 ( ?N 3 calcFunc-utpn )
401 ( ?P 2 calcFunc-utpp )
402 ( ?T 2 calcFunc-utpt ) )
403 ( ( ?n 1 calcFunc-prevprime )
404 ( ?B 3 calcFunc-ltpb )
405 ( ?C 2 calcFunc-ltpc )
406 ( ?F 3 calcFunc-ltpf )
407 ( ?N 3 calcFunc-ltpn )
408 ( ?P 2 calcFunc-ltpp )
409 ( ?T 2 calcFunc-ltpt ) )
410 ( ( ?b 2 calcFunc-bern )
411 ( ?c 2 calcFunc-perm )
412 ( ?e 2 calcFunc-euler )
3132f345
CW
413 ( ?s 2 calcFunc-stir2 ) )))
414
136211a9 415(defconst calc-s-oper-keys '( ( ( ?: 2 calcFunc-assign )
3132f345
CW
416 ( ?= 1 calcFunc-evalto ) )))
417
136211a9
EZ
418(defconst calc-t-oper-keys '( ( ( ?C 3 calcFunc-tzconv )
419 ( ?D 1 calcFunc-date )
420 ( ?I 2 calcFunc-incmonth )
421 ( ?J 1 calcFunc-julian )
422 ( ?M 1 calcFunc-newmonth )
423 ( ?W 1 calcFunc-newweek )
424 ( ?U 1 calcFunc-unixtime )
3132f345
CW
425 ( ?Y 1 calcFunc-newyear ) )))
426
136211a9
EZ
427(defconst calc-u-oper-keys '( ( ( ?C 2 calcFunc-vcov )
428 ( ?G 1 calcFunc-vgmean )
429 ( ?M 1 calcFunc-vmean )
430 ( ?N 1 calcFunc-vmin )
431 ( ?S 1 calcFunc-vsdev )
432 ( ?X 1 calcFunc-vmax ) )
433 ( ( ?C 2 calcFunc-vpcov )
434 ( ?M 1 calcFunc-vmeane )
435 ( ?S 1 calcFunc-vpsdev ) )
436 ( ( ?C 2 calcFunc-vcorr )
437 ( ?G 1 calcFunc-agmean )
438 ( ?M 1 calcFunc-vmedian )
439 ( ?S 1 calcFunc-vvar ) )
440 ( ( ?M 1 calcFunc-vhmean )
3132f345
CW
441 ( ?S 1 calcFunc-vpvar ) )))
442
136211a9
EZ
443(defconst calc-v-oper-keys '( ( ( ?a 2 calcFunc-arrange )
444 ( ?b 2 calcFunc-cvec )
445 ( ?c 2 calcFunc-mcol )
446 ( ?d 2 calcFunc-diag )
447 ( ?e 2 calcFunc-vexp )
448 ( ?f 2 calcFunc-find )
449 ( ?h 1 calcFunc-head )
450 ( ?k 2 calcFunc-cons )
451 ( ?l 1 calcFunc-vlen )
452 ( ?m 2 calcFunc-vmask )
453 ( ?n 1 calcFunc-rnorm )
454 ( ?p 2 calcFunc-pack )
455 ( ?r 2 calcFunc-mrow )
456 ( ?s 3 calcFunc-subvec )
457 ( ?t 1 calcFunc-trn )
458 ( ?u 1 calcFunc-unpack )
459 ( ?v 1 calcFunc-rev )
460 ( ?x 1 calcFunc-index )
461 ( ?A 1 calcFunc-apply )
462 ( ?C 1 calcFunc-cross )
463 ( ?D 1 calcFunc-det )
464 ( ?E 1 calcFunc-venum )
465 ( ?F 1 calcFunc-vfloor )
466 ( ?G 1 calcFunc-grade )
467 ( ?H 2 calcFunc-histogram )
468 ( ?I 2 calcFunc-inner )
469 ( ?L 1 calcFunc-lud )
470 ( ?M 0 calcFunc-map )
471 ( ?N 1 calcFunc-cnorm )
472 ( ?O 2 calcFunc-outer )
473 ( ?R 1 calcFunc-reduce )
474 ( ?S 1 calcFunc-sort )
475 ( ?T 1 calcFunc-tr )
476 ( ?U 1 calcFunc-accum )
477 ( ?V 2 calcFunc-vunion )
478 ( ?X 2 calcFunc-vxor )
479 ( ?- 2 calcFunc-vdiff )
480 ( ?^ 2 calcFunc-vint )
481 ( ?~ 1 calcFunc-vcompl )
482 ( ?# 1 calcFunc-vcard )
483 ( ?: 1 calcFunc-vspan )
484 ( ?+ 1 calcFunc-rdup ) )
485 ( ( ?h 1 calcFunc-tail )
486 ( ?s 3 calcFunc-rsubvec )
487 ( ?G 1 calcFunc-rgrade )
488 ( ?R 1 calcFunc-rreduce )
489 ( ?S 1 calcFunc-rsort )
490 ( ?U 1 calcFunc-raccum ) )
491 ( ( ?e 3 calcFunc-vexp )
492 ( ?h 1 calcFunc-rhead )
493 ( ?k 2 calcFunc-rcons )
494 ( ?H 3 calcFunc-histogram )
495 ( ?R 2 calcFunc-nest )
496 ( ?U 2 calcFunc-anest ) )
497 ( ( ?h 1 calcFunc-rtail )
498 ( ?R 1 calcFunc-fixp )
3132f345
CW
499 ( ?U 1 calcFunc-afixp ) )))
500
501
502;;; Return a list of the form (nargs func name)
503(defun calc-get-operator (msg &optional nargs)
504 (setq calc-aborted-prefix nil)
505 (let ((inv nil) (hyp nil) (prefix nil) (forcenargs nil)
506 done key oper (which 0)
507 (msgs '( "(Press ? for help)"
508 "+, -, *, /, ^, %, \\, :, &, !, |, Neg"
509 "SHIFT + Abs, conJ, arG; maX, miN; Floor, Round; sQrt"
510 "SHIFT + Inv, Hyp; Sin, Cos, Tan; Exp, Ln, logB"
511 "Algebra + Simp, Esimp, Deriv, Integ, !, =, etc."
512 "Binary + And, Or, Xor, Diff; l/r/t/L/R shifts; Not, Clip"
513 "Conversions + Deg, Rad, HMS; Float; SHIFT + Fraction"
514 "Functions + Re, Im; Hypot; Mant, Expon, Scale; etc."
515 "Kombinatorics + Dfact, Lcm, Gcd, Choose; Random; etc."
516 "Time/date + newYear, Incmonth, etc."
517 "Vectors + Length, Row, Col, Diag, Mask, etc."
518 "_ = mapr/reducea, : = mapc/reduced, = = reducer"
519 "X or Z = any function by name; ' = alg entry; $ = stack")))
520 (while (not done)
521 (message "%s%s: %s: %s%s%s"
522 msg
523 (cond ((equal calc-mapping-dir "r") " rows")
524 ((equal calc-mapping-dir "c") " columns")
525 ((equal calc-mapping-dir "a") " across")
526 ((equal calc-mapping-dir "d") " down")
527 (t ""))
528 (if forcenargs
529 (format "(%d arg%s)"
530 forcenargs (if (= forcenargs 1) "" "s"))
531 (nth which msgs))
532 (if inv "Inv " "") (if hyp "Hyp " "")
533 (if prefix (concat (char-to-string prefix) "-") ""))
534 (setq key (read-char))
535 (if (>= key 128) (setq key (- key 128)))
536 (cond ((memq key '(?\C-g ?q))
537 (keyboard-quit))
538 ((memq key '(?\C-u ?\e)))
539 ((= key ??)
540 (setq which (% (1+ which) (length msgs))))
541 ((and (= key ?I) (null prefix))
542 (setq inv (not inv)))
543 ((and (= key ?H) (null prefix))
544 (setq hyp (not hyp)))
545 ((and (eq key prefix) (not (eq key ?v)))
546 (setq prefix nil))
547 ((and (memq key '(?a ?b ?c ?f ?k ?s ?t ?u ?v ?V))
548 (null prefix))
549 (setq prefix (downcase key)))
550 ((and (eq key ?\=) (null prefix))
551 (if calc-mapping-dir
552 (setq calc-mapping-dir (if (equal calc-mapping-dir "r")
553 "" "r"))
554 (beep)))
555 ((and (eq key ?\_) (null prefix))
556 (if calc-mapping-dir
557 (if (string-match "map$" msg)
558 (setq calc-mapping-dir (if (equal calc-mapping-dir "r")
559 "" "r"))
560 (setq calc-mapping-dir (if (equal calc-mapping-dir "a")
561 "" "a")))
562 (beep)))
563 ((and (eq key ?\:) (null prefix))
564 (if calc-mapping-dir
565 (if (string-match "map$" msg)
566 (setq calc-mapping-dir (if (equal calc-mapping-dir "c")
567 "" "c"))
568 (setq calc-mapping-dir (if (equal calc-mapping-dir "d")
569 "" "d")))
570 (beep)))
571 ((and (>= key ?0) (<= key ?9) (null prefix))
572 (setq forcenargs (if (eq forcenargs (- key ?0)) nil (- key ?0)))
573 (and nargs forcenargs (/= nargs forcenargs) (>= nargs 0)
574 (error "Must be a %d-argument operator" nargs)))
575 ((memq key '(?\$ ?\'))
576 (let* ((arglist nil)
577 (has-args nil)
578 (record-entry nil)
579 (expr (if (eq key ?\$)
580 (progn
581 (setq calc-dollar-used 1)
582 (if calc-dollar-values
583 (car calc-dollar-values)
584 (error "Stack underflow")))
585 (let* ((calc-dollar-values calc-arg-values)
586 (calc-dollar-used 0)
587 (calc-hashes-used 0)
588 (func (calc-do-alg-entry "" "Function: ")))
589 (setq record-entry t)
590 (or (= (length func) 1)
591 (error "Bad format"))
592 (if (> calc-dollar-used 0)
593 (progn
594 (setq has-args calc-dollar-used
595 arglist (calc-invent-args has-args))
596 (math-multi-subst (car func)
597 (reverse arglist)
598 arglist))
599 (if (> calc-hashes-used 0)
600 (setq has-args calc-hashes-used
601 arglist (calc-invent-args has-args)))
602 (car func))))))
603 (if (eq (car-safe expr) 'calcFunc-lambda)
604 (setq oper (list "$" (- (length expr) 2) expr)
605 done t)
606 (or has-args
607 (progn
608 (calc-default-formula-arglist expr)
609 (setq record-entry t
610 arglist (sort arglist 'string-lessp))
611 (if calc-verify-arglist
612 (setq arglist (read-from-minibuffer
613 "Function argument list: "
614 (if arglist
615 (prin1-to-string arglist)
616 "()")
617 minibuffer-local-map
618 t)))
619 (setq arglist (mapcar (function
620 (lambda (x)
621 (list 'var
622 x
623 (intern
624 (concat
625 "var-"
626 (symbol-name x))))))
627 arglist))))
628 (setq oper (list "$"
629 (length arglist)
630 (append '(calcFunc-lambda) arglist
631 (list expr)))
632 done t))
633 (if record-entry
634 (calc-record (nth 2 oper) "oper"))))
635 ((setq oper (assq key (nth (if inv (if hyp 3 1) (if hyp 2 0))
636 (if prefix
637 (symbol-value
638 (intern (format "calc-%c-oper-keys"
639 prefix)))
640 calc-oper-keys))))
641 (if (eq (nth 1 oper) 'user)
642 (let ((func (intern
643 (completing-read "Function name: "
644 obarray 'fboundp
645 nil "calcFunc-"))))
646 (if (or forcenargs nargs)
647 (setq oper (list "z" (or forcenargs nargs) func)
648 done t)
649 (if (fboundp func)
650 (let* ((defn (symbol-function func)))
651 (and (symbolp defn)
652 (setq defn (symbol-function defn)))
653 (if (eq (car-safe defn) 'lambda)
654 (let ((args (nth 1 defn))
655 (nargs 0))
656 (while (not (memq (car args) '(&optional
657 &rest nil)))
658 (setq nargs (1+ nargs)
659 args (cdr args)))
660 (setq oper (list "z" nargs func)
661 done t))
662 (error
663 "Function is not suitable for this operation")))
664 (message "Number of arguments: ")
665 (let ((nargs (read-char)))
666 (if (and (>= nargs ?0) (<= nargs ?9))
667 (setq oper (list "z" (- nargs ?0) func)
668 done t)
669 (beep))))))
670 (if (or (and (eq prefix ?v) (memq key '(?A ?I ?M ?O ?R ?U)))
671 (and (eq prefix ?a) (eq key ?M)))
672 (let* ((dir (cond ((and (equal calc-mapping-dir "")
673 (string-match "map$" msg))
674 (setq calc-mapping-dir "r")
675 " rows")
676 ((equal calc-mapping-dir "r") " rows")
677 ((equal calc-mapping-dir "c") " columns")
678 ((equal calc-mapping-dir "a") " across")
679 ((equal calc-mapping-dir "d") " down")
680 (t "")))
681 (calc-mapping-dir (and (memq (nth 2 oper)
682 '(calcFunc-map
683 calcFunc-reduce
684 calcFunc-rreduce))
685 ""))
686 (oper2 (calc-get-operator
687 (format "%s%s, %s%s" msg dir
688 (substring (symbol-name (nth 2 oper))
689 9)
690 (if (eq key ?I) " (mult)" ""))
691 (cdr (assq (nth 2 oper)
692 '((calcFunc-reduce . 2)
693 (calcFunc-rreduce . 2)
694 (calcFunc-accum . 2)
695 (calcFunc-raccum . 2)
696 (calcFunc-nest . 2)
697 (calcFunc-anest . 2)
698 (calcFunc-fixp . 2)
699 (calcFunc-afixp . 2))))))
700 (oper3 (if (eq (nth 2 oper) 'calcFunc-inner)
701 (calc-get-operator
ab5a1765 702 (format "%s%s, inner (add)" msg dir))
3132f345
CW
703 '(0 0 0)))
704 (args nil)
705 (nargs (if (> (nth 1 oper) 0)
706 (nth 1 oper)
707 (car oper2)))
708 (n nargs)
709 (p calc-arg-values))
710 (while (and p (> n 0))
711 (or (math-expr-contains (nth 1 oper2) (car p))
712 (math-expr-contains (nth 1 oper3) (car p))
713 (setq args (nconc args (list (car p)))
714 n (1- n)))
715 (setq p (cdr p)))
716 (setq oper (list "" nargs
717 (append
718 '(calcFunc-lambda)
719 args
720 (list (math-build-call
721 (intern
722 (concat
723 (symbol-name (nth 2 oper))
724 calc-mapping-dir))
725 (cons (math-calcFunc-to-var
726 (nth 1 oper2))
727 (if (eq key ?I)
728 (cons
729 (math-calcFunc-to-var
730 (nth 1 oper3))
731 args)
732 args))))))
733 done t))
734 (setq done t))))
735 (t (beep))))
736 (and nargs (>= nargs 0)
737 (/= nargs (nth 1 oper))
738 (error "Must be a %d-argument operator" nargs))
739 (append (if forcenargs
740 (cons forcenargs (cdr (cdr oper)))
741 (cdr oper))
742 (list
743 (let ((name (concat (if inv "I" "") (if hyp "H" "")
744 (if prefix (char-to-string prefix) "")
745 (char-to-string key))))
746 (if (> (length name) 3)
747 (substring name 0 3)
748 name))))))
136211a9
EZ
749
750
751;;; Convert a variable name (as a formula) into a like-looking function name.
752(defun math-var-to-calcFunc (f)
753 (if (eq (car-safe f) 'var)
754 (if (fboundp (nth 2 f))
755 (nth 2 f)
756 (intern (concat "calcFunc-" (symbol-name (nth 1 f)))))
757 (if (memq (car-safe f) '(lambda calcFunc-lambda))
758 f
bf77c646 759 (math-reject-arg f "*Expected a function name"))))
136211a9
EZ
760
761;;; Convert a function name into a like-looking variable name formula.
762(defun math-calcFunc-to-var (f)
763 (if (symbolp f)
764 (let* ((func (or (cdr (assq f '( ( + . calcFunc-add )
765 ( - . calcFunc-sub )
766 ( * . calcFunc-mul )
767 ( / . calcFunc-div )
768 ( ^ . calcFunc-pow )
769 ( % . calcFunc-mod )
770 ( neg . calcFunc-neg )
771 ( | . calcFunc-vconcat ) )))
772 f))
773 (base (if (string-match "\\`calcFunc-\\(.+\\)\\'"
774 (symbol-name func))
775 (math-match-substring (symbol-name func) 1)
776 (symbol-name func))))
777 (list 'var
778 (intern base)
779 (intern (concat "var-" base))))
bf77c646 780 f))
136211a9
EZ
781
782;;; Expand a function call using "lambda" notation.
783(defun math-build-call (f args)
784 (if (eq (car-safe f) 'calcFunc-lambda)
785 (if (= (length args) (- (length f) 2))
786 (math-multi-subst (nth (1- (length f)) f) (cdr f) args)
787 (calc-record-why "*Wrong number of arguments" f)
788 (cons 'calcFunc-call (cons (math-calcFunc-to-var f) args)))
789 (if (and (eq f 'calcFunc-neg)
790 (= (length args) 1))
791 (list 'neg (car args))
792 (let ((func (assq f '( ( calcFunc-add . + )
793 ( calcFunc-sub . - )
794 ( calcFunc-mul . * )
795 ( calcFunc-div . / )
796 ( calcFunc-pow . ^ )
797 ( calcFunc-mod . % )
798 ( calcFunc-vconcat . | ) ))))
799 (if (and func (= (length args) 2))
800 (cons (cdr func) args)
bf77c646 801 (cons f args))))))
136211a9
EZ
802
803;;; Do substitutions in parallel to avoid crosstalk.
8b31b519
JB
804
805;; The variables math-ms-temp and math-ms-args are local to
806;; math-multi-subst, but are used by math-multi-subst-rec, which
807;; is called by math-multi-subst.
808(defvar math-ms-temp)
809(defvar math-ms-args)
810
136211a9 811(defun math-multi-subst (expr olds news)
8b31b519
JB
812 (let ((math-ms-args nil)
813 math-ms-temp)
136211a9 814 (while (and olds news)
8b31b519 815 (setq math-ms-args (cons (cons (car olds) (car news)) math-ms-args)
136211a9
EZ
816 olds (cdr olds)
817 news (cdr news)))
bf77c646 818 (math-multi-subst-rec expr)))
136211a9
EZ
819
820(defun math-multi-subst-rec (expr)
8b31b519
JB
821 (cond ((setq math-ms-temp (assoc expr math-ms-args))
822 (cdr math-ms-temp))
136211a9
EZ
823 ((Math-primp expr) expr)
824 ((and (eq (car expr) 'calcFunc-lambda) (> (length expr) 2))
825 (let ((new (list (car expr)))
8b31b519 826 (math-ms-args math-ms-args))
136211a9
EZ
827 (while (cdr (setq expr (cdr expr)))
828 (setq new (cons (car expr) new))
8b31b519
JB
829 (if (assoc (car expr) math-ms-args)
830 (setq math-ms-args (cons (cons (car expr) (car expr))
831 math-ms-args))))
136211a9
EZ
832 (nreverse (cons (math-multi-subst-rec (car expr)) new))))
833 (t
834 (cons (car expr)
bf77c646 835 (mapcar 'math-multi-subst-rec (cdr expr))))))
136211a9
EZ
836
837(defun calcFunc-call (f &rest args)
838 (setq args (math-build-call (math-var-to-calcFunc f) args))
839 (if (eq (car-safe args) 'calcFunc-call)
840 args
bf77c646 841 (math-normalize args)))
136211a9
EZ
842
843(defun calcFunc-apply (f args)
844 (or (Math-vectorp args)
845 (math-reject-arg args 'vectorp))
bf77c646 846 (apply 'calcFunc-call (cons f (cdr args))))
136211a9
EZ
847
848
849
850
851;;; Map a function over a vector symbolically. [Public]
852(defun math-symb-map (f mode args)
853 (let* ((func (math-var-to-calcFunc f))
854 (nargs (length args))
855 (ptrs (vconcat args))
856 (vflags (make-vector nargs nil))
857 (heads '(vec))
858 (head nil)
859 (vec nil)
860 (i -1)
861 (math-working-step 0)
862 (math-working-step-2 nil)
863 len cols obj expr)
864 (if (eq mode 'eqn)
865 (setq mode 'elems
866 heads '(calcFunc-eq calcFunc-neq calcFunc-lt calcFunc-gt
867 calcFunc-leq calcFunc-geq))
868 (while (and (< (setq i (1+ i)) nargs)
869 (not (math-matrixp (aref ptrs i)))))
870 (if (< i nargs)
871 (if (eq mode 'elems)
872 (setq func (list 'lambda '(&rest x)
873 (list 'math-symb-map
874 (list 'quote f) '(quote elems) 'x))
875 mode 'rows)
876 (if (eq mode 'cols)
877 (while (< i nargs)
878 (if (math-matrixp (aref ptrs i))
879 (aset ptrs i (math-transpose (aref ptrs i))))
880 (setq i (1+ i)))))
881 (setq mode 'elems))
882 (setq i -1))
883 (while (< (setq i (1+ i)) nargs)
884 (setq obj (aref ptrs i))
885 (if (and (memq (car-safe obj) heads)
886 (or (eq mode 'elems)
887 (math-matrixp obj)))
888 (progn
889 (aset vflags i t)
890 (if head
891 (if (cdr heads)
892 (setq head (nth
893 (aref (aref [ [0 1 2 3 4 5]
894 [1 1 2 3 2 3]
895 [2 2 2 1 2 1]
896 [3 3 1 3 1 3]
897 [4 2 2 1 4 1]
898 [5 3 1 3 1 5] ]
899 (- 6 (length (memq head heads))))
900 (- 6 (length (memq (car obj) heads))))
901 heads)))
902 (setq head (car obj)))
903 (if len
904 (or (= (length obj) len)
905 (math-dimension-error))
906 (setq len (length obj))))))
907 (or len
908 (if (= nargs 1)
909 (math-reject-arg (aref ptrs 0) 'vectorp)
910 (math-reject-arg nil "At least one argument must be a vector")))
911 (setq math-working-step-2 (1- len))
912 (while (> (setq len (1- len)) 0)
913 (setq expr nil
914 i -1)
915 (while (< (setq i (1+ i)) nargs)
916 (if (aref vflags i)
917 (progn
918 (aset ptrs i (cdr (aref ptrs i)))
919 (setq expr (nconc expr (list (car (aref ptrs i))))))
920 (setq expr (nconc expr (list (aref ptrs i))))))
921 (setq math-working-step (1+ math-working-step)
922 vec (cons (math-normalize (math-build-call func expr)) vec)))
923 (setq vec (cons head (nreverse vec)))
924 (if (and (eq mode 'cols) (math-matrixp vec))
925 (math-transpose vec)
bf77c646 926 vec)))
136211a9
EZ
927
928(defun calcFunc-map (func &rest args)
bf77c646 929 (math-symb-map func 'elems args))
136211a9
EZ
930
931(defun calcFunc-mapr (func &rest args)
bf77c646 932 (math-symb-map func 'rows args))
136211a9
EZ
933
934(defun calcFunc-mapc (func &rest args)
bf77c646 935 (math-symb-map func 'cols args))
136211a9
EZ
936
937(defun calcFunc-mapa (func arg)
938 (if (math-matrixp arg)
939 (math-symb-map func 'elems (cdr (math-transpose arg)))
bf77c646 940 (math-symb-map func 'elems arg)))
136211a9
EZ
941
942(defun calcFunc-mapd (func arg)
943 (if (math-matrixp arg)
944 (math-symb-map func 'elems (cdr arg))
bf77c646 945 (math-symb-map func 'elems arg)))
136211a9
EZ
946
947(defun calcFunc-mapeq (func &rest args)
948 (if (and (or (equal func '(var mul var-mul))
949 (equal func '(var div var-div)))
950 (= (length args) 2))
951 (if (math-negp (car args))
952 (let ((func (nth 1 (assq (car-safe (nth 1 args))
953 calc-tweak-eqn-table))))
954 (and func (setq args (list (car args)
955 (cons func (cdr (nth 1 args)))))))
956 (if (math-negp (nth 1 args))
957 (let ((func (nth 1 (assq (car-safe (car args))
958 calc-tweak-eqn-table))))
959 (and func (setq args (list (cons func (cdr (car args)))
960 (nth 1 args))))))))
961 (if (or (and (equal func '(var div var-div))
962 (assq (car-safe (nth 1 args)) calc-tweak-eqn-table))
963 (equal func '(var neg var-neg))
964 (equal func '(var inv var-inv)))
965 (apply 'calcFunc-mapeqr func args)
bf77c646 966 (apply 'calcFunc-mapeqp func args)))
136211a9
EZ
967
968(defun calcFunc-mapeqr (func &rest args)
969 (setq args (mapcar (function (lambda (x)
970 (let ((func (assq (car-safe x)
971 calc-tweak-eqn-table)))
972 (if func
973 (cons (nth 1 func) (cdr x))
974 x))))
975 args))
bf77c646 976 (apply 'calcFunc-mapeqp func args))
136211a9
EZ
977
978(defun calcFunc-mapeqp (func &rest args)
979 (if (or (and (memq (car-safe (car args)) '(calcFunc-lt calcFunc-leq))
980 (memq (car-safe (nth 1 args)) '(calcFunc-gt calcFunc-geq)))
981 (and (memq (car-safe (car args)) '(calcFunc-gt calcFunc-geq))
982 (memq (car-safe (nth 1 args)) '(calcFunc-lt calcFunc-leq))))
983 (setq args (cons (car args)
984 (cons (list (nth 1 (assq (car (nth 1 args))
985 calc-tweak-eqn-table))
986 (nth 2 (nth 1 args))
987 (nth 1 (nth 1 args)))
988 (cdr (cdr args))))))
bf77c646 989 (math-symb-map func 'eqn args))
136211a9
EZ
990
991
992
993;;; Reduce a function over a vector symbolically. [Public]
994(defun calcFunc-reduce (func vec)
995 (if (math-matrixp vec)
996 (let (expr row)
997 (setq func (math-var-to-calcFunc func))
998 (while (setq vec (cdr vec))
999 (setq row (car vec))
1000 (while (setq row (cdr row))
1001 (setq expr (if expr
1002 (if (Math-numberp expr)
1003 (math-normalize
1004 (math-build-call func (list expr (car row))))
1005 (math-build-call func (list expr (car row))))
1006 (car row)))))
1007 (math-normalize expr))
bf77c646 1008 (calcFunc-reducer func vec)))
136211a9
EZ
1009
1010(defun calcFunc-rreduce (func vec)
1011 (if (math-matrixp vec)
1012 (let (expr row)
1013 (setq func (math-var-to-calcFunc func)
1014 vec (reverse (cdr vec)))
1015 (while vec
1016 (setq row (reverse (cdr (car vec))))
1017 (while row
1018 (setq expr (if expr
1019 (math-build-call func (list (car row) expr))
1020 (car row))
1021 row (cdr row)))
1022 (setq vec (cdr vec)))
1023 (math-normalize expr))
bf77c646 1024 (calcFunc-rreducer func vec)))
136211a9
EZ
1025
1026(defun calcFunc-reducer (func vec)
1027 (setq func (math-var-to-calcFunc func))
1028 (or (math-vectorp vec)
1029 (math-reject-arg vec 'vectorp))
1030 (let ((expr (car (setq vec (cdr vec)))))
1031 (if expr
1032 (progn
1033 (condition-case err
1034 (and (symbolp func)
1035 (let ((lfunc (or (cdr (assq func
1036 '( (calcFunc-add . math-add)
1037 (calcFunc-sub . math-sub)
1038 (calcFunc-mul . math-mul)
1039 (calcFunc-div . math-div)
1040 (calcFunc-pow . math-pow)
1041 (calcFunc-mod . math-mod)
1042 (calcFunc-vconcat .
1043 math-concat) )))
8b31b519 1044 func)))
136211a9
EZ
1045 (while (cdr vec)
1046 (setq expr (funcall lfunc expr (nth 1 vec))
1047 vec (cdr vec)))))
1048 (error nil))
1049 (while (setq vec (cdr vec))
1050 (setq expr (math-build-call func (list expr (car vec)))))
1051 (math-normalize expr))
1052 (or (math-identity-value func)
bf77c646 1053 (math-reject-arg vec "*Vector is empty")))))
136211a9
EZ
1054
1055(defun math-identity-value (func)
1056 (cdr (assq func '( (calcFunc-add . 0) (calcFunc-sub . 0)
1057 (calcFunc-mul . 1) (calcFunc-div . 1)
1058 (calcFunc-idiv . 1) (calcFunc-fdiv . 1)
1059 (calcFunc-min . (var inf var-inf))
1060 (calcFunc-max . (neg (var inf var-inf)))
1061 (calcFunc-vconcat . (vec))
bf77c646 1062 (calcFunc-append . (vec)) ))))
136211a9
EZ
1063
1064(defun calcFunc-rreducer (func vec)
1065 (setq func (math-var-to-calcFunc func))
1066 (or (math-vectorp vec)
1067 (math-reject-arg vec 'vectorp))
1068 (if (eq func 'calcFunc-sub) ; do this in a way that looks nicer
1069 (let ((expr (car (setq vec (cdr vec)))))
1070 (if expr
1071 (progn
1072 (while (setq vec (cdr vec))
1073 (setq expr (math-build-call func (list expr (car vec)))
1074 func (if (eq func 'calcFunc-sub)
1075 'calcFunc-add 'calcFunc-sub)))
1076 (math-normalize expr))
1077 0))
1078 (let ((expr (car (setq vec (reverse (cdr vec))))))
1079 (if expr
1080 (progn
1081 (while (setq vec (cdr vec))
1082 (setq expr (math-build-call func (list (car vec) expr))))
1083 (math-normalize expr))
1084 (or (math-identity-value func)
bf77c646 1085 (math-reject-arg vec "*Vector is empty"))))))
136211a9
EZ
1086
1087(defun calcFunc-reducec (func vec)
1088 (if (math-matrixp vec)
1089 (calcFunc-reducer func (math-transpose vec))
bf77c646 1090 (calcFunc-reducer func vec)))
136211a9
EZ
1091
1092(defun calcFunc-rreducec (func vec)
1093 (if (math-matrixp vec)
1094 (calcFunc-rreducer func (math-transpose vec))
bf77c646 1095 (calcFunc-rreducer func vec)))
136211a9
EZ
1096
1097(defun calcFunc-reducea (func vec)
1098 (if (math-matrixp vec)
1099 (cons 'vec
1100 (mapcar (function (lambda (x) (calcFunc-reducer func x)))
1101 (cdr vec)))
bf77c646 1102 (calcFunc-reducer func vec)))
136211a9
EZ
1103
1104(defun calcFunc-rreducea (func vec)
1105 (if (math-matrixp vec)
1106 (cons 'vec
1107 (mapcar (function (lambda (x) (calcFunc-rreducer func x)))
1108 (cdr vec)))
bf77c646 1109 (calcFunc-rreducer func vec)))
136211a9
EZ
1110
1111(defun calcFunc-reduced (func vec)
1112 (if (math-matrixp vec)
1113 (cons 'vec
1114 (mapcar (function (lambda (x) (calcFunc-reducer func x)))
1115 (cdr (math-transpose vec))))
bf77c646 1116 (calcFunc-reducer func vec)))
136211a9
EZ
1117
1118(defun calcFunc-rreduced (func vec)
1119 (if (math-matrixp vec)
1120 (cons 'vec
1121 (mapcar (function (lambda (x) (calcFunc-rreducer func x)))
1122 (cdr (math-transpose vec))))
bf77c646 1123 (calcFunc-rreducer func vec)))
136211a9
EZ
1124
1125(defun calcFunc-accum (func vec)
1126 (setq func (math-var-to-calcFunc func))
1127 (or (math-vectorp vec)
1128 (math-reject-arg vec 'vectorp))
1129 (let* ((expr (car (setq vec (cdr vec))))
1130 (res (list 'vec expr)))
1131 (or expr
1132 (math-reject-arg vec "*Vector is empty"))
1133 (while (setq vec (cdr vec))
1134 (setq expr (math-build-call func (list expr (car vec)))
1135 res (nconc res (list expr))))
bf77c646 1136 (math-normalize res)))
136211a9
EZ
1137
1138(defun calcFunc-raccum (func vec)
1139 (setq func (math-var-to-calcFunc func))
1140 (or (math-vectorp vec)
1141 (math-reject-arg vec 'vectorp))
1142 (let* ((expr (car (setq vec (reverse (cdr vec)))))
1143 (res (list expr)))
1144 (or expr
1145 (math-reject-arg vec "*Vector is empty"))
1146 (while (setq vec (cdr vec))
1147 (setq expr (math-build-call func (list (car vec) expr))
1148 res (cons (list expr) res)))
bf77c646 1149 (math-normalize (cons 'vec res))))
136211a9
EZ
1150
1151
1152(defun math-nest-calls (func base iters accum tol)
1153 (or (symbolp tol)
1154 (if (math-realp tol)
1155 (or (math-numberp base) (math-reject-arg base 'numberp))
1156 (math-reject-arg tol 'realp)))
1157 (setq func (math-var-to-calcFunc func))
1158 (or (null iters)
1159 (if (equal iters '(var inf var-inf))
1160 (setq iters nil)
1161 (progn
1162 (if (math-messy-integerp iters)
1163 (setq iters (math-trunc iters)))
1164 (or (integerp iters) (math-reject-arg iters 'fixnump))
1165 (or (not tol) (natnump iters) (math-reject-arg iters 'fixnatnump))
1166 (if (< iters 0)
1167 (let* ((dummy '(var DummyArg var-DummyArg))
1168 (dummy2 '(var DummyArg2 var-DummyArg2))
1169 (finv (math-solve-for (math-build-call func (list dummy2))
1170 dummy dummy2 nil)))
1171 (or finv (math-reject-arg nil "*Unable to find an inverse"))
1172 (if (and (= (length finv) 2)
1173 (equal (nth 1 finv) dummy))
1174 (setq func (car finv))
1175 (setq func (list 'calcFunc-lambda dummy finv)))
1176 (setq iters (- iters)))))))
1177 (math-with-extra-prec 1
1178 (let ((value base)
1179 (ovalue nil)
1180 (avalues (list base))
1181 (math-working-step 0)
1182 (math-working-step-2 iters))
1183 (while (and (or (null iters)
1184 (>= (setq iters (1- iters)) 0))
1185 (or (null tol)
1186 (null ovalue)
1187 (if (eq tol t)
1188 (not (if (and (Math-numberp value)
1189 (Math-numberp ovalue))
1190 (math-nearly-equal value ovalue)
1191 (Math-equal value ovalue)))
1192 (if (math-numberp value)
1193 (Math-lessp tol (math-abs (math-sub value ovalue)))
1194 (math-reject-arg value 'numberp)))))
1195 (setq ovalue value
1196 math-working-step (1+ math-working-step)
1197 value (math-normalize (math-build-call func (list value))))
1198 (if accum
1199 (setq avalues (cons value avalues))))
1200 (if accum
1201 (cons 'vec (nreverse avalues))
bf77c646 1202 value))))
136211a9
EZ
1203
1204(defun calcFunc-nest (func base iters)
bf77c646 1205 (math-nest-calls func base iters nil nil))
136211a9
EZ
1206
1207(defun calcFunc-anest (func base iters)
bf77c646 1208 (math-nest-calls func base iters t nil))
136211a9
EZ
1209
1210(defun calcFunc-fixp (func base &optional iters tol)
bf77c646 1211 (math-nest-calls func base iters nil (or tol t)))
136211a9
EZ
1212
1213(defun calcFunc-afixp (func base &optional iters tol)
bf77c646 1214 (math-nest-calls func base iters t (or tol t)))
136211a9
EZ
1215
1216
1217(defun calcFunc-outer (func a b)
1218 (or (math-vectorp a) (math-reject-arg a 'vectorp))
1219 (or (math-vectorp b) (math-reject-arg b 'vectorp))
1220 (setq func (math-var-to-calcFunc func))
1221 (let ((mat nil))
1222 (while (setq a (cdr a))
1223 (setq mat (cons (cons 'vec
1224 (mapcar (function (lambda (x)
1225 (math-build-call func
1226 (list (car a)
1227 x))))
1228 (cdr b)))
1229 mat)))
bf77c646 1230 (math-normalize (cons 'vec (nreverse mat)))))
136211a9
EZ
1231
1232
8b31b519
JB
1233;; The variables math-inner-mul-func and math-inner-add-func are
1234;; local to calcFunc-inner, but are used by math-inner-mats,
1235;; which is called by math-inner-mats.
1236(defvar math-inner-mul-func)
1237(defvar math-inner-add-func)
1238
1239(defun calcFunc-inner (math-inner-mul-func math-inner-add-func a b)
136211a9
EZ
1240 (or (math-vectorp a) (math-reject-arg a 'vectorp))
1241 (or (math-vectorp b) (math-reject-arg b 'vectorp))
1242 (if (math-matrixp a)
1243 (if (math-matrixp b)
1244 (if (= (length (nth 1 a)) (length b))
1245 (math-inner-mats a b)
1246 (math-dimension-error))
1247 (if (= (length (nth 1 a)) 2)
1248 (if (= (length a) (length b))
1249 (math-inner-mats a (list 'vec b))
1250 (math-dimension-error))
1251 (if (= (length (nth 1 a)) (length b))
1252 (math-mat-col (math-inner-mats a (math-col-matrix b))
1253 1)
1254 (math-dimension-error))))
1255 (if (math-matrixp b)
1256 (nth 1 (math-inner-mats (list 'vec a) b))
8b31b519 1257 (calcFunc-reduce math-inner-add-func (calcFunc-map math-inner-mul-func a b)))))
136211a9
EZ
1258
1259(defun math-inner-mats (a b)
1260 (let ((mat nil)
1261 (cols (length (nth 1 b)))
1262 row col ap bp accum)
1263 (while (setq a (cdr a))
1264 (setq col cols
1265 row nil)
1266 (while (> (setq col (1- col)) 0)
8b31b519
JB
1267 (setq row (cons (calcFunc-reduce math-inner-add-func
1268 (calcFunc-map math-inner-mul-func
136211a9
EZ
1269 (car a)
1270 (math-mat-col b col)))
1271 row)))
1272 (setq mat (cons (cons 'vec row) mat)))
bf77c646 1273 (cons 'vec (nreverse mat))))
136211a9 1274
ab5796a9 1275;;; arch-tag: 980eac49-00e0-4870-b72a-e726b74c7990
bf77c646 1276;;; calc-map.el ends here