1 ;;; calc-map.el --- higher-order functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2012 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
)
36 (calc-dollar-values (mapcar 'calc-get-stack-element
37 (nthcdr calc-stack-top calc-stack
)))
39 (oper (or oper
(calc-get-operator "Apply"
40 (if (math-vectorp (calc-top 1))
41 (1- (length (calc-top 1)))
43 (expr (calc-top-n (1+ calc-dollar-used
))))
44 (message "Working...")
45 (calc-set-command-flag 'clear-message
)
46 (calc-enter-result (1+ calc-dollar-used
)
47 (concat (substring "apl" 0 (- 4 (length (nth 2 oper
))))
50 (math-calcFunc-to-var (nth 1 oper
))
53 (defun calc-reduce (&optional oper accum
)
57 (nest (calc-is-hyperbolic))
58 (rev (calc-is-inverse))
59 (nargs (if (and nest
(not rev
)) 2 1))
60 (calc-dollar-values (mapcar 'calc-get-stack-element
61 (nthcdr calc-stack-top calc-stack
)))
63 (calc-mapping-dir (and (not accum
) (not nest
) ""))
64 (oper (or oper
(calc-get-operator
66 (concat (if accum
"Accumulate " "")
67 (if rev
"Fixed Point" "Nest"))
68 (concat (if rev
"Inv " "")
69 (if accum
"Accumulate" "Reduce")))
71 (message "Working...")
72 (calc-set-command-flag 'clear-message
)
73 (calc-enter-result (+ calc-dollar-used nargs
)
74 (concat (substring (if nest
76 (if accum
"acc" "red"))
77 0 (- 4 (length (nth 2 oper
))))
81 (if accum
'calcFunc-afixp
'calcFunc-fixp
)
82 (if accum
'calcFunc-anest
'calcFunc-nest
))
83 (cons (math-calcFunc-to-var (nth 1 oper
))
85 nargs
(1+ calc-dollar-used
))))
87 (if rev
'calcFunc-raccum
'calcFunc-accum
)
88 (intern (concat "calcFunc-"
92 (math-calcFunc-to-var (nth 1 oper
))
93 (calc-top-n (1+ calc-dollar-used
))))))))
95 (defun calc-accumulate (&optional oper
)
99 (defun calc-map (&optional oper
)
102 (let* ((sel-mode nil
)
103 (calc-dollar-values (mapcar 'calc-get-stack-element
104 (nthcdr calc-stack-top calc-stack
)))
106 (calc-mapping-dir "")
107 (oper (or oper
(calc-get-operator "Map")))
109 (message "Working...")
110 (calc-set-command-flag 'clear-message
)
111 (calc-enter-result (+ nargs calc-dollar-used
)
112 (concat (substring "map" 0 (- 4 (length (nth 2 oper
))))
114 (cons (intern (concat "calcFunc-map" calc-mapping-dir
))
115 (cons (math-calcFunc-to-var (nth 1 oper
))
118 (1+ calc-dollar-used
))))))))
120 (defun calc-map-equation (&optional oper
)
123 (let* ((sel-mode nil
)
124 (calc-dollar-values (mapcar 'calc-get-stack-element
125 (nthcdr calc-stack-top calc-stack
)))
127 (oper (or oper
(calc-get-operator "Map-equation")))
129 (message "Working...")
130 (calc-set-command-flag 'clear-message
)
131 (calc-enter-result (+ nargs calc-dollar-used
)
132 (concat (substring "map" 0 (- 4 (length (nth 2 oper
))))
134 (cons (if (calc-is-inverse)
136 (if (calc-is-hyperbolic)
137 'calcFunc-mapeqp
'calcFunc-mapeq
))
138 (cons (math-calcFunc-to-var (nth 1 oper
))
141 (1+ calc-dollar-used
))))))))
143 (defvar calc-verify-arglist t
)
144 (defvar calc-mapping-dir nil
)
145 (defun calc-map-stack ()
146 "This is meant to be called by calc-keypad mode."
148 (let ((calc-verify-arglist nil
))
149 (calc-unread-command ?\$
)
152 (defun calc-outer-product (&optional oper
)
155 (let* ((sel-mode nil
)
156 (calc-dollar-values (mapcar 'calc-get-stack-element
157 (nthcdr calc-stack-top calc-stack
)))
159 (oper (or oper
(calc-get-operator "Outer" 2))))
160 (message "Working...")
161 (calc-set-command-flag 'clear-message
)
162 (calc-enter-result (+ 2 calc-dollar-used
)
163 (concat (substring "out" 0 (- 4 (length (nth 2 oper
))))
165 (cons 'calcFunc-outer
166 (cons (math-calcFunc-to-var (nth 1 oper
))
168 2 (1+ calc-dollar-used
))))))))
170 (defun calc-inner-product (&optional mul-oper add-oper
)
173 (let* ((sel-mode nil
)
174 (calc-dollar-values (mapcar 'calc-get-stack-element
175 (nthcdr calc-stack-top calc-stack
)))
177 (mul-oper (or mul-oper
(calc-get-operator "Inner (Mult)" 2)))
178 (mul-used calc-dollar-used
)
179 (calc-dollar-values (if (> mul-used
0)
180 (cdr calc-dollar-values
)
183 (add-oper (or add-oper
(calc-get-operator "Inner (Add)" 2))))
184 (message "Working...")
185 (calc-set-command-flag 'clear-message
)
186 (calc-enter-result (+ 2 mul-used calc-dollar-used
)
188 (substring (nth 2 mul-oper
) 0 1)
189 (substring (nth 2 add-oper
) 0 1))
190 (nconc (list 'calcFunc-inner
191 (math-calcFunc-to-var (nth 1 mul-oper
))
192 (math-calcFunc-to-var (nth 1 add-oper
)))
194 2 (+ 1 mul-used calc-dollar-used
)))))))
196 (defconst calc-oper-keys
'( ( ( ?
+ 2 calcFunc-add
)
197 ( ?-
2 calcFunc-sub
)
198 ( ?
* 2 calcFunc-mul
)
199 ( ?
/ 2 calcFunc-div
)
200 ( ?^
2 calcFunc-pow
)
201 ( ?|
2 calcFunc-vconcat
)
202 ( ?%
2 calcFunc-mod
)
203 ( ?
\\ 2 calcFunc-idiv
)
204 ( ?
! 1 calcFunc-fact
)
205 ( ?
& 1 calcFunc-inv
)
206 ( ?n
1 calcFunc-neg
)
209 ( ?A
1 calcFunc-abs
)
210 ( ?J
1 calcFunc-conj
)
211 ( ?G
1 calcFunc-arg
)
212 ( ?Q
1 calcFunc-sqrt
)
213 ( ?N
2 calcFunc-min
)
214 ( ?X
2 calcFunc-max
)
215 ( ?F
1 calcFunc-floor
)
216 ( ?R
1 calcFunc-round
)
217 ( ?S
1 calcFunc-sin
)
218 ( ?C
1 calcFunc-cos
)
219 ( ?T
1 calcFunc-tan
)
221 ( ?E
1 calcFunc-exp
)
222 ( ?B
2 calcFunc-log
) )
223 ( ( ?F
1 calcFunc-ceil
) ; inverse
224 ( ?R
1 calcFunc-trunc
)
225 ( ?Q
1 calcFunc-sqr
)
226 ( ?S
1 calcFunc-arcsin
)
227 ( ?C
1 calcFunc-arccos
)
228 ( ?T
1 calcFunc-arctan
)
229 ( ?L
1 calcFunc-exp
)
231 ( ?B
2 calcFunc-alog
)
232 ( ?^
2 calcFunc-nroot
)
233 ( ?|
2 calcFunc-vconcatrev
) )
234 ( ( ?F
1 calcFunc-ffloor
) ; hyperbolic
235 ( ?R
1 calcFunc-fround
)
236 ( ?S
1 calcFunc-sinh
)
237 ( ?C
1 calcFunc-cosh
)
238 ( ?T
1 calcFunc-tanh
)
239 ( ?L
1 calcFunc-log10
)
240 ( ?E
1 calcFunc-exp10
)
241 ( ?|
2 calcFunc-append
) )
242 ( ( ?F
1 calcFunc-fceil
) ; inverse-hyperbolic
243 ( ?R
1 calcFunc-ftrunc
)
244 ( ?S
1 calcFunc-arcsinh
)
245 ( ?C
1 calcFunc-arccosh
)
246 ( ?T
1 calcFunc-arctanh
)
247 ( ?L
1 calcFunc-exp10
)
248 ( ?E
1 calcFunc-log10
)
249 ( ?|
2 calcFunc-appendrev
) )))
251 (defconst calc-a-oper-keys
'( ( ( ?a
3 calcFunc-apart
)
252 ( ?b
3 calcFunc-subst
)
253 ( ?c
2 calcFunc-collect
)
254 ( ?d
2 calcFunc-deriv
)
255 ( ?e
1 calcFunc-esimplify
)
256 ( ?f
2 calcFunc-factor
)
257 ( ?g
2 calcFunc-pgcd
)
258 ( ?i
2 calcFunc-integ
)
259 ( ?m
2 calcFunc-match
)
260 ( ?n
1 calcFunc-nrat
)
261 ( ?r
2 calcFunc-rewrite
)
262 ( ?s
1 calcFunc-simplify
)
263 ( ?t
3 calcFunc-taylor
)
264 ( ?x
1 calcFunc-expand
)
265 ( ?M
2 calcFunc-mapeq
)
266 ( ?N
3 calcFunc-minimize
)
267 ( ?P
2 calcFunc-roots
)
268 ( ?R
3 calcFunc-root
)
269 ( ?S
2 calcFunc-solve
)
270 ( ?T
4 calcFunc-table
)
271 ( ?X
3 calcFunc-maximize
)
273 ( ?\
# 2 calcFunc-neq
)
276 ( ?\
[ 2 calcFunc-leq
)
277 ( ?\
] 2 calcFunc-geq
)
279 ( ?
! 1 calcFunc-lnot
)
280 ( ?
& 2 calcFunc-land
)
281 ( ?\|
2 calcFunc-lor
)
283 ( ?.
2 calcFunc-rmeq
)
284 ( ?
+ 4 calcFunc-sum
)
285 ( ?-
4 calcFunc-asum
)
286 ( ?
* 4 calcFunc-prod
)
287 ( ?_
2 calcFunc-subscr
)
288 ( ?
\\ 2 calcFunc-pdiv
)
289 ( ?%
2 calcFunc-prem
)
290 ( ?
/ 2 calcFunc-pdivrem
) )
291 ( ( ?m
2 calcFunc-matchnot
)
292 ( ?M
2 calcFunc-mapeqr
)
293 ( ?S
2 calcFunc-finv
) )
294 ( ( ?d
2 calcFunc-tderiv
)
295 ( ?f
2 calcFunc-factors
)
296 ( ?M
2 calcFunc-mapeqp
)
297 ( ?N
3 calcFunc-wminimize
)
298 ( ?R
3 calcFunc-wroot
)
299 ( ?S
2 calcFunc-fsolve
)
300 ( ?X
3 calcFunc-wmaximize
)
301 ( ?
/ 2 calcFunc-pdivide
) )
302 ( ( ?S
2 calcFunc-ffinv
) )))
304 (defconst calc-b-oper-keys
'( ( ( ?a
2 calcFunc-and
)
306 ( ?x
2 calcFunc-xor
)
307 ( ?d
2 calcFunc-diff
)
308 ( ?n
1 calcFunc-not
)
309 ( ?c
1 calcFunc-clip
)
310 ( ?l
2 calcFunc-lsh
)
311 ( ?r
2 calcFunc-rsh
)
312 ( ?L
2 calcFunc-ash
)
313 ( ?R
2 calcFunc-rash
)
314 ( ?t
2 calcFunc-rot
)
315 ( ?p
1 calcFunc-vpack
)
316 ( ?u
1 calcFunc-vunpack
)
317 ( ?D
4 calcFunc-ddb
)
319 ( ?I
1 calcFunc-irr
)
320 ( ?M
3 calcFunc-pmt
)
321 ( ?N
2 calcFunc-npv
)
323 ( ?S
3 calcFunc-sln
)
324 ( ?T
3 calcFunc-rate
)
325 ( ?Y
4 calcFunc-syd
)
326 ( ?\
# 3 calcFunc-nper
)
327 ( ?\%
2 calcFunc-relch
) )
328 ( ( ?F
3 calcFunc-fvb
)
329 ( ?I
1 calcFunc-irrb
)
330 ( ?M
3 calcFunc-pmtb
)
331 ( ?N
2 calcFunc-npvb
)
332 ( ?P
3 calcFunc-pvb
)
333 ( ?T
3 calcFunc-rateb
)
334 ( ?\
# 3 calcFunc-nperb
) )
335 ( ( ?F
3 calcFunc-fvl
)
336 ( ?M
3 calcFunc-pmtl
)
337 ( ?P
3 calcFunc-pvl
)
338 ( ?T
3 calcFunc-ratel
)
339 ( ?\
# 3 calcFunc-nperl
) )))
341 (defconst calc-c-oper-keys
'( ( ( ?d
1 calcFunc-deg
)
342 ( ?r
1 calcFunc-rad
)
343 ( ?h
1 calcFunc-hms
)
344 ( ?f
1 calcFunc-float
)
345 ( ?F
1 calcFunc-frac
) )))
347 (defconst calc-f-oper-keys
'( ( ( ?b
2 calcFunc-beta
)
348 ( ?e
1 calcFunc-erf
)
349 ( ?g
1 calcFunc-gamma
)
350 ( ?h
2 calcFunc-hypot
)
352 ( ?j
2 calcFunc-besJ
)
353 ( ?n
2 calcFunc-min
)
355 ( ?s
1 calcFunc-sign
)
356 ( ?x
2 calcFunc-max
)
357 ( ?y
2 calcFunc-besY
)
358 ( ?A
1 calcFunc-abssqr
)
359 ( ?B
3 calcFunc-betaI
)
360 ( ?E
1 calcFunc-expm1
)
361 ( ?G
2 calcFunc-gammaP
)
362 ( ?I
2 calcFunc-ilog
)
363 ( ?L
1 calcFunc-lnp1
)
364 ( ?M
1 calcFunc-mant
)
365 ( ?Q
1 calcFunc-isqrt
)
366 ( ?S
1 calcFunc-scf
)
367 ( ?T
2 calcFunc-arctan2
)
368 ( ?X
1 calcFunc-xpon
)
369 ( ?\
[ 2 calcFunc-decr
)
370 ( ?\
] 2 calcFunc-incr
) )
371 ( ( ?e
1 calcFunc-erfc
)
372 ( ?E
1 calcFunc-lnp1
)
373 ( ?G
2 calcFunc-gammaQ
)
374 ( ?L
1 calcFunc-expm1
) )
375 ( ( ?B
3 calcFunc-betaB
)
376 ( ?G
2 calcFunc-gammag
) )
377 ( ( ?G
2 calcFunc-gammaG
) )))
379 (defconst calc-k-oper-keys
'( ( ( ?b
1 calcFunc-bern
)
380 ( ?c
2 calcFunc-choose
)
381 ( ?d
1 calcFunc-dfact
)
382 ( ?e
1 calcFunc-euler
)
383 ( ?f
1 calcFunc-prfac
)
384 ( ?g
2 calcFunc-gcd
)
385 ( ?h
2 calcFunc-shuffle
)
386 ( ?l
2 calcFunc-lcm
)
387 ( ?m
1 calcFunc-moebius
)
388 ( ?n
1 calcFunc-nextprime
)
389 ( ?r
1 calcFunc-random
)
390 ( ?s
2 calcFunc-stir1
)
391 ( ?t
1 calcFunc-totient
)
392 ( ?B
3 calcFunc-utpb
)
393 ( ?C
2 calcFunc-utpc
)
394 ( ?F
3 calcFunc-utpf
)
395 ( ?N
3 calcFunc-utpn
)
396 ( ?P
2 calcFunc-utpp
)
397 ( ?T
2 calcFunc-utpt
) )
398 ( ( ?n
1 calcFunc-prevprime
)
399 ( ?B
3 calcFunc-ltpb
)
400 ( ?C
2 calcFunc-ltpc
)
401 ( ?F
3 calcFunc-ltpf
)
402 ( ?N
3 calcFunc-ltpn
)
403 ( ?P
2 calcFunc-ltpp
)
404 ( ?T
2 calcFunc-ltpt
) )
405 ( ( ?b
2 calcFunc-bern
)
406 ( ?c
2 calcFunc-perm
)
407 ( ?e
2 calcFunc-euler
)
408 ( ?s
2 calcFunc-stir2
) )))
410 (defconst calc-s-oper-keys
'( ( ( ?
: 2 calcFunc-assign
)
411 ( ?
= 1 calcFunc-evalto
) )))
413 (defconst calc-t-oper-keys
'( ( ( ?C
3 calcFunc-tzconv
)
414 ( ?D
1 calcFunc-date
)
415 ( ?I
2 calcFunc-incmonth
)
416 ( ?J
1 calcFunc-julian
)
417 ( ?M
1 calcFunc-newmonth
)
418 ( ?W
1 calcFunc-newweek
)
419 ( ?U
1 calcFunc-unixtime
)
420 ( ?Y
1 calcFunc-newyear
) )))
422 (defconst calc-u-oper-keys
'( ( ( ?C
2 calcFunc-vcov
)
423 ( ?G
1 calcFunc-vgmean
)
424 ( ?M
1 calcFunc-vmean
)
425 ( ?N
1 calcFunc-vmin
)
426 ( ?S
1 calcFunc-vsdev
)
427 ( ?X
1 calcFunc-vmax
) )
428 ( ( ?C
2 calcFunc-vpcov
)
429 ( ?M
1 calcFunc-vmeane
)
430 ( ?S
1 calcFunc-vpsdev
) )
431 ( ( ?C
2 calcFunc-vcorr
)
432 ( ?G
1 calcFunc-agmean
)
433 ( ?M
1 calcFunc-vmedian
)
434 ( ?S
1 calcFunc-vvar
) )
435 ( ( ?M
1 calcFunc-vhmean
)
436 ( ?S
1 calcFunc-vpvar
) )))
438 (defconst calc-v-oper-keys
'( ( ( ?a
2 calcFunc-arrange
)
439 ( ?b
2 calcFunc-cvec
)
440 ( ?c
2 calcFunc-mcol
)
441 ( ?d
2 calcFunc-diag
)
442 ( ?e
2 calcFunc-vexp
)
443 ( ?f
2 calcFunc-find
)
444 ( ?h
1 calcFunc-head
)
445 ( ?k
2 calcFunc-cons
)
446 ( ?l
1 calcFunc-vlen
)
447 ( ?m
2 calcFunc-vmask
)
448 ( ?n
1 calcFunc-rnorm
)
449 ( ?p
2 calcFunc-pack
)
450 ( ?r
2 calcFunc-mrow
)
451 ( ?s
3 calcFunc-subvec
)
452 ( ?t
1 calcFunc-trn
)
453 ( ?u
1 calcFunc-unpack
)
454 ( ?v
1 calcFunc-rev
)
455 ( ?x
1 calcFunc-index
)
456 ( ?A
1 calcFunc-apply
)
457 ( ?C
1 calcFunc-cross
)
458 ( ?D
1 calcFunc-det
)
459 ( ?E
1 calcFunc-venum
)
460 ( ?F
1 calcFunc-vfloor
)
461 ( ?G
1 calcFunc-grade
)
462 ( ?H
2 calcFunc-histogram
)
463 ( ?I
2 calcFunc-inner
)
464 ( ?L
1 calcFunc-lud
)
465 ( ?M
0 calcFunc-map
)
466 ( ?N
1 calcFunc-cnorm
)
467 ( ?O
2 calcFunc-outer
)
468 ( ?R
1 calcFunc-reduce
)
469 ( ?S
1 calcFunc-sort
)
471 ( ?U
1 calcFunc-accum
)
472 ( ?V
2 calcFunc-vunion
)
473 ( ?X
2 calcFunc-vxor
)
474 ( ?-
2 calcFunc-vdiff
)
475 ( ?^
2 calcFunc-vint
)
476 ( ?~
1 calcFunc-vcompl
)
477 ( ?
# 1 calcFunc-vcard
)
478 ( ?
: 1 calcFunc-vspan
)
479 ( ?
+ 1 calcFunc-rdup
) )
480 ( ( ?h
1 calcFunc-tail
)
481 ( ?s
3 calcFunc-rsubvec
)
482 ( ?G
1 calcFunc-rgrade
)
483 ( ?R
1 calcFunc-rreduce
)
484 ( ?S
1 calcFunc-rsort
)
485 ( ?U
1 calcFunc-raccum
) )
486 ( ( ?e
3 calcFunc-vexp
)
487 ( ?h
1 calcFunc-rhead
)
488 ( ?k
2 calcFunc-rcons
)
489 ( ?H
3 calcFunc-histogram
)
490 ( ?R
2 calcFunc-nest
)
491 ( ?U
2 calcFunc-anest
) )
492 ( ( ?h
1 calcFunc-rtail
)
493 ( ?R
1 calcFunc-fixp
)
494 ( ?U
1 calcFunc-afixp
) )))
497 ;;; Return a list of the form (nargs func name)
498 (defvar calc-get-operator-history nil
499 "History for calc-get-operator.")
501 (defun calc-get-operator (msg &optional nargs
)
502 (setq calc-aborted-prefix nil
)
503 (let ((inv nil
) (hyp nil
) (prefix nil
) (forcenargs nil
)
504 done key oper
(which 0)
505 (msgs '( "(Press ? for help)"
506 "+, -, *, /, ^, %, \\, :, &, !, |, Neg"
507 "SHIFT + Abs, conJ, arG; maX, miN; Floor, Round; sQrt"
508 "SHIFT + Inv, Hyp; Sin, Cos, Tan; Exp, Ln, logB"
509 "Algebra + Simp, Esimp, Deriv, Integ, !, =, etc."
510 "Binary + And, Or, Xor, Diff; l/r/t/L/R shifts; Not, Clip"
511 "Conversions + Deg, Rad, HMS; Float; SHIFT + Fraction"
512 "Functions + Re, Im; Hypot; Mant, Expon, Scale; etc."
513 "Kombinatorics + Dfact, Lcm, Gcd, Choose; Random; etc."
514 "Time/date + newYear, Incmonth, etc."
515 "Vectors + Length, Row, Col, Diag, Mask, etc."
516 "_ = mapr/reducea, : = mapc/reduced, = = reducer"
517 "X or Z = any function by name; ' = alg entry; $ = stack")))
519 (message "%s%s: %s: %s%s%s"
521 (cond ((equal calc-mapping-dir
"r") " rows")
522 ((equal calc-mapping-dir
"c") " columns")
523 ((equal calc-mapping-dir
"a") " across")
524 ((equal calc-mapping-dir
"d") " down")
528 forcenargs
(if (= forcenargs
1) "" "s"))
530 (if inv
"Inv " "") (if hyp
"Hyp " "")
531 (if prefix
(concat (char-to-string prefix
) "-") ""))
532 (setq key
(read-char))
533 (if (>= key
128) (setq key
(- key
128)))
534 (cond ((memq key
'(?\C-g ?q
))
536 ((memq key
'(?\C-u ?\e
)))
538 (setq which
(%
(1+ which
) (length msgs
))))
539 ((and (= key ?I
) (null prefix
))
540 (setq inv
(not inv
)))
541 ((and (= key ?H
) (null prefix
))
542 (setq hyp
(not hyp
)))
543 ((and (eq key prefix
) (not (eq key ?v
)))
545 ((and (memq key
'(?a ?b ?c ?f ?k ?s ?t ?u ?v ?V
))
547 (setq prefix
(downcase key
)))
548 ((and (eq key ?\
=) (null prefix
))
550 (setq calc-mapping-dir
(if (equal calc-mapping-dir
"r")
553 ((and (eq key ?\_
) (null prefix
))
555 (if (string-match "map$" msg
)
556 (setq calc-mapping-dir
(if (equal calc-mapping-dir
"r")
558 (setq calc-mapping-dir
(if (equal calc-mapping-dir
"a")
561 ((and (eq key ?\
:) (null prefix
))
563 (if (string-match "map$" msg
)
564 (setq calc-mapping-dir
(if (equal calc-mapping-dir
"c")
566 (setq calc-mapping-dir
(if (equal calc-mapping-dir
"d")
569 ((and (>= key ?
0) (<= key ?
9) (null prefix
))
570 (setq forcenargs
(if (eq forcenargs
(- key ?
0)) nil
(- key ?
0)))
571 (and nargs forcenargs
(/= nargs forcenargs
) (>= nargs
0)
572 (error "Must be a %d-argument operator" nargs
)))
573 ((memq key
'(?\$ ?
\'))
574 (let* ((math-arglist nil
)
577 (expr (if (eq key ?\$
)
579 (setq calc-dollar-used
1)
580 (if calc-dollar-values
581 (car calc-dollar-values
)
582 (error "Stack underflow")))
583 (let* ((calc-dollar-values calc-arg-values
)
586 (func (calc-do-alg-entry "" "Function: " nil
587 'calc-get-operator-history
)))
588 (setq record-entry t
)
589 (or (= (length func
) 1)
590 (error "Bad format"))
591 (if (> calc-dollar-used
0)
593 (setq has-args calc-dollar-used
594 math-arglist
(calc-invent-args has-args
))
595 (math-multi-subst (car func
)
596 (reverse math-arglist
)
598 (if (> calc-hashes-used
0)
599 (setq has-args calc-hashes-used
600 math-arglist
(calc-invent-args has-args
)))
602 (if (eq (car-safe expr
) 'calcFunc-lambda
)
603 (setq oper
(list "$" (- (length expr
) 2) expr
)
607 (calc-default-formula-arglist expr
)
609 math-arglist
(sort math-arglist
'string-lessp
))
610 (if calc-verify-arglist
611 (setq math-arglist
(read-from-minibuffer
612 "Function argument list: "
614 (prin1-to-string math-arglist
)
618 (setq math-arglist
(mapcar (function
628 (length math-arglist
)
629 (append '(calcFunc-lambda) math-arglist
633 (calc-record (nth 2 oper
) "oper"))))
634 ((setq oper
(assq key
(nth (if inv
(if hyp
3 1) (if hyp
2 0))
637 (intern (format "calc-%c-oper-keys"
640 (if (eq (nth 1 oper
) 'user
)
642 (completing-read "Function name: "
645 (if (or forcenargs nargs
)
646 (setq oper
(list "z" (or forcenargs nargs
) func
)
649 (let* ((defn (symbol-function func
)))
651 (setq defn
(symbol-function defn
)))
652 (if (eq (car-safe defn
) 'lambda
)
653 (let ((args (nth 1 defn
))
655 (while (not (memq (car args
) '(&optional
657 (setq nargs
(1+ nargs
)
659 (setq oper
(list "z" nargs func
)
662 "Function is not suitable for this operation")))
663 (message "Number of arguments: ")
664 (let ((nargs (read-char)))
665 (if (and (>= nargs ?
0) (<= nargs ?
9))
666 (setq oper
(list "z" (- nargs ?
0) func
)
669 (if (or (and (eq prefix ?v
) (memq key
'(?A ?I ?M ?O ?R ?U
)))
670 (and (eq prefix ?a
) (eq key ?M
)))
671 (let* ((dir (cond ((and (equal calc-mapping-dir
"")
672 (string-match "map$" msg
))
673 (setq calc-mapping-dir
"r")
675 ((equal calc-mapping-dir
"r") " rows")
676 ((equal calc-mapping-dir
"c") " columns")
677 ((equal calc-mapping-dir
"a") " across")
678 ((equal calc-mapping-dir
"d") " down")
680 (calc-mapping-dir (and (memq (nth 2 oper
)
685 (oper2 (calc-get-operator
686 (format "%s%s, %s%s" msg dir
687 (substring (symbol-name (nth 2 oper
))
689 (if (eq key ?I
) " (mult)" ""))
690 (cdr (assq (nth 2 oper
)
691 '((calcFunc-reduce .
2)
692 (calcFunc-rreduce .
2)
694 (calcFunc-raccum .
2)
698 (calcFunc-afixp .
2))))))
699 (oper3 (if (eq (nth 2 oper
) 'calcFunc-inner
)
701 (format "%s%s, inner (add)" msg dir
))
704 (nargs (if (> (nth 1 oper
) 0)
709 (while (and p
(> n
0))
710 (or (math-expr-contains (nth 1 oper2
) (car p
))
711 (math-expr-contains (nth 1 oper3
) (car p
))
712 (setq args
(nconc args
(list (car p
)))
715 (setq oper
(list "" nargs
719 (list (math-build-call
722 (symbol-name (nth 2 oper
))
724 (cons (math-calcFunc-to-var
728 (math-calcFunc-to-var
735 (and nargs
(>= nargs
0)
736 (/= nargs
(nth 1 oper
))
737 (error "Must be a %d-argument operator" nargs
))
738 (append (if forcenargs
739 (cons forcenargs
(cdr (cdr oper
)))
742 (let ((name (concat (if inv
"I" "") (if hyp
"H" "")
743 (if prefix
(char-to-string prefix
) "")
744 (char-to-string key
))))
745 (if (> (length name
) 3)
750 ;;; Convert a variable name (as a formula) into a like-looking function name.
751 (defun math-var-to-calcFunc (f)
752 (if (eq (car-safe f
) 'var
)
753 (if (fboundp (nth 2 f
))
755 (intern (concat "calcFunc-" (symbol-name (nth 1 f
)))))
756 (if (memq (car-safe f
) '(lambda calcFunc-lambda
))
758 (math-reject-arg f
"*Expected a function name"))))
760 ;;; Convert a function name into a like-looking variable name formula.
761 (defun math-calcFunc-to-var (f)
763 (let* ((func (or (cdr (assq f
'( ( + . calcFunc-add
)
769 ( neg . calcFunc-neg
)
770 ( | . calcFunc-vconcat
) )))
772 (base (if (string-match "\\`calcFunc-\\(.+\\)\\'"
774 (math-match-substring (symbol-name func
) 1)
775 (symbol-name func
))))
778 (intern (concat "var-" base
))))
781 ;;; Expand a function call using "lambda" notation.
782 (defun math-build-call (f args
)
783 (if (eq (car-safe f
) 'calcFunc-lambda
)
784 (if (= (length args
) (- (length f
) 2))
785 (math-multi-subst (nth (1- (length f
)) f
) (cdr f
) args
)
786 (calc-record-why "*Wrong number of arguments" f
)
787 (cons 'calcFunc-call
(cons (math-calcFunc-to-var f
) args
)))
788 (if (and (eq f
'calcFunc-neg
)
790 (list 'neg
(car args
))
791 (let ((func (assq f
'( ( calcFunc-add .
+ )
797 ( calcFunc-vconcat . |
) ))))
798 (if (and func
(= (length args
) 2))
799 (cons (cdr func
) args
)
802 ;;; Do substitutions in parallel to avoid crosstalk.
804 ;; The variables math-ms-temp and math-ms-args are local to
805 ;; math-multi-subst, but are used by math-multi-subst-rec, which
806 ;; is called by math-multi-subst.
807 (defvar math-ms-temp
)
808 (defvar math-ms-args
)
810 (defun math-multi-subst (expr olds news
)
811 (let ((math-ms-args nil
)
813 (while (and olds news
)
814 (setq math-ms-args
(cons (cons (car olds
) (car news
)) math-ms-args
)
817 (math-multi-subst-rec expr
)))
819 (defun math-multi-subst-rec (expr)
820 (cond ((setq math-ms-temp
(assoc expr math-ms-args
))
822 ((Math-primp expr
) expr
)
823 ((and (eq (car expr
) 'calcFunc-lambda
) (> (length expr
) 2))
824 (let ((new (list (car expr
)))
825 (math-ms-args math-ms-args
))
826 (while (cdr (setq expr
(cdr expr
)))
827 (setq new
(cons (car expr
) new
))
828 (if (assoc (car expr
) math-ms-args
)
829 (setq math-ms-args
(cons (cons (car expr
) (car expr
))
831 (nreverse (cons (math-multi-subst-rec (car expr
)) new
))))
834 (mapcar 'math-multi-subst-rec
(cdr expr
))))))
836 (defun calcFunc-call (f &rest args
)
837 (setq args
(math-build-call (math-var-to-calcFunc f
) args
))
838 (if (eq (car-safe args
) 'calcFunc-call
)
840 (math-normalize args
)))
842 (defun calcFunc-apply (f args
)
843 (or (Math-vectorp args
)
844 (math-reject-arg args
'vectorp
))
845 (apply 'calcFunc-call
(cons f
(cdr args
))))
850 ;;; Map a function over a vector symbolically. [Public]
851 (defun math-symb-map (f mode args
)
852 (let* ((func (math-var-to-calcFunc f
))
853 (nargs (length args
))
854 (ptrs (vconcat args
))
855 (vflags (make-vector nargs nil
))
860 (math-working-step 0)
861 (math-working-step-2 nil
)
865 heads
'(calcFunc-eq calcFunc-neq calcFunc-lt calcFunc-gt
866 calcFunc-leq calcFunc-geq
))
867 (while (and (< (setq i
(1+ i
)) nargs
)
868 (not (math-matrixp (aref ptrs i
)))))
871 (setq func
(list 'lambda
'(&rest x
)
873 (list 'quote f
) '(quote elems
) 'x
))
877 (if (math-matrixp (aref ptrs i
))
878 (aset ptrs i
(math-transpose (aref ptrs i
))))
882 (while (< (setq i
(1+ i
)) nargs
)
883 (setq obj
(aref ptrs i
))
884 (if (and (memq (car-safe obj
) heads
)
892 (aref (aref [ [0 1 2 3 4 5]
898 (- 6 (length (memq head heads
))))
899 (- 6 (length (memq (car obj
) heads
))))
901 (setq head
(car obj
)))
903 (or (= (length obj
) len
)
904 (math-dimension-error))
905 (setq len
(length obj
))))))
908 (math-reject-arg (aref ptrs
0) 'vectorp
)
909 (math-reject-arg nil
"At least one argument must be a vector")))
910 (setq math-working-step-2
(1- len
))
911 (while (> (setq len
(1- len
)) 0)
914 (while (< (setq i
(1+ i
)) nargs
)
917 (aset ptrs i
(cdr (aref ptrs i
)))
918 (setq expr
(nconc expr
(list (car (aref ptrs i
))))))
919 (setq expr
(nconc expr
(list (aref ptrs i
))))))
920 (setq math-working-step
(1+ math-working-step
)
921 vec
(cons (math-normalize (math-build-call func expr
)) vec
)))
922 (setq vec
(cons head
(nreverse vec
)))
923 (if (and (eq mode
'cols
) (math-matrixp vec
))
927 (defun calcFunc-map (func &rest args
)
928 (math-symb-map func
'elems args
))
930 (defun calcFunc-mapr (func &rest args
)
931 (math-symb-map func
'rows args
))
933 (defun calcFunc-mapc (func &rest args
)
934 (math-symb-map func
'cols args
))
936 (defun calcFunc-mapa (func arg
)
937 (if (math-matrixp arg
)
938 (math-symb-map func
'elems
(cdr (math-transpose arg
)))
939 (math-symb-map func
'elems arg
)))
941 (defun calcFunc-mapd (func arg
)
942 (if (math-matrixp arg
)
943 (math-symb-map func
'elems
(cdr arg
))
944 (math-symb-map func
'elems arg
)))
946 (defun calcFunc-mapeq (func &rest args
)
947 (if (and (or (equal func
'(var mul var-mul
))
948 (equal func
'(var div var-div
)))
950 (if (math-negp (car args
))
951 (let ((func (nth 1 (assq (car-safe (nth 1 args
))
952 calc-tweak-eqn-table
))))
953 (and func
(setq args
(list (car args
)
954 (cons func
(cdr (nth 1 args
)))))))
955 (if (math-negp (nth 1 args
))
956 (let ((func (nth 1 (assq (car-safe (car args
))
957 calc-tweak-eqn-table
))))
958 (and func
(setq args
(list (cons func
(cdr (car args
)))
960 (if (or (and (equal func
'(var div var-div
))
961 (assq (car-safe (nth 1 args
)) calc-tweak-eqn-table
))
962 (equal func
'(var neg var-neg
))
963 (equal func
'(var inv var-inv
)))
964 (apply 'calcFunc-mapeqr func args
)
965 (apply 'calcFunc-mapeqp func args
)))
967 (defun calcFunc-mapeqr (func &rest args
)
968 (setq args
(mapcar (function (lambda (x)
969 (let ((func (assq (car-safe x
)
970 calc-tweak-eqn-table
)))
972 (cons (nth 1 func
) (cdr x
))
975 (apply 'calcFunc-mapeqp func args
))
977 (defun calcFunc-mapeqp (func &rest args
)
978 (if (or (and (memq (car-safe (car args
)) '(calcFunc-lt calcFunc-leq
))
979 (memq (car-safe (nth 1 args
)) '(calcFunc-gt calcFunc-geq
)))
980 (and (memq (car-safe (car args
)) '(calcFunc-gt calcFunc-geq
))
981 (memq (car-safe (nth 1 args
)) '(calcFunc-lt calcFunc-leq
))))
982 (setq args
(cons (car args
)
983 (cons (list (nth 1 (assq (car (nth 1 args
))
984 calc-tweak-eqn-table
))
986 (nth 1 (nth 1 args
)))
988 (math-symb-map func
'eqn args
))
992 ;;; Reduce a function over a vector symbolically. [Public]
993 (defun calcFunc-reduce (func vec
)
994 (if (math-matrixp vec
)
996 (setq func
(math-var-to-calcFunc func
))
997 (while (setq vec
(cdr vec
))
999 (while (setq row
(cdr row
))
1001 (if (Math-numberp expr
)
1003 (math-build-call func
(list expr
(car row
))))
1004 (math-build-call func
(list expr
(car row
))))
1006 (math-normalize expr
))
1007 (calcFunc-reducer func vec
)))
1009 (defun calcFunc-rreduce (func vec
)
1010 (if (math-matrixp vec
)
1012 (setq func
(math-var-to-calcFunc func
)
1013 vec
(reverse (cdr vec
)))
1015 (setq row
(reverse (cdr (car vec
))))
1018 (math-build-call func
(list (car row
) expr
))
1021 (setq vec
(cdr vec
)))
1022 (math-normalize expr
))
1023 (calcFunc-rreducer func vec
)))
1025 (defun calcFunc-reducer (func vec
)
1026 (setq func
(math-var-to-calcFunc func
))
1027 (or (math-vectorp vec
)
1028 (math-reject-arg vec
'vectorp
))
1029 (let ((expr (car (setq vec
(cdr vec
)))))
1034 (let ((lfunc (or (cdr (assq func
1035 '( (calcFunc-add . math-add
)
1036 (calcFunc-sub . math-sub
)
1037 (calcFunc-mul . math-mul
)
1038 (calcFunc-div . math-div
)
1039 (calcFunc-pow . math-pow
)
1040 (calcFunc-mod . math-mod
)
1045 (setq expr
(funcall lfunc expr
(nth 1 vec
))
1048 (while (setq vec
(cdr vec
))
1049 (setq expr
(math-build-call func
(list expr
(car vec
)))))
1050 (math-normalize expr
))
1051 (or (math-identity-value func
)
1052 (math-reject-arg vec
"*Vector is empty")))))
1054 (defun math-identity-value (func)
1055 (cdr (assq func
'( (calcFunc-add .
0) (calcFunc-sub .
0)
1056 (calcFunc-mul .
1) (calcFunc-div .
1)
1057 (calcFunc-idiv .
1) (calcFunc-fdiv .
1)
1058 (calcFunc-min .
(var inf var-inf
))
1059 (calcFunc-max .
(neg (var inf var-inf
)))
1060 (calcFunc-vconcat .
(vec))
1061 (calcFunc-append .
(vec)) ))))
1063 (defun calcFunc-rreducer (func vec
)
1064 (setq func
(math-var-to-calcFunc func
))
1065 (or (math-vectorp vec
)
1066 (math-reject-arg vec
'vectorp
))
1067 (if (eq func
'calcFunc-sub
) ; do this in a way that looks nicer
1068 (let ((expr (car (setq vec
(cdr vec
)))))
1071 (while (setq vec
(cdr vec
))
1072 (setq expr
(math-build-call func
(list expr
(car vec
)))
1073 func
(if (eq func
'calcFunc-sub
)
1074 'calcFunc-add
'calcFunc-sub
)))
1075 (math-normalize expr
))
1077 (let ((expr (car (setq vec
(reverse (cdr vec
))))))
1080 (while (setq vec
(cdr vec
))
1081 (setq expr
(math-build-call func
(list (car vec
) expr
))))
1082 (math-normalize expr
))
1083 (or (math-identity-value func
)
1084 (math-reject-arg vec
"*Vector is empty"))))))
1086 (defun calcFunc-reducec (func vec
)
1087 (if (math-matrixp vec
)
1088 (calcFunc-reducer func
(math-transpose vec
))
1089 (calcFunc-reducer func vec
)))
1091 (defun calcFunc-rreducec (func vec
)
1092 (if (math-matrixp vec
)
1093 (calcFunc-rreducer func
(math-transpose vec
))
1094 (calcFunc-rreducer func vec
)))
1096 (defun calcFunc-reducea (func vec
)
1097 (if (math-matrixp vec
)
1099 (mapcar (function (lambda (x) (calcFunc-reducer func x
)))
1101 (calcFunc-reducer func vec
)))
1103 (defun calcFunc-rreducea (func vec
)
1104 (if (math-matrixp vec
)
1106 (mapcar (function (lambda (x) (calcFunc-rreducer func x
)))
1108 (calcFunc-rreducer func vec
)))
1110 (defun calcFunc-reduced (func vec
)
1111 (if (math-matrixp vec
)
1113 (mapcar (function (lambda (x) (calcFunc-reducer func x
)))
1114 (cdr (math-transpose vec
))))
1115 (calcFunc-reducer func vec
)))
1117 (defun calcFunc-rreduced (func vec
)
1118 (if (math-matrixp vec
)
1120 (mapcar (function (lambda (x) (calcFunc-rreducer func x
)))
1121 (cdr (math-transpose vec
))))
1122 (calcFunc-rreducer func vec
)))
1124 (defun calcFunc-accum (func vec
)
1125 (setq func
(math-var-to-calcFunc func
))
1126 (or (math-vectorp vec
)
1127 (math-reject-arg vec
'vectorp
))
1128 (let* ((expr (car (setq vec
(cdr vec
))))
1129 (res (list 'vec expr
)))
1131 (math-reject-arg vec
"*Vector is empty"))
1132 (while (setq vec
(cdr vec
))
1133 (setq expr
(math-build-call func
(list expr
(car vec
)))
1134 res
(nconc res
(list expr
))))
1135 (math-normalize res
)))
1137 (defun calcFunc-raccum (func vec
)
1138 (setq func
(math-var-to-calcFunc func
))
1139 (or (math-vectorp vec
)
1140 (math-reject-arg vec
'vectorp
))
1141 (let* ((expr (car (setq vec
(reverse (cdr vec
)))))
1144 (math-reject-arg vec
"*Vector is empty"))
1145 (while (setq vec
(cdr vec
))
1146 (setq expr
(math-build-call func
(list (car vec
) expr
))
1147 res
(cons (list expr
) res
)))
1148 (math-normalize (cons 'vec res
))))
1151 (defun math-nest-calls (func base iters accum tol
)
1153 (if (math-realp tol
)
1154 (or (math-numberp base
) (math-reject-arg base
'numberp
))
1155 (math-reject-arg tol
'realp
)))
1156 (setq func
(math-var-to-calcFunc func
))
1158 (if (equal iters
'(var inf var-inf
))
1161 (if (math-messy-integerp iters
)
1162 (setq iters
(math-trunc iters
)))
1163 (or (integerp iters
) (math-reject-arg iters
'fixnump
))
1164 (or (not tol
) (natnump iters
) (math-reject-arg iters
'fixnatnump
))
1166 (let* ((dummy '(var DummyArg var-DummyArg
))
1167 (dummy2 '(var DummyArg2 var-DummyArg2
))
1168 (finv (math-solve-for (math-build-call func
(list dummy2
))
1170 (or finv
(math-reject-arg nil
"*Unable to find an inverse"))
1171 (if (and (= (length finv
) 2)
1172 (equal (nth 1 finv
) dummy
))
1173 (setq func
(car finv
))
1174 (setq func
(list 'calcFunc-lambda dummy finv
)))
1175 (setq iters
(- iters
)))))))
1176 (math-with-extra-prec 1
1179 (avalues (list base
))
1180 (math-working-step 0)
1181 (math-working-step-2 iters
))
1182 (while (and (or (null iters
)
1183 (>= (setq iters
(1- iters
)) 0))
1187 (not (if (and (Math-numberp value
)
1188 (Math-numberp ovalue
))
1189 (math-nearly-equal value ovalue
)
1190 (Math-equal value ovalue
)))
1191 (if (math-numberp value
)
1192 (Math-lessp tol
(math-abs (math-sub value ovalue
)))
1193 (math-reject-arg value
'numberp
)))))
1195 math-working-step
(1+ math-working-step
)
1196 value
(math-normalize (math-build-call func
(list value
))))
1198 (setq avalues
(cons value avalues
))))
1200 (cons 'vec
(nreverse avalues
))
1203 (defun calcFunc-nest (func base iters
)
1204 (math-nest-calls func base iters nil nil
))
1206 (defun calcFunc-anest (func base iters
)
1207 (math-nest-calls func base iters t nil
))
1209 (defun calcFunc-fixp (func base
&optional iters tol
)
1210 (math-nest-calls func base iters nil
(or tol t
)))
1212 (defun calcFunc-afixp (func base
&optional iters tol
)
1213 (math-nest-calls func base iters t
(or tol t
)))
1216 (defun calcFunc-outer (func a b
)
1217 (or (math-vectorp a
) (math-reject-arg a
'vectorp
))
1218 (or (math-vectorp b
) (math-reject-arg b
'vectorp
))
1219 (setq func
(math-var-to-calcFunc func
))
1221 (while (setq a
(cdr a
))
1222 (setq mat
(cons (cons 'vec
1223 (mapcar (function (lambda (x)
1224 (math-build-call func
1229 (math-normalize (cons 'vec
(nreverse mat
)))))
1232 ;; The variables math-inner-mul-func and math-inner-add-func are
1233 ;; local to calcFunc-inner, but are used by math-inner-mats,
1234 ;; which is called by math-inner-mats.
1235 (defvar math-inner-mul-func
)
1236 (defvar math-inner-add-func
)
1238 (defun calcFunc-inner (math-inner-mul-func math-inner-add-func a b
)
1239 (or (math-vectorp a
) (math-reject-arg a
'vectorp
))
1240 (or (math-vectorp b
) (math-reject-arg b
'vectorp
))
1241 (if (math-matrixp a
)
1242 (if (math-matrixp b
)
1243 (if (= (length (nth 1 a
)) (length b
))
1244 (math-inner-mats a b
)
1245 (math-dimension-error))
1246 (if (= (length (nth 1 a
)) 2)
1247 (if (= (length a
) (length b
))
1248 (math-inner-mats a
(list 'vec b
))
1249 (math-dimension-error))
1250 (if (= (length (nth 1 a
)) (length b
))
1251 (math-mat-col (math-inner-mats a
(math-col-matrix b
))
1253 (math-dimension-error))))
1254 (if (math-matrixp b
)
1255 (nth 1 (math-inner-mats (list 'vec a
) b
))
1256 (calcFunc-reduce math-inner-add-func
(calcFunc-map math-inner-mul-func a b
)))))
1258 (defun math-inner-mats (a b
)
1260 (cols (length (nth 1 b
)))
1261 row col ap bp accum
)
1262 (while (setq a
(cdr a
))
1265 (while (> (setq col
(1- col
)) 0)
1266 (setq row
(cons (calcFunc-reduce math-inner-add-func
1267 (calcFunc-map math-inner-mul-func
1269 (math-mat-col b col
)))
1271 (setq mat
(cons (cons 'vec row
) mat
)))
1272 (cons 'vec
(nreverse mat
))))
1276 ;;; calc-map.el ends here