calc-alg.el 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  1. ;;; calc-alg.el --- algebraic functions for Calc
  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. ;;; Algebra commands.
  22. (defun calc-alg-evaluate (arg)
  23. (interactive "p")
  24. (calc-slow-wrapper
  25. (calc-with-default-simplification
  26. (let ((math-simplify-only nil))
  27. (calc-modify-simplify-mode arg)
  28. (calc-enter-result 1 "dsmp" (calc-top 1))))))
  29. (defun calc-modify-simplify-mode (arg)
  30. (if (= (math-abs arg) 2)
  31. (setq calc-simplify-mode 'alg)
  32. (if (>= (math-abs arg) 3)
  33. (setq calc-simplify-mode 'ext)))
  34. (if (< arg 0)
  35. (setq calc-simplify-mode (list calc-simplify-mode))))
  36. (defun calc-simplify ()
  37. (interactive)
  38. (calc-slow-wrapper
  39. (let ((top (calc-top-n 1)))
  40. (if (calc-is-inverse)
  41. (setq top
  42. (let ((calc-simplify-mode nil))
  43. (math-normalize (math-trig-rewrite top)))))
  44. (if (calc-is-hyperbolic)
  45. (setq top
  46. (let ((calc-simplify-mode nil))
  47. (math-normalize (math-hyperbolic-trig-rewrite top)))))
  48. (calc-with-default-simplification
  49. (calc-enter-result 1 "simp" (math-simplify top))))))
  50. (defun calc-simplify-extended ()
  51. (interactive)
  52. (calc-slow-wrapper
  53. (calc-with-default-simplification
  54. (calc-enter-result 1 "esmp" (math-simplify-extended (calc-top-n 1))))))
  55. (defun calc-expand-formula (arg)
  56. (interactive "p")
  57. (calc-slow-wrapper
  58. (calc-with-default-simplification
  59. (let ((math-simplify-only nil))
  60. (calc-modify-simplify-mode arg)
  61. (calc-enter-result 1 "expf"
  62. (if (> arg 0)
  63. (let ((math-expand-formulas t))
  64. (calc-top-n 1))
  65. (let ((top (calc-top-n 1)))
  66. (or (math-expand-formula top)
  67. top))))))))
  68. (defun calc-factor (arg)
  69. (interactive "P")
  70. (calc-slow-wrapper
  71. (calc-unary-op "fctr" (if (calc-is-hyperbolic)
  72. 'calcFunc-factors 'calcFunc-factor)
  73. arg)))
  74. (defun calc-expand (n)
  75. (interactive "P")
  76. (calc-slow-wrapper
  77. (calc-enter-result 1 "expa"
  78. (append (list 'calcFunc-expand
  79. (calc-top-n 1))
  80. (and n (list (prefix-numeric-value n)))))))
  81. ;;; Write out powers (a*b*...)^n as a*b*...*a*b*...
  82. (defun calcFunc-powerexpand (expr)
  83. (math-normalize (math-map-tree 'math-powerexpand expr)))
  84. (defun math-powerexpand (expr)
  85. (if (eq (car-safe expr) '^)
  86. (let ((n (nth 2 expr)))
  87. (cond ((and (integerp n)
  88. (> n 0))
  89. (let ((i 1)
  90. (a (nth 1 expr))
  91. (prod (nth 1 expr)))
  92. (while (< i n)
  93. (setq prod (math-mul prod a))
  94. (setq i (1+ i)))
  95. prod))
  96. ((and (integerp n)
  97. (< n 0))
  98. (let ((i -1)
  99. (a (math-pow (nth 1 expr) -1))
  100. (prod (math-pow (nth 1 expr) -1)))
  101. (while (> i n)
  102. (setq prod (math-mul a prod))
  103. (setq i (1- i)))
  104. prod))
  105. (t
  106. expr)))
  107. expr))
  108. (defun calc-powerexpand ()
  109. (interactive)
  110. (calc-slow-wrapper
  111. (calc-enter-result 1 "pexp"
  112. (calcFunc-powerexpand (calc-top-n 1)))))
  113. (defun calc-collect (&optional var)
  114. (interactive "sCollect terms involving: ")
  115. (calc-slow-wrapper
  116. (if (or (equal var "") (equal var "$") (null var))
  117. (calc-enter-result 2 "clct" (cons 'calcFunc-collect
  118. (calc-top-list-n 2)))
  119. (let ((var (math-read-expr var)))
  120. (if (eq (car-safe var) 'error)
  121. (error "Bad format in expression: %s" (nth 1 var)))
  122. (calc-enter-result 1 "clct" (list 'calcFunc-collect
  123. (calc-top-n 1)
  124. var))))))
  125. (defun calc-apart (arg)
  126. (interactive "P")
  127. (calc-slow-wrapper
  128. (calc-unary-op "aprt" 'calcFunc-apart arg)))
  129. (defun calc-normalize-rat (arg)
  130. (interactive "P")
  131. (calc-slow-wrapper
  132. (calc-unary-op "nrat" 'calcFunc-nrat arg)))
  133. (defun calc-poly-gcd (arg)
  134. (interactive "P")
  135. (calc-slow-wrapper
  136. (calc-binary-op "pgcd" 'calcFunc-pgcd arg)))
  137. (defun calc-poly-div (arg)
  138. (interactive "P")
  139. (calc-slow-wrapper
  140. (let ((calc-poly-div-remainder nil))
  141. (calc-binary-op "pdiv" 'calcFunc-pdiv arg)
  142. (if (and calc-poly-div-remainder (null arg))
  143. (progn
  144. (calc-clear-command-flag 'clear-message)
  145. (calc-record calc-poly-div-remainder "prem")
  146. (if (not (Math-zerop calc-poly-div-remainder))
  147. (message "(Remainder was %s)"
  148. (math-format-flat-expr calc-poly-div-remainder 0))
  149. (message "(No remainder)")))))))
  150. (defun calc-poly-rem (arg)
  151. (interactive "P")
  152. (calc-slow-wrapper
  153. (calc-binary-op "prem" 'calcFunc-prem arg)))
  154. (defun calc-poly-div-rem (arg)
  155. (interactive "P")
  156. (calc-slow-wrapper
  157. (if (calc-is-hyperbolic)
  158. (calc-binary-op "pdvr" 'calcFunc-pdivide arg)
  159. (calc-binary-op "pdvr" 'calcFunc-pdivrem arg))))
  160. (defun calc-substitute (&optional oldname newname)
  161. (interactive "sSubstitute old: ")
  162. (calc-slow-wrapper
  163. (let (old new (num 1) expr)
  164. (if (or (equal oldname "") (equal oldname "$") (null oldname))
  165. (setq new (calc-top-n 1)
  166. old (calc-top-n 2)
  167. expr (calc-top-n 3)
  168. num 3)
  169. (or newname
  170. (progn (calc-unread-command ?\C-a)
  171. (setq newname (read-string (concat "Substitute old: "
  172. oldname
  173. ", new: ")
  174. oldname))))
  175. (if (or (equal newname "") (equal newname "$") (null newname))
  176. (setq new (calc-top-n 1)
  177. expr (calc-top-n 2)
  178. num 2)
  179. (setq new (if (stringp newname) (math-read-expr newname) newname))
  180. (if (eq (car-safe new) 'error)
  181. (error "Bad format in expression: %s" (nth 1 new)))
  182. (setq expr (calc-top-n 1)))
  183. (setq old (if (stringp oldname) (math-read-expr oldname) oldname))
  184. (if (eq (car-safe old) 'error)
  185. (error "Bad format in expression: %s" (nth 1 old)))
  186. (or (math-expr-contains expr old)
  187. (error "No occurrences found")))
  188. (calc-enter-result num "sbst" (math-expr-subst expr old new)))))
  189. (defun calc-has-rules (name)
  190. (setq name (calc-var-value name))
  191. (and (consp name)
  192. (memq (car name) '(vec calcFunc-assign calcFunc-condition))
  193. name))
  194. ;; math-eval-rules-cache and math-eval-rules-cache-other are
  195. ;; declared in calc.el, but are used here by math-recompile-eval-rules.
  196. (defvar math-eval-rules-cache)
  197. (defvar math-eval-rules-cache-other)
  198. (defun math-recompile-eval-rules ()
  199. (setq math-eval-rules-cache (and (calc-has-rules 'var-EvalRules)
  200. (math-compile-rewrites
  201. '(var EvalRules var-EvalRules)))
  202. math-eval-rules-cache-other (assq nil math-eval-rules-cache)
  203. math-eval-rules-cache-tag (calc-var-value 'var-EvalRules)))
  204. ;;; Try to expand a formula according to its definition.
  205. (defun math-expand-formula (expr)
  206. (and (consp expr)
  207. (symbolp (car expr))
  208. (or (get (car expr) 'calc-user-defn)
  209. (get (car expr) 'math-expandable))
  210. (let ((res (let ((math-expand-formulas t))
  211. (apply (car expr) (cdr expr)))))
  212. (and (not (eq (car-safe res) (car expr)))
  213. res))))
  214. ;;; True if A comes before B in a canonical ordering of expressions. [P X X]
  215. (defun math-beforep (a b) ; [Public]
  216. (cond ((and (Math-realp a) (Math-realp b))
  217. (let ((comp (math-compare a b)))
  218. (or (eq comp -1)
  219. (and (eq comp 0)
  220. (not (equal a b))
  221. (> (length (memq (car-safe a)
  222. '(bigneg nil bigpos frac float)))
  223. (length (memq (car-safe b)
  224. '(bigneg nil bigpos frac float))))))))
  225. ((equal b '(neg (var inf var-inf))) nil)
  226. ((equal a '(neg (var inf var-inf))) t)
  227. ((equal a '(var inf var-inf)) nil)
  228. ((equal b '(var inf var-inf)) t)
  229. ((Math-realp a)
  230. (if (and (eq (car-safe b) 'intv) (math-intv-constp b))
  231. (if (or (math-beforep a (nth 2 b)) (Math-equal a (nth 2 b)))
  232. t
  233. nil)
  234. t))
  235. ((Math-realp b)
  236. (if (and (eq (car-safe a) 'intv) (math-intv-constp a))
  237. (if (math-beforep (nth 2 a) b)
  238. t
  239. nil)
  240. nil))
  241. ((and (eq (car a) 'intv) (eq (car b) 'intv)
  242. (math-intv-constp a) (math-intv-constp b))
  243. (let ((comp (math-compare (nth 2 a) (nth 2 b))))
  244. (cond ((eq comp -1) t)
  245. ((eq comp 1) nil)
  246. ((and (memq (nth 1 a) '(2 3)) (memq (nth 1 b) '(0 1))) t)
  247. ((and (memq (nth 1 a) '(0 1)) (memq (nth 1 b) '(2 3))) nil)
  248. ((eq (setq comp (math-compare (nth 3 a) (nth 3 b))) -1) t)
  249. ((eq comp 1) nil)
  250. ((and (memq (nth 1 a) '(0 2)) (memq (nth 1 b) '(1 3))) t)
  251. (t nil))))
  252. ((not (eq (not (Math-objectp a)) (not (Math-objectp b))))
  253. (Math-objectp a))
  254. ((eq (car a) 'var)
  255. (if (eq (car b) 'var)
  256. (string-lessp (symbol-name (nth 1 a)) (symbol-name (nth 1 b)))
  257. (not (Math-numberp b))))
  258. ((eq (car b) 'var) (Math-numberp a))
  259. ((eq (car a) (car b))
  260. (while (and (setq a (cdr a) b (cdr b)) a
  261. (equal (car a) (car b))))
  262. (and b
  263. (or (null a)
  264. (math-beforep (car a) (car b)))))
  265. (t (string-lessp (symbol-name (car a)) (symbol-name (car b))))))
  266. (defsubst math-simplify-extended (a)
  267. (let ((math-living-dangerously t))
  268. (math-simplify a)))
  269. (defalias 'calcFunc-esimplify 'math-simplify-extended)
  270. ;;; Rewrite the trig functions in a form easier to simplify.
  271. (defun math-trig-rewrite (fn)
  272. "Rewrite trigonometric functions in terms of sines and cosines."
  273. (cond
  274. ((not (consp fn))
  275. fn)
  276. ((eq (car-safe fn) 'calcFunc-sec)
  277. (list '/ 1 (cons 'calcFunc-cos (math-trig-rewrite (cdr fn)))))
  278. ((eq (car-safe fn) 'calcFunc-csc)
  279. (list '/ 1 (cons 'calcFunc-sin (math-trig-rewrite (cdr fn)))))
  280. ((eq (car-safe fn) 'calcFunc-tan)
  281. (let ((newfn (math-trig-rewrite (cdr fn))))
  282. (list '/ (cons 'calcFunc-sin newfn)
  283. (cons 'calcFunc-cos newfn))))
  284. ((eq (car-safe fn) 'calcFunc-cot)
  285. (let ((newfn (math-trig-rewrite (cdr fn))))
  286. (list '/ (cons 'calcFunc-cos newfn)
  287. (cons 'calcFunc-sin newfn))))
  288. (t
  289. (mapcar 'math-trig-rewrite fn))))
  290. (defun math-hyperbolic-trig-rewrite (fn)
  291. "Rewrite hyperbolic functions in terms of sinhs and coshs."
  292. (cond
  293. ((not (consp fn))
  294. fn)
  295. ((eq (car-safe fn) 'calcFunc-sech)
  296. (list '/ 1 (cons 'calcFunc-cosh (math-hyperbolic-trig-rewrite (cdr fn)))))
  297. ((eq (car-safe fn) 'calcFunc-csch)
  298. (list '/ 1 (cons 'calcFunc-sinh (math-hyperbolic-trig-rewrite (cdr fn)))))
  299. ((eq (car-safe fn) 'calcFunc-tanh)
  300. (let ((newfn (math-hyperbolic-trig-rewrite (cdr fn))))
  301. (list '/ (cons 'calcFunc-sinh newfn)
  302. (cons 'calcFunc-cosh newfn))))
  303. ((eq (car-safe fn) 'calcFunc-coth)
  304. (let ((newfn (math-hyperbolic-trig-rewrite (cdr fn))))
  305. (list '/ (cons 'calcFunc-cosh newfn)
  306. (cons 'calcFunc-sinh newfn))))
  307. (t
  308. (mapcar 'math-hyperbolic-trig-rewrite fn))))
  309. ;; math-top-only is local to math-simplify, but is used by
  310. ;; math-simplify-step, which is called by math-simplify.
  311. (defvar math-top-only)
  312. (defun math-simplify (top-expr)
  313. (let ((math-simplifying t)
  314. (math-top-only (consp calc-simplify-mode))
  315. (simp-rules (append (and (calc-has-rules 'var-AlgSimpRules)
  316. '((var AlgSimpRules var-AlgSimpRules)))
  317. (and math-living-dangerously
  318. (calc-has-rules 'var-ExtSimpRules)
  319. '((var ExtSimpRules var-ExtSimpRules)))
  320. (and math-simplifying-units
  321. (calc-has-rules 'var-UnitSimpRules)
  322. '((var UnitSimpRules var-UnitSimpRules)))
  323. (and math-integrating
  324. (calc-has-rules 'var-IntegSimpRules)
  325. '((var IntegSimpRules var-IntegSimpRules)))))
  326. res)
  327. (if math-top-only
  328. (let ((r simp-rules))
  329. (setq res (math-simplify-step (math-normalize top-expr))
  330. calc-simplify-mode '(nil)
  331. top-expr (math-normalize res))
  332. (while r
  333. (setq top-expr (math-rewrite top-expr (car r)
  334. '(neg (var inf var-inf)))
  335. r (cdr r))))
  336. (calc-with-default-simplification
  337. (while (let ((r simp-rules))
  338. (setq res (math-normalize top-expr))
  339. (while r
  340. (setq res (math-rewrite res (car r))
  341. r (cdr r)))
  342. (not (equal top-expr (setq res (math-simplify-step res)))))
  343. (setq top-expr res)))))
  344. top-expr)
  345. (defalias 'calcFunc-simplify 'math-simplify)
  346. ;;; The following has a "bug" in that if any recursive simplifications
  347. ;;; occur only the first handler will be tried; this doesn't really
  348. ;;; matter, since math-simplify-step is iterated to a fixed point anyway.
  349. (defun math-simplify-step (a)
  350. (if (Math-primp a)
  351. a
  352. (let ((aa (if (or math-top-only
  353. (memq (car a) '(calcFunc-quote calcFunc-condition
  354. calcFunc-evalto)))
  355. a
  356. (cons (car a) (mapcar 'math-simplify-step (cdr a))))))
  357. (and (symbolp (car aa))
  358. (let ((handler (get (car aa) 'math-simplify)))
  359. (and handler
  360. (while (and handler
  361. (equal (setq aa (or (funcall (car handler) aa)
  362. aa))
  363. a))
  364. (setq handler (cdr handler))))))
  365. aa)))
  366. (defmacro math-defsimplify (funcs &rest code)
  367. (cons 'progn
  368. (mapcar #'(lambda (func)
  369. `(put ',func 'math-simplify
  370. (nconc
  371. (get ',func 'math-simplify)
  372. (list
  373. #'(lambda (math-simplify-expr) ,@code)))))
  374. (if (symbolp funcs) (list funcs) funcs))))
  375. (put 'math-defsimplify 'lisp-indent-hook 1)
  376. ;; The function created by math-defsimplify uses the variable
  377. ;; math-simplify-expr, and so is used by functions in math-defsimplify
  378. (defvar math-simplify-expr)
  379. (math-defsimplify (+ -)
  380. (math-simplify-plus))
  381. (defun math-simplify-plus ()
  382. (cond ((and (memq (car-safe (nth 1 math-simplify-expr)) '(+ -))
  383. (Math-numberp (nth 2 (nth 1 math-simplify-expr)))
  384. (not (Math-numberp (nth 2 math-simplify-expr))))
  385. (let ((x (nth 2 math-simplify-expr))
  386. (op (car math-simplify-expr)))
  387. (setcar (cdr (cdr math-simplify-expr)) (nth 2 (nth 1 math-simplify-expr)))
  388. (setcar math-simplify-expr (car (nth 1 math-simplify-expr)))
  389. (setcar (cdr (cdr (nth 1 math-simplify-expr))) x)
  390. (setcar (nth 1 math-simplify-expr) op)))
  391. ((and (eq (car math-simplify-expr) '+)
  392. (Math-numberp (nth 1 math-simplify-expr))
  393. (not (Math-numberp (nth 2 math-simplify-expr))))
  394. (let ((x (nth 2 math-simplify-expr)))
  395. (setcar (cdr (cdr math-simplify-expr)) (nth 1 math-simplify-expr))
  396. (setcar (cdr math-simplify-expr) x))))
  397. (let ((aa math-simplify-expr)
  398. aaa temp)
  399. (while (memq (car-safe (setq aaa (nth 1 aa))) '(+ -))
  400. (if (setq temp (math-combine-sum (nth 2 aaa) (nth 2 math-simplify-expr)
  401. (eq (car aaa) '-)
  402. (eq (car math-simplify-expr) '-) t))
  403. (progn
  404. (setcar (cdr (cdr math-simplify-expr)) temp)
  405. (setcar math-simplify-expr '+)
  406. (setcar (cdr (cdr aaa)) 0)))
  407. (setq aa (nth 1 aa)))
  408. (if (setq temp (math-combine-sum aaa (nth 2 math-simplify-expr)
  409. nil (eq (car math-simplify-expr) '-) t))
  410. (progn
  411. (setcar (cdr (cdr math-simplify-expr)) temp)
  412. (setcar math-simplify-expr '+)
  413. (setcar (cdr aa) 0)))
  414. math-simplify-expr))
  415. (math-defsimplify *
  416. (math-simplify-times))
  417. (defun math-simplify-times ()
  418. (if (eq (car-safe (nth 2 math-simplify-expr)) '*)
  419. (and (math-beforep (nth 1 (nth 2 math-simplify-expr)) (nth 1 math-simplify-expr))
  420. (or (math-known-scalarp (nth 1 math-simplify-expr) t)
  421. (math-known-scalarp (nth 1 (nth 2 math-simplify-expr)) t))
  422. (let ((x (nth 1 math-simplify-expr)))
  423. (setcar (cdr math-simplify-expr) (nth 1 (nth 2 math-simplify-expr)))
  424. (setcar (cdr (nth 2 math-simplify-expr)) x)))
  425. (and (math-beforep (nth 2 math-simplify-expr) (nth 1 math-simplify-expr))
  426. (or (math-known-scalarp (nth 1 math-simplify-expr) t)
  427. (math-known-scalarp (nth 2 math-simplify-expr) t))
  428. (let ((x (nth 2 math-simplify-expr)))
  429. (setcar (cdr (cdr math-simplify-expr)) (nth 1 math-simplify-expr))
  430. (setcar (cdr math-simplify-expr) x))))
  431. (let ((aa math-simplify-expr)
  432. aaa temp
  433. (safe t) (scalar (math-known-scalarp (nth 1 math-simplify-expr))))
  434. (if (and (Math-ratp (nth 1 math-simplify-expr))
  435. (setq temp (math-common-constant-factor (nth 2 math-simplify-expr))))
  436. (progn
  437. (setcar (cdr (cdr math-simplify-expr))
  438. (math-cancel-common-factor (nth 2 math-simplify-expr) temp))
  439. (setcar (cdr math-simplify-expr) (math-mul (nth 1 math-simplify-expr) temp))))
  440. (while (and (eq (car-safe (setq aaa (nth 2 aa))) '*)
  441. safe)
  442. (if (setq temp (math-combine-prod (nth 1 math-simplify-expr)
  443. (nth 1 aaa) nil nil t))
  444. (progn
  445. (setcar (cdr math-simplify-expr) temp)
  446. (setcar (cdr aaa) 1)))
  447. (setq safe (or scalar (math-known-scalarp (nth 1 aaa) t))
  448. aa (nth 2 aa)))
  449. (if (and (setq temp (math-combine-prod aaa (nth 1 math-simplify-expr) nil nil t))
  450. safe)
  451. (progn
  452. (setcar (cdr math-simplify-expr) temp)
  453. (setcar (cdr (cdr aa)) 1)))
  454. (if (and (eq (car-safe (nth 1 math-simplify-expr)) 'frac)
  455. (memq (nth 1 (nth 1 math-simplify-expr)) '(1 -1)))
  456. (math-div (math-mul (nth 2 math-simplify-expr)
  457. (nth 1 (nth 1 math-simplify-expr)))
  458. (nth 2 (nth 1 math-simplify-expr)))
  459. math-simplify-expr)))
  460. (math-defsimplify /
  461. (math-simplify-divide))
  462. (defun math-simplify-divide ()
  463. (let ((np (cdr math-simplify-expr))
  464. (nover nil)
  465. (nn (and (or (eq (car math-simplify-expr) '/)
  466. (not (Math-realp (nth 2 math-simplify-expr))))
  467. (math-common-constant-factor (nth 2 math-simplify-expr))))
  468. n op)
  469. (if nn
  470. (progn
  471. (setq n (and (or (eq (car math-simplify-expr) '/)
  472. (not (Math-realp (nth 1 math-simplify-expr))))
  473. (math-common-constant-factor (nth 1 math-simplify-expr))))
  474. (if (and (eq (car-safe nn) 'frac) (eq (nth 1 nn) 1) (not n))
  475. (progn
  476. (setcar (cdr math-simplify-expr)
  477. (math-mul (nth 2 nn) (nth 1 math-simplify-expr)))
  478. (setcar (cdr (cdr math-simplify-expr))
  479. (math-cancel-common-factor (nth 2 math-simplify-expr) nn))
  480. (if (and (math-negp nn)
  481. (setq op (assq (car math-simplify-expr) calc-tweak-eqn-table)))
  482. (setcar math-simplify-expr (nth 1 op))))
  483. (if (and n (not (eq (setq n (math-frac-gcd n nn)) 1)))
  484. (progn
  485. (setcar (cdr math-simplify-expr)
  486. (math-cancel-common-factor (nth 1 math-simplify-expr) n))
  487. (setcar (cdr (cdr math-simplify-expr))
  488. (math-cancel-common-factor (nth 2 math-simplify-expr) n))
  489. (if (and (math-negp n)
  490. (setq op (assq (car math-simplify-expr)
  491. calc-tweak-eqn-table)))
  492. (setcar math-simplify-expr (nth 1 op))))))))
  493. (if (and (eq (car-safe (car np)) '/)
  494. (math-known-scalarp (nth 2 math-simplify-expr) t))
  495. (progn
  496. (setq np (cdr (nth 1 math-simplify-expr)))
  497. (while (eq (car-safe (setq n (car np))) '*)
  498. (and (math-known-scalarp (nth 2 n) t)
  499. (math-simplify-divisor (cdr n) (cdr (cdr math-simplify-expr)) nil t))
  500. (setq np (cdr (cdr n))))
  501. (math-simplify-divisor np (cdr (cdr math-simplify-expr)) nil t)
  502. (setq nover t
  503. np (cdr (cdr (nth 1 math-simplify-expr))))))
  504. (while (eq (car-safe (setq n (car np))) '*)
  505. (and (math-known-scalarp (nth 2 n) t)
  506. (math-simplify-divisor (cdr n) (cdr (cdr math-simplify-expr)) nover t))
  507. (setq np (cdr (cdr n))))
  508. (math-simplify-divisor np (cdr (cdr math-simplify-expr)) nover t)
  509. math-simplify-expr))
  510. ;; The variables math-simplify-divisor-nover and math-simplify-divisor-dover
  511. ;; are local variables for math-simplify-divisor, but are used by
  512. ;; math-simplify-one-divisor.
  513. (defvar math-simplify-divisor-nover)
  514. (defvar math-simplify-divisor-dover)
  515. (defun math-simplify-divisor (np dp math-simplify-divisor-nover
  516. math-simplify-divisor-dover)
  517. (cond ((eq (car-safe (car dp)) '/)
  518. (math-simplify-divisor np (cdr (car dp))
  519. math-simplify-divisor-nover
  520. math-simplify-divisor-dover)
  521. (and (math-known-scalarp (nth 1 (car dp)) t)
  522. (math-simplify-divisor np (cdr (cdr (car dp)))
  523. math-simplify-divisor-nover
  524. (not math-simplify-divisor-dover))))
  525. ((or (or (eq (car math-simplify-expr) '/)
  526. (let ((signs (math-possible-signs (car np))))
  527. (or (memq signs '(1 4))
  528. (and (memq (car math-simplify-expr) '(calcFunc-eq calcFunc-neq))
  529. (eq signs 5))
  530. math-living-dangerously)))
  531. (math-numberp (car np)))
  532. (let (d
  533. (safe t)
  534. (scalar (math-known-scalarp (car np))))
  535. (while (and (eq (car-safe (setq d (car dp))) '*)
  536. safe)
  537. (math-simplify-one-divisor np (cdr d))
  538. (setq safe (or scalar (math-known-scalarp (nth 1 d) t))
  539. dp (cdr (cdr d))))
  540. (if safe
  541. (math-simplify-one-divisor np dp))))))
  542. (defun math-simplify-one-divisor (np dp)
  543. (let ((temp (math-combine-prod (car np) (car dp) math-simplify-divisor-nover
  544. math-simplify-divisor-dover t))
  545. op)
  546. (if temp
  547. (progn
  548. (and (not (memq (car math-simplify-expr) '(/ calcFunc-eq calcFunc-neq)))
  549. (math-known-negp (car dp))
  550. (setq op (assq (car math-simplify-expr) calc-tweak-eqn-table))
  551. (setcar math-simplify-expr (nth 1 op)))
  552. (setcar np (if math-simplify-divisor-nover (math-div 1 temp) temp))
  553. (setcar dp 1))
  554. (and math-simplify-divisor-dover (not math-simplify-divisor-nover)
  555. (eq (car math-simplify-expr) '/)
  556. (eq (car-safe (car dp)) 'calcFunc-sqrt)
  557. (Math-integerp (nth 1 (car dp)))
  558. (progn
  559. (setcar np (math-mul (car np)
  560. (list 'calcFunc-sqrt (nth 1 (car dp)))))
  561. (setcar dp (nth 1 (car dp))))))))
  562. (defun math-common-constant-factor (expr)
  563. (if (Math-realp expr)
  564. (if (Math-ratp expr)
  565. (and (not (memq expr '(0 1 -1)))
  566. (math-abs expr))
  567. (if (math-ratp (setq expr (math-to-simple-fraction expr)))
  568. (math-common-constant-factor expr)))
  569. (if (memq (car expr) '(+ - cplx sdev))
  570. (let ((f1 (math-common-constant-factor (nth 1 expr)))
  571. (f2 (math-common-constant-factor (nth 2 expr))))
  572. (and f1 f2
  573. (not (eq (setq f1 (math-frac-gcd f1 f2)) 1))
  574. f1))
  575. (if (memq (car expr) '(* polar))
  576. (math-common-constant-factor (nth 1 expr))
  577. (if (eq (car expr) '/)
  578. (or (math-common-constant-factor (nth 1 expr))
  579. (and (Math-integerp (nth 2 expr))
  580. (list 'frac 1 (math-abs (nth 2 expr))))))))))
  581. (defun math-cancel-common-factor (expr val)
  582. (if (memq (car-safe expr) '(+ - cplx sdev))
  583. (progn
  584. (setcar (cdr expr) (math-cancel-common-factor (nth 1 expr) val))
  585. (setcar (cdr (cdr expr)) (math-cancel-common-factor (nth 2 expr) val))
  586. expr)
  587. (if (eq (car-safe expr) '*)
  588. (math-mul (math-cancel-common-factor (nth 1 expr) val) (nth 2 expr))
  589. (math-div expr val))))
  590. (defun math-frac-gcd (a b)
  591. (if (Math-zerop a)
  592. b
  593. (if (Math-zerop b)
  594. a
  595. (if (and (Math-integerp a)
  596. (Math-integerp b))
  597. (math-gcd a b)
  598. (and (Math-integerp a) (setq a (list 'frac a 1)))
  599. (and (Math-integerp b) (setq b (list 'frac b 1)))
  600. (math-make-frac (math-gcd (nth 1 a) (nth 1 b))
  601. (math-gcd (nth 2 a) (nth 2 b)))))))
  602. (math-defsimplify %
  603. (math-simplify-mod))
  604. (defun math-simplify-mod ()
  605. (and (Math-realp (nth 2 math-simplify-expr))
  606. (Math-posp (nth 2 math-simplify-expr))
  607. (let ((lin (math-is-linear (nth 1 math-simplify-expr)))
  608. t1 t2 t3)
  609. (or (and lin
  610. (or (math-negp (car lin))
  611. (not (Math-lessp (car lin) (nth 2 math-simplify-expr))))
  612. (list '%
  613. (list '+
  614. (math-mul (nth 1 lin) (nth 2 lin))
  615. (math-mod (car lin) (nth 2 math-simplify-expr)))
  616. (nth 2 math-simplify-expr)))
  617. (and lin
  618. (not (math-equal-int (nth 1 lin) 1))
  619. (math-num-integerp (nth 1 lin))
  620. (math-num-integerp (nth 2 math-simplify-expr))
  621. (setq t1 (calcFunc-gcd (nth 1 lin) (nth 2 math-simplify-expr)))
  622. (not (math-equal-int t1 1))
  623. (list '*
  624. t1
  625. (list '%
  626. (list '+
  627. (math-mul (math-div (nth 1 lin) t1)
  628. (nth 2 lin))
  629. (let ((calc-prefer-frac t))
  630. (math-div (car lin) t1)))
  631. (math-div (nth 2 math-simplify-expr) t1))))
  632. (and (math-equal-int (nth 2 math-simplify-expr) 1)
  633. (math-known-integerp (if lin
  634. (math-mul (nth 1 lin) (nth 2 lin))
  635. (nth 1 math-simplify-expr)))
  636. (if lin (math-mod (car lin) 1) 0))))))
  637. (math-defsimplify (calcFunc-eq calcFunc-neq calcFunc-lt
  638. calcFunc-gt calcFunc-leq calcFunc-geq)
  639. (if (= (length math-simplify-expr) 3)
  640. (math-simplify-ineq)))
  641. (defun math-simplify-ineq ()
  642. (let ((np (cdr math-simplify-expr))
  643. n)
  644. (while (memq (car-safe (setq n (car np))) '(+ -))
  645. (math-simplify-add-term (cdr (cdr n)) (cdr (cdr math-simplify-expr))
  646. (eq (car n) '-) nil)
  647. (setq np (cdr n)))
  648. (math-simplify-add-term np (cdr (cdr math-simplify-expr)) nil
  649. (eq np (cdr math-simplify-expr)))
  650. (math-simplify-divide)
  651. (let ((signs (math-possible-signs (cons '- (cdr math-simplify-expr)))))
  652. (or (cond ((eq (car math-simplify-expr) 'calcFunc-eq)
  653. (or (and (eq signs 2) 1)
  654. (and (memq signs '(1 4 5)) 0)))
  655. ((eq (car math-simplify-expr) 'calcFunc-neq)
  656. (or (and (eq signs 2) 0)
  657. (and (memq signs '(1 4 5)) 1)))
  658. ((eq (car math-simplify-expr) 'calcFunc-lt)
  659. (or (and (eq signs 1) 1)
  660. (and (memq signs '(2 4 6)) 0)))
  661. ((eq (car math-simplify-expr) 'calcFunc-gt)
  662. (or (and (eq signs 4) 1)
  663. (and (memq signs '(1 2 3)) 0)))
  664. ((eq (car math-simplify-expr) 'calcFunc-leq)
  665. (or (and (eq signs 4) 0)
  666. (and (memq signs '(1 2 3)) 1)))
  667. ((eq (car math-simplify-expr) 'calcFunc-geq)
  668. (or (and (eq signs 1) 0)
  669. (and (memq signs '(2 4 6)) 1))))
  670. math-simplify-expr))))
  671. (defun math-simplify-add-term (np dp minus lplain)
  672. (or (math-vectorp (car np))
  673. (let ((rplain t)
  674. n d dd temp)
  675. (while (memq (car-safe (setq n (car np) d (car dp))) '(+ -))
  676. (setq rplain nil)
  677. (if (setq temp (math-combine-sum n (nth 2 d)
  678. minus (eq (car d) '+) t))
  679. (if (or lplain (eq (math-looks-negp temp) minus))
  680. (progn
  681. (setcar np (setq n (if minus (math-neg temp) temp)))
  682. (setcar (cdr (cdr d)) 0))
  683. (progn
  684. (setcar np 0)
  685. (setcar (cdr (cdr d)) (setq n (if (eq (car d) '+)
  686. (math-neg temp)
  687. temp))))))
  688. (setq dp (cdr d)))
  689. (if (setq temp (math-combine-sum n d minus t t))
  690. (if (or lplain
  691. (and (not rplain)
  692. (eq (math-looks-negp temp) minus)))
  693. (progn
  694. (setcar np (setq n (if minus (math-neg temp) temp)))
  695. (setcar dp 0))
  696. (progn
  697. (setcar np 0)
  698. (setcar dp (setq n (math-neg temp)))))))))
  699. (math-defsimplify calcFunc-sin
  700. (or (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsin)
  701. (nth 1 (nth 1 math-simplify-expr)))
  702. (and (math-looks-negp (nth 1 math-simplify-expr))
  703. (math-neg (list 'calcFunc-sin (math-neg (nth 1 math-simplify-expr)))))
  704. (and (eq calc-angle-mode 'rad)
  705. (let ((n (math-linear-in (nth 1 math-simplify-expr) '(var pi var-pi))))
  706. (and n
  707. (math-known-sin (car n) (nth 1 n) 120 0))))
  708. (and (eq calc-angle-mode 'deg)
  709. (let ((n (math-integer-plus (nth 1 math-simplify-expr))))
  710. (and n
  711. (math-known-sin (car n) (nth 1 n) '(frac 2 3) 0))))
  712. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arccos)
  713. (list 'calcFunc-sqrt (math-sub 1 (math-sqr
  714. (nth 1 (nth 1 math-simplify-expr))))))
  715. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arctan)
  716. (math-div (nth 1 (nth 1 math-simplify-expr))
  717. (list 'calcFunc-sqrt
  718. (math-add 1 (math-sqr
  719. (nth 1 (nth 1 math-simplify-expr)))))))
  720. (let ((m (math-should-expand-trig (nth 1 math-simplify-expr))))
  721. (and m (integerp (car m))
  722. (let ((n (car m)) (a (nth 1 m)))
  723. (list '+
  724. (list '* (list 'calcFunc-sin (list '* (1- n) a))
  725. (list 'calcFunc-cos a))
  726. (list '* (list 'calcFunc-cos (list '* (1- n) a))
  727. (list 'calcFunc-sin a))))))))
  728. (math-defsimplify calcFunc-cos
  729. (or (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arccos)
  730. (nth 1 (nth 1 math-simplify-expr)))
  731. (and (math-looks-negp (nth 1 math-simplify-expr))
  732. (list 'calcFunc-cos (math-neg (nth 1 math-simplify-expr))))
  733. (and (eq calc-angle-mode 'rad)
  734. (let ((n (math-linear-in (nth 1 math-simplify-expr) '(var pi var-pi))))
  735. (and n
  736. (math-known-sin (car n) (nth 1 n) 120 300))))
  737. (and (eq calc-angle-mode 'deg)
  738. (let ((n (math-integer-plus (nth 1 math-simplify-expr))))
  739. (and n
  740. (math-known-sin (car n) (nth 1 n) '(frac 2 3) 300))))
  741. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsin)
  742. (list 'calcFunc-sqrt
  743. (math-sub 1 (math-sqr (nth 1 (nth 1 math-simplify-expr))))))
  744. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arctan)
  745. (math-div 1
  746. (list 'calcFunc-sqrt
  747. (math-add 1
  748. (math-sqr (nth 1 (nth 1 math-simplify-expr)))))))
  749. (let ((m (math-should-expand-trig (nth 1 math-simplify-expr))))
  750. (and m (integerp (car m))
  751. (let ((n (car m)) (a (nth 1 m)))
  752. (list '-
  753. (list '* (list 'calcFunc-cos (list '* (1- n) a))
  754. (list 'calcFunc-cos a))
  755. (list '* (list 'calcFunc-sin (list '* (1- n) a))
  756. (list 'calcFunc-sin a))))))))
  757. (math-defsimplify calcFunc-sec
  758. (or (and (math-looks-negp (nth 1 math-simplify-expr))
  759. (list 'calcFunc-sec (math-neg (nth 1 math-simplify-expr))))
  760. (and (eq calc-angle-mode 'rad)
  761. (let ((n (math-linear-in (nth 1 math-simplify-expr) '(var pi var-pi))))
  762. (and n
  763. (math-div 1 (math-known-sin (car n) (nth 1 n) 120 300)))))
  764. (and (eq calc-angle-mode 'deg)
  765. (let ((n (math-integer-plus (nth 1 math-simplify-expr))))
  766. (and n
  767. (math-div 1 (math-known-sin (car n) (nth 1 n) '(frac 2 3) 300)))))
  768. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsin)
  769. (math-div
  770. 1
  771. (list 'calcFunc-sqrt
  772. (math-sub 1 (math-sqr (nth 1 (nth 1 math-simplify-expr)))))))
  773. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arccos)
  774. (math-div
  775. 1
  776. (nth 1 (nth 1 math-simplify-expr))))
  777. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arctan)
  778. (list 'calcFunc-sqrt
  779. (math-add 1
  780. (math-sqr (nth 1 (nth 1 math-simplify-expr))))))))
  781. (math-defsimplify calcFunc-csc
  782. (or (and (math-looks-negp (nth 1 math-simplify-expr))
  783. (math-neg (list 'calcFunc-csc (math-neg (nth 1 math-simplify-expr)))))
  784. (and (eq calc-angle-mode 'rad)
  785. (let ((n (math-linear-in (nth 1 math-simplify-expr) '(var pi var-pi))))
  786. (and n
  787. (math-div 1 (math-known-sin (car n) (nth 1 n) 120 0)))))
  788. (and (eq calc-angle-mode 'deg)
  789. (let ((n (math-integer-plus (nth 1 math-simplify-expr))))
  790. (and n
  791. (math-div 1 (math-known-sin (car n) (nth 1 n) '(frac 2 3) 0)))))
  792. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsin)
  793. (math-div 1 (nth 1 (nth 1 math-simplify-expr))))
  794. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arccos)
  795. (math-div
  796. 1
  797. (list 'calcFunc-sqrt (math-sub 1 (math-sqr
  798. (nth 1 (nth 1 math-simplify-expr)))))))
  799. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arctan)
  800. (math-div (list 'calcFunc-sqrt
  801. (math-add 1 (math-sqr
  802. (nth 1 (nth 1 math-simplify-expr)))))
  803. (nth 1 (nth 1 math-simplify-expr))))))
  804. (defun math-should-expand-trig (x &optional hyperbolic)
  805. (let ((m (math-is-multiple x)))
  806. (and math-living-dangerously
  807. m (or (and (integerp (car m)) (> (car m) 1))
  808. (equal (car m) '(frac 1 2)))
  809. (or math-integrating
  810. (memq (car-safe (nth 1 m))
  811. (if hyperbolic
  812. '(calcFunc-arcsinh calcFunc-arccosh calcFunc-arctanh)
  813. '(calcFunc-arcsin calcFunc-arccos calcFunc-arctan)))
  814. (and (eq (car-safe (nth 1 m)) 'calcFunc-ln)
  815. (eq hyperbolic 'exp)))
  816. m)))
  817. (defun math-known-sin (plus n mul off)
  818. (setq n (math-mul n mul))
  819. (and (math-num-integerp n)
  820. (setq n (math-mod (math-add (math-trunc n) off) 240))
  821. (if (>= n 120)
  822. (and (setq n (math-known-sin plus (- n 120) 1 0))
  823. (math-neg n))
  824. (if (> n 60)
  825. (setq n (- 120 n)))
  826. (if (math-zerop plus)
  827. (and (or calc-symbolic-mode
  828. (memq n '(0 20 60)))
  829. (cdr (assq n
  830. '( (0 . 0)
  831. (10 . (/ (calcFunc-sqrt
  832. (- 2 (calcFunc-sqrt 3))) 2))
  833. (12 . (/ (- (calcFunc-sqrt 5) 1) 4))
  834. (15 . (/ (calcFunc-sqrt
  835. (- 2 (calcFunc-sqrt 2))) 2))
  836. (20 . (/ 1 2))
  837. (24 . (* (^ (/ 1 2) (/ 3 2))
  838. (calcFunc-sqrt
  839. (- 5 (calcFunc-sqrt 5)))))
  840. (30 . (/ (calcFunc-sqrt 2) 2))
  841. (36 . (/ (+ (calcFunc-sqrt 5) 1) 4))
  842. (40 . (/ (calcFunc-sqrt 3) 2))
  843. (45 . (/ (calcFunc-sqrt
  844. (+ 2 (calcFunc-sqrt 2))) 2))
  845. (48 . (* (^ (/ 1 2) (/ 3 2))
  846. (calcFunc-sqrt
  847. (+ 5 (calcFunc-sqrt 5)))))
  848. (50 . (/ (calcFunc-sqrt
  849. (+ 2 (calcFunc-sqrt 3))) 2))
  850. (60 . 1)))))
  851. (cond ((eq n 0) (math-normalize (list 'calcFunc-sin plus)))
  852. ((eq n 60) (math-normalize (list 'calcFunc-cos plus)))
  853. (t nil))))))
  854. (math-defsimplify calcFunc-tan
  855. (or (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arctan)
  856. (nth 1 (nth 1 math-simplify-expr)))
  857. (and (math-looks-negp (nth 1 math-simplify-expr))
  858. (math-neg (list 'calcFunc-tan (math-neg (nth 1 math-simplify-expr)))))
  859. (and (eq calc-angle-mode 'rad)
  860. (let ((n (math-linear-in (nth 1 math-simplify-expr) '(var pi var-pi))))
  861. (and n
  862. (math-known-tan (car n) (nth 1 n) 120))))
  863. (and (eq calc-angle-mode 'deg)
  864. (let ((n (math-integer-plus (nth 1 math-simplify-expr))))
  865. (and n
  866. (math-known-tan (car n) (nth 1 n) '(frac 2 3)))))
  867. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsin)
  868. (math-div (nth 1 (nth 1 math-simplify-expr))
  869. (list 'calcFunc-sqrt
  870. (math-sub 1 (math-sqr (nth 1 (nth 1 math-simplify-expr)))))))
  871. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arccos)
  872. (math-div (list 'calcFunc-sqrt
  873. (math-sub 1 (math-sqr (nth 1 (nth 1 math-simplify-expr)))))
  874. (nth 1 (nth 1 math-simplify-expr))))
  875. (let ((m (math-should-expand-trig (nth 1 math-simplify-expr))))
  876. (and m
  877. (if (equal (car m) '(frac 1 2))
  878. (math-div (math-sub 1 (list 'calcFunc-cos (nth 1 m)))
  879. (list 'calcFunc-sin (nth 1 m)))
  880. (math-div (list 'calcFunc-sin (nth 1 math-simplify-expr))
  881. (list 'calcFunc-cos (nth 1 math-simplify-expr))))))))
  882. (math-defsimplify calcFunc-cot
  883. (or (and (math-looks-negp (nth 1 math-simplify-expr))
  884. (math-neg (list 'calcFunc-cot (math-neg (nth 1 math-simplify-expr)))))
  885. (and (eq calc-angle-mode 'rad)
  886. (let ((n (math-linear-in (nth 1 math-simplify-expr) '(var pi var-pi))))
  887. (and n
  888. (math-div 1 (math-known-tan (car n) (nth 1 n) 120)))))
  889. (and (eq calc-angle-mode 'deg)
  890. (let ((n (math-integer-plus (nth 1 math-simplify-expr))))
  891. (and n
  892. (math-div 1 (math-known-tan (car n) (nth 1 n) '(frac 2 3))))))
  893. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsin)
  894. (math-div (list 'calcFunc-sqrt
  895. (math-sub 1 (math-sqr (nth 1 (nth 1 math-simplify-expr)))))
  896. (nth 1 (nth 1 math-simplify-expr))))
  897. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arccos)
  898. (math-div (nth 1 (nth 1 math-simplify-expr))
  899. (list 'calcFunc-sqrt
  900. (math-sub 1 (math-sqr (nth 1 (nth 1 math-simplify-expr)))))))
  901. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arctan)
  902. (math-div 1 (nth 1 (nth 1 math-simplify-expr))))))
  903. (defun math-known-tan (plus n mul)
  904. (setq n (math-mul n mul))
  905. (and (math-num-integerp n)
  906. (setq n (math-mod (math-trunc n) 120))
  907. (if (> n 60)
  908. (and (setq n (math-known-tan plus (- 120 n) 1))
  909. (math-neg n))
  910. (if (math-zerop plus)
  911. (and (or calc-symbolic-mode
  912. (memq n '(0 30 60)))
  913. (cdr (assq n '( (0 . 0)
  914. (10 . (- 2 (calcFunc-sqrt 3)))
  915. (12 . (calcFunc-sqrt
  916. (- 1 (* (/ 2 5) (calcFunc-sqrt 5)))))
  917. (15 . (- (calcFunc-sqrt 2) 1))
  918. (20 . (/ (calcFunc-sqrt 3) 3))
  919. (24 . (calcFunc-sqrt
  920. (- 5 (* 2 (calcFunc-sqrt 5)))))
  921. (30 . 1)
  922. (36 . (calcFunc-sqrt
  923. (+ 1 (* (/ 2 5) (calcFunc-sqrt 5)))))
  924. (40 . (calcFunc-sqrt 3))
  925. (45 . (+ (calcFunc-sqrt 2) 1))
  926. (48 . (calcFunc-sqrt
  927. (+ 5 (* 2 (calcFunc-sqrt 5)))))
  928. (50 . (+ 2 (calcFunc-sqrt 3)))
  929. (60 . (var uinf var-uinf))))))
  930. (cond ((eq n 0) (math-normalize (list 'calcFunc-tan plus)))
  931. ((eq n 60) (math-normalize (list '/ -1
  932. (list 'calcFunc-tan plus))))
  933. (t nil))))))
  934. (math-defsimplify calcFunc-sinh
  935. (or (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsinh)
  936. (nth 1 (nth 1 math-simplify-expr)))
  937. (and (math-looks-negp (nth 1 math-simplify-expr))
  938. (math-neg (list 'calcFunc-sinh (math-neg (nth 1 math-simplify-expr)))))
  939. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arccosh)
  940. math-living-dangerously
  941. (list 'calcFunc-sqrt
  942. (math-sub (math-sqr (nth 1 (nth 1 math-simplify-expr))) 1)))
  943. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arctanh)
  944. math-living-dangerously
  945. (math-div (nth 1 (nth 1 math-simplify-expr))
  946. (list 'calcFunc-sqrt
  947. (math-sub 1 (math-sqr (nth 1 (nth 1 math-simplify-expr)))))))
  948. (let ((m (math-should-expand-trig (nth 1 math-simplify-expr) t)))
  949. (and m (integerp (car m))
  950. (let ((n (car m)) (a (nth 1 m)))
  951. (if (> n 1)
  952. (list '+
  953. (list '* (list 'calcFunc-sinh (list '* (1- n) a))
  954. (list 'calcFunc-cosh a))
  955. (list '* (list 'calcFunc-cosh (list '* (1- n) a))
  956. (list 'calcFunc-sinh a)))))))))
  957. (math-defsimplify calcFunc-cosh
  958. (or (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arccosh)
  959. (nth 1 (nth 1 math-simplify-expr)))
  960. (and (math-looks-negp (nth 1 math-simplify-expr))
  961. (list 'calcFunc-cosh (math-neg (nth 1 math-simplify-expr))))
  962. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsinh)
  963. math-living-dangerously
  964. (list 'calcFunc-sqrt
  965. (math-add (math-sqr (nth 1 (nth 1 math-simplify-expr))) 1)))
  966. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arctanh)
  967. math-living-dangerously
  968. (math-div 1
  969. (list 'calcFunc-sqrt
  970. (math-sub 1 (math-sqr (nth 1 (nth 1 math-simplify-expr)))))))
  971. (let ((m (math-should-expand-trig (nth 1 math-simplify-expr) t)))
  972. (and m (integerp (car m))
  973. (let ((n (car m)) (a (nth 1 m)))
  974. (if (> n 1)
  975. (list '+
  976. (list '* (list 'calcFunc-cosh (list '* (1- n) a))
  977. (list 'calcFunc-cosh a))
  978. (list '* (list 'calcFunc-sinh (list '* (1- n) a))
  979. (list 'calcFunc-sinh a)))))))))
  980. (math-defsimplify calcFunc-tanh
  981. (or (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arctanh)
  982. (nth 1 (nth 1 math-simplify-expr)))
  983. (and (math-looks-negp (nth 1 math-simplify-expr))
  984. (math-neg (list 'calcFunc-tanh (math-neg (nth 1 math-simplify-expr)))))
  985. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsinh)
  986. math-living-dangerously
  987. (math-div (nth 1 (nth 1 math-simplify-expr))
  988. (list 'calcFunc-sqrt
  989. (math-add (math-sqr (nth 1 (nth 1 math-simplify-expr))) 1))))
  990. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arccosh)
  991. math-living-dangerously
  992. (math-div (list 'calcFunc-sqrt
  993. (math-sub (math-sqr (nth 1 (nth 1 math-simplify-expr))) 1))
  994. (nth 1 (nth 1 math-simplify-expr))))
  995. (let ((m (math-should-expand-trig (nth 1 math-simplify-expr) t)))
  996. (and m
  997. (if (equal (car m) '(frac 1 2))
  998. (math-div (math-sub (list 'calcFunc-cosh (nth 1 m)) 1)
  999. (list 'calcFunc-sinh (nth 1 m)))
  1000. (math-div (list 'calcFunc-sinh (nth 1 math-simplify-expr))
  1001. (list 'calcFunc-cosh (nth 1 math-simplify-expr))))))))
  1002. (math-defsimplify calcFunc-sech
  1003. (or (and (math-looks-negp (nth 1 math-simplify-expr))
  1004. (list 'calcFunc-sech (math-neg (nth 1 math-simplify-expr))))
  1005. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsinh)
  1006. math-living-dangerously
  1007. (math-div
  1008. 1
  1009. (list 'calcFunc-sqrt
  1010. (math-add (math-sqr (nth 1 (nth 1 math-simplify-expr))) 1))))
  1011. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arccosh)
  1012. math-living-dangerously
  1013. (math-div 1 (nth 1 (nth 1 math-simplify-expr))) 1)
  1014. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arctanh)
  1015. math-living-dangerously
  1016. (list 'calcFunc-sqrt
  1017. (math-sub 1 (math-sqr (nth 1 (nth 1 math-simplify-expr))))))))
  1018. (math-defsimplify calcFunc-csch
  1019. (or (and (math-looks-negp (nth 1 math-simplify-expr))
  1020. (math-neg (list 'calcFunc-csch (math-neg (nth 1 math-simplify-expr)))))
  1021. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsinh)
  1022. math-living-dangerously
  1023. (math-div 1 (nth 1 (nth 1 math-simplify-expr))))
  1024. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arccosh)
  1025. math-living-dangerously
  1026. (math-div
  1027. 1
  1028. (list 'calcFunc-sqrt
  1029. (math-sub (math-sqr (nth 1 (nth 1 math-simplify-expr))) 1))))
  1030. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arctanh)
  1031. math-living-dangerously
  1032. (math-div (list 'calcFunc-sqrt
  1033. (math-sub 1 (math-sqr (nth 1 (nth 1 math-simplify-expr)))))
  1034. (nth 1 (nth 1 math-simplify-expr))))))
  1035. (math-defsimplify calcFunc-coth
  1036. (or (and (math-looks-negp (nth 1 math-simplify-expr))
  1037. (math-neg (list 'calcFunc-coth (math-neg (nth 1 math-simplify-expr)))))
  1038. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsinh)
  1039. math-living-dangerously
  1040. (math-div (list 'calcFunc-sqrt
  1041. (math-add (math-sqr (nth 1 (nth 1 math-simplify-expr))) 1))
  1042. (nth 1 (nth 1 math-simplify-expr))))
  1043. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arccosh)
  1044. math-living-dangerously
  1045. (math-div (nth 1 (nth 1 math-simplify-expr))
  1046. (list 'calcFunc-sqrt
  1047. (math-sub (math-sqr (nth 1 (nth 1 math-simplify-expr))) 1))))
  1048. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arctanh)
  1049. math-living-dangerously
  1050. (math-div 1 (nth 1 (nth 1 math-simplify-expr))))))
  1051. (math-defsimplify calcFunc-arcsin
  1052. (or (and (math-looks-negp (nth 1 math-simplify-expr))
  1053. (math-neg (list 'calcFunc-arcsin (math-neg (nth 1 math-simplify-expr)))))
  1054. (and (eq (nth 1 math-simplify-expr) 1)
  1055. (math-quarter-circle t))
  1056. (and (equal (nth 1 math-simplify-expr) '(frac 1 2))
  1057. (math-div (math-half-circle t) 6))
  1058. (and math-living-dangerously
  1059. (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-sin)
  1060. (nth 1 (nth 1 math-simplify-expr)))
  1061. (and math-living-dangerously
  1062. (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-cos)
  1063. (math-sub (math-quarter-circle t)
  1064. (nth 1 (nth 1 math-simplify-expr))))))
  1065. (math-defsimplify calcFunc-arccos
  1066. (or (and (eq (nth 1 math-simplify-expr) 0)
  1067. (math-quarter-circle t))
  1068. (and (eq (nth 1 math-simplify-expr) -1)
  1069. (math-half-circle t))
  1070. (and (equal (nth 1 math-simplify-expr) '(frac 1 2))
  1071. (math-div (math-half-circle t) 3))
  1072. (and (equal (nth 1 math-simplify-expr) '(frac -1 2))
  1073. (math-div (math-mul (math-half-circle t) 2) 3))
  1074. (and math-living-dangerously
  1075. (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-cos)
  1076. (nth 1 (nth 1 math-simplify-expr)))
  1077. (and math-living-dangerously
  1078. (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-sin)
  1079. (math-sub (math-quarter-circle t)
  1080. (nth 1 (nth 1 math-simplify-expr))))))
  1081. (math-defsimplify calcFunc-arctan
  1082. (or (and (math-looks-negp (nth 1 math-simplify-expr))
  1083. (math-neg (list 'calcFunc-arctan (math-neg (nth 1 math-simplify-expr)))))
  1084. (and (eq (nth 1 math-simplify-expr) 1)
  1085. (math-div (math-half-circle t) 4))
  1086. (and math-living-dangerously
  1087. (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-tan)
  1088. (nth 1 (nth 1 math-simplify-expr)))))
  1089. (math-defsimplify calcFunc-arcsinh
  1090. (or (and (math-looks-negp (nth 1 math-simplify-expr))
  1091. (math-neg (list 'calcFunc-arcsinh (math-neg (nth 1 math-simplify-expr)))))
  1092. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-sinh)
  1093. (or math-living-dangerously
  1094. (math-known-realp (nth 1 (nth 1 math-simplify-expr))))
  1095. (nth 1 (nth 1 math-simplify-expr)))))
  1096. (math-defsimplify calcFunc-arccosh
  1097. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-cosh)
  1098. (or math-living-dangerously
  1099. (math-known-realp (nth 1 (nth 1 math-simplify-expr))))
  1100. (nth 1 (nth 1 math-simplify-expr))))
  1101. (math-defsimplify calcFunc-arctanh
  1102. (or (and (math-looks-negp (nth 1 math-simplify-expr))
  1103. (math-neg (list 'calcFunc-arctanh (math-neg (nth 1 math-simplify-expr)))))
  1104. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-tanh)
  1105. (or math-living-dangerously
  1106. (math-known-realp (nth 1 (nth 1 math-simplify-expr))))
  1107. (nth 1 (nth 1 math-simplify-expr)))))
  1108. (math-defsimplify calcFunc-sqrt
  1109. (math-simplify-sqrt))
  1110. (defun math-simplify-sqrt ()
  1111. (or (and (eq (car-safe (nth 1 math-simplify-expr)) 'frac)
  1112. (math-div (list 'calcFunc-sqrt
  1113. (math-mul (nth 1 (nth 1 math-simplify-expr))
  1114. (nth 2 (nth 1 math-simplify-expr))))
  1115. (nth 2 (nth 1 math-simplify-expr))))
  1116. (let ((fac (if (math-objectp (nth 1 math-simplify-expr))
  1117. (math-squared-factor (nth 1 math-simplify-expr))
  1118. (math-common-constant-factor (nth 1 math-simplify-expr)))))
  1119. (and fac (not (eq fac 1))
  1120. (math-mul (math-normalize (list 'calcFunc-sqrt fac))
  1121. (math-normalize
  1122. (list 'calcFunc-sqrt
  1123. (math-cancel-common-factor
  1124. (nth 1 math-simplify-expr) fac))))))
  1125. (and math-living-dangerously
  1126. (or (and (eq (car-safe (nth 1 math-simplify-expr)) '-)
  1127. (math-equal-int (nth 1 (nth 1 math-simplify-expr)) 1)
  1128. (eq (car-safe (nth 2 (nth 1 math-simplify-expr))) '^)
  1129. (math-equal-int (nth 2 (nth 2 (nth 1 math-simplify-expr))) 2)
  1130. (or (and (eq (car-safe (nth 1 (nth 2 (nth 1 math-simplify-expr))))
  1131. 'calcFunc-sin)
  1132. (list 'calcFunc-cos
  1133. (nth 1 (nth 1 (nth 2 (nth 1 math-simplify-expr))))))
  1134. (and (eq (car-safe (nth 1 (nth 2 (nth 1 math-simplify-expr))))
  1135. 'calcFunc-cos)
  1136. (list 'calcFunc-sin
  1137. (nth 1 (nth 1 (nth 2
  1138. (nth 1 math-simplify-expr))))))))
  1139. (and (eq (car-safe (nth 1 math-simplify-expr)) '-)
  1140. (math-equal-int (nth 2 (nth 1 math-simplify-expr)) 1)
  1141. (eq (car-safe (nth 1 (nth 1 math-simplify-expr))) '^)
  1142. (math-equal-int (nth 2 (nth 1 (nth 1 math-simplify-expr))) 2)
  1143. (and (eq (car-safe (nth 1 (nth 1 (nth 1 math-simplify-expr))))
  1144. 'calcFunc-cosh)
  1145. (list 'calcFunc-sinh
  1146. (nth 1 (nth 1 (nth 1 (nth 1 math-simplify-expr)))))))
  1147. (and (eq (car-safe (nth 1 math-simplify-expr)) '+)
  1148. (let ((a (nth 1 (nth 1 math-simplify-expr)))
  1149. (b (nth 2 (nth 1 math-simplify-expr))))
  1150. (and (or (and (math-equal-int a 1)
  1151. (setq a b b (nth 1 (nth 1 math-simplify-expr))))
  1152. (math-equal-int b 1))
  1153. (eq (car-safe a) '^)
  1154. (math-equal-int (nth 2 a) 2)
  1155. (or (and (eq (car-safe (nth 1 a)) 'calcFunc-sinh)
  1156. (list 'calcFunc-cosh (nth 1 (nth 1 a))))
  1157. (and (eq (car-safe (nth 1 a)) 'calcFunc-csch)
  1158. (list 'calcFunc-coth (nth 1 (nth 1 a))))
  1159. (and (eq (car-safe (nth 1 a)) 'calcFunc-tan)
  1160. (list '/ 1 (list 'calcFunc-cos
  1161. (nth 1 (nth 1 a)))))
  1162. (and (eq (car-safe (nth 1 a)) 'calcFunc-cot)
  1163. (list '/ 1 (list 'calcFunc-sin
  1164. (nth 1 (nth 1 a)))))))))
  1165. (and (eq (car-safe (nth 1 math-simplify-expr)) '^)
  1166. (list '^
  1167. (nth 1 (nth 1 math-simplify-expr))
  1168. (math-div (nth 2 (nth 1 math-simplify-expr)) 2)))
  1169. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-sqrt)
  1170. (list '^ (nth 1 (nth 1 math-simplify-expr)) (math-div 1 4)))
  1171. (and (memq (car-safe (nth 1 math-simplify-expr)) '(* /))
  1172. (list (car (nth 1 math-simplify-expr))
  1173. (list 'calcFunc-sqrt (nth 1 (nth 1 math-simplify-expr)))
  1174. (list 'calcFunc-sqrt (nth 2 (nth 1 math-simplify-expr)))))
  1175. (and (memq (car-safe (nth 1 math-simplify-expr)) '(+ -))
  1176. (not (math-any-floats (nth 1 math-simplify-expr)))
  1177. (let ((f (calcFunc-factors (calcFunc-expand
  1178. (nth 1 math-simplify-expr)))))
  1179. (and (math-vectorp f)
  1180. (or (> (length f) 2)
  1181. (> (nth 2 (nth 1 f)) 1))
  1182. (let ((out 1) (rest 1) (sums 1) fac pow)
  1183. (while (setq f (cdr f))
  1184. (setq fac (nth 1 (car f))
  1185. pow (nth 2 (car f)))
  1186. (if (> pow 1)
  1187. (setq out (math-mul out (math-pow
  1188. fac (/ pow 2)))
  1189. pow (% pow 2)))
  1190. (if (> pow 0)
  1191. (if (memq (car-safe fac) '(+ -))
  1192. (setq sums (math-mul-thru sums fac))
  1193. (setq rest (math-mul rest fac)))))
  1194. (and (not (and (eq out 1) (memq rest '(1 -1))))
  1195. (math-mul
  1196. out
  1197. (list 'calcFunc-sqrt
  1198. (math-mul sums rest))))))))))))
  1199. ;;; Rather than factoring x into primes, just check for the first ten primes.
  1200. (defun math-squared-factor (x)
  1201. (if (Math-integerp x)
  1202. (let ((prsqr '(4 9 25 49 121 169 289 361 529 841))
  1203. (fac 1)
  1204. res)
  1205. (while prsqr
  1206. (if (eq (cdr (setq res (math-idivmod x (car prsqr)))) 0)
  1207. (setq x (car res)
  1208. fac (math-mul fac (car prsqr)))
  1209. (setq prsqr (cdr prsqr))))
  1210. fac)))
  1211. (math-defsimplify calcFunc-exp
  1212. (math-simplify-exp (nth 1 math-simplify-expr)))
  1213. (defun math-simplify-exp (x)
  1214. (or (and (eq (car-safe x) 'calcFunc-ln)
  1215. (nth 1 x))
  1216. (and math-living-dangerously
  1217. (or (and (eq (car-safe x) 'calcFunc-arcsinh)
  1218. (math-add (nth 1 x)
  1219. (list 'calcFunc-sqrt
  1220. (math-add (math-sqr (nth 1 x)) 1))))
  1221. (and (eq (car-safe x) 'calcFunc-arccosh)
  1222. (math-add (nth 1 x)
  1223. (list 'calcFunc-sqrt
  1224. (math-sub (math-sqr (nth 1 x)) 1))))
  1225. (and (eq (car-safe x) 'calcFunc-arctanh)
  1226. (math-div (list 'calcFunc-sqrt (math-add 1 (nth 1 x)))
  1227. (list 'calcFunc-sqrt (math-sub 1 (nth 1 x)))))
  1228. (let ((m (math-should-expand-trig x 'exp)))
  1229. (and m (integerp (car m))
  1230. (list '^ (list 'calcFunc-exp (nth 1 m)) (car m))))))
  1231. (and calc-symbolic-mode
  1232. (math-known-imagp x)
  1233. (let* ((ip (calcFunc-im x))
  1234. (n (math-linear-in ip '(var pi var-pi)))
  1235. s c)
  1236. (and n
  1237. (setq s (math-known-sin (car n) (nth 1 n) 120 0))
  1238. (setq c (math-known-sin (car n) (nth 1 n) 120 300))
  1239. (list '+ c (list '* s '(var i var-i))))))))
  1240. (math-defsimplify calcFunc-ln
  1241. (or (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-exp)
  1242. (or math-living-dangerously
  1243. (math-known-realp (nth 1 (nth 1 math-simplify-expr))))
  1244. (nth 1 (nth 1 math-simplify-expr)))
  1245. (and (eq (car-safe (nth 1 math-simplify-expr)) '^)
  1246. (equal (nth 1 (nth 1 math-simplify-expr)) '(var e var-e))
  1247. (or math-living-dangerously
  1248. (math-known-realp (nth 2 (nth 1 math-simplify-expr))))
  1249. (nth 2 (nth 1 math-simplify-expr)))
  1250. (and calc-symbolic-mode
  1251. (math-known-negp (nth 1 math-simplify-expr))
  1252. (math-add (list 'calcFunc-ln (math-neg (nth 1 math-simplify-expr)))
  1253. '(* (var pi var-pi) (var i var-i))))
  1254. (and calc-symbolic-mode
  1255. (math-known-imagp (nth 1 math-simplify-expr))
  1256. (let* ((ip (calcFunc-im (nth 1 math-simplify-expr)))
  1257. (ips (math-possible-signs ip)))
  1258. (or (and (memq ips '(4 6))
  1259. (math-add (list 'calcFunc-ln ip)
  1260. '(/ (* (var pi var-pi) (var i var-i)) 2)))
  1261. (and (memq ips '(1 3))
  1262. (math-sub (list 'calcFunc-ln (math-neg ip))
  1263. '(/ (* (var pi var-pi) (var i var-i)) 2))))))))
  1264. (math-defsimplify ^
  1265. (math-simplify-pow))
  1266. (defun math-simplify-pow ()
  1267. (or (and math-living-dangerously
  1268. (or (and (eq (car-safe (nth 1 math-simplify-expr)) '^)
  1269. (list '^
  1270. (nth 1 (nth 1 math-simplify-expr))
  1271. (math-mul (nth 2 math-simplify-expr)
  1272. (nth 2 (nth 1 math-simplify-expr)))))
  1273. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-sqrt)
  1274. (list '^
  1275. (nth 1 (nth 1 math-simplify-expr))
  1276. (math-div (nth 2 math-simplify-expr) 2)))
  1277. (and (memq (car-safe (nth 1 math-simplify-expr)) '(* /))
  1278. (list (car (nth 1 math-simplify-expr))
  1279. (list '^ (nth 1 (nth 1 math-simplify-expr))
  1280. (nth 2 math-simplify-expr))
  1281. (list '^ (nth 2 (nth 1 math-simplify-expr))
  1282. (nth 2 math-simplify-expr))))))
  1283. (and (math-equal-int (nth 1 math-simplify-expr) 10)
  1284. (eq (car-safe (nth 2 math-simplify-expr)) 'calcFunc-log10)
  1285. (nth 1 (nth 2 math-simplify-expr)))
  1286. (and (equal (nth 1 math-simplify-expr) '(var e var-e))
  1287. (math-simplify-exp (nth 2 math-simplify-expr)))
  1288. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-exp)
  1289. (not math-integrating)
  1290. (list 'calcFunc-exp (math-mul (nth 1 (nth 1 math-simplify-expr))
  1291. (nth 2 math-simplify-expr))))
  1292. (and (equal (nth 1 math-simplify-expr) '(var i var-i))
  1293. (math-imaginary-i)
  1294. (math-num-integerp (nth 2 math-simplify-expr))
  1295. (let ((x (math-mod (math-trunc (nth 2 math-simplify-expr)) 4)))
  1296. (cond ((eq x 0) 1)
  1297. ((eq x 1) (nth 1 math-simplify-expr))
  1298. ((eq x 2) -1)
  1299. ((eq x 3) (math-neg (nth 1 math-simplify-expr))))))
  1300. (and math-integrating
  1301. (integerp (nth 2 math-simplify-expr))
  1302. (>= (nth 2 math-simplify-expr) 2)
  1303. (or (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-cos)
  1304. (math-mul (math-pow (nth 1 math-simplify-expr)
  1305. (- (nth 2 math-simplify-expr) 2))
  1306. (math-sub 1
  1307. (math-sqr
  1308. (list 'calcFunc-sin
  1309. (nth 1 (nth 1 math-simplify-expr)))))))
  1310. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-cosh)
  1311. (math-mul (math-pow (nth 1 math-simplify-expr)
  1312. (- (nth 2 math-simplify-expr) 2))
  1313. (math-add 1
  1314. (math-sqr
  1315. (list 'calcFunc-sinh
  1316. (nth 1 (nth 1 math-simplify-expr)))))))))
  1317. (and (eq (car-safe (nth 2 math-simplify-expr)) 'frac)
  1318. (Math-ratp (nth 1 math-simplify-expr))
  1319. (Math-posp (nth 1 math-simplify-expr))
  1320. (if (equal (nth 2 math-simplify-expr) '(frac 1 2))
  1321. (list 'calcFunc-sqrt (nth 1 math-simplify-expr))
  1322. (let ((flr (math-floor (nth 2 math-simplify-expr))))
  1323. (and (not (Math-zerop flr))
  1324. (list '* (list '^ (nth 1 math-simplify-expr) flr)
  1325. (list '^ (nth 1 math-simplify-expr)
  1326. (math-sub (nth 2 math-simplify-expr) flr)))))))
  1327. (and (eq (math-quarter-integer (nth 2 math-simplify-expr)) 2)
  1328. (let ((temp (math-simplify-sqrt)))
  1329. (and temp
  1330. (list '^ temp (math-mul (nth 2 math-simplify-expr) 2)))))))
  1331. (math-defsimplify calcFunc-log10
  1332. (and (eq (car-safe (nth 1 math-simplify-expr)) '^)
  1333. (math-equal-int (nth 1 (nth 1 math-simplify-expr)) 10)
  1334. (or math-living-dangerously
  1335. (math-known-realp (nth 2 (nth 1 math-simplify-expr))))
  1336. (nth 2 (nth 1 math-simplify-expr))))
  1337. (math-defsimplify calcFunc-erf
  1338. (or (and (math-looks-negp (nth 1 math-simplify-expr))
  1339. (math-neg (list 'calcFunc-erf (math-neg (nth 1 math-simplify-expr)))))
  1340. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-conj)
  1341. (list 'calcFunc-conj
  1342. (list 'calcFunc-erf (nth 1 (nth 1 math-simplify-expr)))))))
  1343. (math-defsimplify calcFunc-erfc
  1344. (or (and (math-looks-negp (nth 1 math-simplify-expr))
  1345. (math-sub 2 (list 'calcFunc-erfc (math-neg (nth 1 math-simplify-expr)))))
  1346. (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-conj)
  1347. (list 'calcFunc-conj
  1348. (list 'calcFunc-erfc (nth 1 (nth 1 math-simplify-expr)))))))
  1349. (defun math-linear-in (expr term &optional always)
  1350. (if (math-expr-contains expr term)
  1351. (let* ((calc-prefer-frac t)
  1352. (p (math-is-polynomial expr term 1)))
  1353. (and (cdr p)
  1354. p))
  1355. (and always (list expr 0))))
  1356. (defun math-multiple-of (expr term)
  1357. (let ((p (math-linear-in expr term)))
  1358. (and p
  1359. (math-zerop (car p))
  1360. (nth 1 p))))
  1361. ; not perfect, but it'll do
  1362. (defun math-integer-plus (expr)
  1363. (cond ((Math-integerp expr)
  1364. (list 0 expr))
  1365. ((and (memq (car expr) '(+ -))
  1366. (Math-integerp (nth 1 expr)))
  1367. (list (if (eq (car expr) '+) (nth 2 expr) (math-neg (nth 2 expr)))
  1368. (nth 1 expr)))
  1369. ((and (memq (car expr) '(+ -))
  1370. (Math-integerp (nth 2 expr)))
  1371. (list (nth 1 expr)
  1372. (if (eq (car expr) '+) (nth 2 expr) (math-neg (nth 2 expr)))))
  1373. (t nil)))
  1374. (defun math-is-linear (expr &optional always)
  1375. (let ((offset nil)
  1376. (coef nil))
  1377. (if (eq (car-safe expr) '+)
  1378. (if (Math-objectp (nth 1 expr))
  1379. (setq offset (nth 1 expr)
  1380. expr (nth 2 expr))
  1381. (if (Math-objectp (nth 2 expr))
  1382. (setq offset (nth 2 expr)
  1383. expr (nth 1 expr))))
  1384. (if (eq (car-safe expr) '-)
  1385. (if (Math-objectp (nth 1 expr))
  1386. (setq offset (nth 1 expr)
  1387. expr (math-neg (nth 2 expr)))
  1388. (if (Math-objectp (nth 2 expr))
  1389. (setq offset (math-neg (nth 2 expr))
  1390. expr (nth 1 expr))))))
  1391. (setq coef (math-is-multiple expr always))
  1392. (if offset
  1393. (list offset (or (car coef) 1) (or (nth 1 coef) expr))
  1394. (if coef
  1395. (cons 0 coef)))))
  1396. (defun math-is-multiple (expr &optional always)
  1397. (or (if (eq (car-safe expr) '*)
  1398. (if (Math-objectp (nth 1 expr))
  1399. (list (nth 1 expr) (nth 2 expr)))
  1400. (if (eq (car-safe expr) '/)
  1401. (if (and (Math-objectp (nth 1 expr))
  1402. (not (math-equal-int (nth 1 expr) 1)))
  1403. (list (nth 1 expr) (math-div 1 (nth 2 expr)))
  1404. (if (Math-objectp (nth 2 expr))
  1405. (list (math-div 1 (nth 2 expr)) (nth 1 expr))
  1406. (let ((res (math-is-multiple (nth 1 expr))))
  1407. (if res
  1408. (list (car res)
  1409. (math-div (nth 2 (nth 1 expr)) (nth 2 expr)))
  1410. (setq res (math-is-multiple (nth 2 expr)))
  1411. (if res
  1412. (list (math-div 1 (car res))
  1413. (math-div (nth 1 expr)
  1414. (nth 2 (nth 2 expr)))))))))
  1415. (if (eq (car-safe expr) 'neg)
  1416. (list -1 (nth 1 expr)))))
  1417. (if (Math-objvecp expr)
  1418. (and (eq always 1)
  1419. (list expr 1))
  1420. (and always
  1421. (list 1 expr)))))
  1422. (defun calcFunc-lin (expr &optional var)
  1423. (if var
  1424. (let ((res (math-linear-in expr var t)))
  1425. (or res (math-reject-arg expr "Linear term expected"))
  1426. (list 'vec (car res) (nth 1 res) var))
  1427. (let ((res (math-is-linear expr t)))
  1428. (or res (math-reject-arg expr "Linear term expected"))
  1429. (cons 'vec res))))
  1430. (defun calcFunc-linnt (expr &optional var)
  1431. (if var
  1432. (let ((res (math-linear-in expr var)))
  1433. (or res (math-reject-arg expr "Linear term expected"))
  1434. (list 'vec (car res) (nth 1 res) var))
  1435. (let ((res (math-is-linear expr)))
  1436. (or res (math-reject-arg expr "Linear term expected"))
  1437. (cons 'vec res))))
  1438. (defun calcFunc-islin (expr &optional var)
  1439. (if (and (Math-objvecp expr) (not var))
  1440. 0
  1441. (calcFunc-lin expr var)
  1442. 1))
  1443. (defun calcFunc-islinnt (expr &optional var)
  1444. (if (Math-objvecp expr)
  1445. 0
  1446. (calcFunc-linnt expr var)
  1447. 1))
  1448. ;;; Simple operations on expressions.
  1449. ;;; Return number of occurrences of thing in expr, or nil if none.
  1450. (defun math-expr-contains-count (expr thing)
  1451. (cond ((equal expr thing) 1)
  1452. ((Math-primp expr) nil)
  1453. (t
  1454. (let ((num 0))
  1455. (while (setq expr (cdr expr))
  1456. (setq num (+ num (or (math-expr-contains-count
  1457. (car expr) thing) 0))))
  1458. (and (> num 0)
  1459. num)))))
  1460. (defun math-expr-contains (expr thing)
  1461. (cond ((equal expr thing) 1)
  1462. ((Math-primp expr) nil)
  1463. (t
  1464. (while (and (setq expr (cdr expr))
  1465. (not (math-expr-contains (car expr) thing))))
  1466. expr)))
  1467. ;;; Return non-nil if any variable of thing occurs in expr.
  1468. (defun math-expr-depends (expr thing)
  1469. (if (Math-primp thing)
  1470. (and (eq (car-safe thing) 'var)
  1471. (math-expr-contains expr thing))
  1472. (while (and (setq thing (cdr thing))
  1473. (not (math-expr-depends expr (car thing)))))
  1474. thing))
  1475. ;;; Substitute all occurrences of old for new in expr (non-destructive).
  1476. ;; The variables math-expr-subst-old and math-expr-subst-new are local
  1477. ;; for math-expr-subst, but used by math-expr-subst-rec.
  1478. (defvar math-expr-subst-old)
  1479. (defvar math-expr-subst-new)
  1480. (defun math-expr-subst (expr math-expr-subst-old math-expr-subst-new)
  1481. (math-expr-subst-rec expr))
  1482. (defalias 'calcFunc-subst 'math-expr-subst)
  1483. (defun math-expr-subst-rec (expr)
  1484. (cond ((equal expr math-expr-subst-old) math-expr-subst-new)
  1485. ((Math-primp expr) expr)
  1486. ((memq (car expr) '(calcFunc-deriv
  1487. calcFunc-tderiv))
  1488. (if (= (length expr) 2)
  1489. (if (equal (nth 1 expr) math-expr-subst-old)
  1490. (append expr (list math-expr-subst-new))
  1491. expr)
  1492. (list (car expr) (nth 1 expr)
  1493. (math-expr-subst-rec (nth 2 expr)))))
  1494. (t
  1495. (cons (car expr)
  1496. (mapcar 'math-expr-subst-rec (cdr expr))))))
  1497. ;;; Various measures of the size of an expression.
  1498. (defun math-expr-weight (expr)
  1499. (if (Math-primp expr)
  1500. 1
  1501. (let ((w 1))
  1502. (while (setq expr (cdr expr))
  1503. (setq w (+ w (math-expr-weight (car expr)))))
  1504. w)))
  1505. (defun math-expr-height (expr)
  1506. (if (Math-primp expr)
  1507. 0
  1508. (let ((h 0))
  1509. (while (setq expr (cdr expr))
  1510. (setq h (max h (math-expr-height (car expr)))))
  1511. (1+ h))))
  1512. ;;; Polynomial operations (to support the integrator and solve-for).
  1513. (defun calcFunc-collect (expr base)
  1514. (let ((p (math-is-polynomial expr base 50 t)))
  1515. (if (cdr p)
  1516. (math-build-polynomial-expr (mapcar 'math-normalize p) base)
  1517. (car p))))
  1518. ;;; If expr is of the form "a + bx + cx^2 + ...", return the list (a b c ...),
  1519. ;;; else return nil if not in polynomial form. If "loose" (math-is-poly-loose),
  1520. ;;; coefficients may contain x, e.g., sin(x) + cos(x) x^2 is a loose polynomial in x.
  1521. ;; These variables are local to math-is-polynomial, but are used by
  1522. ;; math-is-poly-rec.
  1523. (defvar math-is-poly-degree)
  1524. (defvar math-is-poly-loose)
  1525. (defvar math-var)
  1526. (defun math-is-polynomial (expr math-var &optional math-is-poly-degree math-is-poly-loose)
  1527. (let* ((math-poly-base-variable (if math-is-poly-loose
  1528. (if (eq math-is-poly-loose 'gen) math-var '(var XXX XXX))
  1529. math-poly-base-variable))
  1530. (poly (math-is-poly-rec expr math-poly-neg-powers)))
  1531. (and (or (null math-is-poly-degree)
  1532. (<= (length poly) (1+ math-is-poly-degree)))
  1533. poly)))
  1534. (defun math-is-poly-rec (expr negpow)
  1535. (math-poly-simplify
  1536. (or (cond ((or (equal expr math-var)
  1537. (eq (car-safe expr) '^))
  1538. (let ((pow 1)
  1539. (expr expr))
  1540. (or (equal expr math-var)
  1541. (setq pow (nth 2 expr)
  1542. expr (nth 1 expr)))
  1543. (or (eq math-poly-mult-powers 1)
  1544. (setq pow (let ((m (math-is-multiple pow 1)))
  1545. (and (eq (car-safe (car m)) 'cplx)
  1546. (Math-zerop (nth 1 (car m)))
  1547. (setq m (list (nth 2 (car m))
  1548. (math-mul (nth 1 m)
  1549. '(var i var-i)))))
  1550. (and (if math-poly-mult-powers
  1551. (equal math-poly-mult-powers
  1552. (nth 1 m))
  1553. (setq math-poly-mult-powers (nth 1 m)))
  1554. (or (equal expr math-var)
  1555. (eq math-poly-mult-powers 1))
  1556. (car m)))))
  1557. (if (consp pow)
  1558. (progn
  1559. (setq pow (math-to-simple-fraction pow))
  1560. (and (eq (car-safe pow) 'frac)
  1561. math-poly-frac-powers
  1562. (equal expr math-var)
  1563. (setq math-poly-frac-powers
  1564. (calcFunc-lcm math-poly-frac-powers
  1565. (nth 2 pow))))))
  1566. (or (memq math-poly-frac-powers '(1 nil))
  1567. (setq pow (math-mul pow math-poly-frac-powers)))
  1568. (if (integerp pow)
  1569. (if (and (= pow 1)
  1570. (equal expr math-var))
  1571. (list 0 1)
  1572. (if (natnump pow)
  1573. (let ((p1 (if (equal expr math-var)
  1574. (list 0 1)
  1575. (math-is-poly-rec expr nil)))
  1576. (n pow)
  1577. (accum (list 1)))
  1578. (and p1
  1579. (or (null math-is-poly-degree)
  1580. (<= (* (1- (length p1)) n) math-is-poly-degree))
  1581. (progn
  1582. (while (>= n 1)
  1583. (setq accum (math-poly-mul accum p1)
  1584. n (1- n)))
  1585. accum)))
  1586. (and negpow
  1587. (math-is-poly-rec expr nil)
  1588. (setq math-poly-neg-powers
  1589. (cons (math-pow expr (- pow))
  1590. math-poly-neg-powers))
  1591. (list (list '^ expr pow))))))))
  1592. ((Math-objectp expr)
  1593. (list expr))
  1594. ((memq (car expr) '(+ -))
  1595. (let ((p1 (math-is-poly-rec (nth 1 expr) negpow)))
  1596. (and p1
  1597. (let ((p2 (math-is-poly-rec (nth 2 expr) negpow)))
  1598. (and p2
  1599. (math-poly-mix p1 1 p2
  1600. (if (eq (car expr) '+) 1 -1)))))))
  1601. ((eq (car expr) 'neg)
  1602. (mapcar 'math-neg (math-is-poly-rec (nth 1 expr) negpow)))
  1603. ((eq (car expr) '*)
  1604. (let ((p1 (math-is-poly-rec (nth 1 expr) negpow)))
  1605. (and p1
  1606. (let ((p2 (math-is-poly-rec (nth 2 expr) negpow)))
  1607. (and p2
  1608. (or (null math-is-poly-degree)
  1609. (<= (- (+ (length p1) (length p2)) 2)
  1610. math-is-poly-degree))
  1611. (math-poly-mul p1 p2))))))
  1612. ((eq (car expr) '/)
  1613. (and (or (not (math-poly-depends (nth 2 expr) math-var))
  1614. (and negpow
  1615. (math-is-poly-rec (nth 2 expr) nil)
  1616. (setq math-poly-neg-powers
  1617. (cons (nth 2 expr) math-poly-neg-powers))))
  1618. (not (Math-zerop (nth 2 expr)))
  1619. (let ((p1 (math-is-poly-rec (nth 1 expr) negpow)))
  1620. (mapcar (function (lambda (x) (math-div x (nth 2 expr))))
  1621. p1))))
  1622. ((and (eq (car expr) 'calcFunc-exp)
  1623. (equal math-var '(var e var-e)))
  1624. (math-is-poly-rec (list '^ math-var (nth 1 expr)) negpow))
  1625. ((and (eq (car expr) 'calcFunc-sqrt)
  1626. math-poly-frac-powers)
  1627. (math-is-poly-rec (list '^ (nth 1 expr) '(frac 1 2)) negpow))
  1628. (t nil))
  1629. (and (or (not (math-poly-depends expr math-var))
  1630. math-is-poly-loose)
  1631. (not (eq (car expr) 'vec))
  1632. (list expr)))))
  1633. ;;; Check if expr is a polynomial in var; if so, return its degree.
  1634. (defun math-polynomial-p (expr var)
  1635. (cond ((equal expr var) 1)
  1636. ((Math-primp expr) 0)
  1637. ((memq (car expr) '(+ -))
  1638. (let ((p1 (math-polynomial-p (nth 1 expr) var))
  1639. p2)
  1640. (and p1 (setq p2 (math-polynomial-p (nth 2 expr) var))
  1641. (max p1 p2))))
  1642. ((eq (car expr) '*)
  1643. (let ((p1 (math-polynomial-p (nth 1 expr) var))
  1644. p2)
  1645. (and p1 (setq p2 (math-polynomial-p (nth 2 expr) var))
  1646. (+ p1 p2))))
  1647. ((eq (car expr) 'neg)
  1648. (math-polynomial-p (nth 1 expr) var))
  1649. ((and (eq (car expr) '/)
  1650. (not (math-poly-depends (nth 2 expr) var)))
  1651. (math-polynomial-p (nth 1 expr) var))
  1652. ((and (eq (car expr) '^)
  1653. (natnump (nth 2 expr)))
  1654. (let ((p1 (math-polynomial-p (nth 1 expr) var)))
  1655. (and p1 (* p1 (nth 2 expr)))))
  1656. ((math-poly-depends expr var) nil)
  1657. (t 0)))
  1658. (defun math-poly-depends (expr var)
  1659. (if math-poly-base-variable
  1660. (math-expr-contains expr math-poly-base-variable)
  1661. (math-expr-depends expr var)))
  1662. ;;; Find the variable (or sub-expression) which is the base of polynomial expr.
  1663. ;; The variables math-poly-base-const-ok and math-poly-base-pred are
  1664. ;; local to math-polynomial-base, but are used by math-polynomial-base-rec.
  1665. (defvar math-poly-base-const-ok)
  1666. (defvar math-poly-base-pred)
  1667. ;; The variable math-poly-base-top-expr is local to math-polynomial-base,
  1668. ;; but is used by math-polynomial-p1 in calc-poly.el, which is called
  1669. ;; by math-polynomial-base.
  1670. (defun math-polynomial-base (math-poly-base-top-expr &optional math-poly-base-pred)
  1671. (or math-poly-base-pred
  1672. (setq math-poly-base-pred (function (lambda (base) (math-polynomial-p
  1673. math-poly-base-top-expr base)))))
  1674. (or (let ((math-poly-base-const-ok nil))
  1675. (math-polynomial-base-rec math-poly-base-top-expr))
  1676. (let ((math-poly-base-const-ok t))
  1677. (math-polynomial-base-rec math-poly-base-top-expr))))
  1678. (defun math-polynomial-base-rec (mpb-expr)
  1679. (and (not (Math-objvecp mpb-expr))
  1680. (or (and (memq (car mpb-expr) '(+ - *))
  1681. (or (math-polynomial-base-rec (nth 1 mpb-expr))
  1682. (math-polynomial-base-rec (nth 2 mpb-expr))))
  1683. (and (memq (car mpb-expr) '(/ neg))
  1684. (math-polynomial-base-rec (nth 1 mpb-expr)))
  1685. (and (eq (car mpb-expr) '^)
  1686. (math-polynomial-base-rec (nth 1 mpb-expr)))
  1687. (and (eq (car mpb-expr) 'calcFunc-exp)
  1688. (math-polynomial-base-rec '(var e var-e)))
  1689. (and (or math-poly-base-const-ok (math-expr-contains-vars mpb-expr))
  1690. (funcall math-poly-base-pred mpb-expr)
  1691. mpb-expr))))
  1692. ;;; Return non-nil if expr refers to any variables.
  1693. (defun math-expr-contains-vars (expr)
  1694. (or (eq (car-safe expr) 'var)
  1695. (and (not (Math-primp expr))
  1696. (progn
  1697. (while (and (setq expr (cdr expr))
  1698. (not (math-expr-contains-vars (car expr)))))
  1699. expr))))
  1700. ;;; Simplify a polynomial in list form by stripping off high-end zeros.
  1701. ;;; This always leaves the constant part, i.e., nil->nil and non-nil->non-nil.
  1702. (defun math-poly-simplify (p)
  1703. (and p
  1704. (if (Math-zerop (nth (1- (length p)) p))
  1705. (let ((pp (copy-sequence p)))
  1706. (while (and (cdr pp)
  1707. (Math-zerop (nth (1- (length pp)) pp)))
  1708. (setcdr (nthcdr (- (length pp) 2) pp) nil))
  1709. pp)
  1710. p)))
  1711. ;;; Compute ac*a + bc*b for polynomials in list form a, b and
  1712. ;;; coefficients ac, bc. Result may be unsimplified.
  1713. (defun math-poly-mix (a ac b bc)
  1714. (and (or a b)
  1715. (cons (math-add (math-mul (or (car a) 0) ac)
  1716. (math-mul (or (car b) 0) bc))
  1717. (math-poly-mix (cdr a) ac (cdr b) bc))))
  1718. (defun math-poly-zerop (a)
  1719. (or (null a)
  1720. (and (null (cdr a)) (Math-zerop (car a)))))
  1721. ;;; Multiply two polynomials in list form.
  1722. (defun math-poly-mul (a b)
  1723. (and a b
  1724. (math-poly-mix b (car a)
  1725. (math-poly-mul (cdr a) (cons 0 b)) 1)))
  1726. ;;; Build an expression from a polynomial list.
  1727. (defun math-build-polynomial-expr (p var)
  1728. (if p
  1729. (if (Math-numberp var)
  1730. (math-with-extra-prec 1
  1731. (let* ((rp (reverse p))
  1732. (accum (car rp)))
  1733. (while (setq rp (cdr rp))
  1734. (setq accum (math-add (car rp) (math-mul accum var))))
  1735. accum))
  1736. (let* ((rp (reverse p))
  1737. (n (1- (length rp)))
  1738. (accum (math-mul (car rp) (math-pow var n)))
  1739. term)
  1740. (while (setq rp (cdr rp))
  1741. (setq n (1- n))
  1742. (or (math-zerop (car rp))
  1743. (setq accum (list (if (math-looks-negp (car rp)) '- '+)
  1744. accum
  1745. (math-mul (if (math-looks-negp (car rp))
  1746. (math-neg (car rp))
  1747. (car rp))
  1748. (math-pow var n))))))
  1749. accum))
  1750. 0))
  1751. (defun math-to-simple-fraction (f)
  1752. (or (and (eq (car-safe f) 'float)
  1753. (or (and (>= (nth 2 f) 0)
  1754. (math-scale-int (nth 1 f) (nth 2 f)))
  1755. (and (integerp (nth 1 f))
  1756. (> (nth 1 f) -1000)
  1757. (< (nth 1 f) 1000)
  1758. (math-make-frac (nth 1 f)
  1759. (math-scale-int 1 (- (nth 2 f)))))))
  1760. f))
  1761. (provide 'calc-alg)
  1762. ;;; calc-alg.el ends here