2008-11-16 Michael Kifer <kifer@cs.stonybrook.edu>
[emacs.git] / lisp / calc / calc-lang.el
blob205a8f98f45b8cda2a469f8670dda990b125d522
1 ;;; calc-lang.el --- calc language functions
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
28 ;; This file is autoloaded from calc-ext.el.
30 (require 'calc-ext)
31 (require 'calc-macs)
34 ;; Declare functions which are defined elsewhere.
35 (declare-function math-compose-vector "calccomp" (a sep prec))
36 (declare-function math-compose-var "calccomp" (a))
37 (declare-function math-tex-expr-is-flat "calccomp" (a))
38 (declare-function math-read-factor "calc-aent" ())
39 (declare-function math-read-expr-level "calc-aent" (exp-prec &optional exp-term))
41 ;; Declare variables which are defined elsewhere.
42 (defvar calc-lang-slash-idiv)
43 (defvar calc-lang-allow-underscores)
44 (defvar calc-lang-allow-percentsigns)
45 (defvar math-comp-left-bracket)
46 (defvar math-comp-right-bracket)
47 (defvar math-comp-comma)
48 (defvar math-comp-vector-prec)
50 ;;; Alternate entry/display languages.
52 (defun calc-set-language (lang &optional option no-refresh)
53 (setq math-expr-opers (or (get lang 'math-oper-table) (math-standard-ops))
54 math-expr-function-mapping (get lang 'math-function-table)
55 math-expr-variable-mapping (get lang 'math-variable-table)
56 calc-language-input-filter (get lang 'math-input-filter)
57 calc-language-output-filter (get lang 'math-output-filter)
58 calc-vector-brackets (or (get lang 'math-vector-brackets) "[]")
59 calc-complex-format (get lang 'math-complex-format)
60 calc-radix-formatter (get lang 'math-radix-formatter)
61 calc-function-open (or (get lang 'math-function-open) "(")
62 calc-function-close (or (get lang 'math-function-close) ")"))
63 (if no-refresh
64 (setq calc-language lang
65 calc-language-option option)
66 (calc-change-mode '(calc-language calc-language-option)
67 (list lang option) t)))
69 (defun calc-normal-language ()
70 (interactive)
71 (calc-wrapper
72 (calc-set-language nil)
73 (message "Normal language mode")))
75 (defun calc-flat-language ()
76 (interactive)
77 (calc-wrapper
78 (calc-set-language 'flat)
79 (message "Flat language mode (all stack entries shown on one line)")))
81 (defun calc-big-language ()
82 (interactive)
83 (calc-wrapper
84 (calc-set-language 'big)
85 (message "\"Big\" language mode")))
87 (defun calc-unformatted-language ()
88 (interactive)
89 (calc-wrapper
90 (calc-set-language 'unform)
91 (message "Unformatted language mode")))
94 (defun calc-c-language ()
95 (interactive)
96 (calc-wrapper
97 (calc-set-language 'c)
98 (message "`C' language mode")))
100 (put 'c 'math-oper-table
101 '( ( "u!" calcFunc-lnot -1 1000 )
102 ( "~" calcFunc-not -1 1000 )
103 ( "u+" ident -1 197 )
104 ( "u-" neg -1 197 )
105 ( "*" * 190 191 )
106 ( "/" / 190 191 )
107 ( "%" % 190 191 )
108 ( "+" + 180 181 )
109 ( "-" - 180 181 )
110 ( "<<" calcFunc-lsh 170 171 )
111 ( ">>" calcFunc-rsh 170 171 )
112 ( "<" calcFunc-lt 160 161 )
113 ( ">" calcFunc-gt 160 161 )
114 ( "<=" calcFunc-leq 160 161 )
115 ( ">=" calcFunc-geq 160 161 )
116 ( "==" calcFunc-eq 150 151 )
117 ( "!=" calcFunc-neq 150 151 )
118 ( "&" calcFunc-and 140 141 )
119 ( "^" calcFunc-xor 131 130 )
120 ( "|" calcFunc-or 120 121 )
121 ( "&&" calcFunc-land 110 111 )
122 ( "||" calcFunc-lor 100 101 )
123 ( "?" (math-read-if) 91 90 )
124 ( "!!!" calcFunc-pnot -1 88 )
125 ( "&&&" calcFunc-pand 85 86 )
126 ( "|||" calcFunc-por 75 76 )
127 ( "=" calcFunc-assign 51 50 )
128 ( ":=" calcFunc-assign 51 50 )
129 ( "::" calcFunc-condition 45 46 ))) ; should support full assignments
131 (put 'c 'math-function-table
132 '( ( acos . calcFunc-arccos )
133 ( acosh . calcFunc-arccosh )
134 ( asin . calcFunc-arcsin )
135 ( asinh . calcFunc-arcsinh )
136 ( atan . calcFunc-arctan )
137 ( atan2 . calcFunc-arctan2 )
138 ( atanh . calcFunc-arctanh )))
140 (put 'c 'math-variable-table
141 '( ( M_PI . var-pi )
142 ( M_E . var-e )))
144 (put 'c 'math-vector-brackets "{}")
146 (put 'c 'math-radix-formatter
147 (function (lambda (r s)
148 (if (= r 16) (format "0x%s" s)
149 (if (= r 8) (format "0%s" s)
150 (format "%d#%s" r s))))))
152 (put 'c 'math-compose-subscr
153 (function
154 (lambda (a)
155 (let ((args (cdr (cdr a))))
156 (list 'horiz
157 (math-compose-expr (nth 1 a) 1000)
159 (math-compose-vector args ", " 0)
160 "]")))))
162 (add-to-list 'calc-lang-slash-idiv 'c)
163 (add-to-list 'calc-lang-allow-underscores 'c)
164 (add-to-list 'calc-lang-c-type-hex 'c)
165 (add-to-list 'calc-lang-brackets-are-subscripts 'c)
167 (defun calc-pascal-language (n)
168 (interactive "P")
169 (calc-wrapper
170 (and n (setq n (prefix-numeric-value n)))
171 (calc-set-language 'pascal n)
172 (message (if (and n (/= n 0))
173 (if (> n 0)
174 "Pascal language mode (all uppercase)"
175 "Pascal language mode (all lowercase)")
176 "Pascal language mode"))))
178 (put 'pascal 'math-oper-table
179 '( ( "not" calcFunc-lnot -1 1000 )
180 ( "*" * 190 191 )
181 ( "/" / 190 191 )
182 ( "and" calcFunc-and 190 191 )
183 ( "div" calcFunc-idiv 190 191 )
184 ( "mod" % 190 191 )
185 ( "u+" ident -1 185 )
186 ( "u-" neg -1 185 )
187 ( "+" + 180 181 )
188 ( "-" - 180 181 )
189 ( "or" calcFunc-or 180 181 )
190 ( "xor" calcFunc-xor 180 181 )
191 ( "shl" calcFunc-lsh 180 181 )
192 ( "shr" calcFunc-rsh 180 181 )
193 ( "in" calcFunc-in 160 161 )
194 ( "<" calcFunc-lt 160 161 )
195 ( ">" calcFunc-gt 160 161 )
196 ( "<=" calcFunc-leq 160 161 )
197 ( ">=" calcFunc-geq 160 161 )
198 ( "=" calcFunc-eq 160 161 )
199 ( "<>" calcFunc-neq 160 161 )
200 ( "!!!" calcFunc-pnot -1 85 )
201 ( "&&&" calcFunc-pand 80 81 )
202 ( "|||" calcFunc-por 75 76 )
203 ( ":=" calcFunc-assign 51 50 )
204 ( "::" calcFunc-condition 45 46 )))
206 (put 'pascal 'math-input-filter 'calc-input-case-filter)
207 (put 'pascal 'math-output-filter 'calc-output-case-filter)
209 (put 'pascal 'math-radix-formatter
210 (function (lambda (r s)
211 (if (= r 16) (format "$%s" s)
212 (format "%d#%s" r s)))))
214 (put 'pascal 'math-lang-read-symbol
215 '((?\$
216 (eq (string-match
217 "\\(\\$[0-9a-fA-F]+\\)\\($\\|[^0-9a-zA-Z]\\)"
218 math-exp-str math-exp-pos)
219 math-exp-pos)
220 (setq math-exp-token 'number
221 math-expr-data (math-match-substring math-exp-str 1)
222 math-exp-pos (match-end 1)))))
224 (put 'pascal 'math-compose-subscr
225 (function
226 (lambda (a)
227 (let ((args (cdr (cdr a))))
228 (while (eq (car-safe (nth 1 a)) 'calcFunc-subscr)
229 (setq args (append (cdr (cdr (nth 1 a))) args)
230 a (nth 1 a)))
231 (list 'horiz
232 (math-compose-expr (nth 1 a) 1000)
234 (math-compose-vector args ", " 0)
235 "]")))))
237 (add-to-list 'calc-lang-allow-underscores 'pascal)
238 (add-to-list 'calc-lang-brackets-are-subscripts 'pascal)
240 (defun calc-input-case-filter (str)
241 (cond ((or (null calc-language-option) (= calc-language-option 0))
242 str)
244 (downcase str))))
246 (defun calc-output-case-filter (str)
247 (cond ((or (null calc-language-option) (= calc-language-option 0))
248 str)
249 ((> calc-language-option 0)
250 (upcase str))
252 (downcase str))))
255 (defun calc-fortran-language (n)
256 (interactive "P")
257 (calc-wrapper
258 (and n (setq n (prefix-numeric-value n)))
259 (calc-set-language 'fortran n)
260 (message (if (and n (/= n 0))
261 (if (> n 0)
262 "FORTRAN language mode (all uppercase)"
263 "FORTRAN language mode (all lowercase)")
264 "FORTRAN language mode"))))
266 (put 'fortran 'math-oper-table
267 '( ( "u/" (math-parse-fortran-vector) -1 1 )
268 ( "/" (math-parse-fortran-vector-end) 1 -1 )
269 ( "**" ^ 201 200 )
270 ( "u+" ident -1 191 )
271 ( "u-" neg -1 191 )
272 ( "*" * 190 191 )
273 ( "/" / 190 191 )
274 ( "+" + 180 181 )
275 ( "-" - 180 181 )
276 ( ".LT." calcFunc-lt 160 161 )
277 ( ".GT." calcFunc-gt 160 161 )
278 ( ".LE." calcFunc-leq 160 161 )
279 ( ".GE." calcFunc-geq 160 161 )
280 ( ".EQ." calcFunc-eq 160 161 )
281 ( ".NE." calcFunc-neq 160 161 )
282 ( ".NOT." calcFunc-lnot -1 121 )
283 ( ".AND." calcFunc-land 110 111 )
284 ( ".OR." calcFunc-lor 100 101 )
285 ( "!!!" calcFunc-pnot -1 85 )
286 ( "&&&" calcFunc-pand 80 81 )
287 ( "|||" calcFunc-por 75 76 )
288 ( "=" calcFunc-assign 51 50 )
289 ( ":=" calcFunc-assign 51 50 )
290 ( "::" calcFunc-condition 45 46 )))
292 (put 'fortran 'math-vector-brackets "//")
294 (put 'fortran 'math-function-table
295 '( ( acos . calcFunc-arccos )
296 ( acosh . calcFunc-arccosh )
297 ( aimag . calcFunc-im )
298 ( aint . calcFunc-ftrunc )
299 ( asin . calcFunc-arcsin )
300 ( asinh . calcFunc-arcsinh )
301 ( atan . calcFunc-arctan )
302 ( atan2 . calcFunc-arctan2 )
303 ( atanh . calcFunc-arctanh )
304 ( conjg . calcFunc-conj )
305 ( log . calcFunc-ln )
306 ( nint . calcFunc-round )
307 ( real . calcFunc-re )))
309 (put 'fortran 'math-input-filter 'calc-input-case-filter)
311 (put 'fortran 'math-output-filter 'calc-output-case-filter)
313 (put 'fortran 'math-lang-read-symbol
314 '((?\.
315 (eq (string-match "\\.[a-zA-Z][a-zA-Z][a-zA-Z]?\\."
316 math-exp-str math-exp-pos) math-exp-pos)
317 (setq math-exp-token 'punc
318 math-expr-data (upcase (math-match-substring math-exp-str 0))
319 math-exp-pos (match-end 0)))))
321 (put 'fortran 'math-compose-subscr
322 (function
323 (lambda (a)
324 (let ((args (cdr (cdr a))))
325 (while (eq (car-safe (nth 1 a)) 'calcFunc-subscr)
326 (setq args (append (cdr (cdr (nth 1 a))) args)
327 a (nth 1 a)))
328 (list 'horiz
329 (math-compose-expr (nth 1 a) 1000)
331 (math-compose-vector args ", " 0)
332 ")")))))
334 (add-to-list 'calc-lang-slash-idiv 'fortran)
335 (add-to-list 'calc-lang-allow-underscores 'fortran)
336 (add-to-list 'calc-lang-parens-are-subscripts 'fortran)
338 ;; The next few variables are local to math-read-exprs in calc-aent.el
339 ;; and math-read-expr in calc-ext.el, but are set in functions they call.
341 (defvar math-exp-token)
342 (defvar math-expr-data)
343 (defvar math-exp-old-pos)
345 (defvar math-parsing-fortran-vector nil)
346 (defun math-parse-fortran-vector (op)
347 (let ((math-parsing-fortran-vector '(end . "\000")))
348 (prog1
349 (math-read-brackets t "]")
350 (setq math-exp-token (car math-parsing-fortran-vector)
351 math-expr-data (cdr math-parsing-fortran-vector)))))
353 (defun math-parse-fortran-vector-end (x op)
354 (if math-parsing-fortran-vector
355 (progn
356 (setq math-parsing-fortran-vector (cons math-exp-token math-expr-data)
357 math-exp-token 'end
358 math-expr-data "\000")
360 (throw 'syntax "Unmatched closing `/'")))
362 (defun math-parse-fortran-subscr (sym args)
363 (setq sym (math-build-var-name sym))
364 (while args
365 (setq sym (list 'calcFunc-subscr sym (car args))
366 args (cdr args)))
367 sym)
370 (defun calc-tex-language (n)
371 (interactive "P")
372 (calc-wrapper
373 (and n (setq n (prefix-numeric-value n)))
374 (calc-set-language 'tex n)
375 (cond ((not n)
376 (message "TeX language mode"))
377 ((= n 0)
378 (message "TeX language mode with multiline matrices"))
379 ((= n 1)
380 (message "TeX language mode with \\hbox{func}(\\hbox{var})"))
381 ((> n 1)
382 (message
383 "TeX language mode with \\hbox{func}(\\hbox{var}) and multiline matrices"))
384 ((= n -1)
385 (message "TeX language mode with \\func(\\hbox{var})"))
386 ((< n -1)
387 (message
388 "TeX language mode with \\func(\\hbox{var}) and multiline matrices")))))
390 (defun calc-latex-language (n)
391 (interactive "P")
392 (calc-wrapper
393 (and n (setq n (prefix-numeric-value n)))
394 (calc-set-language 'latex n)
395 (cond ((not n)
396 (message "LaTeX language mode"))
397 ((= n 0)
398 (message "LaTeX language mode with multiline matrices"))
399 ((= n 1)
400 (message "LaTeX language mode with \\text{func}(\\text{var})"))
401 ((> n 1)
402 (message
403 "LaTeX language mode with \\text{func}(\\text{var}) and multiline matrices"))
404 ((= n -1)
405 (message "LaTeX language mode with \\func(\\text{var})"))
406 ((< n -1)
407 (message
408 "LaTeX language mode with \\func(\\text{var}) and multiline matrices")))))
410 (put 'tex 'math-lang-name "TeX")
411 (put 'latex 'math-lang-name "LaTeX")
413 (put 'tex 'math-oper-table
414 '( ( "\\hat" calcFunc-hat -1 950 )
415 ( "\\check" calcFunc-check -1 950 )
416 ( "\\tilde" calcFunc-tilde -1 950 )
417 ( "\\acute" calcFunc-acute -1 950 )
418 ( "\\grave" calcFunc-grave -1 950 )
419 ( "\\dot" calcFunc-dot -1 950 )
420 ( "\\ddot" calcFunc-dotdot -1 950 )
421 ( "\\breve" calcFunc-breve -1 950 )
422 ( "\\bar" calcFunc-bar -1 950 )
423 ( "\\vec" calcFunc-Vec -1 950 )
424 ( "\\underline" calcFunc-under -1 950 )
425 ( "u|" calcFunc-abs -1 0 )
426 ( "|" closing 0 -1 )
427 ( "\\lfloor" calcFunc-floor -1 0 )
428 ( "\\rfloor" closing 0 -1 )
429 ( "\\lceil" calcFunc-ceil -1 0 )
430 ( "\\rceil" closing 0 -1 )
431 ( "\\pm" sdev 300 300 )
432 ( "!" calcFunc-fact 210 -1 )
433 ( "^" ^ 201 200 )
434 ( "_" calcFunc-subscr 201 200 )
435 ( "u+" ident -1 197 )
436 ( "u-" neg -1 197 )
437 ( "\\times" * 191 190 )
438 ( "*" * 191 190 )
439 ( "2x" * 191 190 )
440 ( "+" + 180 181 )
441 ( "-" - 180 181 )
442 ( "\\over" / 170 171 )
443 ( "/" / 170 171 )
444 ( "\\choose" calcFunc-choose 170 171 )
445 ( "\\mod" % 170 171 )
446 ( "<" calcFunc-lt 160 161 )
447 ( ">" calcFunc-gt 160 161 )
448 ( "\\leq" calcFunc-leq 160 161 )
449 ( "\\geq" calcFunc-geq 160 161 )
450 ( "=" calcFunc-eq 160 161 )
451 ( "\\neq" calcFunc-neq 160 161 )
452 ( "\\ne" calcFunc-neq 160 161 )
453 ( "\\lnot" calcFunc-lnot -1 121 )
454 ( "\\land" calcFunc-land 110 111 )
455 ( "\\lor" calcFunc-lor 100 101 )
456 ( "?" (math-read-if) 91 90 )
457 ( "!!!" calcFunc-pnot -1 85 )
458 ( "&&&" calcFunc-pand 80 81 )
459 ( "|||" calcFunc-por 75 76 )
460 ( "\\gets" calcFunc-assign 51 50 )
461 ( ":=" calcFunc-assign 51 50 )
462 ( "::" calcFunc-condition 45 46 )
463 ( "\\to" calcFunc-evalto 40 41 )
464 ( "\\to" calcFunc-evalto 40 -1 )
465 ( "=>" calcFunc-evalto 40 41 )
466 ( "=>" calcFunc-evalto 40 -1 )))
468 (put 'tex 'math-function-table
469 '( ( \\arccos . calcFunc-arccos )
470 ( \\arcsin . calcFunc-arcsin )
471 ( \\arctan . calcFunc-arctan )
472 ( \\arg . calcFunc-arg )
473 ( \\cos . calcFunc-cos )
474 ( \\cosh . calcFunc-cosh )
475 ( \\cot . calcFunc-cot )
476 ( \\coth . calcFunc-coth )
477 ( \\csc . calcFunc-csc )
478 ( \\det . calcFunc-det )
479 ( \\exp . calcFunc-exp )
480 ( \\gcd . calcFunc-gcd )
481 ( \\ln . calcFunc-ln )
482 ( \\log . calcFunc-log10 )
483 ( \\max . calcFunc-max )
484 ( \\min . calcFunc-min )
485 ( \\sec . calcFunc-sec )
486 ( \\sin . calcFunc-sin )
487 ( \\sinh . calcFunc-sinh )
488 ( \\sqrt . calcFunc-sqrt )
489 ( \\tan . calcFunc-tan )
490 ( \\tanh . calcFunc-tanh )
491 ( \\phi . calcFunc-totient )
492 ( \\mu . calcFunc-moebius )))
494 (put 'tex 'math-special-function-table
495 '((calcFunc-sum . (math-compose-tex-sum "\\sum"))
496 (calcFunc-prod . (math-compose-tex-sum "\\prod"))
497 (intv . math-compose-tex-intv)))
499 (put 'tex 'math-variable-table
501 ;; The Greek letters
502 ( \\alpha . var-alpha )
503 ( \\beta . var-beta )
504 ( \\gamma . var-gamma )
505 ( \\Gamma . var-Gamma )
506 ( \\delta . var-delta )
507 ( \\Delta . var-Delta )
508 ( \\epsilon . var-epsilon )
509 ( \\varepsilon . var-varepsilon)
510 ( \\zeta . var-zeta )
511 ( \\eta . var-eta )
512 ( \\theta . var-theta )
513 ( \\vartheta . var-vartheta )
514 ( \\Theta . var-Theta )
515 ( \\iota . var-iota )
516 ( \\kappa . var-kappa )
517 ( \\lambda . var-lambda )
518 ( \\Lambda . var-Lambda )
519 ( \\mu . var-mu )
520 ( \\nu . var-nu )
521 ( \\xi . var-xi )
522 ( \\Xi . var-Xi )
523 ( \\pi . var-pi )
524 ( \\varpi . var-varpi )
525 ( \\Pi . var-Pi )
526 ( \\rho . var-rho )
527 ( \\varrho . var-varrho )
528 ( \\sigma . var-sigma )
529 ( \\sigma . var-varsigma )
530 ( \\Sigma . var-Sigma )
531 ( \\tau . var-tau )
532 ( \\upsilon . var-upsilon )
533 ( \\Upsilon . var-Upsilon )
534 ( \\phi . var-phi )
535 ( \\varphi . var-varphi )
536 ( \\Phi . var-Phi )
537 ( \\chi . var-chi )
538 ( \\psi . var-psi )
539 ( \\Psi . var-Psi )
540 ( \\omega . var-omega )
541 ( \\Omega . var-Omega )
542 ;; Others
543 ( \\ell . var-ell )
544 ( \\infty . var-inf )
545 ( \\infty . var-uinf )
546 ( \\sum . (math-parse-tex-sum calcFunc-sum) )
547 ( \\prod . (math-parse-tex-sum calcFunc-prod) )))
549 (put 'tex 'math-punc-table
550 '((?\{ . ?\()
551 (?\} . ?\))
552 (?\& . ?\,)))
554 (put 'tex 'math-complex-format 'i)
556 (put 'tex 'math-input-filter 'math-tex-input-filter)
558 (put 'tex 'math-matrix-formatter
559 (function
560 (lambda (a)
561 (if (and (integerp calc-language-option)
562 (or (= calc-language-option 0)
563 (> calc-language-option 1)
564 (< calc-language-option -1)))
565 (append '(vleft 0 "\\matrix{")
566 (math-compose-tex-matrix (cdr a))
567 '("}"))
568 (append '(horiz "\\matrix{ ")
569 (math-compose-tex-matrix (cdr a))
570 '(" }"))))))
572 (put 'tex 'math-var-formatter 'math-compose-tex-var)
574 (put 'tex 'math-func-formatter 'math-compose-tex-func)
576 (put 'tex 'math-dots "\\ldots")
578 (put 'tex 'math-big-parens '("\\left( " . " \\right)"))
580 (put 'tex 'math-evalto '("\\evalto " . " \\to "))
582 (defconst math-tex-ignore-words
583 '( ("\\hbox") ("\\mbox") ("\\text") ("\\left") ("\\right")
584 ("\\,") ("\\>") ("\\:") ("\\;") ("\\!") ("\\ ")
585 ("\\quad") ("\\qquad") ("\\hfil") ("\\hfill")
586 ("\\displaystyle") ("\\textstyle") ("\\dsize") ("\\tsize")
587 ("\\scriptstyle") ("\\scriptscriptstyle") ("\\ssize") ("\\sssize")
588 ("\\rm") ("\\bf") ("\\it") ("\\sl")
589 ("\\roman") ("\\bold") ("\\italic") ("\\slanted")
590 ("\\cal") ("\\mit") ("\\Cal") ("\\Bbb") ("\\frak") ("\\goth")
591 ("\\evalto")
592 ("\\matrix" mat) ("\\bmatrix" mat) ("\\pmatrix" mat)
593 ("\\begin" begenv)
594 ("\\cr" punc ";") ("\\\\" punc ";") ("\\*" punc "*")
595 ("\\{" punc "[") ("\\}" punc "]")))
597 (defconst math-latex-ignore-words
598 (append math-tex-ignore-words
599 '(("\\begin" begenv))))
601 (put 'tex 'math-lang-read-symbol
602 '((?\\
603 (< math-exp-pos (1- (length math-exp-str)))
604 (progn
605 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
606 math-exp-str math-exp-pos)
607 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
608 math-exp-str math-exp-pos))
609 (setq math-exp-token 'symbol
610 math-exp-pos (match-end 0)
611 math-expr-data (math-restore-dashes
612 (math-match-substring math-exp-str 1)))
613 (let ((code (assoc math-expr-data math-latex-ignore-words)))
614 (cond ((null code))
615 ((null (cdr code))
616 (math-read-token))
617 ((eq (nth 1 code) 'punc)
618 (setq math-exp-token 'punc
619 math-expr-data (nth 2 code)))
620 ((and (eq (nth 1 code) 'mat)
621 (string-match " *{" math-exp-str math-exp-pos))
622 (setq math-exp-pos (match-end 0)
623 math-exp-token 'punc
624 math-expr-data "[")
625 (let ((right (string-match "}" math-exp-str math-exp-pos)))
626 (and right
627 (setq math-exp-str (copy-sequence math-exp-str))
628 (aset math-exp-str right ?\]))))))))))
630 (defun math-compose-tex-matrix (a &optional ltx)
631 (if (cdr a)
632 (cons (append (math-compose-vector (cdr (car a)) " & " 0)
633 (if ltx '(" \\\\ ") '(" \\cr ")))
634 (math-compose-tex-matrix (cdr a) ltx))
635 (list (math-compose-vector (cdr (car a)) " & " 0))))
637 (defun math-compose-tex-sum (a fn)
638 (cond
639 ((nth 4 a)
640 (list 'horiz (nth 1 fn)
641 "_{" (math-compose-expr (nth 2 a) 0)
642 "=" (math-compose-expr (nth 3 a) 0)
643 "}^{" (math-compose-expr (nth 4 a) 0)
644 "}{" (math-compose-expr (nth 1 a) 0) "}"))
645 ((nth 3 a)
646 (list 'horiz (nth 1 fn)
647 "_{" (math-compose-expr (nth 2 a) 0)
648 "=" (math-compose-expr (nth 3 a) 0)
649 "}{" (math-compose-expr (nth 1 a) 0) "}"))
651 (list 'horiz (nth 1 fn)
652 "_{" (math-compose-expr (nth 2 a) 0)
653 "}{" (math-compose-expr (nth 1 a) 0) "}"))))
655 (defun math-parse-tex-sum (f val)
656 (let (low high save)
657 (or (equal math-expr-data "_") (throw 'syntax "Expected `_'"))
658 (math-read-token)
659 (setq save math-exp-old-pos)
660 (setq low (math-read-factor))
661 (or (eq (car-safe low) 'calcFunc-eq)
662 (progn
663 (setq math-exp-old-pos (1+ save))
664 (throw 'syntax "Expected equation")))
665 (or (equal math-expr-data "^") (throw 'syntax "Expected `^'"))
666 (math-read-token)
667 (setq high (math-read-factor))
668 (list (nth 2 f) (math-read-factor) (nth 1 low) (nth 2 low) high)))
670 (defun math-tex-input-filter (str) ; allow parsing of 123\,456\,789.
671 (while (string-match "[0-9]\\\\,[0-9]" str)
672 (setq str (concat (substring str 0 (1+ (match-beginning 0)))
673 (substring str (1- (match-end 0))))))
674 str)
676 ;(defun math-tex-print-sqrt (a)
677 ; (list 'horiz
678 ; "\\sqrt{"
679 ; (math-compose-expr (nth 1 a) 0)
680 ; "}"))
682 (defun math-compose-tex-intv (a)
683 (list 'horiz
684 (if (memq (nth 1 a) '(0 1)) "(" "[")
685 (math-compose-expr (nth 2 a) 0)
686 " \\ldots "
687 (math-compose-expr (nth 3 a) 0)
688 (if (memq (nth 1 a) '(0 2)) ")" "]")))
690 (defun math-compose-tex-var (a prec)
691 (if (and calc-language-option
692 (not (= calc-language-option 0))
693 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'"
694 (symbol-name (nth 1 a))))
695 (if (eq calc-language 'latex)
696 (format "\\text{%s}" (symbol-name (nth 1 a)))
697 (format "\\hbox{%s}" (symbol-name (nth 1 a))))
698 (math-compose-var a)))
700 (defun math-compose-tex-func (func a)
701 (let (left right)
702 (if (and calc-language-option
703 (not (= calc-language-option 0))
704 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'" func))
705 (if (< (prefix-numeric-value calc-language-option) 0)
706 (setq func (format "\\%s" func))
707 (setq func (if (eq calc-language 'latex)
708 (format "\\text{%s}" func)
709 (format "\\hbox{%s}" func)))))
710 (cond ((or (> (length a) 2)
711 (not (math-tex-expr-is-flat (nth 1 a))))
712 (setq left "\\left( "
713 right " \\right)"))
714 ((and (eq (aref func 0) ?\\)
715 (not (or
716 (string-match "\\hbox{" func)
717 (string-match "\\text{" func)))
718 (= (length a) 2)
719 (or (Math-realp (nth 1 a))
720 (memq (car (nth 1 a)) '(var *))))
721 (setq left "{" right "}"))
722 (t (setq left calc-function-open
723 right calc-function-close)))
724 (list 'horiz func
725 left
726 (math-compose-vector (cdr a) ", " 0)
727 right)))
729 (put 'latex 'math-oper-table
730 (append (get 'tex 'math-oper-table)
731 '(( "\\Hat" calcFunc-Hat -1 950 )
732 ( "\\Check" calcFunc-Check -1 950 )
733 ( "\\Tilde" calcFunc-Tilde -1 950 )
734 ( "\\Acute" calcFunc-Acute -1 950 )
735 ( "\\Grave" calcFunc-Grave -1 950 )
736 ( "\\Dot" calcFunc-Dot -1 950 )
737 ( "\\Ddot" calcFunc-Dotdot -1 950 )
738 ( "\\Breve" calcFunc-Breve -1 950 )
739 ( "\\Bar" calcFunc-Bar -1 950 )
740 ( "\\Vec" calcFunc-VEC -1 950 )
741 ( "\\dddot" calcFunc-dddot -1 950 )
742 ( "\\ddddot" calcFunc-ddddot -1 950 )
743 ( "\\div" / 170 171 )
744 ( "\\le" calcFunc-leq 160 161 )
745 ( "\\leqq" calcFunc-leq 160 161 )
746 ( "\\leqsland" calcFunc-leq 160 161 )
747 ( "\\ge" calcFunc-geq 160 161 )
748 ( "\\geqq" calcFunc-geq 160 161 )
749 ( "\\geqslant" calcFunc-geq 160 161 )
750 ( "=" calcFunc-eq 160 161 )
751 ( "\\neq" calcFunc-neq 160 161 )
752 ( "\\ne" calcFunc-neq 160 161 )
753 ( "\\lnot" calcFunc-lnot -1 121 )
754 ( "\\land" calcFunc-land 110 111 )
755 ( "\\lor" calcFunc-lor 100 101 )
756 ( "?" (math-read-if) 91 90 )
757 ( "!!!" calcFunc-pnot -1 85 )
758 ( "&&&" calcFunc-pand 80 81 )
759 ( "|||" calcFunc-por 75 76 )
760 ( "\\gets" calcFunc-assign 51 50 )
761 ( ":=" calcFunc-assign 51 50 )
762 ( "::" calcFunc-condition 45 46 )
763 ( "\\to" calcFunc-evalto 40 41 )
764 ( "\\to" calcFunc-evalto 40 -1 )
765 ( "=>" calcFunc-evalto 40 41 )
766 ( "=>" calcFunc-evalto 40 -1 ))))
768 (put 'latex 'math-function-table
769 (append
770 (get 'tex 'math-function-table)
771 '(( \\frac . (math-latex-parse-frac))
772 ( \\tfrac . (math-latex-parse-frac))
773 ( \\dfrac . (math-latex-parse-frac))
774 ( \\binom . (math-latex-parse-two-args calcFunc-choose))
775 ( \\tbinom . (math-latex-parse-two-args calcFunc-choose))
776 ( \\dbinom . (math-latex-parse-two-args calcFunc-choose))
777 ( \\phi . calcFunc-totient )
778 ( \\mu . calcFunc-moebius ))))
780 (put 'latex 'math-special-function-table
781 '((/ . (math-compose-latex-frac "\\frac"))
782 (calcFunc-choose . (math-compose-latex-frac "\\binom"))
783 (calcFunc-sum . (math-compose-tex-sum "\\sum"))
784 (calcFunc-prod . (math-compose-tex-sum "\\prod"))
785 (intv . math-compose-tex-intv)))
787 (put 'latex 'math-variable-table
788 (get 'tex 'math-variable-table))
790 (put 'latex 'math-punc-table
791 '((?\{ . ?\()
792 (?\} . ?\))
793 (?\& . ?\,)))
795 (put 'latex 'math-complex-format 'i)
797 (put 'latex 'math-matrix-formatter
798 (function
799 (lambda (a)
800 (if (and (integerp calc-language-option)
801 (or (= calc-language-option 0)
802 (> calc-language-option 1)
803 (< calc-language-option -1)))
804 (append '(vleft 0 "\\begin{pmatrix}")
805 (math-compose-tex-matrix (cdr a) t)
806 '("\\end{pmatrix}"))
807 (append '(horiz "\\begin{pmatrix} ")
808 (math-compose-tex-matrix (cdr a) t)
809 '(" \\end{pmatrix}"))))))
811 (put 'latex 'math-var-formatter 'math-compose-tex-var)
813 (put 'latex 'math-func-formatter 'math-compose-tex-func)
815 (put 'latex 'math-dots "\\ldots")
817 (put 'latex 'math-big-parens '("\\left( " . " \\right)"))
819 (put 'latex 'math-evalto '("\\evalto " . " \\to "))
821 (put 'latex 'math-lang-read-symbol
822 '((?\\
823 (< math-exp-pos (1- (length math-exp-str)))
824 (progn
825 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
826 math-exp-str math-exp-pos)
827 (string-match "\\\\text *{\\([a-zA-Z0-9]+\\)}"
828 math-exp-str math-exp-pos)
829 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
830 math-exp-str math-exp-pos))
831 (setq math-exp-token 'symbol
832 math-exp-pos (match-end 0)
833 math-expr-data (math-restore-dashes
834 (math-match-substring math-exp-str 1)))
835 (let ((code (assoc math-expr-data math-tex-ignore-words))
836 envname)
837 (cond ((null code))
838 ((null (cdr code))
839 (math-read-token))
840 ((eq (nth 1 code) 'punc)
841 (setq math-exp-token 'punc
842 math-expr-data (nth 2 code)))
843 ((and (eq (nth 1 code) 'begenv)
844 (string-match " *{\\([^}]*\\)}" math-exp-str math-exp-pos))
845 (setq math-exp-pos (match-end 0)
846 envname (match-string 1 math-exp-str)
847 math-exp-token 'punc
848 math-expr-data "[")
849 (cond ((or (string= envname "matrix")
850 (string= envname "bmatrix")
851 (string= envname "smallmatrix")
852 (string= envname "pmatrix"))
853 (if (string-match (concat "\\\\end{" envname "}")
854 math-exp-str math-exp-pos)
855 (setq math-exp-str
856 (replace-match "]" t t math-exp-str))
857 (error "%s" (concat "No closing \\end{" envname "}"))))))
858 ((and (eq (nth 1 code) 'mat)
859 (string-match " *{" math-exp-str math-exp-pos))
860 (setq math-exp-pos (match-end 0)
861 math-exp-token 'punc
862 math-expr-data "[")
863 (let ((right (string-match "}" math-exp-str math-exp-pos)))
864 (and right
865 (setq math-exp-str (copy-sequence math-exp-str))
866 (aset math-exp-str right ?\]))))))))))
868 (defun math-latex-parse-frac (f val)
869 (let (numer denom)
870 (setq numer (car (math-read-expr-list)))
871 (math-read-token)
872 (setq denom (math-read-factor))
873 (if (and (Math-num-integerp numer)
874 (Math-num-integerp denom))
875 (list 'frac numer denom)
876 (list '/ numer denom))))
878 (defun math-latex-parse-two-args (f val)
879 (let (first second)
880 (setq first (car (math-read-expr-list)))
881 (math-read-token)
882 (setq second (math-read-factor))
883 (list (nth 2 f) first second)))
885 (defun math-compose-latex-frac (a fn)
886 (list 'horiz (nth 1 fn) "{" (math-compose-expr (nth 1 a) -1)
887 "}{"
888 (math-compose-expr (nth 2 a) -1)
889 "}"))
891 (put 'latex 'math-input-filter 'math-tex-input-filter)
893 (defun calc-eqn-language (n)
894 (interactive "P")
895 (calc-wrapper
896 (calc-set-language 'eqn)
897 (message "Eqn language mode")))
899 (put 'eqn 'math-oper-table
900 '( ( "prime" (math-parse-eqn-prime) 950 -1 )
901 ( "prime" calcFunc-Prime 950 -1 )
902 ( "dot" calcFunc-dot 950 -1 )
903 ( "dotdot" calcFunc-dotdot 950 -1 )
904 ( "hat" calcFunc-hat 950 -1 )
905 ( "tilde" calcFunc-tilde 950 -1 )
906 ( "vec" calcFunc-Vec 950 -1 )
907 ( "dyad" calcFunc-dyad 950 -1 )
908 ( "bar" calcFunc-bar 950 -1 )
909 ( "under" calcFunc-under 950 -1 )
910 ( "sub" calcFunc-subscr 931 930 )
911 ( "sup" ^ 921 920 )
912 ( "sqrt" calcFunc-sqrt -1 910 )
913 ( "over" / 900 901 )
914 ( "u|" calcFunc-abs -1 0 )
915 ( "|" closing 0 -1 )
916 ( "left floor" calcFunc-floor -1 0 )
917 ( "right floor" closing 0 -1 )
918 ( "left ceil" calcFunc-ceil -1 0 )
919 ( "right ceil" closing 0 -1 )
920 ( "+-" sdev 300 300 )
921 ( "!" calcFunc-fact 210 -1 )
922 ( "u+" ident -1 197 )
923 ( "u-" neg -1 197 )
924 ( "times" * 191 190 )
925 ( "*" * 191 190 )
926 ( "2x" * 191 190 )
927 ( "/" / 180 181 )
928 ( "%" % 180 181 )
929 ( "+" + 170 171 )
930 ( "-" - 170 171 )
931 ( "<" calcFunc-lt 160 161 )
932 ( ">" calcFunc-gt 160 161 )
933 ( "<=" calcFunc-leq 160 161 )
934 ( ">=" calcFunc-geq 160 161 )
935 ( "=" calcFunc-eq 160 161 )
936 ( "==" calcFunc-eq 160 161 )
937 ( "!=" calcFunc-neq 160 161 )
938 ( "u!" calcFunc-lnot -1 121 )
939 ( "&&" calcFunc-land 110 111 )
940 ( "||" calcFunc-lor 100 101 )
941 ( "?" (math-read-if) 91 90 )
942 ( "!!!" calcFunc-pnot -1 85 )
943 ( "&&&" calcFunc-pand 80 81 )
944 ( "|||" calcFunc-por 75 76 )
945 ( "<-" calcFunc-assign 51 50 )
946 ( ":=" calcFunc-assign 51 50 )
947 ( "::" calcFunc-condition 45 46 )
948 ( "->" calcFunc-evalto 40 41 )
949 ( "->" calcFunc-evalto 40 -1 )
950 ( "=>" calcFunc-evalto 40 41 )
951 ( "=>" calcFunc-evalto 40 -1 )))
953 (put 'eqn 'math-function-table
954 '( ( arc\ cos . calcFunc-arccos )
955 ( arc\ cosh . calcFunc-arccosh )
956 ( arc\ sin . calcFunc-arcsin )
957 ( arc\ sinh . calcFunc-arcsinh )
958 ( arc\ tan . calcFunc-arctan )
959 ( arc\ tanh . calcFunc-arctanh )
960 ( GAMMA . calcFunc-gamma )
961 ( phi . calcFunc-totient )
962 ( mu . calcFunc-moebius )
963 ( matrix . (math-parse-eqn-matrix) )))
965 (put 'eqn 'math-special-function-table
966 '((intv . math-compose-eqn-intv)))
968 (put 'eqn 'math-punc-table
969 '((?\{ . ?\()
970 (?\} . ?\))))
972 (put 'eqn 'math-variable-table
973 '( ( inf . var-uinf )))
975 (put 'eqn 'math-complex-format 'i)
977 (put 'eqn 'math-big-parens '("{left ( " . " right )}"))
979 (put 'eqn 'math-evalto '("evalto " . " -> "))
981 (put 'eqn 'math-matrix-formatter
982 (function
983 (lambda (a)
984 (append '(horiz "matrix { ")
985 (math-compose-eqn-matrix
986 (cdr (math-transpose a)))
987 '("}")))))
989 (put 'eqn 'math-var-formatter
990 (function
991 (lambda (a prec)
992 (let (v)
993 (if (and math-compose-hash-args
994 (let ((p calc-arg-values))
995 (setq v 1)
996 (while (and p (not (equal (car p) a)))
997 (setq p (and (eq math-compose-hash-args t) (cdr p))
998 v (1+ v)))
1000 (if (eq math-compose-hash-args 1)
1002 (format "#%d" v))
1003 (if (string-match ".'\\'" (symbol-name (nth 2 a)))
1004 (math-compose-expr
1005 (list 'calcFunc-Prime
1006 (list
1007 'var
1008 (intern (substring (symbol-name (nth 1 a)) 0 -1))
1009 (intern (substring (symbol-name (nth 2 a)) 0 -1))))
1010 prec)
1011 (symbol-name (nth 1 a))))))))
1013 (defconst math-eqn-special-funcs
1014 '( calcFunc-log
1015 calcFunc-ln calcFunc-exp
1016 calcFunc-sin calcFunc-cos calcFunc-tan
1017 calcFunc-sec calcFunc-csc calcFunc-cot
1018 calcFunc-sinh calcFunc-cosh calcFunc-tanh
1019 calcFunc-sech calcFunc-csch calcFunc-coth
1020 calcFunc-arcsin calcFunc-arccos calcFunc-arctan
1021 calcFunc-arcsinh calcFunc-arccosh calcFunc-arctanh))
1023 (put 'eqn 'math-func-formatter
1024 (function
1025 (lambda (func a)
1026 (let (left right)
1027 (if (string-match "[^']'+\\'" func)
1028 (let ((n (- (length func) (match-beginning 0) 1)))
1029 (setq func (substring func 0 (- n)))
1030 (while (>= (setq n (1- n)) 0)
1031 (setq func (concat func " prime")))))
1032 (cond ((or (> (length a) 2)
1033 (not (math-tex-expr-is-flat (nth 1 a))))
1034 (setq left "{left ( "
1035 right " right )}"))
1037 ((and
1038 (memq (car a) math-eqn-special-funcs)
1039 (= (length a) 2)
1040 (or (Math-realp (nth 1 a))
1041 (memq (car (nth 1 a)) '(var *))))
1042 (setq left "~{" right "}"))
1044 (setq left " ( "
1045 right " )")))
1046 (list 'horiz func left
1047 (math-compose-vector (cdr a) " , " 0)
1048 right)))))
1050 (put 'eqn 'math-lang-read-symbol
1051 '((?\"
1052 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)"
1053 math-exp-str math-exp-pos)
1054 (progn
1055 (setq math-exp-str (copy-sequence math-exp-str))
1056 (aset math-exp-str (match-beginning 1) ?\{)
1057 (if (< (match-end 1) (length math-exp-str))
1058 (aset math-exp-str (match-end 1) ?\}))
1059 (math-read-token)))))
1061 (defconst math-eqn-ignore-words
1062 '( ("roman") ("bold") ("italic") ("mark") ("lineup") ("evalto")
1063 ("left" ("floor") ("ceil"))
1064 ("right" ("floor") ("ceil"))
1065 ("arc" ("sin") ("cos") ("tan") ("sinh") ("cosh") ("tanh"))
1066 ("size" n) ("font" n) ("fwd" n) ("back" n) ("up" n) ("down" n)
1067 ("above" punc ",")))
1069 (put 'eqn 'math-lang-adjust-words
1070 (function
1071 (lambda ()
1072 (let ((code (assoc math-expr-data math-eqn-ignore-words)))
1073 (cond ((null code))
1074 ((null (cdr code))
1075 (math-read-token))
1076 ((consp (nth 1 code))
1077 (math-read-token)
1078 (if (assoc math-expr-data (cdr code))
1079 (setq math-expr-data (format "%s %s"
1080 (car code) math-expr-data))))
1081 ((eq (nth 1 code) 'punc)
1082 (setq math-exp-token 'punc
1083 math-expr-data (nth 2 code)))
1085 (math-read-token)
1086 (math-read-token)))))))
1088 (put 'eqn 'math-lang-read
1089 '((eq (string-match "->\\|<-\\|+-\\|\\\\dots\\|~\\|\\^"
1090 math-exp-str math-exp-pos)
1091 math-exp-pos)
1092 (progn
1093 (setq math-exp-token 'punc
1094 math-expr-data (math-match-substring math-exp-str 0)
1095 math-exp-pos (match-end 0))
1096 (and (eq (string-match "\\\\dots\\." math-exp-str math-exp-pos)
1097 math-exp-pos)
1098 (setq math-exp-pos (match-end 0)))
1099 (if (memq (aref math-expr-data 0) '(?~ ?^))
1100 (math-read-token)))))
1103 (defun math-compose-eqn-matrix (a)
1104 (if a
1105 (cons
1106 (cond ((eq calc-matrix-just 'right) "rcol ")
1107 ((eq calc-matrix-just 'center) "ccol ")
1108 (t "lcol "))
1109 (cons
1110 (list 'break math-compose-level)
1111 (cons
1112 "{ "
1113 (cons
1114 (let ((math-compose-level (1+ math-compose-level)))
1115 (math-compose-vector (cdr (car a)) " above " 1000))
1116 (cons
1117 " } "
1118 (math-compose-eqn-matrix (cdr a)))))))
1119 nil))
1121 (defun math-parse-eqn-matrix (f sym)
1122 (let ((vec nil))
1123 (while (assoc math-expr-data '(("ccol") ("lcol") ("rcol")))
1124 (math-read-token)
1125 (or (equal math-expr-data calc-function-open)
1126 (throw 'syntax "Expected `{'"))
1127 (math-read-token)
1128 (setq vec (cons (cons 'vec (math-read-expr-list)) vec))
1129 (or (equal math-expr-data calc-function-close)
1130 (throw 'syntax "Expected `}'"))
1131 (math-read-token))
1132 (or (equal math-expr-data calc-function-close)
1133 (throw 'syntax "Expected `}'"))
1134 (math-read-token)
1135 (math-transpose (cons 'vec (nreverse vec)))))
1137 (defun math-parse-eqn-prime (x sym)
1138 (if (eq (car-safe x) 'var)
1139 (if (equal math-expr-data calc-function-open)
1140 (progn
1141 (math-read-token)
1142 (let ((args (if (or (equal math-expr-data calc-function-close)
1143 (eq math-exp-token 'end))
1145 (math-read-expr-list))))
1146 (if (not (or (equal math-expr-data calc-function-close)
1147 (eq math-exp-token 'end)))
1148 (throw 'syntax "Expected `)'"))
1149 (math-read-token)
1150 (cons (intern (format "calcFunc-%s'" (nth 1 x))) args)))
1151 (list 'var
1152 (intern (concat (symbol-name (nth 1 x)) "'"))
1153 (intern (concat (symbol-name (nth 2 x)) "'"))))
1154 (list 'calcFunc-Prime x)))
1156 (defun math-compose-eqn-intv (a)
1157 (list 'horiz
1158 (if (memq (nth 1 a) '(0 1)) "(" "[")
1159 (math-compose-expr (nth 2 a) 0)
1160 " ... "
1161 (math-compose-expr (nth 3 a) 0)
1162 (if (memq (nth 1 a) '(0 2)) ")" "]")))
1165 ;;; Yacas
1167 (defun calc-yacas-language ()
1168 "Change the Calc language to be Yacas-like."
1169 (interactive)
1170 (calc-wrapper
1171 (calc-set-language 'yacas)
1172 (message "`Yacas' language mode")))
1174 (put 'yacas 'math-vector-brackets "{}")
1176 (put 'yacas 'math-complex-format 'I)
1178 (add-to-list 'calc-lang-brackets-are-subscripts 'yacas)
1180 (put 'yacas 'math-variable-table
1181 '(( Infinity . var-inf)
1182 ( Infinity . var-uinf)
1183 ( Undefined . var-nan)
1184 ( Pi . var-pi)
1185 ( E . var-e) ;; Not really in Yacas
1186 ( GoldenRatio . var-phi)
1187 ( Gamma . var-gamma)))
1189 (put 'yacas 'math-parse-table
1190 '((("Deriv(" 0 ")" 0)
1191 calcFunc-deriv (var ArgB var-ArgB) (var ArgA var-ArgA))
1192 (("D(" 0 ")" 0)
1193 calcFunc-deriv (var ArgB var-ArgB) (var ArgA var-ArgA))
1194 (("Integrate(" 0 ")" 0)
1195 calcFunc-integ (var ArgB var-ArgB)(var ArgA var-ArgA))
1196 (("Integrate(" 0 "," 0 "," 0 ")" 0)
1197 calcFunc-integ (var ArgD var-ArgD) (var ArgA var-ArgA)
1198 (var ArgB var-ArgB) (var ArgC var-ArgC))
1199 (("Subst(" 0 "," 0 ")" 0)
1200 calcFunc-subst (var ArgC var-ArgC) (var ArgA var-ArgA)
1201 (var ArgB var-ArgB))
1202 (("Taylor(" 0 "," 0 "," 0 ")" 0)
1203 calcFunc-taylor (var ArgD var-ArgD)
1204 (calcFunc-eq (var ArgA var-ArgA) (var ArgB var-ArgB))
1205 (var ArgC var-ArgC))))
1207 (put 'yacas 'math-oper-table
1208 '(("+" + 30 30)
1209 ("-" - 30 60)
1210 ("*" * 60 60)
1211 ("/" / 70 70)
1212 ("u-" neg -1 60)
1213 ("^" ^ 80 80)
1214 ("u+" ident -1 30)
1215 ("<<" calcFunc-lsh 80 80)
1216 (">>" calcFunc-rsh 80 80)
1217 ("!" calcFunc-fact 80 -1)
1218 ("!!" calcFunc-dfact 80 -1)
1219 ("X" calcFunc-cross 70 70)
1220 ("=" calcFunc-eq 10 10)
1221 ("!=" calcFunc-neq 10 10)
1222 ("<" calcFunc-lt 10 10)
1223 (">" calcFunc-gt 10 10)
1224 ("<=" calcFunc-leq 10 10)
1225 (">=" calcFunc-geq 10 10)
1226 ("And" calcFunc-land 5 5)
1227 ("Or" calcFunc-or 4 4)
1228 ("Not" calcFunc-lnot -1 3)
1229 (":=" calcFunc-assign 1 1)))
1231 (put 'yacas 'math-function-table
1232 '(( Div . calcFunc-idiv)
1233 ( Mod . calcFunc-mod)
1234 ( Abs . calcFunc-abs)
1235 ( Sign . calcFunc-sign)
1236 ( Sqrt . calcFunc-sqrt)
1237 ( Max . calcFunc-max)
1238 ( Min . calcFunc-min)
1239 ( Floor . calcFunc-floor)
1240 ( Ceil . calcFunc-ceil)
1241 ( Round . calcFunc-round)
1242 ( Conjugate . calcFunc-conj)
1243 ( Arg . calcFunc-arg)
1244 ( Re . calcFunc-re)
1245 ( Im . calcFunc-im)
1246 ( Rationalize . calcFunc-pfrac)
1247 ( Sin . calcFunc-sin)
1248 ( Cos . calcFunc-cos)
1249 ( Tan . calcFunc-tan)
1250 ( Sec . calcFunc-sec)
1251 ( Csc . calcFunc-csc)
1252 ( Cot . calcFunc-cot)
1253 ( ArcSin . calcFunc-arcsin)
1254 ( ArcCos . calcFunc-arccos)
1255 ( ArcTan . calcFunc-arctan)
1256 ( Sinh . calcFunc-sinh)
1257 ( Cosh . calcFunc-cosh)
1258 ( Tanh . calcFunc-tanh)
1259 ( Sech . calcFunc-sech)
1260 ( Csch . calcFunc-csch)
1261 ( Coth . calcFunc-coth)
1262 ( ArcSinh . calcFunc-arcsinh)
1263 ( ArcCosh . calcFunc-arccosh)
1264 ( ArcTanh . calcFunc-arctanh)
1265 ( Ln . calcFunc-ln)
1266 ( Exp . calcFunc-exp)
1267 ( Gamma . calcFunc-gamma)
1268 ( Gcd . calcFunc-gcd)
1269 ( Lcm . calcFunc-lcm)
1270 ( Bin . calcFunc-choose)
1271 ( Bernoulli . calcFunc-bern)
1272 ( Euler . calcFunc-euler)
1273 ( StirlingNumber1 . calcFunc-stir1)
1274 ( StirlingNumber2 . calcFunc-stir2)
1275 ( IsPrime . calcFunc-prime)
1276 ( Factors . calcFunc-prfac)
1277 ( NextPrime . calcFunc-nextprime)
1278 ( Moebius . calcFunc-moebius)
1279 ( Random . calcFunc-random)
1280 ( Concat . calcFunc-vconcat)
1281 ( Head . calcFunc-head)
1282 ( Tail . calcFunc-tail)
1283 ( Length . calcFunc-vlen)
1284 ( Reverse . calcFunc-rev)
1285 ( CrossProduct . calcFunc-cross)
1286 ( Dot . calcFunc-mul)
1287 ( DiagonalMatrix . calcFunc-diag)
1288 ( Transpose . calcFunc-trn)
1289 ( Inverse . calcFunc-inv)
1290 ( Determinant . calcFunc-det)
1291 ( Trace . calcFunc-tr)
1292 ( RemoveDuplicates . calcFunc-rdup)
1293 ( Union . calcFunc-vunion)
1294 ( Intersection . calcFunc-vint)
1295 ( Difference . calcFunc-vdiff)
1296 ( Apply . calcFunc-apply)
1297 ( Map . calcFunc-map)
1298 ( Simplify . calcFunc-simplify)
1299 ( ExpandBrackets . calcFunc-expand)
1300 ( Solve . calcFunc-solve)
1301 ( Degree . calcFunc-pdeg)
1302 ( If . calcFunc-if)
1303 ( Contains . (math-lang-switch-args calcFunc-in))
1304 ( Sum . (math-yacas-parse-Sum calcFunc-sum))
1305 ( Factorize . (math-yacas-parse-Sum calcFunc-prod))))
1307 (put 'yacas 'math-special-function-table
1308 '(( calcFunc-sum . (math-yacas-compose-sum "Sum"))
1309 ( calcFunc-prod . (math-yacas-compose-sum "Factorize"))
1310 ( calcFunc-deriv . (math-yacas-compose-deriv "Deriv"))
1311 ( calcFunc-integ . (math-yacas-compose-deriv "Integrate"))
1312 ( calcFunc-taylor . math-yacas-compose-taylor)
1313 ( calcFunc-in . (math-lang-compose-switch-args "Contains"))))
1315 (put 'yacas 'math-compose-subscr
1316 (function
1317 (lambda (a)
1318 (let ((args (cdr (cdr a))))
1319 (list 'horiz
1320 (math-compose-expr (nth 1 a) 1000)
1322 (math-compose-vector args ", " 0)
1323 "]")))))
1325 (defun math-yacas-parse-Sum (f val)
1326 "Read in the arguments to \"Sum\" in Calc's Yacas mode."
1327 (let ((args (math-read-expr-list)))
1328 (math-read-token)
1329 (list (nth 2 f)
1330 (nth 3 args)
1331 (nth 0 args)
1332 (nth 1 args)
1333 (nth 2 args))))
1335 (defun math-yacas-compose-sum (a fn)
1336 "Compose the \"Sum\" function in Calc's Yacas mode."
1337 (list 'horiz
1338 (nth 1 fn)
1340 (math-compose-expr (nth 2 a) -1)
1342 (math-compose-expr (nth 3 a) -1)
1344 (math-compose-expr (nth 4 a) -1)
1346 (math-compose-expr (nth 1 a) -1)
1347 ")"))
1349 (defun math-yacas-compose-deriv (a fn)
1350 "Compose the \"Deriv\" function in Calc's Yacas mode."
1351 (list 'horiz
1352 (nth 1 fn)
1354 (math-compose-expr (nth 2 a) -1)
1355 (if (not (nth 3 a))
1357 (concat
1359 (math-compose-expr (nth 3 a) -1)
1361 (math-compose-expr (nth 4 a) -1)
1362 ")"))
1364 (math-compose-expr (nth 1 a) -1)))
1366 (defun math-yacas-compose-taylor (a)
1367 "Compose the \"Taylor\" function in Calc's Yacas mode."
1368 (list 'horiz
1369 "Taylor("
1370 (if (eq (car-safe (nth 2 a)) 'calcFunc-eq)
1371 (concat (math-compose-expr (nth 1 (nth 2 a)) -1)
1373 (math-compose-expr (nth 2 (nth 2 a)) -1))
1374 (concat (math-compose-expr (nth 2 a) -1) ",0"))
1376 (math-compose-expr (nth 3 a) -1)
1377 ") "
1378 (math-compose-expr (nth 1 a) -1)))
1381 ;;; Maxima
1383 (defun calc-maxima-language ()
1384 "Change the Calc language to be Maxima-like."
1385 (interactive)
1386 (calc-wrapper
1387 (calc-set-language 'maxima)
1388 (message "`Maxima' language mode")))
1390 (put 'maxima 'math-oper-table
1391 '(("+" + 100 100)
1392 ("-" - 100 134)
1393 ("*" * 120 120)
1394 ("." * 130 129)
1395 ("/" / 120 120)
1396 ("u-" neg -1 180)
1397 ("u+" ident -1 180)
1398 ("^" ^ 140 139)
1399 ("**" ^ 140 139)
1400 ("!" calcFunc-fact 160 -1)
1401 ("!!" calcFunc-dfact 160 -1)
1402 ("=" calcFunc-eq 80 80)
1403 ("#" calcFunc-neq 80 80)
1404 ("<" calcFunc-lt 80 80)
1405 (">" calcFunc-gt 80 80)
1406 ("<=" calcFunc-leq 80 80)
1407 (">=" calcFunc-geq 80 80)
1408 ("and" calcFunc-land 65 65)
1409 ("or" calcFunc-or 60 60)
1410 ("not" calcFunc-lnot -1 70)
1411 (":" calcFunc-assign 180 20)))
1414 (put 'maxima 'math-function-table
1415 '(( matrix . vec)
1416 ( abs . calcFunc-abs)
1417 ( cabs . calcFunc-abs)
1418 ( signum . calcFunc-sign)
1419 ( floor . calcFunc-floor)
1420 ( entier . calcFunc-floor)
1421 ( fix . calcFunc-floor)
1422 ( conjugate . calcFunc-conj )
1423 ( carg . calcFunc-arg)
1424 ( realpart . calcFunc-re)
1425 ( imagpart . calcFunc-im)
1426 ( rationalize . calcFunc-pfrac)
1427 ( asin . calcFunc-arcsin)
1428 ( acos . calcFunc-arccos)
1429 ( atan . calcFunc-arctan)
1430 ( atan2 . calcFunc-arctan2)
1431 ( asinh . calcFunc-arcsinh)
1432 ( acosh . calcFunc-arccosh)
1433 ( atanh . calcFunc-arctanh)
1434 ( log . calcFunc-ln)
1435 ( plog . calcFunc-ln)
1436 ( bessel_j . calcFunc-besJ)
1437 ( bessel_y . calcFunc-besY)
1438 ( factorial . calcFunc-fact)
1439 ( binomial . calcFunc-choose)
1440 ( primep . calcFunc-prime)
1441 ( next_prime . calcFunc-nextprime)
1442 ( prev_prime . calcFunc-prevprime)
1443 ( append . calcFunc-vconcat)
1444 ( rest . calcFunc-tail)
1445 ( reverse . calcFunc-rev)
1446 ( innerproduct . calcFunc-mul)
1447 ( inprod . calcFunc-mul)
1448 ( row . calcFunc-mrow)
1449 ( columnvector . calcFunc-mcol)
1450 ( covect . calcFunc-mcol)
1451 ( transpose . calcFunc-trn)
1452 ( invert . calcFunc-inv)
1453 ( determinant . calcFunc-det)
1454 ( mattrace . calcFunc-tr)
1455 ( member . calcFunc-in)
1456 ( lmax . calcFunc-vmax)
1457 ( lmin . calcFunc-vmin)
1458 ( distrib . calcFunc-expand)
1459 ( partfrac . calcFunc-apart)
1460 ( rat . calcFunc-nrat)
1461 ( product . calcFunc-prod)
1462 ( diff . calcFunc-deriv)
1463 ( integrate . calcFunc-integ)
1464 ( quotient . calcFunc-pdiv)
1465 ( remainder . calcFunc-prem)
1466 ( divide . calcFunc-pdivrem)
1467 ( equal . calcFunc-eq)
1468 ( notequal . calcFunc-neq)
1469 ( rhs . calcFunc-rmeq)
1470 ( subst . (math-maxima-parse-subst))
1471 ( substitute . (math-maxima-parse-subst))
1472 ( taylor . (math-maxima-parse-taylor))))
1474 (defun math-maxima-parse-subst (f val)
1475 "Read in the arguments to \"subst\" in Calc's Maxima mode."
1476 (let ((args (math-read-expr-list)))
1477 (math-read-token)
1478 (list 'calcFunc-subst
1479 (nth 1 args)
1480 (nth 2 args)
1481 (nth 0 args))))
1483 (defun math-maxima-parse-taylor (f val)
1484 "Read in the arguments to \"taylor\" in Calc's Maxima mode."
1485 (let ((args (math-read-expr-list)))
1486 (math-read-token)
1487 (list 'calcFunc-taylor
1488 (nth 0 args)
1489 (list 'calcFunc-eq
1490 (nth 1 args)
1491 (nth 2 args))
1492 (nth 3 args))))
1494 (put 'maxima 'math-parse-table
1495 '((("if" 0 "then" 0 "else" 0)
1496 calcFunc-if
1497 (var ArgA var-ArgA)
1498 (var ArgB var-ArgB)
1499 (var ArgC var-ArgC))))
1501 (put 'maxima 'math-special-function-table
1502 '(( calcFunc-taylor . math-maxima-compose-taylor)
1503 ( calcFunc-subst . math-maxima-compose-subst)
1504 ( calcFunc-if . math-maxima-compose-if)))
1506 (defun math-maxima-compose-taylor (a)
1507 "Compose the \"taylor\" function in Calc's Maxima mode."
1508 (list 'horiz
1509 "taylor("
1510 (math-compose-expr (nth 1 a) -1)
1512 (if (eq (car-safe (nth 2 a)) 'calcFunc-eq)
1513 (concat (math-compose-expr (nth 1 (nth 2 a)) -1)
1515 (math-compose-expr (nth 2 (nth 2 a)) -1))
1516 (concat (math-compose-expr (nth 2 a) -1) ",0"))
1518 (math-compose-expr (nth 3 a) -1)
1519 ")"))
1521 (defun math-maxima-compose-subst (a)
1522 "Compose the \"subst\" function in Calc's Maxima mode."
1523 (list 'horiz
1524 "substitute("
1525 (math-compose-expr (nth 2 a) -1)
1527 (math-compose-expr (nth 3 a) -1)
1529 (math-compose-expr (nth 1 a) -1)
1530 ")"))
1532 (defun math-maxima-compose-if (a)
1533 "Compose the \"if\" function in Calc's Maxima mode."
1534 (list 'horiz
1535 "if "
1536 (math-compose-expr (nth 1 a) -1)
1537 " then "
1538 (math-compose-expr (nth 2 a) -1)
1539 " else "
1540 (math-compose-expr (nth 3 a) -1)))
1542 (put 'maxima 'math-variable-table
1543 '(( infinity . var-uinf)
1544 ( %pi . var-pi)
1545 ( %e . var-e)
1546 ( %i . var-i)
1547 ( %phi . var-phi)
1548 ( %gamma . var-gamma)))
1550 (put 'maxima 'math-complex-format '%i)
1552 (add-to-list 'calc-lang-allow-underscores 'maxima)
1554 (add-to-list 'calc-lang-allow-percentsigns 'maxima)
1556 (add-to-list 'calc-lang-brackets-are-subscripts 'maxima)
1558 (put 'maxima 'math-compose-subscr
1559 (function
1560 (lambda (a)
1561 (let ((args (cdr (cdr a))))
1562 (list 'horiz
1563 (math-compose-expr (nth 1 a) 1000)
1565 (math-compose-vector args ", " 0)
1566 "]")))))
1568 (put 'maxima 'math-matrix-formatter
1569 (function
1570 (lambda (a)
1571 (list 'horiz
1572 "matrix("
1573 (math-compose-vector (cdr a)
1574 (concat math-comp-comma " ")
1575 math-comp-vector-prec)
1576 ")"))))
1579 ;;; Giac
1581 (defun calc-giac-language ()
1582 "Change the Calc language to be Giac-like."
1583 (interactive)
1584 (calc-wrapper
1585 (calc-set-language 'giac)
1586 (message "`Giac' language mode")))
1588 (put 'giac 'math-oper-table
1589 '( ( "[" (math-read-giac-subscr) 250 -1 )
1590 ( "+" + 180 181 )
1591 ( "-" - 180 181 )
1592 ( "/" / 191 192 )
1593 ( "*" * 191 192 )
1594 ( "^" ^ 201 200 )
1595 ( "u+" ident -1 197 )
1596 ( "u-" neg -1 197 )
1597 ( "!" calcFunc-fact 210 -1 )
1598 ( ".." (math-read-maple-dots) 165 165 )
1599 ( "\\dots" (math-read-maple-dots) 165 165 )
1600 ( "intersect" calcFunc-vint 191 192 )
1601 ( "union" calcFunc-vunion 180 181 )
1602 ( "minus" calcFunc-vdiff 180 181 )
1603 ( "<" calcFunc-lt 160 160 )
1604 ( ">" calcFunc-gt 160 160 )
1605 ( "<=" calcFunc-leq 160 160 )
1606 ( ">=" calcFunc-geq 160 160 )
1607 ( "=" calcFunc-eq 160 160 )
1608 ( "==" calcFunc-eq 160 160 )
1609 ( "!=" calcFunc-neq 160 160 )
1610 ( "and" calcFunc-land 110 111 )
1611 ( "or" calcFunc-lor 100 101 )
1612 ( "&&" calcFunc-land 110 111 )
1613 ( "||" calcFunc-lor 100 101 )
1614 ( "not" calcFunc-lnot -1 121 )
1615 ( ":=" calcFunc-assign 51 50 )))
1618 (put 'giac 'math-function-table
1619 '(( rdiv . calcFunc-div)
1620 ( iquo . calcFunc-idiv)
1621 ( irem . calcFunc-mod)
1622 ( remain . calcFunc-mod)
1623 ( floor . calcFunc-floor)
1624 ( iPart . calcFunc-floor)
1625 ( ceil . calcFunc-ceil)
1626 ( ceiling . calcFunc-ceil)
1627 ( re . calcFunc-re)
1628 ( real . calcFunc-re)
1629 ( im . calcFunc-im)
1630 ( imag . calcFunc-im)
1631 ( float2rational . calcFunc-pfrac)
1632 ( exact . calcFunc-pfrac)
1633 ( evalf . calcFunc-pfloat)
1634 ( bitand . calcFunc-and)
1635 ( bitor . calcFunc-or)
1636 ( bitxor . calcFunc-xor)
1637 ( asin . calcFunc-arcsin)
1638 ( acos . calcFunc-arccos)
1639 ( atan . calcFunc-arctan)
1640 ( asinh . calcFunc-arcsinh)
1641 ( acosh . calcFunc-arccosh)
1642 ( atanh . calcFunc-arctanh)
1643 ( log . calcFunc-ln)
1644 ( logb . calcFunc-log)
1645 ( factorial . calcFunc-fact)
1646 ( comb . calcFunc-choose)
1647 ( binomial . calcFunc-choose)
1648 ( nCr . calcFunc-choose)
1649 ( perm . calcFunc-perm)
1650 ( nPr . calcFunc-perm)
1651 ( bernoulli . calcFunc-bern)
1652 ( is_prime . calcFunc-prime)
1653 ( isprime . calcFunc-prime)
1654 ( isPrime . calcFunc-prime)
1655 ( ifactors . calcFunc-prfac)
1656 ( euler . calcFunc-totient)
1657 ( phi . calcFunc-totient)
1658 ( rand . calcFunc-random)
1659 ( concat . calcFunc-vconcat)
1660 ( augment . calcFunc-vconcat)
1661 ( mid . calcFunc-subvec)
1662 ( length . calcFunc-length)
1663 ( size . calcFunc-length)
1664 ( nops . calcFunc-length)
1665 ( SortA . calcFunc-sort)
1666 ( SortB . calcFunc-rsort)
1667 ( revlist . calcFunc-rev)
1668 ( cross . calcFunc-cross)
1669 ( crossP . calcFunc-cross)
1670 ( crossproduct . calcFunc-cross)
1671 ( mul . calcFunc-mul)
1672 ( dot . calcFunc-mul)
1673 ( dotprod . calcFunc-mul)
1674 ( dotP . calcFunc-mul)
1675 ( scalar_product . calcFunc-mul)
1676 ( scalar_Product . calcFunc-mul)
1677 ( row . calcFunc-mrow)
1678 ( col . calcFunc-mcol)
1679 ( dim . calcFunc-mdims)
1680 ( tran . calcFunc-trn)
1681 ( transpose . calcFunc-trn)
1682 ( lu . calcFunc-lud)
1683 ( trace . calcFunc-tr)
1684 ( member . calcFunc-in)
1685 ( sum . calcFunc-vsum)
1686 ( add . calcFunc-vsum)
1687 ( product . calcFunc-vprod)
1688 ( mean . calcFunc-vmean)
1689 ( median . calcFunc-vmedian)
1690 ( stddev . calcFunc-vsdev)
1691 ( stddevp . calcFunc-vpsdev)
1692 ( variance . calcFunc-vpvar)
1693 ( map . calcFunc-map)
1694 ( apply . calcFunc-map)
1695 ( of . calcFunc-map)
1696 ( zip . calcFunc-map)
1697 ( expand . calcFunc-expand)
1698 ( fdistrib . calcFunc-expand)
1699 ( partfrac . calcFunc-apart)
1700 ( ratnormal . calcFunc-nrat)
1701 ( diff . calcFunc-deriv)
1702 ( derive . calcFunc-deriv)
1703 ( integrate . calcFunc-integ)
1704 ( int . calcFunc-integ)
1705 ( Int . calcFunc-integ)
1706 ( romberg . calcFunc-ninteg)
1707 ( nInt . calcFunc-ninteg)
1708 ( lcoeff . calcFunc-plead)
1709 ( content . calcFunc-pcont)
1710 ( primpart . calcFunc-pprim)
1711 ( quo . calcFunc-pdiv)
1712 ( rem . calcFunc-prem)
1713 ( quorem . calcFunc-pdivrem)
1714 ( divide . calcFunc-pdivrem)
1715 ( equal . calcFunc-eq)
1716 ( ifte . calcFunc-if)
1717 ( not . calcFunc-lnot)
1718 ( rhs . calcFunc-rmeq)
1719 ( right . calcFunc-rmeq)
1720 ( prepend . (math-lang-switch-args calcFunc-cons))
1721 ( contains . (math-lang-switch-args calcFunc-in))
1722 ( has . (math-lang-switch-args calcFunc-refers))))
1724 (defun math-lang-switch-args (f val)
1725 "Read the arguments to a Calc function in reverse order.
1726 This is used for various language modes which have functions in reverse
1727 order to Calc's."
1728 (let ((args (math-read-expr-list)))
1729 (math-read-token)
1730 (list (nth 2 f)
1731 (nth 1 args)
1732 (nth 0 args))))
1734 (put 'giac 'math-parse-table
1735 '((("set" 0)
1736 calcFunc-rdup
1737 (var ArgA var-ArgA))))
1739 (put 'giac 'math-special-function-table
1740 '((calcFunc-cons . (math-lang-compose-switch-args "prepend"))
1741 (calcFunc-in . (math-lang-compose-switch-args "contains"))
1742 (calcFunc-refers . (math-lang-compose-switch-args "has"))
1743 (intv . math-compose-maple-intv)))
1745 (defun math-lang-compose-switch-args (a fn)
1746 "Compose the arguments to a Calc function in reverse order.
1747 This is used for various language modes which have functions in reverse
1748 order to Calc's."
1749 (list 'horiz (nth 1 fn)
1751 (math-compose-expr (nth 2 a) 0)
1753 (math-compose-expr (nth 1 a) 0)
1754 ")"))
1756 (put 'giac 'math-variable-table
1757 '(( infinity . var-inf)
1758 ( infinity . var-uinf)))
1760 (put 'giac 'math-complex-format 'i)
1762 (add-to-list 'calc-lang-allow-underscores 'giac)
1764 (put 'giac 'math-compose-subscr
1765 (function
1766 (lambda (a)
1767 (let ((args (cdr (cdr a))))
1768 (list 'horiz
1769 (math-compose-expr (nth 1 a) 1000)
1771 (math-compose-expr
1772 (calc-normalize (list '- (nth 2 a) 1)) 0)
1773 "]")))))
1775 (defun math-read-giac-subscr (x op)
1776 (let ((idx (math-read-expr-level 0)))
1777 (or (equal math-expr-data "]")
1778 (throw 'syntax "Expected ']'"))
1779 (math-read-token)
1780 (list 'calcFunc-subscr x (calc-normalize (list '+ idx 1)))))
1782 (add-to-list 'calc-lang-c-type-hex 'giac)
1785 (defun calc-mathematica-language ()
1786 (interactive)
1787 (calc-wrapper
1788 (calc-set-language 'math)
1789 (message "Mathematica language mode")))
1791 (put 'math 'math-oper-table
1792 '( ( "[[" (math-read-math-subscr) 250 -1 )
1793 ( "!" calcFunc-fact 210 -1 )
1794 ( "!!" calcFunc-dfact 210 -1 )
1795 ( "^" ^ 201 200 )
1796 ( "u+" ident -1 197 )
1797 ( "u-" neg -1 197 )
1798 ( "/" / 195 196 )
1799 ( "*" * 190 191 )
1800 ( "2x" * 190 191 )
1801 ( "+" + 180 181 )
1802 ( "-" - 180 181 )
1803 ( "<" calcFunc-lt 160 161 )
1804 ( ">" calcFunc-gt 160 161 )
1805 ( "<=" calcFunc-leq 160 161 )
1806 ( ">=" calcFunc-geq 160 161 )
1807 ( "==" calcFunc-eq 150 151 )
1808 ( "!=" calcFunc-neq 150 151 )
1809 ( "u!" calcFunc-lnot -1 121 )
1810 ( "&&" calcFunc-land 110 111 )
1811 ( "||" calcFunc-lor 100 101 )
1812 ( "!!!" calcFunc-pnot -1 85 )
1813 ( "&&&" calcFunc-pand 80 81 )
1814 ( "|||" calcFunc-por 75 76 )
1815 ( ":=" calcFunc-assign 51 50 )
1816 ( "=" calcFunc-assign 51 50 )
1817 ( "->" calcFunc-assign 51 50 )
1818 ( ":>" calcFunc-assign 51 50 )
1819 ( "::" calcFunc-condition 45 46 )
1822 (put 'math 'math-function-table
1823 '( ( Abs . calcFunc-abs )
1824 ( ArcCos . calcFunc-arccos )
1825 ( ArcCosh . calcFunc-arccosh )
1826 ( ArcSin . calcFunc-arcsin )
1827 ( ArcSinh . calcFunc-arcsinh )
1828 ( ArcTan . calcFunc-arctan )
1829 ( ArcTanh . calcFunc-arctanh )
1830 ( Arg . calcFunc-arg )
1831 ( Binomial . calcFunc-choose )
1832 ( Ceiling . calcFunc-ceil )
1833 ( Conjugate . calcFunc-conj )
1834 ( Cos . calcFunc-cos )
1835 ( Cosh . calcFunc-cosh )
1836 ( Cot . calcFunc-cot )
1837 ( Coth . calcFunc-coth )
1838 ( Csc . calcFunc-csc )
1839 ( Csch . calcFunc-csch )
1840 ( D . calcFunc-deriv )
1841 ( Dt . calcFunc-tderiv )
1842 ( Det . calcFunc-det )
1843 ( Exp . calcFunc-exp )
1844 ( EulerPhi . calcFunc-totient )
1845 ( Floor . calcFunc-floor )
1846 ( Gamma . calcFunc-gamma )
1847 ( GCD . calcFunc-gcd )
1848 ( If . calcFunc-if )
1849 ( Im . calcFunc-im )
1850 ( Inverse . calcFunc-inv )
1851 ( Integrate . calcFunc-integ )
1852 ( Join . calcFunc-vconcat )
1853 ( LCM . calcFunc-lcm )
1854 ( Log . calcFunc-ln )
1855 ( Max . calcFunc-max )
1856 ( Min . calcFunc-min )
1857 ( Mod . calcFunc-mod )
1858 ( MoebiusMu . calcFunc-moebius )
1859 ( Random . calcFunc-random )
1860 ( Round . calcFunc-round )
1861 ( Re . calcFunc-re )
1862 ( Sec . calcFunc-sec )
1863 ( Sech . calcFunc-sech )
1864 ( Sign . calcFunc-sign )
1865 ( Sin . calcFunc-sin )
1866 ( Sinh . calcFunc-sinh )
1867 ( Sqrt . calcFunc-sqrt )
1868 ( Tan . calcFunc-tan )
1869 ( Tanh . calcFunc-tanh )
1870 ( Transpose . calcFunc-trn )
1871 ( Length . calcFunc-vlen )
1874 (put 'math 'math-variable-table
1875 '( ( I . var-i )
1876 ( Pi . var-pi )
1877 ( E . var-e )
1878 ( GoldenRatio . var-phi )
1879 ( EulerGamma . var-gamma )
1880 ( Infinity . var-inf )
1881 ( ComplexInfinity . var-uinf )
1882 ( Indeterminate . var-nan )
1885 (put 'math 'math-vector-brackets "{}")
1886 (put 'math 'math-complex-format 'I)
1887 (put 'math 'math-function-open "[")
1888 (put 'math 'math-function-close "]")
1890 (put 'math 'math-radix-formatter
1891 (function (lambda (r s) (format "%d^^%s" r s))))
1893 (put 'math 'math-lang-read
1894 '((eq (string-match "\\[\\[\\|->\\|:>" math-exp-str math-exp-pos)
1895 math-exp-pos)
1896 (setq math-exp-token 'punc
1897 math-expr-data (math-match-substring math-exp-str 0)
1898 math-exp-pos (match-end 0))))
1900 (put 'math 'math-compose-subscr
1901 (function
1902 (lambda (a)
1903 (list 'horiz
1904 (math-compose-expr (nth 1 a) 1000)
1905 "[["
1906 (math-compose-expr (nth 2 a) 0)
1907 "]]"))))
1909 (defun math-read-math-subscr (x op)
1910 (let ((idx (math-read-expr-level 0)))
1911 (or (and (equal math-expr-data "]")
1912 (progn
1913 (math-read-token)
1914 (equal math-expr-data "]")))
1915 (throw 'syntax "Expected ']]'"))
1916 (math-read-token)
1917 (list 'calcFunc-subscr x idx)))
1920 (defun calc-maple-language ()
1921 (interactive)
1922 (calc-wrapper
1923 (calc-set-language 'maple)
1924 (message "Maple language mode")))
1926 (put 'maple 'math-oper-table
1927 '( ( "matrix" ident -1 300 )
1928 ( "MATRIX" ident -1 300 )
1929 ( "!" calcFunc-fact 210 -1 )
1930 ( "^" ^ 201 200 )
1931 ( "**" ^ 201 200 )
1932 ( "u+" ident -1 197 )
1933 ( "u-" neg -1 197 )
1934 ( "/" / 191 192 )
1935 ( "*" * 191 192 )
1936 ( "intersect" calcFunc-vint 191 192 )
1937 ( "+" + 180 181 )
1938 ( "-" - 180 181 )
1939 ( "union" calcFunc-vunion 180 181 )
1940 ( "minus" calcFunc-vdiff 180 181 )
1941 ( "mod" % 170 170 )
1942 ( ".." (math-read-maple-dots) 165 165 )
1943 ( "\\dots" (math-read-maple-dots) 165 165 )
1944 ( "<" calcFunc-lt 160 160 )
1945 ( ">" calcFunc-gt 160 160 )
1946 ( "<=" calcFunc-leq 160 160 )
1947 ( ">=" calcFunc-geq 160 160 )
1948 ( "=" calcFunc-eq 160 160 )
1949 ( "<>" calcFunc-neq 160 160 )
1950 ( "not" calcFunc-lnot -1 121 )
1951 ( "and" calcFunc-land 110 111 )
1952 ( "or" calcFunc-lor 100 101 )
1953 ( "!!!" calcFunc-pnot -1 85 )
1954 ( "&&&" calcFunc-pand 80 81 )
1955 ( "|||" calcFunc-por 75 76 )
1956 ( ":=" calcFunc-assign 51 50 )
1957 ( "::" calcFunc-condition 45 46 )
1960 (put 'maple 'math-function-table
1961 '( ( bernoulli . calcFunc-bern )
1962 ( binomial . calcFunc-choose )
1963 ( diff . calcFunc-deriv )
1964 ( GAMMA . calcFunc-gamma )
1965 ( ifactor . calcFunc-prfac )
1966 ( igcd . calcFunc-gcd )
1967 ( ilcm . calcFunc-lcm )
1968 ( int . calcFunc-integ )
1969 ( modp . % )
1970 ( irem . % )
1971 ( iquo . calcFunc-idiv )
1972 ( isprime . calcFunc-prime )
1973 ( length . calcFunc-vlen )
1974 ( member . calcFunc-in )
1975 ( crossprod . calcFunc-cross )
1976 ( inverse . calcFunc-inv )
1977 ( trace . calcFunc-tr )
1978 ( transpose . calcFunc-trn )
1979 ( vectdim . calcFunc-vlen )
1982 (put 'maple 'math-special-function-table
1983 '((intv . math-compose-maple-intv)))
1985 (put 'maple 'math-variable-table
1986 '( ( I . var-i )
1987 ( Pi . var-pi )
1988 ( E . var-e )
1989 ( infinity . var-inf )
1990 ( infinity . var-uinf )
1991 ( infinity . var-nan )
1994 (put 'maple 'math-complex-format 'I)
1996 (put 'maple 'math-matrix-formatter
1997 (function
1998 (lambda (a)
1999 (list 'horiz
2000 "matrix("
2001 math-comp-left-bracket
2002 (math-compose-vector (cdr a)
2003 (concat math-comp-comma " ")
2004 math-comp-vector-prec)
2005 math-comp-right-bracket
2006 ")"))))
2008 (put 'maple 'math-compose-subscr
2009 (function
2010 (lambda (a)
2011 (let ((args (cdr (cdr a))))
2012 (list 'horiz
2013 (math-compose-expr (nth 1 a) 1000)
2015 (math-compose-vector args ", " 0)
2016 "]")))))
2018 (add-to-list 'calc-lang-allow-underscores 'maple)
2019 (add-to-list 'calc-lang-brackets-are-subscripts 'maple)
2021 (defun math-compose-maple-intv (a)
2022 (list 'horiz
2023 (math-compose-expr (nth 2 a) 0)
2024 " .. "
2025 (math-compose-expr (nth 3 a) 0)))
2027 (defun math-read-maple-dots (x op)
2028 (list 'intv 3 x (math-read-expr-level (nth 3 op))))
2031 ;; The variable math-read-big-lines is local to math-read-big-expr in
2032 ;; calc-ext.el, but is used by math-read-big-rec, math-read-big-char,
2033 ;; math-read-big-emptyp, math-read-big-error and math-read-big-balance,
2034 ;; which are called (directly and indirectly) by math-read-big-expr.
2035 ;; It is also local to math-read-big-bigp in calc-ext.el, which calls
2036 ;; math-read-big-balance.
2037 (defvar math-read-big-lines)
2039 ;; The variables math-read-big-baseline and math-read-big-h2 are
2040 ;; local to math-read-big-expr in calc-ext.el, but used by
2041 ;; math-read-big-rec.
2042 (defvar math-read-big-baseline)
2043 (defvar math-read-big-h2)
2045 ;; The variables math-rb-h1, math-rb-h2, math-rb-v1 and math-rb-v2
2046 ;; are local to math-read-big-rec, but are used by math-read-big-char,
2047 ;; math-read-big-emptyp and math-read-big-balance which are called by
2048 ;; math-read-big-rec.
2049 ;; math-rb-h2 is also local to math-read-big-bigp in calc-ext.el,
2050 ;; which calls math-read-big-balance.
2051 (defvar math-rb-h1)
2052 (defvar math-rb-h2)
2053 (defvar math-rb-v1)
2054 (defvar math-rb-v2)
2056 (defun math-read-big-rec (math-rb-h1 math-rb-v1 math-rb-h2 math-rb-v2
2057 &optional baseline prec short)
2058 (or prec (setq prec 0))
2060 ;; Clip whitespace above or below.
2061 (while (and (< math-rb-v1 math-rb-v2)
2062 (math-read-big-emptyp math-rb-h1 math-rb-v1 math-rb-h2 (1+ math-rb-v1)))
2063 (setq math-rb-v1 (1+ math-rb-v1)))
2064 (while (and (< math-rb-v1 math-rb-v2)
2065 (math-read-big-emptyp math-rb-h1 (1- math-rb-v2) math-rb-h2 math-rb-v2))
2066 (setq math-rb-v2 (1- math-rb-v2)))
2068 ;; If formula is a single line high, normal parser can handle it.
2069 (if (<= math-rb-v2 (1+ math-rb-v1))
2070 (if (or (<= math-rb-v2 math-rb-v1)
2071 (> math-rb-h1 (length (setq math-rb-v2
2072 (nth math-rb-v1 math-read-big-lines)))))
2073 (math-read-big-error math-rb-h1 math-rb-v1)
2074 (setq math-read-big-baseline math-rb-v1
2075 math-read-big-h2 math-rb-h2
2076 math-rb-v2 (nth math-rb-v1 math-read-big-lines)
2077 math-rb-h2 (math-read-expr
2078 (substring math-rb-v2 math-rb-h1
2079 (min math-rb-h2 (length math-rb-v2)))))
2080 (if (eq (car-safe math-rb-h2) 'error)
2081 (math-read-big-error (+ math-rb-h1 (nth 1 math-rb-h2))
2082 math-rb-v1 (nth 2 math-rb-h2))
2083 math-rb-h2))
2085 ;; Clip whitespace at left or right.
2086 (while (and (< math-rb-h1 math-rb-h2)
2087 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) math-rb-v2))
2088 (setq math-rb-h1 (1+ math-rb-h1)))
2089 (while (and (< math-rb-h1 math-rb-h2)
2090 (math-read-big-emptyp (1- math-rb-h2) math-rb-v1 math-rb-h2 math-rb-v2))
2091 (setq math-rb-h2 (1- math-rb-h2)))
2093 ;; Scan to find widest left-justified "----" in the region.
2094 (let* ((widest nil)
2095 (widest-h2 0)
2096 (lines-v1 (nthcdr math-rb-v1 math-read-big-lines))
2097 (p lines-v1)
2098 (v math-rb-v1)
2099 (other-v nil)
2100 other-char line len h)
2101 (while (< v math-rb-v2)
2102 (setq line (car p)
2103 len (min math-rb-h2 (length line)))
2104 (and (< math-rb-h1 len)
2105 (/= (aref line math-rb-h1) ?\ )
2106 (if (and (= (aref line math-rb-h1) ?\-)
2107 ;; Make sure it's not a minus sign.
2108 (or (and (< (1+ math-rb-h1) len)
2109 (= (aref line (1+ math-rb-h1)) ?\-))
2110 (/= (math-read-big-char math-rb-h1 (1- v)) ?\ )
2111 (/= (math-read-big-char math-rb-h1 (1+ v)) ?\ )))
2112 (progn
2113 (setq h math-rb-h1)
2114 (while (and (< (setq h (1+ h)) len)
2115 (= (aref line h) ?\-)))
2116 (if (> h widest-h2)
2117 (setq widest v
2118 widest-h2 h)))
2119 (or other-v (setq other-v v other-char (aref line math-rb-h1)))))
2120 (setq v (1+ v)
2121 p (cdr p)))
2123 (cond ((not (setq v other-v))
2124 (math-read-big-error math-rb-h1 math-rb-v1)) ; Should never happen!
2126 ;; Quotient.
2127 (widest
2128 (setq h widest-h2
2129 v widest)
2130 (let ((num (math-read-big-rec math-rb-h1 math-rb-v1 h v))
2131 (den (math-read-big-rec math-rb-h1 (1+ v) h math-rb-v2)))
2132 (setq p (if (and (math-integerp num) (math-integerp den))
2133 (math-make-frac num den)
2134 (list '/ num den)))))
2136 ;; Big radical sign.
2137 ((= other-char ?\\)
2138 (or (= (math-read-big-char (1+ math-rb-h1) v) ?\|)
2139 (math-read-big-error (1+ math-rb-h1) v "Malformed root sign"))
2140 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
2141 (while (= (math-read-big-char (1+ math-rb-h1) (setq v (1- v))) ?\|))
2142 (or (= (math-read-big-char (setq h (+ math-rb-h1 2)) v) ?\_)
2143 (math-read-big-error h v "Malformed root sign"))
2144 (while (= (math-read-big-char (setq h (1+ h)) v) ?\_))
2145 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
2146 (math-read-big-emptyp math-rb-h1 (1+ other-v) h math-rb-v2 nil t)
2147 (setq p (list 'calcFunc-sqrt (math-read-big-rec
2148 (+ math-rb-h1 2) (1+ v)
2149 h (1+ other-v) baseline))
2150 v math-read-big-baseline))
2152 ;; Small radical sign.
2153 ((and (= other-char ?V)
2154 (= (math-read-big-char (1+ math-rb-h1) (1- v)) ?\_))
2155 (setq h (1+ math-rb-h1))
2156 (math-read-big-emptyp math-rb-h1 math-rb-v1 h (1- v) nil t)
2157 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)
2158 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
2159 (while (= (math-read-big-char (setq h (1+ h)) (1- v)) ?\_))
2160 (setq p (list 'calcFunc-sqrt (math-read-big-rec
2161 (1+ math-rb-h1) v h (1+ v) t))
2162 v math-read-big-baseline))
2164 ;; Binomial coefficient.
2165 ((and (= other-char ?\()
2166 (= (math-read-big-char (1+ math-rb-h1) v) ?\ )
2167 (= (string-match "( *)" (nth v math-read-big-lines)
2168 math-rb-h1) math-rb-h1))
2169 (setq h (match-end 0))
2170 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
2171 (math-read-big-emptyp math-rb-h1 (1+ v) (1+ math-rb-h1) math-rb-v2 nil t)
2172 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
2173 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
2174 (setq p (list 'calcFunc-choose
2175 (math-read-big-rec (1+ math-rb-h1) math-rb-v1 (1- h) v)
2176 (math-read-big-rec (1+ math-rb-h1) (1+ v)
2177 (1- h) math-rb-v2))))
2179 ;; Minus sign.
2180 ((= other-char ?\-)
2181 (setq p (list 'neg (math-read-big-rec (1+ math-rb-h1) math-rb-v1
2182 math-rb-h2 math-rb-v2 v 250 t))
2183 v math-read-big-baseline
2184 h math-read-big-h2))
2186 ;; Parentheses.
2187 ((= other-char ?\()
2188 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
2189 (math-read-big-emptyp math-rb-h1 (1+ v) (1+ math-rb-h1) math-rb-v2 nil t)
2190 (setq h (math-read-big-balance (1+ math-rb-h1) v "(" t))
2191 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
2192 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
2193 (let ((sep (math-read-big-char (1- h) v))
2194 hmid)
2195 (if (= sep ?\.)
2196 (setq h (1+ h)))
2197 (if (= sep ?\])
2198 (math-read-big-error (1- h) v "Expected `)'"))
2199 (if (= sep ?\))
2200 (setq p (math-read-big-rec
2201 (1+ math-rb-h1) math-rb-v1 (1- h) math-rb-v2 v))
2202 (setq hmid (math-read-big-balance h v "(")
2203 p (list p
2204 (math-read-big-rec h math-rb-v1 (1- hmid) math-rb-v2 v))
2205 h hmid)
2206 (cond ((= sep ?\.)
2207 (setq p (cons 'intv (cons (if (= (math-read-big-char
2208 (1- h) v)
2209 ?\))
2210 0 1)
2211 p))))
2212 ((= (math-read-big-char (1- h) v) ?\])
2213 (math-read-big-error (1- h) v "Expected `)'"))
2214 ((= sep ?\,)
2215 (or (and (math-realp (car p)) (math-realp (nth 1 p)))
2216 (math-read-big-error
2217 math-rb-h1 v "Complex components must be real"))
2218 (setq p (cons 'cplx p)))
2219 ((= sep ?\;)
2220 (or (and (math-realp (car p)) (math-anglep (nth 1 p)))
2221 (math-read-big-error
2222 math-rb-h1 v "Complex components must be real"))
2223 (setq p (cons 'polar p)))))))
2225 ;; Matrix.
2226 ((and (= other-char ?\[)
2227 (or (= (math-read-big-char (setq h math-rb-h1) (1+ v)) ?\[)
2228 (= (math-read-big-char (setq h (1+ h)) v) ?\[)
2229 (and (= (math-read-big-char h v) ?\ )
2230 (= (math-read-big-char (setq h (1+ h)) v) ?\[)))
2231 (= (math-read-big-char h (1+ v)) ?\[))
2232 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
2233 (let ((vtop v)
2234 (hleft h)
2235 (hright nil))
2236 (setq p nil)
2237 (while (progn
2238 (setq h (math-read-big-balance (1+ hleft) v "["))
2239 (if hright
2240 (or (= h hright)
2241 (math-read-big-error hright v "Expected `]'"))
2242 (setq hright h))
2243 (setq p (cons (math-read-big-rec
2244 hleft v h (1+ v)) p))
2245 (and (memq (math-read-big-char h v) '(?\ ?\,))
2246 (= (math-read-big-char hleft (1+ v)) ?\[)))
2247 (setq v (1+ v)))
2248 (or (= hleft math-rb-h1)
2249 (progn
2250 (if (= (math-read-big-char h v) ?\ )
2251 (setq h (1+ h)))
2252 (and (= (math-read-big-char h v) ?\])
2253 (setq h (1+ h))))
2254 (math-read-big-error (1- h) v "Expected `]'"))
2255 (if (= (math-read-big-char h vtop) ?\,)
2256 (setq h (1+ h)))
2257 (math-read-big-emptyp math-rb-h1 (1+ v) (1- h) math-rb-v2 nil t)
2258 (setq v (+ vtop (/ (- v vtop) 2))
2259 p (cons 'vec (nreverse p)))))
2261 ;; Square brackets.
2262 ((= other-char ?\[)
2263 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
2264 (math-read-big-emptyp math-rb-h1 (1+ v) (1+ math-rb-h1) math-rb-v2 nil t)
2265 (setq p nil
2266 h (1+ math-rb-h1))
2267 (while (progn
2268 (setq widest (math-read-big-balance h v "[" t))
2269 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
2270 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
2271 (setq p (cons (math-read-big-rec
2272 h math-rb-v1 (1- widest) math-rb-v2 v) p)
2273 h widest)
2274 (= (math-read-big-char (1- h) v) ?\,)))
2275 (setq widest (math-read-big-char (1- h) v))
2276 (if (or (memq widest '(?\; ?\)))
2277 (and (eq widest ?\.) (cdr p)))
2278 (math-read-big-error (1- h) v "Expected `]'"))
2279 (if (= widest ?\.)
2280 (setq h (1+ h)
2281 widest (math-read-big-balance h v "[")
2282 p (nconc p (list (math-read-big-rec
2283 h math-rb-v1 (1- widest) math-rb-v2 v)))
2284 h widest
2285 p (cons 'intv (cons (if (= (math-read-big-char (1- h) v)
2286 ?\])
2287 3 2)
2288 p)))
2289 (setq p (cons 'vec (nreverse p)))))
2291 ;; Date form.
2292 ((= other-char ?\<)
2293 (setq line (nth v math-read-big-lines))
2294 (string-match ">" line math-rb-h1)
2295 (setq h (match-end 0))
2296 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
2297 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)
2298 (setq p (math-read-big-rec math-rb-h1 v h (1+ v) v)))
2300 ;; Variable name or function call.
2301 ((or (and (>= other-char ?a) (<= other-char ?z))
2302 (and (>= other-char ?A) (<= other-char ?Z)))
2303 (setq line (nth v math-read-big-lines))
2304 (string-match "\\([a-zA-Z'_]+\\) *" line math-rb-h1)
2305 (setq h (match-end 1)
2306 widest (match-end 0)
2307 p (math-match-substring line 1))
2308 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
2309 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)
2310 (if (= (math-read-big-char widest v) ?\()
2311 (progn
2312 (setq line (if (string-match "-" p)
2313 (intern p)
2314 (intern (concat "calcFunc-" p)))
2315 h (1+ widest)
2316 p nil)
2317 (math-read-big-emptyp widest math-rb-v1 h v nil t)
2318 (math-read-big-emptyp widest (1+ v) h math-rb-v2 nil t)
2319 (while (progn
2320 (setq widest (math-read-big-balance h v "(" t))
2321 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
2322 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
2323 (setq p (cons (math-read-big-rec
2324 h math-rb-v1 (1- widest) math-rb-v2 v) p)
2325 h widest)
2326 (= (math-read-big-char (1- h) v) ?\,)))
2327 (or (= (math-read-big-char (1- h) v) ?\))
2328 (math-read-big-error (1- h) v "Expected `)'"))
2329 (setq p (cons line (nreverse p))))
2330 (setq p (list 'var
2331 (intern (math-remove-dashes p))
2332 (if (string-match "-" p)
2333 (intern p)
2334 (intern (concat "var-" p)))))))
2336 ;; Number.
2338 (setq line (nth v math-read-big-lines))
2339 (or (= (string-match "_?\\([0-9]+.?0*@ *\\)?\\([0-9]+.?0*' *\\)?\\([0-9]+\\(#\\|\\^\\^\\)[0-9a-zA-Z:]+\\|[0-9]+:[0-9:]+\\|[0-9.]+\\([eE][-+_]?[0-9]+\\)?\"?\\)?" line math-rb-h1) math-rb-h1)
2340 (math-read-big-error h v "Expected a number"))
2341 (setq h (match-end 0)
2342 p (math-read-number (math-match-substring line 0)))
2343 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
2344 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)))
2346 ;; Now left term is bounded by math-rb-h1, math-rb-v1, h, math-rb-v2;
2347 ;; baseline = v.
2348 (if baseline
2349 (or (= v baseline)
2350 (math-read-big-error math-rb-h1 v "Inconsistent baseline in formula"))
2351 (setq baseline v))
2353 ;; Look for superscripts or subscripts.
2354 (setq line (nth baseline math-read-big-lines)
2355 len (min math-rb-h2 (length line))
2356 widest h)
2357 (while (and (< widest len)
2358 (= (aref line widest) ?\ ))
2359 (setq widest (1+ widest)))
2360 (and (>= widest len) (setq widest math-rb-h2))
2361 (if (math-read-big-emptyp h v widest math-rb-v2)
2362 (if (math-read-big-emptyp h math-rb-v1 widest v)
2363 (setq h widest)
2364 (setq p (list '^ p (math-read-big-rec h math-rb-v1 widest v))
2365 h widest))
2366 (if (math-read-big-emptyp h math-rb-v1 widest v)
2367 (setq p (list 'calcFunc-subscr p
2368 (math-read-big-rec h v widest math-rb-v2))
2369 h widest)))
2371 ;; Look for an operator name and grab additional terms.
2372 (while (and (< h len)
2373 (if (setq widest (and (math-read-big-emptyp
2374 h math-rb-v1 (1+ h) v)
2375 (math-read-big-emptyp
2376 h (1+ v) (1+ h) math-rb-v2)
2377 (string-match "<=\\|>=\\|\\+/-\\|!=\\|&&\\|||\\|:=\\|=>\\|." line h)
2378 (assoc (math-match-substring line 0)
2379 (math-standard-ops))))
2380 (and (>= (nth 2 widest) prec)
2381 (setq h (match-end 0)))
2382 (and (not (eq (string-match ",\\|;\\|\\.\\.\\|)\\|\\]\\|:" line h)
2384 (setq widest '("2x" * 196 195)))))
2385 (cond ((eq (nth 3 widest) -1)
2386 (setq p (list (nth 1 widest) p)))
2387 ((equal (car widest) "?")
2388 (let ((y (math-read-big-rec h math-rb-v1 math-rb-h2
2389 math-rb-v2 baseline nil t)))
2390 (or (= (math-read-big-char math-read-big-h2 baseline) ?\:)
2391 (math-read-big-error math-read-big-h2 baseline "Expected `:'"))
2392 (setq p (list (nth 1 widest) p y
2393 (math-read-big-rec
2394 (1+ math-read-big-h2) math-rb-v1 math-rb-h2 math-rb-v2
2395 baseline (nth 3 widest) t))
2396 h math-read-big-h2)))
2398 (setq p (list (nth 1 widest) p
2399 (math-read-big-rec h math-rb-v1 math-rb-h2 math-rb-v2
2400 baseline (nth 3 widest) t))
2401 h math-read-big-h2))))
2403 ;; Return all relevant information to caller.
2404 (setq math-read-big-baseline baseline
2405 math-read-big-h2 h)
2406 (or short (= math-read-big-h2 math-rb-h2)
2407 (math-read-big-error h baseline))
2408 p)))
2410 (defun math-read-big-char (h v)
2411 (or (and (>= h math-rb-h1)
2412 (< h math-rb-h2)
2413 (>= v math-rb-v1)
2414 (< v math-rb-v2)
2415 (let ((line (nth v math-read-big-lines)))
2416 (and line
2417 (< h (length line))
2418 (aref line h))))
2419 ?\ ))
2421 (defun math-read-big-emptyp (eh1 ev1 eh2 ev2 &optional what error)
2422 (and (< ev1 math-rb-v1) (setq ev1 math-rb-v1))
2423 (and (< eh1 math-rb-h1) (setq eh1 math-rb-h1))
2424 (and (> ev2 math-rb-v2) (setq ev2 math-rb-v2))
2425 (and (> eh2 math-rb-h2) (setq eh2 math-rb-h2))
2426 (or what (setq what ?\ ))
2427 (let ((p (nthcdr ev1 math-read-big-lines))
2429 (while (and (< ev1 ev2)
2430 (progn
2431 (setq h (min eh2 (length (car p))))
2432 (while (and (>= (setq h (1- h)) eh1)
2433 (= (aref (car p) h) what)))
2434 (and error (>= h eh1)
2435 (math-read-big-error h ev1 (if (stringp error)
2436 error
2437 "Whitespace expected")))
2438 (< h eh1)))
2439 (setq ev1 (1+ ev1)
2440 p (cdr p)))
2441 (>= ev1 ev2)))
2443 ;; math-read-big-err-msg is local to math-read-big-expr in calc-ext.el,
2444 ;; but is used by math-read-big-error which is called (indirectly) by
2445 ;; math-read-big-expr.
2446 (defvar math-read-big-err-msg)
2448 (defun math-read-big-error (h v &optional msg)
2449 (let ((pos 0)
2450 (p math-read-big-lines))
2451 (while (> v 0)
2452 (setq pos (+ pos 1 (length (car p)))
2453 p (cdr p)
2454 v (1- v)))
2455 (setq h (+ pos (min h (length (car p))))
2456 math-read-big-err-msg (list 'error h (or msg "Syntax error")))
2457 (throw 'syntax nil)))
2459 (defun math-read-big-balance (h v what &optional commas)
2460 (let* ((line (nth v math-read-big-lines))
2461 (len (min math-rb-h2 (length line)))
2462 (count 1))
2463 (while (> count 0)
2464 (if (>= h len)
2465 (if what
2466 (math-read-big-error nil v (format "Unmatched `%s'" what))
2467 (setq count 0))
2468 (if (memq (aref line h) '(?\( ?\[))
2469 (setq count (1+ count))
2470 (if (if (and commas (= count 1))
2471 (or (memq (aref line h) '(?\) ?\] ?\, ?\;))
2472 (and (eq (aref line h) ?\.)
2473 (< (1+ h) len)
2474 (eq (aref line (1+ h)) ?\.)))
2475 (memq (aref line h) '(?\) ?\])))
2476 (setq count (1- count))))
2477 (setq h (1+ h))))
2480 (provide 'calc-lang)
2482 ;; arch-tag: 483bfe15-f290-4fef-bb7d-ce65be687f2e
2483 ;;; calc-lang.el ends here