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