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