calc-math.el 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167
  1. ;;; calc-math.el --- mathematical functions for Calc
  2. ;; Copyright (C) 1990-1993, 2001-2015 Free Software Foundation, Inc.
  3. ;; Author: David Gillespie <daveg@synaptics.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;; Code:
  17. ;; This file is autoloaded from calc-ext.el.
  18. (require 'calc-ext)
  19. (require 'calc-macs)
  20. ;;; Find out how many 9s in 9.9999... will give distinct Emacs floats,
  21. ;;; then back off by one.
  22. (defvar math-emacs-precision
  23. (let* ((n 1)
  24. (x 9)
  25. (xx (+ x (* 9 (expt 10 (- n))))))
  26. (while (/= x xx)
  27. (progn
  28. (setq n (1+ n))
  29. (setq x xx)
  30. (setq xx (+ x (* 9 (expt 10 (- n)))))))
  31. (1- n))
  32. "The number of digits in an Emacs float.")
  33. ;;; Find the largest power of 10 which is an Emacs float,
  34. ;;; then back off by one so that any float d.dddd...eN
  35. ;;; is an Emacs float, for acceptable d.dddd....
  36. (defvar math-largest-emacs-expt
  37. (let ((x 1)
  38. (pow 1e2))
  39. ;; The following loop is for efficiency; it should stop when
  40. ;; 10^(2x) is too large. This could be indicated by a range
  41. ;; error when computing 10^(2x) or an infinite value for 10^(2x).
  42. (while (and
  43. pow
  44. (< pow 1.0e+INF))
  45. (setq x (* 2 x))
  46. (setq pow (condition-case nil
  47. (expt 10.0 (* 2 x))
  48. (error nil))))
  49. ;; The following loop should stop when 10^(x+1) is too large.
  50. (setq pow (condition-case nil
  51. (expt 10.0 (1+ x))
  52. (error nil)))
  53. (while (and
  54. pow
  55. (< pow 1.0e+INF))
  56. (setq x (1+ x))
  57. (setq pow (condition-case nil
  58. (expt 10.0 (1+ x))
  59. (error nil))))
  60. (1- x))
  61. "The largest exponent which Calc will convert to an Emacs float.")
  62. (defvar math-smallest-emacs-expt
  63. (let ((x -1))
  64. (while (condition-case nil
  65. (> (expt 10.0 x) 0.0)
  66. (error nil))
  67. (setq x (* 2 x)))
  68. (setq x (/ x 2))
  69. (while (condition-case nil
  70. (> (expt 10.0 x) 0.0)
  71. (error nil))
  72. (setq x (1- x)))
  73. (+ x 2))
  74. "The smallest exponent which Calc will convert to an Emacs float.")
  75. (defun math-use-emacs-fn (fn x)
  76. "Use the native Emacs function FN to evaluate the Calc number X.
  77. If this can't be done, return NIL."
  78. (and
  79. (<= calc-internal-prec math-emacs-precision)
  80. (math-realp x)
  81. (let* ((fx (math-float x))
  82. (xpon (+ (nth 2 x) (1- (math-numdigs (nth 1 x))))))
  83. (and (<= math-smallest-emacs-expt xpon)
  84. (<= xpon math-largest-emacs-expt)
  85. (condition-case nil
  86. (math-read-number
  87. (number-to-string
  88. (funcall fn
  89. (string-to-number
  90. (let
  91. ((calc-number-radix 10)
  92. (calc-twos-complement-mode nil)
  93. (calc-float-format (list 'float calc-internal-prec))
  94. (calc-group-digits nil)
  95. (calc-point-char "."))
  96. (math-format-number (math-float x)))))))
  97. (error nil))))))
  98. (defun calc-sqrt (arg)
  99. (interactive "P")
  100. (calc-slow-wrapper
  101. (if (calc-is-inverse)
  102. (calc-unary-op "^2" 'calcFunc-sqr arg)
  103. (calc-unary-op "sqrt" 'calcFunc-sqrt arg))))
  104. (defun calc-isqrt (arg)
  105. (interactive "P")
  106. (calc-slow-wrapper
  107. (if (calc-is-inverse)
  108. (calc-unary-op "^2" 'calcFunc-sqr arg)
  109. (calc-unary-op "isqt" 'calcFunc-isqrt arg))))
  110. (defun calc-hypot (arg)
  111. (interactive "P")
  112. (calc-slow-wrapper
  113. (calc-binary-op "hypt" 'calcFunc-hypot arg)))
  114. (defun calc-ln (arg)
  115. (interactive "P")
  116. (calc-invert-func)
  117. (calc-exp arg))
  118. (defun calc-log10 (arg)
  119. (interactive "P")
  120. (calc-hyperbolic-func)
  121. (calc-ln arg))
  122. (defun calc-log (arg)
  123. (interactive "P")
  124. (calc-slow-wrapper
  125. (if (calc-is-inverse)
  126. (calc-binary-op "alog" 'calcFunc-alog arg)
  127. (calc-binary-op "log" 'calcFunc-log arg))))
  128. (defun calc-ilog (arg)
  129. (interactive "P")
  130. (calc-slow-wrapper
  131. (if (calc-is-inverse)
  132. (calc-binary-op "alog" 'calcFunc-alog arg)
  133. (calc-binary-op "ilog" 'calcFunc-ilog arg))))
  134. (defun calc-lnp1 (arg)
  135. (interactive "P")
  136. (calc-invert-func)
  137. (calc-expm1 arg))
  138. (defun calc-exp (arg)
  139. (interactive "P")
  140. (calc-slow-wrapper
  141. (if (calc-is-hyperbolic)
  142. (if (calc-is-inverse)
  143. (calc-unary-op "lg10" 'calcFunc-log10 arg)
  144. (calc-unary-op "10^" 'calcFunc-exp10 arg))
  145. (if (calc-is-inverse)
  146. (calc-unary-op "ln" 'calcFunc-ln arg)
  147. (calc-unary-op "exp" 'calcFunc-exp arg)))))
  148. (defun calc-expm1 (arg)
  149. (interactive "P")
  150. (calc-slow-wrapper
  151. (if (calc-is-inverse)
  152. (calc-unary-op "ln+1" 'calcFunc-lnp1 arg)
  153. (calc-unary-op "ex-1" 'calcFunc-expm1 arg))))
  154. (defun calc-pi ()
  155. (interactive)
  156. (calc-slow-wrapper
  157. (if (calc-is-inverse)
  158. (if (calc-is-hyperbolic)
  159. (if calc-symbolic-mode
  160. (calc-pop-push-record 0 "phi" '(var phi var-phi))
  161. (calc-pop-push-record 0 "phi" (math-phi)))
  162. (if calc-symbolic-mode
  163. (calc-pop-push-record 0 "gmma" '(var gamma var-gamma))
  164. (calc-pop-push-record 0 "gmma" (math-gamma-const))))
  165. (if (calc-is-hyperbolic)
  166. (if calc-symbolic-mode
  167. (calc-pop-push-record 0 "e" '(var e var-e))
  168. (calc-pop-push-record 0 "e" (math-e)))
  169. (if calc-symbolic-mode
  170. (calc-pop-push-record 0 "pi" '(var pi var-pi))
  171. (calc-pop-push-record 0 "pi" (math-pi)))))))
  172. (defun calc-sin (arg)
  173. (interactive "P")
  174. (calc-slow-wrapper
  175. (if (calc-is-hyperbolic)
  176. (if (calc-is-inverse)
  177. (calc-unary-op "asnh" 'calcFunc-arcsinh arg)
  178. (calc-unary-op "sinh" 'calcFunc-sinh arg))
  179. (if (calc-is-inverse)
  180. (calc-unary-op "asin" 'calcFunc-arcsin arg)
  181. (calc-unary-op "sin" 'calcFunc-sin arg)))))
  182. (defun calc-arcsin (arg)
  183. (interactive "P")
  184. (calc-invert-func)
  185. (calc-sin arg))
  186. (defun calc-sinh (arg)
  187. (interactive "P")
  188. (calc-hyperbolic-func)
  189. (calc-sin arg))
  190. (defun calc-arcsinh (arg)
  191. (interactive "P")
  192. (calc-invert-func)
  193. (calc-hyperbolic-func)
  194. (calc-sin arg))
  195. (defun calc-sec (arg)
  196. (interactive "P")
  197. (calc-slow-wrapper
  198. (if (calc-is-hyperbolic)
  199. (calc-unary-op "sech" 'calcFunc-sech arg)
  200. (calc-unary-op "sec" 'calcFunc-sec arg))))
  201. (defun calc-sech (arg)
  202. (interactive "P")
  203. (calc-hyperbolic-func)
  204. (calc-sec arg))
  205. (defun calc-cos (arg)
  206. (interactive "P")
  207. (calc-slow-wrapper
  208. (if (calc-is-hyperbolic)
  209. (if (calc-is-inverse)
  210. (calc-unary-op "acsh" 'calcFunc-arccosh arg)
  211. (calc-unary-op "cosh" 'calcFunc-cosh arg))
  212. (if (calc-is-inverse)
  213. (calc-unary-op "acos" 'calcFunc-arccos arg)
  214. (calc-unary-op "cos" 'calcFunc-cos arg)))))
  215. (defun calc-arccos (arg)
  216. (interactive "P")
  217. (calc-invert-func)
  218. (calc-cos arg))
  219. (defun calc-cosh (arg)
  220. (interactive "P")
  221. (calc-hyperbolic-func)
  222. (calc-cos arg))
  223. (defun calc-arccosh (arg)
  224. (interactive "P")
  225. (calc-invert-func)
  226. (calc-hyperbolic-func)
  227. (calc-cos arg))
  228. (defun calc-csc (arg)
  229. (interactive "P")
  230. (calc-slow-wrapper
  231. (if (calc-is-hyperbolic)
  232. (calc-unary-op "csch" 'calcFunc-csch arg)
  233. (calc-unary-op "csc" 'calcFunc-csc arg))))
  234. (defun calc-csch (arg)
  235. (interactive "P")
  236. (calc-hyperbolic-func)
  237. (calc-csc arg))
  238. (defun calc-sincos ()
  239. (interactive)
  240. (calc-slow-wrapper
  241. (if (calc-is-inverse)
  242. (calc-enter-result 1 "asnc" (list 'calcFunc-arcsincos (calc-top-n 1)))
  243. (calc-enter-result 1 "sncs" (list 'calcFunc-sincos (calc-top-n 1))))))
  244. (defun calc-tan (arg)
  245. (interactive "P")
  246. (calc-slow-wrapper
  247. (if (calc-is-hyperbolic)
  248. (if (calc-is-inverse)
  249. (calc-unary-op "atnh" 'calcFunc-arctanh arg)
  250. (calc-unary-op "tanh" 'calcFunc-tanh arg))
  251. (if (calc-is-inverse)
  252. (calc-unary-op "atan" 'calcFunc-arctan arg)
  253. (calc-unary-op "tan" 'calcFunc-tan arg)))))
  254. (defun calc-arctan (arg)
  255. (interactive "P")
  256. (calc-invert-func)
  257. (calc-tan arg))
  258. (defun calc-tanh (arg)
  259. (interactive "P")
  260. (calc-hyperbolic-func)
  261. (calc-tan arg))
  262. (defun calc-arctanh (arg)
  263. (interactive "P")
  264. (calc-invert-func)
  265. (calc-hyperbolic-func)
  266. (calc-tan arg))
  267. (defun calc-cot (arg)
  268. (interactive "P")
  269. (calc-slow-wrapper
  270. (if (calc-is-hyperbolic)
  271. (calc-unary-op "coth" 'calcFunc-coth arg)
  272. (calc-unary-op "cot" 'calcFunc-cot arg))))
  273. (defun calc-coth (arg)
  274. (interactive "P")
  275. (calc-hyperbolic-func)
  276. (calc-cot arg))
  277. (defun calc-arctan2 ()
  278. (interactive)
  279. (calc-slow-wrapper
  280. (calc-enter-result 2 "atn2" (cons 'calcFunc-arctan2 (calc-top-list-n 2)))))
  281. (defun calc-conj (arg)
  282. (interactive "P")
  283. (calc-wrapper
  284. (calc-unary-op "conj" 'calcFunc-conj arg)))
  285. (defun calc-imaginary ()
  286. (interactive)
  287. (calc-slow-wrapper
  288. (calc-pop-push-record 1 "i*" (math-imaginary (calc-top-n 1)))))
  289. (defun calc-to-degrees (arg)
  290. (interactive "P")
  291. (calc-wrapper
  292. (calc-unary-op ">deg" 'calcFunc-deg arg)))
  293. (defun calc-to-radians (arg)
  294. (interactive "P")
  295. (calc-wrapper
  296. (calc-unary-op ">rad" 'calcFunc-rad arg)))
  297. (defun calc-degrees-mode (arg)
  298. (interactive "p")
  299. (cond ((= arg 1)
  300. (calc-wrapper
  301. (calc-change-mode 'calc-angle-mode 'deg)
  302. (message "Angles measured in degrees")))
  303. ((= arg 2) (calc-radians-mode))
  304. ((= arg 3) (calc-hms-mode))
  305. (t (error "Prefix argument out of range"))))
  306. (defun calc-radians-mode ()
  307. (interactive)
  308. (calc-wrapper
  309. (calc-change-mode 'calc-angle-mode 'rad)
  310. (message "Angles measured in radians")))
  311. ;;; Compute the integer square-root floor(sqrt(A)). A > 0. [I I] [Public]
  312. ;;; This method takes advantage of the fact that Newton's method starting
  313. ;;; with an overestimate always works, even using truncating integer division!
  314. (defun math-isqrt (a)
  315. (cond ((Math-zerop a) a)
  316. ((not (math-natnump a))
  317. (math-reject-arg a 'natnump))
  318. ((integerp a)
  319. (math-isqrt-small a))
  320. (t
  321. (math-normalize (cons 'bigpos (cdr (math-isqrt-bignum (cdr a))))))))
  322. (defun calcFunc-isqrt (a)
  323. (if (math-realp a)
  324. (math-isqrt (math-floor a))
  325. (math-floor (math-sqrt a))))
  326. ;;; This returns (flag . result) where the flag is t if A is a perfect square.
  327. (defun math-isqrt-bignum (a) ; [P.l L]
  328. (let ((len (length a)))
  329. (if (= (% len 2) 0)
  330. (let* ((top (nthcdr (- len 2) a)))
  331. (math-isqrt-bignum-iter
  332. a
  333. (math-scale-bignum-digit-size
  334. (math-bignum-big
  335. (1+ (math-isqrt-small
  336. (+ (* (nth 1 top) math-bignum-digit-size) (car top)))))
  337. (1- (/ len 2)))))
  338. (let* ((top (nth (1- len) a)))
  339. (math-isqrt-bignum-iter
  340. a
  341. (math-scale-bignum-digit-size
  342. (list (1+ (math-isqrt-small top)))
  343. (/ len 2)))))))
  344. (defun math-isqrt-bignum-iter (a guess) ; [l L l]
  345. (math-working "isqrt" (cons 'bigpos guess))
  346. (let* ((q (math-div-bignum a guess))
  347. (s (math-add-bignum (car q) guess))
  348. (g2 (math-div2-bignum s))
  349. (comp (math-compare-bignum g2 guess)))
  350. (if (< comp 0)
  351. (math-isqrt-bignum-iter a g2)
  352. (cons (and (= comp 0)
  353. (math-zerop-bignum (cdr q))
  354. (= (% (car s) 2) 0))
  355. guess))))
  356. (defun math-zerop-bignum (a)
  357. (and (eq (car a) 0)
  358. (progn
  359. (while (eq (car (setq a (cdr a))) 0))
  360. (null a))))
  361. (defun math-scale-bignum-digit-size (a n) ; [L L S]
  362. (while (> n 0)
  363. (setq a (cons 0 a)
  364. n (1- n)))
  365. a)
  366. (defun math-isqrt-small (a) ; A > 0. [S S]
  367. (let ((g (cond ((>= a 1000000) 10000)
  368. ((>= a 10000) 1000)
  369. ((>= a 100) 100)
  370. (t 10)))
  371. g2)
  372. (while (< (setq g2 (/ (+ g (/ a g)) 2)) g)
  373. (setq g g2))
  374. g))
  375. ;;; Compute the square root of a number.
  376. ;;; [T N] if possible, else [F N] if possible, else [C N]. [Public]
  377. (defun math-sqrt (a)
  378. (or
  379. (and (Math-zerop a) a)
  380. (and (math-known-nonposp a)
  381. (math-imaginary (math-sqrt (math-neg a))))
  382. (and (integerp a)
  383. (let ((sqrt (math-isqrt-small a)))
  384. (if (= (* sqrt sqrt) a)
  385. sqrt
  386. (if calc-symbolic-mode
  387. (list 'calcFunc-sqrt a)
  388. (math-sqrt-float (math-float a) (math-float sqrt))))))
  389. (and (eq (car-safe a) 'bigpos)
  390. (let* ((res (math-isqrt-bignum (cdr a)))
  391. (sqrt (math-normalize (cons 'bigpos (cdr res)))))
  392. (if (car res)
  393. sqrt
  394. (if calc-symbolic-mode
  395. (list 'calcFunc-sqrt a)
  396. (math-sqrt-float (math-float a) (math-float sqrt))))))
  397. (and (eq (car-safe a) 'frac)
  398. (let* ((num-res (math-isqrt-bignum (cdr (Math-bignum-test (nth 1 a)))))
  399. (num-sqrt (math-normalize (cons 'bigpos (cdr num-res))))
  400. (den-res (math-isqrt-bignum (cdr (Math-bignum-test (nth 2 a)))))
  401. (den-sqrt (math-normalize (cons 'bigpos (cdr den-res)))))
  402. (if (and (car num-res) (car den-res))
  403. (list 'frac num-sqrt den-sqrt)
  404. (if calc-symbolic-mode
  405. (if (or (car num-res) (car den-res))
  406. (math-div (if (car num-res)
  407. num-sqrt (list 'calcFunc-sqrt (nth 1 a)))
  408. (if (car den-res)
  409. den-sqrt (list 'calcFunc-sqrt (nth 2 a))))
  410. (list 'calcFunc-sqrt a))
  411. (math-sqrt-float (math-float a)
  412. (math-div (math-float num-sqrt) den-sqrt))))))
  413. (and (eq (car-safe a) 'float)
  414. (if calc-symbolic-mode
  415. (if (= (% (nth 2 a) 2) 0)
  416. (let ((res (math-isqrt-bignum
  417. (cdr (Math-bignum-test (nth 1 a))))))
  418. (if (car res)
  419. (math-make-float (math-normalize
  420. (cons 'bigpos (cdr res)))
  421. (/ (nth 2 a) 2))
  422. (signal 'inexact-result nil)))
  423. (signal 'inexact-result nil))
  424. (math-sqrt-float a)))
  425. (and (eq (car-safe a) 'cplx)
  426. (math-with-extra-prec 2
  427. (let* ((d (math-abs a))
  428. (imag (math-sqrt (math-mul (math-sub d (nth 1 a))
  429. '(float 5 -1)))))
  430. (list 'cplx
  431. (math-sqrt (math-mul (math-add d (nth 1 a)) '(float 5 -1)))
  432. (if (math-negp (nth 2 a)) (math-neg imag) imag)))))
  433. (and (eq (car-safe a) 'polar)
  434. (list 'polar
  435. (math-sqrt (nth 1 a))
  436. (math-mul (nth 2 a) '(float 5 -1))))
  437. (and (eq (car-safe a) 'sdev)
  438. (let ((sqrt (math-sqrt (nth 1 a))))
  439. (math-make-sdev sqrt
  440. (math-div (nth 2 a) (math-mul sqrt 2)))))
  441. (and (eq (car-safe a) 'intv)
  442. (not (math-negp (nth 2 a)))
  443. (math-make-intv (nth 1 a) (math-sqrt (nth 2 a)) (math-sqrt (nth 3 a))))
  444. (and (eq (car-safe a) '*)
  445. (or (math-known-nonnegp (nth 1 a))
  446. (math-known-nonnegp (nth 2 a)))
  447. (math-mul (math-sqrt (nth 1 a)) (math-sqrt (nth 2 a))))
  448. (and (eq (car-safe a) '/)
  449. (or (and (math-known-nonnegp (nth 2 a))
  450. (math-div (math-sqrt (nth 1 a)) (math-sqrt (nth 2 a))))
  451. (and (math-known-nonnegp (nth 1 a))
  452. (not (math-equal-int (nth 1 a) 1))
  453. (math-mul (math-sqrt (nth 1 a))
  454. (math-sqrt (math-div 1 (nth 2 a)))))))
  455. (and (eq (car-safe a) '^)
  456. (math-known-evenp (nth 2 a))
  457. (math-known-realp (nth 1 a))
  458. (math-abs (math-pow (nth 1 a) (math-div (nth 2 a) 2))))
  459. (let ((inf (math-infinitep a)))
  460. (and inf
  461. (math-mul (math-sqrt (math-infinite-dir a inf)) inf)))
  462. (progn
  463. (calc-record-why 'numberp a)
  464. (list 'calcFunc-sqrt a))))
  465. (defalias 'calcFunc-sqrt 'math-sqrt)
  466. (defun math-infinite-dir (a &optional inf)
  467. (or inf (setq inf (math-infinitep a)))
  468. (math-normalize (math-expr-subst a inf 1)))
  469. (defun math-sqrt-float (a &optional guess) ; [F F F]
  470. (if calc-symbolic-mode
  471. (signal 'inexact-result nil)
  472. (math-with-extra-prec 1 (math-sqrt-raw a guess))))
  473. (defun math-sqrt-raw (a &optional guess) ; [F F F]
  474. (if (not (Math-posp a))
  475. (math-sqrt a)
  476. (cond
  477. ((math-use-emacs-fn 'sqrt a))
  478. (t
  479. (if (null guess)
  480. (let ((ldiff (- (math-numdigs (nth 1 a)) 6)))
  481. (or (= (% (+ (nth 2 a) ldiff) 2) 0) (setq ldiff (1+ ldiff)))
  482. (setq guess (math-make-float (math-isqrt-small
  483. (math-scale-int (nth 1 a) (- ldiff)))
  484. (/ (+ (nth 2 a) ldiff) 2)))))
  485. (math-sqrt-float-iter a guess)))))
  486. (defun math-sqrt-float-iter (a guess) ; [F F F]
  487. (math-working "sqrt" guess)
  488. (let ((g2 (math-mul-float (math-add-float guess (math-div-float a guess))
  489. '(float 5 -1))))
  490. (if (math-nearly-equal-float g2 guess)
  491. g2
  492. (math-sqrt-float-iter a g2))))
  493. ;;; True if A and B differ only in the last digit of precision. [P F F]
  494. (defun math-nearly-equal-float (a b)
  495. (let ((ediff (- (nth 2 a) (nth 2 b))))
  496. (cond ((= ediff 0) ;; Expanded out for speed
  497. (setq ediff (math-add (Math-integer-neg (nth 1 a)) (nth 1 b)))
  498. (or (eq ediff 0)
  499. (and (not (consp ediff))
  500. (< ediff 10)
  501. (> ediff -10)
  502. (= (math-numdigs (nth 1 a)) calc-internal-prec))))
  503. ((= ediff 1)
  504. (setq ediff (math-add (Math-integer-neg (nth 1 b))
  505. (math-scale-int (nth 1 a) 1)))
  506. (and (not (consp ediff))
  507. (< ediff 10)
  508. (> ediff -10)
  509. (= (math-numdigs (nth 1 b)) calc-internal-prec)))
  510. ((= ediff -1)
  511. (setq ediff (math-add (Math-integer-neg (nth 1 a))
  512. (math-scale-int (nth 1 b) 1)))
  513. (and (not (consp ediff))
  514. (< ediff 10)
  515. (> ediff -10)
  516. (= (math-numdigs (nth 1 a)) calc-internal-prec))))))
  517. (defun math-nearly-equal (a b) ; [P N N] [Public]
  518. (setq a (math-float a))
  519. (setq b (math-float b))
  520. (if (eq (car a) 'polar) (setq a (math-complex a)))
  521. (if (eq (car b) 'polar) (setq b (math-complex b)))
  522. (if (eq (car a) 'cplx)
  523. (if (eq (car b) 'cplx)
  524. (and (or (math-nearly-equal-float (nth 1 a) (nth 1 b))
  525. (and (math-nearly-zerop-float (nth 1 a) (nth 2 a))
  526. (math-nearly-zerop-float (nth 1 b) (nth 2 b))))
  527. (or (math-nearly-equal-float (nth 2 a) (nth 2 b))
  528. (and (math-nearly-zerop-float (nth 2 a) (nth 1 a))
  529. (math-nearly-zerop-float (nth 2 b) (nth 1 b)))))
  530. (and (math-nearly-equal-float (nth 1 a) b)
  531. (math-nearly-zerop-float (nth 2 a) b)))
  532. (if (eq (car b) 'cplx)
  533. (and (math-nearly-equal-float a (nth 1 b))
  534. (math-nearly-zerop-float a (nth 2 b)))
  535. (math-nearly-equal-float a b))))
  536. ;;; True if A is nearly zero compared to B. [P F F]
  537. (defun math-nearly-zerop-float (a b)
  538. (or (eq (nth 1 a) 0)
  539. (<= (+ (math-numdigs (nth 1 a)) (nth 2 a))
  540. (1+ (- (+ (math-numdigs (nth 1 b)) (nth 2 b)) calc-internal-prec)))))
  541. (defun math-nearly-zerop (a b) ; [P N R] [Public]
  542. (setq a (math-float a))
  543. (setq b (math-float b))
  544. (if (eq (car a) 'cplx)
  545. (and (math-nearly-zerop-float (nth 1 a) b)
  546. (math-nearly-zerop-float (nth 2 a) b))
  547. (if (eq (car a) 'polar)
  548. (math-nearly-zerop-float (nth 1 a) b)
  549. (math-nearly-zerop-float a b))))
  550. ;;; This implementation could be improved, accuracy-wise.
  551. (defun math-hypot (a b)
  552. (cond ((Math-zerop a) (math-abs b))
  553. ((Math-zerop b) (math-abs a))
  554. ((not (Math-scalarp a))
  555. (if (math-infinitep a)
  556. (if (math-infinitep b)
  557. (if (equal a b)
  558. a
  559. '(var nan var-nan))
  560. a)
  561. (calc-record-why 'scalarp a)
  562. (list 'calcFunc-hypot a b)))
  563. ((not (Math-scalarp b))
  564. (if (math-infinitep b)
  565. b
  566. (calc-record-why 'scalarp b)
  567. (list 'calcFunc-hypot a b)))
  568. ((and (Math-numberp a) (Math-numberp b))
  569. (math-with-extra-prec 1
  570. (math-sqrt (math-add (calcFunc-abssqr a) (calcFunc-abssqr b)))))
  571. ((eq (car-safe a) 'hms)
  572. (if (eq (car-safe b) 'hms) ; this helps sdev's of hms forms
  573. (math-to-hms (math-hypot (math-from-hms a 'deg)
  574. (math-from-hms b 'deg)))
  575. (math-to-hms (math-hypot (math-from-hms a 'deg) b))))
  576. ((eq (car-safe b) 'hms)
  577. (math-to-hms (math-hypot a (math-from-hms b 'deg))))
  578. (t nil)))
  579. (defalias 'calcFunc-hypot 'math-hypot)
  580. (defun calcFunc-sqr (x)
  581. (math-pow x 2))
  582. (defun math-nth-root (a n)
  583. (cond ((= n 2) (math-sqrt a))
  584. ((Math-zerop a) a)
  585. ((Math-negp a) nil)
  586. ((Math-integerp a)
  587. (let ((root (math-nth-root-integer a n)))
  588. (if (car root)
  589. (cdr root)
  590. (and (not calc-symbolic-mode)
  591. (math-nth-root-float (math-float a) n
  592. (math-float (cdr root)))))))
  593. ((eq (car-safe a) 'frac)
  594. (let* ((num-root (math-nth-root-integer (nth 1 a) n))
  595. (den-root (math-nth-root-integer (nth 2 a) n)))
  596. (if (and (car num-root) (car den-root))
  597. (list 'frac (cdr num-root) (cdr den-root))
  598. (and (not calc-symbolic-mode)
  599. (math-nth-root-float
  600. (math-float a) n
  601. (math-div-float (math-float (cdr num-root))
  602. (math-float (cdr den-root))))))))
  603. ((eq (car-safe a) 'float)
  604. (and (not calc-symbolic-mode)
  605. (math-nth-root-float a n)))
  606. ((eq (car-safe a) 'polar)
  607. (let ((root (math-nth-root (nth 1 a) n)))
  608. (and root (list 'polar root (math-div (nth 2 a) n)))))
  609. (t nil)))
  610. ;; The variables math-nrf-n, math-nrf-nf and math-nrf-nfm1 are local
  611. ;; to math-nth-root-float, but are used by math-nth-root-float-iter,
  612. ;; which is called by math-nth-root-float.
  613. (defvar math-nrf-n)
  614. (defvar math-nrf-nf)
  615. (defvar math-nrf-nfm1)
  616. (defun math-nth-root-float (a math-nrf-n &optional guess)
  617. (math-inexact-result)
  618. (math-with-extra-prec 1
  619. (let ((math-nrf-nf (math-float math-nrf-n))
  620. (math-nrf-nfm1 (math-float (1- math-nrf-n))))
  621. (math-nth-root-float-iter a (or guess
  622. (math-make-float
  623. 1 (/ (+ (math-numdigs (nth 1 a))
  624. (nth 2 a)
  625. (/ math-nrf-n 2))
  626. math-nrf-n)))))))
  627. (defun math-nth-root-float-iter (a guess)
  628. (math-working "root" guess)
  629. (let ((g2 (math-div-float (math-add-float (math-mul math-nrf-nfm1 guess)
  630. (math-div-float
  631. a (math-ipow guess (1- math-nrf-n))))
  632. math-nrf-nf)))
  633. (if (math-nearly-equal-float g2 guess)
  634. g2
  635. (math-nth-root-float-iter a g2))))
  636. ;; The variable math-nri-n is local to math-nth-root-integer, but
  637. ;; is used by math-nth-root-int-iter, which is called by
  638. ;; math-nth-root-int.
  639. (defvar math-nri-n)
  640. (defun math-nth-root-integer (a math-nri-n &optional guess) ; [I I S]
  641. (math-nth-root-int-iter a (or guess
  642. (math-scale-int 1 (/ (+ (math-numdigs a)
  643. (1- math-nri-n))
  644. math-nri-n)))))
  645. (defun math-nth-root-int-iter (a guess)
  646. (math-working "root" guess)
  647. (let* ((q (math-idivmod a (math-ipow guess (1- math-nri-n))))
  648. (s (math-add (car q) (math-mul (1- math-nri-n) guess)))
  649. (g2 (math-idivmod s math-nri-n)))
  650. (if (Math-natnum-lessp (car g2) guess)
  651. (math-nth-root-int-iter a (car g2))
  652. (cons (and (equal (car g2) guess)
  653. (eq (cdr q) 0)
  654. (eq (cdr g2) 0))
  655. guess))))
  656. (defun calcFunc-nroot (x n)
  657. (calcFunc-pow x (if (integerp n)
  658. (math-make-frac 1 n)
  659. (math-div 1 n))))
  660. ;;;; Transcendental functions.
  661. ;;; All of these functions are defined on the complex plane.
  662. ;;; (Branch cuts, etc. follow Steele's Common Lisp book.)
  663. ;;; Most functions increase calc-internal-prec by 2 digits, then round
  664. ;;; down afterward. "-raw" functions use the current precision, require
  665. ;;; their arguments to be in float (or complex float) format, and always
  666. ;;; work in radians (where applicable).
  667. (defun math-to-radians (a) ; [N N]
  668. (cond ((eq (car-safe a) 'hms)
  669. (math-from-hms a 'rad))
  670. ((memq calc-angle-mode '(deg hms))
  671. (math-mul a (math-pi-over-180)))
  672. (t a)))
  673. (defun math-from-radians (a) ; [N N]
  674. (cond ((eq calc-angle-mode 'deg)
  675. (if (math-constp a)
  676. (math-div a (math-pi-over-180))
  677. (list 'calcFunc-deg a)))
  678. ((eq calc-angle-mode 'hms)
  679. (math-to-hms a 'rad))
  680. (t a)))
  681. (defun math-to-radians-2 (a &optional force-symbolic) ; [N N]
  682. (cond ((eq (car-safe a) 'hms)
  683. (math-from-hms a 'rad))
  684. ((memq calc-angle-mode '(deg hms))
  685. (if (or calc-symbolic-mode force-symbolic)
  686. (math-div (math-mul a '(var pi var-pi)) 180)
  687. (math-mul a (math-pi-over-180))))
  688. (t a)))
  689. (defun math-from-radians-2 (a &optional force-symbolic) ; [N N]
  690. (cond ((memq calc-angle-mode '(deg hms))
  691. (if (or calc-symbolic-mode force-symbolic)
  692. (math-div (math-mul 180 a) '(var pi var-pi))
  693. (math-div a (math-pi-over-180))))
  694. (t a)))
  695. ;;; Sine, cosine, and tangent.
  696. (defun calcFunc-sin (x) ; [N N] [Public]
  697. (cond ((and (integerp x)
  698. (if (eq calc-angle-mode 'deg)
  699. (= (% x 90) 0)
  700. (= x 0)))
  701. (aref [0 1 0 -1] (math-mod (/ x 90) 4)))
  702. ((Math-scalarp x)
  703. (math-with-extra-prec 2
  704. (math-sin-raw (math-to-radians (math-float x)))))
  705. ((eq (car x) 'sdev)
  706. (if (math-constp x)
  707. (math-with-extra-prec 2
  708. (let* ((xx (math-to-radians (math-float (nth 1 x))))
  709. (xs (math-to-radians (math-float (nth 2 x))))
  710. (sc (math-sin-cos-raw xx)))
  711. (math-make-sdev (car sc) (math-mul xs (cdr sc)))))
  712. (math-make-sdev (calcFunc-sin (nth 1 x))
  713. (math-mul (nth 2 x) (calcFunc-cos (nth 1 x))))))
  714. ((and (eq (car x) 'intv) (math-intv-constp x))
  715. (calcFunc-cos (math-sub x (math-quarter-circle nil))))
  716. ((equal x '(var nan var-nan))
  717. x)
  718. (t (calc-record-why 'scalarp x)
  719. (list 'calcFunc-sin x))))
  720. (defun calcFunc-cos (x) ; [N N] [Public]
  721. (cond ((and (integerp x)
  722. (if (eq calc-angle-mode 'deg)
  723. (= (% x 90) 0)
  724. (= x 0)))
  725. (aref [1 0 -1 0] (math-mod (/ x 90) 4)))
  726. ((Math-scalarp x)
  727. (math-with-extra-prec 2
  728. (math-cos-raw (math-to-radians (math-float x)))))
  729. ((eq (car x) 'sdev)
  730. (if (math-constp x)
  731. (math-with-extra-prec 2
  732. (let* ((xx (math-to-radians (math-float (nth 1 x))))
  733. (xs (math-to-radians (math-float (nth 2 x))))
  734. (sc (math-sin-cos-raw xx)))
  735. (math-make-sdev (cdr sc) (math-mul xs (car sc)))))
  736. (math-make-sdev (calcFunc-cos (nth 1 x))
  737. (math-mul (nth 2 x) (calcFunc-sin (nth 1 x))))))
  738. ((and (eq (car x) 'intv) (math-intv-constp x))
  739. (math-with-extra-prec 2
  740. (let* ((xx (math-to-radians (math-float x)))
  741. (na (math-floor (math-div (nth 2 xx) (math-pi))))
  742. (nb (math-floor (math-div (nth 3 xx) (math-pi))))
  743. (span (math-sub nb na)))
  744. (if (memq span '(0 1))
  745. (let ((int (math-sort-intv (nth 1 x)
  746. (math-cos-raw (nth 2 xx))
  747. (math-cos-raw (nth 3 xx)))))
  748. (if (eq span 1)
  749. (if (math-evenp na)
  750. (math-make-intv (logior (nth 1 x) 2)
  751. -1
  752. (nth 3 int))
  753. (math-make-intv (logior (nth 1 x) 1)
  754. (nth 2 int)
  755. 1))
  756. int))
  757. (list 'intv 3 -1 1)))))
  758. ((equal x '(var nan var-nan))
  759. x)
  760. (t (calc-record-why 'scalarp x)
  761. (list 'calcFunc-cos x))))
  762. (defun calcFunc-sincos (x) ; [V N] [Public]
  763. (if (Math-scalarp x)
  764. (math-with-extra-prec 2
  765. (let ((sc (math-sin-cos-raw (math-to-radians (math-float x)))))
  766. (list 'vec (cdr sc) (car sc)))) ; the vector [cos, sin]
  767. (list 'vec (calcFunc-sin x) (calcFunc-cos x))))
  768. (defun calcFunc-tan (x) ; [N N] [Public]
  769. (cond ((and (integerp x)
  770. (if (eq calc-angle-mode 'deg)
  771. (= (% x 180) 0)
  772. (= x 0)))
  773. 0)
  774. ((Math-scalarp x)
  775. (math-with-extra-prec 2
  776. (math-tan-raw (math-to-radians (math-float x)))))
  777. ((eq (car x) 'sdev)
  778. (if (math-constp x)
  779. (math-with-extra-prec 2
  780. (let* ((xx (math-to-radians (math-float (nth 1 x))))
  781. (xs (math-to-radians (math-float (nth 2 x))))
  782. (sc (math-sin-cos-raw xx)))
  783. (if (and (math-zerop (cdr sc)) (not calc-infinite-mode))
  784. (progn
  785. (calc-record-why "*Division by zero")
  786. (list 'calcFunc-tan x))
  787. (math-make-sdev (math-div-float (car sc) (cdr sc))
  788. (math-div-float xs (math-sqr (cdr sc)))))))
  789. (math-make-sdev (calcFunc-tan (nth 1 x))
  790. (math-div (nth 2 x)
  791. (math-sqr (calcFunc-cos (nth 1 x)))))))
  792. ((and (eq (car x) 'intv) (math-intv-constp x))
  793. (or (math-with-extra-prec 2
  794. (let* ((xx (math-to-radians (math-float x)))
  795. (na (math-floor (math-div (math-sub (nth 2 xx)
  796. (math-pi-over-2))
  797. (math-pi))))
  798. (nb (math-floor (math-div (math-sub (nth 3 xx)
  799. (math-pi-over-2))
  800. (math-pi)))))
  801. (and (equal na nb)
  802. (math-sort-intv (nth 1 x)
  803. (math-tan-raw (nth 2 xx))
  804. (math-tan-raw (nth 3 xx))))))
  805. '(intv 3 (neg (var inf var-inf)) (var inf var-inf))))
  806. ((equal x '(var nan var-nan))
  807. x)
  808. (t (calc-record-why 'scalarp x)
  809. (list 'calcFunc-tan x))))
  810. (defun calcFunc-sec (x)
  811. (cond ((and (integerp x)
  812. (eq calc-angle-mode 'deg)
  813. (= (% x 180) 0))
  814. (if (= (% x 360) 0)
  815. 1
  816. -1))
  817. ((and (integerp x)
  818. (eq calc-angle-mode 'rad)
  819. (= x 0))
  820. 1)
  821. ((Math-scalarp x)
  822. (math-with-extra-prec 2
  823. (math-sec-raw (math-to-radians (math-float x)))))
  824. ((eq (car x) 'sdev)
  825. (if (math-constp x)
  826. (math-with-extra-prec 2
  827. (let* ((xx (math-to-radians (math-float (nth 1 x))))
  828. (xs (math-to-radians (math-float (nth 2 x))))
  829. (sc (math-sin-cos-raw xx)))
  830. (if (and (math-zerop (cdr sc))
  831. (not calc-infinite-mode))
  832. (progn
  833. (calc-record-why "*Division by zero")
  834. (list 'calcFunc-sec x))
  835. (math-make-sdev (math-div-float '(float 1 0) (cdr sc))
  836. (math-div-float
  837. (math-mul xs (car sc))
  838. (math-sqr (cdr sc)))))))
  839. (math-make-sdev (calcFunc-sec (nth 1 x))
  840. (math-div
  841. (math-mul (nth 2 x)
  842. (calcFunc-sin (nth 1 x)))
  843. (math-sqr (calcFunc-cos (nth 1 x)))))))
  844. ((and (eq (car x) 'intv)
  845. (math-intv-constp x))
  846. (math-with-extra-prec 2
  847. (let* ((xx (math-to-radians (math-float x)))
  848. (na (math-floor (math-div (math-sub (nth 2 xx)
  849. (math-pi-over-2))
  850. (math-pi))))
  851. (nb (math-floor (math-div (math-sub (nth 3 xx)
  852. (math-pi-over-2))
  853. (math-pi))))
  854. (naa (math-floor (math-div (nth 2 xx) (math-pi-over-2))))
  855. (nbb (math-floor (math-div (nth 3 xx) (math-pi-over-2))))
  856. (span (math-sub nbb naa)))
  857. (if (not (equal na nb))
  858. '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
  859. (let ((int (math-sort-intv (nth 1 x)
  860. (math-sec-raw (nth 2 xx))
  861. (math-sec-raw (nth 3 xx)))))
  862. (if (eq span 1)
  863. (if (math-evenp (math-div (math-add naa 1) 2))
  864. (math-make-intv (logior (nth 1 int) 2)
  865. 1
  866. (nth 3 int))
  867. (math-make-intv (logior (nth 1 int) 1)
  868. (nth 2 int)
  869. -1))
  870. int))))))
  871. ((equal x '(var nan var-nan))
  872. x)
  873. (t (calc-record-why 'scalarp x)
  874. (list 'calcFunc-sec x))))
  875. (defun calcFunc-csc (x)
  876. (cond ((and (integerp x)
  877. (eq calc-angle-mode 'deg)
  878. (= (% (- x 90) 180) 0))
  879. (if (= (% (- x 90) 360) 0)
  880. 1
  881. -1))
  882. ((Math-scalarp x)
  883. (math-with-extra-prec 2
  884. (math-csc-raw (math-to-radians (math-float x)))))
  885. ((eq (car x) 'sdev)
  886. (if (math-constp x)
  887. (math-with-extra-prec 2
  888. (let* ((xx (math-to-radians (math-float (nth 1 x))))
  889. (xs (math-to-radians (math-float (nth 2 x))))
  890. (sc (math-sin-cos-raw xx)))
  891. (if (and (math-zerop (car sc))
  892. (not calc-infinite-mode))
  893. (progn
  894. (calc-record-why "*Division by zero")
  895. (list 'calcFunc-csc x))
  896. (math-make-sdev (math-div-float '(float 1 0) (car sc))
  897. (math-div-float
  898. (math-mul xs (cdr sc))
  899. (math-sqr (car sc)))))))
  900. (math-make-sdev (calcFunc-csc (nth 1 x))
  901. (math-div
  902. (math-mul (nth 2 x)
  903. (calcFunc-cos (nth 1 x)))
  904. (math-sqr (calcFunc-sin (nth 1 x)))))))
  905. ((and (eq (car x) 'intv)
  906. (math-intv-constp x))
  907. (math-with-extra-prec 2
  908. (let* ((xx (math-to-radians (math-float x)))
  909. (na (math-floor (math-div (nth 2 xx) (math-pi))))
  910. (nb (math-floor (math-div (nth 3 xx) (math-pi))))
  911. (naa (math-floor (math-div (nth 2 xx) (math-pi-over-2))))
  912. (nbb (math-floor (math-div (nth 3 xx) (math-pi-over-2))))
  913. (span (math-sub nbb naa)))
  914. (if (not (equal na nb))
  915. '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
  916. (let ((int (math-sort-intv (nth 1 x)
  917. (math-csc-raw (nth 2 xx))
  918. (math-csc-raw (nth 3 xx)))))
  919. (if (eq span 1)
  920. (if (math-evenp (math-div naa 2))
  921. (math-make-intv (logior (nth 1 int) 2)
  922. 1
  923. (nth 3 int))
  924. (math-make-intv (logior (nth 1 int) 1)
  925. (nth 2 int)
  926. -1))
  927. int))))))
  928. ((equal x '(var nan var-nan))
  929. x)
  930. (t (calc-record-why 'scalarp x)
  931. (list 'calcFunc-csc x))))
  932. (defun calcFunc-cot (x) ; [N N] [Public]
  933. (cond ((and (integerp x)
  934. (if (eq calc-angle-mode 'deg)
  935. (= (% (- x 90) 180) 0)
  936. (= x 0)))
  937. 0)
  938. ((Math-scalarp x)
  939. (math-with-extra-prec 2
  940. (math-cot-raw (math-to-radians (math-float x)))))
  941. ((eq (car x) 'sdev)
  942. (if (math-constp x)
  943. (math-with-extra-prec 2
  944. (let* ((xx (math-to-radians (math-float (nth 1 x))))
  945. (xs (math-to-radians (math-float (nth 2 x))))
  946. (sc (math-sin-cos-raw xx)))
  947. (if (and (math-zerop (car sc)) (not calc-infinite-mode))
  948. (progn
  949. (calc-record-why "*Division by zero")
  950. (list 'calcFunc-cot x))
  951. (math-make-sdev (math-div-float (cdr sc) (car sc))
  952. (math-div-float xs (math-sqr (car sc)))))))
  953. (math-make-sdev (calcFunc-cot (nth 1 x))
  954. (math-div (nth 2 x)
  955. (math-sqr (calcFunc-sin (nth 1 x)))))))
  956. ((and (eq (car x) 'intv) (math-intv-constp x))
  957. (or (math-with-extra-prec 2
  958. (let* ((xx (math-to-radians (math-float x)))
  959. (na (math-floor (math-div (nth 2 xx) (math-pi))))
  960. (nb (math-floor (math-div (nth 3 xx) (math-pi)))))
  961. (and (equal na nb)
  962. (math-sort-intv (nth 1 x)
  963. (math-cot-raw (nth 2 xx))
  964. (math-cot-raw (nth 3 xx))))))
  965. '(intv 3 (neg (var inf var-inf)) (var inf var-inf))))
  966. ((equal x '(var nan var-nan))
  967. x)
  968. (t (calc-record-why 'scalarp x)
  969. (list 'calcFunc-cot x))))
  970. (defun math-sin-raw (x &optional orgx) ; [N N]
  971. (cond ((eq (car x) 'cplx)
  972. (let* ((expx (math-exp-raw (nth 2 x)))
  973. (expmx (math-div-float '(float 1 0) expx))
  974. (sc (math-sin-cos-raw (nth 1 x))))
  975. (list 'cplx
  976. (math-mul-float (car sc)
  977. (math-mul-float (math-add-float expx expmx)
  978. '(float 5 -1)))
  979. (math-mul-float (cdr sc)
  980. (math-mul-float (math-sub-float expx expmx)
  981. '(float 5 -1))))))
  982. ((eq (car x) 'polar)
  983. (math-polar (math-sin-raw (math-complex x))))
  984. ((Math-integer-negp (nth 1 x))
  985. (math-neg-float (math-sin-raw (math-neg-float x) (if orgx orgx x))))
  986. ((math-lessp-float '(float 7 0) x) ; avoid inf loops due to roundoff
  987. (math-sin-raw (math-mod x (math-two-pi)) (if orgx orgx x)))
  988. (t (math-sin-raw-2 x (if orgx orgx x)))))
  989. (defun math-cos-raw (x) ; [N N]
  990. (if (eq (car-safe x) 'polar)
  991. (math-polar (math-cos-raw (math-complex x)))
  992. (math-sin-raw (math-sub (math-pi-over-2) x) x)))
  993. (defun math-sec-raw (x) ; [N N]
  994. (cond ((eq (car x) 'cplx)
  995. (let* ((x (math-mul x '(float 1 0)))
  996. (expx (math-exp-raw (nth 2 x)))
  997. (expmx (math-div-float '(float 1 0) expx))
  998. (sh (math-mul-float (math-sub-float expx expmx) '(float 5 -1)))
  999. (ch (math-mul-float (math-add-float expx expmx) '(float 5 -1)))
  1000. (sc (math-sin-cos-raw (nth 1 x)))
  1001. (d (math-add-float
  1002. (math-mul-float (math-sqr (car sc))
  1003. (math-sqr sh))
  1004. (math-mul-float (math-sqr (cdr sc))
  1005. (math-sqr ch)))))
  1006. (and (not (eq (nth 1 d) 0))
  1007. (list 'cplx
  1008. (math-div-float (math-mul-float (cdr sc) ch) d)
  1009. (math-div-float (math-mul-float (car sc) sh) d)))))
  1010. ((eq (car x) 'polar)
  1011. (math-polar (math-sec-raw (math-complex x))))
  1012. (t
  1013. (let ((cs (math-cos-raw x)))
  1014. (if (eq cs 0)
  1015. (math-div 1 0)
  1016. (math-div-float '(float 1 0) cs))))))
  1017. (defun math-csc-raw (x) ; [N N]
  1018. (cond ((eq (car x) 'cplx)
  1019. (let* ((x (math-mul x '(float 1 0)))
  1020. (expx (math-exp-raw (nth 2 x)))
  1021. (expmx (math-div-float '(float 1 0) expx))
  1022. (sh (math-mul-float (math-sub-float expx expmx) '(float 5 -1)))
  1023. (ch (math-mul-float (math-add-float expx expmx) '(float 5 -1)))
  1024. (sc (math-sin-cos-raw (nth 1 x)))
  1025. (d (math-add-float
  1026. (math-mul-float (math-sqr (car sc))
  1027. (math-sqr ch))
  1028. (math-mul-float (math-sqr (cdr sc))
  1029. (math-sqr sh)))))
  1030. (and (not (eq (nth 1 d) 0))
  1031. (list 'cplx
  1032. (math-div-float (math-mul-float (car sc) ch) d)
  1033. (math-div-float (math-mul-float (cdr sc) sh) d)))))
  1034. ((eq (car x) 'polar)
  1035. (math-polar (math-csc-raw (math-complex x))))
  1036. (t
  1037. (let ((sn (math-sin-raw x)))
  1038. (if (eq sn 0)
  1039. (math-div 1 0)
  1040. (math-div-float '(float 1 0) sn))))))
  1041. (defun math-cot-raw (x) ; [N N]
  1042. (cond ((eq (car x) 'cplx)
  1043. (let* ((x (math-mul x '(float 1 0)))
  1044. (expx (math-exp-raw (nth 2 x)))
  1045. (expmx (math-div-float '(float 1 0) expx))
  1046. (sh (math-mul-float (math-sub-float expx expmx) '(float 5 -1)))
  1047. (ch (math-mul-float (math-add-float expx expmx) '(float 5 -1)))
  1048. (sc (math-sin-cos-raw (nth 1 x)))
  1049. (d (math-add-float
  1050. (math-sqr (car sc))
  1051. (math-sqr sh))))
  1052. (and (not (eq (nth 1 d) 0))
  1053. (list 'cplx
  1054. (math-div-float
  1055. (math-mul-float (car sc) (cdr sc))
  1056. d)
  1057. (math-neg
  1058. (math-div-float
  1059. (math-mul-float sh ch)
  1060. d))))))
  1061. ((eq (car x) 'polar)
  1062. (math-polar (math-cot-raw (math-complex x))))
  1063. (t
  1064. (let ((sc (math-sin-cos-raw x)))
  1065. (if (eq (nth 1 (car sc)) 0)
  1066. (math-div (cdr sc) 0)
  1067. (math-div-float (cdr sc) (car sc)))))))
  1068. ;;; This could use a smarter method: Reduce x as in math-sin-raw, then
  1069. ;;; compute either sin(x) or cos(x), whichever is smaller, and compute
  1070. ;;; the other using the identity sin(x)^2 + cos(x)^2 = 1.
  1071. (defun math-sin-cos-raw (x) ; [F.F F] (result is (sin x . cos x))
  1072. (cons (math-sin-raw x) (math-cos-raw x)))
  1073. (defun math-tan-raw (x) ; [N N]
  1074. (cond ((eq (car x) 'cplx)
  1075. (let* ((x (math-mul x '(float 2 0)))
  1076. (expx (math-exp-raw (nth 2 x)))
  1077. (expmx (math-div-float '(float 1 0) expx))
  1078. (sc (math-sin-cos-raw (nth 1 x)))
  1079. (d (math-add-float (cdr sc)
  1080. (math-mul-float (math-add-float expx expmx)
  1081. '(float 5 -1)))))
  1082. (and (not (eq (nth 1 d) 0))
  1083. (list 'cplx
  1084. (math-div-float (car sc) d)
  1085. (math-div-float (math-mul-float (math-sub-float expx
  1086. expmx)
  1087. '(float 5 -1)) d)))))
  1088. ((eq (car x) 'polar)
  1089. (math-polar (math-tan-raw (math-complex x))))
  1090. (t
  1091. (let ((sc (math-sin-cos-raw x)))
  1092. (if (eq (nth 1 (cdr sc)) 0)
  1093. (math-div (car sc) 0)
  1094. (math-div-float (car sc) (cdr sc)))))))
  1095. (defun math-sin-raw-2 (x orgx) ; This avoids poss of inf recursion. [F F]
  1096. (let ((xmpo2 (math-sub-float (math-pi-over-2) x)))
  1097. (cond ((Math-integer-negp (nth 1 xmpo2))
  1098. (math-neg-float (math-sin-raw-2 (math-sub-float x (math-pi))
  1099. orgx)))
  1100. ((math-lessp-float (math-pi-over-4) x)
  1101. (math-cos-raw-2 xmpo2 orgx))
  1102. ((math-lessp-float x (math-neg (math-pi-over-4)))
  1103. (math-neg (math-cos-raw-2 (math-add (math-pi-over-2) x) orgx)))
  1104. ((math-with-extra-prec -1 (math-nearly-zerop-float x orgx))
  1105. '(float 0 0))
  1106. ((math-use-emacs-fn 'sin x))
  1107. (calc-symbolic-mode (signal 'inexact-result nil))
  1108. (t (math-sin-series x 6 4 x (math-neg-float (math-sqr-float x)))))))
  1109. (defun math-cos-raw-2 (x orgx) ; [F F]
  1110. (cond ((math-with-extra-prec -1 (math-nearly-zerop-float x orgx))
  1111. '(float 1 0))
  1112. ((math-use-emacs-fn 'cos x))
  1113. (calc-symbolic-mode (signal 'inexact-result nil))
  1114. (t (let ((xnegsqr (math-neg-float (math-sqr-float x))))
  1115. (math-sin-series
  1116. (math-add-float '(float 1 0)
  1117. (math-mul-float xnegsqr '(float 5 -1)))
  1118. 24 5 xnegsqr xnegsqr)))))
  1119. (defun math-sin-series (sum nfac n x xnegsqr)
  1120. (math-working "sin" sum)
  1121. (let* ((nextx (math-mul-float x xnegsqr))
  1122. (nextsum (math-add-float sum (math-div-float nextx
  1123. (math-float nfac)))))
  1124. (if (math-nearly-equal-float sum nextsum)
  1125. sum
  1126. (math-sin-series nextsum (math-mul nfac (* n (1+ n)))
  1127. (+ n 2) nextx xnegsqr))))
  1128. ;;; Inverse sine, cosine, tangent.
  1129. (defun calcFunc-arcsin (x) ; [N N] [Public]
  1130. (cond ((eq x 0) 0)
  1131. ((and (eq x 1) (eq calc-angle-mode 'deg)) 90)
  1132. ((and (eq x -1) (eq calc-angle-mode 'deg)) -90)
  1133. (calc-symbolic-mode (signal 'inexact-result nil))
  1134. ((Math-numberp x)
  1135. (math-with-extra-prec 2
  1136. (math-from-radians (math-arcsin-raw (math-float x)))))
  1137. ((eq (car x) 'sdev)
  1138. (math-make-sdev (calcFunc-arcsin (nth 1 x))
  1139. (math-from-radians
  1140. (math-div (nth 2 x)
  1141. (math-sqrt
  1142. (math-sub 1 (math-sqr (nth 1 x))))))))
  1143. ((eq (car x) 'intv)
  1144. (math-sort-intv (nth 1 x)
  1145. (calcFunc-arcsin (nth 2 x))
  1146. (calcFunc-arcsin (nth 3 x))))
  1147. ((equal x '(var nan var-nan))
  1148. x)
  1149. (t (calc-record-why 'numberp x)
  1150. (list 'calcFunc-arcsin x))))
  1151. (defun calcFunc-arccos (x) ; [N N] [Public]
  1152. (cond ((eq x 1) 0)
  1153. ((and (eq x 0) (eq calc-angle-mode 'deg)) 90)
  1154. ((and (eq x -1) (eq calc-angle-mode 'deg)) 180)
  1155. (calc-symbolic-mode (signal 'inexact-result nil))
  1156. ((Math-numberp x)
  1157. (math-with-extra-prec 2
  1158. (math-from-radians (math-arccos-raw (math-float x)))))
  1159. ((eq (car x) 'sdev)
  1160. (math-make-sdev (calcFunc-arccos (nth 1 x))
  1161. (math-from-radians
  1162. (math-div (nth 2 x)
  1163. (math-sqrt
  1164. (math-sub 1 (math-sqr (nth 1 x))))))))
  1165. ((eq (car x) 'intv)
  1166. (math-sort-intv (nth 1 x)
  1167. (calcFunc-arccos (nth 2 x))
  1168. (calcFunc-arccos (nth 3 x))))
  1169. ((equal x '(var nan var-nan))
  1170. x)
  1171. (t (calc-record-why 'numberp x)
  1172. (list 'calcFunc-arccos x))))
  1173. (defun calcFunc-arctan (x) ; [N N] [Public]
  1174. (cond ((eq x 0) 0)
  1175. ((and (eq x 1) (eq calc-angle-mode 'deg)) 45)
  1176. ((and (eq x -1) (eq calc-angle-mode 'deg)) -45)
  1177. ((Math-numberp x)
  1178. (math-with-extra-prec 2
  1179. (math-from-radians (math-arctan-raw (math-float x)))))
  1180. ((eq (car x) 'sdev)
  1181. (math-make-sdev (calcFunc-arctan (nth 1 x))
  1182. (math-from-radians
  1183. (math-div (nth 2 x)
  1184. (math-add 1 (math-sqr (nth 1 x)))))))
  1185. ((eq (car x) 'intv)
  1186. (math-sort-intv (nth 1 x)
  1187. (calcFunc-arctan (nth 2 x))
  1188. (calcFunc-arctan (nth 3 x))))
  1189. ((equal x '(var inf var-inf))
  1190. (math-quarter-circle t))
  1191. ((equal x '(neg (var inf var-inf)))
  1192. (math-neg (math-quarter-circle t)))
  1193. ((equal x '(var nan var-nan))
  1194. x)
  1195. (t (calc-record-why 'numberp x)
  1196. (list 'calcFunc-arctan x))))
  1197. (defun math-arcsin-raw (x) ; [N N]
  1198. (let ((a (math-sqrt-raw (math-sub '(float 1 0) (math-sqr x)))))
  1199. (if (or (memq (car x) '(cplx polar))
  1200. (memq (car a) '(cplx polar)))
  1201. (math-with-extra-prec 2 ; use extra precision for difficult case
  1202. (math-mul '(cplx 0 -1)
  1203. (math-ln-raw (math-add (math-mul '(cplx 0 1) x) a))))
  1204. (math-arctan2-raw x a))))
  1205. (defun math-arccos-raw (x) ; [N N]
  1206. (math-sub (math-pi-over-2) (math-arcsin-raw x)))
  1207. (defun math-arctan-raw (x) ; [N N]
  1208. (cond ((memq (car x) '(cplx polar))
  1209. (math-with-extra-prec 2 ; extra-extra
  1210. (math-div (math-sub
  1211. (math-ln-raw (math-add 1 (math-mul '(cplx 0 1) x)))
  1212. (math-ln-raw (math-add 1 (math-mul '(cplx 0 -1) x))))
  1213. '(cplx 0 2))))
  1214. ((Math-integer-negp (nth 1 x))
  1215. (math-neg-float (math-arctan-raw (math-neg-float x))))
  1216. ((math-zerop x) x)
  1217. ((math-use-emacs-fn 'atan x))
  1218. (calc-symbolic-mode (signal 'inexact-result nil))
  1219. ((math-equal-int x 1) (math-pi-over-4))
  1220. ((math-equal-int x -1) (math-neg (math-pi-over-4)))
  1221. ((math-lessp-float '(float 414214 -6) x) ; if x > sqrt(2) - 1, reduce
  1222. (if (math-lessp-float '(float 1 0) x)
  1223. (math-sub-float (math-mul-float (math-pi) '(float 5 -1))
  1224. (math-arctan-raw (math-div-float '(float 1 0) x)))
  1225. (math-sub-float (math-mul-float (math-pi) '(float 25 -2))
  1226. (math-arctan-raw (math-div-float
  1227. (math-sub-float '(float 1 0) x)
  1228. (math-add-float '(float 1 0)
  1229. x))))))
  1230. (t (math-arctan-series x 3 x (math-neg-float (math-sqr-float x))))))
  1231. (defun math-arctan-series (sum n x xnegsqr)
  1232. (math-working "arctan" sum)
  1233. (let* ((nextx (math-mul-float x xnegsqr))
  1234. (nextsum (math-add-float sum (math-div-float nextx (math-float n)))))
  1235. (if (math-nearly-equal-float sum nextsum)
  1236. sum
  1237. (math-arctan-series nextsum (+ n 2) nextx xnegsqr))))
  1238. (defun calcFunc-arctan2 (y x) ; [F R R] [Public]
  1239. (if (Math-anglep y)
  1240. (if (Math-anglep x)
  1241. (math-with-extra-prec 2
  1242. (math-from-radians (math-arctan2-raw (math-float y)
  1243. (math-float x))))
  1244. (calc-record-why 'anglep x)
  1245. (list 'calcFunc-arctan2 y x))
  1246. (if (and (or (math-infinitep x) (math-anglep x))
  1247. (or (math-infinitep y) (math-anglep y)))
  1248. (progn
  1249. (if (math-posp x)
  1250. (setq x 1)
  1251. (if (math-negp x)
  1252. (setq x -1)
  1253. (or (math-zerop x)
  1254. (setq x nil))))
  1255. (if (math-posp y)
  1256. (setq y 1)
  1257. (if (math-negp y)
  1258. (setq y -1)
  1259. (or (math-zerop y)
  1260. (setq y nil))))
  1261. (if (and y x)
  1262. (calcFunc-arctan2 y x)
  1263. '(var nan var-nan)))
  1264. (calc-record-why 'anglep y)
  1265. (list 'calcFunc-arctan2 y x))))
  1266. (defun math-arctan2-raw (y x) ; [F R R]
  1267. (cond ((math-zerop y)
  1268. (if (math-negp x) (math-pi)
  1269. (if (or (math-floatp x) (math-floatp y)) '(float 0 0) 0)))
  1270. ((math-zerop x)
  1271. (if (math-posp y)
  1272. (math-pi-over-2)
  1273. (math-neg (math-pi-over-2))))
  1274. ((math-posp x)
  1275. (math-arctan-raw (math-div-float y x)))
  1276. ((math-posp y)
  1277. (math-add-float (math-arctan-raw (math-div-float y x))
  1278. (math-pi)))
  1279. (t
  1280. (math-sub-float (math-arctan-raw (math-div-float y x))
  1281. (math-pi)))))
  1282. (defun calcFunc-arcsincos (x) ; [V N] [Public]
  1283. (if (and (Math-vectorp x)
  1284. (= (length x) 3))
  1285. (calcFunc-arctan2 (nth 2 x) (nth 1 x))
  1286. (math-reject-arg x "*Two-element vector expected")))
  1287. ;;; Exponential function.
  1288. (defun calcFunc-exp (x) ; [N N] [Public]
  1289. (cond ((eq x 0) 1)
  1290. ((and (memq x '(1 -1)) calc-symbolic-mode)
  1291. (if (eq x 1) '(var e var-e) (math-div 1 '(var e var-e))))
  1292. ((Math-numberp x)
  1293. (math-with-extra-prec 2 (math-exp-raw (math-float x))))
  1294. ((eq (car-safe x) 'sdev)
  1295. (let ((ex (calcFunc-exp (nth 1 x))))
  1296. (math-make-sdev ex (math-mul (nth 2 x) ex))))
  1297. ((eq (car-safe x) 'intv)
  1298. (math-make-intv (nth 1 x) (calcFunc-exp (nth 2 x))
  1299. (calcFunc-exp (nth 3 x))))
  1300. ((equal x '(var inf var-inf))
  1301. x)
  1302. ((equal x '(neg (var inf var-inf)))
  1303. 0)
  1304. ((equal x '(var nan var-nan))
  1305. x)
  1306. (t (calc-record-why 'numberp x)
  1307. (list 'calcFunc-exp x))))
  1308. (defun calcFunc-expm1 (x) ; [N N] [Public]
  1309. (cond ((eq x 0) 0)
  1310. ((math-zerop x) '(float 0 0))
  1311. (calc-symbolic-mode (signal 'inexact-result nil))
  1312. ((Math-numberp x)
  1313. (math-with-extra-prec 2
  1314. (let ((x (math-float x)))
  1315. (if (and (eq (car x) 'float)
  1316. (math-lessp-float x '(float 1 0))
  1317. (math-lessp-float '(float -1 0) x))
  1318. (math-exp-minus-1-raw x)
  1319. (math-add (math-exp-raw x) -1)))))
  1320. ((eq (car-safe x) 'sdev)
  1321. (if (math-constp x)
  1322. (let ((ex (calcFunc-expm1 (nth 1 x))))
  1323. (math-make-sdev ex (math-mul (nth 2 x) (math-add ex 1))))
  1324. (math-make-sdev (calcFunc-expm1 (nth 1 x))
  1325. (math-mul (nth 2 x) (calcFunc-exp (nth 1 x))))))
  1326. ((eq (car-safe x) 'intv)
  1327. (math-make-intv (nth 1 x)
  1328. (calcFunc-expm1 (nth 2 x))
  1329. (calcFunc-expm1 (nth 3 x))))
  1330. ((equal x '(var inf var-inf))
  1331. x)
  1332. ((equal x '(neg (var inf var-inf)))
  1333. -1)
  1334. ((equal x '(var nan var-nan))
  1335. x)
  1336. (t (calc-record-why 'numberp x)
  1337. (list 'calcFunc-expm1 x))))
  1338. (defun calcFunc-exp10 (x) ; [N N] [Public]
  1339. (if (eq x 0)
  1340. 1
  1341. (math-pow '(float 1 1) x)))
  1342. (defun math-exp-raw (x) ; [N N]
  1343. (cond ((math-zerop x) '(float 1 0))
  1344. (calc-symbolic-mode (signal 'inexact-result nil))
  1345. ((eq (car x) 'cplx)
  1346. (let ((expx (math-exp-raw (nth 1 x)))
  1347. (sc (math-sin-cos-raw (nth 2 x))))
  1348. (list 'cplx
  1349. (math-mul-float expx (cdr sc))
  1350. (math-mul-float expx (car sc)))))
  1351. ((eq (car x) 'polar)
  1352. (let ((xc (math-complex x)))
  1353. (list 'polar
  1354. (math-exp-raw (nth 1 xc))
  1355. (math-from-radians (nth 2 xc)))))
  1356. ((math-use-emacs-fn 'exp x))
  1357. ((or (math-lessp-float '(float 5 -1) x)
  1358. (math-lessp-float x '(float -5 -1)))
  1359. (if (math-lessp-float '(float 921035 1) x)
  1360. (math-overflow)
  1361. (if (math-lessp-float x '(float -921035 1))
  1362. (math-underflow)))
  1363. (let* ((two-x (math-mul-float x '(float 2 0)))
  1364. (hint (math-scale-int (nth 1 two-x) (nth 2 two-x)))
  1365. (hfrac (math-sub-float x (math-mul-float (math-float hint)
  1366. '(float 5 -1)))))
  1367. (math-mul-float (math-ipow (math-sqrt-e) hint)
  1368. (math-add-float '(float 1 0)
  1369. (math-exp-minus-1-raw hfrac)))))
  1370. (t (math-add-float '(float 1 0) (math-exp-minus-1-raw x)))))
  1371. (defun math-exp-minus-1-raw (x) ; [F F]
  1372. (math-exp-series x 2 3 x x))
  1373. (defun math-exp-series (sum nfac n xpow x)
  1374. (math-working "exp" sum)
  1375. (let* ((nextx (math-mul-float xpow x))
  1376. (nextsum (math-add-float sum (math-div-float nextx
  1377. (math-float nfac)))))
  1378. (if (math-nearly-equal-float sum nextsum)
  1379. sum
  1380. (math-exp-series nextsum (math-mul nfac n) (1+ n) nextx x))))
  1381. ;;; Logarithms.
  1382. (defun calcFunc-ln (x) ; [N N] [Public]
  1383. (cond ((math-zerop x)
  1384. (if calc-infinite-mode
  1385. '(neg (var inf var-inf))
  1386. (math-reject-arg x "*Logarithm of zero")))
  1387. ((eq x 1) 0)
  1388. ((Math-numberp x)
  1389. (math-with-extra-prec 2 (math-ln-raw (math-float x))))
  1390. ((eq (car-safe x) 'sdev)
  1391. (math-make-sdev (calcFunc-ln (nth 1 x))
  1392. (math-div (nth 2 x) (nth 1 x))))
  1393. ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x))
  1394. (Math-zerop (nth 2 x))
  1395. (not (math-intv-constp x))))
  1396. (let ((calc-infinite-mode t))
  1397. (math-make-intv (nth 1 x) (calcFunc-ln (nth 2 x))
  1398. (calcFunc-ln (nth 3 x)))))
  1399. ((equal x '(var e var-e))
  1400. 1)
  1401. ((and (eq (car-safe x) '^)
  1402. (equal (nth 1 x) '(var e var-e))
  1403. (math-known-realp (nth 2 x)))
  1404. (nth 2 x))
  1405. ((math-infinitep x)
  1406. (if (equal x '(var nan var-nan))
  1407. x
  1408. '(var inf var-inf)))
  1409. (t (calc-record-why 'numberp x)
  1410. (list 'calcFunc-ln x))))
  1411. (defun calcFunc-log10 (x) ; [N N] [Public]
  1412. (cond ((math-equal-int x 1)
  1413. (if (math-floatp x) '(float 0 0) 0))
  1414. ((and (Math-integerp x)
  1415. (math-posp x)
  1416. (let ((res (math-integer-log x 10)))
  1417. (and (car res)
  1418. (setq x (cdr res)))))
  1419. x)
  1420. ((and (eq (car-safe x) 'frac)
  1421. (eq (nth 1 x) 1)
  1422. (let ((res (math-integer-log (nth 2 x) 10)))
  1423. (and (car res)
  1424. (setq x (- (cdr res))))))
  1425. x)
  1426. ((math-zerop x)
  1427. (if calc-infinite-mode
  1428. '(neg (var inf var-inf))
  1429. (math-reject-arg x "*Logarithm of zero")))
  1430. (calc-symbolic-mode (signal 'inexact-result nil))
  1431. ((Math-numberp x)
  1432. (math-with-extra-prec 2
  1433. (let ((xf (math-float x)))
  1434. (if (eq (nth 1 xf) 0)
  1435. (math-reject-arg x "*Logarithm of zero"))
  1436. (if (Math-integer-posp (nth 1 xf))
  1437. (if (eq (nth 1 xf) 1) ; log10(1*10^n) = n
  1438. (math-float (nth 2 xf))
  1439. (let ((xdigs (1- (math-numdigs (nth 1 xf)))))
  1440. (math-add-float
  1441. (math-div-float (math-ln-raw-2
  1442. (list 'float (nth 1 xf) (- xdigs)))
  1443. (math-ln-10))
  1444. (math-float (+ (nth 2 xf) xdigs)))))
  1445. (math-div (calcFunc-ln xf) (math-ln-10))))))
  1446. ((eq (car-safe x) 'sdev)
  1447. (math-make-sdev (calcFunc-log10 (nth 1 x))
  1448. (math-div (nth 2 x)
  1449. (math-mul (nth 1 x) (math-ln-10)))))
  1450. ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x))
  1451. (not (math-intv-constp x))))
  1452. (math-make-intv (nth 1 x)
  1453. (calcFunc-log10 (nth 2 x))
  1454. (calcFunc-log10 (nth 3 x))))
  1455. ((math-infinitep x)
  1456. (if (equal x '(var nan var-nan))
  1457. x
  1458. '(var inf var-inf)))
  1459. (t (calc-record-why 'numberp x)
  1460. (list 'calcFunc-log10 x))))
  1461. (defun calcFunc-log (x &optional b) ; [N N N] [Public]
  1462. (cond ((or (null b) (equal b '(var e var-e)))
  1463. (math-normalize (list 'calcFunc-ln x)))
  1464. ((or (eq b 10) (equal b '(float 1 1)))
  1465. (math-normalize (list 'calcFunc-log10 x)))
  1466. ((math-zerop x)
  1467. (if calc-infinite-mode
  1468. (math-div (calcFunc-ln x) (calcFunc-ln b))
  1469. (math-reject-arg x "*Logarithm of zero")))
  1470. ((math-zerop b)
  1471. (if calc-infinite-mode
  1472. (math-div (calcFunc-ln x) (calcFunc-ln b))
  1473. (math-reject-arg b "*Logarithm of zero")))
  1474. ((math-equal-int b 1)
  1475. (if calc-infinite-mode
  1476. (math-div (calcFunc-ln x) 0)
  1477. (math-reject-arg b "*Logarithm base one")))
  1478. ((math-equal-int x 1)
  1479. (if (math-floatp b) '(float 0 0) 0))
  1480. ((and (Math-ratp x) (Math-ratp b)
  1481. (math-posp x) (math-posp b)
  1482. (let* ((sign 1) (inv nil)
  1483. (xx (if (Math-lessp 1 x)
  1484. x
  1485. (setq sign -1)
  1486. (math-div 1 x)))
  1487. (bb (if (Math-lessp 1 b)
  1488. b
  1489. (setq sign (- sign))
  1490. (math-div 1 b)))
  1491. (res (if (Math-lessp xx bb)
  1492. (setq inv (math-integer-log bb xx))
  1493. (math-integer-log xx bb))))
  1494. (and (car res)
  1495. (setq x (if inv
  1496. (math-div 1 (* sign (cdr res)))
  1497. (* sign (cdr res)))))))
  1498. x)
  1499. (calc-symbolic-mode (signal 'inexact-result nil))
  1500. ((and (Math-numberp x) (Math-numberp b))
  1501. (math-with-extra-prec 2
  1502. (math-div (math-ln-raw (math-float x))
  1503. (math-log-base-raw b))))
  1504. ((and (eq (car-safe x) 'sdev)
  1505. (Math-numberp b))
  1506. (math-make-sdev (calcFunc-log (nth 1 x) b)
  1507. (math-div (nth 2 x)
  1508. (math-mul (nth 1 x)
  1509. (math-log-base-raw b)))))
  1510. ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x))
  1511. (not (math-intv-constp x)))
  1512. (math-realp b))
  1513. (math-make-intv (nth 1 x)
  1514. (calcFunc-log (nth 2 x) b)
  1515. (calcFunc-log (nth 3 x) b)))
  1516. ((or (eq (car-safe x) 'intv) (eq (car-safe b) 'intv))
  1517. (math-div (calcFunc-ln x) (calcFunc-ln b)))
  1518. ((or (math-infinitep x)
  1519. (math-infinitep b))
  1520. (math-div (calcFunc-ln x) (calcFunc-ln b)))
  1521. (t (if (Math-numberp b)
  1522. (calc-record-why 'numberp x)
  1523. (calc-record-why 'numberp b))
  1524. (list 'calcFunc-log x b))))
  1525. (defun calcFunc-alog (x &optional b)
  1526. (cond ((or (null b) (equal b '(var e var-e)))
  1527. (math-normalize (list 'calcFunc-exp x)))
  1528. (t (math-pow b x))))
  1529. (defun calcFunc-ilog (x b)
  1530. (if (and (math-natnump x) (not (eq x 0))
  1531. (math-natnump b) (not (eq b 0)))
  1532. (if (eq b 1)
  1533. (math-reject-arg x "*Logarithm base one")
  1534. (if (Math-natnum-lessp x b)
  1535. 0
  1536. (cdr (math-integer-log x b))))
  1537. (math-floor (calcFunc-log x b))))
  1538. (defun math-integer-log (x b)
  1539. (let ((pows (list b))
  1540. (pow (math-sqr b))
  1541. next
  1542. sum n)
  1543. (while (not (Math-lessp x pow))
  1544. (setq pows (cons pow pows)
  1545. pow (math-sqr pow)))
  1546. (setq n (lsh 1 (1- (length pows)))
  1547. sum n
  1548. pow (car pows))
  1549. (while (and (setq pows (cdr pows))
  1550. (Math-lessp pow x))
  1551. (setq n (/ n 2)
  1552. next (math-mul pow (car pows)))
  1553. (or (Math-lessp x next)
  1554. (setq pow next
  1555. sum (+ sum n))))
  1556. (cons (equal pow x) sum)))
  1557. (defvar math-log-base-cache nil)
  1558. (defun math-log-base-raw (b) ; [N N]
  1559. (if (not (and (equal (car math-log-base-cache) b)
  1560. (eq (nth 1 math-log-base-cache) calc-internal-prec)))
  1561. (setq math-log-base-cache (list b calc-internal-prec
  1562. (math-ln-raw (math-float b)))))
  1563. (nth 2 math-log-base-cache))
  1564. (defun calcFunc-lnp1 (x) ; [N N] [Public]
  1565. (cond ((Math-equal-int x -1)
  1566. (if calc-infinite-mode
  1567. '(neg (var inf var-inf))
  1568. (math-reject-arg x "*Logarithm of zero")))
  1569. ((eq x 0) 0)
  1570. ((math-zerop x) '(float 0 0))
  1571. (calc-symbolic-mode (signal 'inexact-result nil))
  1572. ((Math-numberp x)
  1573. (math-with-extra-prec 2
  1574. (let ((x (math-float x)))
  1575. (if (and (eq (car x) 'float)
  1576. (math-lessp-float x '(float 5 -1))
  1577. (math-lessp-float '(float -5 -1) x))
  1578. (math-ln-plus-1-raw x)
  1579. (math-ln-raw (math-add-float x '(float 1 0)))))))
  1580. ((eq (car-safe x) 'sdev)
  1581. (math-make-sdev (calcFunc-lnp1 (nth 1 x))
  1582. (math-div (nth 2 x) (math-add (nth 1 x) 1))))
  1583. ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x))
  1584. (not (math-intv-constp x))))
  1585. (math-make-intv (nth 1 x)
  1586. (calcFunc-lnp1 (nth 2 x))
  1587. (calcFunc-lnp1 (nth 3 x))))
  1588. ((math-infinitep x)
  1589. (if (equal x '(var nan var-nan))
  1590. x
  1591. '(var inf var-inf)))
  1592. (t (calc-record-why 'numberp x)
  1593. (list 'calcFunc-lnp1 x))))
  1594. (defun math-ln-raw (x) ; [N N] --- must be float format!
  1595. (cond ((eq (car-safe x) 'cplx)
  1596. (list 'cplx
  1597. (math-mul-float (math-ln-raw
  1598. (math-add-float (math-sqr-float (nth 1 x))
  1599. (math-sqr-float (nth 2 x))))
  1600. '(float 5 -1))
  1601. (math-arctan2-raw (nth 2 x) (nth 1 x))))
  1602. ((eq (car x) 'polar)
  1603. (math-polar (list 'cplx
  1604. (math-ln-raw (nth 1 x))
  1605. (math-to-radians (nth 2 x)))))
  1606. ((Math-equal-int x 1)
  1607. '(float 0 0))
  1608. (calc-symbolic-mode (signal 'inexact-result nil))
  1609. ((math-posp (nth 1 x)) ; positive and real
  1610. (cond
  1611. ((math-use-emacs-fn 'log x))
  1612. (t
  1613. (let ((xdigs (1- (math-numdigs (nth 1 x)))))
  1614. (math-add-float (math-ln-raw-2 (list 'float (nth 1 x) (- xdigs)))
  1615. (math-mul-float (math-float (+ (nth 2 x) xdigs))
  1616. (math-ln-10)))))))
  1617. ((math-zerop x)
  1618. (math-reject-arg x "*Logarithm of zero"))
  1619. ((eq calc-complex-mode 'polar) ; negative and real
  1620. (math-polar
  1621. (list 'cplx ; negative and real
  1622. (math-ln-raw (math-neg-float x))
  1623. (math-pi))))
  1624. (t (list 'cplx ; negative and real
  1625. (math-ln-raw (math-neg-float x))
  1626. (math-pi)))))
  1627. (defun math-ln-raw-2 (x) ; [F F]
  1628. (cond ((math-lessp-float '(float 14 -1) x)
  1629. (math-add-float (math-ln-raw-2 (math-mul-float x '(float 5 -1)))
  1630. (math-ln-2)))
  1631. (t ; now .7 < x <= 1.4
  1632. (math-ln-raw-3 (math-div-float (math-sub-float x '(float 1 0))
  1633. (math-add-float x '(float 1 0)))))))
  1634. (defun math-ln-raw-3 (x) ; [F F]
  1635. (math-mul-float (math-ln-raw-series x 3 x (math-sqr-float x))
  1636. '(float 2 0)))
  1637. ;;; Compute ln((1+x)/(1-x))
  1638. (defun math-ln-raw-series (sum n x xsqr)
  1639. (math-working "log" sum)
  1640. (let* ((nextx (math-mul-float x xsqr))
  1641. (nextsum (math-add-float sum (math-div-float nextx (math-float n)))))
  1642. (if (math-nearly-equal-float sum nextsum)
  1643. sum
  1644. (math-ln-raw-series nextsum (+ n 2) nextx xsqr))))
  1645. (defun math-ln-plus-1-raw (x)
  1646. (math-lnp1-series x 2 x (math-neg x)))
  1647. (defun math-lnp1-series (sum n xpow x)
  1648. (math-working "lnp1" sum)
  1649. (let* ((nextx (math-mul-float xpow x))
  1650. (nextsum (math-add-float sum (math-div-float nextx (math-float n)))))
  1651. (if (math-nearly-equal-float sum nextsum)
  1652. sum
  1653. (math-lnp1-series nextsum (1+ n) nextx x))))
  1654. (defconst math-approx-ln-10
  1655. (math-read-number-simple "2.302585092994045684018")
  1656. "An approximation for ln(10).")
  1657. (math-defcache math-ln-10 math-approx-ln-10
  1658. (math-ln-raw-2 '(float 1 1)))
  1659. (defconst math-approx-ln-2
  1660. (math-read-number-simple "0.693147180559945309417")
  1661. "An approximation for ln(2).")
  1662. (math-defcache math-ln-2 math-approx-ln-2
  1663. (math-ln-raw-3 (math-float '(frac 1 3))))
  1664. ;;; Hyperbolic functions.
  1665. (defun calcFunc-sinh (x) ; [N N] [Public]
  1666. (cond ((eq x 0) 0)
  1667. (math-expand-formulas
  1668. (math-normalize
  1669. (list '/ (list '- (list 'calcFunc-exp x)
  1670. (list 'calcFunc-exp (list 'neg x))) 2)))
  1671. ((Math-numberp x)
  1672. (if calc-symbolic-mode (signal 'inexact-result nil))
  1673. (math-with-extra-prec 2
  1674. (let ((expx (math-exp-raw (math-float x))))
  1675. (math-mul (math-add expx (math-div -1 expx)) '(float 5 -1)))))
  1676. ((eq (car-safe x) 'sdev)
  1677. (math-make-sdev (calcFunc-sinh (nth 1 x))
  1678. (math-mul (nth 2 x) (calcFunc-cosh (nth 1 x)))))
  1679. ((eq (car x) 'intv)
  1680. (math-sort-intv (nth 1 x)
  1681. (calcFunc-sinh (nth 2 x))
  1682. (calcFunc-sinh (nth 3 x))))
  1683. ((or (equal x '(var inf var-inf))
  1684. (equal x '(neg (var inf var-inf)))
  1685. (equal x '(var nan var-nan)))
  1686. x)
  1687. (t (calc-record-why 'numberp x)
  1688. (list 'calcFunc-sinh x))))
  1689. (put 'calcFunc-sinh 'math-expandable t)
  1690. (defun calcFunc-cosh (x) ; [N N] [Public]
  1691. (cond ((eq x 0) 1)
  1692. (math-expand-formulas
  1693. (math-normalize
  1694. (list '/ (list '+ (list 'calcFunc-exp x)
  1695. (list 'calcFunc-exp (list 'neg x))) 2)))
  1696. ((Math-numberp x)
  1697. (if calc-symbolic-mode (signal 'inexact-result nil))
  1698. (math-with-extra-prec 2
  1699. (let ((expx (math-exp-raw (math-float x))))
  1700. (math-mul (math-add expx (math-div 1 expx)) '(float 5 -1)))))
  1701. ((eq (car-safe x) 'sdev)
  1702. (math-make-sdev (calcFunc-cosh (nth 1 x))
  1703. (math-mul (nth 2 x)
  1704. (calcFunc-sinh (nth 1 x)))))
  1705. ((and (eq (car x) 'intv) (math-intv-constp x))
  1706. (setq x (math-abs x))
  1707. (math-sort-intv (nth 1 x)
  1708. (calcFunc-cosh (nth 2 x))
  1709. (calcFunc-cosh (nth 3 x))))
  1710. ((or (equal x '(var inf var-inf))
  1711. (equal x '(neg (var inf var-inf)))
  1712. (equal x '(var nan var-nan)))
  1713. (math-abs x))
  1714. (t (calc-record-why 'numberp x)
  1715. (list 'calcFunc-cosh x))))
  1716. (put 'calcFunc-cosh 'math-expandable t)
  1717. (defun calcFunc-tanh (x) ; [N N] [Public]
  1718. (cond ((eq x 0) 0)
  1719. (math-expand-formulas
  1720. (math-normalize
  1721. (let ((expx (list 'calcFunc-exp x))
  1722. (expmx (list 'calcFunc-exp (list 'neg x))))
  1723. (math-normalize
  1724. (list '/ (list '- expx expmx) (list '+ expx expmx))))))
  1725. ((Math-numberp x)
  1726. (if calc-symbolic-mode (signal 'inexact-result nil))
  1727. (math-with-extra-prec 2
  1728. (let* ((expx (calcFunc-exp (math-float x)))
  1729. (expmx (math-div 1 expx)))
  1730. (math-div (math-sub expx expmx)
  1731. (math-add expx expmx)))))
  1732. ((eq (car-safe x) 'sdev)
  1733. (math-make-sdev (calcFunc-tanh (nth 1 x))
  1734. (math-div (nth 2 x)
  1735. (math-sqr (calcFunc-cosh (nth 1 x))))))
  1736. ((eq (car x) 'intv)
  1737. (math-sort-intv (nth 1 x)
  1738. (calcFunc-tanh (nth 2 x))
  1739. (calcFunc-tanh (nth 3 x))))
  1740. ((equal x '(var inf var-inf))
  1741. 1)
  1742. ((equal x '(neg (var inf var-inf)))
  1743. -1)
  1744. ((equal x '(var nan var-nan))
  1745. x)
  1746. (t (calc-record-why 'numberp x)
  1747. (list 'calcFunc-tanh x))))
  1748. (put 'calcFunc-tanh 'math-expandable t)
  1749. (defun calcFunc-sech (x) ; [N N] [Public]
  1750. (cond ((eq x 0) 1)
  1751. (math-expand-formulas
  1752. (math-normalize
  1753. (list '/ 2 (list '+ (list 'calcFunc-exp x)
  1754. (list 'calcFunc-exp (list 'neg x))))))
  1755. ((Math-numberp x)
  1756. (if calc-symbolic-mode (signal 'inexact-result nil))
  1757. (math-with-extra-prec 2
  1758. (let ((expx (math-exp-raw (math-float x))))
  1759. (math-div '(float 2 0) (math-add expx (math-div 1 expx))))))
  1760. ((eq (car-safe x) 'sdev)
  1761. (math-make-sdev (calcFunc-sech (nth 1 x))
  1762. (math-mul (nth 2 x)
  1763. (math-mul (calcFunc-sech (nth 1 x))
  1764. (calcFunc-tanh (nth 1 x))))))
  1765. ((and (eq (car x) 'intv) (math-intv-constp x))
  1766. (setq x (math-abs x))
  1767. (math-sort-intv (nth 1 x)
  1768. (calcFunc-sech (nth 2 x))
  1769. (calcFunc-sech (nth 3 x))))
  1770. ((or (equal x '(var inf var-inf))
  1771. (equal x '(neg (var inf var-inf))))
  1772. 0)
  1773. ((equal x '(var nan var-nan))
  1774. x)
  1775. (t (calc-record-why 'numberp x)
  1776. (list 'calcFunc-sech x))))
  1777. (put 'calcFunc-sech 'math-expandable t)
  1778. (defun calcFunc-csch (x) ; [N N] [Public]
  1779. (cond ((eq x 0) (math-div 1 0))
  1780. (math-expand-formulas
  1781. (math-normalize
  1782. (list '/ 2 (list '- (list 'calcFunc-exp x)
  1783. (list 'calcFunc-exp (list 'neg x))))))
  1784. ((Math-numberp x)
  1785. (if calc-symbolic-mode (signal 'inexact-result nil))
  1786. (math-with-extra-prec 2
  1787. (let ((expx (math-exp-raw (math-float x))))
  1788. (math-div '(float 2 0) (math-add expx (math-div -1 expx))))))
  1789. ((eq (car-safe x) 'sdev)
  1790. (math-make-sdev (calcFunc-csch (nth 1 x))
  1791. (math-mul (nth 2 x)
  1792. (math-mul (calcFunc-csch (nth 1 x))
  1793. (calcFunc-coth (nth 1 x))))))
  1794. ((eq (car x) 'intv)
  1795. (if (and (Math-negp (nth 2 x))
  1796. (Math-posp (nth 3 x)))
  1797. '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
  1798. (math-sort-intv (nth 1 x)
  1799. (calcFunc-csch (nth 2 x))
  1800. (calcFunc-csch (nth 3 x)))))
  1801. ((or (equal x '(var inf var-inf))
  1802. (equal x '(neg (var inf var-inf))))
  1803. 0)
  1804. ((equal x '(var nan var-nan))
  1805. x)
  1806. (t (calc-record-why 'numberp x)
  1807. (list 'calcFunc-csch x))))
  1808. (put 'calcFunc-csch 'math-expandable t)
  1809. (defun calcFunc-coth (x) ; [N N] [Public]
  1810. (cond ((eq x 0) (math-div 1 0))
  1811. (math-expand-formulas
  1812. (math-normalize
  1813. (let ((expx (list 'calcFunc-exp x))
  1814. (expmx (list 'calcFunc-exp (list 'neg x))))
  1815. (math-normalize
  1816. (list '/ (list '+ expx expmx) (list '- expx expmx))))))
  1817. ((Math-numberp x)
  1818. (if calc-symbolic-mode (signal 'inexact-result nil))
  1819. (math-with-extra-prec 2
  1820. (let* ((expx (calcFunc-exp (math-float x)))
  1821. (expmx (math-div 1 expx)))
  1822. (math-div (math-add expx expmx)
  1823. (math-sub expx expmx)))))
  1824. ((eq (car-safe x) 'sdev)
  1825. (math-make-sdev (calcFunc-coth (nth 1 x))
  1826. (math-div (nth 2 x)
  1827. (math-sqr (calcFunc-sinh (nth 1 x))))))
  1828. ((eq (car x) 'intv)
  1829. (if (and (Math-negp (nth 2 x))
  1830. (Math-posp (nth 3 x)))
  1831. '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
  1832. (math-sort-intv (nth 1 x)
  1833. (calcFunc-coth (nth 2 x))
  1834. (calcFunc-coth (nth 3 x)))))
  1835. ((equal x '(var inf var-inf))
  1836. 1)
  1837. ((equal x '(neg (var inf var-inf)))
  1838. -1)
  1839. ((equal x '(var nan var-nan))
  1840. x)
  1841. (t (calc-record-why 'numberp x)
  1842. (list 'calcFunc-coth x))))
  1843. (put 'calcFunc-coth 'math-expandable t)
  1844. (defun calcFunc-arcsinh (x) ; [N N] [Public]
  1845. (cond ((eq x 0) 0)
  1846. (math-expand-formulas
  1847. (math-normalize
  1848. (list 'calcFunc-ln (list '+ x (list 'calcFunc-sqrt
  1849. (list '+ (list '^ x 2) 1))))))
  1850. ((Math-numberp x)
  1851. (if calc-symbolic-mode (signal 'inexact-result nil))
  1852. (math-with-extra-prec 2
  1853. (math-ln-raw (math-add x (math-sqrt-raw (math-add (math-sqr x)
  1854. '(float 1 0)))))))
  1855. ((eq (car-safe x) 'sdev)
  1856. (math-make-sdev (calcFunc-arcsinh (nth 1 x))
  1857. (math-div (nth 2 x)
  1858. (math-sqrt
  1859. (math-add (math-sqr (nth 1 x)) 1)))))
  1860. ((eq (car x) 'intv)
  1861. (math-sort-intv (nth 1 x)
  1862. (calcFunc-arcsinh (nth 2 x))
  1863. (calcFunc-arcsinh (nth 3 x))))
  1864. ((or (equal x '(var inf var-inf))
  1865. (equal x '(neg (var inf var-inf)))
  1866. (equal x '(var nan var-nan)))
  1867. x)
  1868. (t (calc-record-why 'numberp x)
  1869. (list 'calcFunc-arcsinh x))))
  1870. (put 'calcFunc-arcsinh 'math-expandable t)
  1871. (defun calcFunc-arccosh (x) ; [N N] [Public]
  1872. (cond ((eq x 1) 0)
  1873. ((and (eq x -1) calc-symbolic-mode)
  1874. '(var pi var-pi))
  1875. ((and (eq x 0) calc-symbolic-mode)
  1876. (math-div (math-mul '(var pi var-pi) '(var i var-i)) 2))
  1877. (math-expand-formulas
  1878. (math-normalize
  1879. (list 'calcFunc-ln (list '+ x (list 'calcFunc-sqrt
  1880. (list '- (list '^ x 2) 1))))))
  1881. ((Math-numberp x)
  1882. (if calc-symbolic-mode (signal 'inexact-result nil))
  1883. (if (Math-equal-int x -1)
  1884. (math-imaginary (math-pi))
  1885. (math-with-extra-prec 2
  1886. (if (or t ; need to do this even in the real case!
  1887. (memq (car-safe x) '(cplx polar)))
  1888. (let ((xp1 (math-add 1 x))) ; this gets the branch cuts right
  1889. (math-ln-raw
  1890. (math-add x (math-mul xp1
  1891. (math-sqrt-raw
  1892. (math-div (math-sub
  1893. x
  1894. '(float 1 0))
  1895. xp1))))))
  1896. (math-ln-raw
  1897. (math-add x (math-sqrt-raw (math-add (math-sqr x)
  1898. '(float -1 0)))))))))
  1899. ((eq (car-safe x) 'sdev)
  1900. (math-make-sdev (calcFunc-arccosh (nth 1 x))
  1901. (math-div (nth 2 x)
  1902. (math-sqrt
  1903. (math-add (math-sqr (nth 1 x)) -1)))))
  1904. ((eq (car x) 'intv)
  1905. (math-sort-intv (nth 1 x)
  1906. (calcFunc-arccosh (nth 2 x))
  1907. (calcFunc-arccosh (nth 3 x))))
  1908. ((or (equal x '(var inf var-inf))
  1909. (equal x '(neg (var inf var-inf)))
  1910. (equal x '(var nan var-nan)))
  1911. x)
  1912. (t (calc-record-why 'numberp x)
  1913. (list 'calcFunc-arccosh x))))
  1914. (put 'calcFunc-arccosh 'math-expandable t)
  1915. (defun calcFunc-arctanh (x) ; [N N] [Public]
  1916. (cond ((eq x 0) 0)
  1917. ((and (Math-equal-int x 1) calc-infinite-mode)
  1918. '(var inf var-inf))
  1919. ((and (Math-equal-int x -1) calc-infinite-mode)
  1920. '(neg (var inf var-inf)))
  1921. (math-expand-formulas
  1922. (list '/ (list '-
  1923. (list 'calcFunc-ln (list '+ 1 x))
  1924. (list 'calcFunc-ln (list '- 1 x))) 2))
  1925. ((Math-numberp x)
  1926. (if calc-symbolic-mode (signal 'inexact-result nil))
  1927. (math-with-extra-prec 2
  1928. (if (or (memq (car-safe x) '(cplx polar))
  1929. (Math-lessp 1 x))
  1930. (math-mul (math-sub (math-ln-raw (math-add '(float 1 0) x))
  1931. (math-ln-raw (math-sub '(float 1 0) x)))
  1932. '(float 5 -1))
  1933. (if (and (math-equal-int x 1) calc-infinite-mode)
  1934. '(var inf var-inf)
  1935. (if (and (math-equal-int x -1) calc-infinite-mode)
  1936. '(neg (var inf var-inf))
  1937. (math-mul (math-ln-raw (math-div (math-add '(float 1 0) x)
  1938. (math-sub 1 x)))
  1939. '(float 5 -1)))))))
  1940. ((eq (car-safe x) 'sdev)
  1941. (math-make-sdev (calcFunc-arctanh (nth 1 x))
  1942. (math-div (nth 2 x)
  1943. (math-sub 1 (math-sqr (nth 1 x))))))
  1944. ((eq (car x) 'intv)
  1945. (math-sort-intv (nth 1 x)
  1946. (calcFunc-arctanh (nth 2 x))
  1947. (calcFunc-arctanh (nth 3 x))))
  1948. ((equal x '(var nan var-nan))
  1949. x)
  1950. (t (calc-record-why 'numberp x)
  1951. (list 'calcFunc-arctanh x))))
  1952. (put 'calcFunc-arctanh 'math-expandable t)
  1953. ;;; Convert A from HMS or degrees to radians.
  1954. (defun calcFunc-rad (a) ; [R R] [Public]
  1955. (cond ((or (Math-numberp a)
  1956. (eq (car a) 'intv))
  1957. (math-with-extra-prec 2
  1958. (math-mul a (math-pi-over-180))))
  1959. ((eq (car a) 'hms)
  1960. (math-from-hms a 'rad))
  1961. ((eq (car a) 'sdev)
  1962. (math-make-sdev (calcFunc-rad (nth 1 a))
  1963. (calcFunc-rad (nth 2 a))))
  1964. (math-expand-formulas
  1965. (math-div (math-mul a '(var pi var-pi)) 180))
  1966. ((math-infinitep a) a)
  1967. (t (list 'calcFunc-rad a))))
  1968. (put 'calcFunc-rad 'math-expandable t)
  1969. ;;; Convert A from HMS or radians to degrees.
  1970. (defun calcFunc-deg (a) ; [R R] [Public]
  1971. (cond ((or (Math-numberp a)
  1972. (eq (car a) 'intv))
  1973. (math-with-extra-prec 2
  1974. (math-div a (math-pi-over-180))))
  1975. ((eq (car a) 'hms)
  1976. (math-from-hms a 'deg))
  1977. ((eq (car a) 'sdev)
  1978. (math-make-sdev (calcFunc-deg (nth 1 a))
  1979. (calcFunc-deg (nth 2 a))))
  1980. (math-expand-formulas
  1981. (math-div (math-mul 180 a) '(var pi var-pi)))
  1982. ((math-infinitep a) a)
  1983. (t (list 'calcFunc-deg a))))
  1984. (put 'calcFunc-deg 'math-expandable t)
  1985. (provide 'calc-math)
  1986. ;;; calc-math.el ends here