1 ;;; calc-map.el --- higher-order functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2004 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainers: D. Goel <deego@gnufans.org>
7 ;; Colin Walters <walters@debian.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
30 ;; This file is autoloaded from calc-ext.el.
35 (defun calc-Need-calc-map () nil
)
38 (defun calc-apply (&optional oper
)
42 (calc-dollar-values (mapcar 'calc-get-stack-element
43 (nthcdr calc-stack-top calc-stack
)))
45 (oper (or oper
(calc-get-operator "Apply"
46 (if (math-vectorp (calc-top 1))
47 (1- (length (calc-top 1)))
49 (expr (calc-top-n (1+ calc-dollar-used
))))
50 (message "Working...")
51 (calc-set-command-flag 'clear-message
)
52 (calc-enter-result (1+ calc-dollar-used
)
53 (concat (substring "apl" 0 (- 4 (length (nth 2 oper
))))
56 (math-calcFunc-to-var (nth 1 oper
))
59 (defun calc-reduce (&optional oper accum
)
63 (nest (calc-is-hyperbolic))
64 (rev (calc-is-inverse))
65 (nargs (if (and nest
(not rev
)) 2 1))
66 (calc-dollar-values (mapcar 'calc-get-stack-element
67 (nthcdr calc-stack-top calc-stack
)))
69 (calc-mapping-dir (and (not accum
) (not nest
) ""))
70 (oper (or oper
(calc-get-operator
72 (concat (if accum
"Accumulate " "")
73 (if rev
"Fixed Point" "Nest"))
74 (concat (if rev
"Inv " "")
75 (if accum
"Accumulate" "Reduce")))
77 (message "Working...")
78 (calc-set-command-flag 'clear-message
)
79 (calc-enter-result (+ calc-dollar-used nargs
)
80 (concat (substring (if nest
82 (if accum
"acc" "red"))
83 0 (- 4 (length (nth 2 oper
))))
87 (if accum
'calcFunc-afixp
'calcFunc-fixp
)
88 (if accum
'calcFunc-anest
'calcFunc-nest
))
89 (cons (math-calcFunc-to-var (nth 1 oper
))
91 nargs
(1+ calc-dollar-used
))))
93 (if rev
'calcFunc-raccum
'calcFunc-accum
)
94 (intern (concat "calcFunc-"
98 (math-calcFunc-to-var (nth 1 oper
))
99 (calc-top-n (1+ calc-dollar-used
))))))))
101 (defun calc-accumulate (&optional oper
)
103 (calc-reduce oper t
))
105 (defun calc-map (&optional oper
)
108 (let* ((sel-mode nil
)
109 (calc-dollar-values (mapcar 'calc-get-stack-element
110 (nthcdr calc-stack-top calc-stack
)))
112 (calc-mapping-dir "")
113 (oper (or oper
(calc-get-operator "Map")))
115 (message "Working...")
116 (calc-set-command-flag 'clear-message
)
117 (calc-enter-result (+ nargs calc-dollar-used
)
118 (concat (substring "map" 0 (- 4 (length (nth 2 oper
))))
120 (cons (intern (concat "calcFunc-map" calc-mapping-dir
))
121 (cons (math-calcFunc-to-var (nth 1 oper
))
124 (1+ calc-dollar-used
))))))))
126 (defun calc-map-equation (&optional oper
)
129 (let* ((sel-mode nil
)
130 (calc-dollar-values (mapcar 'calc-get-stack-element
131 (nthcdr calc-stack-top calc-stack
)))
133 (oper (or oper
(calc-get-operator "Map-equation")))
135 (message "Working...")
136 (calc-set-command-flag 'clear-message
)
137 (calc-enter-result (+ nargs calc-dollar-used
)
138 (concat (substring "map" 0 (- 4 (length (nth 2 oper
))))
140 (cons (if (calc-is-inverse)
142 (if (calc-is-hyperbolic)
143 'calcFunc-mapeqp
'calcFunc-mapeq
))
144 (cons (math-calcFunc-to-var (nth 1 oper
))
147 (1+ calc-dollar-used
))))))))
149 (defvar calc-verify-arglist t
)
150 (defvar calc-mapping-dir nil
)
151 (defun calc-map-stack ()
152 "This is meant to be called by calc-keypad mode."
154 (let ((calc-verify-arglist nil
))
155 (calc-unread-command ?\$
)
158 (defun calc-outer-product (&optional oper
)
161 (let* ((sel-mode nil
)
162 (calc-dollar-values (mapcar 'calc-get-stack-element
163 (nthcdr calc-stack-top calc-stack
)))
165 (oper (or oper
(calc-get-operator "Outer" 2))))
166 (message "Working...")
167 (calc-set-command-flag 'clear-message
)
168 (calc-enter-result (+ 2 calc-dollar-used
)
169 (concat (substring "out" 0 (- 4 (length (nth 2 oper
))))
171 (cons 'calcFunc-outer
172 (cons (math-calcFunc-to-var (nth 1 oper
))
174 2 (1+ calc-dollar-used
))))))))
176 (defun calc-inner-product (&optional mul-oper add-oper
)
179 (let* ((sel-mode nil
)
180 (calc-dollar-values (mapcar 'calc-get-stack-element
181 (nthcdr calc-stack-top calc-stack
)))
183 (mul-oper (or mul-oper
(calc-get-operator "Inner (Mult)" 2)))
184 (mul-used calc-dollar-used
)
185 (calc-dollar-values (if (> mul-used
0)
186 (cdr calc-dollar-values
)
189 (add-oper (or add-oper
(calc-get-operator "Inner (Add)" 2))))
190 (message "Working...")
191 (calc-set-command-flag 'clear-message
)
192 (calc-enter-result (+ 2 mul-used calc-dollar-used
)
194 (substring (nth 2 mul-oper
) 0 1)
195 (substring (nth 2 add-oper
) 0 1))
196 (nconc (list 'calcFunc-inner
197 (math-calcFunc-to-var (nth 1 mul-oper
))
198 (math-calcFunc-to-var (nth 1 add-oper
)))
200 2 (+ 1 mul-used calc-dollar-used
)))))))
202 (defconst calc-oper-keys
'( ( ( ?
+ 2 calcFunc-add
)
203 ( ?-
2 calcFunc-sub
)
204 ( ?
* 2 calcFunc-mul
)
205 ( ?
/ 2 calcFunc-div
)
206 ( ?^
2 calcFunc-pow
)
207 ( ?|
2 calcFunc-vconcat
)
208 ( ?%
2 calcFunc-mod
)
209 ( ?
\\ 2 calcFunc-idiv
)
210 ( ?
! 1 calcFunc-fact
)
211 ( ?
& 1 calcFunc-inv
)
212 ( ?n
1 calcFunc-neg
)
215 ( ?A
1 calcFunc-abs
)
216 ( ?J
1 calcFunc-conj
)
217 ( ?G
1 calcFunc-arg
)
218 ( ?Q
1 calcFunc-sqrt
)
219 ( ?N
2 calcFunc-min
)
220 ( ?X
2 calcFunc-max
)
221 ( ?F
1 calcFunc-floor
)
222 ( ?R
1 calcFunc-round
)
223 ( ?S
1 calcFunc-sin
)
224 ( ?C
1 calcFunc-cos
)
225 ( ?T
1 calcFunc-tan
)
227 ( ?E
1 calcFunc-exp
)
228 ( ?B
2 calcFunc-log
) )
229 ( ( ?F
1 calcFunc-ceil
) ; inverse
230 ( ?R
1 calcFunc-trunc
)
231 ( ?Q
1 calcFunc-sqr
)
232 ( ?S
1 calcFunc-arcsin
)
233 ( ?C
1 calcFunc-arccos
)
234 ( ?T
1 calcFunc-arctan
)
235 ( ?L
1 calcFunc-exp
)
237 ( ?B
2 calcFunc-alog
)
238 ( ?^
2 calcFunc-nroot
)
239 ( ?|
2 calcFunc-vconcatrev
) )
240 ( ( ?F
1 calcFunc-ffloor
) ; hyperbolic
241 ( ?R
1 calcFunc-fround
)
242 ( ?S
1 calcFunc-sinh
)
243 ( ?C
1 calcFunc-cosh
)
244 ( ?T
1 calcFunc-tanh
)
245 ( ?L
1 calcFunc-log10
)
246 ( ?E
1 calcFunc-exp10
)
247 ( ?|
2 calcFunc-append
) )
248 ( ( ?F
1 calcFunc-fceil
) ; inverse-hyperbolic
249 ( ?R
1 calcFunc-ftrunc
)
250 ( ?S
1 calcFunc-arcsinh
)
251 ( ?C
1 calcFunc-arccosh
)
252 ( ?T
1 calcFunc-arctanh
)
253 ( ?L
1 calcFunc-exp10
)
254 ( ?E
1 calcFunc-log10
)
255 ( ?|
2 calcFunc-appendrev
) )))
257 (defconst calc-a-oper-keys
'( ( ( ?a
3 calcFunc-apart
)
258 ( ?b
3 calcFunc-subst
)
259 ( ?c
2 calcFunc-collect
)
260 ( ?d
2 calcFunc-deriv
)
261 ( ?e
1 calcFunc-esimplify
)
262 ( ?f
2 calcFunc-factor
)
263 ( ?g
2 calcFunc-pgcd
)
264 ( ?i
2 calcFunc-integ
)
265 ( ?m
2 calcFunc-match
)
266 ( ?n
1 calcFunc-nrat
)
267 ( ?r
2 calcFunc-rewrite
)
268 ( ?s
1 calcFunc-simplify
)
269 ( ?t
3 calcFunc-taylor
)
270 ( ?x
1 calcFunc-expand
)
271 ( ?M
2 calcFunc-mapeq
)
272 ( ?N
3 calcFunc-minimize
)
273 ( ?P
2 calcFunc-roots
)
274 ( ?R
3 calcFunc-root
)
275 ( ?S
2 calcFunc-solve
)
276 ( ?T
4 calcFunc-table
)
277 ( ?X
3 calcFunc-maximize
)
279 ( ?\
# 2 calcFunc-neq
)
282 ( ?\
[ 2 calcFunc-leq
)
283 ( ?\
] 2 calcFunc-geq
)
285 ( ?
! 1 calcFunc-lnot
)
286 ( ?
& 2 calcFunc-land
)
287 ( ?\|
2 calcFunc-lor
)
289 ( ?.
2 calcFunc-rmeq
)
290 ( ?
+ 4 calcFunc-sum
)
291 ( ?-
4 calcFunc-asum
)
292 ( ?
* 4 calcFunc-prod
)
293 ( ?_
2 calcFunc-subscr
)
294 ( ?
\\ 2 calcFunc-pdiv
)
295 ( ?%
2 calcFunc-prem
)
296 ( ?
/ 2 calcFunc-pdivrem
) )
297 ( ( ?m
2 calcFunc-matchnot
)
298 ( ?M
2 calcFunc-mapeqr
)
299 ( ?S
2 calcFunc-finv
) )
300 ( ( ?d
2 calcFunc-tderiv
)
301 ( ?f
2 calcFunc-factors
)
302 ( ?M
2 calcFunc-mapeqp
)
303 ( ?N
3 calcFunc-wminimize
)
304 ( ?R
3 calcFunc-wroot
)
305 ( ?S
2 calcFunc-fsolve
)
306 ( ?X
3 calcFunc-wmaximize
)
307 ( ?
/ 2 calcFunc-pdivide
) )
308 ( ( ?S
2 calcFunc-ffinv
) )))
310 (defconst calc-b-oper-keys
'( ( ( ?a
2 calcFunc-and
)
312 ( ?x
2 calcFunc-xor
)
313 ( ?d
2 calcFunc-diff
)
314 ( ?n
1 calcFunc-not
)
315 ( ?c
1 calcFunc-clip
)
316 ( ?l
2 calcFunc-lsh
)
317 ( ?r
2 calcFunc-rsh
)
318 ( ?L
2 calcFunc-ash
)
319 ( ?R
2 calcFunc-rash
)
320 ( ?t
2 calcFunc-rot
)
321 ( ?p
1 calcFunc-vpack
)
322 ( ?u
1 calcFunc-vunpack
)
323 ( ?D
4 calcFunc-ddb
)
325 ( ?I
1 calcFunc-irr
)
326 ( ?M
3 calcFunc-pmt
)
327 ( ?N
2 calcFunc-npv
)
329 ( ?S
3 calcFunc-sln
)
330 ( ?T
3 calcFunc-rate
)
331 ( ?Y
4 calcFunc-syd
)
332 ( ?\
# 3 calcFunc-nper
)
333 ( ?\%
2 calcFunc-relch
) )
334 ( ( ?F
3 calcFunc-fvb
)
335 ( ?I
1 calcFunc-irrb
)
336 ( ?M
3 calcFunc-pmtb
)
337 ( ?N
2 calcFunc-npvb
)
338 ( ?P
3 calcFunc-pvb
)
339 ( ?T
3 calcFunc-rateb
)
340 ( ?\
# 3 calcFunc-nperb
) )
341 ( ( ?F
3 calcFunc-fvl
)
342 ( ?M
3 calcFunc-pmtl
)
343 ( ?P
3 calcFunc-pvl
)
344 ( ?T
3 calcFunc-ratel
)
345 ( ?\
# 3 calcFunc-nperl
) )))
347 (defconst calc-c-oper-keys
'( ( ( ?d
1 calcFunc-deg
)
348 ( ?r
1 calcFunc-rad
)
349 ( ?h
1 calcFunc-hms
)
350 ( ?f
1 calcFunc-float
)
351 ( ?F
1 calcFunc-frac
) )))
353 (defconst calc-f-oper-keys
'( ( ( ?b
2 calcFunc-beta
)
354 ( ?e
1 calcFunc-erf
)
355 ( ?g
1 calcFunc-gamma
)
356 ( ?h
2 calcFunc-hypot
)
358 ( ?j
2 calcFunc-besJ
)
359 ( ?n
2 calcFunc-min
)
361 ( ?s
1 calcFunc-sign
)
362 ( ?x
2 calcFunc-max
)
363 ( ?y
2 calcFunc-besY
)
364 ( ?A
1 calcFunc-abssqr
)
365 ( ?B
3 calcFunc-betaI
)
366 ( ?E
1 calcFunc-expm1
)
367 ( ?G
2 calcFunc-gammaP
)
368 ( ?I
2 calcFunc-ilog
)
369 ( ?L
1 calcFunc-lnp1
)
370 ( ?M
1 calcFunc-mant
)
371 ( ?Q
1 calcFunc-isqrt
)
372 ( ?S
1 calcFunc-scf
)
373 ( ?T
2 calcFunc-arctan2
)
374 ( ?X
1 calcFunc-xpon
)
375 ( ?\
[ 2 calcFunc-decr
)
376 ( ?\
] 2 calcFunc-incr
) )
377 ( ( ?e
1 calcFunc-erfc
)
378 ( ?E
1 calcFunc-lnp1
)
379 ( ?G
2 calcFunc-gammaQ
)
380 ( ?L
1 calcFunc-expm1
) )
381 ( ( ?B
3 calcFunc-betaB
)
382 ( ?G
2 calcFunc-gammag
) )
383 ( ( ?G
2 calcFunc-gammaG
) )))
385 (defconst calc-k-oper-keys
'( ( ( ?b
1 calcFunc-bern
)
386 ( ?c
2 calcFunc-choose
)
387 ( ?d
1 calcFunc-dfact
)
388 ( ?e
1 calcFunc-euler
)
389 ( ?f
1 calcFunc-prfac
)
390 ( ?g
2 calcFunc-gcd
)
391 ( ?h
2 calcFunc-shuffle
)
392 ( ?l
2 calcFunc-lcm
)
393 ( ?m
1 calcFunc-moebius
)
394 ( ?n
1 calcFunc-nextprime
)
395 ( ?r
1 calcFunc-random
)
396 ( ?s
2 calcFunc-stir1
)
397 ( ?t
1 calcFunc-totient
)
398 ( ?B
3 calcFunc-utpb
)
399 ( ?C
2 calcFunc-utpc
)
400 ( ?F
3 calcFunc-utpf
)
401 ( ?N
3 calcFunc-utpn
)
402 ( ?P
2 calcFunc-utpp
)
403 ( ?T
2 calcFunc-utpt
) )
404 ( ( ?n
1 calcFunc-prevprime
)
405 ( ?B
3 calcFunc-ltpb
)
406 ( ?C
2 calcFunc-ltpc
)
407 ( ?F
3 calcFunc-ltpf
)
408 ( ?N
3 calcFunc-ltpn
)
409 ( ?P
2 calcFunc-ltpp
)
410 ( ?T
2 calcFunc-ltpt
) )
411 ( ( ?b
2 calcFunc-bern
)
412 ( ?c
2 calcFunc-perm
)
413 ( ?e
2 calcFunc-euler
)
414 ( ?s
2 calcFunc-stir2
) )))
416 (defconst calc-s-oper-keys
'( ( ( ?
: 2 calcFunc-assign
)
417 ( ?
= 1 calcFunc-evalto
) )))
419 (defconst calc-t-oper-keys
'( ( ( ?C
3 calcFunc-tzconv
)
420 ( ?D
1 calcFunc-date
)
421 ( ?I
2 calcFunc-incmonth
)
422 ( ?J
1 calcFunc-julian
)
423 ( ?M
1 calcFunc-newmonth
)
424 ( ?W
1 calcFunc-newweek
)
425 ( ?U
1 calcFunc-unixtime
)
426 ( ?Y
1 calcFunc-newyear
) )))
428 (defconst calc-u-oper-keys
'( ( ( ?C
2 calcFunc-vcov
)
429 ( ?G
1 calcFunc-vgmean
)
430 ( ?M
1 calcFunc-vmean
)
431 ( ?N
1 calcFunc-vmin
)
432 ( ?S
1 calcFunc-vsdev
)
433 ( ?X
1 calcFunc-vmax
) )
434 ( ( ?C
2 calcFunc-vpcov
)
435 ( ?M
1 calcFunc-vmeane
)
436 ( ?S
1 calcFunc-vpsdev
) )
437 ( ( ?C
2 calcFunc-vcorr
)
438 ( ?G
1 calcFunc-agmean
)
439 ( ?M
1 calcFunc-vmedian
)
440 ( ?S
1 calcFunc-vvar
) )
441 ( ( ?M
1 calcFunc-vhmean
)
442 ( ?S
1 calcFunc-vpvar
) )))
444 (defconst calc-v-oper-keys
'( ( ( ?a
2 calcFunc-arrange
)
445 ( ?b
2 calcFunc-cvec
)
446 ( ?c
2 calcFunc-mcol
)
447 ( ?d
2 calcFunc-diag
)
448 ( ?e
2 calcFunc-vexp
)
449 ( ?f
2 calcFunc-find
)
450 ( ?h
1 calcFunc-head
)
451 ( ?k
2 calcFunc-cons
)
452 ( ?l
1 calcFunc-vlen
)
453 ( ?m
2 calcFunc-vmask
)
454 ( ?n
1 calcFunc-rnorm
)
455 ( ?p
2 calcFunc-pack
)
456 ( ?r
2 calcFunc-mrow
)
457 ( ?s
3 calcFunc-subvec
)
458 ( ?t
1 calcFunc-trn
)
459 ( ?u
1 calcFunc-unpack
)
460 ( ?v
1 calcFunc-rev
)
461 ( ?x
1 calcFunc-index
)
462 ( ?A
1 calcFunc-apply
)
463 ( ?C
1 calcFunc-cross
)
464 ( ?D
1 calcFunc-det
)
465 ( ?E
1 calcFunc-venum
)
466 ( ?F
1 calcFunc-vfloor
)
467 ( ?G
1 calcFunc-grade
)
468 ( ?H
2 calcFunc-histogram
)
469 ( ?I
2 calcFunc-inner
)
470 ( ?L
1 calcFunc-lud
)
471 ( ?M
0 calcFunc-map
)
472 ( ?N
1 calcFunc-cnorm
)
473 ( ?O
2 calcFunc-outer
)
474 ( ?R
1 calcFunc-reduce
)
475 ( ?S
1 calcFunc-sort
)
477 ( ?U
1 calcFunc-accum
)
478 ( ?V
2 calcFunc-vunion
)
479 ( ?X
2 calcFunc-vxor
)
480 ( ?-
2 calcFunc-vdiff
)
481 ( ?^
2 calcFunc-vint
)
482 ( ?~
1 calcFunc-vcompl
)
483 ( ?
# 1 calcFunc-vcard
)
484 ( ?
: 1 calcFunc-vspan
)
485 ( ?
+ 1 calcFunc-rdup
) )
486 ( ( ?h
1 calcFunc-tail
)
487 ( ?s
3 calcFunc-rsubvec
)
488 ( ?G
1 calcFunc-rgrade
)
489 ( ?R
1 calcFunc-rreduce
)
490 ( ?S
1 calcFunc-rsort
)
491 ( ?U
1 calcFunc-raccum
) )
492 ( ( ?e
3 calcFunc-vexp
)
493 ( ?h
1 calcFunc-rhead
)
494 ( ?k
2 calcFunc-rcons
)
495 ( ?H
3 calcFunc-histogram
)
496 ( ?R
2 calcFunc-nest
)
497 ( ?U
2 calcFunc-anest
) )
498 ( ( ?h
1 calcFunc-rtail
)
499 ( ?R
1 calcFunc-fixp
)
500 ( ?U
1 calcFunc-afixp
) )))
503 ;;; Return a list of the form (nargs func name)
504 (defun calc-get-operator (msg &optional nargs
)
505 (setq calc-aborted-prefix nil
)
506 (let ((inv nil
) (hyp nil
) (prefix nil
) (forcenargs nil
)
507 done key oper
(which 0)
508 (msgs '( "(Press ? for help)"
509 "+, -, *, /, ^, %, \\, :, &, !, |, Neg"
510 "SHIFT + Abs, conJ, arG; maX, miN; Floor, Round; sQrt"
511 "SHIFT + Inv, Hyp; Sin, Cos, Tan; Exp, Ln, logB"
512 "Algebra + Simp, Esimp, Deriv, Integ, !, =, etc."
513 "Binary + And, Or, Xor, Diff; l/r/t/L/R shifts; Not, Clip"
514 "Conversions + Deg, Rad, HMS; Float; SHIFT + Fraction"
515 "Functions + Re, Im; Hypot; Mant, Expon, Scale; etc."
516 "Kombinatorics + Dfact, Lcm, Gcd, Choose; Random; etc."
517 "Time/date + newYear, Incmonth, etc."
518 "Vectors + Length, Row, Col, Diag, Mask, etc."
519 "_ = mapr/reducea, : = mapc/reduced, = = reducer"
520 "X or Z = any function by name; ' = alg entry; $ = stack")))
522 (message "%s%s: %s: %s%s%s"
524 (cond ((equal calc-mapping-dir
"r") " rows")
525 ((equal calc-mapping-dir
"c") " columns")
526 ((equal calc-mapping-dir
"a") " across")
527 ((equal calc-mapping-dir
"d") " down")
531 forcenargs
(if (= forcenargs
1) "" "s"))
533 (if inv
"Inv " "") (if hyp
"Hyp " "")
534 (if prefix
(concat (char-to-string prefix
) "-") ""))
535 (setq key
(read-char))
536 (if (>= key
128) (setq key
(- key
128)))
537 (cond ((memq key
'(?\C-g ?q
))
539 ((memq key
'(?\C-u ?\e
)))
541 (setq which
(%
(1+ which
) (length msgs
))))
542 ((and (= key ?I
) (null prefix
))
543 (setq inv
(not inv
)))
544 ((and (= key ?H
) (null prefix
))
545 (setq hyp
(not hyp
)))
546 ((and (eq key prefix
) (not (eq key ?v
)))
548 ((and (memq key
'(?a ?b ?c ?f ?k ?s ?t ?u ?v ?V
))
550 (setq prefix
(downcase key
)))
551 ((and (eq key ?\
=) (null prefix
))
553 (setq calc-mapping-dir
(if (equal calc-mapping-dir
"r")
556 ((and (eq key ?\_
) (null prefix
))
558 (if (string-match "map$" msg
)
559 (setq calc-mapping-dir
(if (equal calc-mapping-dir
"r")
561 (setq calc-mapping-dir
(if (equal calc-mapping-dir
"a")
564 ((and (eq key ?\
:) (null prefix
))
566 (if (string-match "map$" msg
)
567 (setq calc-mapping-dir
(if (equal calc-mapping-dir
"c")
569 (setq calc-mapping-dir
(if (equal calc-mapping-dir
"d")
572 ((and (>= key ?
0) (<= key ?
9) (null prefix
))
573 (setq forcenargs
(if (eq forcenargs
(- key ?
0)) nil
(- key ?
0)))
574 (and nargs forcenargs
(/= nargs forcenargs
) (>= nargs
0)
575 (error "Must be a %d-argument operator" nargs
)))
576 ((memq key
'(?\$ ?
\'))
580 (expr (if (eq key ?\$
)
582 (setq calc-dollar-used
1)
583 (if calc-dollar-values
584 (car calc-dollar-values
)
585 (error "Stack underflow")))
586 (let* ((calc-dollar-values calc-arg-values
)
589 (func (calc-do-alg-entry "" "Function: ")))
590 (setq record-entry t
)
591 (or (= (length func
) 1)
592 (error "Bad format"))
593 (if (> calc-dollar-used
0)
595 (setq has-args calc-dollar-used
596 arglist
(calc-invent-args has-args
))
597 (math-multi-subst (car func
)
600 (if (> calc-hashes-used
0)
601 (setq has-args calc-hashes-used
602 arglist
(calc-invent-args has-args
)))
604 (if (eq (car-safe expr
) 'calcFunc-lambda
)
605 (setq oper
(list "$" (- (length expr
) 2) expr
)
609 (calc-default-formula-arglist expr
)
611 arglist
(sort arglist
'string-lessp
))
612 (if calc-verify-arglist
613 (setq arglist
(read-from-minibuffer
614 "Function argument list: "
616 (prin1-to-string arglist
)
620 (setq arglist
(mapcar (function
631 (append '(calcFunc-lambda) arglist
635 (calc-record (nth 2 oper
) "oper"))))
636 ((setq oper
(assq key
(nth (if inv
(if hyp
3 1) (if hyp
2 0))
639 (intern (format "calc-%c-oper-keys"
642 (if (eq (nth 1 oper
) 'user
)
644 (completing-read "Function name: "
647 (if (or forcenargs nargs
)
648 (setq oper
(list "z" (or forcenargs nargs
) func
)
651 (let* ((defn (symbol-function func
)))
653 (setq defn
(symbol-function defn
)))
654 (if (eq (car-safe defn
) 'lambda
)
655 (let ((args (nth 1 defn
))
657 (while (not (memq (car args
) '(&optional
659 (setq nargs
(1+ nargs
)
661 (setq oper
(list "z" nargs func
)
664 "Function is not suitable for this operation")))
665 (message "Number of arguments: ")
666 (let ((nargs (read-char)))
667 (if (and (>= nargs ?
0) (<= nargs ?
9))
668 (setq oper
(list "z" (- nargs ?
0) func
)
671 (if (or (and (eq prefix ?v
) (memq key
'(?A ?I ?M ?O ?R ?U
)))
672 (and (eq prefix ?a
) (eq key ?M
)))
673 (let* ((dir (cond ((and (equal calc-mapping-dir
"")
674 (string-match "map$" msg
))
675 (setq calc-mapping-dir
"r")
677 ((equal calc-mapping-dir
"r") " rows")
678 ((equal calc-mapping-dir
"c") " columns")
679 ((equal calc-mapping-dir
"a") " across")
680 ((equal calc-mapping-dir
"d") " down")
682 (calc-mapping-dir (and (memq (nth 2 oper
)
687 (oper2 (calc-get-operator
688 (format "%s%s, %s%s" msg dir
689 (substring (symbol-name (nth 2 oper
))
691 (if (eq key ?I
) " (mult)" ""))
692 (cdr (assq (nth 2 oper
)
693 '((calcFunc-reduce .
2)
694 (calcFunc-rreduce .
2)
696 (calcFunc-raccum .
2)
700 (calcFunc-afixp .
2))))))
701 (oper3 (if (eq (nth 2 oper
) 'calcFunc-inner
)
703 (format "%s%s, inner (add)" msg dir
))
706 (nargs (if (> (nth 1 oper
) 0)
711 (while (and p
(> n
0))
712 (or (math-expr-contains (nth 1 oper2
) (car p
))
713 (math-expr-contains (nth 1 oper3
) (car p
))
714 (setq args
(nconc args
(list (car p
)))
717 (setq oper
(list "" nargs
721 (list (math-build-call
724 (symbol-name (nth 2 oper
))
726 (cons (math-calcFunc-to-var
730 (math-calcFunc-to-var
737 (and nargs
(>= nargs
0)
738 (/= nargs
(nth 1 oper
))
739 (error "Must be a %d-argument operator" nargs
))
740 (append (if forcenargs
741 (cons forcenargs
(cdr (cdr oper
)))
744 (let ((name (concat (if inv
"I" "") (if hyp
"H" "")
745 (if prefix
(char-to-string prefix
) "")
746 (char-to-string key
))))
747 (if (> (length name
) 3)
752 ;;; Convert a variable name (as a formula) into a like-looking function name.
753 (defun math-var-to-calcFunc (f)
754 (if (eq (car-safe f
) 'var
)
755 (if (fboundp (nth 2 f
))
757 (intern (concat "calcFunc-" (symbol-name (nth 1 f
)))))
758 (if (memq (car-safe f
) '(lambda calcFunc-lambda
))
760 (math-reject-arg f
"*Expected a function name"))))
762 ;;; Convert a function name into a like-looking variable name formula.
763 (defun math-calcFunc-to-var (f)
765 (let* ((func (or (cdr (assq f
'( ( + . calcFunc-add
)
771 ( neg . calcFunc-neg
)
772 ( | . calcFunc-vconcat
) )))
774 (base (if (string-match "\\`calcFunc-\\(.+\\)\\'"
776 (math-match-substring (symbol-name func
) 1)
777 (symbol-name func
))))
780 (intern (concat "var-" base
))))
783 ;;; Expand a function call using "lambda" notation.
784 (defun math-build-call (f args
)
785 (if (eq (car-safe f
) 'calcFunc-lambda
)
786 (if (= (length args
) (- (length f
) 2))
787 (math-multi-subst (nth (1- (length f
)) f
) (cdr f
) args
)
788 (calc-record-why "*Wrong number of arguments" f
)
789 (cons 'calcFunc-call
(cons (math-calcFunc-to-var f
) args
)))
790 (if (and (eq f
'calcFunc-neg
)
792 (list 'neg
(car args
))
793 (let ((func (assq f
'( ( calcFunc-add .
+ )
799 ( calcFunc-vconcat . |
) ))))
800 (if (and func
(= (length args
) 2))
801 (cons (cdr func
) args
)
804 ;;; Do substitutions in parallel to avoid crosstalk.
805 (defun math-multi-subst (expr olds news
)
808 (while (and olds news
)
809 (setq args
(cons (cons (car olds
) (car news
)) args
)
812 (math-multi-subst-rec expr
)))
814 (defun math-multi-subst-rec (expr)
815 (cond ((setq temp
(assoc expr args
)) (cdr temp
))
816 ((Math-primp expr
) expr
)
817 ((and (eq (car expr
) 'calcFunc-lambda
) (> (length expr
) 2))
818 (let ((new (list (car expr
)))
820 (while (cdr (setq expr
(cdr expr
)))
821 (setq new
(cons (car expr
) new
))
822 (if (assoc (car expr
) args
)
823 (setq args
(cons (cons (car expr
) (car expr
)) args
))))
824 (nreverse (cons (math-multi-subst-rec (car expr
)) new
))))
827 (mapcar 'math-multi-subst-rec
(cdr expr
))))))
829 (defun calcFunc-call (f &rest args
)
830 (setq args
(math-build-call (math-var-to-calcFunc f
) args
))
831 (if (eq (car-safe args
) 'calcFunc-call
)
833 (math-normalize args
)))
835 (defun calcFunc-apply (f args
)
836 (or (Math-vectorp args
)
837 (math-reject-arg args
'vectorp
))
838 (apply 'calcFunc-call
(cons f
(cdr args
))))
843 ;;; Map a function over a vector symbolically. [Public]
844 (defun math-symb-map (f mode args
)
845 (let* ((func (math-var-to-calcFunc f
))
846 (nargs (length args
))
847 (ptrs (vconcat args
))
848 (vflags (make-vector nargs nil
))
853 (math-working-step 0)
854 (math-working-step-2 nil
)
858 heads
'(calcFunc-eq calcFunc-neq calcFunc-lt calcFunc-gt
859 calcFunc-leq calcFunc-geq
))
860 (while (and (< (setq i
(1+ i
)) nargs
)
861 (not (math-matrixp (aref ptrs i
)))))
864 (setq func
(list 'lambda
'(&rest x
)
866 (list 'quote f
) '(quote elems
) 'x
))
870 (if (math-matrixp (aref ptrs i
))
871 (aset ptrs i
(math-transpose (aref ptrs i
))))
875 (while (< (setq i
(1+ i
)) nargs
)
876 (setq obj
(aref ptrs i
))
877 (if (and (memq (car-safe obj
) heads
)
885 (aref (aref [ [0 1 2 3 4 5]
891 (- 6 (length (memq head heads
))))
892 (- 6 (length (memq (car obj
) heads
))))
894 (setq head
(car obj
)))
896 (or (= (length obj
) len
)
897 (math-dimension-error))
898 (setq len
(length obj
))))))
901 (math-reject-arg (aref ptrs
0) 'vectorp
)
902 (math-reject-arg nil
"At least one argument must be a vector")))
903 (setq math-working-step-2
(1- len
))
904 (while (> (setq len
(1- len
)) 0)
907 (while (< (setq i
(1+ i
)) nargs
)
910 (aset ptrs i
(cdr (aref ptrs i
)))
911 (setq expr
(nconc expr
(list (car (aref ptrs i
))))))
912 (setq expr
(nconc expr
(list (aref ptrs i
))))))
913 (setq math-working-step
(1+ math-working-step
)
914 vec
(cons (math-normalize (math-build-call func expr
)) vec
)))
915 (setq vec
(cons head
(nreverse vec
)))
916 (if (and (eq mode
'cols
) (math-matrixp vec
))
920 (defun calcFunc-map (func &rest args
)
921 (math-symb-map func
'elems args
))
923 (defun calcFunc-mapr (func &rest args
)
924 (math-symb-map func
'rows args
))
926 (defun calcFunc-mapc (func &rest args
)
927 (math-symb-map func
'cols args
))
929 (defun calcFunc-mapa (func arg
)
930 (if (math-matrixp arg
)
931 (math-symb-map func
'elems
(cdr (math-transpose arg
)))
932 (math-symb-map func
'elems arg
)))
934 (defun calcFunc-mapd (func arg
)
935 (if (math-matrixp arg
)
936 (math-symb-map func
'elems
(cdr arg
))
937 (math-symb-map func
'elems arg
)))
939 (defun calcFunc-mapeq (func &rest args
)
940 (if (and (or (equal func
'(var mul var-mul
))
941 (equal func
'(var div var-div
)))
943 (if (math-negp (car args
))
944 (let ((func (nth 1 (assq (car-safe (nth 1 args
))
945 calc-tweak-eqn-table
))))
946 (and func
(setq args
(list (car args
)
947 (cons func
(cdr (nth 1 args
)))))))
948 (if (math-negp (nth 1 args
))
949 (let ((func (nth 1 (assq (car-safe (car args
))
950 calc-tweak-eqn-table
))))
951 (and func
(setq args
(list (cons func
(cdr (car args
)))
953 (if (or (and (equal func
'(var div var-div
))
954 (assq (car-safe (nth 1 args
)) calc-tweak-eqn-table
))
955 (equal func
'(var neg var-neg
))
956 (equal func
'(var inv var-inv
)))
957 (apply 'calcFunc-mapeqr func args
)
958 (apply 'calcFunc-mapeqp func args
)))
960 (defun calcFunc-mapeqr (func &rest args
)
961 (setq args
(mapcar (function (lambda (x)
962 (let ((func (assq (car-safe x
)
963 calc-tweak-eqn-table
)))
965 (cons (nth 1 func
) (cdr x
))
968 (apply 'calcFunc-mapeqp func args
))
970 (defun calcFunc-mapeqp (func &rest args
)
971 (if (or (and (memq (car-safe (car args
)) '(calcFunc-lt calcFunc-leq
))
972 (memq (car-safe (nth 1 args
)) '(calcFunc-gt calcFunc-geq
)))
973 (and (memq (car-safe (car args
)) '(calcFunc-gt calcFunc-geq
))
974 (memq (car-safe (nth 1 args
)) '(calcFunc-lt calcFunc-leq
))))
975 (setq args
(cons (car args
)
976 (cons (list (nth 1 (assq (car (nth 1 args
))
977 calc-tweak-eqn-table
))
979 (nth 1 (nth 1 args
)))
981 (math-symb-map func
'eqn args
))
985 ;;; Reduce a function over a vector symbolically. [Public]
986 (defun calcFunc-reduce (func vec
)
987 (if (math-matrixp vec
)
989 (setq func
(math-var-to-calcFunc func
))
990 (while (setq vec
(cdr vec
))
992 (while (setq row
(cdr row
))
994 (if (Math-numberp expr
)
996 (math-build-call func
(list expr
(car row
))))
997 (math-build-call func
(list expr
(car row
))))
999 (math-normalize expr
))
1000 (calcFunc-reducer func vec
)))
1002 (defun calcFunc-rreduce (func vec
)
1003 (if (math-matrixp vec
)
1005 (setq func
(math-var-to-calcFunc func
)
1006 vec
(reverse (cdr vec
)))
1008 (setq row
(reverse (cdr (car vec
))))
1011 (math-build-call func
(list (car row
) expr
))
1014 (setq vec
(cdr vec
)))
1015 (math-normalize expr
))
1016 (calcFunc-rreducer func vec
)))
1018 (defun calcFunc-reducer (func vec
)
1019 (setq func
(math-var-to-calcFunc func
))
1020 (or (math-vectorp vec
)
1021 (math-reject-arg vec
'vectorp
))
1022 (let ((expr (car (setq vec
(cdr vec
)))))
1027 (let ((lfunc (or (cdr (assq func
1028 '( (calcFunc-add . math-add
)
1029 (calcFunc-sub . math-sub
)
1030 (calcFunc-mul . math-mul
)
1031 (calcFunc-div . math-div
)
1032 (calcFunc-pow . math-pow
)
1033 (calcFunc-mod . math-mod
)
1038 (setq expr
(funcall lfunc expr
(nth 1 vec
))
1041 (while (setq vec
(cdr vec
))
1042 (setq expr
(math-build-call func
(list expr
(car vec
)))))
1043 (math-normalize expr
))
1044 (or (math-identity-value func
)
1045 (math-reject-arg vec
"*Vector is empty")))))
1047 (defun math-identity-value (func)
1048 (cdr (assq func
'( (calcFunc-add .
0) (calcFunc-sub .
0)
1049 (calcFunc-mul .
1) (calcFunc-div .
1)
1050 (calcFunc-idiv .
1) (calcFunc-fdiv .
1)
1051 (calcFunc-min .
(var inf var-inf
))
1052 (calcFunc-max .
(neg (var inf var-inf
)))
1053 (calcFunc-vconcat .
(vec))
1054 (calcFunc-append .
(vec)) ))))
1056 (defun calcFunc-rreducer (func vec
)
1057 (setq func
(math-var-to-calcFunc func
))
1058 (or (math-vectorp vec
)
1059 (math-reject-arg vec
'vectorp
))
1060 (if (eq func
'calcFunc-sub
) ; do this in a way that looks nicer
1061 (let ((expr (car (setq vec
(cdr vec
)))))
1064 (while (setq vec
(cdr vec
))
1065 (setq expr
(math-build-call func
(list expr
(car vec
)))
1066 func
(if (eq func
'calcFunc-sub
)
1067 'calcFunc-add
'calcFunc-sub
)))
1068 (math-normalize expr
))
1070 (let ((expr (car (setq vec
(reverse (cdr vec
))))))
1073 (while (setq vec
(cdr vec
))
1074 (setq expr
(math-build-call func
(list (car vec
) expr
))))
1075 (math-normalize expr
))
1076 (or (math-identity-value func
)
1077 (math-reject-arg vec
"*Vector is empty"))))))
1079 (defun calcFunc-reducec (func vec
)
1080 (if (math-matrixp vec
)
1081 (calcFunc-reducer func
(math-transpose vec
))
1082 (calcFunc-reducer func vec
)))
1084 (defun calcFunc-rreducec (func vec
)
1085 (if (math-matrixp vec
)
1086 (calcFunc-rreducer func
(math-transpose vec
))
1087 (calcFunc-rreducer func vec
)))
1089 (defun calcFunc-reducea (func vec
)
1090 (if (math-matrixp vec
)
1092 (mapcar (function (lambda (x) (calcFunc-reducer func x
)))
1094 (calcFunc-reducer func vec
)))
1096 (defun calcFunc-rreducea (func vec
)
1097 (if (math-matrixp vec
)
1099 (mapcar (function (lambda (x) (calcFunc-rreducer func x
)))
1101 (calcFunc-rreducer func vec
)))
1103 (defun calcFunc-reduced (func vec
)
1104 (if (math-matrixp vec
)
1106 (mapcar (function (lambda (x) (calcFunc-reducer func x
)))
1107 (cdr (math-transpose vec
))))
1108 (calcFunc-reducer func vec
)))
1110 (defun calcFunc-rreduced (func vec
)
1111 (if (math-matrixp vec
)
1113 (mapcar (function (lambda (x) (calcFunc-rreducer func x
)))
1114 (cdr (math-transpose vec
))))
1115 (calcFunc-rreducer func vec
)))
1117 (defun calcFunc-accum (func vec
)
1118 (setq func
(math-var-to-calcFunc func
))
1119 (or (math-vectorp vec
)
1120 (math-reject-arg vec
'vectorp
))
1121 (let* ((expr (car (setq vec
(cdr vec
))))
1122 (res (list 'vec expr
)))
1124 (math-reject-arg vec
"*Vector is empty"))
1125 (while (setq vec
(cdr vec
))
1126 (setq expr
(math-build-call func
(list expr
(car vec
)))
1127 res
(nconc res
(list expr
))))
1128 (math-normalize res
)))
1130 (defun calcFunc-raccum (func vec
)
1131 (setq func
(math-var-to-calcFunc func
))
1132 (or (math-vectorp vec
)
1133 (math-reject-arg vec
'vectorp
))
1134 (let* ((expr (car (setq vec
(reverse (cdr vec
)))))
1137 (math-reject-arg vec
"*Vector is empty"))
1138 (while (setq vec
(cdr vec
))
1139 (setq expr
(math-build-call func
(list (car vec
) expr
))
1140 res
(cons (list expr
) res
)))
1141 (math-normalize (cons 'vec res
))))
1144 (defun math-nest-calls (func base iters accum tol
)
1146 (if (math-realp tol
)
1147 (or (math-numberp base
) (math-reject-arg base
'numberp
))
1148 (math-reject-arg tol
'realp
)))
1149 (setq func
(math-var-to-calcFunc func
))
1151 (if (equal iters
'(var inf var-inf
))
1154 (if (math-messy-integerp iters
)
1155 (setq iters
(math-trunc iters
)))
1156 (or (integerp iters
) (math-reject-arg iters
'fixnump
))
1157 (or (not tol
) (natnump iters
) (math-reject-arg iters
'fixnatnump
))
1159 (let* ((dummy '(var DummyArg var-DummyArg
))
1160 (dummy2 '(var DummyArg2 var-DummyArg2
))
1161 (finv (math-solve-for (math-build-call func
(list dummy2
))
1163 (or finv
(math-reject-arg nil
"*Unable to find an inverse"))
1164 (if (and (= (length finv
) 2)
1165 (equal (nth 1 finv
) dummy
))
1166 (setq func
(car finv
))
1167 (setq func
(list 'calcFunc-lambda dummy finv
)))
1168 (setq iters
(- iters
)))))))
1169 (math-with-extra-prec 1
1172 (avalues (list base
))
1173 (math-working-step 0)
1174 (math-working-step-2 iters
))
1175 (while (and (or (null iters
)
1176 (>= (setq iters
(1- iters
)) 0))
1180 (not (if (and (Math-numberp value
)
1181 (Math-numberp ovalue
))
1182 (math-nearly-equal value ovalue
)
1183 (Math-equal value ovalue
)))
1184 (if (math-numberp value
)
1185 (Math-lessp tol
(math-abs (math-sub value ovalue
)))
1186 (math-reject-arg value
'numberp
)))))
1188 math-working-step
(1+ math-working-step
)
1189 value
(math-normalize (math-build-call func
(list value
))))
1191 (setq avalues
(cons value avalues
))))
1193 (cons 'vec
(nreverse avalues
))
1196 (defun calcFunc-nest (func base iters
)
1197 (math-nest-calls func base iters nil nil
))
1199 (defun calcFunc-anest (func base iters
)
1200 (math-nest-calls func base iters t nil
))
1202 (defun calcFunc-fixp (func base
&optional iters tol
)
1203 (math-nest-calls func base iters nil
(or tol t
)))
1205 (defun calcFunc-afixp (func base
&optional iters tol
)
1206 (math-nest-calls func base iters t
(or tol t
)))
1209 (defun calcFunc-outer (func a b
)
1210 (or (math-vectorp a
) (math-reject-arg a
'vectorp
))
1211 (or (math-vectorp b
) (math-reject-arg b
'vectorp
))
1212 (setq func
(math-var-to-calcFunc func
))
1214 (while (setq a
(cdr a
))
1215 (setq mat
(cons (cons 'vec
1216 (mapcar (function (lambda (x)
1217 (math-build-call func
1222 (math-normalize (cons 'vec
(nreverse mat
)))))
1225 (defun calcFunc-inner (mul-func add-func a b
)
1226 (or (math-vectorp a
) (math-reject-arg a
'vectorp
))
1227 (or (math-vectorp b
) (math-reject-arg b
'vectorp
))
1228 (if (math-matrixp a
)
1229 (if (math-matrixp b
)
1230 (if (= (length (nth 1 a
)) (length b
))
1231 (math-inner-mats a b
)
1232 (math-dimension-error))
1233 (if (= (length (nth 1 a
)) 2)
1234 (if (= (length a
) (length b
))
1235 (math-inner-mats a
(list 'vec b
))
1236 (math-dimension-error))
1237 (if (= (length (nth 1 a
)) (length b
))
1238 (math-mat-col (math-inner-mats a
(math-col-matrix b
))
1240 (math-dimension-error))))
1241 (if (math-matrixp b
)
1242 (nth 1 (math-inner-mats (list 'vec a
) b
))
1243 (calcFunc-reduce add-func
(calcFunc-map mul-func a b
)))))
1245 (defun math-inner-mats (a b
)
1247 (cols (length (nth 1 b
)))
1248 row col ap bp accum
)
1249 (while (setq a
(cdr a
))
1252 (while (> (setq col
(1- col
)) 0)
1253 (setq row
(cons (calcFunc-reduce add-func
1254 (calcFunc-map mul-func
1256 (math-mat-col b col
)))
1258 (setq mat
(cons (cons 'vec row
) mat
)))
1259 (cons 'vec
(nreverse mat
))))
1261 ;;; arch-tag: 980eac49-00e0-4870-b72a-e726b74c7990
1262 ;;; calc-map.el ends here