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