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 v))
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-oper-table
412 '( ( "\\hat" calcFunc-hat -1 950 )
413 ( "\\check" calcFunc-check -1 950 )
414 ( "\\tilde" calcFunc-tilde -1 950 )
415 ( "\\acute" calcFunc-acute -1 950 )
416 ( "\\grave" calcFunc-grave -1 950 )
417 ( "\\dot" calcFunc-dot -1 950 )
418 ( "\\ddot" calcFunc-dotdot -1 950 )
419 ( "\\breve" calcFunc-breve -1 950 )
420 ( "\\bar" calcFunc-bar -1 950 )
421 ( "\\vec" calcFunc-Vec -1 950 )
422 ( "\\underline" calcFunc-under -1 950 )
423 ( "u|" calcFunc-abs -1 0 )
425 ( "\\lfloor" calcFunc-floor -1 0 )
426 ( "\\rfloor" closing 0 -1 )
427 ( "\\lceil" calcFunc-ceil -1 0 )
428 ( "\\rceil" closing 0 -1 )
429 ( "\\pm" sdev 300 300 )
430 ( "!" calcFunc-fact 210 -1 )
432 ( "_" calcFunc-subscr 201 200 )
433 ( "u+" ident -1 197 )
435 ( "\\times" * 191 190 )
440 ( "\\over" / 170 171 )
442 ( "\\choose" calcFunc-choose 170 171 )
443 ( "\\mod" % 170 171 )
444 ( "<" calcFunc-lt 160 161 )
445 ( ">" calcFunc-gt 160 161 )
446 ( "\\leq" calcFunc-leq 160 161 )
447 ( "\\geq" calcFunc-geq 160 161 )
448 ( "=" calcFunc-eq 160 161 )
449 ( "\\neq" calcFunc-neq 160 161 )
450 ( "\\ne" calcFunc-neq 160 161 )
451 ( "\\lnot" calcFunc-lnot -1 121 )
452 ( "\\land" calcFunc-land 110 111 )
453 ( "\\lor" calcFunc-lor 100 101 )
454 ( "?" (math-read-if) 91 90 )
455 ( "!!!" calcFunc-pnot -1 85 )
456 ( "&&&" calcFunc-pand 80 81 )
457 ( "|||" calcFunc-por 75 76 )
458 ( "\\gets" calcFunc-assign 51 50 )
459 ( ":=" calcFunc-assign 51 50 )
460 ( "::" calcFunc-condition 45 46 )
461 ( "\\to" calcFunc-evalto 40 41 )
462 ( "\\to" calcFunc-evalto 40 -1 )
463 ( "=>" calcFunc-evalto 40 41 )
464 ( "=>" calcFunc-evalto 40 -1 )))
466 (put 'tex 'math-function-table
467 '( ( \\arccos . calcFunc-arccos )
468 ( \\arcsin . calcFunc-arcsin )
469 ( \\arctan . calcFunc-arctan )
470 ( \\arg . calcFunc-arg )
471 ( \\cos . calcFunc-cos )
472 ( \\cosh . calcFunc-cosh )
473 ( \\cot . calcFunc-cot )
474 ( \\coth . calcFunc-coth )
475 ( \\csc . calcFunc-csc )
476 ( \\det . calcFunc-det )
477 ( \\exp . calcFunc-exp )
478 ( \\gcd . calcFunc-gcd )
479 ( \\ln . calcFunc-ln )
480 ( \\log . calcFunc-log10 )
481 ( \\max . calcFunc-max )
482 ( \\min . calcFunc-min )
483 ( \\sec . calcFunc-sec )
484 ( \\sin . calcFunc-sin )
485 ( \\sinh . calcFunc-sinh )
486 ( \\sqrt . calcFunc-sqrt )
487 ( \\tan . calcFunc-tan )
488 ( \\tanh . calcFunc-tanh )
489 ( \\phi . calcFunc-totient )
490 ( \\mu . calcFunc-moebius )))
492 (put 'tex 'math-special-function-table
493 '((calcFunc-sum . (math-compose-tex-sum "\\sum"))
494 (calcFunc-prod . (math-compose-tex-sum "\\prod"))
495 (intv . math-compose-tex-intv)))
497 (put 'tex 'math-variable-table
500 ( \\alpha . var-alpha )
501 ( \\beta . var-beta )
502 ( \\gamma . var-gamma )
503 ( \\Gamma . var-Gamma )
504 ( \\delta . var-delta )
505 ( \\Delta . var-Delta )
506 ( \\epsilon . var-epsilon )
507 ( \\varepsilon . var-varepsilon)
508 ( \\zeta . var-zeta )
510 ( \\theta . var-theta )
511 ( \\vartheta . var-vartheta )
512 ( \\Theta . var-Theta )
513 ( \\iota . var-iota )
514 ( \\kappa . var-kappa )
515 ( \\lambda . var-lambda )
516 ( \\Lambda . var-Lambda )
522 ( \\varpi . var-varpi )
525 ( \\varrho . var-varrho )
526 ( \\sigma . var-sigma )
527 ( \\sigma . var-varsigma )
528 ( \\Sigma . var-Sigma )
530 ( \\upsilon . var-upsilon )
531 ( \\Upsilon . var-Upsilon )
533 ( \\varphi . var-varphi )
538 ( \\omega . var-omega )
539 ( \\Omega . var-Omega )
542 ( \\infty . var-inf )
543 ( \\infty . var-uinf )
544 ( \\sum . (math-parse-tex-sum calcFunc-sum) )
545 ( \\prod . (math-parse-tex-sum calcFunc-prod) )))
547 (put 'tex 'math-punc-table
552 (put 'tex 'math-complex-format 'i)
554 (put 'tex 'math-input-filter 'math-tex-input-filter)
556 (put 'tex 'math-matrix-formatter
559 (if (and (integerp calc-language-option)
560 (or (= calc-language-option 0)
561 (> calc-language-option 1)
562 (< calc-language-option -1)))
563 (append '(vleft 0 "\\matrix{")
564 (math-compose-tex-matrix (cdr a))
566 (append '(horiz "\\matrix{ ")
567 (math-compose-tex-matrix (cdr a))
570 (put 'tex 'math-var-formatter 'math-compose-tex-var)
572 (put 'tex 'math-func-formatter 'math-compose-tex-func)
574 (put 'tex 'math-dots "\\ldots")
576 (put 'tex 'math-big-parens '("\\left( " . " \\right)"))
578 (put 'tex 'math-evalto '("\\evalto " . " \\to "))
580 (defconst math-tex-ignore-words
581 '( ("\\hbox") ("\\mbox") ("\\text") ("\\left") ("\\right")
582 ("\\,") ("\\>") ("\\:") ("\\;") ("\\!") ("\\ ")
583 ("\\quad") ("\\qquad") ("\\hfil") ("\\hfill")
584 ("\\displaystyle") ("\\textstyle") ("\\dsize") ("\\tsize")
585 ("\\scriptstyle") ("\\scriptscriptstyle") ("\\ssize") ("\\sssize")
586 ("\\rm") ("\\bf") ("\\it") ("\\sl")
587 ("\\roman") ("\\bold") ("\\italic") ("\\slanted")
588 ("\\cal") ("\\mit") ("\\Cal") ("\\Bbb") ("\\frak") ("\\goth")
590 ("\\matrix" mat) ("\\bmatrix" mat) ("\\pmatrix" mat)
592 ("\\cr" punc ";") ("\\\\" punc ";") ("\\*" punc "*")
593 ("\\{" punc "[") ("\\}" punc "]")))
595 (defconst math-latex-ignore-words
596 (append math-tex-ignore-words
597 '(("\\begin" begenv))))
599 (put 'tex 'math-lang-read-symbol
601 (< math-exp-pos (1- (length math-exp-str)))
603 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
604 math-exp-str math-exp-pos)
605 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
606 math-exp-str math-exp-pos))
607 (setq math-exp-token 'symbol
608 math-exp-pos (match-end 0)
609 math-expr-data (math-restore-dashes
610 (math-match-substring math-exp-str 1)))
611 (let ((code (assoc math-expr-data math-latex-ignore-words)))
615 ((eq (nth 1 code) 'punc)
616 (setq math-exp-token 'punc
617 math-expr-data (nth 2 code)))
618 ((and (eq (nth 1 code) 'mat)
619 (string-match " *{" math-exp-str math-exp-pos))
620 (setq math-exp-pos (match-end 0)
623 (let ((right (string-match "}" math-exp-str math-exp-pos)))
625 (setq math-exp-str (copy-sequence math-exp-str))
626 (aset math-exp-str right ?\]))))))))))
628 (defun math-compose-tex-matrix (a &optional ltx)
630 (cons (append (math-compose-vector (cdr (car a)) " & " 0)
631 (if ltx '(" \\\\ ") '(" \\cr ")))
632 (math-compose-tex-matrix (cdr a) ltx))
633 (list (math-compose-vector (cdr (car a)) " & " 0))))
635 (defun math-compose-tex-sum (a fn)
638 (list 'horiz (nth 1 fn)
639 "_{" (math-compose-expr (nth 2 a) 0)
640 "=" (math-compose-expr (nth 3 a) 0)
641 "}^{" (math-compose-expr (nth 4 a) 0)
642 "}{" (math-compose-expr (nth 1 a) 0) "}"))
644 (list 'horiz (nth 1 fn)
645 "_{" (math-compose-expr (nth 2 a) 0)
646 "=" (math-compose-expr (nth 3 a) 0)
647 "}{" (math-compose-expr (nth 1 a) 0) "}"))
649 (list 'horiz (nth 1 fn)
650 "_{" (math-compose-expr (nth 2 a) 0)
651 "}{" (math-compose-expr (nth 1 a) 0) "}"))))
653 (defun math-parse-tex-sum (f val)
655 (or (equal math-expr-data "_") (throw 'syntax "Expected `_'"))
657 (setq save math-exp-old-pos)
658 (setq low (math-read-factor))
659 (or (eq (car-safe low) 'calcFunc-eq)
661 (setq math-exp-old-pos (1+ save))
662 (throw 'syntax "Expected equation")))
663 (or (equal math-expr-data "^") (throw 'syntax "Expected `^'"))
665 (setq high (math-read-factor))
666 (list (nth 2 f) (math-read-factor) (nth 1 low) (nth 2 low) high)))
668 (defun math-tex-input-filter (str) ; allow parsing of 123\,456\,789.
669 (while (string-match "[0-9]\\\\,[0-9]" str)
670 (setq str (concat (substring str 0 (1+ (match-beginning 0)))
671 (substring str (1- (match-end 0))))))
674 ;(defun math-tex-print-sqrt (a)
677 ; (math-compose-expr (nth 1 a) 0)
680 (defun math-compose-tex-intv (a)
682 (if (memq (nth 1 a) '(0 1)) "(" "[")
683 (math-compose-expr (nth 2 a) 0)
685 (math-compose-expr (nth 3 a) 0)
686 (if (memq (nth 1 a) '(0 2)) ")" "]")))
688 (defun math-compose-tex-var (a v prec)
689 (if (and calc-language-option
690 (not (= calc-language-option 0))
691 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'"
692 (symbol-name (nth 1 a))))
693 (if (eq calc-language 'latex)
694 (format "\\text{%s}" (symbol-name (nth 1 a)))
695 (format "\\hbox{%s}" (symbol-name (nth 1 a))))
696 (math-compose-var a v)))
698 (defun math-compose-tex-func (func a)
700 (if (and calc-language-option
701 (not (= calc-language-option 0))
702 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'" func))
703 (if (< (prefix-numeric-value calc-language-option) 0)
704 (setq func (format "\\%s" func))
705 (setq func (if (eq calc-language 'latex)
706 (format "\\text{%s}" func)
707 (format "\\hbox{%s}" func)))))
708 (cond ((or (> (length a) 2)
709 (not (math-tex-expr-is-flat (nth 1 a))))
710 (setq left "\\left( "
712 ((and (eq (aref func 0) ?\\)
714 (string-match "\\hbox{" func)
715 (string-match "\\text{" func)))
717 (or (Math-realp (nth 1 a))
718 (memq (car (nth 1 a)) '(var *))))
719 (setq left "{" right "}"))
720 (t (setq left calc-function-open
721 right calc-function-close)))
724 (math-compose-vector (cdr a) ", " 0)
727 (put 'latex 'math-oper-table
728 (append (get 'tex 'math-oper-table)
729 '(( "\\Hat" calcFunc-Hat -1 950 )
730 ( "\\Check" calcFunc-Check -1 950 )
731 ( "\\Tilde" calcFunc-Tilde -1 950 )
732 ( "\\Acute" calcFunc-Acute -1 950 )
733 ( "\\Grave" calcFunc-Grave -1 950 )
734 ( "\\Dot" calcFunc-Dot -1 950 )
735 ( "\\Ddot" calcFunc-Dotdot -1 950 )
736 ( "\\Breve" calcFunc-Breve -1 950 )
737 ( "\\Bar" calcFunc-Bar -1 950 )
738 ( "\\Vec" calcFunc-VEC -1 950 )
739 ( "\\dddot" calcFunc-dddot -1 950 )
740 ( "\\ddddot" calcFunc-ddddot -1 950 )
741 ( "\\div" / 170 171 )
742 ( "\\le" calcFunc-leq 160 161 )
743 ( "\\leqq" calcFunc-leq 160 161 )
744 ( "\\leqsland" calcFunc-leq 160 161 )
745 ( "\\ge" calcFunc-geq 160 161 )
746 ( "\\geqq" calcFunc-geq 160 161 )
747 ( "\\geqslant" calcFunc-geq 160 161 )
748 ( "=" calcFunc-eq 160 161 )
749 ( "\\neq" calcFunc-neq 160 161 )
750 ( "\\ne" calcFunc-neq 160 161 )
751 ( "\\lnot" calcFunc-lnot -1 121 )
752 ( "\\land" calcFunc-land 110 111 )
753 ( "\\lor" calcFunc-lor 100 101 )
754 ( "?" (math-read-if) 91 90 )
755 ( "!!!" calcFunc-pnot -1 85 )
756 ( "&&&" calcFunc-pand 80 81 )
757 ( "|||" calcFunc-por 75 76 )
758 ( "\\gets" calcFunc-assign 51 50 )
759 ( ":=" calcFunc-assign 51 50 )
760 ( "::" calcFunc-condition 45 46 )
761 ( "\\to" calcFunc-evalto 40 41 )
762 ( "\\to" calcFunc-evalto 40 -1 )
763 ( "=>" calcFunc-evalto 40 41 )
764 ( "=>" calcFunc-evalto 40 -1 ))))
766 (put 'latex 'math-function-table
768 (get 'tex 'math-function-table)
769 '(( \\frac . (math-latex-parse-frac))
770 ( \\tfrac . (math-latex-parse-frac))
771 ( \\dfrac . (math-latex-parse-frac))
772 ( \\binom . (math-latex-parse-two-args calcFunc-choose))
773 ( \\tbinom . (math-latex-parse-two-args calcFunc-choose))
774 ( \\dbinom . (math-latex-parse-two-args calcFunc-choose))
775 ( \\phi . calcFunc-totient )
776 ( \\mu . calcFunc-moebius ))))
778 (put 'latex 'math-special-function-table
779 '((/ . (math-compose-latex-frac "\\frac"))
780 (calcFunc-choose . (math-compose-latex-frac "\\binom"))
781 (calcFunc-sum . (math-compose-tex-sum "\\sum"))
782 (calcFunc-prod . (math-compose-tex-sum "\\prod"))
783 (intv . math-compose-tex-intv)))
785 (put 'latex 'math-variable-table
786 (get 'tex 'math-variable-table))
788 (put 'latex 'math-punc-table
793 (put 'latex 'math-complex-format 'i)
795 (put 'latex 'math-matrix-formatter
798 (if (and (integerp calc-language-option)
799 (or (= calc-language-option 0)
800 (> calc-language-option 1)
801 (< calc-language-option -1)))
802 (append '(vleft 0 "\\begin{pmatrix}")
803 (math-compose-tex-matrix (cdr a) t)
805 (append '(horiz "\\begin{pmatrix} ")
806 (math-compose-tex-matrix (cdr a) t)
807 '(" \\end{pmatrix}"))))))
809 (put 'latex 'math-var-formatter 'math-compose-tex-var)
811 (put 'latex 'math-func-formatter 'math-compose-tex-func)
813 (put 'latex 'math-dots "\\ldots")
815 (put 'latex 'math-big-parens '("\\left( " . " \\right)"))
817 (put 'latex 'math-evalto '("\\evalto " . " \\to "))
819 (put 'latex 'math-lang-read-symbol
821 (< math-exp-pos (1- (length math-exp-str)))
823 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
824 math-exp-str math-exp-pos)
825 (string-match "\\\\text *{\\([a-zA-Z0-9]+\\)}"
826 math-exp-str math-exp-pos)
827 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
828 math-exp-str math-exp-pos))
829 (setq math-exp-token 'symbol
830 math-exp-pos (match-end 0)
831 math-expr-data (math-restore-dashes
832 (math-match-substring math-exp-str 1)))
833 (let ((code (assoc math-expr-data math-tex-ignore-words))
838 ((eq (nth 1 code) 'punc)
839 (setq math-exp-token 'punc
840 math-expr-data (nth 2 code)))
841 ((and (eq (nth 1 code) 'begenv)
842 (string-match " *{\\([^}]*\\)}" math-exp-str math-exp-pos))
843 (setq math-exp-pos (match-end 0)
844 envname (match-string 1 math-exp-str)
847 (cond ((or (string= envname "matrix")
848 (string= envname "bmatrix")
849 (string= envname "smallmatrix")
850 (string= envname "pmatrix"))
851 (if (string-match (concat "\\\\end{" envname "}")
852 math-exp-str math-exp-pos)
854 (replace-match "]" t t math-exp-str))
855 (error "%s" (concat "No closing \\end{" envname "}"))))))
856 ((and (eq (nth 1 code) 'mat)
857 (string-match " *{" math-exp-str math-exp-pos))
858 (setq math-exp-pos (match-end 0)
861 (let ((right (string-match "}" math-exp-str math-exp-pos)))
863 (setq math-exp-str (copy-sequence math-exp-str))
864 (aset math-exp-str right ?\]))))))))))
866 (defun math-latex-parse-frac (f val)
868 (setq numer (car (math-read-expr-list)))
870 (setq denom (math-read-factor))
871 (if (and (Math-num-integerp numer)
872 (Math-num-integerp denom))
873 (list 'frac numer denom)
874 (list '/ numer denom))))
876 (defun math-latex-parse-two-args (f val)
878 (setq first (car (math-read-expr-list)))
880 (setq second (math-read-factor))
881 (list (nth 2 f) first second)))
883 (defun math-compose-latex-frac (a fn)
884 (list 'horiz (nth 1 fn) "{" (math-compose-expr (nth 1 a) -1)
886 (math-compose-expr (nth 2 a) -1)
889 (put 'latex 'math-input-filter 'math-tex-input-filter)
891 (defun calc-eqn-language (n)
894 (calc-set-language 'eqn)
895 (message "Eqn language mode")))
897 (put 'eqn 'math-oper-table
898 '( ( "prime" (math-parse-eqn-prime) 950 -1 )
899 ( "prime" calcFunc-Prime 950 -1 )
900 ( "dot" calcFunc-dot 950 -1 )
901 ( "dotdot" calcFunc-dotdot 950 -1 )
902 ( "hat" calcFunc-hat 950 -1 )
903 ( "tilde" calcFunc-tilde 950 -1 )
904 ( "vec" calcFunc-Vec 950 -1 )
905 ( "dyad" calcFunc-dyad 950 -1 )
906 ( "bar" calcFunc-bar 950 -1 )
907 ( "under" calcFunc-under 950 -1 )
908 ( "sub" calcFunc-subscr 931 930 )
910 ( "sqrt" calcFunc-sqrt -1 910 )
912 ( "u|" calcFunc-abs -1 0 )
914 ( "left floor" calcFunc-floor -1 0 )
915 ( "right floor" closing 0 -1 )
916 ( "left ceil" calcFunc-ceil -1 0 )
917 ( "right ceil" closing 0 -1 )
918 ( "+-" sdev 300 300 )
919 ( "!" calcFunc-fact 210 -1 )
920 ( "u+" ident -1 197 )
922 ( "times" * 191 190 )
929 ( "<" calcFunc-lt 160 161 )
930 ( ">" calcFunc-gt 160 161 )
931 ( "<=" calcFunc-leq 160 161 )
932 ( ">=" calcFunc-geq 160 161 )
933 ( "=" calcFunc-eq 160 161 )
934 ( "==" calcFunc-eq 160 161 )
935 ( "!=" calcFunc-neq 160 161 )
936 ( "u!" calcFunc-lnot -1 121 )
937 ( "&&" calcFunc-land 110 111 )
938 ( "||" calcFunc-lor 100 101 )
939 ( "?" (math-read-if) 91 90 )
940 ( "!!!" calcFunc-pnot -1 85 )
941 ( "&&&" calcFunc-pand 80 81 )
942 ( "|||" calcFunc-por 75 76 )
943 ( "<-" calcFunc-assign 51 50 )
944 ( ":=" calcFunc-assign 51 50 )
945 ( "::" calcFunc-condition 45 46 )
946 ( "->" calcFunc-evalto 40 41 )
947 ( "->" calcFunc-evalto 40 -1 )
948 ( "=>" calcFunc-evalto 40 41 )
949 ( "=>" calcFunc-evalto 40 -1 )))
951 (put 'eqn 'math-function-table
952 '( ( arc\ cos . calcFunc-arccos )
953 ( arc\ cosh . calcFunc-arccosh )
954 ( arc\ sin . calcFunc-arcsin )
955 ( arc\ sinh . calcFunc-arcsinh )
956 ( arc\ tan . calcFunc-arctan )
957 ( arc\ tanh . calcFunc-arctanh )
958 ( GAMMA . calcFunc-gamma )
959 ( phi . calcFunc-totient )
960 ( mu . calcFunc-moebius )
961 ( matrix . (math-parse-eqn-matrix) )))
963 (put 'eqn 'math-special-function-table
964 '((intv . math-compose-eqn-intv)))
966 (put 'eqn 'math-punc-table
970 (put 'eqn 'math-variable-table
971 '( ( inf . var-uinf )))
973 (put 'eqn 'math-complex-format 'i)
975 (put 'eqn 'math-big-parens '("{left ( " . " right )}"))
977 (put 'eqn 'math-evalto '("evalto " . " -> "))
979 (put 'eqn 'math-matrix-formatter
982 (append '(horiz "matrix { ")
983 (math-compose-eqn-matrix
984 (cdr (math-transpose a)))
987 (put 'eqn 'math-var-formatter
990 (if (and math-compose-hash-args
991 (let ((p calc-arg-values))
993 (while (and p (not (equal (car p) a)))
994 (setq p (and (eq math-compose-hash-args t) (cdr p))
997 (if (eq math-compose-hash-args 1)
1000 (if (string-match ".'\\'" (symbol-name (nth 2 a)))
1002 (list 'calcFunc-Prime
1005 (intern (substring (symbol-name (nth 1 a)) 0 -1))
1006 (intern (substring (symbol-name (nth 2 a)) 0 -1))))
1008 (symbol-name (nth 1 a)))))))
1010 (defconst math-eqn-special-funcs
1012 calcFunc-ln calcFunc-exp
1013 calcFunc-sin calcFunc-cos calcFunc-tan
1014 calcFunc-sec calcFunc-csc calcFunc-cot
1015 calcFunc-sinh calcFunc-cosh calcFunc-tanh
1016 calcFunc-sech calcFunc-csch calcFunc-coth
1017 calcFunc-arcsin calcFunc-arccos calcFunc-arctan
1018 calcFunc-arcsinh calcFunc-arccosh calcFunc-arctanh))
1020 (put 'eqn 'math-func-formatter
1024 (if (string-match "[^']'+\\'" func)
1025 (let ((n (- (length func) (match-beginning 0) 1)))
1026 (setq func (substring func 0 (- n)))
1027 (while (>= (setq n (1- n)) 0)
1028 (setq func (concat func " prime")))))
1029 (cond ((or (> (length a) 2)
1030 (not (math-tex-expr-is-flat (nth 1 a))))
1031 (setq left "{left ( "
1035 (memq (car a) math-eqn-special-funcs)
1037 (or (Math-realp (nth 1 a))
1038 (memq (car (nth 1 a)) '(var *))))
1039 (setq left "~{" right "}"))
1043 (list 'horiz func left
1044 (math-compose-vector (cdr a) " , " 0)
1047 (put 'eqn 'math-lang-read-symbol
1049 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)"
1050 math-exp-str math-exp-pos)
1052 (setq math-exp-str (copy-sequence math-exp-str))
1053 (aset math-exp-str (match-beginning 1) ?\{)
1054 (if (< (match-end 1) (length math-exp-str))
1055 (aset math-exp-str (match-end 1) ?\}))
1056 (math-read-token)))))
1058 (defconst math-eqn-ignore-words
1059 '( ("roman") ("bold") ("italic") ("mark") ("lineup") ("evalto")
1060 ("left" ("floor") ("ceil"))
1061 ("right" ("floor") ("ceil"))
1062 ("arc" ("sin") ("cos") ("tan") ("sinh") ("cosh") ("tanh"))
1063 ("size" n) ("font" n) ("fwd" n) ("back" n) ("up" n) ("down" n)
1064 ("above" punc ",")))
1066 (put 'eqn 'math-lang-adjust-words
1069 (let ((code (assoc math-expr-data math-eqn-ignore-words)))
1073 ((consp (nth 1 code))
1075 (if (assoc math-expr-data (cdr code))
1076 (setq math-expr-data (format "%s %s"
1077 (car code) math-expr-data))))
1078 ((eq (nth 1 code) 'punc)
1079 (setq math-exp-token 'punc
1080 math-expr-data (nth 2 code)))
1083 (math-read-token)))))))
1085 (put 'eqn 'math-lang-read
1086 '((eq (string-match "->\\|<-\\|+-\\|\\\\dots\\|~\\|\\^"
1087 math-exp-str math-exp-pos)
1090 (setq math-exp-token 'punc
1091 math-expr-data (math-match-substring math-exp-str 0)
1092 math-exp-pos (match-end 0))
1093 (and (eq (string-match "\\\\dots\\." math-exp-str math-exp-pos)
1095 (setq math-exp-pos (match-end 0)))
1096 (if (memq (aref math-expr-data 0) '(?~ ?^))
1097 (math-read-token)))))
1100 (defun math-compose-eqn-matrix (a)
1103 (cond ((eq calc-matrix-just 'right) "rcol ")
1104 ((eq calc-matrix-just 'center) "ccol ")
1107 (list 'break math-compose-level)
1111 (let ((math-compose-level (1+ math-compose-level)))
1112 (math-compose-vector (cdr (car a)) " above " 1000))
1115 (math-compose-eqn-matrix (cdr a)))))))
1118 (defun math-parse-eqn-matrix (f sym)
1120 (while (assoc math-expr-data '(("ccol") ("lcol") ("rcol")))
1122 (or (equal math-expr-data calc-function-open)
1123 (throw 'syntax "Expected `{'"))
1125 (setq vec (cons (cons 'vec (math-read-expr-list)) vec))
1126 (or (equal math-expr-data calc-function-close)
1127 (throw 'syntax "Expected `}'"))
1129 (or (equal math-expr-data calc-function-close)
1130 (throw 'syntax "Expected `}'"))
1132 (math-transpose (cons 'vec (nreverse vec)))))
1134 (defun math-parse-eqn-prime (x sym)
1135 (if (eq (car-safe x) 'var)
1136 (if (equal math-expr-data calc-function-open)
1139 (let ((args (if (or (equal math-expr-data calc-function-close)
1140 (eq math-exp-token 'end))
1142 (math-read-expr-list))))
1143 (if (not (or (equal math-expr-data calc-function-close)
1144 (eq math-exp-token 'end)))
1145 (throw 'syntax "Expected `)'"))
1147 (cons (intern (format "calcFunc-%s'" (nth 1 x))) args)))
1149 (intern (concat (symbol-name (nth 1 x)) "'"))
1150 (intern (concat (symbol-name (nth 2 x)) "'"))))
1151 (list 'calcFunc-Prime x)))
1153 (defun math-compose-eqn-intv (a)
1155 (if (memq (nth 1 a) '(0 1)) "(" "[")
1156 (math-compose-expr (nth 2 a) 0)
1158 (math-compose-expr (nth 3 a) 0)
1159 (if (memq (nth 1 a) '(0 2)) ")" "]")))
1162 (defun calc-mathematica-language ()
1165 (calc-set-language 'math)
1166 (message "Mathematica language mode")))
1168 (put 'math 'math-oper-table
1169 '( ( "[[" (math-read-math-subscr) 250 -1 )
1170 ( "!" calcFunc-fact 210 -1 )
1171 ( "!!" calcFunc-dfact 210 -1 )
1173 ( "u+" ident -1 197 )
1180 ( "<" calcFunc-lt 160 161 )
1181 ( ">" calcFunc-gt 160 161 )
1182 ( "<=" calcFunc-leq 160 161 )
1183 ( ">=" calcFunc-geq 160 161 )
1184 ( "==" calcFunc-eq 150 151 )
1185 ( "!=" calcFunc-neq 150 151 )
1186 ( "u!" calcFunc-lnot -1 121 )
1187 ( "&&" calcFunc-land 110 111 )
1188 ( "||" calcFunc-lor 100 101 )
1189 ( "!!!" calcFunc-pnot -1 85 )
1190 ( "&&&" calcFunc-pand 80 81 )
1191 ( "|||" calcFunc-por 75 76 )
1192 ( ":=" calcFunc-assign 51 50 )
1193 ( "=" calcFunc-assign 51 50 )
1194 ( "->" calcFunc-assign 51 50 )
1195 ( ":>" calcFunc-assign 51 50 )
1196 ( "::" calcFunc-condition 45 46 )
1199 (put 'math 'math-function-table
1200 '( ( Abs . calcFunc-abs )
1201 ( ArcCos . calcFunc-arccos )
1202 ( ArcCosh . calcFunc-arccosh )
1203 ( ArcSin . calcFunc-arcsin )
1204 ( ArcSinh . calcFunc-arcsinh )
1205 ( ArcTan . calcFunc-arctan )
1206 ( ArcTanh . calcFunc-arctanh )
1207 ( Arg . calcFunc-arg )
1208 ( Binomial . calcFunc-choose )
1209 ( Ceiling . calcFunc-ceil )
1210 ( Conjugate . calcFunc-conj )
1211 ( Cos . calcFunc-cos )
1212 ( Cosh . calcFunc-cosh )
1213 ( Cot . calcFunc-cot )
1214 ( Coth . calcFunc-coth )
1215 ( Csc . calcFunc-csc )
1216 ( Csch . calcFunc-csch )
1217 ( D . calcFunc-deriv )
1218 ( Dt . calcFunc-tderiv )
1219 ( Det . calcFunc-det )
1220 ( Exp . calcFunc-exp )
1221 ( EulerPhi . calcFunc-totient )
1222 ( Floor . calcFunc-floor )
1223 ( Gamma . calcFunc-gamma )
1224 ( GCD . calcFunc-gcd )
1225 ( If . calcFunc-if )
1226 ( Im . calcFunc-im )
1227 ( Inverse . calcFunc-inv )
1228 ( Integrate . calcFunc-integ )
1229 ( Join . calcFunc-vconcat )
1230 ( LCM . calcFunc-lcm )
1231 ( Log . calcFunc-ln )
1232 ( Max . calcFunc-max )
1233 ( Min . calcFunc-min )
1234 ( Mod . calcFunc-mod )
1235 ( MoebiusMu . calcFunc-moebius )
1236 ( Random . calcFunc-random )
1237 ( Round . calcFunc-round )
1238 ( Re . calcFunc-re )
1239 ( Sec . calcFunc-sec )
1240 ( Sech . calcFunc-sech )
1241 ( Sign . calcFunc-sign )
1242 ( Sin . calcFunc-sin )
1243 ( Sinh . calcFunc-sinh )
1244 ( Sqrt . calcFunc-sqrt )
1245 ( Tan . calcFunc-tan )
1246 ( Tanh . calcFunc-tanh )
1247 ( Transpose . calcFunc-trn )
1248 ( Length . calcFunc-vlen )
1251 (put 'math 'math-variable-table
1255 ( GoldenRatio . var-phi )
1256 ( EulerGamma . var-gamma )
1257 ( Infinity . var-inf )
1258 ( ComplexInfinity . var-uinf )
1259 ( Indeterminate . var-nan )
1262 (put 'math 'math-vector-brackets "{}")
1263 (put 'math 'math-complex-format 'I)
1264 (put 'math 'math-function-open "[")
1265 (put 'math 'math-function-close "]")
1267 (put 'math 'math-radix-formatter
1268 (function (lambda (r s) (format "%d^^%s" r s))))
1270 (put 'math 'math-lang-read
1271 '((eq (string-match "\\[\\[\\|->\\|:>" math-exp-str math-exp-pos)
1273 (setq math-exp-token 'punc
1274 math-expr-data (math-match-substring math-exp-str 0)
1275 math-exp-pos (match-end 0))))
1277 (put 'math 'math-compose-subscr
1281 (math-compose-expr (nth 1 a) 1000)
1283 (math-compose-expr (nth 2 a) 0)
1286 (defun math-read-math-subscr (x op)
1287 (let ((idx (math-read-expr-level 0)))
1288 (or (and (equal math-expr-data "]")
1291 (equal math-expr-data "]")))
1292 (throw 'syntax "Expected ']]'"))
1294 (list 'calcFunc-subscr x idx)))
1297 (defun calc-maple-language ()
1300 (calc-set-language 'maple)
1301 (message "Maple language mode")))
1303 (put 'maple 'math-oper-table
1304 '( ( "matrix" ident -1 300 )
1305 ( "MATRIX" ident -1 300 )
1306 ( "!" calcFunc-fact 210 -1 )
1309 ( "u+" ident -1 197 )
1313 ( "intersect" calcFunc-vint 191 192 )
1316 ( "union" calcFunc-vunion 180 181 )
1317 ( "minus" calcFunc-vdiff 180 181 )
1319 ( ".." (math-read-maple-dots) 165 165 )
1320 ( "\\dots" (math-read-maple-dots) 165 165 )
1321 ( "<" calcFunc-lt 160 160 )
1322 ( ">" calcFunc-gt 160 160 )
1323 ( "<=" calcFunc-leq 160 160 )
1324 ( ">=" calcFunc-geq 160 160 )
1325 ( "=" calcFunc-eq 160 160 )
1326 ( "<>" calcFunc-neq 160 160 )
1327 ( "not" calcFunc-lnot -1 121 )
1328 ( "and" calcFunc-land 110 111 )
1329 ( "or" calcFunc-lor 100 101 )
1330 ( "!!!" calcFunc-pnot -1 85 )
1331 ( "&&&" calcFunc-pand 80 81 )
1332 ( "|||" calcFunc-por 75 76 )
1333 ( ":=" calcFunc-assign 51 50 )
1334 ( "::" calcFunc-condition 45 46 )
1337 (put 'maple 'math-function-table
1338 '( ( bernoulli . calcFunc-bern )
1339 ( binomial . calcFunc-choose )
1340 ( diff . calcFunc-deriv )
1341 ( GAMMA . calcFunc-gamma )
1342 ( ifactor . calcFunc-prfac )
1343 ( igcd . calcFunc-gcd )
1344 ( ilcm . calcFunc-lcm )
1345 ( int . calcFunc-integ )
1348 ( iquo . calcFunc-idiv )
1349 ( isprime . calcFunc-prime )
1350 ( length . calcFunc-vlen )
1351 ( member . calcFunc-in )
1352 ( crossprod . calcFunc-cross )
1353 ( inverse . calcFunc-inv )
1354 ( trace . calcFunc-tr )
1355 ( transpose . calcFunc-trn )
1356 ( vectdim . calcFunc-vlen )
1359 (put 'maple 'math-special-function-table
1360 '((intv . math-compose-maple-intv)))
1362 (put 'maple 'math-variable-table
1366 ( infinity . var-inf )
1367 ( infinity . var-uinf )
1368 ( infinity . var-nan )
1371 (put 'maple 'math-complex-format 'I)
1373 (put 'maple 'math-matrix-formatter
1378 math-comp-left-bracket
1379 (math-compose-vector (cdr a)
1380 (concat math-comp-comma " ")
1381 math-comp-vector-prec)
1382 math-comp-right-bracket
1385 (put 'maple 'math-compose-subscr
1388 (let ((args (cdr (cdr a))))
1390 (math-compose-expr (nth 1 a) 1000)
1392 (math-compose-vector args ", " 0)
1395 (add-to-list 'calc-lang-allow-underscores 'maple)
1396 (add-to-list 'calc-lang-brackets-are-subscripts 'maple)
1398 (defun math-compose-maple-intv (a)
1400 (math-compose-expr (nth 2 a) 0)
1402 (math-compose-expr (nth 3 a) 0)))
1404 (defun math-read-maple-dots (x op)
1405 (list 'intv 3 x (math-read-expr-level (nth 3 op))))
1408 ;; The variable math-read-big-lines is local to math-read-big-expr in
1409 ;; calc-ext.el, but is used by math-read-big-rec, math-read-big-char,
1410 ;; math-read-big-emptyp, math-read-big-error and math-read-big-balance,
1411 ;; which are called (directly and indirectly) by math-read-big-expr.
1412 ;; It is also local to math-read-big-bigp in calc-ext.el, which calls
1413 ;; math-read-big-balance.
1414 (defvar math-read-big-lines)
1416 ;; The variables math-read-big-baseline and math-read-big-h2 are
1417 ;; local to math-read-big-expr in calc-ext.el, but used by
1418 ;; math-read-big-rec.
1419 (defvar math-read-big-baseline)
1420 (defvar math-read-big-h2)
1422 ;; The variables math-rb-h1, math-rb-h2, math-rb-v1 and math-rb-v2
1423 ;; are local to math-read-big-rec, but are used by math-read-big-char,
1424 ;; math-read-big-emptyp and math-read-big-balance which are called by
1425 ;; math-read-big-rec.
1426 ;; math-rb-h2 is also local to math-read-big-bigp in calc-ext.el,
1427 ;; which calls math-read-big-balance.
1433 (defun math-read-big-rec (math-rb-h1 math-rb-v1 math-rb-h2 math-rb-v2
1434 &optional baseline prec short)
1435 (or prec (setq prec 0))
1437 ;; Clip whitespace above or below.
1438 (while (and (< math-rb-v1 math-rb-v2)
1439 (math-read-big-emptyp math-rb-h1 math-rb-v1 math-rb-h2 (1+ math-rb-v1)))
1440 (setq math-rb-v1 (1+ math-rb-v1)))
1441 (while (and (< math-rb-v1 math-rb-v2)
1442 (math-read-big-emptyp math-rb-h1 (1- math-rb-v2) math-rb-h2 math-rb-v2))
1443 (setq math-rb-v2 (1- math-rb-v2)))
1445 ;; If formula is a single line high, normal parser can handle it.
1446 (if (<= math-rb-v2 (1+ math-rb-v1))
1447 (if (or (<= math-rb-v2 math-rb-v1)
1448 (> math-rb-h1 (length (setq math-rb-v2
1449 (nth math-rb-v1 math-read-big-lines)))))
1450 (math-read-big-error math-rb-h1 math-rb-v1)
1451 (setq math-read-big-baseline math-rb-v1
1452 math-read-big-h2 math-rb-h2
1453 math-rb-v2 (nth math-rb-v1 math-read-big-lines)
1454 math-rb-h2 (math-read-expr
1455 (substring math-rb-v2 math-rb-h1
1456 (min math-rb-h2 (length math-rb-v2)))))
1457 (if (eq (car-safe math-rb-h2) 'error)
1458 (math-read-big-error (+ math-rb-h1 (nth 1 math-rb-h2))
1459 math-rb-v1 (nth 2 math-rb-h2))
1462 ;; Clip whitespace at left or right.
1463 (while (and (< math-rb-h1 math-rb-h2)
1464 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) math-rb-v2))
1465 (setq math-rb-h1 (1+ math-rb-h1)))
1466 (while (and (< math-rb-h1 math-rb-h2)
1467 (math-read-big-emptyp (1- math-rb-h2) math-rb-v1 math-rb-h2 math-rb-v2))
1468 (setq math-rb-h2 (1- math-rb-h2)))
1470 ;; Scan to find widest left-justified "----" in the region.
1473 (lines-v1 (nthcdr math-rb-v1 math-read-big-lines))
1477 other-char line len h)
1478 (while (< v math-rb-v2)
1480 len (min math-rb-h2 (length line)))
1481 (and (< math-rb-h1 len)
1482 (/= (aref line math-rb-h1) ?\ )
1483 (if (and (= (aref line math-rb-h1) ?\-)
1484 ;; Make sure it's not a minus sign.
1485 (or (and (< (1+ math-rb-h1) len)
1486 (= (aref line (1+ math-rb-h1)) ?\-))
1487 (/= (math-read-big-char math-rb-h1 (1- v)) ?\ )
1488 (/= (math-read-big-char math-rb-h1 (1+ v)) ?\ )))
1491 (while (and (< (setq h (1+ h)) len)
1492 (= (aref line h) ?\-)))
1496 (or other-v (setq other-v v other-char (aref line math-rb-h1)))))
1500 (cond ((not (setq v other-v))
1501 (math-read-big-error math-rb-h1 math-rb-v1)) ; Should never happen!
1507 (let ((num (math-read-big-rec math-rb-h1 math-rb-v1 h v))
1508 (den (math-read-big-rec math-rb-h1 (1+ v) h math-rb-v2)))
1509 (setq p (if (and (math-integerp num) (math-integerp den))
1510 (math-make-frac num den)
1511 (list '/ num den)))))
1513 ;; Big radical sign.
1515 (or (= (math-read-big-char (1+ math-rb-h1) v) ?\|)
1516 (math-read-big-error (1+ math-rb-h1) v "Malformed root sign"))
1517 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1518 (while (= (math-read-big-char (1+ math-rb-h1) (setq v (1- v))) ?\|))
1519 (or (= (math-read-big-char (setq h (+ math-rb-h1 2)) v) ?\_)
1520 (math-read-big-error h v "Malformed root sign"))
1521 (while (= (math-read-big-char (setq h (1+ h)) v) ?\_))
1522 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1523 (math-read-big-emptyp math-rb-h1 (1+ other-v) h math-rb-v2 nil t)
1524 (setq p (list 'calcFunc-sqrt (math-read-big-rec
1525 (+ math-rb-h1 2) (1+ v)
1526 h (1+ other-v) baseline))
1527 v math-read-big-baseline))
1529 ;; Small radical sign.
1530 ((and (= other-char ?V)
1531 (= (math-read-big-char (1+ math-rb-h1) (1- v)) ?\_))
1532 (setq h (1+ math-rb-h1))
1533 (math-read-big-emptyp math-rb-h1 math-rb-v1 h (1- v) nil t)
1534 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)
1535 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1536 (while (= (math-read-big-char (setq h (1+ h)) (1- v)) ?\_))
1537 (setq p (list 'calcFunc-sqrt (math-read-big-rec
1538 (1+ math-rb-h1) v h (1+ v) t))
1539 v math-read-big-baseline))
1541 ;; Binomial coefficient.
1542 ((and (= other-char ?\()
1543 (= (math-read-big-char (1+ math-rb-h1) v) ?\ )
1544 (= (string-match "( *)" (nth v math-read-big-lines)
1545 math-rb-h1) math-rb-h1))
1546 (setq h (match-end 0))
1547 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1548 (math-read-big-emptyp math-rb-h1 (1+ v) (1+ math-rb-h1) math-rb-v2 nil t)
1549 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
1550 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
1551 (setq p (list 'calcFunc-choose
1552 (math-read-big-rec (1+ math-rb-h1) math-rb-v1 (1- h) v)
1553 (math-read-big-rec (1+ math-rb-h1) (1+ v)
1554 (1- h) math-rb-v2))))
1558 (setq p (list 'neg (math-read-big-rec (1+ math-rb-h1) math-rb-v1
1559 math-rb-h2 math-rb-v2 v 250 t))
1560 v math-read-big-baseline
1561 h math-read-big-h2))
1565 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1566 (math-read-big-emptyp math-rb-h1 (1+ v) (1+ math-rb-h1) math-rb-v2 nil t)
1567 (setq h (math-read-big-balance (1+ math-rb-h1) v "(" t))
1568 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
1569 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
1570 (let ((sep (math-read-big-char (1- h) v))
1575 (math-read-big-error (1- h) v "Expected `)'"))
1577 (setq p (math-read-big-rec
1578 (1+ math-rb-h1) math-rb-v1 (1- h) math-rb-v2 v))
1579 (setq hmid (math-read-big-balance h v "(")
1581 (math-read-big-rec h math-rb-v1 (1- hmid) math-rb-v2 v))
1584 (setq p (cons 'intv (cons (if (= (math-read-big-char
1589 ((= (math-read-big-char (1- h) v) ?\])
1590 (math-read-big-error (1- h) v "Expected `)'"))
1592 (or (and (math-realp (car p)) (math-realp (nth 1 p)))
1593 (math-read-big-error
1594 math-rb-h1 v "Complex components must be real"))
1595 (setq p (cons 'cplx p)))
1597 (or (and (math-realp (car p)) (math-anglep (nth 1 p)))
1598 (math-read-big-error
1599 math-rb-h1 v "Complex components must be real"))
1600 (setq p (cons 'polar p)))))))
1603 ((and (= other-char ?\[)
1604 (or (= (math-read-big-char (setq h math-rb-h1) (1+ v)) ?\[)
1605 (= (math-read-big-char (setq h (1+ h)) v) ?\[)
1606 (and (= (math-read-big-char h v) ?\ )
1607 (= (math-read-big-char (setq h (1+ h)) v) ?\[)))
1608 (= (math-read-big-char h (1+ v)) ?\[))
1609 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
1615 (setq h (math-read-big-balance (1+ hleft) v "["))
1618 (math-read-big-error hright v "Expected `]'"))
1620 (setq p (cons (math-read-big-rec
1621 hleft v h (1+ v)) p))
1622 (and (memq (math-read-big-char h v) '(?\ ?\,))
1623 (= (math-read-big-char hleft (1+ v)) ?\[)))
1625 (or (= hleft math-rb-h1)
1627 (if (= (math-read-big-char h v) ?\ )
1629 (and (= (math-read-big-char h v) ?\])
1631 (math-read-big-error (1- h) v "Expected `]'"))
1632 (if (= (math-read-big-char h vtop) ?\,)
1634 (math-read-big-emptyp math-rb-h1 (1+ v) (1- h) math-rb-v2 nil t)
1635 (setq v (+ vtop (/ (- v vtop) 2))
1636 p (cons 'vec (nreverse p)))))
1640 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1641 (math-read-big-emptyp math-rb-h1 (1+ v) (1+ math-rb-h1) math-rb-v2 nil t)
1645 (setq widest (math-read-big-balance h v "[" t))
1646 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
1647 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
1648 (setq p (cons (math-read-big-rec
1649 h math-rb-v1 (1- widest) math-rb-v2 v) p)
1651 (= (math-read-big-char (1- h) v) ?\,)))
1652 (setq widest (math-read-big-char (1- h) v))
1653 (if (or (memq widest '(?\; ?\)))
1654 (and (eq widest ?\.) (cdr p)))
1655 (math-read-big-error (1- h) v "Expected `]'"))
1658 widest (math-read-big-balance h v "[")
1659 p (nconc p (list (math-read-big-rec
1660 h math-rb-v1 (1- widest) math-rb-v2 v)))
1662 p (cons 'intv (cons (if (= (math-read-big-char (1- h) v)
1666 (setq p (cons 'vec (nreverse p)))))
1670 (setq line (nth v math-read-big-lines))
1671 (string-match ">" line math-rb-h1)
1672 (setq h (match-end 0))
1673 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
1674 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)
1675 (setq p (math-read-big-rec math-rb-h1 v h (1+ v) v)))
1677 ;; Variable name or function call.
1678 ((or (and (>= other-char ?a) (<= other-char ?z))
1679 (and (>= other-char ?A) (<= other-char ?Z)))
1680 (setq line (nth v math-read-big-lines))
1681 (string-match "\\([a-zA-Z'_]+\\) *" line math-rb-h1)
1682 (setq h (match-end 1)
1683 widest (match-end 0)
1684 p (math-match-substring line 1))
1685 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
1686 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)
1687 (if (= (math-read-big-char widest v) ?\()
1689 (setq line (if (string-match "-" p)
1691 (intern (concat "calcFunc-" p)))
1694 (math-read-big-emptyp widest math-rb-v1 h v nil t)
1695 (math-read-big-emptyp widest (1+ v) h math-rb-v2 nil t)
1697 (setq widest (math-read-big-balance h v "(" t))
1698 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
1699 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
1700 (setq p (cons (math-read-big-rec
1701 h math-rb-v1 (1- widest) math-rb-v2 v) p)
1703 (= (math-read-big-char (1- h) v) ?\,)))
1704 (or (= (math-read-big-char (1- h) v) ?\))
1705 (math-read-big-error (1- h) v "Expected `)'"))
1706 (setq p (cons line (nreverse p))))
1708 (intern (math-remove-dashes p))
1709 (if (string-match "-" p)
1711 (intern (concat "var-" p)))))))
1715 (setq line (nth v math-read-big-lines))
1716 (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)
1717 (math-read-big-error h v "Expected a number"))
1718 (setq h (match-end 0)
1719 p (math-read-number (math-match-substring line 0)))
1720 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
1721 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)))
1723 ;; Now left term is bounded by math-rb-h1, math-rb-v1, h, math-rb-v2;
1727 (math-read-big-error math-rb-h1 v "Inconsistent baseline in formula"))
1730 ;; Look for superscripts or subscripts.
1731 (setq line (nth baseline math-read-big-lines)
1732 len (min math-rb-h2 (length line))
1734 (while (and (< widest len)
1735 (= (aref line widest) ?\ ))
1736 (setq widest (1+ widest)))
1737 (and (>= widest len) (setq widest math-rb-h2))
1738 (if (math-read-big-emptyp h v widest math-rb-v2)
1739 (if (math-read-big-emptyp h math-rb-v1 widest v)
1741 (setq p (list '^ p (math-read-big-rec h math-rb-v1 widest v))
1743 (if (math-read-big-emptyp h math-rb-v1 widest v)
1744 (setq p (list 'calcFunc-subscr p
1745 (math-read-big-rec h v widest math-rb-v2))
1748 ;; Look for an operator name and grab additional terms.
1749 (while (and (< h len)
1750 (if (setq widest (and (math-read-big-emptyp
1751 h math-rb-v1 (1+ h) v)
1752 (math-read-big-emptyp
1753 h (1+ v) (1+ h) math-rb-v2)
1754 (string-match "<=\\|>=\\|\\+/-\\|!=\\|&&\\|||\\|:=\\|=>\\|." line h)
1755 (assoc (math-match-substring line 0)
1756 (math-standard-ops))))
1757 (and (>= (nth 2 widest) prec)
1758 (setq h (match-end 0)))
1759 (and (not (eq (string-match ",\\|;\\|\\.\\.\\|)\\|\\]\\|:" line h)
1761 (setq widest '("2x" * 196 195)))))
1762 (cond ((eq (nth 3 widest) -1)
1763 (setq p (list (nth 1 widest) p)))
1764 ((equal (car widest) "?")
1765 (let ((y (math-read-big-rec h math-rb-v1 math-rb-h2
1766 math-rb-v2 baseline nil t)))
1767 (or (= (math-read-big-char math-read-big-h2 baseline) ?\:)
1768 (math-read-big-error math-read-big-h2 baseline "Expected `:'"))
1769 (setq p (list (nth 1 widest) p y
1771 (1+ math-read-big-h2) math-rb-v1 math-rb-h2 math-rb-v2
1772 baseline (nth 3 widest) t))
1773 h math-read-big-h2)))
1775 (setq p (list (nth 1 widest) p
1776 (math-read-big-rec h math-rb-v1 math-rb-h2 math-rb-v2
1777 baseline (nth 3 widest) t))
1778 h math-read-big-h2))))
1780 ;; Return all relevant information to caller.
1781 (setq math-read-big-baseline baseline
1783 (or short (= math-read-big-h2 math-rb-h2)
1784 (math-read-big-error h baseline))
1787 (defun math-read-big-char (h v)
1788 (or (and (>= h math-rb-h1)
1792 (let ((line (nth v math-read-big-lines)))
1798 (defun math-read-big-emptyp (eh1 ev1 eh2 ev2 &optional what error)
1799 (and (< ev1 math-rb-v1) (setq ev1 math-rb-v1))
1800 (and (< eh1 math-rb-h1) (setq eh1 math-rb-h1))
1801 (and (> ev2 math-rb-v2) (setq ev2 math-rb-v2))
1802 (and (> eh2 math-rb-h2) (setq eh2 math-rb-h2))
1803 (or what (setq what ?\ ))
1804 (let ((p (nthcdr ev1 math-read-big-lines))
1806 (while (and (< ev1 ev2)
1808 (setq h (min eh2 (length (car p))))
1809 (while (and (>= (setq h (1- h)) eh1)
1810 (= (aref (car p) h) what)))
1811 (and error (>= h eh1)
1812 (math-read-big-error h ev1 (if (stringp error)
1814 "Whitespace expected")))
1820 ;; math-read-big-err-msg is local to math-read-big-expr in calc-ext.el,
1821 ;; but is used by math-read-big-error which is called (indirectly) by
1822 ;; math-read-big-expr.
1823 (defvar math-read-big-err-msg)
1825 (defun math-read-big-error (h v &optional msg)
1827 (p math-read-big-lines))
1829 (setq pos (+ pos 1 (length (car p)))
1832 (setq h (+ pos (min h (length (car p))))
1833 math-read-big-err-msg (list 'error h (or msg "Syntax error")))
1834 (throw 'syntax nil)))
1836 (defun math-read-big-balance (h v what &optional commas)
1837 (let* ((line (nth v math-read-big-lines))
1838 (len (min math-rb-h2 (length line)))
1843 (math-read-big-error nil v (format "Unmatched `%s'" what))
1845 (if (memq (aref line h) '(?\( ?\[))
1846 (setq count (1+ count))
1847 (if (if (and commas (= count 1))
1848 (or (memq (aref line h) '(?\) ?\] ?\, ?\;))
1849 (and (eq (aref line h) ?\.)
1851 (eq (aref line (1+ h)) ?\.)))
1852 (memq (aref line h) '(?\) ?\])))
1853 (setq count (1- count))))
1857 (provide 'calc-lang)
1859 ;;; arch-tag: 483bfe15-f290-4fef-bb7d-ce65be687f2e
1860 ;;; calc-lang.el ends here