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