calc-arith.el 95 KB

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