calc-math.el 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171
  1. ;;; calc-math.el --- mathematical functions for Calc
  2. ;; Copyright (C) 1990-1993, 2001-2017 Free Software Foundation, Inc.
  3. ;; Author: David Gillespie <daveg@synaptics.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;; Code:
  17. ;; This file is autoloaded from calc-ext.el.
  18. (require 'calc-ext)
  19. (require 'calc-macs)
  20. ;;; 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. ((and (not math-simplifying-units)
  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 ((and (not math-simplifying-units)
  676. (eq calc-angle-mode 'deg))
  677. (if (math-constp a)
  678. (math-div a (math-pi-over-180))
  679. (list 'calcFunc-deg a)))
  680. ((eq calc-angle-mode 'hms)
  681. (math-to-hms a 'rad))
  682. (t a)))
  683. (defun math-to-radians-2 (a &optional force-symbolic) ; [N N]
  684. (cond ((eq (car-safe a) 'hms)
  685. (math-from-hms a 'rad))
  686. ((and (not math-simplifying-units)
  687. (memq calc-angle-mode '(deg hms)))
  688. (if (or calc-symbolic-mode force-symbolic)
  689. (math-div (math-mul a '(var pi var-pi)) 180)
  690. (math-mul a (math-pi-over-180))))
  691. (t a)))
  692. (defun math-from-radians-2 (a &optional force-symbolic) ; [N N]
  693. (cond ((and (not math-simplifying-units)
  694. (memq calc-angle-mode '(deg hms)))
  695. (if (or calc-symbolic-mode force-symbolic)
  696. (math-div (math-mul 180 a) '(var pi var-pi))
  697. (math-div a (math-pi-over-180))))
  698. (t a)))
  699. ;;; Sine, cosine, and tangent.
  700. (defun calcFunc-sin (x) ; [N N] [Public]
  701. (cond ((and (integerp x)
  702. (if (eq calc-angle-mode 'deg)
  703. (= (% x 90) 0)
  704. (= x 0)))
  705. (aref [0 1 0 -1] (math-mod (/ x 90) 4)))
  706. ((Math-scalarp x)
  707. (math-with-extra-prec 2
  708. (math-sin-raw (math-to-radians (math-float x)))))
  709. ((eq (car x) 'sdev)
  710. (if (math-constp x)
  711. (math-with-extra-prec 2
  712. (let* ((xx (math-to-radians (math-float (nth 1 x))))
  713. (xs (math-to-radians (math-float (nth 2 x))))
  714. (sc (math-sin-cos-raw xx)))
  715. (math-make-sdev (car sc) (math-mul xs (cdr sc)))))
  716. (math-make-sdev (calcFunc-sin (nth 1 x))
  717. (math-mul (nth 2 x) (calcFunc-cos (nth 1 x))))))
  718. ((and (eq (car x) 'intv) (math-intv-constp x))
  719. (calcFunc-cos (math-sub x (math-quarter-circle nil))))
  720. ((equal x '(var nan var-nan))
  721. x)
  722. (t (calc-record-why 'scalarp x)
  723. (list 'calcFunc-sin x))))
  724. (defun calcFunc-cos (x) ; [N N] [Public]
  725. (cond ((and (integerp x)
  726. (if (eq calc-angle-mode 'deg)
  727. (= (% x 90) 0)
  728. (= x 0)))
  729. (aref [1 0 -1 0] (math-mod (/ x 90) 4)))
  730. ((Math-scalarp x)
  731. (math-with-extra-prec 2
  732. (math-cos-raw (math-to-radians (math-float x)))))
  733. ((eq (car x) 'sdev)
  734. (if (math-constp x)
  735. (math-with-extra-prec 2
  736. (let* ((xx (math-to-radians (math-float (nth 1 x))))
  737. (xs (math-to-radians (math-float (nth 2 x))))
  738. (sc (math-sin-cos-raw xx)))
  739. (math-make-sdev (cdr sc) (math-mul xs (car sc)))))
  740. (math-make-sdev (calcFunc-cos (nth 1 x))
  741. (math-mul (nth 2 x) (calcFunc-sin (nth 1 x))))))
  742. ((and (eq (car x) 'intv) (math-intv-constp x))
  743. (math-with-extra-prec 2
  744. (let* ((xx (math-to-radians (math-float x)))
  745. (na (math-floor (math-div (nth 2 xx) (math-pi))))
  746. (nb (math-floor (math-div (nth 3 xx) (math-pi))))
  747. (span (math-sub nb na)))
  748. (if (memq span '(0 1))
  749. (let ((int (math-sort-intv (nth 1 x)
  750. (math-cos-raw (nth 2 xx))
  751. (math-cos-raw (nth 3 xx)))))
  752. (if (eq span 1)
  753. (if (math-evenp na)
  754. (math-make-intv (logior (nth 1 x) 2)
  755. -1
  756. (nth 3 int))
  757. (math-make-intv (logior (nth 1 x) 1)
  758. (nth 2 int)
  759. 1))
  760. int))
  761. (list 'intv 3 -1 1)))))
  762. ((equal x '(var nan var-nan))
  763. x)
  764. (t (calc-record-why 'scalarp x)
  765. (list 'calcFunc-cos x))))
  766. (defun calcFunc-sincos (x) ; [V N] [Public]
  767. (if (Math-scalarp x)
  768. (math-with-extra-prec 2
  769. (let ((sc (math-sin-cos-raw (math-to-radians (math-float x)))))
  770. (list 'vec (cdr sc) (car sc)))) ; the vector [cos, sin]
  771. (list 'vec (calcFunc-sin x) (calcFunc-cos x))))
  772. (defun calcFunc-tan (x) ; [N N] [Public]
  773. (cond ((and (integerp x)
  774. (if (eq calc-angle-mode 'deg)
  775. (= (% x 180) 0)
  776. (= x 0)))
  777. 0)
  778. ((Math-scalarp x)
  779. (math-with-extra-prec 2
  780. (math-tan-raw (math-to-radians (math-float x)))))
  781. ((eq (car x) 'sdev)
  782. (if (math-constp x)
  783. (math-with-extra-prec 2
  784. (let* ((xx (math-to-radians (math-float (nth 1 x))))
  785. (xs (math-to-radians (math-float (nth 2 x))))
  786. (sc (math-sin-cos-raw xx)))
  787. (if (and (math-zerop (cdr sc)) (not calc-infinite-mode))
  788. (progn
  789. (calc-record-why "*Division by zero")
  790. (list 'calcFunc-tan x))
  791. (math-make-sdev (math-div-float (car sc) (cdr sc))
  792. (math-div-float xs (math-sqr (cdr sc)))))))
  793. (math-make-sdev (calcFunc-tan (nth 1 x))
  794. (math-div (nth 2 x)
  795. (math-sqr (calcFunc-cos (nth 1 x)))))))
  796. ((and (eq (car x) 'intv) (math-intv-constp x))
  797. (or (math-with-extra-prec 2
  798. (let* ((xx (math-to-radians (math-float x)))
  799. (na (math-floor (math-div (math-sub (nth 2 xx)
  800. (math-pi-over-2))
  801. (math-pi))))
  802. (nb (math-floor (math-div (math-sub (nth 3 xx)
  803. (math-pi-over-2))
  804. (math-pi)))))
  805. (and (equal na nb)
  806. (math-sort-intv (nth 1 x)
  807. (math-tan-raw (nth 2 xx))
  808. (math-tan-raw (nth 3 xx))))))
  809. '(intv 3 (neg (var inf var-inf)) (var inf var-inf))))
  810. ((equal x '(var nan var-nan))
  811. x)
  812. (t (calc-record-why 'scalarp x)
  813. (list 'calcFunc-tan x))))
  814. (defun calcFunc-sec (x)
  815. (cond ((and (integerp x)
  816. (eq calc-angle-mode 'deg)
  817. (= (% x 180) 0))
  818. (if (= (% x 360) 0)
  819. 1
  820. -1))
  821. ((and (integerp x)
  822. (eq calc-angle-mode 'rad)
  823. (= x 0))
  824. 1)
  825. ((Math-scalarp x)
  826. (math-with-extra-prec 2
  827. (math-sec-raw (math-to-radians (math-float x)))))
  828. ((eq (car x) 'sdev)
  829. (if (math-constp x)
  830. (math-with-extra-prec 2
  831. (let* ((xx (math-to-radians (math-float (nth 1 x))))
  832. (xs (math-to-radians (math-float (nth 2 x))))
  833. (sc (math-sin-cos-raw xx)))
  834. (if (and (math-zerop (cdr sc))
  835. (not calc-infinite-mode))
  836. (progn
  837. (calc-record-why "*Division by zero")
  838. (list 'calcFunc-sec x))
  839. (math-make-sdev (math-div-float '(float 1 0) (cdr sc))
  840. (math-div-float
  841. (math-mul xs (car sc))
  842. (math-sqr (cdr sc)))))))
  843. (math-make-sdev (calcFunc-sec (nth 1 x))
  844. (math-div
  845. (math-mul (nth 2 x)
  846. (calcFunc-sin (nth 1 x)))
  847. (math-sqr (calcFunc-cos (nth 1 x)))))))
  848. ((and (eq (car x) 'intv)
  849. (math-intv-constp x))
  850. (math-with-extra-prec 2
  851. (let* ((xx (math-to-radians (math-float x)))
  852. (na (math-floor (math-div (math-sub (nth 2 xx)
  853. (math-pi-over-2))
  854. (math-pi))))
  855. (nb (math-floor (math-div (math-sub (nth 3 xx)
  856. (math-pi-over-2))
  857. (math-pi))))
  858. (naa (math-floor (math-div (nth 2 xx) (math-pi-over-2))))
  859. (nbb (math-floor (math-div (nth 3 xx) (math-pi-over-2))))
  860. (span (math-sub nbb naa)))
  861. (if (not (equal na nb))
  862. '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
  863. (let ((int (math-sort-intv (nth 1 x)
  864. (math-sec-raw (nth 2 xx))
  865. (math-sec-raw (nth 3 xx)))))
  866. (if (eq span 1)
  867. (if (math-evenp (math-div (math-add naa 1) 2))
  868. (math-make-intv (logior (nth 1 int) 2)
  869. 1
  870. (nth 3 int))
  871. (math-make-intv (logior (nth 1 int) 1)
  872. (nth 2 int)
  873. -1))
  874. int))))))
  875. ((equal x '(var nan var-nan))
  876. x)
  877. (t (calc-record-why 'scalarp x)
  878. (list 'calcFunc-sec x))))
  879. (defun calcFunc-csc (x)
  880. (cond ((and (integerp x)
  881. (eq calc-angle-mode 'deg)
  882. (= (% (- x 90) 180) 0))
  883. (if (= (% (- x 90) 360) 0)
  884. 1
  885. -1))
  886. ((Math-scalarp x)
  887. (math-with-extra-prec 2
  888. (math-csc-raw (math-to-radians (math-float x)))))
  889. ((eq (car x) 'sdev)
  890. (if (math-constp x)
  891. (math-with-extra-prec 2
  892. (let* ((xx (math-to-radians (math-float (nth 1 x))))
  893. (xs (math-to-radians (math-float (nth 2 x))))
  894. (sc (math-sin-cos-raw xx)))
  895. (if (and (math-zerop (car sc))
  896. (not calc-infinite-mode))
  897. (progn
  898. (calc-record-why "*Division by zero")
  899. (list 'calcFunc-csc x))
  900. (math-make-sdev (math-div-float '(float 1 0) (car sc))
  901. (math-div-float
  902. (math-mul xs (cdr sc))
  903. (math-sqr (car sc)))))))
  904. (math-make-sdev (calcFunc-csc (nth 1 x))
  905. (math-div
  906. (math-mul (nth 2 x)
  907. (calcFunc-cos (nth 1 x)))
  908. (math-sqr (calcFunc-sin (nth 1 x)))))))
  909. ((and (eq (car x) 'intv)
  910. (math-intv-constp x))
  911. (math-with-extra-prec 2
  912. (let* ((xx (math-to-radians (math-float x)))
  913. (na (math-floor (math-div (nth 2 xx) (math-pi))))
  914. (nb (math-floor (math-div (nth 3 xx) (math-pi))))
  915. (naa (math-floor (math-div (nth 2 xx) (math-pi-over-2))))
  916. (nbb (math-floor (math-div (nth 3 xx) (math-pi-over-2))))
  917. (span (math-sub nbb naa)))
  918. (if (not (equal na nb))
  919. '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
  920. (let ((int (math-sort-intv (nth 1 x)
  921. (math-csc-raw (nth 2 xx))
  922. (math-csc-raw (nth 3 xx)))))
  923. (if (eq span 1)
  924. (if (math-evenp (math-div naa 2))
  925. (math-make-intv (logior (nth 1 int) 2)
  926. 1
  927. (nth 3 int))
  928. (math-make-intv (logior (nth 1 int) 1)
  929. (nth 2 int)
  930. -1))
  931. int))))))
  932. ((equal x '(var nan var-nan))
  933. x)
  934. (t (calc-record-why 'scalarp x)
  935. (list 'calcFunc-csc x))))
  936. (defun calcFunc-cot (x) ; [N N] [Public]
  937. (cond ((and (integerp x)
  938. (if (eq calc-angle-mode 'deg)
  939. (= (% (- x 90) 180) 0)
  940. (= x 0)))
  941. 0)
  942. ((Math-scalarp x)
  943. (math-with-extra-prec 2
  944. (math-cot-raw (math-to-radians (math-float x)))))
  945. ((eq (car x) 'sdev)
  946. (if (math-constp x)
  947. (math-with-extra-prec 2
  948. (let* ((xx (math-to-radians (math-float (nth 1 x))))
  949. (xs (math-to-radians (math-float (nth 2 x))))
  950. (sc (math-sin-cos-raw xx)))
  951. (if (and (math-zerop (car sc)) (not calc-infinite-mode))
  952. (progn
  953. (calc-record-why "*Division by zero")
  954. (list 'calcFunc-cot x))
  955. (math-make-sdev (math-div-float (cdr sc) (car sc))
  956. (math-div-float xs (math-sqr (car sc)))))))
  957. (math-make-sdev (calcFunc-cot (nth 1 x))
  958. (math-div (nth 2 x)
  959. (math-sqr (calcFunc-sin (nth 1 x)))))))
  960. ((and (eq (car x) 'intv) (math-intv-constp x))
  961. (or (math-with-extra-prec 2
  962. (let* ((xx (math-to-radians (math-float x)))
  963. (na (math-floor (math-div (nth 2 xx) (math-pi))))
  964. (nb (math-floor (math-div (nth 3 xx) (math-pi)))))
  965. (and (equal na nb)
  966. (math-sort-intv (nth 1 x)
  967. (math-cot-raw (nth 2 xx))
  968. (math-cot-raw (nth 3 xx))))))
  969. '(intv 3 (neg (var inf var-inf)) (var inf var-inf))))
  970. ((equal x '(var nan var-nan))
  971. x)
  972. (t (calc-record-why 'scalarp x)
  973. (list 'calcFunc-cot x))))
  974. (defun math-sin-raw (x &optional orgx) ; [N N]
  975. (cond ((eq (car x) 'cplx)
  976. (let* ((expx (math-exp-raw (nth 2 x)))
  977. (expmx (math-div-float '(float 1 0) expx))
  978. (sc (math-sin-cos-raw (nth 1 x))))
  979. (list 'cplx
  980. (math-mul-float (car sc)
  981. (math-mul-float (math-add-float expx expmx)
  982. '(float 5 -1)))
  983. (math-mul-float (cdr sc)
  984. (math-mul-float (math-sub-float expx expmx)
  985. '(float 5 -1))))))
  986. ((eq (car x) 'polar)
  987. (math-polar (math-sin-raw (math-complex x))))
  988. ((Math-integer-negp (nth 1 x))
  989. (math-neg-float (math-sin-raw (math-neg-float x) (if orgx orgx x))))
  990. ((math-lessp-float '(float 7 0) x) ; avoid inf loops due to roundoff
  991. (math-sin-raw (math-mod x (math-two-pi)) (if orgx orgx x)))
  992. (t (math-sin-raw-2 x (if orgx orgx x)))))
  993. (defun math-cos-raw (x) ; [N N]
  994. (if (eq (car-safe x) 'polar)
  995. (math-polar (math-cos-raw (math-complex x)))
  996. (math-sin-raw (math-sub (math-pi-over-2) x) x)))
  997. (defun math-sec-raw (x) ; [N N]
  998. (cond ((eq (car x) 'cplx)
  999. (let* ((x (math-mul x '(float 1 0)))
  1000. (expx (math-exp-raw (nth 2 x)))
  1001. (expmx (math-div-float '(float 1 0) expx))
  1002. (sh (math-mul-float (math-sub-float expx expmx) '(float 5 -1)))
  1003. (ch (math-mul-float (math-add-float expx expmx) '(float 5 -1)))
  1004. (sc (math-sin-cos-raw (nth 1 x)))
  1005. (d (math-add-float
  1006. (math-mul-float (math-sqr (car sc))
  1007. (math-sqr sh))
  1008. (math-mul-float (math-sqr (cdr sc))
  1009. (math-sqr ch)))))
  1010. (and (not (eq (nth 1 d) 0))
  1011. (list 'cplx
  1012. (math-div-float (math-mul-float (cdr sc) ch) d)
  1013. (math-div-float (math-mul-float (car sc) sh) d)))))
  1014. ((eq (car x) 'polar)
  1015. (math-polar (math-sec-raw (math-complex x))))
  1016. (t
  1017. (let ((cs (math-cos-raw x)))
  1018. (if (eq cs 0)
  1019. (math-div 1 0)
  1020. (math-div-float '(float 1 0) cs))))))
  1021. (defun math-csc-raw (x) ; [N N]
  1022. (cond ((eq (car x) 'cplx)
  1023. (let* ((x (math-mul x '(float 1 0)))
  1024. (expx (math-exp-raw (nth 2 x)))
  1025. (expmx (math-div-float '(float 1 0) expx))
  1026. (sh (math-mul-float (math-sub-float expx expmx) '(float 5 -1)))
  1027. (ch (math-mul-float (math-add-float expx expmx) '(float 5 -1)))
  1028. (sc (math-sin-cos-raw (nth 1 x)))
  1029. (d (math-add-float
  1030. (math-mul-float (math-sqr (car sc))
  1031. (math-sqr ch))
  1032. (math-mul-float (math-sqr (cdr sc))
  1033. (math-sqr sh)))))
  1034. (and (not (eq (nth 1 d) 0))
  1035. (list 'cplx
  1036. (math-div-float (math-mul-float (car sc) ch) d)
  1037. (math-div-float (math-mul-float (cdr sc) sh) d)))))
  1038. ((eq (car x) 'polar)
  1039. (math-polar (math-csc-raw (math-complex x))))
  1040. (t
  1041. (let ((sn (math-sin-raw x)))
  1042. (if (eq sn 0)
  1043. (math-div 1 0)
  1044. (math-div-float '(float 1 0) sn))))))
  1045. (defun math-cot-raw (x) ; [N N]
  1046. (cond ((eq (car x) 'cplx)
  1047. (let* ((x (math-mul x '(float 1 0)))
  1048. (expx (math-exp-raw (nth 2 x)))
  1049. (expmx (math-div-float '(float 1 0) expx))
  1050. (sh (math-mul-float (math-sub-float expx expmx) '(float 5 -1)))
  1051. (ch (math-mul-float (math-add-float expx expmx) '(float 5 -1)))
  1052. (sc (math-sin-cos-raw (nth 1 x)))
  1053. (d (math-add-float
  1054. (math-sqr (car sc))
  1055. (math-sqr sh))))
  1056. (and (not (eq (nth 1 d) 0))
  1057. (list 'cplx
  1058. (math-div-float
  1059. (math-mul-float (car sc) (cdr sc))
  1060. d)
  1061. (math-neg
  1062. (math-div-float
  1063. (math-mul-float sh ch)
  1064. d))))))
  1065. ((eq (car x) 'polar)
  1066. (math-polar (math-cot-raw (math-complex x))))
  1067. (t
  1068. (let ((sc (math-sin-cos-raw x)))
  1069. (if (eq (nth 1 (car sc)) 0)
  1070. (math-div (cdr sc) 0)
  1071. (math-div-float (cdr sc) (car sc)))))))
  1072. ;;; This could use a smarter method: Reduce x as in math-sin-raw, then
  1073. ;;; compute either sin(x) or cos(x), whichever is smaller, and compute
  1074. ;;; the other using the identity sin(x)^2 + cos(x)^2 = 1.
  1075. (defun math-sin-cos-raw (x) ; [F.F F] (result is (sin x . cos x))
  1076. (cons (math-sin-raw x) (math-cos-raw x)))
  1077. (defun math-tan-raw (x) ; [N N]
  1078. (cond ((eq (car x) 'cplx)
  1079. (let* ((x (math-mul x '(float 2 0)))
  1080. (expx (math-exp-raw (nth 2 x)))
  1081. (expmx (math-div-float '(float 1 0) expx))
  1082. (sc (math-sin-cos-raw (nth 1 x)))
  1083. (d (math-add-float (cdr sc)
  1084. (math-mul-float (math-add-float expx expmx)
  1085. '(float 5 -1)))))
  1086. (and (not (eq (nth 1 d) 0))
  1087. (list 'cplx
  1088. (math-div-float (car sc) d)
  1089. (math-div-float (math-mul-float (math-sub-float expx
  1090. expmx)
  1091. '(float 5 -1)) d)))))
  1092. ((eq (car x) 'polar)
  1093. (math-polar (math-tan-raw (math-complex x))))
  1094. (t
  1095. (let ((sc (math-sin-cos-raw x)))
  1096. (if (eq (nth 1 (cdr sc)) 0)
  1097. (math-div (car sc) 0)
  1098. (math-div-float (car sc) (cdr sc)))))))
  1099. (defun math-sin-raw-2 (x orgx) ; This avoids poss of inf recursion. [F F]
  1100. (let ((xmpo2 (math-sub-float (math-pi-over-2) x)))
  1101. (cond ((Math-integer-negp (nth 1 xmpo2))
  1102. (math-neg-float (math-sin-raw-2 (math-sub-float x (math-pi))
  1103. orgx)))
  1104. ((math-lessp-float (math-pi-over-4) x)
  1105. (math-cos-raw-2 xmpo2 orgx))
  1106. ((math-lessp-float x (math-neg (math-pi-over-4)))
  1107. (math-neg (math-cos-raw-2 (math-add (math-pi-over-2) x) orgx)))
  1108. ((math-with-extra-prec -1 (math-nearly-zerop-float x orgx))
  1109. '(float 0 0))
  1110. ((math-use-emacs-fn 'sin x))
  1111. (calc-symbolic-mode (signal 'inexact-result nil))
  1112. (t (math-sin-series x 6 4 x (math-neg-float (math-sqr-float x)))))))
  1113. (defun math-cos-raw-2 (x orgx) ; [F F]
  1114. (cond ((math-with-extra-prec -1 (math-nearly-zerop-float x orgx))
  1115. '(float 1 0))
  1116. ((math-use-emacs-fn 'cos x))
  1117. (calc-symbolic-mode (signal 'inexact-result nil))
  1118. (t (let ((xnegsqr (math-neg-float (math-sqr-float x))))
  1119. (math-sin-series
  1120. (math-add-float '(float 1 0)
  1121. (math-mul-float xnegsqr '(float 5 -1)))
  1122. 24 5 xnegsqr xnegsqr)))))
  1123. (defun math-sin-series (sum nfac n x xnegsqr)
  1124. (math-working "sin" sum)
  1125. (let* ((nextx (math-mul-float x xnegsqr))
  1126. (nextsum (math-add-float sum (math-div-float nextx
  1127. (math-float nfac)))))
  1128. (if (math-nearly-equal-float sum nextsum)
  1129. sum
  1130. (math-sin-series nextsum (math-mul nfac (* n (1+ n)))
  1131. (+ n 2) nextx xnegsqr))))
  1132. ;;; Inverse sine, cosine, tangent.
  1133. (defun calcFunc-arcsin (x) ; [N N] [Public]
  1134. (cond ((eq x 0) 0)
  1135. ((and (eq x 1) (eq calc-angle-mode 'deg)) 90)
  1136. ((and (eq x -1) (eq calc-angle-mode 'deg)) -90)
  1137. (calc-symbolic-mode (signal 'inexact-result nil))
  1138. ((Math-numberp x)
  1139. (math-with-extra-prec 2
  1140. (math-from-radians (math-arcsin-raw (math-float x)))))
  1141. ((eq (car x) 'sdev)
  1142. (math-make-sdev (calcFunc-arcsin (nth 1 x))
  1143. (math-from-radians
  1144. (math-div (nth 2 x)
  1145. (math-sqrt
  1146. (math-sub 1 (math-sqr (nth 1 x))))))))
  1147. ((eq (car x) 'intv)
  1148. (math-sort-intv (nth 1 x)
  1149. (calcFunc-arcsin (nth 2 x))
  1150. (calcFunc-arcsin (nth 3 x))))
  1151. ((equal x '(var nan var-nan))
  1152. x)
  1153. (t (calc-record-why 'numberp x)
  1154. (list 'calcFunc-arcsin x))))
  1155. (defun calcFunc-arccos (x) ; [N N] [Public]
  1156. (cond ((eq x 1) 0)
  1157. ((and (eq x 0) (eq calc-angle-mode 'deg)) 90)
  1158. ((and (eq x -1) (eq calc-angle-mode 'deg)) 180)
  1159. (calc-symbolic-mode (signal 'inexact-result nil))
  1160. ((Math-numberp x)
  1161. (math-with-extra-prec 2
  1162. (math-from-radians (math-arccos-raw (math-float x)))))
  1163. ((eq (car x) 'sdev)
  1164. (math-make-sdev (calcFunc-arccos (nth 1 x))
  1165. (math-from-radians
  1166. (math-div (nth 2 x)
  1167. (math-sqrt
  1168. (math-sub 1 (math-sqr (nth 1 x))))))))
  1169. ((eq (car x) 'intv)
  1170. (math-sort-intv (nth 1 x)
  1171. (calcFunc-arccos (nth 2 x))
  1172. (calcFunc-arccos (nth 3 x))))
  1173. ((equal x '(var nan var-nan))
  1174. x)
  1175. (t (calc-record-why 'numberp x)
  1176. (list 'calcFunc-arccos x))))
  1177. (defun calcFunc-arctan (x) ; [N N] [Public]
  1178. (cond ((eq x 0) 0)
  1179. ((and (eq x 1) (eq calc-angle-mode 'deg)) 45)
  1180. ((and (eq x -1) (eq calc-angle-mode 'deg)) -45)
  1181. ((Math-numberp x)
  1182. (math-with-extra-prec 2
  1183. (math-from-radians (math-arctan-raw (math-float x)))))
  1184. ((eq (car x) 'sdev)
  1185. (math-make-sdev (calcFunc-arctan (nth 1 x))
  1186. (math-from-radians
  1187. (math-div (nth 2 x)
  1188. (math-add 1 (math-sqr (nth 1 x)))))))
  1189. ((eq (car x) 'intv)
  1190. (math-sort-intv (nth 1 x)
  1191. (calcFunc-arctan (nth 2 x))
  1192. (calcFunc-arctan (nth 3 x))))
  1193. ((equal x '(var inf var-inf))
  1194. (math-quarter-circle t))
  1195. ((equal x '(neg (var inf var-inf)))
  1196. (math-neg (math-quarter-circle t)))
  1197. ((equal x '(var nan var-nan))
  1198. x)
  1199. (t (calc-record-why 'numberp x)
  1200. (list 'calcFunc-arctan x))))
  1201. (defun math-arcsin-raw (x) ; [N N]
  1202. (let ((a (math-sqrt-raw (math-sub '(float 1 0) (math-sqr x)))))
  1203. (if (or (memq (car x) '(cplx polar))
  1204. (memq (car a) '(cplx polar)))
  1205. (math-with-extra-prec 2 ; use extra precision for difficult case
  1206. (math-mul '(cplx 0 -1)
  1207. (math-ln-raw (math-add (math-mul '(cplx 0 1) x) a))))
  1208. (math-arctan2-raw x a))))
  1209. (defun math-arccos-raw (x) ; [N N]
  1210. (math-sub (math-pi-over-2) (math-arcsin-raw x)))
  1211. (defun math-arctan-raw (x) ; [N N]
  1212. (cond ((memq (car x) '(cplx polar))
  1213. (math-with-extra-prec 2 ; extra-extra
  1214. (math-div (math-sub
  1215. (math-ln-raw (math-add 1 (math-mul '(cplx 0 1) x)))
  1216. (math-ln-raw (math-add 1 (math-mul '(cplx 0 -1) x))))
  1217. '(cplx 0 2))))
  1218. ((Math-integer-negp (nth 1 x))
  1219. (math-neg-float (math-arctan-raw (math-neg-float x))))
  1220. ((math-zerop x) x)
  1221. ((math-use-emacs-fn 'atan x))
  1222. (calc-symbolic-mode (signal 'inexact-result nil))
  1223. ((math-equal-int x 1) (math-pi-over-4))
  1224. ((math-equal-int x -1) (math-neg (math-pi-over-4)))
  1225. ((math-lessp-float '(float 414214 -6) x) ; if x > sqrt(2) - 1, reduce
  1226. (if (math-lessp-float '(float 1 0) x)
  1227. (math-sub-float (math-mul-float (math-pi) '(float 5 -1))
  1228. (math-arctan-raw (math-div-float '(float 1 0) x)))
  1229. (math-sub-float (math-mul-float (math-pi) '(float 25 -2))
  1230. (math-arctan-raw (math-div-float
  1231. (math-sub-float '(float 1 0) x)
  1232. (math-add-float '(float 1 0)
  1233. x))))))
  1234. (t (math-arctan-series x 3 x (math-neg-float (math-sqr-float x))))))
  1235. (defun math-arctan-series (sum n x xnegsqr)
  1236. (math-working "arctan" sum)
  1237. (let* ((nextx (math-mul-float x xnegsqr))
  1238. (nextsum (math-add-float sum (math-div-float nextx (math-float n)))))
  1239. (if (math-nearly-equal-float sum nextsum)
  1240. sum
  1241. (math-arctan-series nextsum (+ n 2) nextx xnegsqr))))
  1242. (defun calcFunc-arctan2 (y x) ; [F R R] [Public]
  1243. (if (Math-anglep y)
  1244. (if (Math-anglep x)
  1245. (math-with-extra-prec 2
  1246. (math-from-radians (math-arctan2-raw (math-float y)
  1247. (math-float x))))
  1248. (calc-record-why 'anglep x)
  1249. (list 'calcFunc-arctan2 y x))
  1250. (if (and (or (math-infinitep x) (math-anglep x))
  1251. (or (math-infinitep y) (math-anglep y)))
  1252. (progn
  1253. (if (math-posp x)
  1254. (setq x 1)
  1255. (if (math-negp x)
  1256. (setq x -1)
  1257. (or (math-zerop x)
  1258. (setq x nil))))
  1259. (if (math-posp y)
  1260. (setq y 1)
  1261. (if (math-negp y)
  1262. (setq y -1)
  1263. (or (math-zerop y)
  1264. (setq y nil))))
  1265. (if (and y x)
  1266. (calcFunc-arctan2 y x)
  1267. '(var nan var-nan)))
  1268. (calc-record-why 'anglep y)
  1269. (list 'calcFunc-arctan2 y x))))
  1270. (defun math-arctan2-raw (y x) ; [F R R]
  1271. (cond ((math-zerop y)
  1272. (if (math-negp x) (math-pi)
  1273. (if (or (math-floatp x) (math-floatp y)) '(float 0 0) 0)))
  1274. ((math-zerop x)
  1275. (if (math-posp y)
  1276. (math-pi-over-2)
  1277. (math-neg (math-pi-over-2))))
  1278. ((math-posp x)
  1279. (math-arctan-raw (math-div-float y x)))
  1280. ((math-posp y)
  1281. (math-add-float (math-arctan-raw (math-div-float y x))
  1282. (math-pi)))
  1283. (t
  1284. (math-sub-float (math-arctan-raw (math-div-float y x))
  1285. (math-pi)))))
  1286. (defun calcFunc-arcsincos (x) ; [V N] [Public]
  1287. (if (and (Math-vectorp x)
  1288. (= (length x) 3))
  1289. (calcFunc-arctan2 (nth 2 x) (nth 1 x))
  1290. (math-reject-arg x "*Two-element vector expected")))
  1291. ;;; Exponential function.
  1292. (defun calcFunc-exp (x) ; [N N] [Public]
  1293. (cond ((eq x 0) 1)
  1294. ((and (memq x '(1 -1)) calc-symbolic-mode)
  1295. (if (eq x 1) '(var e var-e) (math-div 1 '(var e var-e))))
  1296. ((Math-numberp x)
  1297. (math-with-extra-prec 2 (math-exp-raw (math-float x))))
  1298. ((eq (car-safe x) 'sdev)
  1299. (let ((ex (calcFunc-exp (nth 1 x))))
  1300. (math-make-sdev ex (math-mul (nth 2 x) ex))))
  1301. ((eq (car-safe x) 'intv)
  1302. (math-make-intv (nth 1 x) (calcFunc-exp (nth 2 x))
  1303. (calcFunc-exp (nth 3 x))))
  1304. ((equal x '(var inf var-inf))
  1305. x)
  1306. ((equal x '(neg (var inf var-inf)))
  1307. 0)
  1308. ((equal x '(var nan var-nan))
  1309. x)
  1310. (t (calc-record-why 'numberp x)
  1311. (list 'calcFunc-exp x))))
  1312. (defun calcFunc-expm1 (x) ; [N N] [Public]
  1313. (cond ((eq x 0) 0)
  1314. ((math-zerop x) '(float 0 0))
  1315. (calc-symbolic-mode (signal 'inexact-result nil))
  1316. ((Math-numberp x)
  1317. (math-with-extra-prec 2
  1318. (let ((x (math-float x)))
  1319. (if (and (eq (car x) 'float)
  1320. (math-lessp-float x '(float 1 0))
  1321. (math-lessp-float '(float -1 0) x))
  1322. (math-exp-minus-1-raw x)
  1323. (math-add (math-exp-raw x) -1)))))
  1324. ((eq (car-safe x) 'sdev)
  1325. (if (math-constp x)
  1326. (let ((ex (calcFunc-expm1 (nth 1 x))))
  1327. (math-make-sdev ex (math-mul (nth 2 x) (math-add ex 1))))
  1328. (math-make-sdev (calcFunc-expm1 (nth 1 x))
  1329. (math-mul (nth 2 x) (calcFunc-exp (nth 1 x))))))
  1330. ((eq (car-safe x) 'intv)
  1331. (math-make-intv (nth 1 x)
  1332. (calcFunc-expm1 (nth 2 x))
  1333. (calcFunc-expm1 (nth 3 x))))
  1334. ((equal x '(var inf var-inf))
  1335. x)
  1336. ((equal x '(neg (var inf var-inf)))
  1337. -1)
  1338. ((equal x '(var nan var-nan))
  1339. x)
  1340. (t (calc-record-why 'numberp x)
  1341. (list 'calcFunc-expm1 x))))
  1342. (defun calcFunc-exp10 (x) ; [N N] [Public]
  1343. (if (eq x 0)
  1344. 1
  1345. (math-pow '(float 1 1) x)))
  1346. (defun math-exp-raw (x) ; [N N]
  1347. (cond ((math-zerop x) '(float 1 0))
  1348. (calc-symbolic-mode (signal 'inexact-result nil))
  1349. ((eq (car x) 'cplx)
  1350. (let ((expx (math-exp-raw (nth 1 x)))
  1351. (sc (math-sin-cos-raw (nth 2 x))))
  1352. (list 'cplx
  1353. (math-mul-float expx (cdr sc))
  1354. (math-mul-float expx (car sc)))))
  1355. ((eq (car x) 'polar)
  1356. (let ((xc (math-complex x)))
  1357. (list 'polar
  1358. (math-exp-raw (nth 1 xc))
  1359. (math-from-radians (nth 2 xc)))))
  1360. ((math-use-emacs-fn 'exp x))
  1361. ((or (math-lessp-float '(float 5 -1) x)
  1362. (math-lessp-float x '(float -5 -1)))
  1363. (if (math-lessp-float '(float 921035 1) x)
  1364. (math-overflow)
  1365. (if (math-lessp-float x '(float -921035 1))
  1366. (math-underflow)))
  1367. (let* ((two-x (math-mul-float x '(float 2 0)))
  1368. (hint (math-scale-int (nth 1 two-x) (nth 2 two-x)))
  1369. (hfrac (math-sub-float x (math-mul-float (math-float hint)
  1370. '(float 5 -1)))))
  1371. (math-mul-float (math-ipow (math-sqrt-e) hint)
  1372. (math-add-float '(float 1 0)
  1373. (math-exp-minus-1-raw hfrac)))))
  1374. (t (math-add-float '(float 1 0) (math-exp-minus-1-raw x)))))
  1375. (defun math-exp-minus-1-raw (x) ; [F F]
  1376. (math-exp-series x 2 3 x x))
  1377. (defun math-exp-series (sum nfac n xpow x)
  1378. (math-working "exp" sum)
  1379. (let* ((nextx (math-mul-float xpow x))
  1380. (nextsum (math-add-float sum (math-div-float nextx
  1381. (math-float nfac)))))
  1382. (if (math-nearly-equal-float sum nextsum)
  1383. sum
  1384. (math-exp-series nextsum (math-mul nfac n) (1+ n) nextx x))))
  1385. ;;; Logarithms.
  1386. (defun calcFunc-ln (x) ; [N N] [Public]
  1387. (cond ((math-zerop x)
  1388. (if calc-infinite-mode
  1389. '(neg (var inf var-inf))
  1390. (math-reject-arg x "*Logarithm of zero")))
  1391. ((eq x 1) 0)
  1392. ((Math-numberp x)
  1393. (math-with-extra-prec 2 (math-ln-raw (math-float x))))
  1394. ((eq (car-safe x) 'sdev)
  1395. (math-make-sdev (calcFunc-ln (nth 1 x))
  1396. (math-div (nth 2 x) (nth 1 x))))
  1397. ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x))
  1398. (Math-zerop (nth 2 x))
  1399. (not (math-intv-constp x))))
  1400. (let ((calc-infinite-mode t))
  1401. (math-make-intv (nth 1 x) (calcFunc-ln (nth 2 x))
  1402. (calcFunc-ln (nth 3 x)))))
  1403. ((equal x '(var e var-e))
  1404. 1)
  1405. ((and (eq (car-safe x) '^)
  1406. (equal (nth 1 x) '(var e var-e))
  1407. (math-known-realp (nth 2 x)))
  1408. (nth 2 x))
  1409. ((math-infinitep x)
  1410. (if (equal x '(var nan var-nan))
  1411. x
  1412. '(var inf var-inf)))
  1413. (t (calc-record-why 'numberp x)
  1414. (list 'calcFunc-ln x))))
  1415. (defun calcFunc-log10 (x) ; [N N] [Public]
  1416. (cond ((math-equal-int x 1)
  1417. (if (math-floatp x) '(float 0 0) 0))
  1418. ((and (Math-integerp x)
  1419. (math-posp x)
  1420. (let ((res (math-integer-log x 10)))
  1421. (and (car res)
  1422. (setq x (cdr res)))))
  1423. x)
  1424. ((and (eq (car-safe x) 'frac)
  1425. (eq (nth 1 x) 1)
  1426. (let ((res (math-integer-log (nth 2 x) 10)))
  1427. (and (car res)
  1428. (setq x (- (cdr res))))))
  1429. x)
  1430. ((math-zerop x)
  1431. (if calc-infinite-mode
  1432. '(neg (var inf var-inf))
  1433. (math-reject-arg x "*Logarithm of zero")))
  1434. (calc-symbolic-mode (signal 'inexact-result nil))
  1435. ((Math-numberp x)
  1436. (math-with-extra-prec 2
  1437. (let ((xf (math-float x)))
  1438. (if (eq (nth 1 xf) 0)
  1439. (math-reject-arg x "*Logarithm of zero"))
  1440. (if (Math-integer-posp (nth 1 xf))
  1441. (if (eq (nth 1 xf) 1) ; log10(1*10^n) = n
  1442. (math-float (nth 2 xf))
  1443. (let ((xdigs (1- (math-numdigs (nth 1 xf)))))
  1444. (math-add-float
  1445. (math-div-float (math-ln-raw-2
  1446. (list 'float (nth 1 xf) (- xdigs)))
  1447. (math-ln-10))
  1448. (math-float (+ (nth 2 xf) xdigs)))))
  1449. (math-div (calcFunc-ln xf) (math-ln-10))))))
  1450. ((eq (car-safe x) 'sdev)
  1451. (math-make-sdev (calcFunc-log10 (nth 1 x))
  1452. (math-div (nth 2 x)
  1453. (math-mul (nth 1 x) (math-ln-10)))))
  1454. ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x))
  1455. (not (math-intv-constp x))))
  1456. (math-make-intv (nth 1 x)
  1457. (calcFunc-log10 (nth 2 x))
  1458. (calcFunc-log10 (nth 3 x))))
  1459. ((math-infinitep x)
  1460. (if (equal x '(var nan var-nan))
  1461. x
  1462. '(var inf var-inf)))
  1463. (t (calc-record-why 'numberp x)
  1464. (list 'calcFunc-log10 x))))
  1465. (defun calcFunc-log (x &optional b) ; [N N N] [Public]
  1466. (cond ((or (null b) (equal b '(var e var-e)))
  1467. (math-normalize (list 'calcFunc-ln x)))
  1468. ((or (eq b 10) (equal b '(float 1 1)))
  1469. (math-normalize (list 'calcFunc-log10 x)))
  1470. ((math-zerop x)
  1471. (if calc-infinite-mode
  1472. (math-div (calcFunc-ln x) (calcFunc-ln b))
  1473. (math-reject-arg x "*Logarithm of zero")))
  1474. ((math-zerop b)
  1475. (if calc-infinite-mode
  1476. (math-div (calcFunc-ln x) (calcFunc-ln b))
  1477. (math-reject-arg b "*Logarithm of zero")))
  1478. ((math-equal-int b 1)
  1479. (if calc-infinite-mode
  1480. (math-div (calcFunc-ln x) 0)
  1481. (math-reject-arg b "*Logarithm base one")))
  1482. ((math-equal-int x 1)
  1483. (if (math-floatp b) '(float 0 0) 0))
  1484. ((and (Math-ratp x) (Math-ratp b)
  1485. (math-posp x) (math-posp b)
  1486. (let* ((sign 1) (inv nil)
  1487. (xx (if (Math-lessp 1 x)
  1488. x
  1489. (setq sign -1)
  1490. (math-div 1 x)))
  1491. (bb (if (Math-lessp 1 b)
  1492. b
  1493. (setq sign (- sign))
  1494. (math-div 1 b)))
  1495. (res (if (Math-lessp xx bb)
  1496. (setq inv (math-integer-log bb xx))
  1497. (math-integer-log xx bb))))
  1498. (and (car res)
  1499. (setq x (if inv
  1500. (math-div 1 (* sign (cdr res)))
  1501. (* sign (cdr res)))))))
  1502. x)
  1503. (calc-symbolic-mode (signal 'inexact-result nil))
  1504. ((and (Math-numberp x) (Math-numberp b))
  1505. (math-with-extra-prec 2
  1506. (math-div (math-ln-raw (math-float x))
  1507. (math-log-base-raw b))))
  1508. ((and (eq (car-safe x) 'sdev)
  1509. (Math-numberp b))
  1510. (math-make-sdev (calcFunc-log (nth 1 x) b)
  1511. (math-div (nth 2 x)
  1512. (math-mul (nth 1 x)
  1513. (math-log-base-raw b)))))
  1514. ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x))
  1515. (not (math-intv-constp x)))
  1516. (math-realp b))
  1517. (math-make-intv (nth 1 x)
  1518. (calcFunc-log (nth 2 x) b)
  1519. (calcFunc-log (nth 3 x) b)))
  1520. ((or (eq (car-safe x) 'intv) (eq (car-safe b) 'intv))
  1521. (math-div (calcFunc-ln x) (calcFunc-ln b)))
  1522. ((or (math-infinitep x)
  1523. (math-infinitep b))
  1524. (math-div (calcFunc-ln x) (calcFunc-ln b)))
  1525. (t (if (Math-numberp b)
  1526. (calc-record-why 'numberp x)
  1527. (calc-record-why 'numberp b))
  1528. (list 'calcFunc-log x b))))
  1529. (defun calcFunc-alog (x &optional b)
  1530. (cond ((or (null b) (equal b '(var e var-e)))
  1531. (math-normalize (list 'calcFunc-exp x)))
  1532. (t (math-pow b x))))
  1533. (defun calcFunc-ilog (x b)
  1534. (if (and (math-natnump x) (not (eq x 0))
  1535. (math-natnump b) (not (eq b 0)))
  1536. (if (eq b 1)
  1537. (math-reject-arg x "*Logarithm base one")
  1538. (if (Math-natnum-lessp x b)
  1539. 0
  1540. (cdr (math-integer-log x b))))
  1541. (math-floor (calcFunc-log x b))))
  1542. (defun math-integer-log (x b)
  1543. (let ((pows (list b))
  1544. (pow (math-sqr b))
  1545. next
  1546. sum n)
  1547. (while (not (Math-lessp x pow))
  1548. (setq pows (cons pow pows)
  1549. pow (math-sqr pow)))
  1550. (setq n (lsh 1 (1- (length pows)))
  1551. sum n
  1552. pow (car pows))
  1553. (while (and (setq pows (cdr pows))
  1554. (Math-lessp pow x))
  1555. (setq n (/ n 2)
  1556. next (math-mul pow (car pows)))
  1557. (or (Math-lessp x next)
  1558. (setq pow next
  1559. sum (+ sum n))))
  1560. (cons (equal pow x) sum)))
  1561. (defvar math-log-base-cache nil)
  1562. (defun math-log-base-raw (b) ; [N N]
  1563. (if (not (and (equal (car math-log-base-cache) b)
  1564. (eq (nth 1 math-log-base-cache) calc-internal-prec)))
  1565. (setq math-log-base-cache (list b calc-internal-prec
  1566. (math-ln-raw (math-float b)))))
  1567. (nth 2 math-log-base-cache))
  1568. (defun calcFunc-lnp1 (x) ; [N N] [Public]
  1569. (cond ((Math-equal-int x -1)
  1570. (if calc-infinite-mode
  1571. '(neg (var inf var-inf))
  1572. (math-reject-arg x "*Logarithm of zero")))
  1573. ((eq x 0) 0)
  1574. ((math-zerop x) '(float 0 0))
  1575. (calc-symbolic-mode (signal 'inexact-result nil))
  1576. ((Math-numberp x)
  1577. (math-with-extra-prec 2
  1578. (let ((x (math-float x)))
  1579. (if (and (eq (car x) 'float)
  1580. (math-lessp-float x '(float 5 -1))
  1581. (math-lessp-float '(float -5 -1) x))
  1582. (math-ln-plus-1-raw x)
  1583. (math-ln-raw (math-add-float x '(float 1 0)))))))
  1584. ((eq (car-safe x) 'sdev)
  1585. (math-make-sdev (calcFunc-lnp1 (nth 1 x))
  1586. (math-div (nth 2 x) (math-add (nth 1 x) 1))))
  1587. ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x))
  1588. (not (math-intv-constp x))))
  1589. (math-make-intv (nth 1 x)
  1590. (calcFunc-lnp1 (nth 2 x))
  1591. (calcFunc-lnp1 (nth 3 x))))
  1592. ((math-infinitep x)
  1593. (if (equal x '(var nan var-nan))
  1594. x
  1595. '(var inf var-inf)))
  1596. (t (calc-record-why 'numberp x)
  1597. (list 'calcFunc-lnp1 x))))
  1598. (defun math-ln-raw (x) ; [N N] --- must be float format!
  1599. (cond ((eq (car-safe x) 'cplx)
  1600. (list 'cplx
  1601. (math-mul-float (math-ln-raw
  1602. (math-add-float (math-sqr-float (nth 1 x))
  1603. (math-sqr-float (nth 2 x))))
  1604. '(float 5 -1))
  1605. (math-arctan2-raw (nth 2 x) (nth 1 x))))
  1606. ((eq (car x) 'polar)
  1607. (math-polar (list 'cplx
  1608. (math-ln-raw (nth 1 x))
  1609. (math-to-radians (nth 2 x)))))
  1610. ((Math-equal-int x 1)
  1611. '(float 0 0))
  1612. (calc-symbolic-mode (signal 'inexact-result nil))
  1613. ((math-posp (nth 1 x)) ; positive and real
  1614. (cond
  1615. ((math-use-emacs-fn 'log x))
  1616. (t
  1617. (let ((xdigs (1- (math-numdigs (nth 1 x)))))
  1618. (math-add-float (math-ln-raw-2 (list 'float (nth 1 x) (- xdigs)))
  1619. (math-mul-float (math-float (+ (nth 2 x) xdigs))
  1620. (math-ln-10)))))))
  1621. ((math-zerop x)
  1622. (math-reject-arg x "*Logarithm of zero"))
  1623. ((eq calc-complex-mode 'polar) ; negative and real
  1624. (math-polar
  1625. (list 'cplx ; negative and real
  1626. (math-ln-raw (math-neg-float x))
  1627. (math-pi))))
  1628. (t (list 'cplx ; negative and real
  1629. (math-ln-raw (math-neg-float x))
  1630. (math-pi)))))
  1631. (defun math-ln-raw-2 (x) ; [F F]
  1632. (cond ((math-lessp-float '(float 14 -1) x)
  1633. (math-add-float (math-ln-raw-2 (math-mul-float x '(float 5 -1)))
  1634. (math-ln-2)))
  1635. (t ; now .7 < x <= 1.4
  1636. (math-ln-raw-3 (math-div-float (math-sub-float x '(float 1 0))
  1637. (math-add-float x '(float 1 0)))))))
  1638. (defun math-ln-raw-3 (x) ; [F F]
  1639. (math-mul-float (math-ln-raw-series x 3 x (math-sqr-float x))
  1640. '(float 2 0)))
  1641. ;;; Compute ln((1+x)/(1-x))
  1642. (defun math-ln-raw-series (sum n x xsqr)
  1643. (math-working "log" sum)
  1644. (let* ((nextx (math-mul-float x xsqr))
  1645. (nextsum (math-add-float sum (math-div-float nextx (math-float n)))))
  1646. (if (math-nearly-equal-float sum nextsum)
  1647. sum
  1648. (math-ln-raw-series nextsum (+ n 2) nextx xsqr))))
  1649. (defun math-ln-plus-1-raw (x)
  1650. (math-lnp1-series x 2 x (math-neg x)))
  1651. (defun math-lnp1-series (sum n xpow x)
  1652. (math-working "lnp1" sum)
  1653. (let* ((nextx (math-mul-float xpow x))
  1654. (nextsum (math-add-float sum (math-div-float nextx (math-float n)))))
  1655. (if (math-nearly-equal-float sum nextsum)
  1656. sum
  1657. (math-lnp1-series nextsum (1+ n) nextx x))))
  1658. (defconst math-approx-ln-10
  1659. (math-read-number-simple "2.302585092994045684018")
  1660. "An approximation for ln(10).")
  1661. (math-defcache math-ln-10 math-approx-ln-10
  1662. (math-ln-raw-2 '(float 1 1)))
  1663. (defconst math-approx-ln-2
  1664. (math-read-number-simple "0.693147180559945309417")
  1665. "An approximation for ln(2).")
  1666. (math-defcache math-ln-2 math-approx-ln-2
  1667. (math-ln-raw-3 (math-float '(frac 1 3))))
  1668. ;;; Hyperbolic functions.
  1669. (defun calcFunc-sinh (x) ; [N N] [Public]
  1670. (cond ((eq x 0) 0)
  1671. (math-expand-formulas
  1672. (math-normalize
  1673. (list '/ (list '- (list 'calcFunc-exp x)
  1674. (list 'calcFunc-exp (list 'neg x))) 2)))
  1675. ((Math-numberp x)
  1676. (if calc-symbolic-mode (signal 'inexact-result nil))
  1677. (math-with-extra-prec 2
  1678. (let ((expx (math-exp-raw (math-float x))))
  1679. (math-mul (math-add expx (math-div -1 expx)) '(float 5 -1)))))
  1680. ((eq (car-safe x) 'sdev)
  1681. (math-make-sdev (calcFunc-sinh (nth 1 x))
  1682. (math-mul (nth 2 x) (calcFunc-cosh (nth 1 x)))))
  1683. ((eq (car x) 'intv)
  1684. (math-sort-intv (nth 1 x)
  1685. (calcFunc-sinh (nth 2 x))
  1686. (calcFunc-sinh (nth 3 x))))
  1687. ((or (equal x '(var inf var-inf))
  1688. (equal x '(neg (var inf var-inf)))
  1689. (equal x '(var nan var-nan)))
  1690. x)
  1691. (t (calc-record-why 'numberp x)
  1692. (list 'calcFunc-sinh x))))
  1693. (put 'calcFunc-sinh 'math-expandable t)
  1694. (defun calcFunc-cosh (x) ; [N N] [Public]
  1695. (cond ((eq x 0) 1)
  1696. (math-expand-formulas
  1697. (math-normalize
  1698. (list '/ (list '+ (list 'calcFunc-exp x)
  1699. (list 'calcFunc-exp (list 'neg x))) 2)))
  1700. ((Math-numberp x)
  1701. (if calc-symbolic-mode (signal 'inexact-result nil))
  1702. (math-with-extra-prec 2
  1703. (let ((expx (math-exp-raw (math-float x))))
  1704. (math-mul (math-add expx (math-div 1 expx)) '(float 5 -1)))))
  1705. ((eq (car-safe x) 'sdev)
  1706. (math-make-sdev (calcFunc-cosh (nth 1 x))
  1707. (math-mul (nth 2 x)
  1708. (calcFunc-sinh (nth 1 x)))))
  1709. ((and (eq (car x) 'intv) (math-intv-constp x))
  1710. (setq x (math-abs x))
  1711. (math-sort-intv (nth 1 x)
  1712. (calcFunc-cosh (nth 2 x))
  1713. (calcFunc-cosh (nth 3 x))))
  1714. ((or (equal x '(var inf var-inf))
  1715. (equal x '(neg (var inf var-inf)))
  1716. (equal x '(var nan var-nan)))
  1717. (math-abs x))
  1718. (t (calc-record-why 'numberp x)
  1719. (list 'calcFunc-cosh x))))
  1720. (put 'calcFunc-cosh 'math-expandable t)
  1721. (defun calcFunc-tanh (x) ; [N N] [Public]
  1722. (cond ((eq x 0) 0)
  1723. (math-expand-formulas
  1724. (math-normalize
  1725. (let ((expx (list 'calcFunc-exp x))
  1726. (expmx (list 'calcFunc-exp (list 'neg x))))
  1727. (math-normalize
  1728. (list '/ (list '- expx expmx) (list '+ expx expmx))))))
  1729. ((Math-numberp x)
  1730. (if calc-symbolic-mode (signal 'inexact-result nil))
  1731. (math-with-extra-prec 2
  1732. (let* ((expx (calcFunc-exp (math-float x)))
  1733. (expmx (math-div 1 expx)))
  1734. (math-div (math-sub expx expmx)
  1735. (math-add expx expmx)))))
  1736. ((eq (car-safe x) 'sdev)
  1737. (math-make-sdev (calcFunc-tanh (nth 1 x))
  1738. (math-div (nth 2 x)
  1739. (math-sqr (calcFunc-cosh (nth 1 x))))))
  1740. ((eq (car x) 'intv)
  1741. (math-sort-intv (nth 1 x)
  1742. (calcFunc-tanh (nth 2 x))
  1743. (calcFunc-tanh (nth 3 x))))
  1744. ((equal x '(var inf var-inf))
  1745. 1)
  1746. ((equal x '(neg (var inf var-inf)))
  1747. -1)
  1748. ((equal x '(var nan var-nan))
  1749. x)
  1750. (t (calc-record-why 'numberp x)
  1751. (list 'calcFunc-tanh x))))
  1752. (put 'calcFunc-tanh 'math-expandable t)
  1753. (defun calcFunc-sech (x) ; [N N] [Public]
  1754. (cond ((eq x 0) 1)
  1755. (math-expand-formulas
  1756. (math-normalize
  1757. (list '/ 2 (list '+ (list 'calcFunc-exp x)
  1758. (list 'calcFunc-exp (list 'neg x))))))
  1759. ((Math-numberp x)
  1760. (if calc-symbolic-mode (signal 'inexact-result nil))
  1761. (math-with-extra-prec 2
  1762. (let ((expx (math-exp-raw (math-float x))))
  1763. (math-div '(float 2 0) (math-add expx (math-div 1 expx))))))
  1764. ((eq (car-safe x) 'sdev)
  1765. (math-make-sdev (calcFunc-sech (nth 1 x))
  1766. (math-mul (nth 2 x)
  1767. (math-mul (calcFunc-sech (nth 1 x))
  1768. (calcFunc-tanh (nth 1 x))))))
  1769. ((and (eq (car x) 'intv) (math-intv-constp x))
  1770. (setq x (math-abs x))
  1771. (math-sort-intv (nth 1 x)
  1772. (calcFunc-sech (nth 2 x))
  1773. (calcFunc-sech (nth 3 x))))
  1774. ((or (equal x '(var inf var-inf))
  1775. (equal x '(neg (var inf var-inf))))
  1776. 0)
  1777. ((equal x '(var nan var-nan))
  1778. x)
  1779. (t (calc-record-why 'numberp x)
  1780. (list 'calcFunc-sech x))))
  1781. (put 'calcFunc-sech 'math-expandable t)
  1782. (defun calcFunc-csch (x) ; [N N] [Public]
  1783. (cond ((eq x 0) (math-div 1 0))
  1784. (math-expand-formulas
  1785. (math-normalize
  1786. (list '/ 2 (list '- (list 'calcFunc-exp x)
  1787. (list 'calcFunc-exp (list 'neg x))))))
  1788. ((Math-numberp x)
  1789. (if calc-symbolic-mode (signal 'inexact-result nil))
  1790. (math-with-extra-prec 2
  1791. (let ((expx (math-exp-raw (math-float x))))
  1792. (math-div '(float 2 0) (math-add expx (math-div -1 expx))))))
  1793. ((eq (car-safe x) 'sdev)
  1794. (math-make-sdev (calcFunc-csch (nth 1 x))
  1795. (math-mul (nth 2 x)
  1796. (math-mul (calcFunc-csch (nth 1 x))
  1797. (calcFunc-coth (nth 1 x))))))
  1798. ((eq (car x) 'intv)
  1799. (if (and (Math-negp (nth 2 x))
  1800. (Math-posp (nth 3 x)))
  1801. '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
  1802. (math-sort-intv (nth 1 x)
  1803. (calcFunc-csch (nth 2 x))
  1804. (calcFunc-csch (nth 3 x)))))
  1805. ((or (equal x '(var inf var-inf))
  1806. (equal x '(neg (var inf var-inf))))
  1807. 0)
  1808. ((equal x '(var nan var-nan))
  1809. x)
  1810. (t (calc-record-why 'numberp x)
  1811. (list 'calcFunc-csch x))))
  1812. (put 'calcFunc-csch 'math-expandable t)
  1813. (defun calcFunc-coth (x) ; [N N] [Public]
  1814. (cond ((eq x 0) (math-div 1 0))
  1815. (math-expand-formulas
  1816. (math-normalize
  1817. (let ((expx (list 'calcFunc-exp x))
  1818. (expmx (list 'calcFunc-exp (list 'neg x))))
  1819. (math-normalize
  1820. (list '/ (list '+ expx expmx) (list '- expx expmx))))))
  1821. ((Math-numberp x)
  1822. (if calc-symbolic-mode (signal 'inexact-result nil))
  1823. (math-with-extra-prec 2
  1824. (let* ((expx (calcFunc-exp (math-float x)))
  1825. (expmx (math-div 1 expx)))
  1826. (math-div (math-add expx expmx)
  1827. (math-sub expx expmx)))))
  1828. ((eq (car-safe x) 'sdev)
  1829. (math-make-sdev (calcFunc-coth (nth 1 x))
  1830. (math-div (nth 2 x)
  1831. (math-sqr (calcFunc-sinh (nth 1 x))))))
  1832. ((eq (car x) 'intv)
  1833. (if (and (Math-negp (nth 2 x))
  1834. (Math-posp (nth 3 x)))
  1835. '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
  1836. (math-sort-intv (nth 1 x)
  1837. (calcFunc-coth (nth 2 x))
  1838. (calcFunc-coth (nth 3 x)))))
  1839. ((equal x '(var inf var-inf))
  1840. 1)
  1841. ((equal x '(neg (var inf var-inf)))
  1842. -1)
  1843. ((equal x '(var nan var-nan))
  1844. x)
  1845. (t (calc-record-why 'numberp x)
  1846. (list 'calcFunc-coth x))))
  1847. (put 'calcFunc-coth 'math-expandable t)
  1848. (defun calcFunc-arcsinh (x) ; [N N] [Public]
  1849. (cond ((eq x 0) 0)
  1850. (math-expand-formulas
  1851. (math-normalize
  1852. (list 'calcFunc-ln (list '+ x (list 'calcFunc-sqrt
  1853. (list '+ (list '^ x 2) 1))))))
  1854. ((Math-numberp x)
  1855. (if calc-symbolic-mode (signal 'inexact-result nil))
  1856. (math-with-extra-prec 2
  1857. (math-ln-raw (math-add x (math-sqrt-raw (math-add (math-sqr x)
  1858. '(float 1 0)))))))
  1859. ((eq (car-safe x) 'sdev)
  1860. (math-make-sdev (calcFunc-arcsinh (nth 1 x))
  1861. (math-div (nth 2 x)
  1862. (math-sqrt
  1863. (math-add (math-sqr (nth 1 x)) 1)))))
  1864. ((eq (car x) 'intv)
  1865. (math-sort-intv (nth 1 x)
  1866. (calcFunc-arcsinh (nth 2 x))
  1867. (calcFunc-arcsinh (nth 3 x))))
  1868. ((or (equal x '(var inf var-inf))
  1869. (equal x '(neg (var inf var-inf)))
  1870. (equal x '(var nan var-nan)))
  1871. x)
  1872. (t (calc-record-why 'numberp x)
  1873. (list 'calcFunc-arcsinh x))))
  1874. (put 'calcFunc-arcsinh 'math-expandable t)
  1875. (defun calcFunc-arccosh (x) ; [N N] [Public]
  1876. (cond ((eq x 1) 0)
  1877. ((and (eq x -1) calc-symbolic-mode)
  1878. '(var pi var-pi))
  1879. ((and (eq x 0) calc-symbolic-mode)
  1880. (math-div (math-mul '(var pi var-pi) '(var i var-i)) 2))
  1881. (math-expand-formulas
  1882. (math-normalize
  1883. (list 'calcFunc-ln (list '+ x (list 'calcFunc-sqrt
  1884. (list '- (list '^ x 2) 1))))))
  1885. ((Math-numberp x)
  1886. (if calc-symbolic-mode (signal 'inexact-result nil))
  1887. (if (Math-equal-int x -1)
  1888. (math-imaginary (math-pi))
  1889. (math-with-extra-prec 2
  1890. (if (or t ; need to do this even in the real case!
  1891. (memq (car-safe x) '(cplx polar)))
  1892. (let ((xp1 (math-add 1 x))) ; this gets the branch cuts right
  1893. (math-ln-raw
  1894. (math-add x (math-mul xp1
  1895. (math-sqrt-raw
  1896. (math-div (math-sub
  1897. x
  1898. '(float 1 0))
  1899. xp1))))))
  1900. (math-ln-raw
  1901. (math-add x (math-sqrt-raw (math-add (math-sqr x)
  1902. '(float -1 0)))))))))
  1903. ((eq (car-safe x) 'sdev)
  1904. (math-make-sdev (calcFunc-arccosh (nth 1 x))
  1905. (math-div (nth 2 x)
  1906. (math-sqrt
  1907. (math-add (math-sqr (nth 1 x)) -1)))))
  1908. ((eq (car x) 'intv)
  1909. (math-sort-intv (nth 1 x)
  1910. (calcFunc-arccosh (nth 2 x))
  1911. (calcFunc-arccosh (nth 3 x))))
  1912. ((or (equal x '(var inf var-inf))
  1913. (equal x '(neg (var inf var-inf)))
  1914. (equal x '(var nan var-nan)))
  1915. x)
  1916. (t (calc-record-why 'numberp x)
  1917. (list 'calcFunc-arccosh x))))
  1918. (put 'calcFunc-arccosh 'math-expandable t)
  1919. (defun calcFunc-arctanh (x) ; [N N] [Public]
  1920. (cond ((eq x 0) 0)
  1921. ((and (Math-equal-int x 1) calc-infinite-mode)
  1922. '(var inf var-inf))
  1923. ((and (Math-equal-int x -1) calc-infinite-mode)
  1924. '(neg (var inf var-inf)))
  1925. (math-expand-formulas
  1926. (list '/ (list '-
  1927. (list 'calcFunc-ln (list '+ 1 x))
  1928. (list 'calcFunc-ln (list '- 1 x))) 2))
  1929. ((Math-numberp x)
  1930. (if calc-symbolic-mode (signal 'inexact-result nil))
  1931. (math-with-extra-prec 2
  1932. (if (or (memq (car-safe x) '(cplx polar))
  1933. (Math-lessp 1 x))
  1934. (math-mul (math-sub (math-ln-raw (math-add '(float 1 0) x))
  1935. (math-ln-raw (math-sub '(float 1 0) x)))
  1936. '(float 5 -1))
  1937. (if (and (math-equal-int x 1) calc-infinite-mode)
  1938. '(var inf var-inf)
  1939. (if (and (math-equal-int x -1) calc-infinite-mode)
  1940. '(neg (var inf var-inf))
  1941. (math-mul (math-ln-raw (math-div (math-add '(float 1 0) x)
  1942. (math-sub 1 x)))
  1943. '(float 5 -1)))))))
  1944. ((eq (car-safe x) 'sdev)
  1945. (math-make-sdev (calcFunc-arctanh (nth 1 x))
  1946. (math-div (nth 2 x)
  1947. (math-sub 1 (math-sqr (nth 1 x))))))
  1948. ((eq (car x) 'intv)
  1949. (math-sort-intv (nth 1 x)
  1950. (calcFunc-arctanh (nth 2 x))
  1951. (calcFunc-arctanh (nth 3 x))))
  1952. ((equal x '(var nan var-nan))
  1953. x)
  1954. (t (calc-record-why 'numberp x)
  1955. (list 'calcFunc-arctanh x))))
  1956. (put 'calcFunc-arctanh 'math-expandable t)
  1957. ;;; Convert A from HMS or degrees to radians.
  1958. (defun calcFunc-rad (a) ; [R R] [Public]
  1959. (cond ((or (Math-numberp a)
  1960. (eq (car a) 'intv))
  1961. (math-with-extra-prec 2
  1962. (math-mul a (math-pi-over-180))))
  1963. ((eq (car a) 'hms)
  1964. (math-from-hms a 'rad))
  1965. ((eq (car a) 'sdev)
  1966. (math-make-sdev (calcFunc-rad (nth 1 a))
  1967. (calcFunc-rad (nth 2 a))))
  1968. (math-expand-formulas
  1969. (math-div (math-mul a '(var pi var-pi)) 180))
  1970. ((math-infinitep a) a)
  1971. (t (list 'calcFunc-rad a))))
  1972. (put 'calcFunc-rad 'math-expandable t)
  1973. ;;; Convert A from HMS or radians to degrees.
  1974. (defun calcFunc-deg (a) ; [R R] [Public]
  1975. (cond ((or (Math-numberp a)
  1976. (eq (car a) 'intv))
  1977. (math-with-extra-prec 2
  1978. (math-div a (math-pi-over-180))))
  1979. ((eq (car a) 'hms)
  1980. (math-from-hms a 'deg))
  1981. ((eq (car a) 'sdev)
  1982. (math-make-sdev (calcFunc-deg (nth 1 a))
  1983. (calcFunc-deg (nth 2 a))))
  1984. (math-expand-formulas
  1985. (math-div (math-mul 180 a) '(var pi var-pi)))
  1986. ((math-infinitep a) a)
  1987. (t (list 'calcFunc-deg a))))
  1988. (put 'calcFunc-deg 'math-expandable t)
  1989. (provide 'calc-math)
  1990. ;;; calc-math.el ends here