calc-math.el 69 KB

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