calc-lang.el 85 KB

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