calc-arith.el 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069
  1. ;;; calc-arith.el --- arithmetic functions for Calc
  2. ;; Copyright (C) 1990-1993, 2001-2017 Free Software Foundation, Inc.
  3. ;; Author: David Gillespie <daveg@synaptics.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;; Code:
  17. ;; This file is autoloaded from calc-ext.el.
  18. (require 'calc-ext)
  19. (require 'calc-macs)
  20. ;;; The following lists are not exhaustive.
  21. (defvar math-scalar-functions '(calcFunc-det
  22. calcFunc-cnorm calcFunc-rnorm
  23. calcFunc-vlen calcFunc-vcount
  24. calcFunc-vsum calcFunc-vprod
  25. calcFunc-vmin calcFunc-vmax))
  26. (defvar math-nonscalar-functions '(vec calcFunc-idn calcFunc-diag
  27. calcFunc-cvec calcFunc-index
  28. calcFunc-trn
  29. | calcFunc-append
  30. calcFunc-cons calcFunc-rcons
  31. calcFunc-tail calcFunc-rhead))
  32. (defvar math-scalar-if-args-functions '(+ - * / neg))
  33. (defvar math-real-functions '(calcFunc-arg
  34. calcFunc-re calcFunc-im
  35. calcFunc-floor calcFunc-ceil
  36. calcFunc-trunc calcFunc-round
  37. calcFunc-rounde calcFunc-roundu
  38. calcFunc-ffloor calcFunc-fceil
  39. calcFunc-ftrunc calcFunc-fround
  40. calcFunc-frounde calcFunc-froundu))
  41. (defvar math-positive-functions '())
  42. (defvar math-nonnegative-functions '(calcFunc-cnorm calcFunc-rnorm
  43. calcFunc-vlen calcFunc-vcount))
  44. (defvar math-real-scalar-functions '(% calcFunc-idiv calcFunc-abs
  45. calcFunc-choose calcFunc-perm
  46. calcFunc-eq calcFunc-neq
  47. calcFunc-lt calcFunc-gt
  48. calcFunc-leq calcFunc-geq
  49. calcFunc-lnot
  50. calcFunc-max calcFunc-min))
  51. (defvar math-real-if-arg-functions '(calcFunc-sin calcFunc-cos
  52. calcFunc-tan calcFunc-sec
  53. calcFunc-csc calcFunc-cot
  54. calcFunc-arctan
  55. calcFunc-sinh calcFunc-cosh
  56. calcFunc-tanh calcFunc-sech
  57. calcFunc-csch calcFunc-coth
  58. calcFunc-exp
  59. calcFunc-gamma calcFunc-fact))
  60. (defvar math-integer-functions '(calcFunc-idiv
  61. calcFunc-isqrt calcFunc-ilog
  62. calcFunc-vlen calcFunc-vcount))
  63. (defvar math-num-integer-functions '())
  64. (defvar math-rounding-functions '(calcFunc-floor
  65. calcFunc-ceil
  66. calcFunc-round calcFunc-trunc
  67. calcFunc-rounde calcFunc-roundu))
  68. (defvar math-float-rounding-functions '(calcFunc-ffloor
  69. calcFunc-fceil
  70. calcFunc-fround calcFunc-ftrunc
  71. calcFunc-frounde calcFunc-froundu))
  72. (defvar math-integer-if-args-functions '(+ - * % neg calcFunc-abs
  73. calcFunc-min calcFunc-max
  74. calcFunc-choose calcFunc-perm))
  75. ;;; Arithmetic.
  76. (defun calc-min (arg)
  77. (interactive "P")
  78. (calc-slow-wrapper
  79. (calc-binary-op "min" 'calcFunc-min arg '(var inf var-inf))))
  80. (defun calc-max (arg)
  81. (interactive "P")
  82. (calc-slow-wrapper
  83. (calc-binary-op "max" 'calcFunc-max arg '(neg (var inf var-inf)))))
  84. (defun calc-abs (arg)
  85. (interactive "P")
  86. (calc-slow-wrapper
  87. (calc-unary-op "abs" 'calcFunc-abs arg)))
  88. (defun calc-idiv (arg)
  89. (interactive "P")
  90. (calc-slow-wrapper
  91. (calc-binary-op "\\" 'calcFunc-idiv arg 1)))
  92. (defun calc-floor (arg)
  93. (interactive "P")
  94. (calc-slow-wrapper
  95. (if (calc-is-inverse)
  96. (if (calc-is-hyperbolic)
  97. (calc-unary-op "ceil" 'calcFunc-fceil arg)
  98. (calc-unary-op "ceil" 'calcFunc-ceil arg))
  99. (if (calc-is-hyperbolic)
  100. (calc-unary-op "flor" 'calcFunc-ffloor arg)
  101. (calc-unary-op "flor" 'calcFunc-floor arg)))))
  102. (defun calc-ceiling (arg)
  103. (interactive "P")
  104. (calc-invert-func)
  105. (calc-floor arg))
  106. (defun calc-round (arg)
  107. (interactive "P")
  108. (calc-slow-wrapper
  109. (if (calc-is-inverse)
  110. (if (calc-is-hyperbolic)
  111. (calc-unary-op "trnc" 'calcFunc-ftrunc arg)
  112. (calc-unary-op "trnc" 'calcFunc-trunc arg))
  113. (if (calc-is-hyperbolic)
  114. (calc-unary-op "rond" 'calcFunc-fround arg)
  115. (calc-unary-op "rond" 'calcFunc-round arg)))))
  116. (defun calc-trunc (arg)
  117. (interactive "P")
  118. (calc-invert-func)
  119. (calc-round arg))
  120. (defun calc-mant-part (arg)
  121. (interactive "P")
  122. (calc-slow-wrapper
  123. (calc-unary-op "mant" 'calcFunc-mant arg)))
  124. (defun calc-xpon-part (arg)
  125. (interactive "P")
  126. (calc-slow-wrapper
  127. (calc-unary-op "xpon" 'calcFunc-xpon arg)))
  128. (defun calc-scale-float (arg)
  129. (interactive "P")
  130. (calc-slow-wrapper
  131. (calc-binary-op "scal" 'calcFunc-scf arg)))
  132. (defun calc-abssqr (arg)
  133. (interactive "P")
  134. (calc-slow-wrapper
  135. (calc-unary-op "absq" 'calcFunc-abssqr arg)))
  136. (defun calc-sign (arg)
  137. (interactive "P")
  138. (calc-slow-wrapper
  139. (calc-unary-op "sign" 'calcFunc-sign arg)))
  140. (defun calc-increment (arg)
  141. (interactive "p")
  142. (calc-wrapper
  143. (calc-enter-result 1 "incr" (list 'calcFunc-incr (calc-top-n 1) arg))))
  144. (defun calc-decrement (arg)
  145. (interactive "p")
  146. (calc-wrapper
  147. (calc-enter-result 1 "decr" (list 'calcFunc-decr (calc-top-n 1) arg))))
  148. (defun math-abs-approx (a)
  149. (cond ((Math-negp a)
  150. (math-neg a))
  151. ((Math-anglep a)
  152. a)
  153. ((eq (car a) 'cplx)
  154. (math-add (math-abs (nth 1 a)) (math-abs (nth 2 a))))
  155. ((eq (car a) 'polar)
  156. (nth 1 a))
  157. ((eq (car a) 'sdev)
  158. (math-abs-approx (nth 1 a)))
  159. ((eq (car a) 'intv)
  160. (math-max (math-abs (nth 2 a)) (math-abs (nth 3 a))))
  161. ((eq (car a) 'date)
  162. a)
  163. ((eq (car a) 'vec)
  164. (math-reduce-vec 'math-add-abs-approx a))
  165. ((eq (car a) 'calcFunc-abs)
  166. (car a))
  167. (t a)))
  168. (defun math-add-abs-approx (a b)
  169. (math-add (math-abs-approx a) (math-abs-approx b)))
  170. ;;;; Declarations.
  171. (defvar math-decls-cache-tag nil)
  172. (defvar math-decls-cache nil)
  173. (defvar math-decls-all nil)
  174. ;;; Math-decls-cache is an a-list where each entry is a list of the form:
  175. ;;; (VAR TYPES RANGE)
  176. ;;; where VAR is a variable name (with var- prefix) or function name;
  177. ;;; TYPES is a list of type symbols (any, int, frac, ...)
  178. ;;; RANGE is a sorted vector of intervals describing the range.
  179. (defvar math-super-types
  180. '((int numint rat real number)
  181. (numint real number)
  182. (frac rat real number)
  183. (rat real number)
  184. (float real number)
  185. (real number)
  186. (number)
  187. (scalar)
  188. (sqmatrix matrix vector)
  189. (matrix vector)
  190. (vector)
  191. (const)))
  192. (defun math-setup-declarations ()
  193. (or (eq math-decls-cache-tag (calc-var-value 'var-Decls))
  194. (let ((p (calc-var-value 'var-Decls))
  195. vec type range)
  196. (setq math-decls-cache-tag p
  197. math-decls-cache nil)
  198. (and (eq (car-safe p) 'vec)
  199. (while (setq p (cdr p))
  200. (and (eq (car-safe (car p)) 'vec)
  201. (setq vec (nth 2 (car p)))
  202. (condition-case err
  203. (let ((v (nth 1 (car p))))
  204. (setq type nil range nil)
  205. (or (eq (car-safe vec) 'vec)
  206. (setq vec (list 'vec vec)))
  207. (while (and (setq vec (cdr vec))
  208. (not (Math-objectp (car vec))))
  209. (and (eq (car-safe (car vec)) 'var)
  210. (let ((st (assq (nth 1 (car vec))
  211. math-super-types)))
  212. (cond (st (setq type (append type st)))
  213. ((eq (nth 1 (car vec)) 'pos)
  214. (setq type (append type
  215. '(real number))
  216. range
  217. '(intv 1 0 (var inf var-inf))))
  218. ((eq (nth 1 (car vec)) 'nonneg)
  219. (setq type (append type
  220. '(real number))
  221. range
  222. '(intv 3 0
  223. (var inf var-inf))))))))
  224. (if vec
  225. (setq type (append type '(real number))
  226. range (math-prepare-set (cons 'vec vec))))
  227. (setq type (list type range))
  228. (or (eq (car-safe v) 'vec)
  229. (setq v (list 'vec v)))
  230. (while (setq v (cdr v))
  231. (if (or (eq (car-safe (car v)) 'var)
  232. (not (Math-primp (car v))))
  233. (setq math-decls-cache
  234. (cons (cons (if (eq (car (car v)) 'var)
  235. (nth 2 (car v))
  236. (car (car v)))
  237. type)
  238. math-decls-cache)))))
  239. (error nil)))))
  240. (setq math-decls-all (assq 'var-All math-decls-cache)))))
  241. (defun math-known-scalarp (a &optional assume-scalar)
  242. (math-setup-declarations)
  243. (if (if calc-matrix-mode
  244. (eq calc-matrix-mode 'scalar)
  245. assume-scalar)
  246. (not (math-check-known-matrixp a))
  247. (math-check-known-scalarp a)))
  248. (defun math-known-matrixp (a)
  249. (and (not (Math-scalarp a))
  250. (not (math-known-scalarp a t))))
  251. (defun math-known-square-matrixp (a)
  252. (and (math-known-matrixp a)
  253. (math-check-known-square-matrixp a)))
  254. ;;; Try to prove that A is a scalar (i.e., a non-vector).
  255. (defun math-check-known-scalarp (a)
  256. (cond ((Math-objectp a) t)
  257. ((memq (car a) math-scalar-functions)
  258. t)
  259. ((memq (car a) math-real-scalar-functions)
  260. t)
  261. ((memq (car a) math-scalar-if-args-functions)
  262. (while (and (setq a (cdr a))
  263. (math-check-known-scalarp (car a))))
  264. (null a))
  265. ((eq (car a) '^)
  266. (math-check-known-scalarp (nth 1 a)))
  267. ((math-const-var a) t)
  268. (t
  269. (let ((decl (if (eq (car a) 'var)
  270. (or (assq (nth 2 a) math-decls-cache)
  271. math-decls-all)
  272. (assq (car a) math-decls-cache)))
  273. val)
  274. (cond
  275. ((memq 'scalar (nth 1 decl))
  276. t)
  277. ((and (eq (car a) 'var)
  278. (symbolp (nth 2 a))
  279. (boundp (nth 2 a))
  280. (setq val (symbol-value (nth 2 a))))
  281. (math-check-known-scalarp val))
  282. (t
  283. nil))))))
  284. ;;; Try to prove that A is *not* a scalar.
  285. (defun math-check-known-matrixp (a)
  286. (cond ((Math-objectp a) nil)
  287. ((memq (car a) math-nonscalar-functions)
  288. t)
  289. ((memq (car a) math-scalar-if-args-functions)
  290. (while (and (setq a (cdr a))
  291. (not (math-check-known-matrixp (car a)))))
  292. a)
  293. ((eq (car a) '^)
  294. (math-check-known-matrixp (nth 1 a)))
  295. ((math-const-var a) nil)
  296. (t
  297. (let ((decl (if (eq (car a) 'var)
  298. (or (assq (nth 2 a) math-decls-cache)
  299. math-decls-all)
  300. (assq (car a) math-decls-cache)))
  301. val)
  302. (cond
  303. ((memq 'matrix (nth 1 decl))
  304. t)
  305. ((and (eq (car a) 'var)
  306. (symbolp (nth 2 a))
  307. (boundp (nth 2 a))
  308. (setq val (symbol-value (nth 2 a))))
  309. (math-check-known-matrixp val))
  310. (t
  311. nil))))))
  312. ;;; Given that A is a matrix, try to prove that it is a square matrix.
  313. (defun math-check-known-square-matrixp (a)
  314. (cond ((math-square-matrixp a)
  315. t)
  316. ((eq (car-safe a) '^)
  317. (math-check-known-square-matrixp (nth 1 a)))
  318. ((or
  319. (eq (car-safe a) '*)
  320. (eq (car-safe a) '+)
  321. (eq (car-safe a) '-))
  322. (and
  323. (math-check-known-square-matrixp (nth 1 a))
  324. (math-check-known-square-matrixp (nth 2 a))))
  325. (t
  326. (let ((decl (if (eq (car a) 'var)
  327. (or (assq (nth 2 a) math-decls-cache)
  328. math-decls-all)
  329. (assq (car a) math-decls-cache)))
  330. val)
  331. (cond
  332. ((memq 'sqmatrix (nth 1 decl))
  333. t)
  334. ((and (eq (car a) 'var)
  335. (boundp (nth 2 a))
  336. (setq val (symbol-value (nth 2 a))))
  337. (math-check-known-square-matrixp val))
  338. ((and (or
  339. (integerp calc-matrix-mode)
  340. (eq calc-matrix-mode 'sqmatrix))
  341. (eq (car-safe a) 'var))
  342. t)
  343. ((memq 'matrix (nth 1 decl))
  344. nil)
  345. (t
  346. nil))))))
  347. ;;; Try to prove that A is a real (i.e., not complex).
  348. (defun math-known-realp (a)
  349. (< (math-possible-signs a) 8))
  350. ;;; Try to prove that A is real and positive.
  351. (defun math-known-posp (a)
  352. (eq (math-possible-signs a) 4))
  353. ;;; Try to prove that A is real and negative.
  354. (defun math-known-negp (a)
  355. (eq (math-possible-signs a) 1))
  356. ;;; Try to prove that A is real and nonnegative.
  357. (defun math-known-nonnegp (a)
  358. (memq (math-possible-signs a) '(2 4 6)))
  359. ;;; Try to prove that A is real and nonpositive.
  360. (defun math-known-nonposp (a)
  361. (memq (math-possible-signs a) '(1 2 3)))
  362. ;;; Try to prove that A is nonzero.
  363. (defun math-known-nonzerop (a)
  364. (memq (math-possible-signs a) '(1 4 5 8 9 12 13)))
  365. ;;; Return true if A is negative, or looks negative but we don't know.
  366. (defun math-guess-if-neg (a)
  367. (let ((sgn (math-possible-signs a)))
  368. (if (memq sgn '(1 3))
  369. t
  370. (if (memq sgn '(2 4 6))
  371. nil
  372. (math-looks-negp a)))))
  373. ;;; Find the possible signs of A, assuming A is a number of some kind.
  374. ;;; Returns an integer with bits: 1 may be negative,
  375. ;;; 2 may be zero,
  376. ;;; 4 may be positive,
  377. ;;; 8 may be nonreal.
  378. (defun math-possible-signs (a &optional origin)
  379. (cond ((Math-objectp a)
  380. (if origin (setq a (math-sub a origin)))
  381. (cond ((Math-posp a) 4)
  382. ((Math-negp a) 1)
  383. ((Math-zerop a) 2)
  384. ((eq (car a) 'intv)
  385. (cond
  386. ((math-known-posp (nth 2 a)) 4)
  387. ((math-known-negp (nth 3 a)) 1)
  388. ((Math-zerop (nth 2 a)) 6)
  389. ((Math-zerop (nth 3 a)) 3)
  390. (t 7)))
  391. ((eq (car a) 'sdev)
  392. (if (math-known-realp (nth 1 a)) 7 15))
  393. (t 8)))
  394. ((memq (car a) '(+ -))
  395. (cond ((Math-realp (nth 1 a))
  396. (if (eq (car a) '-)
  397. (math-neg-signs
  398. (math-possible-signs (nth 2 a)
  399. (if origin
  400. (math-add origin (nth 1 a))
  401. (nth 1 a))))
  402. (math-possible-signs (nth 2 a)
  403. (if origin
  404. (math-sub origin (nth 1 a))
  405. (math-neg (nth 1 a))))))
  406. ((Math-realp (nth 2 a))
  407. (let ((org (if (eq (car a) '-)
  408. (nth 2 a)
  409. (math-neg (nth 2 a)))))
  410. (math-possible-signs (nth 1 a)
  411. (if origin
  412. (math-add origin org)
  413. org))))
  414. (t
  415. (let ((s1 (math-possible-signs (nth 1 a) origin))
  416. (s2 (math-possible-signs (nth 2 a))))
  417. (if (eq (car a) '-) (setq s2 (math-neg-signs s2)))
  418. (cond ((eq s1 s2) s1)
  419. ((eq s1 2) s2)
  420. ((eq s2 2) s1)
  421. ((>= s1 8) 15)
  422. ((>= s2 8) 15)
  423. ((and (eq s1 4) (eq s2 6)) 4)
  424. ((and (eq s2 4) (eq s1 6)) 4)
  425. ((and (eq s1 1) (eq s2 3)) 1)
  426. ((and (eq s2 1) (eq s1 3)) 1)
  427. (t 7))))))
  428. ((eq (car a) 'neg)
  429. (math-neg-signs (math-possible-signs
  430. (nth 1 a)
  431. (and origin (math-neg origin)))))
  432. ((and origin (Math-zerop origin) (setq origin nil)
  433. nil))
  434. ((and (or (eq (car a) '*)
  435. (and (eq (car a) '/) origin))
  436. (Math-realp (nth 1 a)))
  437. (let ((s (if (eq (car a) '*)
  438. (if (Math-zerop (nth 1 a))
  439. (math-possible-signs 0 origin)
  440. (math-possible-signs (nth 2 a)
  441. (math-div (or origin 0)
  442. (nth 1 a))))
  443. (math-neg-signs
  444. (math-possible-signs (nth 2 a)
  445. (math-div (nth 1 a)
  446. origin))))))
  447. (if (Math-negp (nth 1 a)) (math-neg-signs s) s)))
  448. ((and (memq (car a) '(* /)) (Math-realp (nth 2 a)))
  449. (let ((s (math-possible-signs (nth 1 a)
  450. (if (eq (car a) '*)
  451. (math-mul (or origin 0) (nth 2 a))
  452. (math-div (or origin 0) (nth 2 a))))))
  453. (if (Math-negp (nth 2 a)) (math-neg-signs s) s)))
  454. ((eq (car a) 'vec)
  455. (let ((signs 0))
  456. (while (and (setq a (cdr a)) (< signs 15))
  457. (setq signs (logior signs (math-possible-signs
  458. (car a) origin))))
  459. signs))
  460. (t (let ((sign
  461. (cond
  462. ((memq (car a) '(* /))
  463. (let ((s1 (math-possible-signs (nth 1 a)))
  464. (s2 (math-possible-signs (nth 2 a))))
  465. (cond ((>= s1 8) 15)
  466. ((>= s2 8) 15)
  467. ((and (eq (car a) '/) (memq s2 '(2 3 6 7))) 15)
  468. (t
  469. (logior (if (memq s1 '(4 5 6 7)) s2 0)
  470. (if (memq s1 '(2 3 6 7)) 2 0)
  471. (if (memq s1 '(1 3 5 7))
  472. (math-neg-signs s2) 0))))))
  473. ((eq (car a) '^)
  474. (let ((s1 (math-possible-signs (nth 1 a)))
  475. (s2 (math-possible-signs (nth 2 a))))
  476. (cond ((>= s1 8) 15)
  477. ((>= s2 8) 15)
  478. ((eq s1 4) 4)
  479. ((eq s1 2) (if (eq s2 4) 2 15))
  480. ((eq s2 2) (if (memq s1 '(1 5)) 2 15))
  481. ((Math-integerp (nth 2 a))
  482. (if (math-evenp (nth 2 a))
  483. (if (memq s1 '(3 6 7)) 6 4)
  484. s1))
  485. ((eq s1 6) (if (eq s2 4) 6 15))
  486. (t 7))))
  487. ((eq (car a) '%)
  488. (let ((s2 (math-possible-signs (nth 2 a))))
  489. (cond ((>= s2 8) 7)
  490. ((eq s2 2) 2)
  491. ((memq s2 '(4 6)) 6)
  492. ((memq s2 '(1 3)) 3)
  493. (t 7))))
  494. ((and (memq (car a) '(calcFunc-abs calcFunc-abssqr))
  495. (= (length a) 2))
  496. (let ((s1 (math-possible-signs (nth 1 a))))
  497. (cond ((eq s1 2) 2)
  498. ((memq s1 '(1 4 5)) 4)
  499. (t 6))))
  500. ((and (eq (car a) 'calcFunc-exp) (= (length a) 2))
  501. (let ((s1 (math-possible-signs (nth 1 a))))
  502. (if (>= s1 8)
  503. 15
  504. (if (or (not origin) (math-negp origin))
  505. 4
  506. (setq origin (math-sub (or origin 0) 1))
  507. (if (Math-zerop origin) (setq origin nil))
  508. s1))))
  509. ((or (and (memq (car a) '(calcFunc-ln calcFunc-log10))
  510. (= (length a) 2))
  511. (and (eq (car a) 'calcFunc-log)
  512. (= (length a) 3)
  513. (math-known-posp (nth 2 a))))
  514. (if (math-known-nonnegp (nth 1 a))
  515. (math-possible-signs (nth 1 a) 1)
  516. 15))
  517. ((and (eq (car a) 'calcFunc-sqrt) (= (length a) 2))
  518. (let ((s1 (math-possible-signs (nth 1 a))))
  519. (if (memq s1 '(2 4 6)) s1 15)))
  520. ((memq (car a) math-nonnegative-functions) 6)
  521. ((memq (car a) math-positive-functions) 4)
  522. ((memq (car a) math-real-functions) 7)
  523. ((memq (car a) math-real-scalar-functions) 7)
  524. ((and (memq (car a) math-real-if-arg-functions)
  525. (= (length a) 2))
  526. (if (math-known-realp (nth 1 a)) 7 15)))))
  527. (cond (sign
  528. (if origin
  529. (+ (logand sign 8)
  530. (if (Math-posp origin)
  531. (if (memq sign '(1 2 3 8 9 10 11)) 1 7)
  532. (if (memq sign '(2 4 6 8 10 12 14)) 4 7)))
  533. sign))
  534. ((math-const-var a)
  535. (cond ((eq (nth 2 a) 'var-pi)
  536. (if origin
  537. (math-possible-signs (math-pi) origin)
  538. 4))
  539. ((eq (nth 2 a) 'var-e)
  540. (if origin
  541. (math-possible-signs (math-e) origin)
  542. 4))
  543. ((eq (nth 2 a) 'var-inf) 4)
  544. ((eq (nth 2 a) 'var-uinf) 13)
  545. ((eq (nth 2 a) 'var-i) 8)
  546. (t 15)))
  547. (t
  548. (math-setup-declarations)
  549. (let ((decl (if (eq (car a) 'var)
  550. (or (assq (nth 2 a) math-decls-cache)
  551. math-decls-all)
  552. (assq (car a) math-decls-cache))))
  553. (if (and origin
  554. (memq 'int (nth 1 decl))
  555. (not (Math-num-integerp origin)))
  556. 5
  557. (if (nth 2 decl)
  558. (math-possible-signs (nth 2 decl) origin)
  559. (if (memq 'real (nth 1 decl))
  560. 7
  561. 15))))))))))
  562. (defun math-neg-signs (s1)
  563. (if (>= s1 8)
  564. (+ 8 (math-neg-signs (- s1 8)))
  565. (+ (if (memq s1 '(1 3 5 7)) 4 0)
  566. (if (memq s1 '(2 3 6 7)) 2 0)
  567. (if (memq s1 '(4 5 6 7)) 1 0))))
  568. ;;; Try to prove that A is an integer.
  569. (defun math-known-integerp (a)
  570. (eq (math-possible-types a) 1))
  571. (defun math-known-num-integerp (a)
  572. (<= (math-possible-types a t) 3))
  573. (defun math-known-imagp (a)
  574. (= (math-possible-types a) 16))
  575. ;;; Find the possible types of A.
  576. ;;; Returns an integer with bits: 1 may be integer.
  577. ;;; 2 may be integer-valued float.
  578. ;;; 4 may be fraction.
  579. ;;; 8 may be non-integer-valued float.
  580. ;;; 16 may be imaginary.
  581. ;;; 32 may be non-real, non-imaginary.
  582. ;;; Real infinities count as integers for the purposes of this function.
  583. (defun math-possible-types (a &optional num)
  584. (cond ((Math-objectp a)
  585. (cond ((Math-integerp a) (if num 3 1))
  586. ((Math-messy-integerp a) (if num 3 2))
  587. ((eq (car a) 'frac) (if num 12 4))
  588. ((eq (car a) 'float) (if num 12 8))
  589. ((eq (car a) 'intv)
  590. (if (equal (nth 2 a) (nth 3 a))
  591. (math-possible-types (nth 2 a))
  592. 15))
  593. ((eq (car a) 'sdev)
  594. (if (math-known-realp (nth 1 a)) 15 63))
  595. ((eq (car a) 'cplx)
  596. (if (math-zerop (nth 1 a)) 16 32))
  597. ((eq (car a) 'polar)
  598. (if (or (Math-equal (nth 2 a) (math-quarter-circle nil))
  599. (Math-equal (nth 2 a)
  600. (math-neg (math-quarter-circle nil))))
  601. 16 48))
  602. (t 63)))
  603. ((eq (car a) '/)
  604. (let* ((t1 (math-possible-types (nth 1 a) num))
  605. (t2 (math-possible-types (nth 2 a) num))
  606. (t12 (logior t1 t2)))
  607. (if (< t12 16)
  608. (if (> (logand t12 10) 0)
  609. 10
  610. (if (or (= t1 4) (= t2 4) calc-prefer-frac)
  611. 5
  612. 15))
  613. (if (< t12 32)
  614. (if (= t1 16)
  615. (if (= t2 16) 15
  616. (if (< t2 16) 16 31))
  617. (if (= t2 16)
  618. (if (< t1 16) 16 31)
  619. 31))
  620. 63))))
  621. ((memq (car a) '(+ - * %))
  622. (let* ((t1 (math-possible-types (nth 1 a) num))
  623. (t2 (math-possible-types (nth 2 a) num))
  624. (t12 (logior t1 t2)))
  625. (if (eq (car a) '%)
  626. (setq t1 (logand t1 15) t2 (logand t2 15) t12 (logand t12 15)))
  627. (if (< t12 16)
  628. (let ((mask (if (<= t12 3)
  629. 1
  630. (if (and (or (and (<= t1 3) (= (logand t2 3) 0))
  631. (and (<= t2 3) (= (logand t1 3) 0)))
  632. (memq (car a) '(+ -)))
  633. 4
  634. 5))))
  635. (if num
  636. (* mask 3)
  637. (logior (if (and (> (logand t1 5) 0) (> (logand t2 5) 0))
  638. mask 0)
  639. (if (> (logand t12 10) 0)
  640. (* mask 2) 0))))
  641. (if (< t12 32)
  642. (if (eq (car a) '*)
  643. (if (= t1 16)
  644. (if (= t2 16) 15
  645. (if (< t2 16) 16 31))
  646. (if (= t2 16)
  647. (if (< t1 16) 16 31)
  648. 31))
  649. (if (= t12 16) 16
  650. (if (or (and (= t1 16) (< t2 16))
  651. (and (= t2 16) (< t1 16))) 32 63)))
  652. 63))))
  653. ((eq (car a) 'neg)
  654. (math-possible-types (nth 1 a)))
  655. ((eq (car a) '^)
  656. (let* ((t1 (math-possible-types (nth 1 a) num))
  657. (t2 (math-possible-types (nth 2 a) num))
  658. (t12 (logior t1 t2)))
  659. (if (and (<= t2 3) (math-known-nonnegp (nth 2 a)) (< t1 16))
  660. (let ((mask (logior (if (> (logand t1 3) 0) 1 0)
  661. (logand t1 4)
  662. (if (> (logand t1 12) 0) 5 0))))
  663. (if num
  664. (* mask 3)
  665. (logior (if (and (> (logand t1 5) 0) (> (logand t2 5) 0))
  666. mask 0)
  667. (if (> (logand t12 10) 0)
  668. (* mask 2) 0))))
  669. (if (and (math-known-nonnegp (nth 1 a))
  670. (math-known-posp (nth 2 a)))
  671. 15
  672. 63))))
  673. ((eq (car a) 'calcFunc-sqrt)
  674. (let ((t1 (math-possible-signs (nth 1 a))))
  675. (logior (if (> (logand t1 2) 0) 3 0)
  676. (if (> (logand t1 1) 0) 16 0)
  677. (if (> (logand t1 4) 0) 15 0)
  678. (if (> (logand t1 8) 0) 32 0))))
  679. ((eq (car a) 'vec)
  680. (let ((types 0))
  681. (while (and (setq a (cdr a)) (< types 63))
  682. (setq types (logior types (math-possible-types (car a) t))))
  683. types))
  684. ((or (memq (car a) math-integer-functions)
  685. (and (memq (car a) math-rounding-functions)
  686. (math-known-nonnegp (or (nth 2 a) 0))))
  687. 1)
  688. ((or (memq (car a) math-num-integer-functions)
  689. (and (memq (car a) math-float-rounding-functions)
  690. (math-known-nonnegp (or (nth 2 a) 0))))
  691. 2)
  692. ((eq (car a) 'calcFunc-frac)
  693. 5)
  694. ((and (eq (car a) 'calcFunc-float) (= (length a) 2))
  695. (let ((t1 (math-possible-types (nth 1 a))))
  696. (logior (if (> (logand t1 3) 0) 2 0)
  697. (if (> (logand t1 12) 0) 8 0)
  698. (logand t1 48))))
  699. ((and (memq (car a) '(calcFunc-abs calcFunc-abssqr))
  700. (= (length a) 2))
  701. (let ((t1 (math-possible-types (nth 1 a))))
  702. (if (>= t1 16)
  703. 15
  704. t1)))
  705. ((math-const-var a)
  706. (cond ((memq (nth 2 a) '(var-e var-pi var-phi var-gamma)) 8)
  707. ((eq (nth 2 a) 'var-inf) 1)
  708. ((eq (nth 2 a) 'var-i) 16)
  709. (t 63)))
  710. (t
  711. (math-setup-declarations)
  712. (let ((decl (if (eq (car a) 'var)
  713. (or (assq (nth 2 a) math-decls-cache)
  714. math-decls-all)
  715. (assq (car a) math-decls-cache))))
  716. (cond ((memq 'int (nth 1 decl))
  717. 1)
  718. ((memq 'numint (nth 1 decl))
  719. 3)
  720. ((memq 'frac (nth 1 decl))
  721. 4)
  722. ((memq 'rat (nth 1 decl))
  723. 5)
  724. ((memq 'float (nth 1 decl))
  725. 10)
  726. ((nth 2 decl)
  727. (math-possible-types (nth 2 decl)))
  728. ((memq 'real (nth 1 decl))
  729. 15)
  730. (t 63))))))
  731. (defun math-known-evenp (a)
  732. (cond ((Math-integerp a)
  733. (math-evenp a))
  734. ((Math-messy-integerp a)
  735. (or (> (nth 2 a) 0)
  736. (math-evenp (math-trunc a))))
  737. ((eq (car a) '*)
  738. (if (math-known-evenp (nth 1 a))
  739. (math-known-num-integerp (nth 2 a))
  740. (if (math-known-num-integerp (nth 1 a))
  741. (math-known-evenp (nth 2 a)))))
  742. ((memq (car a) '(+ -))
  743. (or (and (math-known-evenp (nth 1 a))
  744. (math-known-evenp (nth 2 a)))
  745. (and (math-known-oddp (nth 1 a))
  746. (math-known-oddp (nth 2 a)))))
  747. ((eq (car a) 'neg)
  748. (math-known-evenp (nth 1 a)))))
  749. (defun math-known-oddp (a)
  750. (cond ((Math-integerp a)
  751. (math-oddp a))
  752. ((Math-messy-integerp a)
  753. (and (<= (nth 2 a) 0)
  754. (math-oddp (math-trunc a))))
  755. ((memq (car a) '(+ -))
  756. (or (and (math-known-evenp (nth 1 a))
  757. (math-known-oddp (nth 2 a)))
  758. (and (math-known-oddp (nth 1 a))
  759. (math-known-evenp (nth 2 a)))))
  760. ((eq (car a) 'neg)
  761. (math-known-oddp (nth 1 a)))))
  762. (defun calcFunc-dreal (expr)
  763. (let ((types (math-possible-types expr)))
  764. (if (< types 16) 1
  765. (if (= (logand types 15) 0) 0
  766. (math-reject-arg expr 'realp 'quiet)))))
  767. (defun calcFunc-dimag (expr)
  768. (let ((types (math-possible-types expr)))
  769. (if (= types 16) 1
  770. (if (= (logand types 16) 0) 0
  771. (math-reject-arg expr "Expected an imaginary number")))))
  772. (defun calcFunc-dpos (expr)
  773. (let ((signs (math-possible-signs expr)))
  774. (if (eq signs 4) 1
  775. (if (memq signs '(1 2 3)) 0
  776. (math-reject-arg expr 'posp 'quiet)))))
  777. (defun calcFunc-dneg (expr)
  778. (let ((signs (math-possible-signs expr)))
  779. (if (eq signs 1) 1
  780. (if (memq signs '(2 4 6)) 0
  781. (math-reject-arg expr 'negp 'quiet)))))
  782. (defun calcFunc-dnonneg (expr)
  783. (let ((signs (math-possible-signs expr)))
  784. (if (memq signs '(2 4 6)) 1
  785. (if (eq signs 1) 0
  786. (math-reject-arg expr 'posp 'quiet)))))
  787. (defun calcFunc-dnonzero (expr)
  788. (let ((signs (math-possible-signs expr)))
  789. (if (memq signs '(1 4 5 8 9 12 13)) 1
  790. (if (eq signs 2) 0
  791. (math-reject-arg expr 'nonzerop 'quiet)))))
  792. (defun calcFunc-dint (expr)
  793. (let ((types (math-possible-types expr)))
  794. (if (= types 1) 1
  795. (if (= (logand types 1) 0) 0
  796. (math-reject-arg expr 'integerp 'quiet)))))
  797. (defun calcFunc-dnumint (expr)
  798. (let ((types (math-possible-types expr t)))
  799. (if (<= types 3) 1
  800. (if (= (logand types 3) 0) 0
  801. (math-reject-arg expr 'integerp 'quiet)))))
  802. (defun calcFunc-dnatnum (expr)
  803. (let ((res (calcFunc-dint expr)))
  804. (if (eq res 1)
  805. (calcFunc-dnonneg expr)
  806. res)))
  807. (defun calcFunc-deven (expr)
  808. (if (math-known-evenp expr)
  809. 1
  810. (if (or (math-known-oddp expr)
  811. (= (logand (math-possible-types expr) 3) 0))
  812. 0
  813. (math-reject-arg expr "Can't tell if expression is odd or even"))))
  814. (defun calcFunc-dodd (expr)
  815. (if (math-known-oddp expr)
  816. 1
  817. (if (or (math-known-evenp expr)
  818. (= (logand (math-possible-types expr) 3) 0))
  819. 0
  820. (math-reject-arg expr "Can't tell if expression is odd or even"))))
  821. (defun calcFunc-drat (expr)
  822. (let ((types (math-possible-types expr)))
  823. (if (memq types '(1 4 5)) 1
  824. (if (= (logand types 5) 0) 0
  825. (math-reject-arg expr "Rational number expected")))))
  826. (defun calcFunc-drange (expr)
  827. (math-setup-declarations)
  828. (let (range)
  829. (if (Math-realp expr)
  830. (list 'vec expr)
  831. (if (eq (car-safe expr) 'intv)
  832. expr
  833. (if (eq (car-safe expr) 'var)
  834. (setq range (nth 2 (or (assq (nth 2 expr) math-decls-cache)
  835. math-decls-all)))
  836. (setq range (nth 2 (assq (car-safe expr) math-decls-cache))))
  837. (if range
  838. (math-clean-set (copy-sequence range))
  839. (setq range (math-possible-signs expr))
  840. (if (< range 8)
  841. (aref [(vec)
  842. (intv 2 (neg (var inf var-inf)) 0)
  843. (vec 0)
  844. (intv 3 (neg (var inf var-inf)) 0)
  845. (intv 1 0 (var inf var-inf))
  846. (vec (intv 2 (neg (var inf var-inf)) 0)
  847. (intv 1 0 (var inf var-inf)))
  848. (intv 3 0 (var inf var-inf))
  849. (intv 3 (neg (var inf var-inf)) (var inf var-inf))] range)
  850. (math-reject-arg expr 'realp 'quiet)))))))
  851. (defun calcFunc-dscalar (a)
  852. (if (math-known-scalarp a) 1
  853. (if (math-known-matrixp a) 0
  854. (math-reject-arg a 'objectp 'quiet))))
  855. ;;;; Arithmetic.
  856. (defsubst calcFunc-neg (a)
  857. (math-normalize (list 'neg a)))
  858. (defun math-neg-fancy (a)
  859. (cond ((eq (car a) 'polar)
  860. (list 'polar
  861. (nth 1 a)
  862. (if (math-posp (nth 2 a))
  863. (math-sub (nth 2 a) (math-half-circle nil))
  864. (math-add (nth 2 a) (math-half-circle nil)))))
  865. ((eq (car a) 'mod)
  866. (if (math-zerop (nth 1 a))
  867. a
  868. (list 'mod (math-sub (nth 2 a) (nth 1 a)) (nth 2 a))))
  869. ((eq (car a) 'sdev)
  870. (list 'sdev (math-neg (nth 1 a)) (nth 2 a)))
  871. ((eq (car a) 'intv)
  872. (math-make-intv (aref [0 2 1 3] (nth 1 a))
  873. (math-neg (nth 3 a))
  874. (math-neg (nth 2 a))))
  875. ((and math-simplify-only
  876. (not (equal a math-simplify-only)))
  877. (list 'neg a))
  878. ((eq (car a) '+)
  879. (math-sub (math-neg (nth 1 a)) (nth 2 a)))
  880. ((eq (car a) '-)
  881. (math-sub (nth 2 a) (nth 1 a)))
  882. ((and (memq (car a) '(* /))
  883. (math-okay-neg (nth 1 a)))
  884. (list (car a) (math-neg (nth 1 a)) (nth 2 a)))
  885. ((and (memq (car a) '(* /))
  886. (math-okay-neg (nth 2 a)))
  887. (list (car a) (nth 1 a) (math-neg (nth 2 a))))
  888. ((and (memq (car a) '(* /))
  889. (or (math-objectp (nth 1 a))
  890. (and (eq (car (nth 1 a)) '*)
  891. (math-objectp (nth 1 (nth 1 a))))))
  892. (list (car a) (math-neg (nth 1 a)) (nth 2 a)))
  893. ((and (eq (car a) '/)
  894. (or (math-objectp (nth 2 a))
  895. (and (eq (car (nth 2 a)) '*)
  896. (math-objectp (nth 1 (nth 2 a))))))
  897. (list (car a) (nth 1 a) (math-neg (nth 2 a))))
  898. ((and (eq (car a) 'var) (memq (nth 2 a) '(var-uinf var-nan)))
  899. a)
  900. ((eq (car a) 'neg)
  901. (nth 1 a))
  902. (t (list 'neg a))))
  903. (defun math-okay-neg (a)
  904. (or (math-looks-negp a)
  905. (eq (car-safe a) '-)))
  906. (defun math-neg-float (a)
  907. (list 'float (Math-integer-neg (nth 1 a)) (nth 2 a)))
  908. (defun calcFunc-add (&rest rest)
  909. (if rest
  910. (let ((a (car rest)))
  911. (while (setq rest (cdr rest))
  912. (setq a (list '+ a (car rest))))
  913. (math-normalize a))
  914. 0))
  915. (defun calcFunc-sub (&rest rest)
  916. (if rest
  917. (let ((a (car rest)))
  918. (while (setq rest (cdr rest))
  919. (setq a (list '- a (car rest))))
  920. (math-normalize a))
  921. 0))
  922. (defun math-add-objects-fancy (a b)
  923. (cond ((and (Math-numberp a) (Math-numberp b))
  924. (let ((aa (math-complex a))
  925. (bb (math-complex b)))
  926. (math-normalize
  927. (let ((res (list 'cplx
  928. (math-add (nth 1 aa) (nth 1 bb))
  929. (math-add (nth 2 aa) (nth 2 bb)))))
  930. (if (math-want-polar a b)
  931. (math-polar res)
  932. res)))))
  933. ((or (Math-vectorp a) (Math-vectorp b))
  934. (math-map-vec-2 'math-add a b))
  935. ((eq (car-safe a) 'sdev)
  936. (if (eq (car-safe b) 'sdev)
  937. (math-make-sdev (math-add (nth 1 a) (nth 1 b))
  938. (math-hypot (nth 2 a) (nth 2 b)))
  939. (and (or (Math-scalarp b)
  940. (not (Math-objvecp b)))
  941. (math-make-sdev (math-add (nth 1 a) b) (nth 2 a)))))
  942. ((and (eq (car-safe b) 'sdev)
  943. (or (Math-scalarp a)
  944. (not (Math-objvecp a))))
  945. (math-make-sdev (math-add a (nth 1 b)) (nth 2 b)))
  946. ((eq (car-safe a) 'intv)
  947. (if (eq (car-safe b) 'intv)
  948. (math-make-intv (logior (logand (nth 1 a) (nth 1 b))
  949. (if (equal (nth 2 a)
  950. '(neg (var inf var-inf)))
  951. (logand (nth 1 a) 2) 0)
  952. (if (equal (nth 2 b)
  953. '(neg (var inf var-inf)))
  954. (logand (nth 1 b) 2) 0)
  955. (if (equal (nth 3 a) '(var inf var-inf))
  956. (logand (nth 1 a) 1) 0)
  957. (if (equal (nth 3 b) '(var inf var-inf))
  958. (logand (nth 1 b) 1) 0))
  959. (math-add (nth 2 a) (nth 2 b))
  960. (math-add (nth 3 a) (nth 3 b)))
  961. (and (or (Math-anglep b)
  962. (eq (car b) 'date)
  963. (not (Math-objvecp b)))
  964. (math-make-intv (nth 1 a)
  965. (math-add (nth 2 a) b)
  966. (math-add (nth 3 a) b)))))
  967. ((and (eq (car-safe b) 'intv)
  968. (or (Math-anglep a)
  969. (eq (car a) 'date)
  970. (not (Math-objvecp a))))
  971. (math-make-intv (nth 1 b)
  972. (math-add a (nth 2 b))
  973. (math-add a (nth 3 b))))
  974. ((eq (car-safe a) 'date)
  975. (cond ((eq (car-safe b) 'date)
  976. (math-add (nth 1 a) (nth 1 b)))
  977. ((eq (car-safe b) 'hms)
  978. (let ((parts (math-date-parts (nth 1 a))))
  979. (list 'date
  980. (math-add (car parts) ; this minimizes roundoff
  981. (math-div (math-add
  982. (math-add (nth 1 parts)
  983. (nth 2 parts))
  984. (math-add
  985. (math-mul (nth 1 b) 3600)
  986. (math-add (math-mul (nth 2 b) 60)
  987. (nth 3 b))))
  988. 86400)))))
  989. ((Math-realp b)
  990. (list 'date (math-add (nth 1 a) b)))
  991. (t nil)))
  992. ((eq (car-safe b) 'date)
  993. (math-add-objects-fancy b a))
  994. ((and (eq (car-safe a) 'mod)
  995. (eq (car-safe b) 'mod)
  996. (equal (nth 2 a) (nth 2 b)))
  997. (math-make-mod (math-add (nth 1 a) (nth 1 b)) (nth 2 a)))
  998. ((and (eq (car-safe a) 'mod)
  999. (Math-anglep b))
  1000. (math-make-mod (math-add (nth 1 a) b) (nth 2 a)))
  1001. ((and (eq (car-safe b) 'mod)
  1002. (Math-anglep a))
  1003. (math-make-mod (math-add a (nth 1 b)) (nth 2 b)))
  1004. ((and (or (eq (car-safe a) 'hms) (eq (car-safe b) 'hms))
  1005. (and (Math-anglep a) (Math-anglep b)))
  1006. (or (eq (car-safe a) 'hms) (setq a (math-to-hms a)))
  1007. (or (eq (car-safe b) 'hms) (setq b (math-to-hms b)))
  1008. (math-normalize
  1009. (if (math-negp a)
  1010. (math-neg (math-add (math-neg a) (math-neg b)))
  1011. (if (math-negp b)
  1012. (let* ((s (math-add (nth 3 a) (nth 3 b)))
  1013. (m (math-add (nth 2 a) (nth 2 b)))
  1014. (h (math-add (nth 1 a) (nth 1 b))))
  1015. (if (math-negp s)
  1016. (setq s (math-add s 60)
  1017. m (math-add m -1)))
  1018. (if (math-negp m)
  1019. (setq m (math-add m 60)
  1020. h (math-add h -1)))
  1021. (if (math-negp h)
  1022. (math-add b a)
  1023. (list 'hms h m s)))
  1024. (let* ((s (math-add (nth 3 a) (nth 3 b)))
  1025. (m (math-add (nth 2 a) (nth 2 b)))
  1026. (h (math-add (nth 1 a) (nth 1 b))))
  1027. (list 'hms h m s))))))
  1028. (t (calc-record-why "*Incompatible arguments for +" a b))))
  1029. (defun math-add-symb-fancy (a b)
  1030. (or (and math-simplify-only
  1031. (not (equal a math-simplify-only))
  1032. (list '+ a b))
  1033. (and (eq (car-safe b) '+)
  1034. (math-add (math-add a (nth 1 b))
  1035. (nth 2 b)))
  1036. (and (eq (car-safe b) '-)
  1037. (math-sub (math-add a (nth 1 b))
  1038. (nth 2 b)))
  1039. (and (eq (car-safe b) 'neg)
  1040. (eq (car-safe (nth 1 b)) '+)
  1041. (math-sub (math-sub a (nth 1 (nth 1 b)))
  1042. (nth 2 (nth 1 b))))
  1043. (and (or (and (Math-vectorp a) (math-known-scalarp b))
  1044. (and (Math-vectorp b) (math-known-scalarp a)))
  1045. (math-map-vec-2 'math-add a b))
  1046. (let ((inf (math-infinitep a)))
  1047. (cond
  1048. (inf
  1049. (let ((inf2 (math-infinitep b)))
  1050. (if inf2
  1051. (if (or (memq (nth 2 inf) '(var-uinf var-nan))
  1052. (memq (nth 2 inf2) '(var-uinf var-nan)))
  1053. '(var nan var-nan)
  1054. (let ((dir (math-infinite-dir a inf))
  1055. (dir2 (math-infinite-dir b inf2)))
  1056. (if (and (Math-objectp dir) (Math-objectp dir2))
  1057. (if (Math-equal dir dir2)
  1058. a
  1059. '(var nan var-nan)))))
  1060. (if (and (equal a '(var inf var-inf))
  1061. (eq (car-safe b) 'intv)
  1062. (memq (nth 1 b) '(2 3))
  1063. (equal (nth 2 b) '(neg (var inf var-inf))))
  1064. (list 'intv 3 (nth 2 b) a)
  1065. (if (and (equal a '(neg (var inf var-inf)))
  1066. (eq (car-safe b) 'intv)
  1067. (memq (nth 1 b) '(1 3))
  1068. (equal (nth 3 b) '(var inf var-inf)))
  1069. (list 'intv 3 a (nth 3 b))
  1070. a)))))
  1071. ((math-infinitep b)
  1072. (if (eq (car-safe a) 'intv)
  1073. (math-add b a)
  1074. b))
  1075. ((eq (car-safe a) '+)
  1076. (let ((temp (math-combine-sum (nth 2 a) b nil nil t)))
  1077. (and temp
  1078. (math-add (nth 1 a) temp))))
  1079. ((eq (car-safe a) '-)
  1080. (let ((temp (math-combine-sum (nth 2 a) b t nil t)))
  1081. (and temp
  1082. (math-add (nth 1 a) temp))))
  1083. ((and (Math-objectp a) (Math-objectp b))
  1084. nil)
  1085. (t
  1086. (math-combine-sum a b nil nil nil))))
  1087. (and (Math-looks-negp b)
  1088. (list '- a (math-neg b)))
  1089. (and (Math-looks-negp a)
  1090. (list '- b (math-neg a)))
  1091. (and (eq (car-safe a) 'calcFunc-idn)
  1092. (= (length a) 2)
  1093. (or (and (eq (car-safe b) 'calcFunc-idn)
  1094. (= (length b) 2)
  1095. (list 'calcFunc-idn (math-add (nth 1 a) (nth 1 b))))
  1096. (and (math-square-matrixp b)
  1097. (math-add (math-mimic-ident (nth 1 a) b) b))
  1098. (and (math-known-scalarp b)
  1099. (math-add (nth 1 a) b))))
  1100. (and (eq (car-safe b) 'calcFunc-idn)
  1101. (= (length b) 2)
  1102. (or (and (math-square-matrixp a)
  1103. (math-add a (math-mimic-ident (nth 1 b) a)))
  1104. (and (math-known-scalarp a)
  1105. (math-add a (nth 1 b)))))
  1106. (list '+ a b)))
  1107. (defun calcFunc-mul (&rest rest)
  1108. (if rest
  1109. (let ((a (car rest)))
  1110. (while (setq rest (cdr rest))
  1111. (setq a (list '* a (car rest))))
  1112. (math-normalize a))
  1113. 1))
  1114. (defun math-mul-objects-fancy (a b)
  1115. (cond ((and (Math-numberp a) (Math-numberp b))
  1116. (math-normalize
  1117. (if (math-want-polar a b)
  1118. (let ((a (math-polar a))
  1119. (b (math-polar b)))
  1120. (list 'polar
  1121. (math-mul (nth 1 a) (nth 1 b))
  1122. (math-fix-circular (math-add (nth 2 a) (nth 2 b)))))
  1123. (setq a (math-complex a)
  1124. b (math-complex b))
  1125. (list 'cplx
  1126. (math-sub (math-mul (nth 1 a) (nth 1 b))
  1127. (math-mul (nth 2 a) (nth 2 b)))
  1128. (math-add (math-mul (nth 1 a) (nth 2 b))
  1129. (math-mul (nth 2 a) (nth 1 b)))))))
  1130. ((Math-vectorp a)
  1131. (if (Math-vectorp b)
  1132. (if (math-matrixp a)
  1133. (if (math-matrixp b)
  1134. (if (= (length (nth 1 a)) (length b))
  1135. (math-mul-mats a b)
  1136. (math-dimension-error))
  1137. (if (= (length (nth 1 a)) 2)
  1138. (if (= (length a) (length b))
  1139. (math-mul-mats a (list 'vec b))
  1140. (math-dimension-error))
  1141. (if (= (length (nth 1 a)) (length b))
  1142. (math-mul-mat-vec a b)
  1143. (math-dimension-error))))
  1144. (if (math-matrixp b)
  1145. (if (= (length a) (length b))
  1146. (nth 1 (math-mul-mats (list 'vec a) b))
  1147. (math-dimension-error))
  1148. (if (= (length a) (length b))
  1149. (math-dot-product a b)
  1150. (math-dimension-error))))
  1151. (math-map-vec-2 'math-mul a b)))
  1152. ((Math-vectorp b)
  1153. (math-map-vec-2 'math-mul a b))
  1154. ((eq (car-safe a) 'sdev)
  1155. (if (eq (car-safe b) 'sdev)
  1156. (math-make-sdev (math-mul (nth 1 a) (nth 1 b))
  1157. (math-hypot (math-mul (nth 2 a) (nth 1 b))
  1158. (math-mul (nth 2 b) (nth 1 a))))
  1159. (and (or (Math-scalarp b)
  1160. (not (Math-objvecp b)))
  1161. (math-make-sdev (math-mul (nth 1 a) b)
  1162. (math-mul (nth 2 a) b)))))
  1163. ((and (eq (car-safe b) 'sdev)
  1164. (or (Math-scalarp a)
  1165. (not (Math-objvecp a))))
  1166. (math-make-sdev (math-mul a (nth 1 b)) (math-mul a (nth 2 b))))
  1167. ((and (eq (car-safe a) 'intv) (Math-anglep b))
  1168. (if (Math-negp b)
  1169. (math-neg (math-mul a (math-neg b)))
  1170. (math-make-intv (nth 1 a)
  1171. (math-mul (nth 2 a) b)
  1172. (math-mul (nth 3 a) b))))
  1173. ((and (eq (car-safe b) 'intv) (Math-anglep a))
  1174. (math-mul b a))
  1175. ((and (eq (car-safe a) 'intv) (math-intv-constp a)
  1176. (eq (car-safe b) 'intv) (math-intv-constp b))
  1177. (let ((lo (math-mul a (nth 2 b)))
  1178. (hi (math-mul a (nth 3 b))))
  1179. (or (eq (car-safe lo) 'intv)
  1180. (setq lo (list 'intv (if (memq (nth 1 b) '(2 3)) 3 0) lo lo)))
  1181. (or (eq (car-safe hi) 'intv)
  1182. (setq hi (list 'intv (if (memq (nth 1 b) '(1 3)) 3 0) hi hi)))
  1183. (math-combine-intervals
  1184. (nth 2 lo) (and (or (memq (nth 1 b) '(2 3))
  1185. (math-infinitep (nth 2 lo)))
  1186. (memq (nth 1 lo) '(2 3)))
  1187. (nth 3 lo) (and (or (memq (nth 1 b) '(2 3))
  1188. (math-infinitep (nth 3 lo)))
  1189. (memq (nth 1 lo) '(1 3)))
  1190. (nth 2 hi) (and (or (memq (nth 1 b) '(1 3))
  1191. (math-infinitep (nth 2 hi)))
  1192. (memq (nth 1 hi) '(2 3)))
  1193. (nth 3 hi) (and (or (memq (nth 1 b) '(1 3))
  1194. (math-infinitep (nth 3 hi)))
  1195. (memq (nth 1 hi) '(1 3))))))
  1196. ((and (eq (car-safe a) 'mod)
  1197. (eq (car-safe b) 'mod)
  1198. (equal (nth 2 a) (nth 2 b)))
  1199. (math-make-mod (math-mul (nth 1 a) (nth 1 b)) (nth 2 a)))
  1200. ((and (eq (car-safe a) 'mod)
  1201. (Math-anglep b))
  1202. (math-make-mod (math-mul (nth 1 a) b) (nth 2 a)))
  1203. ((and (eq (car-safe b) 'mod)
  1204. (Math-anglep a))
  1205. (math-make-mod (math-mul a (nth 1 b)) (nth 2 b)))
  1206. ((and (eq (car-safe a) 'hms) (Math-realp b))
  1207. (math-with-extra-prec 2
  1208. (math-to-hms (math-mul (math-from-hms a 'deg) b) 'deg)))
  1209. ((and (eq (car-safe b) 'hms) (Math-realp a))
  1210. (math-mul b a))
  1211. (t (calc-record-why "*Incompatible arguments for *" a b))))
  1212. ;;; Fast function to multiply floating-point numbers.
  1213. (defun math-mul-float (a b) ; [F F F]
  1214. (math-make-float (math-mul (nth 1 a) (nth 1 b))
  1215. (+ (nth 2 a) (nth 2 b))))
  1216. (defun math-sqr-float (a) ; [F F]
  1217. (math-make-float (math-mul (nth 1 a) (nth 1 a))
  1218. (+ (nth 2 a) (nth 2 a))))
  1219. (defun math-intv-constp (a &optional finite)
  1220. (and (or (Math-anglep (nth 2 a))
  1221. (and (equal (nth 2 a) '(neg (var inf var-inf)))
  1222. (or (not finite)
  1223. (memq (nth 1 a) '(0 1)))))
  1224. (or (Math-anglep (nth 3 a))
  1225. (and (equal (nth 3 a) '(var inf var-inf))
  1226. (or (not finite)
  1227. (memq (nth 1 a) '(0 2)))))))
  1228. (defun math-mul-zero (a b)
  1229. (if (math-known-matrixp b)
  1230. (if (math-vectorp b)
  1231. (math-map-vec-2 'math-mul a b)
  1232. (math-mimic-ident 0 b))
  1233. (if (math-infinitep b)
  1234. '(var nan var-nan)
  1235. (let ((aa nil) (bb nil))
  1236. (if (and (eq (car-safe b) 'intv)
  1237. (progn
  1238. (and (equal (nth 2 b) '(neg (var inf var-inf)))
  1239. (memq (nth 1 b) '(2 3))
  1240. (setq aa (nth 2 b)))
  1241. (and (equal (nth 3 b) '(var inf var-inf))
  1242. (memq (nth 1 b) '(1 3))
  1243. (setq bb (nth 3 b)))
  1244. (or aa bb)))
  1245. (if (or (math-posp a)
  1246. (and (math-zerop a)
  1247. (or (memq calc-infinite-mode '(-1 1))
  1248. (setq aa '(neg (var inf var-inf))
  1249. bb '(var inf var-inf)))))
  1250. (list 'intv 3 (or aa 0) (or bb 0))
  1251. (if (math-negp a)
  1252. (math-neg (list 'intv 3 (or aa 0) (or bb 0)))
  1253. '(var nan var-nan)))
  1254. (if (or (math-floatp a) (math-floatp b)) '(float 0 0) 0))))))
  1255. (defun math-mul-symb-fancy (a b)
  1256. (or (and math-simplify-only
  1257. (not (equal a math-simplify-only))
  1258. (list '* a b))
  1259. (and (Math-equal-int a 1)
  1260. b)
  1261. (and (Math-equal-int a -1)
  1262. (math-neg b))
  1263. (and (or (and (Math-vectorp a) (math-known-scalarp b))
  1264. (and (Math-vectorp b) (math-known-scalarp a)))
  1265. (math-map-vec-2 'math-mul a b))
  1266. (and (Math-objectp b) (not (Math-objectp a))
  1267. (math-mul b a))
  1268. (and (eq (car-safe a) 'neg)
  1269. (math-neg (math-mul (nth 1 a) b)))
  1270. (and (eq (car-safe b) 'neg)
  1271. (math-neg (math-mul a (nth 1 b))))
  1272. (and (eq (car-safe a) '*)
  1273. (math-mul (nth 1 a)
  1274. (math-mul (nth 2 a) b)))
  1275. (and (eq (car-safe a) '^)
  1276. (Math-looks-negp (nth 2 a))
  1277. (not (and (eq (car-safe b) '^) (Math-looks-negp (nth 2 b))))
  1278. (math-known-scalarp b t)
  1279. (math-div b (math-normalize
  1280. (list '^ (nth 1 a) (math-neg (nth 2 a))))))
  1281. (and (eq (car-safe b) '^)
  1282. (Math-looks-negp (nth 2 b))
  1283. (not (and (eq (car-safe a) '^) (Math-looks-negp (nth 2 a))))
  1284. (not (math-known-matrixp (nth 1 b)))
  1285. (math-div a (math-normalize
  1286. (list '^ (nth 1 b) (math-neg (nth 2 b))))))
  1287. (and (eq (car-safe a) '/)
  1288. (or (math-known-scalarp a t) (math-known-scalarp b t))
  1289. (let ((temp (math-combine-prod (nth 2 a) b t nil t)))
  1290. (if temp
  1291. (math-mul (nth 1 a) temp)
  1292. (math-div (math-mul (nth 1 a) b) (nth 2 a)))))
  1293. (and (eq (car-safe b) '/)
  1294. (math-div (math-mul a (nth 1 b)) (nth 2 b)))
  1295. (and (eq (car-safe b) '+)
  1296. (Math-numberp a)
  1297. (or (Math-numberp (nth 1 b))
  1298. (Math-numberp (nth 2 b)))
  1299. (math-add (math-mul a (nth 1 b))
  1300. (math-mul a (nth 2 b))))
  1301. (and (eq (car-safe b) '-)
  1302. (Math-numberp a)
  1303. (or (Math-numberp (nth 1 b))
  1304. (Math-numberp (nth 2 b)))
  1305. (math-sub (math-mul a (nth 1 b))
  1306. (math-mul a (nth 2 b))))
  1307. (and (eq (car-safe b) '*)
  1308. (Math-numberp (nth 1 b))
  1309. (not (Math-numberp a))
  1310. (math-mul (nth 1 b) (math-mul a (nth 2 b))))
  1311. (and (eq (car-safe a) 'calcFunc-idn)
  1312. (= (length a) 2)
  1313. (or (and (eq (car-safe b) 'calcFunc-idn)
  1314. (= (length b) 2)
  1315. (list 'calcFunc-idn (math-mul (nth 1 a) (nth 1 b))))
  1316. (and (math-known-scalarp b)
  1317. (list 'calcFunc-idn (math-mul (nth 1 a) b)))
  1318. (and (math-known-matrixp b)
  1319. (math-mul (nth 1 a) b))))
  1320. (and (eq (car-safe b) 'calcFunc-idn)
  1321. (= (length b) 2)
  1322. (or (and (math-known-scalarp a)
  1323. (list 'calcFunc-idn (math-mul a (nth 1 b))))
  1324. (and (math-known-matrixp a)
  1325. (math-mul a (nth 1 b)))))
  1326. (and (math-identity-matrix-p a t)
  1327. (or (and (eq (car-safe b) 'calcFunc-idn)
  1328. (= (length b) 2)
  1329. (list 'calcFunc-idn (math-mul
  1330. (nth 1 (nth 1 a))
  1331. (nth 1 b))
  1332. (1- (length a))))
  1333. (and (math-known-scalarp b)
  1334. (list 'calcFunc-idn (math-mul
  1335. (nth 1 (nth 1 a)) b)
  1336. (1- (length a))))
  1337. (and (math-known-matrixp b)
  1338. (math-mul (nth 1 (nth 1 a)) b))))
  1339. (and (math-identity-matrix-p b t)
  1340. (or (and (eq (car-safe a) 'calcFunc-idn)
  1341. (= (length a) 2)
  1342. (list 'calcFunc-idn (math-mul (nth 1 a)
  1343. (nth 1 (nth 1 b)))
  1344. (1- (length b))))
  1345. (and (math-known-scalarp a)
  1346. (list 'calcFunc-idn (math-mul a (nth 1 (nth 1 b)))
  1347. (1- (length b))))
  1348. (and (math-known-matrixp a)
  1349. (math-mul a (nth 1 (nth 1 b))))))
  1350. (and (math-looks-negp b)
  1351. (math-mul (math-neg a) (math-neg b)))
  1352. (and (eq (car-safe b) '-)
  1353. (math-looks-negp a)
  1354. (math-mul (math-neg a) (math-neg b)))
  1355. (cond
  1356. ((eq (car-safe b) '*)
  1357. (let ((temp (math-combine-prod a (nth 1 b) nil nil t)))
  1358. (and temp
  1359. (math-mul temp (nth 2 b)))))
  1360. (t
  1361. (math-combine-prod a b nil nil nil)))
  1362. (and (equal a '(var nan var-nan))
  1363. a)
  1364. (and (equal b '(var nan var-nan))
  1365. b)
  1366. (and (equal a '(var uinf var-uinf))
  1367. a)
  1368. (and (equal b '(var uinf var-uinf))
  1369. b)
  1370. (and (equal b '(var inf var-inf))
  1371. (let ((s1 (math-possible-signs a)))
  1372. (cond ((eq s1 4)
  1373. b)
  1374. ((eq s1 6)
  1375. '(intv 3 0 (var inf var-inf)))
  1376. ((eq s1 1)
  1377. (math-neg b))
  1378. ((eq s1 3)
  1379. '(intv 3 (neg (var inf var-inf)) 0))
  1380. ((and (eq (car a) 'intv) (math-intv-constp a))
  1381. '(intv 3 (neg (var inf var-inf)) (var inf var-inf)))
  1382. ((and (eq (car a) 'cplx)
  1383. (math-zerop (nth 1 a)))
  1384. (list '* (list 'cplx 0 (calcFunc-sign (nth 2 a))) b))
  1385. ((eq (car a) 'polar)
  1386. (list '* (list 'polar 1 (nth 2 a)) b)))))
  1387. (and (equal a '(var inf var-inf))
  1388. (math-mul b a))
  1389. (list '* a b)))
  1390. (defun calcFunc-div (a &rest rest)
  1391. (while rest
  1392. (setq a (list '/ a (car rest))
  1393. rest (cdr rest)))
  1394. (math-normalize a))
  1395. (defun math-div-objects-fancy (a b)
  1396. (cond ((and (Math-numberp a) (Math-numberp b))
  1397. (math-normalize
  1398. (cond ((math-want-polar a b)
  1399. (let ((a (math-polar a))
  1400. (b (math-polar b)))
  1401. (list 'polar
  1402. (math-div (nth 1 a) (nth 1 b))
  1403. (math-fix-circular (math-sub (nth 2 a)
  1404. (nth 2 b))))))
  1405. ((Math-realp b)
  1406. (setq a (math-complex a))
  1407. (list 'cplx (math-div (nth 1 a) b)
  1408. (math-div (nth 2 a) b)))
  1409. (t
  1410. (setq a (math-complex a)
  1411. b (math-complex b))
  1412. (math-div
  1413. (list 'cplx
  1414. (math-add (math-mul (nth 1 a) (nth 1 b))
  1415. (math-mul (nth 2 a) (nth 2 b)))
  1416. (math-sub (math-mul (nth 2 a) (nth 1 b))
  1417. (math-mul (nth 1 a) (nth 2 b))))
  1418. (math-add (math-sqr (nth 1 b))
  1419. (math-sqr (nth 2 b))))))))
  1420. ((math-matrixp b)
  1421. (if (math-square-matrixp b)
  1422. (let ((n1 (length b)))
  1423. (if (Math-vectorp a)
  1424. (if (math-matrixp a)
  1425. (if (= (length a) n1)
  1426. (math-lud-solve (math-matrix-lud b) a b)
  1427. (if (= (length (nth 1 a)) n1)
  1428. (math-transpose
  1429. (math-lud-solve (math-matrix-lud
  1430. (math-transpose b))
  1431. (math-transpose a) b))
  1432. (math-dimension-error)))
  1433. (if (= (length a) n1)
  1434. (math-mat-col (math-lud-solve (math-matrix-lud b)
  1435. (math-col-matrix a) b)
  1436. 1)
  1437. (math-dimension-error)))
  1438. (if (Math-equal-int a 1)
  1439. (calcFunc-inv b)
  1440. (math-mul a (calcFunc-inv b)))))
  1441. (math-reject-arg b 'square-matrixp)))
  1442. ((and (Math-vectorp a) (Math-objectp b))
  1443. (math-map-vec-2 'math-div a b))
  1444. ((eq (car-safe a) 'sdev)
  1445. (if (eq (car-safe b) 'sdev)
  1446. (let ((x (math-div (nth 1 a) (nth 1 b))))
  1447. (math-make-sdev x
  1448. (math-div (math-hypot (nth 2 a)
  1449. (math-mul (nth 2 b) x))
  1450. (nth 1 b))))
  1451. (if (or (Math-scalarp b)
  1452. (not (Math-objvecp b)))
  1453. (math-make-sdev (math-div (nth 1 a) b) (math-div (nth 2 a) b))
  1454. (math-reject-arg 'realp b))))
  1455. ((and (eq (car-safe b) 'sdev)
  1456. (or (Math-scalarp a)
  1457. (not (Math-objvecp a))))
  1458. (let ((x (math-div a (nth 1 b))))
  1459. (math-make-sdev x
  1460. (math-div (math-mul (nth 2 b) x) (nth 1 b)))))
  1461. ((and (eq (car-safe a) 'intv) (Math-anglep b))
  1462. (if (Math-negp b)
  1463. (math-neg (math-div a (math-neg b)))
  1464. (math-make-intv (nth 1 a)
  1465. (math-div (nth 2 a) b)
  1466. (math-div (nth 3 a) b))))
  1467. ((and (eq (car-safe b) 'intv) (Math-anglep a))
  1468. (if (or (Math-posp (nth 2 b))
  1469. (and (Math-zerop (nth 2 b)) (or (memq (nth 1 b) '(0 1))
  1470. calc-infinite-mode)))
  1471. (if (Math-negp a)
  1472. (math-neg (math-div (math-neg a) b))
  1473. (let ((calc-infinite-mode 1))
  1474. (math-make-intv (aref [0 2 1 3] (nth 1 b))
  1475. (math-div a (nth 3 b))
  1476. (math-div a (nth 2 b)))))
  1477. (if (or (Math-negp (nth 3 b))
  1478. (and (Math-zerop (nth 3 b)) (or (memq (nth 1 b) '(0 2))
  1479. calc-infinite-mode)))
  1480. (math-neg (math-div a (math-neg b)))
  1481. (if calc-infinite-mode
  1482. '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
  1483. (math-reject-arg b "*Division by zero")))))
  1484. ((and (eq (car-safe a) 'intv) (math-intv-constp a)
  1485. (eq (car-safe b) 'intv) (math-intv-constp b))
  1486. (if (or (Math-posp (nth 2 b))
  1487. (and (Math-zerop (nth 2 b)) (or (memq (nth 1 b) '(0 1))
  1488. calc-infinite-mode)))
  1489. (let* ((calc-infinite-mode 1)
  1490. (lo (math-div a (nth 2 b)))
  1491. (hi (math-div a (nth 3 b))))
  1492. (or (eq (car-safe lo) 'intv)
  1493. (setq lo (list 'intv (if (memq (nth 1 b) '(2 3)) 3 0)
  1494. lo lo)))
  1495. (or (eq (car-safe hi) 'intv)
  1496. (setq hi (list 'intv (if (memq (nth 1 b) '(1 3)) 3 0)
  1497. hi hi)))
  1498. (math-combine-intervals
  1499. (nth 2 lo) (and (or (memq (nth 1 b) '(2 3))
  1500. (and (math-infinitep (nth 2 lo))
  1501. (not (math-zerop (nth 2 b)))))
  1502. (memq (nth 1 lo) '(2 3)))
  1503. (nth 3 lo) (and (or (memq (nth 1 b) '(2 3))
  1504. (and (math-infinitep (nth 3 lo))
  1505. (not (math-zerop (nth 2 b)))))
  1506. (memq (nth 1 lo) '(1 3)))
  1507. (nth 2 hi) (and (or (memq (nth 1 b) '(1 3))
  1508. (and (math-infinitep (nth 2 hi))
  1509. (not (math-zerop (nth 3 b)))))
  1510. (memq (nth 1 hi) '(2 3)))
  1511. (nth 3 hi) (and (or (memq (nth 1 b) '(1 3))
  1512. (and (math-infinitep (nth 3 hi))
  1513. (not (math-zerop (nth 3 b)))))
  1514. (memq (nth 1 hi) '(1 3)))))
  1515. (if (or (Math-negp (nth 3 b))
  1516. (and (Math-zerop (nth 3 b)) (or (memq (nth 1 b) '(0 2))
  1517. calc-infinite-mode)))
  1518. (math-neg (math-div a (math-neg b)))
  1519. (if calc-infinite-mode
  1520. '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
  1521. (math-reject-arg b "*Division by zero")))))
  1522. ((and (eq (car-safe a) 'mod)
  1523. (eq (car-safe b) 'mod)
  1524. (equal (nth 2 a) (nth 2 b)))
  1525. (math-make-mod (math-div-mod (nth 1 a) (nth 1 b) (nth 2 a))
  1526. (nth 2 a)))
  1527. ((and (eq (car-safe a) 'mod)
  1528. (Math-anglep b))
  1529. (math-make-mod (math-div-mod (nth 1 a) b (nth 2 a)) (nth 2 a)))
  1530. ((and (eq (car-safe b) 'mod)
  1531. (Math-anglep a))
  1532. (math-make-mod (math-div-mod a (nth 1 b) (nth 2 b)) (nth 2 b)))
  1533. ((eq (car-safe a) 'hms)
  1534. (if (eq (car-safe b) 'hms)
  1535. (math-with-extra-prec 1
  1536. (math-div (math-from-hms a 'deg)
  1537. (math-from-hms b 'deg)))
  1538. (math-with-extra-prec 2
  1539. (math-to-hms (math-div (math-from-hms a 'deg) b) 'deg))))
  1540. (t (calc-record-why "*Incompatible arguments for /" a b))))
  1541. (defun math-div-by-zero (a b)
  1542. (if (math-infinitep a)
  1543. (if (or (equal a '(var nan var-nan))
  1544. (equal b '(var uinf var-uinf))
  1545. (memq calc-infinite-mode '(-1 1)))
  1546. a
  1547. '(var uinf var-uinf))
  1548. (if calc-infinite-mode
  1549. (if (math-zerop a)
  1550. '(var nan var-nan)
  1551. (if (eq calc-infinite-mode 1)
  1552. (math-mul a '(var inf var-inf))
  1553. (if (eq calc-infinite-mode -1)
  1554. (math-mul a '(neg (var inf var-inf)))
  1555. (if (eq (car-safe a) 'intv)
  1556. '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
  1557. '(var uinf var-uinf)))))
  1558. (math-reject-arg a "*Division by zero"))))
  1559. (defun math-div-zero (a b)
  1560. (if (math-known-matrixp b)
  1561. (if (math-vectorp b)
  1562. (math-map-vec-2 'math-div a b)
  1563. (math-mimic-ident 0 b))
  1564. (if (equal b '(var nan var-nan))
  1565. b
  1566. (if (and (eq (car-safe b) 'intv) (math-intv-constp b)
  1567. (not (math-posp b)) (not (math-negp b)))
  1568. (if calc-infinite-mode
  1569. (list 'intv 3
  1570. (if (and (math-zerop (nth 2 b))
  1571. (memq calc-infinite-mode '(1 -1)))
  1572. (nth 2 b) '(neg (var inf var-inf)))
  1573. (if (and (math-zerop (nth 3 b))
  1574. (memq calc-infinite-mode '(1 -1)))
  1575. (nth 3 b) '(var inf var-inf)))
  1576. (math-reject-arg b "*Division by zero"))
  1577. a))))
  1578. ;; For math-div-symb-fancy
  1579. (defvar math-trig-inverses
  1580. '((calcFunc-sin . calcFunc-csc)
  1581. (calcFunc-cos . calcFunc-sec)
  1582. (calcFunc-tan . calcFunc-cot)
  1583. (calcFunc-sec . calcFunc-cos)
  1584. (calcFunc-csc . calcFunc-sin)
  1585. (calcFunc-cot . calcFunc-tan)
  1586. (calcFunc-sinh . calcFunc-csch)
  1587. (calcFunc-cosh . calcFunc-sech)
  1588. (calcFunc-tanh . calcFunc-coth)
  1589. (calcFunc-sech . calcFunc-cosh)
  1590. (calcFunc-csch . calcFunc-sinh)
  1591. (calcFunc-coth . calcFunc-tanh)))
  1592. (defvar math-div-trig)
  1593. (defvar math-div-non-trig)
  1594. (defun math-div-new-trig (tr)
  1595. (if math-div-trig
  1596. (setq math-div-trig
  1597. (list '* tr math-div-trig))
  1598. (setq math-div-trig tr)))
  1599. (defun math-div-new-non-trig (ntr)
  1600. (if math-div-non-trig
  1601. (setq math-div-non-trig
  1602. (list '* ntr math-div-non-trig))
  1603. (setq math-div-non-trig ntr)))
  1604. (defun math-div-isolate-trig (expr)
  1605. (if (eq (car-safe expr) '*)
  1606. (progn
  1607. (math-div-isolate-trig-term (nth 1 expr))
  1608. (math-div-isolate-trig (nth 2 expr)))
  1609. (math-div-isolate-trig-term expr)))
  1610. (defun math-div-isolate-trig-term (term)
  1611. (let ((fn (assoc (car-safe term) math-trig-inverses)))
  1612. (if fn
  1613. (math-div-new-trig
  1614. (cons (cdr fn) (cdr term)))
  1615. (math-div-new-non-trig term))))
  1616. (defun math-div-symb-fancy (a b)
  1617. (or (and (math-known-matrixp b)
  1618. (math-mul a (math-pow b -1)))
  1619. (and math-simplify-only
  1620. (not (equal a math-simplify-only))
  1621. (list '/ a b))
  1622. (and (Math-equal-int b 1) a)
  1623. (and (Math-equal-int b -1) (math-neg a))
  1624. (and (Math-vectorp a) (math-known-scalarp b)
  1625. (math-map-vec-2 'math-div a b))
  1626. (and (eq (car-safe b) '^)
  1627. (or (Math-looks-negp (nth 2 b)) (Math-equal-int a 1))
  1628. (math-mul a (math-normalize
  1629. (list '^ (nth 1 b) (math-neg (nth 2 b))))))
  1630. (and (eq (car-safe a) 'neg)
  1631. (math-neg (math-div (nth 1 a) b)))
  1632. (and (eq (car-safe b) 'neg)
  1633. (math-neg (math-div a (nth 1 b))))
  1634. (and (eq (car-safe a) '/)
  1635. (math-div (nth 1 a) (math-mul (nth 2 a) b)))
  1636. (and (eq (car-safe b) '/)
  1637. (or (math-known-scalarp (nth 1 b) t)
  1638. (math-known-scalarp (nth 2 b) t))
  1639. (math-div (math-mul a (nth 2 b)) (nth 1 b)))
  1640. (and (eq (car-safe b) 'frac)
  1641. (math-mul (math-make-frac (nth 2 b) (nth 1 b)) a))
  1642. (and (eq (car-safe a) '+)
  1643. (or (Math-numberp (nth 1 a))
  1644. (Math-numberp (nth 2 a)))
  1645. (Math-numberp b)
  1646. (math-add (math-div (nth 1 a) b)
  1647. (math-div (nth 2 a) b)))
  1648. (and (eq (car-safe a) '-)
  1649. (or (Math-numberp (nth 1 a))
  1650. (Math-numberp (nth 2 a)))
  1651. (Math-numberp b)
  1652. (math-sub (math-div (nth 1 a) b)
  1653. (math-div (nth 2 a) b)))
  1654. (and (or (eq (car-safe a) '-)
  1655. (math-looks-negp a))
  1656. (math-looks-negp b)
  1657. (math-div (math-neg a) (math-neg b)))
  1658. (and (eq (car-safe b) '-)
  1659. (math-looks-negp a)
  1660. (math-div (math-neg a) (math-neg b)))
  1661. (and (eq (car-safe a) 'calcFunc-idn)
  1662. (= (length a) 2)
  1663. (or (and (eq (car-safe b) 'calcFunc-idn)
  1664. (= (length b) 2)
  1665. (list 'calcFunc-idn (math-div (nth 1 a) (nth 1 b))))
  1666. (and (math-known-scalarp b)
  1667. (list 'calcFunc-idn (math-div (nth 1 a) b)))
  1668. (and (math-known-matrixp b)
  1669. (math-div (nth 1 a) b))))
  1670. (and (eq (car-safe b) 'calcFunc-idn)
  1671. (= (length b) 2)
  1672. (or (and (math-known-scalarp a)
  1673. (list 'calcFunc-idn (math-div a (nth 1 b))))
  1674. (and (math-known-matrixp a)
  1675. (math-div a (nth 1 b)))))
  1676. (and math-simplifying
  1677. (let ((math-div-trig nil)
  1678. (math-div-non-trig nil))
  1679. (math-div-isolate-trig b)
  1680. (if math-div-trig
  1681. (if math-div-non-trig
  1682. (math-div (math-mul a math-div-trig) math-div-non-trig)
  1683. (math-mul a math-div-trig))
  1684. nil)))
  1685. (if (and calc-matrix-mode
  1686. (or (math-known-matrixp a) (math-known-matrixp b)))
  1687. (math-combine-prod a b nil t nil)
  1688. (if (eq (car-safe a) '*)
  1689. (if (eq (car-safe b) '*)
  1690. (let ((c (math-combine-prod (nth 1 a) (nth 1 b) nil t t)))
  1691. (and c
  1692. (math-div (math-mul c (nth 2 a)) (nth 2 b))))
  1693. (let ((c (math-combine-prod (nth 1 a) b nil t t)))
  1694. (and c
  1695. (math-mul c (nth 2 a)))))
  1696. (if (eq (car-safe b) '*)
  1697. (let ((c (math-combine-prod a (nth 1 b) nil t t)))
  1698. (and c
  1699. (math-div c (nth 2 b))))
  1700. (math-combine-prod a b nil t nil))))
  1701. (and (math-infinitep a)
  1702. (if (math-infinitep b)
  1703. '(var nan var-nan)
  1704. (if (or (equal a '(var nan var-nan))
  1705. (equal a '(var uinf var-uinf)))
  1706. a
  1707. (if (equal a '(var inf var-inf))
  1708. (if (or (math-posp b)
  1709. (and (eq (car-safe b) 'intv)
  1710. (math-zerop (nth 2 b))))
  1711. (if (and (eq (car-safe b) 'intv)
  1712. (not (math-intv-constp b t)))
  1713. '(intv 3 0 (var inf var-inf))
  1714. a)
  1715. (if (or (math-negp b)
  1716. (and (eq (car-safe b) 'intv)
  1717. (math-zerop (nth 3 b))))
  1718. (if (and (eq (car-safe b) 'intv)
  1719. (not (math-intv-constp b t)))
  1720. '(intv 3 (neg (var inf var-inf)) 0)
  1721. (math-neg a))
  1722. (if (and (eq (car-safe b) 'intv)
  1723. (math-negp (nth 2 b)) (math-posp (nth 3 b)))
  1724. '(intv 3 (neg (var inf var-inf))
  1725. (var inf var-inf)))))))))
  1726. (and (math-infinitep b)
  1727. (if (equal b '(var nan var-nan))
  1728. b
  1729. (let ((calc-infinite-mode 1))
  1730. (math-mul-zero b a))))
  1731. (list '/ a b)))
  1732. ;;; Division from the left.
  1733. (defun calcFunc-ldiv (a b)
  1734. (if (math-known-scalarp a)
  1735. (math-div b a)
  1736. (math-mul (math-pow a -1) b)))
  1737. (defun calcFunc-mod (a b)
  1738. (math-normalize (list '% a b)))
  1739. (defun math-mod-fancy (a b)
  1740. (cond ((equal b '(var inf var-inf))
  1741. (if (or (math-posp a) (math-zerop a))
  1742. a
  1743. (if (math-negp a)
  1744. b
  1745. (if (eq (car-safe a) 'intv)
  1746. (if (math-negp (nth 2 a))
  1747. '(intv 3 0 (var inf var-inf))
  1748. a)
  1749. (list '% a b)))))
  1750. ((and (eq (car-safe a) 'mod) (Math-realp b) (math-posp b))
  1751. (math-make-mod (nth 1 a) b))
  1752. ((and (eq (car-safe a) 'intv) (math-intv-constp a t) (math-posp b))
  1753. (math-mod-intv a b))
  1754. (t
  1755. (if (Math-anglep a)
  1756. (calc-record-why 'anglep b)
  1757. (calc-record-why 'anglep a))
  1758. (list '% a b))))
  1759. (defun calcFunc-pow (a b)
  1760. (math-normalize (list '^ a b)))
  1761. (defun math-pow-of-zero (a b)
  1762. "Raise A to the power of B, where A is a form of zero."
  1763. (if (math-floatp b) (setq a (math-float a)))
  1764. (cond
  1765. ;; 0^0 = 1
  1766. ((eq b 0)
  1767. 1)
  1768. ;; 0^0.0, etc., are undetermined
  1769. ((Math-zerop b)
  1770. (if calc-infinite-mode
  1771. '(var nan var-nan)
  1772. (math-reject-arg (list '^ a b) "*Indeterminate form")))
  1773. ;; 0^positive = 0
  1774. ((math-known-posp b)
  1775. a)
  1776. ;; 0^negative is undefined (let math-div handle it)
  1777. ((math-known-negp b)
  1778. (math-div 1 a))
  1779. ;; 0^infinity is undefined
  1780. ((math-infinitep b)
  1781. '(var nan var-nan))
  1782. ;; Some intervals
  1783. ((and (eq (car b) 'intv)
  1784. calc-infinite-mode
  1785. (math-negp (nth 2 b))
  1786. (math-posp (nth 3 b)))
  1787. '(intv 3 (neg (var inf var-inf)) (var inf var-inf)))
  1788. ;; If none of the above, leave it alone.
  1789. (t
  1790. (list '^ a b))))
  1791. (defun math-pow-zero (a b)
  1792. (if (eq (car-safe a) 'mod)
  1793. (math-make-mod 1 (nth 2 a))
  1794. (if (math-known-matrixp a)
  1795. (math-mimic-ident 1 a)
  1796. (if (math-infinitep a)
  1797. '(var nan var-nan)
  1798. (if (and (eq (car a) 'intv) (math-intv-constp a)
  1799. (or (and (not (math-posp a)) (not (math-negp a)))
  1800. (not (math-intv-constp a t))))
  1801. '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
  1802. (if (or (math-floatp a) (math-floatp b))
  1803. '(float 1 0) 1))))))
  1804. (defun math-pow-fancy (a b)
  1805. (cond ((and (Math-numberp a) (Math-numberp b))
  1806. (or (if (memq (math-quarter-integer b) '(1 2 3))
  1807. (let ((sqrt (math-sqrt (if (math-floatp b)
  1808. (math-float a) a))))
  1809. (and (Math-numberp sqrt)
  1810. (math-pow sqrt (math-mul 2 b))))
  1811. (and (eq (car b) 'frac)
  1812. (integerp (nth 2 b))
  1813. (<= (nth 2 b) 10)
  1814. (let ((root (math-nth-root a (nth 2 b))))
  1815. (and root (math-ipow root (nth 1 b))))))
  1816. (and (or (eq a 10) (equal a '(float 1 1)))
  1817. (math-num-integerp b)
  1818. (calcFunc-scf '(float 1 0) b))
  1819. (and calc-symbolic-mode
  1820. (list '^ a b))
  1821. (math-with-extra-prec 2
  1822. (math-exp-raw
  1823. (math-float (math-mul b (math-ln-raw (math-float a))))))))
  1824. ((or (not (Math-objvecp a))
  1825. (not (Math-objectp b)))
  1826. (let (temp)
  1827. (cond ((and math-simplify-only
  1828. (not (equal a math-simplify-only)))
  1829. (list '^ a b))
  1830. ((and (eq (car-safe a) '*)
  1831. (or
  1832. (and
  1833. (math-known-matrixp (nth 1 a))
  1834. (math-known-matrixp (nth 2 a)))
  1835. (and
  1836. calc-matrix-mode
  1837. (not (eq calc-matrix-mode 'scalar))
  1838. (and (not (math-known-scalarp (nth 1 a)))
  1839. (not (math-known-scalarp (nth 2 a)))))))
  1840. (if (and (= b -1)
  1841. (math-known-square-matrixp (nth 1 a))
  1842. (math-known-square-matrixp (nth 2 a)))
  1843. (math-mul (math-pow-fancy (nth 2 a) -1)
  1844. (math-pow-fancy (nth 1 a) -1))
  1845. (list '^ a b)))
  1846. ((and (eq (car-safe a) '*)
  1847. (or (math-known-num-integerp b)
  1848. (math-known-nonnegp (nth 1 a))
  1849. (math-known-nonnegp (nth 2 a))))
  1850. (math-mul (math-pow (nth 1 a) b)
  1851. (math-pow (nth 2 a) b)))
  1852. ((and (eq (car-safe a) '/)
  1853. (or (math-known-num-integerp b)
  1854. (math-known-nonnegp (nth 2 a))))
  1855. (math-div (math-pow (nth 1 a) b)
  1856. (math-pow (nth 2 a) b)))
  1857. ((and (eq (car-safe a) '/)
  1858. (math-known-nonnegp (nth 1 a))
  1859. (not (math-equal-int (nth 1 a) 1)))
  1860. (math-mul (math-pow (nth 1 a) b)
  1861. (math-pow (math-div 1 (nth 2 a)) b)))
  1862. ((and (eq (car-safe a) '^)
  1863. (or (math-known-num-integerp b)
  1864. (math-known-nonnegp (nth 1 a))))
  1865. (math-pow (nth 1 a) (math-mul (nth 2 a) b)))
  1866. ((and (eq (car-safe a) 'calcFunc-sqrt)
  1867. (or (math-known-num-integerp b)
  1868. (math-known-nonnegp (nth 1 a))))
  1869. (math-pow (nth 1 a) (math-div b 2)))
  1870. ((and (eq (car-safe a) '^)
  1871. (math-known-evenp (nth 2 a))
  1872. (memq (math-quarter-integer b) '(1 2 3))
  1873. (math-known-realp (nth 1 a)))
  1874. (math-abs (math-pow (nth 1 a) (math-mul (nth 2 a) b))))
  1875. ((and (math-looks-negp a)
  1876. (math-known-integerp b)
  1877. (setq temp (or (and (math-known-evenp b)
  1878. (math-pow (math-neg a) b))
  1879. (and (math-known-oddp b)
  1880. (math-neg (math-pow (math-neg a)
  1881. b))))))
  1882. temp)
  1883. ((and (eq (car-safe a) 'calcFunc-abs)
  1884. (math-known-realp (nth 1 a))
  1885. (math-known-evenp b))
  1886. (math-pow (nth 1 a) b))
  1887. ((math-infinitep a)
  1888. (cond ((equal a '(var nan var-nan))
  1889. a)
  1890. ((eq (car a) 'neg)
  1891. (math-mul (math-pow -1 b) (math-pow (nth 1 a) b)))
  1892. ((math-posp b)
  1893. a)
  1894. ((math-negp b)
  1895. (if (math-floatp b) '(float 0 0) 0))
  1896. ((and (eq (car-safe b) 'intv)
  1897. (math-intv-constp b))
  1898. '(intv 3 0 (var inf var-inf)))
  1899. (t
  1900. '(var nan var-nan))))
  1901. ((math-infinitep b)
  1902. (let (scale)
  1903. (cond ((math-negp b)
  1904. (math-pow (math-div 1 a) (math-neg b)))
  1905. ((not (math-posp b))
  1906. '(var nan var-nan))
  1907. ((math-equal-int (setq scale (calcFunc-abssqr a)) 1)
  1908. '(var nan var-nan))
  1909. ((Math-lessp scale 1)
  1910. (if (math-floatp a) '(float 0 0) 0))
  1911. ((Math-lessp 1 a)
  1912. b)
  1913. ((Math-lessp a -1)
  1914. '(var uinf var-uinf))
  1915. ((and (eq (car a) 'intv)
  1916. (math-intv-constp a))
  1917. (if (Math-lessp -1 a)
  1918. (if (math-equal-int (nth 3 a) 1)
  1919. '(intv 3 0 1)
  1920. '(intv 3 0 (var inf var-inf)))
  1921. '(intv 3 (neg (var inf var-inf))
  1922. (var inf var-inf))))
  1923. (t (list '^ a b)))))
  1924. ((and (eq (car-safe a) 'calcFunc-idn)
  1925. (= (length a) 2)
  1926. (math-known-num-integerp b))
  1927. (list 'calcFunc-idn (math-pow (nth 1 a) b)))
  1928. (t (if (Math-objectp a)
  1929. (calc-record-why 'objectp b)
  1930. (calc-record-why 'objectp a))
  1931. (list '^ a b)))))
  1932. ((and (eq (car-safe a) 'sdev) (eq (car-safe b) 'sdev))
  1933. (if (and (math-constp a) (math-constp b))
  1934. (math-with-extra-prec 2
  1935. (let* ((ln (math-ln-raw (math-float (nth 1 a))))
  1936. (pow (math-exp-raw
  1937. (math-float (math-mul (nth 1 b) ln)))))
  1938. (math-make-sdev
  1939. pow
  1940. (math-mul
  1941. pow
  1942. (math-hypot (math-mul (nth 2 a)
  1943. (math-div (nth 1 b) (nth 1 a)))
  1944. (math-mul (nth 2 b) ln))))))
  1945. (let ((pow (math-pow (nth 1 a) (nth 1 b))))
  1946. (math-make-sdev
  1947. pow
  1948. (math-mul pow
  1949. (math-hypot (math-mul (nth 2 a)
  1950. (math-div (nth 1 b) (nth 1 a)))
  1951. (math-mul (nth 2 b) (calcFunc-ln
  1952. (nth 1 a)))))))))
  1953. ((and (eq (car-safe a) 'sdev) (Math-numberp b))
  1954. (if (math-constp a)
  1955. (math-with-extra-prec 2
  1956. (let ((pow (math-pow (nth 1 a) (math-sub b 1))))
  1957. (math-make-sdev (math-mul pow (nth 1 a))
  1958. (math-mul pow (math-mul (nth 2 a) b)))))
  1959. (math-make-sdev (math-pow (nth 1 a) b)
  1960. (math-mul (math-pow (nth 1 a) (math-add b -1))
  1961. (math-mul (nth 2 a) b)))))
  1962. ((and (eq (car-safe b) 'sdev) (Math-numberp a))
  1963. (math-with-extra-prec 2
  1964. (let* ((ln (math-ln-raw (math-float a)))
  1965. (pow (calcFunc-exp (math-mul (nth 1 b) ln))))
  1966. (math-make-sdev pow (math-mul pow (math-mul (nth 2 b) ln))))))
  1967. ((and (eq (car-safe a) 'intv) (math-intv-constp a)
  1968. (Math-realp b)
  1969. (or (Math-natnump b)
  1970. (Math-posp (nth 2 a))
  1971. (and (math-zerop (nth 2 a))
  1972. (or (Math-posp b)
  1973. (and (Math-integerp b) calc-infinite-mode)))
  1974. (Math-negp (nth 3 a))
  1975. (and (math-zerop (nth 3 a))
  1976. (or (Math-posp b)
  1977. (and (Math-integerp b) calc-infinite-mode)))))
  1978. (if (math-evenp b)
  1979. (setq a (math-abs a)))
  1980. (let ((calc-infinite-mode (if (math-zerop (nth 3 a)) -1 1)))
  1981. (math-sort-intv (nth 1 a)
  1982. (math-pow (nth 2 a) b)
  1983. (math-pow (nth 3 a) b))))
  1984. ((and (eq (car-safe b) 'intv) (math-intv-constp b)
  1985. (Math-realp a) (Math-posp a))
  1986. (math-sort-intv (nth 1 b)
  1987. (math-pow a (nth 2 b))
  1988. (math-pow a (nth 3 b))))
  1989. ((and (eq (car-safe a) 'intv) (math-intv-constp a)
  1990. (eq (car-safe b) 'intv) (math-intv-constp b)
  1991. (or (and (not (Math-negp (nth 2 a)))
  1992. (not (Math-negp (nth 2 b))))
  1993. (and (Math-posp (nth 2 a))
  1994. (not (Math-posp (nth 3 b))))))
  1995. (let ((lo (math-pow a (nth 2 b)))
  1996. (hi (math-pow a (nth 3 b))))
  1997. (or (eq (car-safe lo) 'intv)
  1998. (setq lo (list 'intv (if (memq (nth 1 b) '(2 3)) 3 0) lo lo)))
  1999. (or (eq (car-safe hi) 'intv)
  2000. (setq hi (list 'intv (if (memq (nth 1 b) '(1 3)) 3 0) hi hi)))
  2001. (math-combine-intervals
  2002. (nth 2 lo) (and (or (memq (nth 1 b) '(2 3))
  2003. (math-infinitep (nth 2 lo)))
  2004. (memq (nth 1 lo) '(2 3)))
  2005. (nth 3 lo) (and (or (memq (nth 1 b) '(2 3))
  2006. (math-infinitep (nth 3 lo)))
  2007. (memq (nth 1 lo) '(1 3)))
  2008. (nth 2 hi) (and (or (memq (nth 1 b) '(1 3))
  2009. (math-infinitep (nth 2 hi)))
  2010. (memq (nth 1 hi) '(2 3)))
  2011. (nth 3 hi) (and (or (memq (nth 1 b) '(1 3))
  2012. (math-infinitep (nth 3 hi)))
  2013. (memq (nth 1 hi) '(1 3))))))
  2014. ((and (eq (car-safe a) 'mod) (eq (car-safe b) 'mod)
  2015. (equal (nth 2 a) (nth 2 b)))
  2016. (math-make-mod (math-pow-mod (nth 1 a) (nth 1 b) (nth 2 a))
  2017. (nth 2 a)))
  2018. ((and (eq (car-safe a) 'mod) (Math-anglep b))
  2019. (math-make-mod (math-pow-mod (nth 1 a) b (nth 2 a)) (nth 2 a)))
  2020. ((and (eq (car-safe b) 'mod) (Math-anglep a))
  2021. (math-make-mod (math-pow-mod a (nth 1 b) (nth 2 b)) (nth 2 b)))
  2022. ((not (Math-numberp a))
  2023. (math-reject-arg a 'numberp))
  2024. (t
  2025. (math-reject-arg b 'numberp))))
  2026. (defun math-quarter-integer (x)
  2027. (if (Math-integerp x)
  2028. 0
  2029. (if (math-negp x)
  2030. (progn
  2031. (setq x (math-quarter-integer (math-neg x)))
  2032. (and x (- 4 x)))
  2033. (if (eq (car x) 'frac)
  2034. (if (eq (nth 2 x) 2)
  2035. 2
  2036. (and (eq (nth 2 x) 4)
  2037. (progn
  2038. (setq x (nth 1 x))
  2039. (% (if (consp x) (nth 1 x) x) 4))))
  2040. (if (eq (car x) 'float)
  2041. (if (>= (nth 2 x) 0)
  2042. 0
  2043. (if (= (nth 2 x) -1)
  2044. (progn
  2045. (setq x (nth 1 x))
  2046. (and (= (% (if (consp x) (nth 1 x) x) 10) 5) 2))
  2047. (if (= (nth 2 x) -2)
  2048. (progn
  2049. (setq x (nth 1 x)
  2050. x (% (if (consp x) (nth 1 x) x) 100))
  2051. (if (= x 25) 1
  2052. (if (= x 75) 3)))))))))))
  2053. ;;; This assumes A < M and M > 0.
  2054. (defun math-pow-mod (a b m) ; [R R R R]
  2055. (if (and (Math-integerp a) (Math-integerp b) (Math-integerp m))
  2056. (if (Math-negp b)
  2057. (math-div-mod 1 (math-pow-mod a (Math-integer-neg b) m) m)
  2058. (if (eq m 1)
  2059. 0
  2060. (math-pow-mod-step a b m)))
  2061. (math-mod (math-pow a b) m)))
  2062. (defun math-pow-mod-step (a n m) ; [I I I I]
  2063. (math-working "pow" a)
  2064. (let ((val (cond
  2065. ((eq n 0) 1)
  2066. ((eq n 1) a)
  2067. (t
  2068. (let ((rest (math-pow-mod-step
  2069. (math-imod (math-mul a a) m)
  2070. (math-div2 n)
  2071. m)))
  2072. (if (math-evenp n)
  2073. rest
  2074. (math-mod (math-mul a rest) m)))))))
  2075. (math-working "pow" val)
  2076. val))
  2077. ;;; Compute the minimum of two real numbers. [R R R] [Public]
  2078. (defun math-min (a b)
  2079. (if (and (consp a) (eq (car a) 'intv))
  2080. (if (and (consp b) (eq (car b) 'intv))
  2081. (let ((lo (nth 2 a))
  2082. (lom (memq (nth 1 a) '(2 3)))
  2083. (hi (nth 3 a))
  2084. (him (memq (nth 1 a) '(1 3)))
  2085. res)
  2086. (if (= (setq res (math-compare (nth 2 b) lo)) -1)
  2087. (setq lo (nth 2 b) lom (memq (nth 1 b) '(2 3)))
  2088. (if (= res 0)
  2089. (setq lom (or lom (memq (nth 1 b) '(2 3))))))
  2090. (if (= (setq res (math-compare (nth 3 b) hi)) -1)
  2091. (setq hi (nth 3 b) him (memq (nth 1 b) '(1 3)))
  2092. (if (= res 0)
  2093. (setq him (or him (memq (nth 1 b) '(1 3))))))
  2094. (math-make-intv (+ (if lom 2 0) (if him 1 0)) lo hi))
  2095. (math-min a (list 'intv 3 b b)))
  2096. (if (and (consp b) (eq (car b) 'intv))
  2097. (math-min (list 'intv 3 a a) b)
  2098. (let ((res (math-compare a b)))
  2099. (if (= res 1)
  2100. b
  2101. (if (= res 2)
  2102. '(var nan var-nan)
  2103. a))))))
  2104. (defun calcFunc-min (&optional a &rest b)
  2105. (if (not a)
  2106. '(var inf var-inf)
  2107. (if (not (or (Math-anglep a) (eq (car a) 'date)
  2108. (and (eq (car a) 'intv) (math-intv-constp a))
  2109. (math-infinitep a)))
  2110. (math-reject-arg a 'anglep))
  2111. (math-min-list a b)))
  2112. (defun math-min-list (a b)
  2113. (if b
  2114. (if (or (Math-anglep (car b)) (eq (caar b) 'date)
  2115. (and (eq (car (car b)) 'intv) (math-intv-constp (car b)))
  2116. (math-infinitep (car b)))
  2117. (math-min-list (math-min a (car b)) (cdr b))
  2118. (math-reject-arg (car b) 'anglep))
  2119. a))
  2120. ;;; Compute the maximum of two real numbers. [R R R] [Public]
  2121. (defun math-max (a b)
  2122. (if (or (and (consp a) (eq (car a) 'intv))
  2123. (and (consp b) (eq (car b) 'intv)))
  2124. (math-neg (math-min (math-neg a) (math-neg b)))
  2125. (let ((res (math-compare a b)))
  2126. (if (= res -1)
  2127. b
  2128. (if (= res 2)
  2129. '(var nan var-nan)
  2130. a)))))
  2131. (defun calcFunc-max (&optional a &rest b)
  2132. (if (not a)
  2133. '(neg (var inf var-inf))
  2134. (if (not (or (Math-anglep a) (eq (car a) 'date)
  2135. (and (eq (car a) 'intv) (math-intv-constp a))
  2136. (math-infinitep a)))
  2137. (math-reject-arg a 'anglep))
  2138. (math-max-list a b)))
  2139. (defun math-max-list (a b)
  2140. (if b
  2141. (if (or (Math-anglep (car b)) (eq (caar b) 'date)
  2142. (and (eq (car (car b)) 'intv) (math-intv-constp (car b)))
  2143. (math-infinitep (car b)))
  2144. (math-max-list (math-max a (car b)) (cdr b))
  2145. (math-reject-arg (car b) 'anglep))
  2146. a))
  2147. ;;; Compute the absolute value of A. [O O; r r] [Public]
  2148. (defun math-abs (a)
  2149. (cond ((Math-negp a)
  2150. (math-neg a))
  2151. ((Math-anglep a)
  2152. a)
  2153. ((eq (car a) 'cplx)
  2154. (math-hypot (nth 1 a) (nth 2 a)))
  2155. ((eq (car a) 'polar)
  2156. (nth 1 a))
  2157. ((eq (car a) 'vec)
  2158. (if (cdr (cdr (cdr a)))
  2159. (math-sqrt (calcFunc-abssqr a))
  2160. (if (cdr (cdr a))
  2161. (math-hypot (nth 1 a) (nth 2 a))
  2162. (if (cdr a)
  2163. (math-abs (nth 1 a))
  2164. a))))
  2165. ((eq (car a) 'sdev)
  2166. (list 'sdev (math-abs (nth 1 a)) (nth 2 a)))
  2167. ((and (eq (car a) 'intv) (math-intv-constp a))
  2168. (if (Math-posp a)
  2169. a
  2170. (let* ((nlo (math-neg (nth 2 a)))
  2171. (res (math-compare nlo (nth 3 a))))
  2172. (cond ((= res 1)
  2173. (math-make-intv (if (memq (nth 1 a) '(0 1)) 2 3) 0 nlo))
  2174. ((= res 0)
  2175. (math-make-intv (if (eq (nth 1 a) 0) 2 3) 0 nlo))
  2176. (t
  2177. (math-make-intv (if (memq (nth 1 a) '(0 2)) 2 3)
  2178. 0 (nth 3 a)))))))
  2179. ((math-looks-negp a)
  2180. (list 'calcFunc-abs (math-neg a)))
  2181. ((let ((signs (math-possible-signs a)))
  2182. (or (and (memq signs '(2 4 6)) a)
  2183. (and (memq signs '(1 3)) (math-neg a)))))
  2184. ((let ((inf (math-infinitep a)))
  2185. (and inf
  2186. (if (equal inf '(var nan var-nan))
  2187. inf
  2188. '(var inf var-inf)))))
  2189. (t (calc-record-why 'numvecp a)
  2190. (list 'calcFunc-abs a))))
  2191. (defalias 'calcFunc-abs 'math-abs)
  2192. (defun math-float-fancy (a)
  2193. (cond ((eq (car a) 'intv)
  2194. (cons (car a) (cons (nth 1 a) (mapcar 'math-float (nthcdr 2 a)))))
  2195. ((and (memq (car a) '(* /))
  2196. (math-numberp (nth 1 a)))
  2197. (list (car a) (math-float (nth 1 a))
  2198. (list 'calcFunc-float (nth 2 a))))
  2199. ((and (eq (car a) '/)
  2200. (eq (car (nth 1 a)) '*)
  2201. (math-numberp (nth 1 (nth 1 a))))
  2202. (list '* (math-float (nth 1 (nth 1 a)))
  2203. (list 'calcFunc-float (list '/ (nth 2 (nth 1 a)) (nth 2 a)))))
  2204. ((math-infinitep a) a)
  2205. ((eq (car a) 'calcFunc-float) a)
  2206. ((let ((func (assq (car a) '((calcFunc-floor . calcFunc-ffloor)
  2207. (calcFunc-ceil . calcFunc-fceil)
  2208. (calcFunc-trunc . calcFunc-ftrunc)
  2209. (calcFunc-round . calcFunc-fround)
  2210. (calcFunc-rounde . calcFunc-frounde)
  2211. (calcFunc-roundu . calcFunc-froundu)))))
  2212. (and func (cons (cdr func) (cdr a)))))
  2213. (t (math-reject-arg a 'objectp))))
  2214. (defalias 'calcFunc-float 'math-float)
  2215. ;; The variable math-trunc-prec is local to math-trunc in calc-misc.el,
  2216. ;; but used by math-trunc-fancy which is called by math-trunc.
  2217. (defvar math-trunc-prec)
  2218. (defun math-trunc-fancy (a)
  2219. (cond ((eq (car a) 'frac) (math-quotient (nth 1 a) (nth 2 a)))
  2220. ((eq (car a) 'cplx) (math-trunc (nth 1 a)))
  2221. ((eq (car a) 'polar) (math-trunc (math-complex a)))
  2222. ((eq (car a) 'hms) (list 'hms (nth 1 a) 0 0))
  2223. ((eq (car a) 'date) (list 'date (math-trunc (nth 1 a))))
  2224. ((eq (car a) 'mod)
  2225. (if (math-messy-integerp (nth 2 a))
  2226. (math-trunc (math-make-mod (nth 1 a) (math-trunc (nth 2 a))))
  2227. (math-make-mod (math-trunc (nth 1 a)) (nth 2 a))))
  2228. ((eq (car a) 'intv)
  2229. (math-make-intv (+ (if (and (equal (nth 2 a) '(neg (var inf var-inf)))
  2230. (memq (nth 1 a) '(0 1)))
  2231. 0 2)
  2232. (if (and (equal (nth 3 a) '(var inf var-inf))
  2233. (memq (nth 1 a) '(0 2)))
  2234. 0 1))
  2235. (if (and (Math-negp (nth 2 a))
  2236. (Math-num-integerp (nth 2 a))
  2237. (memq (nth 1 a) '(0 1)))
  2238. (math-add (math-trunc (nth 2 a)) 1)
  2239. (math-trunc (nth 2 a)))
  2240. (if (and (Math-posp (nth 3 a))
  2241. (Math-num-integerp (nth 3 a))
  2242. (memq (nth 1 a) '(0 2)))
  2243. (math-add (math-trunc (nth 3 a)) -1)
  2244. (math-trunc (nth 3 a)))))
  2245. ((math-provably-integerp a) a)
  2246. ((Math-vectorp a)
  2247. (math-map-vec (function (lambda (x) (math-trunc x math-trunc-prec))) a))
  2248. ((math-infinitep a)
  2249. (if (or (math-posp a) (math-negp a))
  2250. a
  2251. '(var nan var-nan)))
  2252. ((math-to-integer a))
  2253. (t (math-reject-arg a 'numberp))))
  2254. (defun math-trunc-special (a prec)
  2255. (if (Math-messy-integerp prec)
  2256. (setq prec (math-trunc prec)))
  2257. (or (integerp prec)
  2258. (math-reject-arg prec 'fixnump))
  2259. (if (and (<= prec 0)
  2260. (math-provably-integerp a))
  2261. a
  2262. (calcFunc-scf (math-trunc (let ((calc-prefer-frac t))
  2263. (calcFunc-scf a prec)))
  2264. (- prec))))
  2265. (defun math-to-integer (a)
  2266. (let ((func (assq (car-safe a) '((calcFunc-ffloor . calcFunc-floor)
  2267. (calcFunc-fceil . calcFunc-ceil)
  2268. (calcFunc-ftrunc . calcFunc-trunc)
  2269. (calcFunc-fround . calcFunc-round)
  2270. (calcFunc-frounde . calcFunc-rounde)
  2271. (calcFunc-froundu . calcFunc-roundu)))))
  2272. (and func (= (length a) 2)
  2273. (cons (cdr func) (cdr a)))))
  2274. (defun calcFunc-ftrunc (a &optional prec)
  2275. (if (and (Math-messy-integerp a)
  2276. (or (not prec) (and (integerp prec)
  2277. (<= prec 0))))
  2278. a
  2279. (math-float (math-trunc a prec))))
  2280. ;; The variable math-floor-prec is local to math-floor in calc-misc.el,
  2281. ;; but used by math-floor-fancy which is called by math-floor.
  2282. (defvar math-floor-prec)
  2283. (defun math-floor-fancy (a)
  2284. (cond ((math-provably-integerp a) a)
  2285. ((eq (car a) 'hms)
  2286. (if (or (math-posp a)
  2287. (and (math-zerop (nth 2 a))
  2288. (math-zerop (nth 3 a))))
  2289. (math-trunc a)
  2290. (math-add (math-trunc a) -1)))
  2291. ((eq (car a) 'date) (list 'date (math-floor (nth 1 a))))
  2292. ((eq (car a) 'intv)
  2293. (math-make-intv (+ (if (and (equal (nth 2 a) '(neg (var inf var-inf)))
  2294. (memq (nth 1 a) '(0 1)))
  2295. 0 2)
  2296. (if (and (equal (nth 3 a) '(var inf var-inf))
  2297. (memq (nth 1 a) '(0 2)))
  2298. 0 1))
  2299. (math-floor (nth 2 a))
  2300. (if (and (Math-num-integerp (nth 3 a))
  2301. (memq (nth 1 a) '(0 2)))
  2302. (math-add (math-floor (nth 3 a)) -1)
  2303. (math-floor (nth 3 a)))))
  2304. ((Math-vectorp a)
  2305. (math-map-vec (function (lambda (x) (math-floor x math-floor-prec))) a))
  2306. ((math-infinitep a)
  2307. (if (or (math-posp a) (math-negp a))
  2308. a
  2309. '(var nan var-nan)))
  2310. ((math-to-integer a))
  2311. (t (math-reject-arg a 'anglep))))
  2312. (defun math-floor-special (a prec)
  2313. (if (Math-messy-integerp prec)
  2314. (setq prec (math-trunc prec)))
  2315. (or (integerp prec)
  2316. (math-reject-arg prec 'fixnump))
  2317. (if (and (<= prec 0)
  2318. (math-provably-integerp a))
  2319. a
  2320. (calcFunc-scf (math-floor (let ((calc-prefer-frac t))
  2321. (calcFunc-scf a prec)))
  2322. (- prec))))
  2323. (defun calcFunc-ffloor (a &optional prec)
  2324. (if (and (Math-messy-integerp a)
  2325. (or (not prec) (and (integerp prec)
  2326. (<= prec 0))))
  2327. a
  2328. (math-float (math-floor a prec))))
  2329. ;;; Coerce A to be an integer (by truncation toward plus infinity). [I N]
  2330. (defun math-ceiling (a &optional prec) ; [Public]
  2331. (cond (prec
  2332. (if (Math-messy-integerp prec)
  2333. (setq prec (math-trunc prec)))
  2334. (or (integerp prec)
  2335. (math-reject-arg prec 'fixnump))
  2336. (if (and (<= prec 0)
  2337. (math-provably-integerp a))
  2338. a
  2339. (calcFunc-scf (math-ceiling (let ((calc-prefer-frac t))
  2340. (calcFunc-scf a prec)))
  2341. (- prec))))
  2342. ((Math-integerp a) a)
  2343. ((Math-messy-integerp a) (math-trunc a))
  2344. ((Math-realp a)
  2345. (if (Math-posp a)
  2346. (math-add (math-trunc a) 1)
  2347. (math-trunc a)))
  2348. ((math-provably-integerp a) a)
  2349. ((eq (car a) 'hms)
  2350. (if (or (math-negp a)
  2351. (and (math-zerop (nth 2 a))
  2352. (math-zerop (nth 3 a))))
  2353. (math-trunc a)
  2354. (math-add (math-trunc a) 1)))
  2355. ((eq (car a) 'date) (list 'date (math-ceiling (nth 1 a))))
  2356. ((eq (car a) 'intv)
  2357. (math-make-intv (+ (if (and (equal (nth 2 a) '(neg (var inf var-inf)))
  2358. (memq (nth 1 a) '(0 1)))
  2359. 0 2)
  2360. (if (and (equal (nth 3 a) '(var inf var-inf))
  2361. (memq (nth 1 a) '(0 2)))
  2362. 0 1))
  2363. (if (and (Math-num-integerp (nth 2 a))
  2364. (memq (nth 1 a) '(0 1)))
  2365. (math-add (math-floor (nth 2 a)) 1)
  2366. (math-ceiling (nth 2 a)))
  2367. (math-ceiling (nth 3 a))))
  2368. ((Math-vectorp a)
  2369. (math-map-vec (function (lambda (x) (math-ceiling x prec))) a))
  2370. ((math-infinitep a)
  2371. (if (or (math-posp a) (math-negp a))
  2372. a
  2373. '(var nan var-nan)))
  2374. ((math-to-integer a))
  2375. (t (math-reject-arg a 'anglep))))
  2376. (defalias 'calcFunc-ceil 'math-ceiling)
  2377. (defun calcFunc-fceil (a &optional prec)
  2378. (if (and (Math-messy-integerp a)
  2379. (or (not prec) (and (integerp prec)
  2380. (<= prec 0))))
  2381. a
  2382. (math-float (math-ceiling a prec))))
  2383. (defvar math-rounding-mode nil)
  2384. ;;; Coerce A to be an integer (by rounding to nearest integer). [I N] [Public]
  2385. (defun math-round (a &optional prec)
  2386. (cond (prec
  2387. (if (Math-messy-integerp prec)
  2388. (setq prec (math-trunc prec)))
  2389. (or (integerp prec)
  2390. (math-reject-arg prec 'fixnump))
  2391. (if (and (<= prec 0)
  2392. (math-provably-integerp a))
  2393. a
  2394. (calcFunc-scf (math-round (let ((calc-prefer-frac t))
  2395. (calcFunc-scf a prec)))
  2396. (- prec))))
  2397. ((Math-anglep a)
  2398. (if (Math-num-integerp a)
  2399. (math-trunc a)
  2400. (if (and (Math-negp a) (not (eq math-rounding-mode 'up)))
  2401. (math-neg (math-round (math-neg a)))
  2402. (setq a (let ((calc-angle-mode 'deg)) ; in case of HMS forms
  2403. (math-add a (if (Math-ratp a)
  2404. '(frac 1 2)
  2405. '(float 5 -1)))))
  2406. (if (and (Math-num-integerp a) (eq math-rounding-mode 'even))
  2407. (progn
  2408. (setq a (math-floor a))
  2409. (or (math-evenp a)
  2410. (setq a (math-sub a 1)))
  2411. a)
  2412. (math-floor a)))))
  2413. ((math-provably-integerp a) a)
  2414. ((eq (car a) 'date) (list 'date (math-round (nth 1 a))))
  2415. ((eq (car a) 'intv)
  2416. (math-floor (math-add a '(frac 1 2))))
  2417. ((Math-vectorp a)
  2418. (math-map-vec (function (lambda (x) (math-round x prec))) a))
  2419. ((math-infinitep a)
  2420. (if (or (math-posp a) (math-negp a))
  2421. a
  2422. '(var nan var-nan)))
  2423. ((math-to-integer a))
  2424. (t (math-reject-arg a 'anglep))))
  2425. (defalias 'calcFunc-round 'math-round)
  2426. (defsubst calcFunc-rounde (a &optional prec)
  2427. (let ((math-rounding-mode 'even))
  2428. (math-round a prec)))
  2429. (defsubst calcFunc-roundu (a &optional prec)
  2430. (let ((math-rounding-mode 'up))
  2431. (math-round a prec)))
  2432. (defun calcFunc-fround (a &optional prec)
  2433. (if (and (Math-messy-integerp a)
  2434. (or (not prec) (and (integerp prec)
  2435. (<= prec 0))))
  2436. a
  2437. (math-float (math-round a prec))))
  2438. (defsubst calcFunc-frounde (a &optional prec)
  2439. (let ((math-rounding-mode 'even))
  2440. (calcFunc-fround a prec)))
  2441. (defsubst calcFunc-froundu (a &optional prec)
  2442. (let ((math-rounding-mode 'up))
  2443. (calcFunc-fround a prec)))
  2444. ;;; Pull floating-point values apart into mantissa and exponent.
  2445. (defun calcFunc-mant (x)
  2446. (if (Math-realp x)
  2447. (if (or (Math-ratp x)
  2448. (eq (nth 1 x) 0))
  2449. x
  2450. (list 'float (nth 1 x) (- 1 (math-numdigs (nth 1 x)))))
  2451. (calc-record-why 'realp x)
  2452. (list 'calcFunc-mant x)))
  2453. (defun calcFunc-xpon (x)
  2454. (if (Math-realp x)
  2455. (if (or (Math-ratp x)
  2456. (eq (nth 1 x) 0))
  2457. 0
  2458. (math-normalize (+ (nth 2 x) (1- (math-numdigs (nth 1 x))))))
  2459. (calc-record-why 'realp x)
  2460. (list 'calcFunc-xpon x)))
  2461. (defun calcFunc-scf (x n)
  2462. (if (integerp n)
  2463. (cond ((eq n 0)
  2464. x)
  2465. ((Math-integerp x)
  2466. (if (> n 0)
  2467. (math-scale-int x n)
  2468. (math-div x (math-scale-int 1 (- n)))))
  2469. ((eq (car x) 'frac)
  2470. (if (> n 0)
  2471. (math-make-frac (math-scale-int (nth 1 x) n) (nth 2 x))
  2472. (math-make-frac (nth 1 x) (math-scale-int (nth 2 x) (- n)))))
  2473. ((eq (car x) 'float)
  2474. (math-make-float (nth 1 x) (+ (nth 2 x) n)))
  2475. ((memq (car x) '(cplx sdev))
  2476. (math-normalize
  2477. (list (car x)
  2478. (calcFunc-scf (nth 1 x) n)
  2479. (calcFunc-scf (nth 2 x) n))))
  2480. ((memq (car x) '(polar mod))
  2481. (math-normalize
  2482. (list (car x)
  2483. (calcFunc-scf (nth 1 x) n)
  2484. (nth 2 x))))
  2485. ((eq (car x) 'intv)
  2486. (math-normalize
  2487. (list (car x)
  2488. (nth 1 x)
  2489. (calcFunc-scf (nth 2 x) n)
  2490. (calcFunc-scf (nth 3 x) n))))
  2491. ((eq (car x) 'vec)
  2492. (math-map-vec (function (lambda (x) (calcFunc-scf x n))) x))
  2493. ((math-infinitep x)
  2494. x)
  2495. (t
  2496. (calc-record-why 'realp x)
  2497. (list 'calcFunc-scf x n)))
  2498. (if (math-messy-integerp n)
  2499. (if (< (nth 2 n) 10)
  2500. (calcFunc-scf x (math-trunc n))
  2501. (math-overflow n))
  2502. (if (math-integerp n)
  2503. (math-overflow n)
  2504. (calc-record-why 'integerp n)
  2505. (list 'calcFunc-scf x n)))))
  2506. (defun calcFunc-incr (x &optional step relative-to)
  2507. (or step (setq step 1))
  2508. (cond ((not (Math-integerp step))
  2509. (math-reject-arg step 'integerp))
  2510. ((Math-integerp x)
  2511. (math-add x step))
  2512. ((eq (car x) 'float)
  2513. (if (and (math-zerop x)
  2514. (eq (car-safe relative-to) 'float))
  2515. (math-mul step
  2516. (calcFunc-scf relative-to (- 1 calc-internal-prec)))
  2517. (math-add-float x (math-make-float
  2518. step
  2519. (+ (nth 2 x)
  2520. (- (math-numdigs (nth 1 x))
  2521. calc-internal-prec))))))
  2522. ((eq (car x) 'date)
  2523. (if (Math-integerp (nth 1 x))
  2524. (math-add x step)
  2525. (math-add x (list 'hms 0 0 step))))
  2526. (t
  2527. (math-reject-arg x 'realp))))
  2528. (defsubst calcFunc-decr (x &optional step relative-to)
  2529. (calcFunc-incr x (math-neg (or step 1)) relative-to))
  2530. (defun calcFunc-percent (x)
  2531. (if (math-objectp x)
  2532. (let ((calc-prefer-frac nil))
  2533. (math-div x 100))
  2534. (list 'calcFunc-percent x)))
  2535. (defun calcFunc-relch (x y)
  2536. (if (and (math-objectp x) (math-objectp y))
  2537. (math-div (math-sub y x) x)
  2538. (list 'calcFunc-relch x y)))
  2539. ;;; Compute the absolute value squared of A. [F N] [Public]
  2540. (defun calcFunc-abssqr (a)
  2541. (cond ((Math-realp a)
  2542. (math-mul a a))
  2543. ((eq (car a) 'cplx)
  2544. (math-add (math-sqr (nth 1 a))
  2545. (math-sqr (nth 2 a))))
  2546. ((eq (car a) 'polar)
  2547. (math-sqr (nth 1 a)))
  2548. ((and (memq (car a) '(sdev intv)) (math-constp a))
  2549. (math-sqr (math-abs a)))
  2550. ((eq (car a) 'vec)
  2551. (math-reduce-vec 'math-add (math-map-vec 'calcFunc-abssqr a)))
  2552. ((math-known-realp a)
  2553. (math-pow a 2))
  2554. ((let ((inf (math-infinitep a)))
  2555. (and inf
  2556. (math-mul (calcFunc-abssqr (math-infinite-dir a inf)) inf))))
  2557. (t (calc-record-why 'numvecp a)
  2558. (list 'calcFunc-abssqr a))))
  2559. (defsubst math-sqr (a)
  2560. (math-mul a a))
  2561. ;;;; Number theory.
  2562. (defun calcFunc-idiv (a b) ; [I I I] [Public]
  2563. (cond ((and (Math-natnump a) (Math-natnump b) (not (eq b 0)))
  2564. (math-quotient a b))
  2565. ((Math-realp a)
  2566. (if (Math-realp b)
  2567. (let ((calc-prefer-frac t))
  2568. (math-floor (math-div a b)))
  2569. (math-reject-arg b 'realp)))
  2570. ((eq (car-safe a) 'hms)
  2571. (if (eq (car-safe b) 'hms)
  2572. (let ((calc-prefer-frac t))
  2573. (math-floor (math-div a b)))
  2574. (math-reject-arg b 'hmsp)))
  2575. ((and (or (eq (car-safe a) 'intv) (Math-realp a))
  2576. (or (eq (car-safe b) 'intv) (Math-realp b)))
  2577. (math-floor (math-div a b)))
  2578. ((or (math-infinitep a)
  2579. (math-infinitep b))
  2580. (math-div a b))
  2581. (t (math-reject-arg a 'anglep))))
  2582. ;;; Combine two terms being added, if possible.
  2583. (defun math-combine-sum (a b nega negb scalar-okay)
  2584. (if (and scalar-okay (Math-objvecp a) (Math-objvecp b))
  2585. (math-add-or-sub a b nega negb)
  2586. (let ((amult 1) (bmult 1))
  2587. (and (consp a)
  2588. (cond ((and (eq (car a) '*)
  2589. (Math-objectp (nth 1 a)))
  2590. (setq amult (nth 1 a)
  2591. a (nth 2 a)))
  2592. ((and (eq (car a) '/)
  2593. (Math-objectp (nth 2 a)))
  2594. (setq amult (if (Math-integerp (nth 2 a))
  2595. (list 'frac 1 (nth 2 a))
  2596. (math-div 1 (nth 2 a)))
  2597. a (nth 1 a)))
  2598. ((eq (car a) 'neg)
  2599. (setq amult -1
  2600. a (nth 1 a)))))
  2601. (and (consp b)
  2602. (cond ((and (eq (car b) '*)
  2603. (Math-objectp (nth 1 b)))
  2604. (setq bmult (nth 1 b)
  2605. b (nth 2 b)))
  2606. ((and (eq (car b) '/)
  2607. (Math-objectp (nth 2 b)))
  2608. (setq bmult (if (Math-integerp (nth 2 b))
  2609. (list 'frac 1 (nth 2 b))
  2610. (math-div 1 (nth 2 b)))
  2611. b (nth 1 b)))
  2612. ((eq (car b) 'neg)
  2613. (setq bmult -1
  2614. b (nth 1 b)))))
  2615. (and (if math-simplifying
  2616. (Math-equal a b)
  2617. (equal a b))
  2618. (progn
  2619. (if nega (setq amult (math-neg amult)))
  2620. (if negb (setq bmult (math-neg bmult)))
  2621. (setq amult (math-add amult bmult))
  2622. (math-mul amult a))))))
  2623. (defun math-add-or-sub (a b aneg bneg)
  2624. (if aneg (setq a (math-neg a)))
  2625. (if bneg (setq b (math-neg b)))
  2626. (if (or (Math-vectorp a) (Math-vectorp b))
  2627. (math-normalize (list '+ a b))
  2628. (math-add a b)))
  2629. (defvar math-combine-prod-e '(var e var-e))
  2630. ;;; The following is expanded out four ways for speed.
  2631. ;; math-unit-prefixes is defined in calc-units.el,
  2632. ;; but used here.
  2633. (defvar math-unit-prefixes)
  2634. (defun math-combine-prod (a b inva invb scalar-okay)
  2635. (cond
  2636. ((or (and inva (Math-zerop a))
  2637. (and invb (Math-zerop b)))
  2638. nil)
  2639. ((and scalar-okay (Math-objvecp a) (Math-objvecp b))
  2640. (setq a (math-mul-or-div a b inva invb))
  2641. (and (Math-objvecp a)
  2642. a))
  2643. ((and (eq (car-safe a) '^)
  2644. inva
  2645. (math-looks-negp (nth 2 a)))
  2646. (math-mul (math-pow (nth 1 a) (math-neg (nth 2 a))) b))
  2647. ((and (eq (car-safe b) '^)
  2648. invb
  2649. (math-looks-negp (nth 2 b)))
  2650. (math-mul a (math-pow (nth 1 b) (math-neg (nth 2 b)))))
  2651. ((and math-simplifying
  2652. (math-combine-prod-trig a b)))
  2653. (t (let ((apow 1) (bpow 1))
  2654. (and (consp a)
  2655. (cond ((and (eq (car a) '^)
  2656. (or math-simplifying
  2657. (Math-numberp (nth 2 a))))
  2658. (setq apow (nth 2 a)
  2659. a (nth 1 a)))
  2660. ((eq (car a) 'calcFunc-sqrt)
  2661. (setq apow '(frac 1 2)
  2662. a (nth 1 a)))
  2663. ((and (eq (car a) 'calcFunc-exp)
  2664. (or math-simplifying
  2665. (Math-numberp (nth 1 a))))
  2666. (setq apow (nth 1 a)
  2667. a math-combine-prod-e))))
  2668. (and (consp a) (eq (car a) 'frac)
  2669. (Math-lessp (nth 1 a) (nth 2 a))
  2670. (setq a (math-div 1 a) apow (math-neg apow)))
  2671. (and (consp b)
  2672. (cond ((and (eq (car b) '^)
  2673. (or math-simplifying
  2674. (Math-numberp (nth 2 b))))
  2675. (setq bpow (nth 2 b)
  2676. b (nth 1 b)))
  2677. ((eq (car b) 'calcFunc-sqrt)
  2678. (setq bpow '(frac 1 2)
  2679. b (nth 1 b)))
  2680. ((and (eq (car b) 'calcFunc-exp)
  2681. (or math-simplifying
  2682. (Math-numberp (nth 1 b))))
  2683. (setq bpow (nth 1 b)
  2684. b math-combine-prod-e))))
  2685. (and (consp b) (eq (car b) 'frac)
  2686. (Math-lessp (nth 1 b) (nth 2 b))
  2687. (setq b (math-div 1 b) bpow (math-neg bpow)))
  2688. (if inva (setq apow (math-neg apow)))
  2689. (if invb (setq bpow (math-neg bpow)))
  2690. (or (and (if math-simplifying
  2691. (math-commutative-equal a b)
  2692. (equal a b))
  2693. (let ((sumpow (math-add apow bpow)))
  2694. (and (or (not (Math-integerp a))
  2695. (Math-zerop sumpow)
  2696. (eq (eq (car-safe apow) 'frac)
  2697. (eq (car-safe bpow) 'frac)))
  2698. (progn
  2699. (and (math-looks-negp sumpow)
  2700. (Math-ratp a) (Math-posp a)
  2701. (setq a (math-div 1 a)
  2702. sumpow (math-neg sumpow)))
  2703. (cond ((equal sumpow '(frac 1 2))
  2704. (list 'calcFunc-sqrt a))
  2705. ((equal sumpow '(frac -1 2))
  2706. (math-div 1 (list 'calcFunc-sqrt a)))
  2707. ((and (eq a math-combine-prod-e)
  2708. (eq a b))
  2709. (list 'calcFunc-exp sumpow))
  2710. (t
  2711. (condition-case err
  2712. (math-pow a sumpow)
  2713. (inexact-result (list '^ a sumpow)))))))))
  2714. (and math-simplifying-units
  2715. math-combining-units
  2716. (let* ((ua (math-check-unit-name a))
  2717. ub)
  2718. (and ua
  2719. (eq ua (setq ub (math-check-unit-name b)))
  2720. (progn
  2721. (setq ua (if (eq (nth 1 a) (car ua))
  2722. 1
  2723. (nth 1 (assq (aref (symbol-name (nth 1 a))
  2724. 0)
  2725. math-unit-prefixes)))
  2726. ub (if (eq (nth 1 b) (car ub))
  2727. 1
  2728. (nth 1 (assq (aref (symbol-name (nth 1 b))
  2729. 0)
  2730. math-unit-prefixes))))
  2731. (if (Math-lessp ua ub)
  2732. (let (temp)
  2733. (setq temp a a b b temp
  2734. temp ua ua ub ub temp
  2735. temp apow apow bpow bpow temp)))
  2736. (math-mul (math-pow (math-div ua ub) apow)
  2737. (math-pow b (math-add apow bpow)))))))
  2738. (and (equal apow bpow)
  2739. (Math-natnump a) (Math-natnump b)
  2740. (cond ((equal apow '(frac 1 2))
  2741. (list 'calcFunc-sqrt (math-mul a b)))
  2742. ((equal apow '(frac -1 2))
  2743. (math-div 1 (list 'calcFunc-sqrt (math-mul a b))))
  2744. (t
  2745. (setq a (math-mul a b))
  2746. (condition-case err
  2747. (math-pow a apow)
  2748. (inexact-result (list '^ a apow)))))))))))
  2749. (defun math-combine-prod-trig (a b)
  2750. (cond
  2751. ((and (eq (car-safe a) 'calcFunc-sin)
  2752. (eq (car-safe b) 'calcFunc-csc)
  2753. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2754. 1)
  2755. ((and (eq (car-safe a) 'calcFunc-sin)
  2756. (eq (car-safe b) 'calcFunc-sec)
  2757. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2758. (cons 'calcFunc-tan (cdr a)))
  2759. ((and (eq (car-safe a) 'calcFunc-sin)
  2760. (eq (car-safe b) 'calcFunc-cot)
  2761. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2762. (cons 'calcFunc-cos (cdr a)))
  2763. ((and (eq (car-safe a) 'calcFunc-cos)
  2764. (eq (car-safe b) 'calcFunc-sec)
  2765. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2766. 1)
  2767. ((and (eq (car-safe a) 'calcFunc-cos)
  2768. (eq (car-safe b) 'calcFunc-csc)
  2769. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2770. (cons 'calcFunc-cot (cdr a)))
  2771. ((and (eq (car-safe a) 'calcFunc-cos)
  2772. (eq (car-safe b) 'calcFunc-tan)
  2773. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2774. (cons 'calcFunc-sin (cdr a)))
  2775. ((and (eq (car-safe a) 'calcFunc-tan)
  2776. (eq (car-safe b) 'calcFunc-cot)
  2777. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2778. 1)
  2779. ((and (eq (car-safe a) 'calcFunc-tan)
  2780. (eq (car-safe b) 'calcFunc-csc)
  2781. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2782. (cons 'calcFunc-sec (cdr a)))
  2783. ((and (eq (car-safe a) 'calcFunc-sec)
  2784. (eq (car-safe b) 'calcFunc-cot)
  2785. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2786. (cons 'calcFunc-csc (cdr a)))
  2787. ((and (eq (car-safe a) 'calcFunc-sinh)
  2788. (eq (car-safe b) 'calcFunc-csch)
  2789. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2790. 1)
  2791. ((and (eq (car-safe a) 'calcFunc-sinh)
  2792. (eq (car-safe b) 'calcFunc-sech)
  2793. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2794. (cons 'calcFunc-tanh (cdr a)))
  2795. ((and (eq (car-safe a) 'calcFunc-sinh)
  2796. (eq (car-safe b) 'calcFunc-coth)
  2797. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2798. (cons 'calcFunc-cosh (cdr a)))
  2799. ((and (eq (car-safe a) 'calcFunc-cosh)
  2800. (eq (car-safe b) 'calcFunc-sech)
  2801. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2802. 1)
  2803. ((and (eq (car-safe a) 'calcFunc-cosh)
  2804. (eq (car-safe b) 'calcFunc-csch)
  2805. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2806. (cons 'calcFunc-coth (cdr a)))
  2807. ((and (eq (car-safe a) 'calcFunc-cosh)
  2808. (eq (car-safe b) 'calcFunc-tanh)
  2809. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2810. (cons 'calcFunc-sinh (cdr a)))
  2811. ((and (eq (car-safe a) 'calcFunc-tanh)
  2812. (eq (car-safe b) 'calcFunc-coth)
  2813. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2814. 1)
  2815. ((and (eq (car-safe a) 'calcFunc-tanh)
  2816. (eq (car-safe b) 'calcFunc-csch)
  2817. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2818. (cons 'calcFunc-sech (cdr a)))
  2819. ((and (eq (car-safe a) 'calcFunc-sech)
  2820. (eq (car-safe b) 'calcFunc-coth)
  2821. (= 0 (math-simplify (math-sub (cdr a) (cdr b)))))
  2822. (cons 'calcFunc-csch (cdr a)))
  2823. (t
  2824. nil)))
  2825. (defun math-mul-or-div (a b ainv binv)
  2826. (if (or (Math-vectorp a) (Math-vectorp b))
  2827. (math-normalize
  2828. (if ainv
  2829. (if binv
  2830. (list '/ (math-div 1 a) b)
  2831. (list '/ b a))
  2832. (if binv
  2833. (list '/ a b)
  2834. (list '* a b))))
  2835. (if ainv
  2836. (if binv
  2837. (math-div (math-div 1 a) b)
  2838. (math-div b a))
  2839. (if binv
  2840. (math-div a b)
  2841. (math-mul a b)))))
  2842. ;; The variable math-com-bterms is local to math-commutative-equal,
  2843. ;; but is used by math-commutative collect, which is called by
  2844. ;; math-commutative-equal.
  2845. (defvar math-com-bterms)
  2846. (defun math-commutative-equal (a b)
  2847. (if (memq (car-safe a) '(+ -))
  2848. (and (memq (car-safe b) '(+ -))
  2849. (let ((math-com-bterms nil) aterms p)
  2850. (math-commutative-collect b nil)
  2851. (setq aterms math-com-bterms math-com-bterms nil)
  2852. (math-commutative-collect a nil)
  2853. (and (= (length aterms) (length math-com-bterms))
  2854. (progn
  2855. (while (and aterms
  2856. (progn
  2857. (setq p math-com-bterms)
  2858. (while (and p (not (equal (car aterms)
  2859. (car p))))
  2860. (setq p (cdr p)))
  2861. p))
  2862. (setq math-com-bterms (delq (car p) math-com-bterms)
  2863. aterms (cdr aterms)))
  2864. (not aterms)))))
  2865. (equal a b)))
  2866. (defun math-commutative-collect (b neg)
  2867. (if (eq (car-safe b) '+)
  2868. (progn
  2869. (math-commutative-collect (nth 1 b) neg)
  2870. (math-commutative-collect (nth 2 b) neg))
  2871. (if (eq (car-safe b) '-)
  2872. (progn
  2873. (math-commutative-collect (nth 1 b) neg)
  2874. (math-commutative-collect (nth 2 b) (not neg)))
  2875. (setq math-com-bterms (cons (if neg (math-neg b) b) math-com-bterms)))))
  2876. (provide 'calc-arith)
  2877. ;;; calc-arith.el ends here