1 ;;; calc-lang.el --- calc language functions
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 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, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 ;; This file is autoloaded from calc-ext.el.
36 ;; Declare functions which are defined elsewhere.
37 (declare-function math-compose-vector "calccomp" (a sep prec))
38 (declare-function math-compose-var "calccomp" (a))
39 (declare-function math-tex-expr-is-flat "calccomp" (a))
40 (declare-function math-read-factor "calc-aent" ())
41 (declare-function math-read-expr-level "calc-aent" (exp-prec &optional exp-term))
43 ;; Declare variables which are defined elsewhere.
44 (defvar calc-lang-slash-idiv)
45 (defvar calc-lang-allow-underscores)
46 (defvar math-comp-left-bracket)
47 (defvar math-comp-right-bracket)
48 (defvar math-comp-comma)
49 (defvar math-comp-vector-prec)
51 ;;; Alternate entry/display languages.
53 (defun calc-set-language (lang &optional option no-refresh)
54 (setq math-expr-opers (or (get lang 'math-oper-table) (math-standard-ops))
55 math-expr-function-mapping (get lang 'math-function-table)
56 math-expr-variable-mapping (get lang 'math-variable-table)
57 calc-language-input-filter (get lang 'math-input-filter)
58 calc-language-output-filter (get lang 'math-output-filter)
59 calc-vector-brackets (or (get lang 'math-vector-brackets) "[]")
60 calc-complex-format (get lang 'math-complex-format)
61 calc-radix-formatter (get lang 'math-radix-formatter)
62 calc-function-open (or (get lang 'math-function-open) "(")
63 calc-function-close (or (get lang 'math-function-close) ")"))
65 (setq calc-language lang
66 calc-language-option option)
67 (calc-change-mode '(calc-language calc-language-option)
68 (list lang option) t)))
70 (defun calc-normal-language ()
73 (calc-set-language nil)
74 (message "Normal language mode")))
76 (defun calc-flat-language ()
79 (calc-set-language 'flat)
80 (message "Flat language mode (all stack entries shown on one line)")))
82 (defun calc-big-language ()
85 (calc-set-language 'big)
86 (message "\"Big\" language mode")))
88 (defun calc-unformatted-language ()
91 (calc-set-language 'unform)
92 (message "Unformatted language mode")))
95 (defun calc-c-language ()
98 (calc-set-language 'c)
99 (message "`C' language mode")))
101 (put 'c 'math-oper-table
102 '( ( "u!" calcFunc-lnot -1 1000 )
103 ( "~" calcFunc-not -1 1000 )
104 ( "u+" ident -1 197 )
111 ( "<<" calcFunc-lsh 170 171 )
112 ( ">>" calcFunc-rsh 170 171 )
113 ( "<" calcFunc-lt 160 161 )
114 ( ">" calcFunc-gt 160 161 )
115 ( "<=" calcFunc-leq 160 161 )
116 ( ">=" calcFunc-geq 160 161 )
117 ( "==" calcFunc-eq 150 151 )
118 ( "!=" calcFunc-neq 150 151 )
119 ( "&" calcFunc-and 140 141 )
120 ( "^" calcFunc-xor 131 130 )
121 ( "|" calcFunc-or 120 121 )
122 ( "&&" calcFunc-land 110 111 )
123 ( "||" calcFunc-lor 100 101 )
124 ( "?" (math-read-if) 91 90 )
125 ( "!!!" calcFunc-pnot -1 88 )
126 ( "&&&" calcFunc-pand 85 86 )
127 ( "|||" calcFunc-por 75 76 )
128 ( "=" calcFunc-assign 51 50 )
129 ( ":=" calcFunc-assign 51 50 )
130 ( "::" calcFunc-condition 45 46 ))) ; should support full assignments
132 (put 'c 'math-function-table
133 '( ( acos . calcFunc-arccos )
134 ( acosh . calcFunc-arccosh )
135 ( asin . calcFunc-arcsin )
136 ( asinh . calcFunc-arcsinh )
137 ( atan . calcFunc-arctan )
138 ( atan2 . calcFunc-arctan2 )
139 ( atanh . calcFunc-arctanh )))
141 (put 'c 'math-variable-table
145 (put 'c 'math-vector-brackets "{}")
147 (put 'c 'math-radix-formatter
148 (function (lambda (r s)
149 (if (= r 16) (format "0x%s" s)
150 (if (= r 8) (format "0%s" s)
151 (format "%d#%s" r s))))))
153 (put 'c 'math-compose-subscr
156 (let ((args (cdr (cdr a))))
158 (math-compose-expr (nth 1 a) 1000)
160 (math-compose-vector args ", " 0)
163 (add-to-list 'calc-lang-slash-idiv 'c)
164 (add-to-list 'calc-lang-allow-underscores 'c)
165 (add-to-list 'calc-lang-c-type-hex 'c)
166 (add-to-list 'calc-lang-brackets-are-subscripts 'c)
168 (defun calc-pascal-language (n)
171 (and n (setq n (prefix-numeric-value n)))
172 (calc-set-language 'pascal n)
173 (message (if (and n (/= n 0))
175 "Pascal language mode (all uppercase)"
176 "Pascal language mode (all lowercase)")
177 "Pascal language mode"))))
179 (put 'pascal 'math-oper-table
180 '( ( "not" calcFunc-lnot -1 1000 )
183 ( "and" calcFunc-and 190 191 )
184 ( "div" calcFunc-idiv 190 191 )
186 ( "u+" ident -1 185 )
190 ( "or" calcFunc-or 180 181 )
191 ( "xor" calcFunc-xor 180 181 )
192 ( "shl" calcFunc-lsh 180 181 )
193 ( "shr" calcFunc-rsh 180 181 )
194 ( "in" calcFunc-in 160 161 )
195 ( "<" calcFunc-lt 160 161 )
196 ( ">" calcFunc-gt 160 161 )
197 ( "<=" calcFunc-leq 160 161 )
198 ( ">=" calcFunc-geq 160 161 )
199 ( "=" calcFunc-eq 160 161 )
200 ( "<>" calcFunc-neq 160 161 )
201 ( "!!!" calcFunc-pnot -1 85 )
202 ( "&&&" calcFunc-pand 80 81 )
203 ( "|||" calcFunc-por 75 76 )
204 ( ":=" calcFunc-assign 51 50 )
205 ( "::" calcFunc-condition 45 46 )))
207 (put 'pascal 'math-input-filter 'calc-input-case-filter)
208 (put 'pascal 'math-output-filter 'calc-output-case-filter)
210 (put 'pascal 'math-radix-formatter
211 (function (lambda (r s)
212 (if (= r 16) (format "$%s" s)
213 (format "%d#%s" r s)))))
215 (put 'pascal 'math-lang-read-symbol
218 "\\(\\$[0-9a-fA-F]+\\)\\($\\|[^0-9a-zA-Z]\\)"
219 math-exp-str math-exp-pos)
221 (setq math-exp-token 'number
222 math-expr-data (math-match-substring math-exp-str 1)
223 math-exp-pos (match-end 1)))))
225 (put 'pascal 'math-compose-subscr
228 (let ((args (cdr (cdr a))))
229 (while (eq (car-safe (nth 1 a)) 'calcFunc-subscr)
230 (setq args (append (cdr (cdr (nth 1 a))) args)
233 (math-compose-expr (nth 1 a) 1000)
235 (math-compose-vector args ", " 0)
238 (add-to-list 'calc-lang-allow-underscores 'pascal)
239 (add-to-list 'calc-lang-brackets-are-subscripts 'pascal)
241 (defun calc-input-case-filter (str)
242 (cond ((or (null calc-language-option) (= calc-language-option 0))
247 (defun calc-output-case-filter (str)
248 (cond ((or (null calc-language-option) (= calc-language-option 0))
250 ((> calc-language-option 0)
256 (defun calc-fortran-language (n)
259 (and n (setq n (prefix-numeric-value n)))
260 (calc-set-language 'fortran n)
261 (message (if (and n (/= n 0))
263 "FORTRAN language mode (all uppercase)"
264 "FORTRAN language mode (all lowercase)")
265 "FORTRAN language mode"))))
267 (put 'fortran 'math-oper-table
268 '( ( "u/" (math-parse-fortran-vector) -1 1 )
269 ( "/" (math-parse-fortran-vector-end) 1 -1 )
271 ( "u+" ident -1 191 )
277 ( ".LT." calcFunc-lt 160 161 )
278 ( ".GT." calcFunc-gt 160 161 )
279 ( ".LE." calcFunc-leq 160 161 )
280 ( ".GE." calcFunc-geq 160 161 )
281 ( ".EQ." calcFunc-eq 160 161 )
282 ( ".NE." calcFunc-neq 160 161 )
283 ( ".NOT." calcFunc-lnot -1 121 )
284 ( ".AND." calcFunc-land 110 111 )
285 ( ".OR." calcFunc-lor 100 101 )
286 ( "!!!" calcFunc-pnot -1 85 )
287 ( "&&&" calcFunc-pand 80 81 )
288 ( "|||" calcFunc-por 75 76 )
289 ( "=" calcFunc-assign 51 50 )
290 ( ":=" calcFunc-assign 51 50 )
291 ( "::" calcFunc-condition 45 46 )))
293 (put 'fortran 'math-vector-brackets "//")
295 (put 'fortran 'math-function-table
296 '( ( acos . calcFunc-arccos )
297 ( acosh . calcFunc-arccosh )
298 ( aimag . calcFunc-im )
299 ( aint . calcFunc-ftrunc )
300 ( asin . calcFunc-arcsin )
301 ( asinh . calcFunc-arcsinh )
302 ( atan . calcFunc-arctan )
303 ( atan2 . calcFunc-arctan2 )
304 ( atanh . calcFunc-arctanh )
305 ( conjg . calcFunc-conj )
306 ( log . calcFunc-ln )
307 ( nint . calcFunc-round )
308 ( real . calcFunc-re )))
310 (put 'fortran 'math-input-filter 'calc-input-case-filter)
312 (put 'fortran 'math-output-filter 'calc-output-case-filter)
314 (put 'fortran 'math-lang-read-symbol
316 (eq (string-match "\\.[a-zA-Z][a-zA-Z][a-zA-Z]?\\."
317 math-exp-str math-exp-pos) math-exp-pos)
318 (setq math-exp-token 'punc
319 math-expr-data (upcase (math-match-substring math-exp-str 0))
320 math-exp-pos (match-end 0)))))
322 (put 'fortran 'math-compose-subscr
325 (let ((args (cdr (cdr a))))
326 (while (eq (car-safe (nth 1 a)) 'calcFunc-subscr)
327 (setq args (append (cdr (cdr (nth 1 a))) args)
330 (math-compose-expr (nth 1 a) 1000)
332 (math-compose-vector args ", " 0)
335 (add-to-list 'calc-lang-slash-idiv 'fortran)
336 (add-to-list 'calc-lang-allow-underscores 'fortran)
337 (add-to-list 'calc-lang-parens-are-subscripts 'fortran)
339 ;; The next few variables are local to math-read-exprs in calc-aent.el
340 ;; and math-read-expr in calc-ext.el, but are set in functions they call.
342 (defvar math-exp-token)
343 (defvar math-expr-data)
344 (defvar math-exp-old-pos)
346 (defvar math-parsing-fortran-vector nil)
347 (defun math-parse-fortran-vector (op)
348 (let ((math-parsing-fortran-vector '(end . "\000")))
350 (math-read-brackets t "]")
351 (setq math-exp-token (car math-parsing-fortran-vector)
352 math-expr-data (cdr math-parsing-fortran-vector)))))
354 (defun math-parse-fortran-vector-end (x op)
355 (if math-parsing-fortran-vector
357 (setq math-parsing-fortran-vector (cons math-exp-token math-expr-data)
359 math-expr-data "\000")
361 (throw 'syntax "Unmatched closing `/'")))
363 (defun math-parse-fortran-subscr (sym args)
364 (setq sym (math-build-var-name sym))
366 (setq sym (list 'calcFunc-subscr sym (car args))
371 (defun calc-tex-language (n)
374 (and n (setq n (prefix-numeric-value n)))
375 (calc-set-language 'tex n)
377 (message "TeX language mode"))
379 (message "TeX language mode with multiline matrices"))
381 (message "TeX language mode with \\hbox{func}(\\hbox{var})"))
384 "TeX language mode with \\hbox{func}(\\hbox{var}) and multiline matrices"))
386 (message "TeX language mode with \\func(\\hbox{var})"))
389 "TeX language mode with \\func(\\hbox{var}) and multiline matrices")))))
391 (defun calc-latex-language (n)
394 (and n (setq n (prefix-numeric-value n)))
395 (calc-set-language 'latex n)
397 (message "LaTeX language mode"))
399 (message "LaTeX language mode with multiline matrices"))
401 (message "LaTeX language mode with \\text{func}(\\text{var})"))
404 "LaTeX language mode with \\text{func}(\\text{var}) and multiline matrices"))
406 (message "LaTeX language mode with \\func(\\text{var})"))
409 "LaTeX language mode with \\func(\\text{var}) and multiline matrices")))))
411 (put 'tex 'math-lang-name "TeX")
412 (put 'latex 'math-lang-name "LaTeX")
414 (put 'tex 'math-oper-table
415 '( ( "\\hat" calcFunc-hat -1 950 )
416 ( "\\check" calcFunc-check -1 950 )
417 ( "\\tilde" calcFunc-tilde -1 950 )
418 ( "\\acute" calcFunc-acute -1 950 )
419 ( "\\grave" calcFunc-grave -1 950 )
420 ( "\\dot" calcFunc-dot -1 950 )
421 ( "\\ddot" calcFunc-dotdot -1 950 )
422 ( "\\breve" calcFunc-breve -1 950 )
423 ( "\\bar" calcFunc-bar -1 950 )
424 ( "\\vec" calcFunc-Vec -1 950 )
425 ( "\\underline" calcFunc-under -1 950 )
426 ( "u|" calcFunc-abs -1 0 )
428 ( "\\lfloor" calcFunc-floor -1 0 )
429 ( "\\rfloor" closing 0 -1 )
430 ( "\\lceil" calcFunc-ceil -1 0 )
431 ( "\\rceil" closing 0 -1 )
432 ( "\\pm" sdev 300 300 )
433 ( "!" calcFunc-fact 210 -1 )
435 ( "_" calcFunc-subscr 201 200 )
436 ( "u+" ident -1 197 )
438 ( "\\times" * 191 190 )
443 ( "\\over" / 170 171 )
445 ( "\\choose" calcFunc-choose 170 171 )
446 ( "\\mod" % 170 171 )
447 ( "<" calcFunc-lt 160 161 )
448 ( ">" calcFunc-gt 160 161 )
449 ( "\\leq" calcFunc-leq 160 161 )
450 ( "\\geq" calcFunc-geq 160 161 )
451 ( "=" calcFunc-eq 160 161 )
452 ( "\\neq" calcFunc-neq 160 161 )
453 ( "\\ne" calcFunc-neq 160 161 )
454 ( "\\lnot" calcFunc-lnot -1 121 )
455 ( "\\land" calcFunc-land 110 111 )
456 ( "\\lor" calcFunc-lor 100 101 )
457 ( "?" (math-read-if) 91 90 )
458 ( "!!!" calcFunc-pnot -1 85 )
459 ( "&&&" calcFunc-pand 80 81 )
460 ( "|||" calcFunc-por 75 76 )
461 ( "\\gets" calcFunc-assign 51 50 )
462 ( ":=" calcFunc-assign 51 50 )
463 ( "::" calcFunc-condition 45 46 )
464 ( "\\to" calcFunc-evalto 40 41 )
465 ( "\\to" calcFunc-evalto 40 -1 )
466 ( "=>" calcFunc-evalto 40 41 )
467 ( "=>" calcFunc-evalto 40 -1 )))
469 (put 'tex 'math-function-table
470 '( ( \\arccos . calcFunc-arccos )
471 ( \\arcsin . calcFunc-arcsin )
472 ( \\arctan . calcFunc-arctan )
473 ( \\arg . calcFunc-arg )
474 ( \\cos . calcFunc-cos )
475 ( \\cosh . calcFunc-cosh )
476 ( \\cot . calcFunc-cot )
477 ( \\coth . calcFunc-coth )
478 ( \\csc . calcFunc-csc )
479 ( \\det . calcFunc-det )
480 ( \\exp . calcFunc-exp )
481 ( \\gcd . calcFunc-gcd )
482 ( \\ln . calcFunc-ln )
483 ( \\log . calcFunc-log10 )
484 ( \\max . calcFunc-max )
485 ( \\min . calcFunc-min )
486 ( \\sec . calcFunc-sec )
487 ( \\sin . calcFunc-sin )
488 ( \\sinh . calcFunc-sinh )
489 ( \\sqrt . calcFunc-sqrt )
490 ( \\tan . calcFunc-tan )
491 ( \\tanh . calcFunc-tanh )
492 ( \\phi . calcFunc-totient )
493 ( \\mu . calcFunc-moebius )))
495 (put 'tex 'math-special-function-table
496 '((calcFunc-sum . (math-compose-tex-sum "\\sum"))
497 (calcFunc-prod . (math-compose-tex-sum "\\prod"))
498 (intv . math-compose-tex-intv)))
500 (put 'tex 'math-variable-table
503 ( \\alpha . var-alpha )
504 ( \\beta . var-beta )
505 ( \\gamma . var-gamma )
506 ( \\Gamma . var-Gamma )
507 ( \\delta . var-delta )
508 ( \\Delta . var-Delta )
509 ( \\epsilon . var-epsilon )
510 ( \\varepsilon . var-varepsilon)
511 ( \\zeta . var-zeta )
513 ( \\theta . var-theta )
514 ( \\vartheta . var-vartheta )
515 ( \\Theta . var-Theta )
516 ( \\iota . var-iota )
517 ( \\kappa . var-kappa )
518 ( \\lambda . var-lambda )
519 ( \\Lambda . var-Lambda )
525 ( \\varpi . var-varpi )
528 ( \\varrho . var-varrho )
529 ( \\sigma . var-sigma )
530 ( \\sigma . var-varsigma )
531 ( \\Sigma . var-Sigma )
533 ( \\upsilon . var-upsilon )
534 ( \\Upsilon . var-Upsilon )
536 ( \\varphi . var-varphi )
541 ( \\omega . var-omega )
542 ( \\Omega . var-Omega )
545 ( \\infty . var-inf )
546 ( \\infty . var-uinf )
547 ( \\sum . (math-parse-tex-sum calcFunc-sum) )
548 ( \\prod . (math-parse-tex-sum calcFunc-prod) )))
550 (put 'tex 'math-punc-table
555 (put 'tex 'math-complex-format 'i)
557 (put 'tex 'math-input-filter 'math-tex-input-filter)
559 (put 'tex 'math-matrix-formatter
562 (if (and (integerp calc-language-option)
563 (or (= calc-language-option 0)
564 (> calc-language-option 1)
565 (< calc-language-option -1)))
566 (append '(vleft 0 "\\matrix{")
567 (math-compose-tex-matrix (cdr a))
569 (append '(horiz "\\matrix{ ")
570 (math-compose-tex-matrix (cdr a))
573 (put 'tex 'math-var-formatter 'math-compose-tex-var)
575 (put 'tex 'math-func-formatter 'math-compose-tex-func)
577 (put 'tex 'math-dots "\\ldots")
579 (put 'tex 'math-big-parens '("\\left( " . " \\right)"))
581 (put 'tex 'math-evalto '("\\evalto " . " \\to "))
583 (defconst math-tex-ignore-words
584 '( ("\\hbox") ("\\mbox") ("\\text") ("\\left") ("\\right")
585 ("\\,") ("\\>") ("\\:") ("\\;") ("\\!") ("\\ ")
586 ("\\quad") ("\\qquad") ("\\hfil") ("\\hfill")
587 ("\\displaystyle") ("\\textstyle") ("\\dsize") ("\\tsize")
588 ("\\scriptstyle") ("\\scriptscriptstyle") ("\\ssize") ("\\sssize")
589 ("\\rm") ("\\bf") ("\\it") ("\\sl")
590 ("\\roman") ("\\bold") ("\\italic") ("\\slanted")
591 ("\\cal") ("\\mit") ("\\Cal") ("\\Bbb") ("\\frak") ("\\goth")
593 ("\\matrix" mat) ("\\bmatrix" mat) ("\\pmatrix" mat)
595 ("\\cr" punc ";") ("\\\\" punc ";") ("\\*" punc "*")
596 ("\\{" punc "[") ("\\}" punc "]")))
598 (defconst math-latex-ignore-words
599 (append math-tex-ignore-words
600 '(("\\begin" begenv))))
602 (put 'tex 'math-lang-read-symbol
604 (< math-exp-pos (1- (length math-exp-str)))
606 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
607 math-exp-str math-exp-pos)
608 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
609 math-exp-str math-exp-pos))
610 (setq math-exp-token 'symbol
611 math-exp-pos (match-end 0)
612 math-expr-data (math-restore-dashes
613 (math-match-substring math-exp-str 1)))
614 (let ((code (assoc math-expr-data math-latex-ignore-words)))
618 ((eq (nth 1 code) 'punc)
619 (setq math-exp-token 'punc
620 math-expr-data (nth 2 code)))
621 ((and (eq (nth 1 code) 'mat)
622 (string-match " *{" math-exp-str math-exp-pos))
623 (setq math-exp-pos (match-end 0)
626 (let ((right (string-match "}" math-exp-str math-exp-pos)))
628 (setq math-exp-str (copy-sequence math-exp-str))
629 (aset math-exp-str right ?\]))))))))))
631 (defun math-compose-tex-matrix (a &optional ltx)
633 (cons (append (math-compose-vector (cdr (car a)) " & " 0)
634 (if ltx '(" \\\\ ") '(" \\cr ")))
635 (math-compose-tex-matrix (cdr a) ltx))
636 (list (math-compose-vector (cdr (car a)) " & " 0))))
638 (defun math-compose-tex-sum (a fn)
641 (list 'horiz (nth 1 fn)
642 "_{" (math-compose-expr (nth 2 a) 0)
643 "=" (math-compose-expr (nth 3 a) 0)
644 "}^{" (math-compose-expr (nth 4 a) 0)
645 "}{" (math-compose-expr (nth 1 a) 0) "}"))
647 (list 'horiz (nth 1 fn)
648 "_{" (math-compose-expr (nth 2 a) 0)
649 "=" (math-compose-expr (nth 3 a) 0)
650 "}{" (math-compose-expr (nth 1 a) 0) "}"))
652 (list 'horiz (nth 1 fn)
653 "_{" (math-compose-expr (nth 2 a) 0)
654 "}{" (math-compose-expr (nth 1 a) 0) "}"))))
656 (defun math-parse-tex-sum (f val)
658 (or (equal math-expr-data "_") (throw 'syntax "Expected `_'"))
660 (setq save math-exp-old-pos)
661 (setq low (math-read-factor))
662 (or (eq (car-safe low) 'calcFunc-eq)
664 (setq math-exp-old-pos (1+ save))
665 (throw 'syntax "Expected equation")))
666 (or (equal math-expr-data "^") (throw 'syntax "Expected `^'"))
668 (setq high (math-read-factor))
669 (list (nth 2 f) (math-read-factor) (nth 1 low) (nth 2 low) high)))
671 (defun math-tex-input-filter (str) ; allow parsing of 123\,456\,789.
672 (while (string-match "[0-9]\\\\,[0-9]" str)
673 (setq str (concat (substring str 0 (1+ (match-beginning 0)))
674 (substring str (1- (match-end 0))))))
677 ;(defun math-tex-print-sqrt (a)
680 ; (math-compose-expr (nth 1 a) 0)
683 (defun math-compose-tex-intv (a)
685 (if (memq (nth 1 a) '(0 1)) "(" "[")
686 (math-compose-expr (nth 2 a) 0)
688 (math-compose-expr (nth 3 a) 0)
689 (if (memq (nth 1 a) '(0 2)) ")" "]")))
691 (defun math-compose-tex-var (a prec)
692 (if (and calc-language-option
693 (not (= calc-language-option 0))
694 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'"
695 (symbol-name (nth 1 a))))
696 (if (eq calc-language 'latex)
697 (format "\\text{%s}" (symbol-name (nth 1 a)))
698 (format "\\hbox{%s}" (symbol-name (nth 1 a))))
699 (math-compose-var a)))
701 (defun math-compose-tex-func (func a)
703 (if (and calc-language-option
704 (not (= calc-language-option 0))
705 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'" func))
706 (if (< (prefix-numeric-value calc-language-option) 0)
707 (setq func (format "\\%s" func))
708 (setq func (if (eq calc-language 'latex)
709 (format "\\text{%s}" func)
710 (format "\\hbox{%s}" func)))))
711 (cond ((or (> (length a) 2)
712 (not (math-tex-expr-is-flat (nth 1 a))))
713 (setq left "\\left( "
715 ((and (eq (aref func 0) ?\\)
717 (string-match "\\hbox{" func)
718 (string-match "\\text{" func)))
720 (or (Math-realp (nth 1 a))
721 (memq (car (nth 1 a)) '(var *))))
722 (setq left "{" right "}"))
723 (t (setq left calc-function-open
724 right calc-function-close)))
727 (math-compose-vector (cdr a) ", " 0)
730 (put 'latex 'math-oper-table
731 (append (get 'tex 'math-oper-table)
732 '(( "\\Hat" calcFunc-Hat -1 950 )
733 ( "\\Check" calcFunc-Check -1 950 )
734 ( "\\Tilde" calcFunc-Tilde -1 950 )
735 ( "\\Acute" calcFunc-Acute -1 950 )
736 ( "\\Grave" calcFunc-Grave -1 950 )
737 ( "\\Dot" calcFunc-Dot -1 950 )
738 ( "\\Ddot" calcFunc-Dotdot -1 950 )
739 ( "\\Breve" calcFunc-Breve -1 950 )
740 ( "\\Bar" calcFunc-Bar -1 950 )
741 ( "\\Vec" calcFunc-VEC -1 950 )
742 ( "\\dddot" calcFunc-dddot -1 950 )
743 ( "\\ddddot" calcFunc-ddddot -1 950 )
744 ( "\\div" / 170 171 )
745 ( "\\le" calcFunc-leq 160 161 )
746 ( "\\leqq" calcFunc-leq 160 161 )
747 ( "\\leqsland" calcFunc-leq 160 161 )
748 ( "\\ge" calcFunc-geq 160 161 )
749 ( "\\geqq" calcFunc-geq 160 161 )
750 ( "\\geqslant" calcFunc-geq 160 161 )
751 ( "=" calcFunc-eq 160 161 )
752 ( "\\neq" calcFunc-neq 160 161 )
753 ( "\\ne" calcFunc-neq 160 161 )
754 ( "\\lnot" calcFunc-lnot -1 121 )
755 ( "\\land" calcFunc-land 110 111 )
756 ( "\\lor" calcFunc-lor 100 101 )
757 ( "?" (math-read-if) 91 90 )
758 ( "!!!" calcFunc-pnot -1 85 )
759 ( "&&&" calcFunc-pand 80 81 )
760 ( "|||" calcFunc-por 75 76 )
761 ( "\\gets" calcFunc-assign 51 50 )
762 ( ":=" calcFunc-assign 51 50 )
763 ( "::" calcFunc-condition 45 46 )
764 ( "\\to" calcFunc-evalto 40 41 )
765 ( "\\to" calcFunc-evalto 40 -1 )
766 ( "=>" calcFunc-evalto 40 41 )
767 ( "=>" calcFunc-evalto 40 -1 ))))
769 (put 'latex 'math-function-table
771 (get 'tex 'math-function-table)
772 '(( \\frac . (math-latex-parse-frac))
773 ( \\tfrac . (math-latex-parse-frac))
774 ( \\dfrac . (math-latex-parse-frac))
775 ( \\binom . (math-latex-parse-two-args calcFunc-choose))
776 ( \\tbinom . (math-latex-parse-two-args calcFunc-choose))
777 ( \\dbinom . (math-latex-parse-two-args calcFunc-choose))
778 ( \\phi . calcFunc-totient )
779 ( \\mu . calcFunc-moebius ))))
781 (put 'latex 'math-special-function-table
782 '((/ . (math-compose-latex-frac "\\frac"))
783 (calcFunc-choose . (math-compose-latex-frac "\\binom"))
784 (calcFunc-sum . (math-compose-tex-sum "\\sum"))
785 (calcFunc-prod . (math-compose-tex-sum "\\prod"))
786 (intv . math-compose-tex-intv)))
788 (put 'latex 'math-variable-table
789 (get 'tex 'math-variable-table))
791 (put 'latex 'math-punc-table
796 (put 'latex 'math-complex-format 'i)
798 (put 'latex 'math-matrix-formatter
801 (if (and (integerp calc-language-option)
802 (or (= calc-language-option 0)
803 (> calc-language-option 1)
804 (< calc-language-option -1)))
805 (append '(vleft 0 "\\begin{pmatrix}")
806 (math-compose-tex-matrix (cdr a) t)
808 (append '(horiz "\\begin{pmatrix} ")
809 (math-compose-tex-matrix (cdr a) t)
810 '(" \\end{pmatrix}"))))))
812 (put 'latex 'math-var-formatter 'math-compose-tex-var)
814 (put 'latex 'math-func-formatter 'math-compose-tex-func)
816 (put 'latex 'math-dots "\\ldots")
818 (put 'latex 'math-big-parens '("\\left( " . " \\right)"))
820 (put 'latex 'math-evalto '("\\evalto " . " \\to "))
822 (put 'latex 'math-lang-read-symbol
824 (< math-exp-pos (1- (length math-exp-str)))
826 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
827 math-exp-str math-exp-pos)
828 (string-match "\\\\text *{\\([a-zA-Z0-9]+\\)}"
829 math-exp-str math-exp-pos)
830 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
831 math-exp-str math-exp-pos))
832 (setq math-exp-token 'symbol
833 math-exp-pos (match-end 0)
834 math-expr-data (math-restore-dashes
835 (math-match-substring math-exp-str 1)))
836 (let ((code (assoc math-expr-data math-tex-ignore-words))
841 ((eq (nth 1 code) 'punc)
842 (setq math-exp-token 'punc
843 math-expr-data (nth 2 code)))
844 ((and (eq (nth 1 code) 'begenv)
845 (string-match " *{\\([^}]*\\)}" math-exp-str math-exp-pos))
846 (setq math-exp-pos (match-end 0)
847 envname (match-string 1 math-exp-str)
850 (cond ((or (string= envname "matrix")
851 (string= envname "bmatrix")
852 (string= envname "smallmatrix")
853 (string= envname "pmatrix"))
854 (if (string-match (concat "\\\\end{" envname "}")
855 math-exp-str math-exp-pos)
857 (replace-match "]" t t math-exp-str))
858 (error "%s" (concat "No closing \\end{" envname "}"))))))
859 ((and (eq (nth 1 code) 'mat)
860 (string-match " *{" math-exp-str math-exp-pos))
861 (setq math-exp-pos (match-end 0)
864 (let ((right (string-match "}" math-exp-str math-exp-pos)))
866 (setq math-exp-str (copy-sequence math-exp-str))
867 (aset math-exp-str right ?\]))))))))))
869 (defun math-latex-parse-frac (f val)
871 (setq numer (car (math-read-expr-list)))
873 (setq denom (math-read-factor))
874 (if (and (Math-num-integerp numer)
875 (Math-num-integerp denom))
876 (list 'frac numer denom)
877 (list '/ numer denom))))
879 (defun math-latex-parse-two-args (f val)
881 (setq first (car (math-read-expr-list)))
883 (setq second (math-read-factor))
884 (list (nth 2 f) first second)))
886 (defun math-compose-latex-frac (a fn)
887 (list 'horiz (nth 1 fn) "{" (math-compose-expr (nth 1 a) -1)
889 (math-compose-expr (nth 2 a) -1)
892 (put 'latex 'math-input-filter 'math-tex-input-filter)
894 (defun calc-eqn-language (n)
897 (calc-set-language 'eqn)
898 (message "Eqn language mode")))
900 (put 'eqn 'math-oper-table
901 '( ( "prime" (math-parse-eqn-prime) 950 -1 )
902 ( "prime" calcFunc-Prime 950 -1 )
903 ( "dot" calcFunc-dot 950 -1 )
904 ( "dotdot" calcFunc-dotdot 950 -1 )
905 ( "hat" calcFunc-hat 950 -1 )
906 ( "tilde" calcFunc-tilde 950 -1 )
907 ( "vec" calcFunc-Vec 950 -1 )
908 ( "dyad" calcFunc-dyad 950 -1 )
909 ( "bar" calcFunc-bar 950 -1 )
910 ( "under" calcFunc-under 950 -1 )
911 ( "sub" calcFunc-subscr 931 930 )
913 ( "sqrt" calcFunc-sqrt -1 910 )
915 ( "u|" calcFunc-abs -1 0 )
917 ( "left floor" calcFunc-floor -1 0 )
918 ( "right floor" closing 0 -1 )
919 ( "left ceil" calcFunc-ceil -1 0 )
920 ( "right ceil" closing 0 -1 )
921 ( "+-" sdev 300 300 )
922 ( "!" calcFunc-fact 210 -1 )
923 ( "u+" ident -1 197 )
925 ( "times" * 191 190 )
932 ( "<" calcFunc-lt 160 161 )
933 ( ">" calcFunc-gt 160 161 )
934 ( "<=" calcFunc-leq 160 161 )
935 ( ">=" calcFunc-geq 160 161 )
936 ( "=" calcFunc-eq 160 161 )
937 ( "==" calcFunc-eq 160 161 )
938 ( "!=" calcFunc-neq 160 161 )
939 ( "u!" calcFunc-lnot -1 121 )
940 ( "&&" calcFunc-land 110 111 )
941 ( "||" calcFunc-lor 100 101 )
942 ( "?" (math-read-if) 91 90 )
943 ( "!!!" calcFunc-pnot -1 85 )
944 ( "&&&" calcFunc-pand 80 81 )
945 ( "|||" calcFunc-por 75 76 )
946 ( "<-" calcFunc-assign 51 50 )
947 ( ":=" calcFunc-assign 51 50 )
948 ( "::" calcFunc-condition 45 46 )
949 ( "->" calcFunc-evalto 40 41 )
950 ( "->" calcFunc-evalto 40 -1 )
951 ( "=>" calcFunc-evalto 40 41 )
952 ( "=>" calcFunc-evalto 40 -1 )))
954 (put 'eqn 'math-function-table
955 '( ( arc\ cos . calcFunc-arccos )
956 ( arc\ cosh . calcFunc-arccosh )
957 ( arc\ sin . calcFunc-arcsin )
958 ( arc\ sinh . calcFunc-arcsinh )
959 ( arc\ tan . calcFunc-arctan )
960 ( arc\ tanh . calcFunc-arctanh )
961 ( GAMMA . calcFunc-gamma )
962 ( phi . calcFunc-totient )
963 ( mu . calcFunc-moebius )
964 ( matrix . (math-parse-eqn-matrix) )))
966 (put 'eqn 'math-special-function-table
967 '((intv . math-compose-eqn-intv)))
969 (put 'eqn 'math-punc-table
973 (put 'eqn 'math-variable-table
974 '( ( inf . var-uinf )))
976 (put 'eqn 'math-complex-format 'i)
978 (put 'eqn 'math-big-parens '("{left ( " . " right )}"))
980 (put 'eqn 'math-evalto '("evalto " . " -> "))
982 (put 'eqn 'math-matrix-formatter
985 (append '(horiz "matrix { ")
986 (math-compose-eqn-matrix
987 (cdr (math-transpose a)))
990 (put 'eqn 'math-var-formatter
994 (if (and math-compose-hash-args
995 (let ((p calc-arg-values))
997 (while (and p (not (equal (car p) a)))
998 (setq p (and (eq math-compose-hash-args t) (cdr p))
1001 (if (eq math-compose-hash-args 1)
1004 (if (string-match ".'\\'" (symbol-name (nth 2 a)))
1006 (list 'calcFunc-Prime
1009 (intern (substring (symbol-name (nth 1 a)) 0 -1))
1010 (intern (substring (symbol-name (nth 2 a)) 0 -1))))
1012 (symbol-name (nth 1 a))))))))
1014 (defconst math-eqn-special-funcs
1016 calcFunc-ln calcFunc-exp
1017 calcFunc-sin calcFunc-cos calcFunc-tan
1018 calcFunc-sec calcFunc-csc calcFunc-cot
1019 calcFunc-sinh calcFunc-cosh calcFunc-tanh
1020 calcFunc-sech calcFunc-csch calcFunc-coth
1021 calcFunc-arcsin calcFunc-arccos calcFunc-arctan
1022 calcFunc-arcsinh calcFunc-arccosh calcFunc-arctanh))
1024 (put 'eqn 'math-func-formatter
1028 (if (string-match "[^']'+\\'" func)
1029 (let ((n (- (length func) (match-beginning 0) 1)))
1030 (setq func (substring func 0 (- n)))
1031 (while (>= (setq n (1- n)) 0)
1032 (setq func (concat func " prime")))))
1033 (cond ((or (> (length a) 2)
1034 (not (math-tex-expr-is-flat (nth 1 a))))
1035 (setq left "{left ( "
1039 (memq (car a) math-eqn-special-funcs)
1041 (or (Math-realp (nth 1 a))
1042 (memq (car (nth 1 a)) '(var *))))
1043 (setq left "~{" right "}"))
1047 (list 'horiz func left
1048 (math-compose-vector (cdr a) " , " 0)
1051 (put 'eqn 'math-lang-read-symbol
1053 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)"
1054 math-exp-str math-exp-pos)
1056 (setq math-exp-str (copy-sequence math-exp-str))
1057 (aset math-exp-str (match-beginning 1) ?\{)
1058 (if (< (match-end 1) (length math-exp-str))
1059 (aset math-exp-str (match-end 1) ?\}))
1060 (math-read-token)))))
1062 (defconst math-eqn-ignore-words
1063 '( ("roman") ("bold") ("italic") ("mark") ("lineup") ("evalto")
1064 ("left" ("floor") ("ceil"))
1065 ("right" ("floor") ("ceil"))
1066 ("arc" ("sin") ("cos") ("tan") ("sinh") ("cosh") ("tanh"))
1067 ("size" n) ("font" n) ("fwd" n) ("back" n) ("up" n) ("down" n)
1068 ("above" punc ",")))
1070 (put 'eqn 'math-lang-adjust-words
1073 (let ((code (assoc math-expr-data math-eqn-ignore-words)))
1077 ((consp (nth 1 code))
1079 (if (assoc math-expr-data (cdr code))
1080 (setq math-expr-data (format "%s %s"
1081 (car code) math-expr-data))))
1082 ((eq (nth 1 code) 'punc)
1083 (setq math-exp-token 'punc
1084 math-expr-data (nth 2 code)))
1087 (math-read-token)))))))
1089 (put 'eqn 'math-lang-read
1090 '((eq (string-match "->\\|<-\\|+-\\|\\\\dots\\|~\\|\\^"
1091 math-exp-str math-exp-pos)
1094 (setq math-exp-token 'punc
1095 math-expr-data (math-match-substring math-exp-str 0)
1096 math-exp-pos (match-end 0))
1097 (and (eq (string-match "\\\\dots\\." math-exp-str math-exp-pos)
1099 (setq math-exp-pos (match-end 0)))
1100 (if (memq (aref math-expr-data 0) '(?~ ?^))
1101 (math-read-token)))))
1104 (defun math-compose-eqn-matrix (a)
1107 (cond ((eq calc-matrix-just 'right) "rcol ")
1108 ((eq calc-matrix-just 'center) "ccol ")
1111 (list 'break math-compose-level)
1115 (let ((math-compose-level (1+ math-compose-level)))
1116 (math-compose-vector (cdr (car a)) " above " 1000))
1119 (math-compose-eqn-matrix (cdr a)))))))
1122 (defun math-parse-eqn-matrix (f sym)
1124 (while (assoc math-expr-data '(("ccol") ("lcol") ("rcol")))
1126 (or (equal math-expr-data calc-function-open)
1127 (throw 'syntax "Expected `{'"))
1129 (setq vec (cons (cons 'vec (math-read-expr-list)) vec))
1130 (or (equal math-expr-data calc-function-close)
1131 (throw 'syntax "Expected `}'"))
1133 (or (equal math-expr-data calc-function-close)
1134 (throw 'syntax "Expected `}'"))
1136 (math-transpose (cons 'vec (nreverse vec)))))
1138 (defun math-parse-eqn-prime (x sym)
1139 (if (eq (car-safe x) 'var)
1140 (if (equal math-expr-data calc-function-open)
1143 (let ((args (if (or (equal math-expr-data calc-function-close)
1144 (eq math-exp-token 'end))
1146 (math-read-expr-list))))
1147 (if (not (or (equal math-expr-data calc-function-close)
1148 (eq math-exp-token 'end)))
1149 (throw 'syntax "Expected `)'"))
1151 (cons (intern (format "calcFunc-%s'" (nth 1 x))) args)))
1153 (intern (concat (symbol-name (nth 1 x)) "'"))
1154 (intern (concat (symbol-name (nth 2 x)) "'"))))
1155 (list 'calcFunc-Prime x)))
1157 (defun math-compose-eqn-intv (a)
1159 (if (memq (nth 1 a) '(0 1)) "(" "[")
1160 (math-compose-expr (nth 2 a) 0)
1162 (math-compose-expr (nth 3 a) 0)
1163 (if (memq (nth 1 a) '(0 2)) ")" "]")))
1166 (defun calc-mathematica-language ()
1169 (calc-set-language 'math)
1170 (message "Mathematica language mode")))
1172 (put 'math 'math-oper-table
1173 '( ( "[[" (math-read-math-subscr) 250 -1 )
1174 ( "!" calcFunc-fact 210 -1 )
1175 ( "!!" calcFunc-dfact 210 -1 )
1177 ( "u+" ident -1 197 )
1184 ( "<" calcFunc-lt 160 161 )
1185 ( ">" calcFunc-gt 160 161 )
1186 ( "<=" calcFunc-leq 160 161 )
1187 ( ">=" calcFunc-geq 160 161 )
1188 ( "==" calcFunc-eq 150 151 )
1189 ( "!=" calcFunc-neq 150 151 )
1190 ( "u!" calcFunc-lnot -1 121 )
1191 ( "&&" calcFunc-land 110 111 )
1192 ( "||" calcFunc-lor 100 101 )
1193 ( "!!!" calcFunc-pnot -1 85 )
1194 ( "&&&" calcFunc-pand 80 81 )
1195 ( "|||" calcFunc-por 75 76 )
1196 ( ":=" calcFunc-assign 51 50 )
1197 ( "=" calcFunc-assign 51 50 )
1198 ( "->" calcFunc-assign 51 50 )
1199 ( ":>" calcFunc-assign 51 50 )
1200 ( "::" calcFunc-condition 45 46 )
1203 (put 'math 'math-function-table
1204 '( ( Abs . calcFunc-abs )
1205 ( ArcCos . calcFunc-arccos )
1206 ( ArcCosh . calcFunc-arccosh )
1207 ( ArcSin . calcFunc-arcsin )
1208 ( ArcSinh . calcFunc-arcsinh )
1209 ( ArcTan . calcFunc-arctan )
1210 ( ArcTanh . calcFunc-arctanh )
1211 ( Arg . calcFunc-arg )
1212 ( Binomial . calcFunc-choose )
1213 ( Ceiling . calcFunc-ceil )
1214 ( Conjugate . calcFunc-conj )
1215 ( Cos . calcFunc-cos )
1216 ( Cosh . calcFunc-cosh )
1217 ( Cot . calcFunc-cot )
1218 ( Coth . calcFunc-coth )
1219 ( Csc . calcFunc-csc )
1220 ( Csch . calcFunc-csch )
1221 ( D . calcFunc-deriv )
1222 ( Dt . calcFunc-tderiv )
1223 ( Det . calcFunc-det )
1224 ( Exp . calcFunc-exp )
1225 ( EulerPhi . calcFunc-totient )
1226 ( Floor . calcFunc-floor )
1227 ( Gamma . calcFunc-gamma )
1228 ( GCD . calcFunc-gcd )
1229 ( If . calcFunc-if )
1230 ( Im . calcFunc-im )
1231 ( Inverse . calcFunc-inv )
1232 ( Integrate . calcFunc-integ )
1233 ( Join . calcFunc-vconcat )
1234 ( LCM . calcFunc-lcm )
1235 ( Log . calcFunc-ln )
1236 ( Max . calcFunc-max )
1237 ( Min . calcFunc-min )
1238 ( Mod . calcFunc-mod )
1239 ( MoebiusMu . calcFunc-moebius )
1240 ( Random . calcFunc-random )
1241 ( Round . calcFunc-round )
1242 ( Re . calcFunc-re )
1243 ( Sec . calcFunc-sec )
1244 ( Sech . calcFunc-sech )
1245 ( Sign . calcFunc-sign )
1246 ( Sin . calcFunc-sin )
1247 ( Sinh . calcFunc-sinh )
1248 ( Sqrt . calcFunc-sqrt )
1249 ( Tan . calcFunc-tan )
1250 ( Tanh . calcFunc-tanh )
1251 ( Transpose . calcFunc-trn )
1252 ( Length . calcFunc-vlen )
1255 (put 'math 'math-variable-table
1259 ( GoldenRatio . var-phi )
1260 ( EulerGamma . var-gamma )
1261 ( Infinity . var-inf )
1262 ( ComplexInfinity . var-uinf )
1263 ( Indeterminate . var-nan )
1266 (put 'math 'math-vector-brackets "{}")
1267 (put 'math 'math-complex-format 'I)
1268 (put 'math 'math-function-open "[")
1269 (put 'math 'math-function-close "]")
1271 (put 'math 'math-radix-formatter
1272 (function (lambda (r s) (format "%d^^%s" r s))))
1274 (put 'math 'math-lang-read
1275 '((eq (string-match "\\[\\[\\|->\\|:>" math-exp-str math-exp-pos)
1277 (setq math-exp-token 'punc
1278 math-expr-data (math-match-substring math-exp-str 0)
1279 math-exp-pos (match-end 0))))
1281 (put 'math 'math-compose-subscr
1285 (math-compose-expr (nth 1 a) 1000)
1287 (math-compose-expr (nth 2 a) 0)
1290 (defun math-read-math-subscr (x op)
1291 (let ((idx (math-read-expr-level 0)))
1292 (or (and (equal math-expr-data "]")
1295 (equal math-expr-data "]")))
1296 (throw 'syntax "Expected ']]'"))
1298 (list 'calcFunc-subscr x idx)))
1301 (defun calc-maple-language ()
1304 (calc-set-language 'maple)
1305 (message "Maple language mode")))
1307 (put 'maple 'math-oper-table
1308 '( ( "matrix" ident -1 300 )
1309 ( "MATRIX" ident -1 300 )
1310 ( "!" calcFunc-fact 210 -1 )
1313 ( "u+" ident -1 197 )
1317 ( "intersect" calcFunc-vint 191 192 )
1320 ( "union" calcFunc-vunion 180 181 )
1321 ( "minus" calcFunc-vdiff 180 181 )
1323 ( ".." (math-read-maple-dots) 165 165 )
1324 ( "\\dots" (math-read-maple-dots) 165 165 )
1325 ( "<" calcFunc-lt 160 160 )
1326 ( ">" calcFunc-gt 160 160 )
1327 ( "<=" calcFunc-leq 160 160 )
1328 ( ">=" calcFunc-geq 160 160 )
1329 ( "=" calcFunc-eq 160 160 )
1330 ( "<>" calcFunc-neq 160 160 )
1331 ( "not" calcFunc-lnot -1 121 )
1332 ( "and" calcFunc-land 110 111 )
1333 ( "or" calcFunc-lor 100 101 )
1334 ( "!!!" calcFunc-pnot -1 85 )
1335 ( "&&&" calcFunc-pand 80 81 )
1336 ( "|||" calcFunc-por 75 76 )
1337 ( ":=" calcFunc-assign 51 50 )
1338 ( "::" calcFunc-condition 45 46 )
1341 (put 'maple 'math-function-table
1342 '( ( bernoulli . calcFunc-bern )
1343 ( binomial . calcFunc-choose )
1344 ( diff . calcFunc-deriv )
1345 ( GAMMA . calcFunc-gamma )
1346 ( ifactor . calcFunc-prfac )
1347 ( igcd . calcFunc-gcd )
1348 ( ilcm . calcFunc-lcm )
1349 ( int . calcFunc-integ )
1352 ( iquo . calcFunc-idiv )
1353 ( isprime . calcFunc-prime )
1354 ( length . calcFunc-vlen )
1355 ( member . calcFunc-in )
1356 ( crossprod . calcFunc-cross )
1357 ( inverse . calcFunc-inv )
1358 ( trace . calcFunc-tr )
1359 ( transpose . calcFunc-trn )
1360 ( vectdim . calcFunc-vlen )
1363 (put 'maple 'math-special-function-table
1364 '((intv . math-compose-maple-intv)))
1366 (put 'maple 'math-variable-table
1370 ( infinity . var-inf )
1371 ( infinity . var-uinf )
1372 ( infinity . var-nan )
1375 (put 'maple 'math-complex-format 'I)
1377 (put 'maple 'math-matrix-formatter
1382 math-comp-left-bracket
1383 (math-compose-vector (cdr a)
1384 (concat math-comp-comma " ")
1385 math-comp-vector-prec)
1386 math-comp-right-bracket
1389 (put 'maple 'math-compose-subscr
1392 (let ((args (cdr (cdr a))))
1394 (math-compose-expr (nth 1 a) 1000)
1396 (math-compose-vector args ", " 0)
1399 (add-to-list 'calc-lang-allow-underscores 'maple)
1400 (add-to-list 'calc-lang-brackets-are-subscripts 'maple)
1402 (defun math-compose-maple-intv (a)
1404 (math-compose-expr (nth 2 a) 0)
1406 (math-compose-expr (nth 3 a) 0)))
1408 (defun math-read-maple-dots (x op)
1409 (list 'intv 3 x (math-read-expr-level (nth 3 op))))
1412 ;; The variable math-read-big-lines is local to math-read-big-expr in
1413 ;; calc-ext.el, but is used by math-read-big-rec, math-read-big-char,
1414 ;; math-read-big-emptyp, math-read-big-error and math-read-big-balance,
1415 ;; which are called (directly and indirectly) by math-read-big-expr.
1416 ;; It is also local to math-read-big-bigp in calc-ext.el, which calls
1417 ;; math-read-big-balance.
1418 (defvar math-read-big-lines)
1420 ;; The variables math-read-big-baseline and math-read-big-h2 are
1421 ;; local to math-read-big-expr in calc-ext.el, but used by
1422 ;; math-read-big-rec.
1423 (defvar math-read-big-baseline)
1424 (defvar math-read-big-h2)
1426 ;; The variables math-rb-h1, math-rb-h2, math-rb-v1 and math-rb-v2
1427 ;; are local to math-read-big-rec, but are used by math-read-big-char,
1428 ;; math-read-big-emptyp and math-read-big-balance which are called by
1429 ;; math-read-big-rec.
1430 ;; math-rb-h2 is also local to math-read-big-bigp in calc-ext.el,
1431 ;; which calls math-read-big-balance.
1437 (defun math-read-big-rec (math-rb-h1 math-rb-v1 math-rb-h2 math-rb-v2
1438 &optional baseline prec short)
1439 (or prec (setq prec 0))
1441 ;; Clip whitespace above or below.
1442 (while (and (< math-rb-v1 math-rb-v2)
1443 (math-read-big-emptyp math-rb-h1 math-rb-v1 math-rb-h2 (1+ math-rb-v1)))
1444 (setq math-rb-v1 (1+ math-rb-v1)))
1445 (while (and (< math-rb-v1 math-rb-v2)
1446 (math-read-big-emptyp math-rb-h1 (1- math-rb-v2) math-rb-h2 math-rb-v2))
1447 (setq math-rb-v2 (1- math-rb-v2)))
1449 ;; If formula is a single line high, normal parser can handle it.
1450 (if (<= math-rb-v2 (1+ math-rb-v1))
1451 (if (or (<= math-rb-v2 math-rb-v1)
1452 (> math-rb-h1 (length (setq math-rb-v2
1453 (nth math-rb-v1 math-read-big-lines)))))
1454 (math-read-big-error math-rb-h1 math-rb-v1)
1455 (setq math-read-big-baseline math-rb-v1
1456 math-read-big-h2 math-rb-h2
1457 math-rb-v2 (nth math-rb-v1 math-read-big-lines)
1458 math-rb-h2 (math-read-expr
1459 (substring math-rb-v2 math-rb-h1
1460 (min math-rb-h2 (length math-rb-v2)))))
1461 (if (eq (car-safe math-rb-h2) 'error)
1462 (math-read-big-error (+ math-rb-h1 (nth 1 math-rb-h2))
1463 math-rb-v1 (nth 2 math-rb-h2))
1466 ;; Clip whitespace at left or right.
1467 (while (and (< math-rb-h1 math-rb-h2)
1468 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) math-rb-v2))
1469 (setq math-rb-h1 (1+ math-rb-h1)))
1470 (while (and (< math-rb-h1 math-rb-h2)
1471 (math-read-big-emptyp (1- math-rb-h2) math-rb-v1 math-rb-h2 math-rb-v2))
1472 (setq math-rb-h2 (1- math-rb-h2)))
1474 ;; Scan to find widest left-justified "----" in the region.
1477 (lines-v1 (nthcdr math-rb-v1 math-read-big-lines))
1481 other-char line len h)
1482 (while (< v math-rb-v2)
1484 len (min math-rb-h2 (length line)))
1485 (and (< math-rb-h1 len)
1486 (/= (aref line math-rb-h1) ?\ )
1487 (if (and (= (aref line math-rb-h1) ?\-)
1488 ;; Make sure it's not a minus sign.
1489 (or (and (< (1+ math-rb-h1) len)
1490 (= (aref line (1+ math-rb-h1)) ?\-))
1491 (/= (math-read-big-char math-rb-h1 (1- v)) ?\ )
1492 (/= (math-read-big-char math-rb-h1 (1+ v)) ?\ )))
1495 (while (and (< (setq h (1+ h)) len)
1496 (= (aref line h) ?\-)))
1500 (or other-v (setq other-v v other-char (aref line math-rb-h1)))))
1504 (cond ((not (setq v other-v))
1505 (math-read-big-error math-rb-h1 math-rb-v1)) ; Should never happen!
1511 (let ((num (math-read-big-rec math-rb-h1 math-rb-v1 h v))
1512 (den (math-read-big-rec math-rb-h1 (1+ v) h math-rb-v2)))
1513 (setq p (if (and (math-integerp num) (math-integerp den))
1514 (math-make-frac num den)
1515 (list '/ num den)))))
1517 ;; Big radical sign.
1519 (or (= (math-read-big-char (1+ math-rb-h1) v) ?\|)
1520 (math-read-big-error (1+ math-rb-h1) v "Malformed root sign"))
1521 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1522 (while (= (math-read-big-char (1+ math-rb-h1) (setq v (1- v))) ?\|))
1523 (or (= (math-read-big-char (setq h (+ math-rb-h1 2)) v) ?\_)
1524 (math-read-big-error h v "Malformed root sign"))
1525 (while (= (math-read-big-char (setq h (1+ h)) v) ?\_))
1526 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1527 (math-read-big-emptyp math-rb-h1 (1+ other-v) h math-rb-v2 nil t)
1528 (setq p (list 'calcFunc-sqrt (math-read-big-rec
1529 (+ math-rb-h1 2) (1+ v)
1530 h (1+ other-v) baseline))
1531 v math-read-big-baseline))
1533 ;; Small radical sign.
1534 ((and (= other-char ?V)
1535 (= (math-read-big-char (1+ math-rb-h1) (1- v)) ?\_))
1536 (setq h (1+ math-rb-h1))
1537 (math-read-big-emptyp math-rb-h1 math-rb-v1 h (1- v) nil t)
1538 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)
1539 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1540 (while (= (math-read-big-char (setq h (1+ h)) (1- v)) ?\_))
1541 (setq p (list 'calcFunc-sqrt (math-read-big-rec
1542 (1+ math-rb-h1) v h (1+ v) t))
1543 v math-read-big-baseline))
1545 ;; Binomial coefficient.
1546 ((and (= other-char ?\()
1547 (= (math-read-big-char (1+ math-rb-h1) v) ?\ )
1548 (= (string-match "( *)" (nth v math-read-big-lines)
1549 math-rb-h1) math-rb-h1))
1550 (setq h (match-end 0))
1551 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1552 (math-read-big-emptyp math-rb-h1 (1+ v) (1+ math-rb-h1) math-rb-v2 nil t)
1553 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
1554 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
1555 (setq p (list 'calcFunc-choose
1556 (math-read-big-rec (1+ math-rb-h1) math-rb-v1 (1- h) v)
1557 (math-read-big-rec (1+ math-rb-h1) (1+ v)
1558 (1- h) math-rb-v2))))
1562 (setq p (list 'neg (math-read-big-rec (1+ math-rb-h1) math-rb-v1
1563 math-rb-h2 math-rb-v2 v 250 t))
1564 v math-read-big-baseline
1565 h math-read-big-h2))
1569 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1570 (math-read-big-emptyp math-rb-h1 (1+ v) (1+ math-rb-h1) math-rb-v2 nil t)
1571 (setq h (math-read-big-balance (1+ math-rb-h1) v "(" t))
1572 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
1573 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
1574 (let ((sep (math-read-big-char (1- h) v))
1579 (math-read-big-error (1- h) v "Expected `)'"))
1581 (setq p (math-read-big-rec
1582 (1+ math-rb-h1) math-rb-v1 (1- h) math-rb-v2 v))
1583 (setq hmid (math-read-big-balance h v "(")
1585 (math-read-big-rec h math-rb-v1 (1- hmid) math-rb-v2 v))
1588 (setq p (cons 'intv (cons (if (= (math-read-big-char
1593 ((= (math-read-big-char (1- h) v) ?\])
1594 (math-read-big-error (1- h) v "Expected `)'"))
1596 (or (and (math-realp (car p)) (math-realp (nth 1 p)))
1597 (math-read-big-error
1598 math-rb-h1 v "Complex components must be real"))
1599 (setq p (cons 'cplx p)))
1601 (or (and (math-realp (car p)) (math-anglep (nth 1 p)))
1602 (math-read-big-error
1603 math-rb-h1 v "Complex components must be real"))
1604 (setq p (cons 'polar p)))))))
1607 ((and (= other-char ?\[)
1608 (or (= (math-read-big-char (setq h math-rb-h1) (1+ v)) ?\[)
1609 (= (math-read-big-char (setq h (1+ h)) v) ?\[)
1610 (and (= (math-read-big-char h v) ?\ )
1611 (= (math-read-big-char (setq h (1+ h)) v) ?\[)))
1612 (= (math-read-big-char h (1+ v)) ?\[))
1613 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
1619 (setq h (math-read-big-balance (1+ hleft) v "["))
1622 (math-read-big-error hright v "Expected `]'"))
1624 (setq p (cons (math-read-big-rec
1625 hleft v h (1+ v)) p))
1626 (and (memq (math-read-big-char h v) '(?\ ?\,))
1627 (= (math-read-big-char hleft (1+ v)) ?\[)))
1629 (or (= hleft math-rb-h1)
1631 (if (= (math-read-big-char h v) ?\ )
1633 (and (= (math-read-big-char h v) ?\])
1635 (math-read-big-error (1- h) v "Expected `]'"))
1636 (if (= (math-read-big-char h vtop) ?\,)
1638 (math-read-big-emptyp math-rb-h1 (1+ v) (1- h) math-rb-v2 nil t)
1639 (setq v (+ vtop (/ (- v vtop) 2))
1640 p (cons 'vec (nreverse p)))))
1644 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1645 (math-read-big-emptyp math-rb-h1 (1+ v) (1+ math-rb-h1) math-rb-v2 nil t)
1649 (setq widest (math-read-big-balance h v "[" t))
1650 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
1651 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
1652 (setq p (cons (math-read-big-rec
1653 h math-rb-v1 (1- widest) math-rb-v2 v) p)
1655 (= (math-read-big-char (1- h) v) ?\,)))
1656 (setq widest (math-read-big-char (1- h) v))
1657 (if (or (memq widest '(?\; ?\)))
1658 (and (eq widest ?\.) (cdr p)))
1659 (math-read-big-error (1- h) v "Expected `]'"))
1662 widest (math-read-big-balance h v "[")
1663 p (nconc p (list (math-read-big-rec
1664 h math-rb-v1 (1- widest) math-rb-v2 v)))
1666 p (cons 'intv (cons (if (= (math-read-big-char (1- h) v)
1670 (setq p (cons 'vec (nreverse p)))))
1674 (setq line (nth v math-read-big-lines))
1675 (string-match ">" line math-rb-h1)
1676 (setq h (match-end 0))
1677 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
1678 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)
1679 (setq p (math-read-big-rec math-rb-h1 v h (1+ v) v)))
1681 ;; Variable name or function call.
1682 ((or (and (>= other-char ?a) (<= other-char ?z))
1683 (and (>= other-char ?A) (<= other-char ?Z)))
1684 (setq line (nth v math-read-big-lines))
1685 (string-match "\\([a-zA-Z'_]+\\) *" line math-rb-h1)
1686 (setq h (match-end 1)
1687 widest (match-end 0)
1688 p (math-match-substring line 1))
1689 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
1690 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)
1691 (if (= (math-read-big-char widest v) ?\()
1693 (setq line (if (string-match "-" p)
1695 (intern (concat "calcFunc-" p)))
1698 (math-read-big-emptyp widest math-rb-v1 h v nil t)
1699 (math-read-big-emptyp widest (1+ v) h math-rb-v2 nil t)
1701 (setq widest (math-read-big-balance h v "(" t))
1702 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
1703 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
1704 (setq p (cons (math-read-big-rec
1705 h math-rb-v1 (1- widest) math-rb-v2 v) p)
1707 (= (math-read-big-char (1- h) v) ?\,)))
1708 (or (= (math-read-big-char (1- h) v) ?\))
1709 (math-read-big-error (1- h) v "Expected `)'"))
1710 (setq p (cons line (nreverse p))))
1712 (intern (math-remove-dashes p))
1713 (if (string-match "-" p)
1715 (intern (concat "var-" p)))))))
1719 (setq line (nth v math-read-big-lines))
1720 (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)
1721 (math-read-big-error h v "Expected a number"))
1722 (setq h (match-end 0)
1723 p (math-read-number (math-match-substring line 0)))
1724 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
1725 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)))
1727 ;; Now left term is bounded by math-rb-h1, math-rb-v1, h, math-rb-v2;
1731 (math-read-big-error math-rb-h1 v "Inconsistent baseline in formula"))
1734 ;; Look for superscripts or subscripts.
1735 (setq line (nth baseline math-read-big-lines)
1736 len (min math-rb-h2 (length line))
1738 (while (and (< widest len)
1739 (= (aref line widest) ?\ ))
1740 (setq widest (1+ widest)))
1741 (and (>= widest len) (setq widest math-rb-h2))
1742 (if (math-read-big-emptyp h v widest math-rb-v2)
1743 (if (math-read-big-emptyp h math-rb-v1 widest v)
1745 (setq p (list '^ p (math-read-big-rec h math-rb-v1 widest v))
1747 (if (math-read-big-emptyp h math-rb-v1 widest v)
1748 (setq p (list 'calcFunc-subscr p
1749 (math-read-big-rec h v widest math-rb-v2))
1752 ;; Look for an operator name and grab additional terms.
1753 (while (and (< h len)
1754 (if (setq widest (and (math-read-big-emptyp
1755 h math-rb-v1 (1+ h) v)
1756 (math-read-big-emptyp
1757 h (1+ v) (1+ h) math-rb-v2)
1758 (string-match "<=\\|>=\\|\\+/-\\|!=\\|&&\\|||\\|:=\\|=>\\|." line h)
1759 (assoc (math-match-substring line 0)
1760 (math-standard-ops))))
1761 (and (>= (nth 2 widest) prec)
1762 (setq h (match-end 0)))
1763 (and (not (eq (string-match ",\\|;\\|\\.\\.\\|)\\|\\]\\|:" line h)
1765 (setq widest '("2x" * 196 195)))))
1766 (cond ((eq (nth 3 widest) -1)
1767 (setq p (list (nth 1 widest) p)))
1768 ((equal (car widest) "?")
1769 (let ((y (math-read-big-rec h math-rb-v1 math-rb-h2
1770 math-rb-v2 baseline nil t)))
1771 (or (= (math-read-big-char math-read-big-h2 baseline) ?\:)
1772 (math-read-big-error math-read-big-h2 baseline "Expected `:'"))
1773 (setq p (list (nth 1 widest) p y
1775 (1+ math-read-big-h2) math-rb-v1 math-rb-h2 math-rb-v2
1776 baseline (nth 3 widest) t))
1777 h math-read-big-h2)))
1779 (setq p (list (nth 1 widest) p
1780 (math-read-big-rec h math-rb-v1 math-rb-h2 math-rb-v2
1781 baseline (nth 3 widest) t))
1782 h math-read-big-h2))))
1784 ;; Return all relevant information to caller.
1785 (setq math-read-big-baseline baseline
1787 (or short (= math-read-big-h2 math-rb-h2)
1788 (math-read-big-error h baseline))
1791 (defun math-read-big-char (h v)
1792 (or (and (>= h math-rb-h1)
1796 (let ((line (nth v math-read-big-lines)))
1802 (defun math-read-big-emptyp (eh1 ev1 eh2 ev2 &optional what error)
1803 (and (< ev1 math-rb-v1) (setq ev1 math-rb-v1))
1804 (and (< eh1 math-rb-h1) (setq eh1 math-rb-h1))
1805 (and (> ev2 math-rb-v2) (setq ev2 math-rb-v2))
1806 (and (> eh2 math-rb-h2) (setq eh2 math-rb-h2))
1807 (or what (setq what ?\ ))
1808 (let ((p (nthcdr ev1 math-read-big-lines))
1810 (while (and (< ev1 ev2)
1812 (setq h (min eh2 (length (car p))))
1813 (while (and (>= (setq h (1- h)) eh1)
1814 (= (aref (car p) h) what)))
1815 (and error (>= h eh1)
1816 (math-read-big-error h ev1 (if (stringp error)
1818 "Whitespace expected")))
1824 ;; math-read-big-err-msg is local to math-read-big-expr in calc-ext.el,
1825 ;; but is used by math-read-big-error which is called (indirectly) by
1826 ;; math-read-big-expr.
1827 (defvar math-read-big-err-msg)
1829 (defun math-read-big-error (h v &optional msg)
1831 (p math-read-big-lines))
1833 (setq pos (+ pos 1 (length (car p)))
1836 (setq h (+ pos (min h (length (car p))))
1837 math-read-big-err-msg (list 'error h (or msg "Syntax error")))
1838 (throw 'syntax nil)))
1840 (defun math-read-big-balance (h v what &optional commas)
1841 (let* ((line (nth v math-read-big-lines))
1842 (len (min math-rb-h2 (length line)))
1847 (math-read-big-error nil v (format "Unmatched `%s'" what))
1849 (if (memq (aref line h) '(?\( ?\[))
1850 (setq count (1+ count))
1851 (if (if (and commas (= count 1))
1852 (or (memq (aref line h) '(?\) ?\] ?\, ?\;))
1853 (and (eq (aref line h) ?\.)
1855 (eq (aref line (1+ h)) ?\.)))
1856 (memq (aref line h) '(?\) ?\])))
1857 (setq count (1- count))))
1861 (provide 'calc-lang)
1863 ;;; arch-tag: 483bfe15-f290-4fef-bb7d-ce65be687f2e
1864 ;;; calc-lang.el ends here