1 ;;; calc-map.el --- higher-order functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2013 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file is autoloaded from calc-ext.el.
32 (defun calc-apply (&optional oper
)
35 (let* ((calc-dollar-values (mapcar #'calc-get-stack-element
36 (nthcdr calc-stack-top calc-stack
)))
38 (oper (or oper
(calc-get-operator "Apply"
39 (if (math-vectorp (calc-top 1))
40 (1- (length (calc-top 1)))
42 (expr (calc-top-n (1+ calc-dollar-used
))))
43 (message "Working...")
44 (calc-set-command-flag 'clear-message
)
45 (calc-enter-result (1+ calc-dollar-used
)
46 (concat (substring "apl" 0 (- 4 (length (nth 2 oper
))))
49 (math-calcFunc-to-var (nth 1 oper
))
52 (defun calc-reduce (&optional oper accum
)
55 (let* ((nest (calc-is-hyperbolic))
56 (rev (calc-is-inverse))
57 (nargs (if (and nest
(not rev
)) 2 1))
58 (calc-dollar-values (mapcar #'calc-get-stack-element
59 (nthcdr calc-stack-top calc-stack
)))
61 (calc-mapping-dir (and (not accum
) (not nest
) ""))
62 (oper (or oper
(calc-get-operator
64 (concat (if accum
"Accumulate " "")
65 (if rev
"Fixed Point" "Nest"))
66 (concat (if rev
"Inv " "")
67 (if accum
"Accumulate" "Reduce")))
69 (message "Working...")
70 (calc-set-command-flag 'clear-message
)
71 (calc-enter-result (+ calc-dollar-used nargs
)
72 (concat (substring (if nest
74 (if accum
"acc" "red"))
75 0 (- 4 (length (nth 2 oper
))))
79 (if accum
'calcFunc-afixp
'calcFunc-fixp
)
80 (if accum
'calcFunc-anest
'calcFunc-nest
))
81 (cons (math-calcFunc-to-var (nth 1 oper
))
83 nargs
(1+ calc-dollar-used
))))
85 (if rev
'calcFunc-raccum
'calcFunc-accum
)
86 (intern (concat "calcFunc-"
90 (math-calcFunc-to-var (nth 1 oper
))
91 (calc-top-n (1+ calc-dollar-used
))))))))
93 (defun calc-accumulate (&optional oper
)
97 (defun calc-map (&optional oper
)
100 (let* ((calc-dollar-values (mapcar #'calc-get-stack-element
101 (nthcdr calc-stack-top calc-stack
)))
103 (calc-mapping-dir "")
104 (oper (or oper
(calc-get-operator "Map")))
106 (message "Working...")
107 (calc-set-command-flag 'clear-message
)
108 (calc-enter-result (+ nargs calc-dollar-used
)
109 (concat (substring "map" 0 (- 4 (length (nth 2 oper
))))
111 (cons (intern (concat "calcFunc-map" calc-mapping-dir
))
112 (cons (math-calcFunc-to-var (nth 1 oper
))
115 (1+ calc-dollar-used
))))))))
117 (defun calc-map-equation (&optional oper
)
120 (let* ((calc-dollar-values (mapcar #'calc-get-stack-element
121 (nthcdr calc-stack-top calc-stack
)))
123 (oper (or oper
(calc-get-operator "Map-equation")))
125 (message "Working...")
126 (calc-set-command-flag 'clear-message
)
127 (calc-enter-result (+ nargs calc-dollar-used
)
128 (concat (substring "map" 0 (- 4 (length (nth 2 oper
))))
130 (cons (if (calc-is-inverse)
132 (if (calc-is-hyperbolic)
133 'calcFunc-mapeqp
'calcFunc-mapeq
))
134 (cons (math-calcFunc-to-var (nth 1 oper
))
137 (1+ calc-dollar-used
))))))))
139 (defvar calc-verify-arglist t
)
140 (defvar calc-mapping-dir nil
)
141 (defun calc-map-stack ()
142 "This is meant to be called by calc-keypad mode."
144 (let ((calc-verify-arglist nil
))
145 (calc-unread-command ?\$
)
148 (defun calc-outer-product (&optional oper
)
151 (let* ((calc-dollar-values (mapcar #'calc-get-stack-element
152 (nthcdr calc-stack-top calc-stack
)))
154 (oper (or oper
(calc-get-operator "Outer" 2))))
155 (message "Working...")
156 (calc-set-command-flag 'clear-message
)
157 (calc-enter-result (+ 2 calc-dollar-used
)
158 (concat (substring "out" 0 (- 4 (length (nth 2 oper
))))
160 (cons 'calcFunc-outer
161 (cons (math-calcFunc-to-var (nth 1 oper
))
163 2 (1+ calc-dollar-used
))))))))
165 (defun calc-inner-product (&optional mul-oper add-oper
)
168 (let* ((calc-dollar-values (mapcar #'calc-get-stack-element
169 (nthcdr calc-stack-top calc-stack
)))
171 (mul-oper (or mul-oper
(calc-get-operator "Inner (Mult)" 2)))
172 (mul-used calc-dollar-used
)
173 (calc-dollar-values (if (> mul-used
0)
174 (cdr calc-dollar-values
)
177 (add-oper (or add-oper
(calc-get-operator "Inner (Add)" 2))))
178 (message "Working...")
179 (calc-set-command-flag 'clear-message
)
180 (calc-enter-result (+ 2 mul-used calc-dollar-used
)
182 (substring (nth 2 mul-oper
) 0 1)
183 (substring (nth 2 add-oper
) 0 1))
184 (nconc (list 'calcFunc-inner
185 (math-calcFunc-to-var (nth 1 mul-oper
))
186 (math-calcFunc-to-var (nth 1 add-oper
)))
188 2 (+ 1 mul-used calc-dollar-used
)))))))
190 (defconst calc-oper-keys
'( ( ( ?
+ 2 calcFunc-add
)
191 ( ?-
2 calcFunc-sub
)
192 ( ?
* 2 calcFunc-mul
)
193 ( ?
/ 2 calcFunc-div
)
194 ( ?^
2 calcFunc-pow
)
195 ( ?|
2 calcFunc-vconcat
)
196 ( ?%
2 calcFunc-mod
)
197 ( ?
\\ 2 calcFunc-idiv
)
198 ( ?
! 1 calcFunc-fact
)
199 ( ?
& 1 calcFunc-inv
)
200 ( ?n
1 calcFunc-neg
)
203 ( ?A
1 calcFunc-abs
)
204 ( ?J
1 calcFunc-conj
)
205 ( ?G
1 calcFunc-arg
)
206 ( ?Q
1 calcFunc-sqrt
)
207 ( ?N
2 calcFunc-min
)
208 ( ?X
2 calcFunc-max
)
209 ( ?F
1 calcFunc-floor
)
210 ( ?R
1 calcFunc-round
)
211 ( ?S
1 calcFunc-sin
)
212 ( ?C
1 calcFunc-cos
)
213 ( ?T
1 calcFunc-tan
)
215 ( ?E
1 calcFunc-exp
)
216 ( ?B
2 calcFunc-log
) )
217 ( ( ?F
1 calcFunc-ceil
) ; inverse
218 ( ?R
1 calcFunc-trunc
)
219 ( ?Q
1 calcFunc-sqr
)
220 ( ?S
1 calcFunc-arcsin
)
221 ( ?C
1 calcFunc-arccos
)
222 ( ?T
1 calcFunc-arctan
)
223 ( ?L
1 calcFunc-exp
)
225 ( ?B
2 calcFunc-alog
)
226 ( ?^
2 calcFunc-nroot
)
227 ( ?|
2 calcFunc-vconcatrev
) )
228 ( ( ?F
1 calcFunc-ffloor
) ; hyperbolic
229 ( ?R
1 calcFunc-fround
)
230 ( ?S
1 calcFunc-sinh
)
231 ( ?C
1 calcFunc-cosh
)
232 ( ?T
1 calcFunc-tanh
)
233 ( ?L
1 calcFunc-log10
)
234 ( ?E
1 calcFunc-exp10
)
235 ( ?|
2 calcFunc-append
) )
236 ( ( ?F
1 calcFunc-fceil
) ; inverse-hyperbolic
237 ( ?R
1 calcFunc-ftrunc
)
238 ( ?S
1 calcFunc-arcsinh
)
239 ( ?C
1 calcFunc-arccosh
)
240 ( ?T
1 calcFunc-arctanh
)
241 ( ?L
1 calcFunc-exp10
)
242 ( ?E
1 calcFunc-log10
)
243 ( ?|
2 calcFunc-appendrev
) )))
245 (defconst calc-a-oper-keys
'( ( ( ?a
3 calcFunc-apart
)
246 ( ?b
3 calcFunc-subst
)
247 ( ?c
2 calcFunc-collect
)
248 ( ?d
2 calcFunc-deriv
)
249 ( ?e
1 calcFunc-esimplify
)
250 ( ?f
2 calcFunc-factor
)
251 ( ?g
2 calcFunc-pgcd
)
252 ( ?i
2 calcFunc-integ
)
253 ( ?m
2 calcFunc-match
)
254 ( ?n
1 calcFunc-nrat
)
255 ( ?r
2 calcFunc-rewrite
)
256 ( ?s
1 calcFunc-simplify
)
257 ( ?t
3 calcFunc-taylor
)
258 ( ?x
1 calcFunc-expand
)
259 ( ?M
2 calcFunc-mapeq
)
260 ( ?N
3 calcFunc-minimize
)
261 ( ?P
2 calcFunc-roots
)
262 ( ?R
3 calcFunc-root
)
263 ( ?S
2 calcFunc-solve
)
264 ( ?T
4 calcFunc-table
)
265 ( ?X
3 calcFunc-maximize
)
267 ( ?\
# 2 calcFunc-neq
)
270 ( ?\
[ 2 calcFunc-leq
)
271 ( ?\
] 2 calcFunc-geq
)
273 ( ?
! 1 calcFunc-lnot
)
274 ( ?
& 2 calcFunc-land
)
275 ( ?\|
2 calcFunc-lor
)
277 ( ?.
2 calcFunc-rmeq
)
278 ( ?
+ 4 calcFunc-sum
)
279 ( ?-
4 calcFunc-asum
)
280 ( ?
* 4 calcFunc-prod
)
281 ( ?_
2 calcFunc-subscr
)
282 ( ?
\\ 2 calcFunc-pdiv
)
283 ( ?%
2 calcFunc-prem
)
284 ( ?
/ 2 calcFunc-pdivrem
) )
285 ( ( ?m
2 calcFunc-matchnot
)
286 ( ?M
2 calcFunc-mapeqr
)
287 ( ?S
2 calcFunc-finv
) )
288 ( ( ?d
2 calcFunc-tderiv
)
289 ( ?f
2 calcFunc-factors
)
290 ( ?M
2 calcFunc-mapeqp
)
291 ( ?N
3 calcFunc-wminimize
)
292 ( ?R
3 calcFunc-wroot
)
293 ( ?S
2 calcFunc-fsolve
)
294 ( ?X
3 calcFunc-wmaximize
)
295 ( ?
/ 2 calcFunc-pdivide
) )
296 ( ( ?S
2 calcFunc-ffinv
) )))
298 (defconst calc-b-oper-keys
'( ( ( ?a
2 calcFunc-and
)
300 ( ?x
2 calcFunc-xor
)
301 ( ?d
2 calcFunc-diff
)
302 ( ?n
1 calcFunc-not
)
303 ( ?c
1 calcFunc-clip
)
304 ( ?l
2 calcFunc-lsh
)
305 ( ?r
2 calcFunc-rsh
)
306 ( ?L
2 calcFunc-ash
)
307 ( ?R
2 calcFunc-rash
)
308 ( ?t
2 calcFunc-rot
)
309 ( ?p
1 calcFunc-vpack
)
310 ( ?u
1 calcFunc-vunpack
)
311 ( ?D
4 calcFunc-ddb
)
313 ( ?I
1 calcFunc-irr
)
314 ( ?M
3 calcFunc-pmt
)
315 ( ?N
2 calcFunc-npv
)
317 ( ?S
3 calcFunc-sln
)
318 ( ?T
3 calcFunc-rate
)
319 ( ?Y
4 calcFunc-syd
)
320 ( ?\
# 3 calcFunc-nper
)
321 ( ?\%
2 calcFunc-relch
) )
322 ( ( ?F
3 calcFunc-fvb
)
323 ( ?I
1 calcFunc-irrb
)
324 ( ?M
3 calcFunc-pmtb
)
325 ( ?N
2 calcFunc-npvb
)
326 ( ?P
3 calcFunc-pvb
)
327 ( ?T
3 calcFunc-rateb
)
328 ( ?\
# 3 calcFunc-nperb
) )
329 ( ( ?F
3 calcFunc-fvl
)
330 ( ?M
3 calcFunc-pmtl
)
331 ( ?P
3 calcFunc-pvl
)
332 ( ?T
3 calcFunc-ratel
)
333 ( ?\
# 3 calcFunc-nperl
) )))
335 (defconst calc-c-oper-keys
'( ( ( ?d
1 calcFunc-deg
)
336 ( ?r
1 calcFunc-rad
)
337 ( ?h
1 calcFunc-hms
)
338 ( ?f
1 calcFunc-float
)
339 ( ?F
1 calcFunc-frac
) )))
341 (defconst calc-f-oper-keys
'( ( ( ?b
2 calcFunc-beta
)
342 ( ?e
1 calcFunc-erf
)
343 ( ?g
1 calcFunc-gamma
)
344 ( ?h
2 calcFunc-hypot
)
346 ( ?j
2 calcFunc-besJ
)
347 ( ?n
2 calcFunc-min
)
349 ( ?s
1 calcFunc-sign
)
350 ( ?x
2 calcFunc-max
)
351 ( ?y
2 calcFunc-besY
)
352 ( ?A
1 calcFunc-abssqr
)
353 ( ?B
3 calcFunc-betaI
)
354 ( ?E
1 calcFunc-expm1
)
355 ( ?G
2 calcFunc-gammaP
)
356 ( ?I
2 calcFunc-ilog
)
357 ( ?L
1 calcFunc-lnp1
)
358 ( ?M
1 calcFunc-mant
)
359 ( ?Q
1 calcFunc-isqrt
)
360 ( ?S
1 calcFunc-scf
)
361 ( ?T
2 calcFunc-arctan2
)
362 ( ?X
1 calcFunc-xpon
)
363 ( ?\
[ 2 calcFunc-decr
)
364 ( ?\
] 2 calcFunc-incr
) )
365 ( ( ?e
1 calcFunc-erfc
)
366 ( ?E
1 calcFunc-lnp1
)
367 ( ?G
2 calcFunc-gammaQ
)
368 ( ?L
1 calcFunc-expm1
) )
369 ( ( ?B
3 calcFunc-betaB
)
370 ( ?G
2 calcFunc-gammag
) )
371 ( ( ?G
2 calcFunc-gammaG
) )))
373 (defconst calc-k-oper-keys
'( ( ( ?b
1 calcFunc-bern
)
374 ( ?c
2 calcFunc-choose
)
375 ( ?d
1 calcFunc-dfact
)
376 ( ?e
1 calcFunc-euler
)
377 ( ?f
1 calcFunc-prfac
)
378 ( ?g
2 calcFunc-gcd
)
379 ( ?h
2 calcFunc-shuffle
)
380 ( ?l
2 calcFunc-lcm
)
381 ( ?m
1 calcFunc-moebius
)
382 ( ?n
1 calcFunc-nextprime
)
383 ( ?r
1 calcFunc-random
)
384 ( ?s
2 calcFunc-stir1
)
385 ( ?t
1 calcFunc-totient
)
386 ( ?B
3 calcFunc-utpb
)
387 ( ?C
2 calcFunc-utpc
)
388 ( ?F
3 calcFunc-utpf
)
389 ( ?N
3 calcFunc-utpn
)
390 ( ?P
2 calcFunc-utpp
)
391 ( ?T
2 calcFunc-utpt
) )
392 ( ( ?n
1 calcFunc-prevprime
)
393 ( ?B
3 calcFunc-ltpb
)
394 ( ?C
2 calcFunc-ltpc
)
395 ( ?F
3 calcFunc-ltpf
)
396 ( ?N
3 calcFunc-ltpn
)
397 ( ?P
2 calcFunc-ltpp
)
398 ( ?T
2 calcFunc-ltpt
) )
399 ( ( ?b
2 calcFunc-bern
)
400 ( ?c
2 calcFunc-perm
)
401 ( ?e
2 calcFunc-euler
)
402 ( ?s
2 calcFunc-stir2
) )))
404 (defconst calc-s-oper-keys
'( ( ( ?
: 2 calcFunc-assign
)
405 ( ?
= 1 calcFunc-evalto
) )))
407 (defconst calc-t-oper-keys
'( ( ( ?C
3 calcFunc-tzconv
)
408 ( ?D
1 calcFunc-date
)
409 ( ?I
2 calcFunc-incmonth
)
410 ( ?J
1 calcFunc-julian
)
411 ( ?M
1 calcFunc-newmonth
)
412 ( ?W
1 calcFunc-newweek
)
413 ( ?U
1 calcFunc-unixtime
)
414 ( ?Y
1 calcFunc-newyear
) )))
416 (defconst calc-u-oper-keys
'( ( ( ?C
2 calcFunc-vcov
)
417 ( ?G
1 calcFunc-vgmean
)
418 ( ?M
1 calcFunc-vmean
)
419 ( ?N
1 calcFunc-vmin
)
420 ( ?S
1 calcFunc-vsdev
)
421 ( ?X
1 calcFunc-vmax
) )
422 ( ( ?C
2 calcFunc-vpcov
)
423 ( ?M
1 calcFunc-vmeane
)
424 ( ?S
1 calcFunc-vpsdev
) )
425 ( ( ?C
2 calcFunc-vcorr
)
426 ( ?G
1 calcFunc-agmean
)
427 ( ?M
1 calcFunc-vmedian
)
428 ( ?S
1 calcFunc-vvar
) )
429 ( ( ?M
1 calcFunc-vhmean
)
430 ( ?S
1 calcFunc-vpvar
) )))
432 (defconst calc-v-oper-keys
'( ( ( ?a
2 calcFunc-arrange
)
433 ( ?b
2 calcFunc-cvec
)
434 ( ?c
2 calcFunc-mcol
)
435 ( ?d
2 calcFunc-diag
)
436 ( ?e
2 calcFunc-vexp
)
437 ( ?f
2 calcFunc-find
)
438 ( ?h
1 calcFunc-head
)
439 ( ?k
2 calcFunc-cons
)
440 ( ?l
1 calcFunc-vlen
)
441 ( ?m
2 calcFunc-vmask
)
442 ( ?n
1 calcFunc-rnorm
)
443 ( ?p
2 calcFunc-pack
)
444 ( ?r
2 calcFunc-mrow
)
445 ( ?s
3 calcFunc-subvec
)
446 ( ?t
1 calcFunc-trn
)
447 ( ?u
1 calcFunc-unpack
)
448 ( ?v
1 calcFunc-rev
)
449 ( ?x
1 calcFunc-index
)
450 ( ?A
1 calcFunc-apply
)
451 ( ?C
1 calcFunc-cross
)
452 ( ?D
1 calcFunc-det
)
453 ( ?E
1 calcFunc-venum
)
454 ( ?F
1 calcFunc-vfloor
)
455 ( ?G
1 calcFunc-grade
)
456 ( ?H
2 calcFunc-histogram
)
457 ( ?I
2 calcFunc-inner
)
458 ( ?L
1 calcFunc-lud
)
459 ( ?M
0 calcFunc-map
)
460 ( ?N
1 calcFunc-cnorm
)
461 ( ?O
2 calcFunc-outer
)
462 ( ?R
1 calcFunc-reduce
)
463 ( ?S
1 calcFunc-sort
)
465 ( ?U
1 calcFunc-accum
)
466 ( ?V
2 calcFunc-vunion
)
467 ( ?X
2 calcFunc-vxor
)
468 ( ?-
2 calcFunc-vdiff
)
469 ( ?^
2 calcFunc-vint
)
470 ( ?~
1 calcFunc-vcompl
)
471 ( ?
# 1 calcFunc-vcard
)
472 ( ?
: 1 calcFunc-vspan
)
473 ( ?
+ 1 calcFunc-rdup
) )
474 ( ( ?h
1 calcFunc-tail
)
475 ( ?s
3 calcFunc-rsubvec
)
476 ( ?G
1 calcFunc-rgrade
)
477 ( ?R
1 calcFunc-rreduce
)
478 ( ?S
1 calcFunc-rsort
)
479 ( ?U
1 calcFunc-raccum
) )
480 ( ( ?e
3 calcFunc-vexp
)
481 ( ?h
1 calcFunc-rhead
)
482 ( ?k
2 calcFunc-rcons
)
483 ( ?H
3 calcFunc-histogram
)
484 ( ?R
2 calcFunc-nest
)
485 ( ?U
2 calcFunc-anest
) )
486 ( ( ?h
1 calcFunc-rtail
)
487 ( ?R
1 calcFunc-fixp
)
488 ( ?U
1 calcFunc-afixp
) )))
491 ;;; Return a list of the form (nargs func name)
492 (defvar calc-get-operator-history nil
493 "History for calc-get-operator.")
495 (defun calc-get-operator (msg &optional nargs
)
496 (setq calc-aborted-prefix nil
)
497 (let ((inv nil
) (hyp nil
) (prefix nil
) (forcenargs nil
)
498 done key oper
(which 0)
499 (msgs '( "(Press ? for help)"
500 "+, -, *, /, ^, %, \\, :, &, !, |, Neg"
501 "SHIFT + Abs, conJ, arG; maX, miN; Floor, Round; sQrt"
502 "SHIFT + Inv, Hyp; Sin, Cos, Tan; Exp, Ln, logB"
503 "Algebra + Simp, Esimp, Deriv, Integ, !, =, etc."
504 "Binary + And, Or, Xor, Diff; l/r/t/L/R shifts; Not, Clip"
505 "Conversions + Deg, Rad, HMS; Float; SHIFT + Fraction"
506 "Functions + Re, Im; Hypot; Mant, Expon, Scale; etc."
507 "Kombinatorics + Dfact, Lcm, Gcd, Choose; Random; etc."
508 "Time/date + newYear, Incmonth, etc."
509 "Vectors + Length, Row, Col, Diag, Mask, etc."
510 "_ = mapr/reducea, : = mapc/reduced, = = reducer"
511 "X or Z = any function by name; ' = alg entry; $ = stack")))
513 (message "%s%s: %s: %s%s%s"
515 (cond ((equal calc-mapping-dir
"r") " rows")
516 ((equal calc-mapping-dir
"c") " columns")
517 ((equal calc-mapping-dir
"a") " across")
518 ((equal calc-mapping-dir
"d") " down")
522 forcenargs
(if (= forcenargs
1) "" "s"))
524 (if inv
"Inv " "") (if hyp
"Hyp " "")
525 (if prefix
(concat (char-to-string prefix
) "-") ""))
526 (setq key
(read-char))
527 (if (>= key
128) (setq key
(- key
128)))
528 (cond ((memq key
'(?\C-g ?q
))
530 ((memq key
'(?\C-u ?\e
)))
532 (setq which
(%
(1+ which
) (length msgs
))))
533 ((and (= key ?I
) (null prefix
))
534 (setq inv
(not inv
)))
535 ((and (= key ?H
) (null prefix
))
536 (setq hyp
(not hyp
)))
537 ((and (eq key prefix
) (not (eq key ?v
)))
539 ((and (memq key
'(?a ?b ?c ?f ?k ?s ?t ?u ?v ?V
))
541 (setq prefix
(downcase key
)))
542 ((and (eq key ?\
=) (null prefix
))
544 (setq calc-mapping-dir
(if (equal calc-mapping-dir
"r")
547 ((and (eq key ?\_
) (null prefix
))
549 (if (string-match "map$" msg
)
550 (setq calc-mapping-dir
(if (equal calc-mapping-dir
"r")
552 (setq calc-mapping-dir
(if (equal calc-mapping-dir
"a")
555 ((and (eq key ?\
:) (null prefix
))
557 (if (string-match "map$" msg
)
558 (setq calc-mapping-dir
(if (equal calc-mapping-dir
"c")
560 (setq calc-mapping-dir
(if (equal calc-mapping-dir
"d")
563 ((and (>= key ?
0) (<= key ?
9) (null prefix
))
564 (setq forcenargs
(if (eq forcenargs
(- key ?
0)) nil
(- key ?
0)))
565 (and nargs forcenargs
(/= nargs forcenargs
) (>= nargs
0)
566 (error "Must be a %d-argument operator" nargs
)))
567 ((memq key
'(?\$ ?
\'))
568 (let* ((math-arglist nil
)
571 (expr (if (eq key ?\$
)
573 (setq calc-dollar-used
1)
574 (if calc-dollar-values
575 (car calc-dollar-values
)
576 (error "Stack underflow")))
577 (let* ((calc-dollar-values calc-arg-values
)
580 (func (calc-do-alg-entry "" "Function: " nil
581 'calc-get-operator-history
)))
582 (setq record-entry t
)
583 (or (= (length func
) 1)
584 (error "Bad format"))
585 (if (> calc-dollar-used
0)
587 (setq has-args calc-dollar-used
588 math-arglist
(calc-invent-args has-args
))
589 (math-multi-subst (car func
)
590 (reverse math-arglist
)
592 (if (> calc-hashes-used
0)
593 (setq has-args calc-hashes-used
594 math-arglist
(calc-invent-args has-args
)))
596 (if (eq (car-safe expr
) 'calcFunc-lambda
)
597 (setq oper
(list "$" (- (length expr
) 2) expr
)
601 (calc-default-formula-arglist expr
)
603 math-arglist
(sort math-arglist
'string-lessp
))
604 (if calc-verify-arglist
605 (setq math-arglist
(read-from-minibuffer
606 "Function argument list: "
608 (prin1-to-string math-arglist
)
612 (setq math-arglist
(mapcar (function
622 (length math-arglist
)
623 (append '(calcFunc-lambda) math-arglist
627 (calc-record (nth 2 oper
) "oper"))))
628 ((setq oper
(assq key
(nth (if inv
(if hyp
3 1) (if hyp
2 0))
631 (intern (format "calc-%c-oper-keys"
634 (if (eq (nth 1 oper
) 'user
)
636 (completing-read "Function name: "
639 (if (or forcenargs nargs
)
640 (setq oper
(list "z" (or forcenargs nargs
) func
)
643 (let* ((defn (symbol-function func
)))
645 (setq defn
(symbol-function defn
)))
646 (if (eq (car-safe defn
) 'lambda
)
647 (let ((args (nth 1 defn
))
649 (while (not (memq (car args
) '(&optional
651 (setq nargs
(1+ nargs
)
653 (setq oper
(list "z" nargs func
)
656 "Function is not suitable for this operation")))
657 (message "Number of arguments: ")
658 (let ((nargs (read-char)))
659 (if (and (>= nargs ?
0) (<= nargs ?
9))
660 (setq oper
(list "z" (- nargs ?
0) func
)
663 (if (or (and (eq prefix ?v
) (memq key
'(?A ?I ?M ?O ?R ?U
)))
664 (and (eq prefix ?a
) (eq key ?M
)))
665 (let* ((dir (cond ((and (equal calc-mapping-dir
"")
666 (string-match "map$" msg
))
667 (setq calc-mapping-dir
"r")
669 ((equal calc-mapping-dir
"r") " rows")
670 ((equal calc-mapping-dir
"c") " columns")
671 ((equal calc-mapping-dir
"a") " across")
672 ((equal calc-mapping-dir
"d") " down")
674 (calc-mapping-dir (and (memq (nth 2 oper
)
679 (oper2 (calc-get-operator
680 (format "%s%s, %s%s" msg dir
681 (substring (symbol-name (nth 2 oper
))
683 (if (eq key ?I
) " (mult)" ""))
684 (cdr (assq (nth 2 oper
)
685 '((calcFunc-reduce .
2)
686 (calcFunc-rreduce .
2)
688 (calcFunc-raccum .
2)
692 (calcFunc-afixp .
2))))))
693 (oper3 (if (eq (nth 2 oper
) 'calcFunc-inner
)
695 (format "%s%s, inner (add)" msg dir
))
698 (nargs (if (> (nth 1 oper
) 0)
703 (while (and p
(> n
0))
704 (or (math-expr-contains (nth 1 oper2
) (car p
))
705 (math-expr-contains (nth 1 oper3
) (car p
))
706 (setq args
(nconc args
(list (car p
)))
709 (setq oper
(list "" nargs
713 (list (math-build-call
716 (symbol-name (nth 2 oper
))
718 (cons (math-calcFunc-to-var
722 (math-calcFunc-to-var
729 (and nargs
(>= nargs
0)
730 (/= nargs
(nth 1 oper
))
731 (error "Must be a %d-argument operator" nargs
))
732 (append (if forcenargs
733 (cons forcenargs
(cdr (cdr oper
)))
736 (let ((name (concat (if inv
"I" "") (if hyp
"H" "")
737 (if prefix
(char-to-string prefix
) "")
738 (char-to-string key
))))
739 (if (> (length name
) 3)
744 ;;; Convert a variable name (as a formula) into a like-looking function name.
745 (defun math-var-to-calcFunc (f)
746 (if (eq (car-safe f
) 'var
)
747 (if (fboundp (nth 2 f
))
749 (intern (concat "calcFunc-" (symbol-name (nth 1 f
)))))
750 (if (memq (car-safe f
) '(lambda calcFunc-lambda
))
752 (math-reject-arg f
"*Expected a function name"))))
754 ;;; Convert a function name into a like-looking variable name formula.
755 (defun math-calcFunc-to-var (f)
757 (let* ((func (or (cdr (assq f
'( ( + . calcFunc-add
)
763 ( neg . calcFunc-neg
)
764 ( | . calcFunc-vconcat
) )))
766 (base (if (string-match "\\`calcFunc-\\(.+\\)\\'"
768 (math-match-substring (symbol-name func
) 1)
769 (symbol-name func
))))
772 (intern (concat "var-" base
))))
775 ;;; Expand a function call using "lambda" notation.
776 (defun math-build-call (f args
)
777 (if (eq (car-safe f
) 'calcFunc-lambda
)
778 (if (= (length args
) (- (length f
) 2))
779 (math-multi-subst (nth (1- (length f
)) f
) (cdr f
) args
)
780 (calc-record-why "*Wrong number of arguments" f
)
781 (cons 'calcFunc-call
(cons (math-calcFunc-to-var f
) args
)))
782 (if (and (eq f
'calcFunc-neg
)
784 (list 'neg
(car args
))
785 (let ((func (assq f
'( ( calcFunc-add .
+ )
791 ( calcFunc-vconcat . |
) ))))
792 (if (and func
(= (length args
) 2))
793 (cons (cdr func
) args
)
796 ;;; Do substitutions in parallel to avoid crosstalk.
798 ;; The variables math-ms-temp and math-ms-args are local to
799 ;; math-multi-subst, but are used by math-multi-subst-rec, which
800 ;; is called by math-multi-subst.
801 (defvar math-ms-temp
)
802 (defvar math-ms-args
)
804 (defun math-multi-subst (expr olds news
)
805 (let ((math-ms-args nil
)
807 (while (and olds news
)
808 (setq math-ms-args
(cons (cons (car olds
) (car news
)) math-ms-args
)
811 (math-multi-subst-rec expr
)))
813 (defun math-multi-subst-rec (expr)
814 (cond ((setq math-ms-temp
(assoc expr math-ms-args
))
816 ((Math-primp expr
) expr
)
817 ((and (eq (car expr
) 'calcFunc-lambda
) (> (length expr
) 2))
818 (let ((new (list (car expr
)))
819 (math-ms-args math-ms-args
))
820 (while (cdr (setq expr
(cdr expr
)))
821 (setq new
(cons (car expr
) new
))
822 (if (assoc (car expr
) math-ms-args
)
823 (setq math-ms-args
(cons (cons (car expr
) (car expr
))
825 (nreverse (cons (math-multi-subst-rec (car expr
)) new
))))
828 (mapcar 'math-multi-subst-rec
(cdr expr
))))))
830 (defun calcFunc-call (f &rest args
)
831 (setq args
(math-build-call (math-var-to-calcFunc f
) args
))
832 (if (eq (car-safe args
) 'calcFunc-call
)
834 (math-normalize args
)))
836 (defun calcFunc-apply (f args
)
837 (or (Math-vectorp args
)
838 (math-reject-arg args
'vectorp
))
839 (apply 'calcFunc-call
(cons f
(cdr args
))))
844 ;;; Map a function over a vector symbolically. [Public]
845 (defun math-symb-map (f mode args
)
846 (let* ((func (math-var-to-calcFunc f
))
847 (nargs (length args
))
848 (ptrs (vconcat args
))
849 (vflags (make-vector nargs nil
))
854 (math-working-step 0)
855 (math-working-step-2 nil
)
859 heads
'(calcFunc-eq calcFunc-neq calcFunc-lt calcFunc-gt
860 calcFunc-leq calcFunc-geq
))
861 (while (and (< (setq i
(1+ i
)) nargs
)
862 (not (math-matrixp (aref ptrs i
)))))
865 (setq func
(list 'lambda
'(&rest x
)
867 (list 'quote f
) '(quote elems
) 'x
))
871 (if (math-matrixp (aref ptrs i
))
872 (aset ptrs i
(math-transpose (aref ptrs i
))))
876 (while (< (setq i
(1+ i
)) nargs
)
877 (setq obj
(aref ptrs i
))
878 (if (and (memq (car-safe obj
) heads
)
886 (aref (aref [ [0 1 2 3 4 5]
892 (- 6 (length (memq head heads
))))
893 (- 6 (length (memq (car obj
) heads
))))
895 (setq head
(car obj
)))
897 (or (= (length obj
) len
)
898 (math-dimension-error))
899 (setq len
(length obj
))))))
902 (math-reject-arg (aref ptrs
0) 'vectorp
)
903 (math-reject-arg nil
"At least one argument must be a vector")))
904 (setq math-working-step-2
(1- len
))
905 (while (> (setq len
(1- len
)) 0)
908 (while (< (setq i
(1+ i
)) nargs
)
911 (aset ptrs i
(cdr (aref ptrs i
)))
912 (setq expr
(nconc expr
(list (car (aref ptrs i
))))))
913 (setq expr
(nconc expr
(list (aref ptrs i
))))))
914 (setq math-working-step
(1+ math-working-step
)
915 vec
(cons (math-normalize (math-build-call func expr
)) vec
)))
916 (setq vec
(cons head
(nreverse vec
)))
917 (if (and (eq mode
'cols
) (math-matrixp vec
))
921 (defun calcFunc-map (func &rest args
)
922 (math-symb-map func
'elems args
))
924 (defun calcFunc-mapr (func &rest args
)
925 (math-symb-map func
'rows args
))
927 (defun calcFunc-mapc (func &rest args
)
928 (math-symb-map func
'cols args
))
930 (defun calcFunc-mapa (func arg
)
931 (if (math-matrixp arg
)
932 (math-symb-map func
'elems
(cdr (math-transpose arg
)))
933 (math-symb-map func
'elems arg
)))
935 (defun calcFunc-mapd (func arg
)
936 (if (math-matrixp arg
)
937 (math-symb-map func
'elems
(cdr arg
))
938 (math-symb-map func
'elems arg
)))
940 (defun calcFunc-mapeq (func &rest args
)
941 (if (and (or (equal func
'(var mul var-mul
))
942 (equal func
'(var div var-div
)))
944 (if (math-negp (car args
))
945 (let ((func (nth 1 (assq (car-safe (nth 1 args
))
946 calc-tweak-eqn-table
))))
947 (and func
(setq args
(list (car args
)
948 (cons func
(cdr (nth 1 args
)))))))
949 (if (math-negp (nth 1 args
))
950 (let ((func (nth 1 (assq (car-safe (car args
))
951 calc-tweak-eqn-table
))))
952 (and func
(setq args
(list (cons func
(cdr (car args
)))
954 (if (or (and (equal func
'(var div var-div
))
955 (assq (car-safe (nth 1 args
)) calc-tweak-eqn-table
))
956 (equal func
'(var neg var-neg
))
957 (equal func
'(var inv var-inv
)))
958 (apply 'calcFunc-mapeqr func args
)
959 (apply 'calcFunc-mapeqp func args
)))
961 (defun calcFunc-mapeqr (func &rest args
)
962 (setq args
(mapcar (function (lambda (x)
963 (let ((func (assq (car-safe x
)
964 calc-tweak-eqn-table
)))
966 (cons (nth 1 func
) (cdr x
))
969 (apply 'calcFunc-mapeqp func args
))
971 (defun calcFunc-mapeqp (func &rest args
)
972 (if (or (and (memq (car-safe (car args
)) '(calcFunc-lt calcFunc-leq
))
973 (memq (car-safe (nth 1 args
)) '(calcFunc-gt calcFunc-geq
)))
974 (and (memq (car-safe (car args
)) '(calcFunc-gt calcFunc-geq
))
975 (memq (car-safe (nth 1 args
)) '(calcFunc-lt calcFunc-leq
))))
976 (setq args
(cons (car args
)
977 (cons (list (nth 1 (assq (car (nth 1 args
))
978 calc-tweak-eqn-table
))
980 (nth 1 (nth 1 args
)))
982 (math-symb-map func
'eqn args
))
986 ;;; Reduce a function over a vector symbolically. [Public]
987 (defun calcFunc-reduce (func vec
)
988 (if (math-matrixp vec
)
990 (setq func
(math-var-to-calcFunc func
))
991 (while (setq vec
(cdr vec
))
993 (while (setq row
(cdr row
))
995 (if (Math-numberp expr
)
997 (math-build-call func
(list expr
(car row
))))
998 (math-build-call func
(list expr
(car row
))))
1000 (math-normalize expr
))
1001 (calcFunc-reducer func vec
)))
1003 (defun calcFunc-rreduce (func vec
)
1004 (if (math-matrixp vec
)
1006 (setq func
(math-var-to-calcFunc func
)
1007 vec
(reverse (cdr vec
)))
1009 (setq row
(reverse (cdr (car vec
))))
1012 (math-build-call func
(list (car row
) expr
))
1015 (setq vec
(cdr vec
)))
1016 (math-normalize expr
))
1017 (calcFunc-rreducer func vec
)))
1019 (defun calcFunc-reducer (func vec
)
1020 (setq func
(math-var-to-calcFunc func
))
1021 (or (math-vectorp vec
)
1022 (math-reject-arg vec
'vectorp
))
1023 (let ((expr (car (setq vec
(cdr vec
)))))
1028 (let ((lfunc (or (cdr (assq func
1029 '( (calcFunc-add . math-add
)
1030 (calcFunc-sub . math-sub
)
1031 (calcFunc-mul . math-mul
)
1032 (calcFunc-div . math-div
)
1033 (calcFunc-pow . math-pow
)
1034 (calcFunc-mod . math-mod
)
1039 (setq expr
(funcall lfunc expr
(nth 1 vec
))
1042 (while (setq vec
(cdr vec
))
1043 (setq expr
(math-build-call func
(list expr
(car vec
)))))
1044 (math-normalize expr
))
1045 (or (math-identity-value func
)
1046 (math-reject-arg vec
"*Vector is empty")))))
1048 (defun math-identity-value (func)
1049 (cdr (assq func
'( (calcFunc-add .
0) (calcFunc-sub .
0)
1050 (calcFunc-mul .
1) (calcFunc-div .
1)
1051 (calcFunc-idiv .
1) (calcFunc-fdiv .
1)
1052 (calcFunc-min .
(var inf var-inf
))
1053 (calcFunc-max .
(neg (var inf var-inf
)))
1054 (calcFunc-vconcat .
(vec))
1055 (calcFunc-append .
(vec)) ))))
1057 (defun calcFunc-rreducer (func vec
)
1058 (setq func
(math-var-to-calcFunc func
))
1059 (or (math-vectorp vec
)
1060 (math-reject-arg vec
'vectorp
))
1061 (if (eq func
'calcFunc-sub
) ; do this in a way that looks nicer
1062 (let ((expr (car (setq vec
(cdr vec
)))))
1065 (while (setq vec
(cdr vec
))
1066 (setq expr
(math-build-call func
(list expr
(car vec
)))
1067 func
(if (eq func
'calcFunc-sub
)
1068 'calcFunc-add
'calcFunc-sub
)))
1069 (math-normalize expr
))
1071 (let ((expr (car (setq vec
(reverse (cdr vec
))))))
1074 (while (setq vec
(cdr vec
))
1075 (setq expr
(math-build-call func
(list (car vec
) expr
))))
1076 (math-normalize expr
))
1077 (or (math-identity-value func
)
1078 (math-reject-arg vec
"*Vector is empty"))))))
1080 (defun calcFunc-reducec (func vec
)
1081 (if (math-matrixp vec
)
1082 (calcFunc-reducer func
(math-transpose vec
))
1083 (calcFunc-reducer func vec
)))
1085 (defun calcFunc-rreducec (func vec
)
1086 (if (math-matrixp vec
)
1087 (calcFunc-rreducer func
(math-transpose vec
))
1088 (calcFunc-rreducer func vec
)))
1090 (defun calcFunc-reducea (func vec
)
1091 (if (math-matrixp vec
)
1093 (mapcar (function (lambda (x) (calcFunc-reducer func x
)))
1095 (calcFunc-reducer func vec
)))
1097 (defun calcFunc-rreducea (func vec
)
1098 (if (math-matrixp vec
)
1100 (mapcar (function (lambda (x) (calcFunc-rreducer func x
)))
1102 (calcFunc-rreducer func vec
)))
1104 (defun calcFunc-reduced (func vec
)
1105 (if (math-matrixp vec
)
1107 (mapcar (function (lambda (x) (calcFunc-reducer func x
)))
1108 (cdr (math-transpose vec
))))
1109 (calcFunc-reducer func vec
)))
1111 (defun calcFunc-rreduced (func vec
)
1112 (if (math-matrixp vec
)
1114 (mapcar (function (lambda (x) (calcFunc-rreducer func x
)))
1115 (cdr (math-transpose vec
))))
1116 (calcFunc-rreducer func vec
)))
1118 (defun calcFunc-accum (func vec
)
1119 (setq func
(math-var-to-calcFunc func
))
1120 (or (math-vectorp vec
)
1121 (math-reject-arg vec
'vectorp
))
1122 (let* ((expr (car (setq vec
(cdr vec
))))
1123 (res (list 'vec expr
)))
1125 (math-reject-arg vec
"*Vector is empty"))
1126 (while (setq vec
(cdr vec
))
1127 (setq expr
(math-build-call func
(list expr
(car vec
)))
1128 res
(nconc res
(list expr
))))
1129 (math-normalize res
)))
1131 (defun calcFunc-raccum (func vec
)
1132 (setq func
(math-var-to-calcFunc func
))
1133 (or (math-vectorp vec
)
1134 (math-reject-arg vec
'vectorp
))
1135 (let* ((expr (car (setq vec
(reverse (cdr vec
)))))
1138 (math-reject-arg vec
"*Vector is empty"))
1139 (while (setq vec
(cdr vec
))
1140 (setq expr
(math-build-call func
(list (car vec
) expr
))
1141 res
(cons (list expr
) res
)))
1142 (math-normalize (cons 'vec res
))))
1145 (defun math-nest-calls (func base iters accum tol
)
1147 (if (math-realp tol
)
1148 (or (math-numberp base
) (math-reject-arg base
'numberp
))
1149 (math-reject-arg tol
'realp
)))
1150 (setq func
(math-var-to-calcFunc func
))
1152 (if (equal iters
'(var inf var-inf
))
1155 (if (math-messy-integerp iters
)
1156 (setq iters
(math-trunc iters
)))
1157 (or (integerp iters
) (math-reject-arg iters
'fixnump
))
1158 (or (not tol
) (natnump iters
) (math-reject-arg iters
'fixnatnump
))
1160 (let* ((dummy '(var DummyArg var-DummyArg
))
1161 (dummy2 '(var DummyArg2 var-DummyArg2
))
1162 (finv (math-solve-for (math-build-call func
(list dummy2
))
1164 (or finv
(math-reject-arg nil
"*Unable to find an inverse"))
1165 (if (and (= (length finv
) 2)
1166 (equal (nth 1 finv
) dummy
))
1167 (setq func
(car finv
))
1168 (setq func
(list 'calcFunc-lambda dummy finv
)))
1169 (setq iters
(- iters
)))))))
1170 (math-with-extra-prec 1
1173 (avalues (list base
))
1174 (math-working-step 0)
1175 (math-working-step-2 iters
))
1176 (while (and (or (null iters
)
1177 (>= (setq iters
(1- iters
)) 0))
1181 (not (if (and (Math-numberp value
)
1182 (Math-numberp ovalue
))
1183 (math-nearly-equal value ovalue
)
1184 (Math-equal value ovalue
)))
1185 (if (math-numberp value
)
1186 (Math-lessp tol
(math-abs (math-sub value ovalue
)))
1187 (math-reject-arg value
'numberp
)))))
1189 math-working-step
(1+ math-working-step
)
1190 value
(math-normalize (math-build-call func
(list value
))))
1192 (setq avalues
(cons value avalues
))))
1194 (cons 'vec
(nreverse avalues
))
1197 (defun calcFunc-nest (func base iters
)
1198 (math-nest-calls func base iters nil nil
))
1200 (defun calcFunc-anest (func base iters
)
1201 (math-nest-calls func base iters t nil
))
1203 (defun calcFunc-fixp (func base
&optional iters tol
)
1204 (math-nest-calls func base iters nil
(or tol t
)))
1206 (defun calcFunc-afixp (func base
&optional iters tol
)
1207 (math-nest-calls func base iters t
(or tol t
)))
1210 (defun calcFunc-outer (func a b
)
1211 (or (math-vectorp a
) (math-reject-arg a
'vectorp
))
1212 (or (math-vectorp b
) (math-reject-arg b
'vectorp
))
1213 (setq func
(math-var-to-calcFunc func
))
1215 (while (setq a
(cdr a
))
1216 (setq mat
(cons (cons 'vec
1217 (mapcar (function (lambda (x)
1218 (math-build-call func
1223 (math-normalize (cons 'vec
(nreverse mat
)))))
1226 ;; The variables math-inner-mul-func and math-inner-add-func are
1227 ;; local to calcFunc-inner, but are used by math-inner-mats,
1228 ;; which is called by math-inner-mats.
1229 (defvar math-inner-mul-func
)
1230 (defvar math-inner-add-func
)
1232 (defun calcFunc-inner (math-inner-mul-func math-inner-add-func a b
)
1233 (or (math-vectorp a
) (math-reject-arg a
'vectorp
))
1234 (or (math-vectorp b
) (math-reject-arg b
'vectorp
))
1235 (if (math-matrixp a
)
1236 (if (math-matrixp b
)
1237 (if (= (length (nth 1 a
)) (length b
))
1238 (math-inner-mats a b
)
1239 (math-dimension-error))
1240 (if (= (length (nth 1 a
)) 2)
1241 (if (= (length a
) (length b
))
1242 (math-inner-mats a
(list 'vec b
))
1243 (math-dimension-error))
1244 (if (= (length (nth 1 a
)) (length b
))
1245 (math-mat-col (math-inner-mats a
(math-col-matrix b
))
1247 (math-dimension-error))))
1248 (if (math-matrixp b
)
1249 (nth 1 (math-inner-mats (list 'vec a
) b
))
1250 (calcFunc-reduce math-inner-add-func
(calcFunc-map math-inner-mul-func a b
)))))
1252 (defun math-inner-mats (a b
)
1254 (cols (length (nth 1 b
)))
1255 row col ap bp accum
)
1256 (while (setq a
(cdr a
))
1259 (while (> (setq col
(1- col
)) 0)
1260 (setq row
(cons (calcFunc-reduce math-inner-add-func
1261 (calcFunc-map math-inner-mul-func
1263 (math-mat-col b col
)))
1265 (setq mat
(cons (cons 'vec row
) mat
)))
1266 (cons 'vec
(nreverse mat
))))
1270 ;;; calc-map.el ends here