home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume15 / calc-1.05 / part08 < prev    next >
Lisp/Scheme  |  1990-10-14  |  56KB  |  1,854 lines

  1. Newsgroups: comp.sources.misc
  2. X-UNIX-From: daveg@csvax.cs.caltech.edu
  3. subject: v15i035: Patch for GNU Emacs Calc, version 1.04 -> 1.05, part 08/20
  4. from: daveg@csvax.cs.caltech.edu (David Gillespie)
  5. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  6.  
  7. Posting-number: Volume 15, Issue 35
  8. Submitted-by: daveg@csvax.cs.caltech.edu (David Gillespie)
  9. Archive-name: calc-1.05/part08
  10.  
  11. #!/bin/sh
  12. # this is part 8 of a multipart archive
  13. # do not concatenate these parts, unpack them in order with /bin/sh
  14. # file calc.patch continued
  15. #
  16. CurArch=8
  17. if test ! -r s2_seq_.tmp
  18. then echo "Please unpack part 1 first!"
  19.      exit 1; fi
  20. ( read Scheck
  21.   if test "$Scheck" != $CurArch
  22.   then echo "Please unpack part $Scheck next!"
  23.        exit 1;
  24.   else exit 0; fi
  25. ) < s2_seq_.tmp || exit 1
  26. sed 's/^X//' << 'SHAR_EOF' >> calc.patch
  27. X!           (while (<= (setq n (1+ n)) 0)
  28. X!         (setq vec (cons start vec)
  29. X!               start (math-mul start (or incr 2)))))
  30. X!         (setq vec (nreverse vec)))
  31. X!     (if (>= n 0)
  32. X!         (while (> n 0)
  33. X!           (setq vec (cons n vec)
  34. X!             n (1- n)))
  35. X!       (let ((i -1))
  36. X!         (while (>= i n)
  37. X!           (setq vec (cons i vec)
  38. X!             i (1- i))))))
  39. X!       (cons 'vec vec)))
  40. X  )
  41. X  (fset 'calcFunc-index (symbol-function 'math-vec-index))
  42. X  
  43. X+ ;;; Find an element in a vector.
  44. X+ (defun math-vec-find (vec x &optional start)
  45. X+   (setq start (if start (math-check-fixnum start) 1))
  46. X+   (if (< start 1) (math-reject-arg start 'posp))
  47. X+   (setq vec (nthcdr start vec))
  48. X+   (let ((n start))
  49. X+     (while (and vec (not (math-equal x (car vec))))
  50. X+       (setq n (1+ n)
  51. X+         vec (cdr vec)))
  52. X+     (if vec n 0))
  53. X+ )
  54. X+ (fset 'calcFunc-find (symbol-function 'math-vec-find))
  55. X+ 
  56. X+ ;;; Return a subvector of a vector.
  57. X+ (defun math-subvector (vec start &optional end)
  58. X+   (setq start (math-check-fixnum start)
  59. X+     end (math-check-fixnum (or end 0)))
  60. X+   (or (math-vectorp vec) (math-reject-arg vec 'vectorp))
  61. X+   (let ((len (1- (length vec))))
  62. X+     (if (<= start 0)
  63. X+     (setq start (+ len start 1)))
  64. X+     (if (<= end 0)
  65. X+     (setq end (+ len end 1)))
  66. X+     (if (or (> start len)
  67. X+         (<= end start))
  68. X+     '(vec)
  69. X+       (setq vec (nthcdr start vec))
  70. X+       (if (<= end len)
  71. X+       (let ((chop (nthcdr (- end start 1) (setq vec (copy-sequence vec)))))
  72. X+         (setcdr chop nil)))
  73. X+       (cons 'vec vec)))
  74. X+ )
  75. X+ (fset 'calcFunc-subvec (symbol-function 'math-subvector))
  76. X+ 
  77. X+ ;;; Reverse the order of the elements of a vector.
  78. X+ (defun math-reverse-vector (vec)
  79. X+   (if (math-vectorp vec)
  80. X+       (cons 'vec (reverse (cdr vec)))
  81. X+     (math-reject-arg vec 'vectorp))
  82. X+ )
  83. X+ (fset 'calcFunc-rev (symbol-function 'math-reverse-vector))
  84. X+ 
  85. X+ ;;; Compress a vector according to a mask vector.
  86. X+ (defun math-vec-compress (mask vec)
  87. X+   (if (math-numberp mask)
  88. X+       (if (math-zerop mask)
  89. X+       '(vec)
  90. X+     vec)
  91. X+     (or (math-vectorp mask) (math-reject-arg mask 'vectorp))
  92. X+     (or (math-constp mask) (math-reject-arg mask 'constp))
  93. X+     (or (math-vectorp vec) (math-reject-arg vec 'vectorp))
  94. X+     (or (= (length mask) (length vec)) (math-dimension-error))
  95. X+     (let ((new nil))
  96. X+       (while (setq mask (cdr mask) vec (cdr vec))
  97. X+     (or (math-zerop (car mask))
  98. X+         (setq new (cons (car vec) new))))
  99. X+       (cons 'vec (nreverse new))))
  100. X+ )
  101. X+ (fset 'calcFunc-vmask (symbol-function 'math-vec-compress))
  102. X+ 
  103. X+ ;;; Expand a vector according to a mask vector.
  104. X+ (defun math-vec-expand (mask vec &optional filler)
  105. X+   (or (math-vectorp mask) (math-reject-arg mask 'vectorp))
  106. X+   (or (math-constp mask) (math-reject-arg mask 'constp))
  107. X+   (or (math-vectorp vec) (math-reject-arg vec 'vectorp))
  108. X+   (setq vec (cdr vec))
  109. X+   (let ((new nil))
  110. X+     (while (setq mask (cdr mask))
  111. X+       (if (math-zerop (car mask))
  112. X+       (setq new (cons (or filler (car mask)) new))
  113. X+     (setq new (cons (or (car vec) (car mask)) new)
  114. X+           vec (cdr vec))))
  115. X+     (cons 'vec (nreverse new)))
  116. X+ )
  117. X+ (fset 'calcFunc-vexp (symbol-function 'math-vec-expand))
  118. X+ 
  119. X  
  120. X  ;;; Compute the row and column norms of a vector or matrix.  [Public]
  121. X  (defun math-rnorm (a)
  122. X***************
  123. X*** 6824,6832 ****
  124. X  )
  125. X  (fset 'calcFunc-rsort (symbol-function 'math-rsort-vector))
  126. X  
  127. X  
  128. X  ;;; Compile a histogram of data from a vector.
  129. X! (defun math-histogram (vec wts n)
  130. X    (or (Math-vectorp vec)
  131. X        (math-reject-arg vec 'vectorp))
  132. X    (if (Math-vectorp wts)
  133. X--- 11121,11151 ----
  134. X  )
  135. X  (fset 'calcFunc-rsort (symbol-function 'math-rsort-vector))
  136. X  
  137. X+ (defun math-grade-up-vector (grade-vec)
  138. X+   (if (math-vectorp grade-vec)
  139. X+       (let* ((len (1- (length grade-vec))))
  140. X+     (cons 'vec (sort (cdr (math-vec-index len)) 'math-grade-beforep)))
  141. X+     (math-reject-arg grade-vec 'vectorp))
  142. X+ )
  143. X+ (fset 'calcFunc-grade (symbol-function 'math-grade-up-vector))
  144. X+ 
  145. X+ (defun math-grade-down-vector (grade-vec)
  146. X+   (if (math-vectorp grade-vec)
  147. X+       (let* ((len (1- (length grade-vec))))
  148. X+     (cons 'vec (nreverse (sort (cdr (math-vec-index len))
  149. X+                    'math-grade-beforep))))
  150. X+     (math-reject-arg grade-vec 'vectorp))
  151. X+ )
  152. X+ (fset 'calcFunc-rgrade (symbol-function 'math-grade-down-vector))
  153. X+ 
  154. X+ (defun math-grade-beforep (i j)
  155. X+   (math-beforep (nth i grade-vec) (nth j grade-vec))
  156. X+ )
  157. X+ 
  158. X  
  159. X  ;;; Compile a histogram of data from a vector.
  160. X! (defun math-histogram (vec wts &optional n)
  161. X!   (or n (setq n wts wts 1))
  162. X    (or (Math-vectorp vec)
  163. X        (math-reject-arg vec 'vectorp))
  164. X    (if (Math-vectorp wts)
  165. X***************
  166. X*** 7064,7069 ****
  167. X--- 11383,11390 ----
  168. X    m
  169. X  )
  170. X  
  171. X+ ;;;; [calc-arith.el]
  172. X+ 
  173. X  (defun math-abs-approx (a)
  174. X    (cond ((Math-negp a)
  175. X       (math-neg a))
  176. X***************
  177. X*** 7078,7089 ****
  178. X      ((eq (car a) 'intv)
  179. X       (math-max (math-abs (nth 2 a)) (math-abs (nth 3 a))))
  180. X      ((eq (car a) 'vec)
  181. X!      (math-cnorm a))
  182. X      ((eq (car a) 'calcFunc-abs)
  183. X       (car a))
  184. X      (t a))
  185. X  )
  186. X  
  187. X  (defun math-lud-solve (lud b)
  188. X    (and lud
  189. X       (let* ((x (math-copy-matrix b))
  190. X--- 11399,11416 ----
  191. X      ((eq (car a) 'intv)
  192. X       (math-max (math-abs (nth 2 a)) (math-abs (nth 3 a))))
  193. X      ((eq (car a) 'vec)
  194. X!      (math-reduce-vec 'math-add-abs-approx a))
  195. X      ((eq (car a) 'calcFunc-abs)
  196. X       (car a))
  197. X      (t a))
  198. X  )
  199. X  
  200. X+ (defun math-add-abs-approx (a b)
  201. X+   (math-add (math-abs-approx a) (math-abs-approx b))
  202. X+ )
  203. X+ 
  204. X+ ;;;; [calc-mat.el]
  205. X+ 
  206. X  (defun math-lud-solve (lud b)
  207. X    (and lud
  208. X       (let* ((x (math-copy-matrix b))
  209. X***************
  210. X*** 7386,7392 ****
  211. X  
  212. X  ;;;; Interval forms.
  213. X  
  214. X! ;;; Build an interval form.  [X I X X]
  215. X  (defun math-make-intv (mask lo hi)
  216. X    (if (memq (car-safe lo) '(cplx polar mod sdev intv vec))
  217. X        (math-reject-arg lo 'realp))
  218. X--- 11713,11719 ----
  219. X  
  220. X  ;;;; Interval forms.
  221. X  
  222. X! ;;; Build an interval form.  [X S X X]
  223. X  (defun math-make-intv (mask lo hi)
  224. X    (if (memq (car-safe lo) '(cplx polar mod sdev intv vec))
  225. X        (math-reject-arg lo 'realp))
  226. X***************
  227. X*** 7405,7410 ****
  228. X--- 11732,11743 ----
  229. X          (list 'intv mask lo hi))))
  230. X      (list 'intv mask lo hi))
  231. X  )
  232. X+ (defun calcFunc-intv (mask lo hi)
  233. X+   (if (math-messy-integerp mask) (setq mask (math-trunc mask)))
  234. X+   (or (and (integerp mask) (>= mask 0) (<= mask 3))
  235. X+       (math-reject-arg mask 'range))
  236. X+   (math-make-intv mask lo hi)
  237. X+ )
  238. X  
  239. X  (defun math-sort-intv (mask lo hi)
  240. X    (if (Math-lessp hi lo)
  241. X***************
  242. X*** 7768,7784 ****
  243. X  (defun math-want-polar (a b)
  244. X    (cond ((eq (car-safe a) 'polar)
  245. X       (if (eq (car-safe b) 'cplx)
  246. X!          (eq car-complex-mode 'polar)
  247. X         t))
  248. X      ((eq (car-safe a) 'cplx)
  249. X       (if (eq (car-safe b) 'polar)
  250. X!          (eq car-complex-mode 'polar)
  251. X         nil))
  252. X      ((eq (car-safe b) 'polar)
  253. X       t)
  254. X      ((eq (car-safe b) 'cplx)
  255. X       nil)
  256. X!     (t (eq (car-complex-mode 'polar))))
  257. X  )
  258. X  
  259. X  ;;; Force A to be in the (-pi,pi] or (-180,180] range.
  260. X--- 12101,12117 ----
  261. X  (defun math-want-polar (a b)
  262. X    (cond ((eq (car-safe a) 'polar)
  263. X       (if (eq (car-safe b) 'cplx)
  264. X!          (eq calc-complex-mode 'polar)
  265. X         t))
  266. X      ((eq (car-safe a) 'cplx)
  267. X       (if (eq (car-safe b) 'polar)
  268. X!          (eq calc-complex-mode 'polar)
  269. X         nil))
  270. X      ((eq (car-safe b) 'polar)
  271. X       t)
  272. X      ((eq (car-safe b) 'cplx)
  273. X       nil)
  274. X!     (t (eq calc-complex-mode 'polar)))
  275. X  )
  276. X  
  277. X  ;;; Force A to be in the (-pi,pi] or (-180,180] range.
  278. X***************
  279. X*** 8019,8031 ****
  280. X  
  281. X  ;;;; [calc-arith.el]
  282. X  
  283. X  (defun math-pow-fancy (a b)
  284. X    (cond ((and (Math-numberp a) (Math-numberp b))
  285. X!      (cond ((and (eq (car-safe b) 'frac)
  286. X!              (equal (nth 2 b) 2))
  287. X!         (math-ipow (math-sqrt-raw (math-float a)) (nth 1 b)))
  288. X!            ((equal b '(float 5 -1))
  289. X!         (math-sqrt-raw (math-float a)))
  290. X             (t
  291. X          (math-with-extra-prec 2
  292. X            (math-exp-raw
  293. X--- 12352,12379 ----
  294. X  
  295. X  ;;;; [calc-arith.el]
  296. X  
  297. X+ (defun math-mod-fancy (a b)
  298. X+   (cond ((and (eq (car-safe a) 'mod) (Math-realp b) (math-posp b))
  299. X+      (math-make-mod (nth 1 a) b))
  300. X+     ((and (eq (car-safe a) 'intv) (math-constp a) (math-posp b))
  301. X+      (math-mod-intv a b))
  302. X+     (t
  303. X+      (if (Math-anglep a)
  304. X+          (calc-record-why 'anglep b)
  305. X+        (calc-record-why 'anglep a))
  306. X+      (list '% a b)))
  307. X+ )
  308. X+ 
  309. X  (defun math-pow-fancy (a b)
  310. X    (cond ((and (Math-numberp a) (Math-numberp b))
  311. X!      (cond ((memq (math-quarter-integer b) '(1 2 3))
  312. X!         (math-pow (math-sqrt (if (math-floatp b) (math-float a) a))
  313. X!               (math-mul 2 b)))
  314. X!            ((and (eq (car b) 'frac)
  315. X!              (integerp (nth 2 b))
  316. X!              (<= (nth 2 b) 10))
  317. X!         (math-ipow (math-nth-root a (nth 2 b))
  318. X!                (nth 1 b)))
  319. X             (t
  320. X          (math-with-extra-prec 2
  321. X            (math-exp-raw
  322. X***************
  323. X*** 8141,8146 ****
  324. X--- 12489,12523 ----
  325. X       (math-reject-arg b 'numberp)))
  326. X  )
  327. X  
  328. X+ (defun math-quarter-integer (x)
  329. X+   (if (Math-integerp x)
  330. X+       0
  331. X+     (if (math-negp x)
  332. X+     (progn
  333. X+       (setq x (math-quarter-integer (math-neg x)))
  334. X+       (and x (- 4 x)))
  335. X+       (if (eq (car x) 'frac)
  336. X+       (if (eq (nth 2 x) 2)
  337. X+           2
  338. X+         (and (eq (nth 2 x) 4)
  339. X+          (progn
  340. X+            (setq x (nth 1 x))
  341. X+            (% (if (consp x) (nth 1 x) x) 4))))
  342. X+     (if (eq (car x) 'float)
  343. X+         (if (>= (nth 2 x) 0)
  344. X+         0
  345. X+           (if (= (nth 2 x) -1)
  346. X+           (progn
  347. X+             (setq x (nth 1 x))
  348. X+             (and (= (% (if (consp x) (nth 1 x) x) 10) 5) 2))
  349. X+         (if (= (nth 2 x) -2)
  350. X+             (progn
  351. X+               (setq x (nth 1 x)
  352. X+                 x (% (if (consp x) (nth 1 x) x) 100))
  353. X+               (if (= x 25) 1
  354. X+             (if (= x 75) 3))))))))))
  355. X+ )
  356. X+ 
  357. X  ;;; This assumes A < M and M > 0.
  358. X  (defun math-pow-mod (a b m)   ; [R R R R]
  359. X    (if (and (Math-integerp a) (Math-integerp b) (Math-integerp m))
  360. X***************
  361. X*** 8223,8237 ****
  362. X  ;;; with an overestimate always works, even using truncating integer division!
  363. X  (defun math-isqrt (a)
  364. X    (cond ((Math-zerop a) a)
  365. X!     ((Math-negp a)
  366. X!      (math-imaginary (math-isqrt (math-neg a))))
  367. X      ((integerp a)
  368. X       (math-isqrt-small a))
  369. X-     ((eq (car a) 'bigpos)
  370. X-      (math-normalize (cons 'bigpos (cdr (math-isqrt-bignum (cdr a))))))
  371. X      (t
  372. X!      (math-floor (math-sqrt a))))
  373. X  )
  374. X  
  375. X  ;;; This returns (flag . result) where the flag is T if A is a perfect square.
  376. X  (defun math-isqrt-bignum (a)   ; [P.l L]
  377. X--- 12600,12613 ----
  378. X  ;;; with an overestimate always works, even using truncating integer division!
  379. X  (defun math-isqrt (a)
  380. X    (cond ((Math-zerop a) a)
  381. X!     ((not (math-num-natnump a))
  382. X!      (math-reject-arg a 'natnump))
  383. X      ((integerp a)
  384. X       (math-isqrt-small a))
  385. X      (t
  386. X!      (math-normalize (cons 'bigpos (cdr (math-isqrt-bignum (cdr a)))))))
  387. X  )
  388. X+ (fset 'calcFunc-isqrt (symbol-function 'math-isqrt))
  389. X  
  390. X  ;;; This returns (flag . result) where the flag is T if A is a perfect square.
  391. X  (defun math-isqrt-bignum (a)   ; [P.l L]
  392. X***************
  393. X*** 8267,8272 ****
  394. X--- 12643,12655 ----
  395. X          guess)))
  396. X  )
  397. X  
  398. X+ (defun math-zerop-bignum (a)
  399. X+   (and (eq (car a) 0)
  400. X+        (progn
  401. X+      (while (eq (car (setq a (cdr a))) 0))
  402. X+      (null a)))
  403. X+ )
  404. X+ 
  405. X  (defun math-scale-bignum-3 (a n)   ; [L L S]
  406. X    (while (> n 0)
  407. X      (setq a (cons 0 a)
  408. X***************
  409. X*** 8285,8290 ****
  410. X--- 12668,12674 ----
  411. X  )
  412. X  
  413. X  
  414. X+ 
  415. X  ;;;; [calc-ext.el]
  416. X  
  417. X  (defun math-inexact-result ()
  418. X***************
  419. X*** 8395,8413 ****
  420. X  
  421. X  ;;; True if A and B differ only in the last digit of precision.  [P F F]
  422. X  (defun math-nearly-equal-float (a b)
  423. X!   (let ((diff (nth 1 (math-sub-float a b))))
  424. X!     (or (eq diff 0)
  425. X!     (and (not (consp diff))
  426. X!          (< diff 10)
  427. X!          (> diff -10)
  428. X!          (= diff (if (< (nth 2 a) (nth 2 b))
  429. X!              (nth 2 a) (nth 2 b)))
  430. X!          (or (= (math-numdigs (nth 1 a)) calc-internal-prec)
  431. X!          (= (math-numdigs (nth 1 b)) calc-internal-prec)))))
  432. X! )
  433. X! 
  434. X! (defun math-nearly-equal (a b)   ;  [P R R] [Public]
  435. X!   (math-nearly-equal-float (math-float a) (math-float b))
  436. X  )
  437. X  
  438. X  ;;; True if A is nearly zero compared to B.  [P F F]
  439. X--- 12779,12823 ----
  440. X  
  441. X  ;;; True if A and B differ only in the last digit of precision.  [P F F]
  442. X  (defun math-nearly-equal-float (a b)
  443. X!   (let ((ediff (- (nth 2 a) (nth 2 b))))
  444. X!     (cond ((= ediff 0)   ;; Expanded out for speed
  445. X!        (setq ediff (math-add (Math-integer-neg (nth 1 a)) (nth 1 b)))
  446. X!        (or (eq ediff 0)
  447. X!            (and (not (consp ediff))
  448. X!             (< ediff 10)
  449. X!             (> ediff -10)
  450. X!             (= (math-numdigs (nth 1 a)) calc-internal-prec))))
  451. X!       ((= ediff 1)
  452. X!        (setq ediff (math-add (Math-integer-neg (nth 1 b))
  453. X!                  (math-scale-int (nth 1 a) 1)))
  454. X!        (and (not (consp ediff))
  455. X!         (< ediff 10)
  456. X!         (> ediff -10)
  457. X!         (= (math-numdigs (nth 1 b)) calc-internal-prec)))
  458. X!       ((= ediff -1)
  459. X!        (setq ediff (math-add (Math-integer-neg (nth 1 a))
  460. X!                  (math-scale-int (nth 1 b) 1)))
  461. X!        (and (not (consp ediff))
  462. X!         (< ediff 10)
  463. X!         (> ediff -10)
  464. X!         (= (math-numdigs (nth 1 a)) calc-internal-prec)))))
  465. X! )
  466. X! 
  467. X! (defun math-nearly-equal (a b)   ;  [P N N] [Public]
  468. X!   (setq a (math-float a))
  469. X!   (setq b (math-float b))
  470. X!   (if (eq (car a) 'polar) (setq a (math-complex a)))
  471. X!   (if (eq (car b) 'polar) (setq b (math-complex b)))
  472. X!   (if (eq (car a) 'cplx)
  473. X!       (if (eq (car b) 'cplx)
  474. X!       (and (math-nearly-equal-float (nth 1 a) (nth 1 b))
  475. X!            (math-nearly-equal-float (nth 2 a) (nth 2 b)))
  476. X!     (and (math-nearly-equal-float (nth 1 a) b)
  477. X!          (math-nearly-zerop-float (nth 2 a) b)))
  478. X!       (if (eq (car b) 'cplx)
  479. X!       (and (math-nearly-equal-float a (nth 1 b))
  480. X!            (math-nearly-zerop-float a (nth 2 b)))
  481. X!     (math-nearly-equal-float a b)))
  482. X  )
  483. X  
  484. X  ;;; True if A is nearly zero compared to B.  [P F F]
  485. X***************
  486. X*** 8417,8424 ****
  487. X        (1+ (- (+ (math-numdigs (nth 1 b)) (nth 2 b)) calc-internal-prec))))
  488. X  )
  489. X  
  490. X! (defun math-nearly-zerop (a b)
  491. X!   (math-nearly-zerop-float (math-float a) (math-float b))
  492. X  )
  493. X  
  494. X  ;;; This implementation could be improved, accuracy-wise.
  495. X--- 12827,12841 ----
  496. X        (1+ (- (+ (math-numdigs (nth 1 b)) (nth 2 b)) calc-internal-prec))))
  497. X  )
  498. X  
  499. X! (defun math-nearly-zerop (a b)   ; [P N R] [Public]
  500. X!   (setq a (math-float a))
  501. X!   (setq b (math-float b))
  502. X!   (if (eq (car a) 'cplx)
  503. X!       (and (math-nearly-zerop-float (nth 1 a) b)
  504. X!        (math-nearly-zerop-float (nth 2 a) b))
  505. X!     (if (eq (car a) 'polar)
  506. X!     (math-nearly-zerop-float (nth 1 a) b)
  507. X!       (math-nearly-zerop-float a b)))
  508. X  )
  509. X  
  510. X  ;;; This implementation could be improved, accuracy-wise.
  511. X***************
  512. X*** 8451,8456 ****
  513. X--- 12868,12953 ----
  514. X  
  515. X  
  516. X  
  517. X+ (defun math-nth-root (a n)
  518. X+   (or
  519. X+    (and (= n 2) (math-sqrt a))
  520. X+    (and (Math-zerop a) a)
  521. X+    (and (Math-negp a)
  522. X+     (math-pow a (math-div-float '(float 1 0) (math-float n))))
  523. X+    (and (Math-integerp a)
  524. X+     (let ((root (math-nth-root-integer a n)))
  525. X+       (if (car root)
  526. X+           (cdr root)
  527. X+         (math-nth-root-float (math-float a) n (math-float (cdr root))))))
  528. X+    (and (eq (car-safe a) 'frac)
  529. X+     (let* ((num-root (math-nth-root-integer (nth 1 a) n))
  530. X+            (den-root (math-nth-root-integer (nth 2 a) n)))
  531. X+       (if (and (car num-root) (car den-root))
  532. X+           (list 'frac (cdr num-root) (cdr den-root))
  533. X+         (math-nth-root-float (math-float a) n
  534. X+                  (math-div (math-float (cdr num-root))
  535. X+                        (math-float (cdr den-root)))))))
  536. X+    (and (eq (car-safe a) 'float)
  537. X+     (math-nth-root-float a n))
  538. X+    (and (eq (car-safe a) 'polar)
  539. X+     (list 'polar
  540. X+           (math-nth-root (nth 1 a) n)
  541. X+           (math-div (nth 2 a) n)))
  542. X+    (math-pow a (math-div-float '(float 1 0) (math-float n))))
  543. X+ )
  544. X+ 
  545. X+ (defun math-nth-root-float (a n &optional guess)
  546. X+   (math-inexact-result)
  547. X+   (math-with-extra-prec 1
  548. X+     (let ((nf (math-float n))
  549. X+       (nfm1 (math-float (1- n))))
  550. X+       (math-nth-root-float-iter a (or guess
  551. X+                       (math-make-float
  552. X+                        1 (/ (+ (math-numdigs (nth 1 a))
  553. X+                            (nth 2 a)
  554. X+                            (/ n 2))
  555. X+                         n))))))
  556. X+ )
  557. X+ 
  558. X+ (defun math-nth-root-float-iter (a guess)   ; uses "n", "nf", "nfm1"
  559. X+   (math-working "root" guess)
  560. X+   (let ((g2 (math-div-float (math-add-float (math-mul nfm1 guess)
  561. X+                         (math-div-float
  562. X+                          a (math-ipow guess (1- n))))
  563. X+                 nf)))
  564. X+     (if (math-nearly-equal-float g2 guess)
  565. X+     g2
  566. X+       (math-nth-root-float-iter a g2)))
  567. X+ )
  568. X+ 
  569. X+ (defun math-nth-root-integer (a n &optional guess)   ; [I I S]
  570. X+   (math-nth-root-int-iter a (or guess
  571. X+                 (math-scale-int 1 (/ (+ (math-numdigs a)
  572. X+                             (1- n))
  573. X+                              n))))
  574. X+ )
  575. X+ 
  576. X+ (defun math-nth-root-int-iter (a guess)   ; uses "n"
  577. X+   (math-working "root" guess)
  578. X+   (let* ((q (math-idivmod a (math-ipow guess (1- n))))
  579. X+      (s (math-add (car q) (math-mul (1- n) guess)))
  580. X+      (g2 (math-idivmod s n)))
  581. X+     (if (Math-natnum-lessp (car g2) guess)
  582. X+     (math-nth-root-int-iter a (car g2))
  583. X+       (cons (and (equal (car g2) guess)
  584. X+          (eq (cdr q) 0)
  585. X+          (eq (cdr g2) 0))
  586. X+         guess)))
  587. X+ )
  588. X+ 
  589. X+ (defun calcFunc-nroot (x n)
  590. X+   (calcFunc-pow x (if (integerp n)
  591. X+               (math-make-frac 1 n)
  592. X+             (math-div 1 n)))
  593. X+ )
  594. X+ 
  595. X+ 
  596. X+ 
  597. X  ;;;; [calc-arith.el]
  598. X  
  599. X  ;;; Compute the minimum of two real numbers.  [R R R] [Public]
  600. X***************
  601. X*** 8565,8571 ****
  602. X  
  603. X  
  604. X  (defun math-trunc-fancy (a)
  605. X!   (cond ((eq (car a) 'cplx) (math-trunc (nth 1 a)))
  606. X      ((eq (car a) 'polar) (math-trunc (math-complex a)))
  607. X      ((eq (car a) 'hms) (list 'hms (nth 1 a) 0 0))
  608. X      ((eq (car a) 'mod)
  609. X--- 13062,13069 ----
  610. X  
  611. X  
  612. X  (defun math-trunc-fancy (a)
  613. X!   (cond ((eq (car a) 'frac) (math-quotient (nth 1 a) (nth 2 a)))
  614. X!     ((eq (car a) 'cplx) (math-trunc (nth 1 a)))
  615. X      ((eq (car a) 'polar) (math-trunc (math-complex a)))
  616. X      ((eq (car a) 'hms) (list 'hms (nth 1 a) 0 0))
  617. X      ((eq (car a) 'mod)
  618. X***************
  619. X*** 8736,8741 ****
  620. X--- 13234,13265 ----
  621. X  (fset 'calcFunc-scf (symbol-function 'math-scale-float))
  622. X  
  623. X  
  624. X+ (defun math-increment (x &optional step relative-to)
  625. X+   (or step (setq step 1))
  626. X+   (cond ((not (Math-integerp step))
  627. X+      (math-reject-arg step 'integerp))
  628. X+     ((Math-integerp x)
  629. X+      (math-add x step))
  630. X+     ((eq (car x) 'float)
  631. X+      (if (and (math-zerop x)
  632. X+           (eq (car-safe relative-to) 'float))
  633. X+          (math-mul step
  634. X+                (math-scale-float relative-to (- 1 calc-internal-prec)))
  635. X+        (math-add-float x (math-make-float
  636. X+                   step
  637. X+                   (+ (nth 2 x)
  638. X+                  (- (math-numdigs (nth 1 x))
  639. X+                     calc-internal-prec))))))
  640. X+     (t
  641. X+      (math-reject-arg x 'realp)))
  642. X+ )
  643. X+ (fset 'calcFunc-incr (symbol-function 'math-increment))
  644. X+ 
  645. X+ (defun calcFunc-decr (x &optional step relative-to)
  646. X+   (math-increment x (math-neg (or step 1)) relative-to)
  647. X+ )
  648. X+ 
  649. X+ 
  650. X  ;;;; [calc-frac.el]
  651. X  
  652. X  ;;; Convert a real value to fractional form.  [T R I; T R F] [Public]
  653. X***************
  654. X*** 8814,8820 ****
  655. X  )
  656. X  
  657. X  
  658. X! ;;;; [calc-ext.el]
  659. X  
  660. X  (defun math-clean-number (a &optional prec)   ; [X X S] [Public]
  661. X    (if prec
  662. X--- 13338,13344 ----
  663. X  )
  664. X  
  665. X  
  666. X! ;;;; [calc-stuff.el]
  667. X  
  668. X  (defun math-clean-number (a &optional prec)   ; [X X S] [Public]
  669. X    (if prec
  670. X***************
  671. X*** 8856,8862 ****
  672. X      (if (= res 0)
  673. X      1
  674. X        (if (= res 2)
  675. X!       (list 'calcFunc-eq a b)
  676. X      0)))
  677. X  )
  678. X  
  679. X--- 13380,13388 ----
  680. X      (if (= res 0)
  681. X      1
  682. X        (if (= res 2)
  683. X!       (if (and (math-looks-negp a) (math-looks-negp b))
  684. X!           (list 'calcFunc-eq (math-neg a) (math-neg b))
  685. X!         (list 'calcFunc-eq a b))
  686. X      0)))
  687. X  )
  688. X  
  689. X***************
  690. X*** 8865,8871 ****
  691. X      (if (= res 0)
  692. X      0
  693. X        (if (= res 2)
  694. X!       (list 'calcFunc-neq a b)
  695. X      1)))
  696. X  )
  697. X  
  698. X--- 13391,13399 ----
  699. X      (if (= res 0)
  700. X      0
  701. X        (if (= res 2)
  702. X!       (if (and (math-looks-negp a) (math-looks-negp b))
  703. X!           (list 'calcFunc-neq (math-neg a) (math-neg b))
  704. X!         (list 'calcFunc-neq a b))
  705. X      1)))
  706. X  )
  707. X  
  708. X***************
  709. X*** 8874,8880 ****
  710. X      (if (= res -1)
  711. X      1
  712. X        (if (= res 2)
  713. X!       (list 'calcFunc-lt a b)
  714. X      0)))
  715. X  )
  716. X  
  717. X--- 13402,13410 ----
  718. X      (if (= res -1)
  719. X      1
  720. X        (if (= res 2)
  721. X!       (if (and (math-looks-negp a) (math-looks-negp b))
  722. X!           (list 'calcFunc-gt (math-neg a) (math-neg b))
  723. X!         (list 'calcFunc-lt a b))
  724. X      0)))
  725. X  )
  726. X  
  727. X***************
  728. X*** 8883,8889 ****
  729. X      (if (= res 1)
  730. X      1
  731. X        (if (= res 2)
  732. X!       (list 'calcFunc-gt a b)
  733. X      0)))
  734. X  )
  735. X  
  736. X--- 13413,13421 ----
  737. X      (if (= res 1)
  738. X      1
  739. X        (if (= res 2)
  740. X!       (if (and (math-looks-negp a) (math-looks-negp b))
  741. X!           (list 'calcFunc-lt (math-neg a) (math-neg b))
  742. X!         (list 'calcFunc-gt a b))
  743. X      0)))
  744. X  )
  745. X  
  746. X***************
  747. X*** 8892,8898 ****
  748. X      (if (= res 1)
  749. X      0
  750. X        (if (= res 2)
  751. X!       (list 'calcFunc-leq a b)
  752. X      1)))
  753. X  )
  754. X  
  755. X--- 13424,13432 ----
  756. X      (if (= res 1)
  757. X      0
  758. X        (if (= res 2)
  759. X!       (if (and (math-looks-negp a) (math-looks-negp b))
  760. X!           (list 'calcFunc-geq (math-neg a) (math-neg b))
  761. X!         (list 'calcFunc-leq a b))
  762. X      1)))
  763. X  )
  764. X  
  765. X***************
  766. X*** 8901,8907 ****
  767. X      (if (= res -1)
  768. X      0
  769. X        (if (= res 2)
  770. X!       (list 'calcFunc-geq a b)
  771. X      1)))
  772. X  )
  773. X  
  774. X--- 13435,13443 ----
  775. X      (if (= res -1)
  776. X      0
  777. X        (if (= res 2)
  778. X!       (if (and (math-looks-negp a) (math-looks-negp b))
  779. X!           (list 'calcFunc-leq (math-neg a) (math-neg b))
  780. X!         (list 'calcFunc-geq a b))
  781. X      1)))
  782. X  )
  783. X  
  784. X***************
  785. X*** 8934,8940 ****
  786. X        1
  787. X      (if (Math-numberp a)
  788. X      0
  789. X!       (list 'calcFunc-lnot a)))
  790. X  )
  791. X  
  792. X  (defun calcFunc-if (c e1 e2)
  793. X--- 13470,13480 ----
  794. X        1
  795. X      (if (Math-numberp a)
  796. X      0
  797. X!       (let ((op (and (= (length a) 3)
  798. X!              (assq (car a) calc-tweak-eqn-table))))
  799. X!     (if op
  800. X!         (cons (nth 2 op) (cdr a))
  801. X!       (list 'calcFunc-lnot a)))))
  802. X  )
  803. X  
  804. X  (defun calcFunc-if (c e1 e2)
  805. X***************
  806. X*** 8942,8948 ****
  807. X        e2
  808. X      (if (Math-numberp c)
  809. X      e1
  810. X!       (list 'calcFunc-if c e1 e2)))
  811. X  )
  812. X  
  813. X  (defun math-normalize-logical-op (a)
  814. X--- 13482,13510 ----
  815. X        e2
  816. X      (if (Math-numberp c)
  817. X      e1
  818. X!       (or (and (Math-vectorp c)
  819. X!            (math-constp c)
  820. X!            (let ((ee1 (if (Math-vectorp e1)
  821. X!                   (if (= (length c) (length e1))
  822. X!                   (cdr e1)
  823. X!                 (calc-record-why "Dimension error" e1))
  824. X!                 (list e1)))
  825. X!              (ee2 (if (Math-vectorp e2)
  826. X!                   (if (= (length c) (length e2))
  827. X!                   (cdr e2)
  828. X!                 (calc-record-why "Dimension error" e2))
  829. X!                 (list e2))))
  830. X!          (and ee1 ee2
  831. X!               (cons 'vec (math-if-vector (cdr c) ee1 ee2)))))
  832. X!       (list 'calcFunc-if c e1 e2))))
  833. X! )
  834. X! 
  835. X! (defun math-if-vector (c e1 e2)
  836. X!   (and c
  837. X!        (cons (if (Math-zerop (car c)) (car e2) (car e1))
  838. X!          (math-if-vector (cdr c)
  839. X!                  (or (cdr e1) e1)
  840. X!                  (or (cdr e2) e2))))
  841. X  )
  842. X  
  843. X  (defun math-normalize-logical-op (a)
  844. X***************
  845. X*** 8953,8959 ****
  846. X           (math-normalize (nth 3 a))
  847. X             (if (Math-numberp a1)
  848. X             (math-normalize (nth 2 a))
  849. X!          (list 'calcFunc-if a1 (nth 2 a) (nth 3 a))))))
  850. X        a)
  851. X  )
  852. X  
  853. X--- 13515,13529 ----
  854. X           (math-normalize (nth 3 a))
  855. X             (if (Math-numberp a1)
  856. X             (math-normalize (nth 2 a))
  857. X!          (if (and (Math-vectorp (nth 1 a))
  858. X!               (math-constp (nth 1 a)))
  859. X!              (calcFunc-if (nth 1 a)
  860. X!                   (math-normalize (nth 2 a))
  861. X!                   (math-normalize (nth 3 a)))
  862. X!            (let ((calc-simplify-mode 'none))
  863. X!              (list 'calcFunc-if a1
  864. X!                (math-normalize (nth 2 a))
  865. X!                (math-normalize (nth 3 a)))))))))
  866. X        a)
  867. X  )
  868. X  
  869. X***************
  870. X*** 8999,9014 ****
  871. X      ((eq (car a) 'mod) 9)
  872. X      ((eq (car a) 'var) 100)
  873. X      ((eq (car a) 'vec) (if (math-matrixp a) 102 101))
  874. X!     (t (let ((func (assq (car a) '( ( + . calcFunc-add )
  875. X!                     ( - . calcFunc-sub )
  876. X!                     ( * . calcFunc-mul )
  877. X!                     ( / . calcFunc-div )
  878. X!                     ( ^ . calcFunc-pow )
  879. X!                     ( % . calcFunc-mod )
  880. X!                     ( neg . calcFunc-neg )
  881. X!                     ( | . calcFunc-vconcat ) ))))
  882. X!          (setq func (if func (cdr func) (car a)))
  883. X!          (math-calcFunc-to-var func))))
  884. X  )
  885. X  
  886. X  (defun calcFunc-integer (a)
  887. X--- 13569,13575 ----
  888. X      ((eq (car a) 'mod) 9)
  889. X      ((eq (car a) 'var) 100)
  890. X      ((eq (car a) 'vec) (if (math-matrixp a) 102 101))
  891. X!     (t (math-calcFunc-to-var func)))
  892. X  )
  893. X  
  894. X  (defun calcFunc-integer (a)
  895. X***************
  896. X*** 9043,9048 ****
  897. X--- 13604,13637 ----
  898. X        0))
  899. X  )
  900. X  
  901. X+ (defun calcFunc-negative (a)
  902. X+   (if (math-looks-negp a)
  903. X+       1
  904. X+     (if (or (math-zerop a)
  905. X+         (math-posp a))
  906. X+     0
  907. X+       (list 'calcFunc-negative a)))
  908. X+ )
  909. X+ 
  910. X+ (defun calcFunc-variable (a)
  911. X+   (if (eq (car-safe a) 'var)
  912. X+       1
  913. X+     (if (Math-objvecp a)
  914. X+     0
  915. X+       (list 'calcFunc-variable a)))
  916. X+ )
  917. X+ 
  918. X+ (defun calcFunc-nonvar (a)
  919. X+   (if (eq (car-safe a) 'var)
  920. X+       (list 'calcFunc-nonvar a)
  921. X+     1)
  922. X+ )
  923. X+ 
  924. X+ (defun calcFunc-istrue (a)
  925. X+   (if (math-is-true a)
  926. X+       1
  927. X+     0)
  928. X+ )
  929. X  
  930. X  
  931. X  
  932. X***************
  933. X*** 9320,9337 ****
  934. X  (fset 'calcFunc-tan (symbol-function 'math-tan))
  935. X  
  936. X  (defun math-sin-raw (x)   ; [N N]
  937. X!   (cond ((eq (car-safe x) 'cplx)
  938. X       (let* ((expx (math-exp-raw (nth 2 x)))
  939. X          (expmx (math-div-float '(float 1 0) expx))
  940. X          (sc (math-sin-cos-raw (nth 1 x))))
  941. X         (list 'cplx
  942. X           (math-mul-float (car sc)
  943. X!                  (math-mul-float (math-sub expx expmx)
  944. X                           '(float 5 -1)))
  945. X           (math-mul-float (cdr sc)
  946. X!                  (math-mul-float (math-add-float expx expmx)
  947. X                           '(float 5 -1))))))
  948. X!     ((eq (car-safe x) 'polar)
  949. X       (math-polar (math-sin-raw (math-complex x))))
  950. X      ((Math-integer-negp (nth 1 x))
  951. X       (math-neg-float (math-sin-raw (math-neg-float x))))
  952. X--- 13909,13926 ----
  953. X  (fset 'calcFunc-tan (symbol-function 'math-tan))
  954. X  
  955. X  (defun math-sin-raw (x)   ; [N N]
  956. X!   (cond ((eq (car x) 'cplx)
  957. X       (let* ((expx (math-exp-raw (nth 2 x)))
  958. X          (expmx (math-div-float '(float 1 0) expx))
  959. X          (sc (math-sin-cos-raw (nth 1 x))))
  960. X         (list 'cplx
  961. X           (math-mul-float (car sc)
  962. X!                  (math-mul-float (math-add-float expx expmx)
  963. X                           '(float 5 -1)))
  964. X           (math-mul-float (cdr sc)
  965. X!                  (math-mul-float (math-sub-float expx expmx)
  966. X                           '(float 5 -1))))))
  967. X!     ((eq (car x) 'polar)
  968. X       (math-polar (math-sin-raw (math-complex x))))
  969. X      ((Math-integer-negp (nth 1 x))
  970. X       (math-neg-float (math-sin-raw (math-neg-float x))))
  971. X***************
  972. X*** 9343,9349 ****
  973. X  (defun math-cos-raw (x)   ; [N N]
  974. X    (if (eq (car-safe x) 'polar)
  975. X        (math-polar (math-cos-raw (math-complex x)))
  976. X!     (math-sin-raw (math-sub-float (math-pi-over-2) x)))
  977. X  )
  978. X  
  979. X  ;;; This could use a smarter method:  Reduce x as in math-sin-raw, then
  980. X--- 13932,13938 ----
  981. X  (defun math-cos-raw (x)   ; [N N]
  982. X    (if (eq (car-safe x) 'polar)
  983. X        (math-polar (math-cos-raw (math-complex x)))
  984. X!     (math-sin-raw (math-sub (math-pi-over-2) x)))
  985. X  )
  986. X  
  987. X  ;;; This could use a smarter method:  Reduce x as in math-sin-raw, then
  988. X***************
  989. X*** 9354,9361 ****
  990. X  )
  991. X  
  992. X  (defun math-tan-raw (x)   ; [N N]
  993. X!   (cond ((eq (car-safe x) 'cplx)
  994. X!      (let* ((x (math-mul-float x '(float 2 0)))
  995. X          (expx (math-exp-raw (nth 2 x)))
  996. X          (expmx (math-div-float '(float 1 0) expx))
  997. X          (sc (math-sin-cos-raw (nth 1 x)))
  998. X--- 13943,13950 ----
  999. X  )
  1000. X  
  1001. X  (defun math-tan-raw (x)   ; [N N]
  1002. X!   (cond ((eq (car x) 'cplx)
  1003. X!      (let* ((x (math-mul x '(float 2 0)))
  1004. X          (expx (math-exp-raw (nth 2 x)))
  1005. X          (expmx (math-div-float '(float 1 0) expx))
  1006. X          (sc (math-sin-cos-raw (nth 1 x)))
  1007. X***************
  1008. X*** 9365,9373 ****
  1009. X         (and (not (eq (nth 1 d) 0))
  1010. X          (list 'cplx
  1011. X                (math-div-float (car sc) d)
  1012. X!               (math-div-float (math-mul-float (math-add expx expmx)
  1013. X                                '(float 5 -1)) d)))))
  1014. X!     ((eq (car-safe x) 'polar)
  1015. X       (math-polar (math-tan-raw (math-complex x))))
  1016. X      (t
  1017. X       (let ((sc (math-sin-cos-raw x)))
  1018. X--- 13954,13963 ----
  1019. X         (and (not (eq (nth 1 d) 0))
  1020. X          (list 'cplx
  1021. X                (math-div-float (car sc) d)
  1022. X!               (math-div-float (math-mul-float (math-sub-float expx
  1023. X!                                       expmx)
  1024. X                                '(float 5 -1)) d)))))
  1025. X!     ((eq (car x) 'polar)
  1026. X       (math-polar (math-tan-raw (math-complex x))))
  1027. X      (t
  1028. X       (let ((sc (math-sin-cos-raw x)))
  1029. X***************
  1030. X*** 9476,9483 ****
  1031. X  
  1032. X  (defun math-arcsin-raw (x)   ; [N N]
  1033. X    (let ((a (math-sqrt-raw (math-sub '(float 1 0) (math-sqr x)))))
  1034. X!     (if (or (memq (car-safe x) '(cplx polar))
  1035. X!         (memq (car-safe a) '(cplx polar)))
  1036. X      (math-with-extra-prec 2   ; use extra precision for difficult case
  1037. X        (math-mul '(cplx 0 -1)
  1038. X              (math-ln-raw (math-add (math-mul '(cplx 0 1) x) a))))
  1039. X--- 14066,14073 ----
  1040. X  
  1041. X  (defun math-arcsin-raw (x)   ; [N N]
  1042. X    (let ((a (math-sqrt-raw (math-sub '(float 1 0) (math-sqr x)))))
  1043. X!     (if (or (memq (car x) '(cplx polar))
  1044. X!         (memq (car a) '(cplx polar)))
  1045. X      (math-with-extra-prec 2   ; use extra precision for difficult case
  1046. X        (math-mul '(cplx 0 -1)
  1047. X              (math-ln-raw (math-add (math-mul '(cplx 0 1) x) a))))
  1048. X***************
  1049. X*** 9489,9495 ****
  1050. X  )
  1051. X  
  1052. X  (defun math-arctan-raw (x)   ; [N N]
  1053. X!   (cond ((memq (car-safe x) '(cplx polar))
  1054. X       (math-with-extra-prec 2   ; extra-extra
  1055. X         (math-mul '(cplx 0 -1)
  1056. X               (math-ln-raw (math-mul
  1057. X--- 14079,14085 ----
  1058. X  )
  1059. X  
  1060. X  (defun math-arctan-raw (x)   ; [N N]
  1061. X!   (cond ((memq (car x) '(cplx polar))
  1062. X       (math-with-extra-prec 2   ; extra-extra
  1063. X         (math-mul '(cplx 0 -1)
  1064. X               (math-ln-raw (math-mul
  1065. X***************
  1066. X*** 9643,9653 ****
  1067. X  (defun math-exp-series (sum nfac n xpow x)
  1068. X    (math-working "exp" sum)
  1069. X    (let* ((nextx (math-mul-float xpow x))
  1070. X!       (nextsum (math-add-float sum (math-div-float nextx
  1071. X!                                (math-float nfac)))))
  1072. X!      (if (math-nearly-equal-float sum nextsum)
  1073. X!      sum
  1074. X!        (math-exp-series nextsum (math-mul nfac n) (1+ n) nextx x)))
  1075. X  )
  1076. X  
  1077. X  
  1078. X--- 14233,14243 ----
  1079. X  (defun math-exp-series (sum nfac n xpow x)
  1080. X    (math-working "exp" sum)
  1081. X    (let* ((nextx (math-mul-float xpow x))
  1082. X!      (nextsum (math-add-float sum (math-div-float nextx
  1083. X!                               (math-float nfac)))))
  1084. X!     (if (math-nearly-equal-float sum nextsum)
  1085. X!     sum
  1086. X!       (math-exp-series nextsum (math-mul nfac n) (1+ n) nextx x)))
  1087. X  )
  1088. X  
  1089. X  
  1090. X***************
  1091. X*** 9677,9683 ****
  1092. X  (fset 'calcFunc-ln (symbol-function 'math-ln))
  1093. X  
  1094. X  (defun math-log10 (x)   ; [N N] [Public]
  1095. X!   (cond ((Math-numberp x)
  1096. X       (math-with-extra-prec 2
  1097. X         (let ((xf (math-float x)))
  1098. X           (if (eq (nth 1 xf) 0)
  1099. X--- 14267,14288 ----
  1100. X  (fset 'calcFunc-ln (symbol-function 'math-ln))
  1101. X  
  1102. X  (defun math-log10 (x)   ; [N N] [Public]
  1103. X!   (cond ((math-equal-int x 1)
  1104. X!      (if (math-floatp x) '(float 0 0) 0))
  1105. X!     ((and (Math-integerp x)
  1106. X!           (math-posp x)
  1107. X!           (let ((res (math-integer-log x 10)))
  1108. X!         (and (car res)
  1109. X!              (setq x (cdr res)))))
  1110. X!      x)
  1111. X!     ((and (eq (car-safe x) 'frac)
  1112. X!           (eq (nth 1 x) 1)
  1113. X!           (let ((res (math-integer-log (nth 2 x) 10)))
  1114. X!         (and (car res)
  1115. X!              (setq x (- (cdr res))))))
  1116. X!      x)
  1117. X!     (calc-symbolic-mode (signal 'inexact-result nil))
  1118. X!     ((Math-numberp x)
  1119. X       (math-with-extra-prec 2
  1120. X         (let ((xf (math-float x)))
  1121. X           (if (eq (nth 1 xf) 0)
  1122. X***************
  1123. X*** 9718,9723 ****
  1124. X--- 14323,14352 ----
  1125. X       (math-reject-arg x "Logarithm of zero"))
  1126. X      ((math-zerop b)
  1127. X       (math-reject-arg b "Logarithm of zero"))
  1128. X+     ((math-equal-int b 1)
  1129. X+      (math-reject-arg b "Logarithm base one"))
  1130. X+     ((math-equal-int x 1)
  1131. X+      (if (or (math-floatp a) (math-floatp b)) '(float 0 0) 0))
  1132. X+     ((and (Math-ratp x) (Math-ratp b)
  1133. X+           (math-posp x) (math-posp b)
  1134. X+           (let* ((sign 1) (inv nil)
  1135. X+              (xx (if (math-lessp 1 x)
  1136. X+                  x
  1137. X+                (setq sign -1)
  1138. X+                (math-div 1 x)))
  1139. X+              (bb (if (math-lessp 1 b)
  1140. X+                  b
  1141. X+                (setq sign (- sign))
  1142. X+                (math-div 1 b)))
  1143. X+              (res (if (math-lessp xx bb)
  1144. X+                   (setq inv (math-integer-log bb xx))
  1145. X+                 (math-integer-log xx bb))))
  1146. X+         (and (car res)
  1147. X+              (setq x (if inv
  1148. X+                  (math-div 1 (* sign (cdr res)))
  1149. X+                    (* sign (cdr res)))))))
  1150. X+      x)
  1151. X+     (calc-symbolic-mode (signal 'inexact-result nil))
  1152. X      ((and (Math-numberp x) (Math-numberp b))
  1153. X       (math-with-extra-prec 2
  1154. X         (math-div (math-ln-raw (math-float x))
  1155. X***************
  1156. X*** 9749,9755 ****
  1157. X        (math-log x b)))
  1158. X      (math-normalize (list 'calcFunc-ln x)))
  1159. X  )
  1160. X! (defun calcFunc-ilog (x &optional b)
  1161. X    (if b
  1162. X        (if (equal b '(var e var-e))
  1163. X        (math-normalize (list 'calcFunc-exp x))
  1164. X--- 14378,14384 ----
  1165. X        (math-log x b)))
  1166. X      (math-normalize (list 'calcFunc-ln x)))
  1167. X  )
  1168. X! (defun calcFunc-alog (x &optional b)
  1169. X    (if b
  1170. X        (if (equal b '(var e var-e))
  1171. X        (math-normalize (list 'calcFunc-exp x))
  1172. X***************
  1173. X*** 9757,9762 ****
  1174. X--- 14386,14425 ----
  1175. X      (math-normalize (list 'calcFunc-exp x)))
  1176. X  )
  1177. X  
  1178. X+ (defun calcFunc-ilog (x b)
  1179. X+   (or (math-num-integerp x) (math-reject-arg x 'integerp))
  1180. X+   (or (math-num-integerp b) (math-reject-arg b 'integerp))
  1181. X+   (setq x (math-trunc x))
  1182. X+   (setq b (math-trunc b))
  1183. X+   (or (math-posp x) (math-reject-arg x 'posp))
  1184. X+   (or (math-posp b) (math-reject-arg b 'posp))
  1185. X+   (if (eq b 1) (math-reject-arg x "Logarithm of zero"))
  1186. X+   (if (Math-natnum-lessp x b)
  1187. X+       0
  1188. X+     (cdr (math-integer-log x b)))
  1189. X+ )
  1190. X+ 
  1191. X+ (defun math-integer-log (x b)
  1192. X+   (let ((pows (list b))
  1193. X+     (pow (math-sqr b))
  1194. X+     next
  1195. X+     sum n)
  1196. X+     (while (not (math-lessp x pow))
  1197. X+       (setq pows (cons pow pows)
  1198. X+         pow (math-sqr pow)))
  1199. X+     (setq n (lsh 1 (1- (length pows)))
  1200. X+       sum n
  1201. X+       pow (car pows))
  1202. X+     (while (and (setq pows (cdr pows))
  1203. X+         (math-lessp pow x))
  1204. X+       (setq n (/ n 2)
  1205. X+         next (math-mul pow (car pows)))
  1206. X+       (or (math-lessp x next)
  1207. X+       (setq pow next
  1208. X+         sum (+ sum n))))
  1209. X+     (cons (equal pow x) sum))
  1210. X+ )
  1211. X+ 
  1212. X  
  1213. X  (defun math-log-base-raw (b)   ; [N N]
  1214. X    (if (not (and (equal (car math-log-base-cache) b)
  1215. X***************
  1216. X*** 10036,10041 ****
  1217. X--- 14699,15504 ----
  1218. X  
  1219. X  
  1220. X  
  1221. X+ ;;;; [calc-funcs.el]
  1222. X+ 
  1223. X+ ;;; Sources:  Numerical Recipes, Press et al;
  1224. X+ ;;;           Handbook of Mathematical Functions, Abramowitz & Stegun.
  1225. X+ 
  1226. X+ 
  1227. X+ ;;; Gamma function.
  1228. X+ 
  1229. X+ (defun calcFunc-gamma (x)
  1230. X+   (or (math-numberp x) (math-reject-arg x 'numberp))
  1231. X+   (calcFunc-fact (math-add x -1))
  1232. X+ )
  1233. X+ 
  1234. X+ (defun math-gammap1-raw (x fprec nfprec)   ; compute gamma(1 + x)
  1235. X+   (cond ((math-lessp-float (math-real-part x) fprec)
  1236. X+      (if (math-lessp-float (math-real-part x) nfprec)
  1237. X+          (math-neg (math-div
  1238. X+             (math-pi)
  1239. X+             (math-mul (math-gammap1-raw
  1240. X+                    (math-add (math-neg x)
  1241. X+                          '(float -1 0))
  1242. X+                    fprec nfprec)
  1243. X+                   (math-sin-raw
  1244. X+                    (math-mul (math-pi) x)))))
  1245. X+        (let ((xplus1 (math-add x '(float 1 0))))
  1246. X+          (math-div (math-gammap1-raw xplus1 fprec nfprec) xplus1))))
  1247. X+     (t   ; re(x) now >= 10.0
  1248. X+      (let ((xinv (math-div 1 x))
  1249. X+            (lnx (math-ln-raw x)))
  1250. X+        (math-mul (math-sqrt-two-pi)
  1251. X+              (math-exp-raw
  1252. X+               (math-gamma-series
  1253. X+                (math-sub (math-mul (math-add x '(float 5 -1))
  1254. X+                        lnx)
  1255. X+                  x)
  1256. X+                xinv
  1257. X+                (math-sqr xinv)
  1258. X+                '(float 0 0)
  1259. X+                2))))))
  1260. X+ )
  1261. X+ 
  1262. X+ (defun math-gamma-series (sum x xinvsqr oterm n)
  1263. X+   (math-working "gamma" sum)
  1264. X+   (let* ((bn (math-bernoulli-number n))
  1265. X+      (term (math-mul (math-div-float (math-float (nth 1 bn))
  1266. X+                      (math-float (* (nth 2 bn)
  1267. X+                             (* n (1- n)))))
  1268. X+              x))
  1269. X+      (next (math-add sum term)))
  1270. X+     (if (math-nearly-equal sum next)
  1271. X+     next
  1272. X+       (if (> n (* 2 calc-internal-prec))
  1273. X+       (progn
  1274. X+         ;; Need this because series eventually diverges for large enough n.
  1275. X+         (calc-record-why "Gamma computation stopped early, not all digits may be valid")
  1276. X+         next)
  1277. X+     (math-gamma-series next (math-mul x xinvsqr) xinvsqr term (+ n 2)))))
  1278. X+ )
  1279. X+ 
  1280. X+ 
  1281. X+ ;;; Incomplete gamma function.
  1282. X+ 
  1283. X+ (defun calcFunc-gammaP (a x)
  1284. X+   (math-inexact-result)
  1285. X+   (or (Math-numberp a) (math-reject-arg a 'numberp))
  1286. X+   (or (math-numberp x) (math-reject-arg x 'numberp))
  1287. X+   (if (and (math-num-integerp a)
  1288. X+        (integerp (setq a (math-trunc a)))
  1289. X+        (> a 0) (< a 20))
  1290. X+       (math-sub 1 (calcFunc-gammaQ a x))
  1291. X+     (let ((math-current-gamma-value (calcFunc-gamma a)))
  1292. X+       (math-div (calcFunc-gammag a x) math-current-gamma-value)))
  1293. X+ )
  1294. X+ 
  1295. X+ (defun calcFunc-gammaQ (a x)
  1296. X+   (math-inexact-result)
  1297. X+   (or (Math-numberp a) (math-reject-arg a 'numberp))
  1298. X+   (or (math-numberp x) (math-reject-arg x 'numberp))
  1299. X+   (if (and (math-num-integerp a)
  1300. X+        (integerp (setq a (math-trunc a)))
  1301. X+        (> a 0) (< a 20))
  1302. X+       (let ((n 0)
  1303. X+         (sum '(float 1 0))
  1304. X+         (term '(float 1 0)))
  1305. X+     (math-with-extra-prec 1
  1306. X+       (while (< (setq n (1+ n)) a)
  1307. X+         (setq term (math-div (math-mul term x) n)
  1308. X+           sum (math-add sum term))
  1309. X+         (math-working "gamma" sum))
  1310. X+       (math-mul sum (math-exp (math-neg x)))))
  1311. X+     (let ((math-current-gamma-value (calcFunc-gamma a)))
  1312. X+       (math-div (calcFunc-gammaG a x) math-current-gamma-value)))
  1313. X+ )
  1314. X+ 
  1315. X+ (defun calcFunc-gammag (a x)
  1316. X+   (math-inexact-result)
  1317. X+   (or (Math-numberp a) (math-reject-arg a 'numberp))
  1318. X+   (or (Math-numberp x) (math-reject-arg x 'numberp))
  1319. X+   (math-with-extra-prec 2
  1320. X+     (setq a (math-float a))
  1321. X+     (setq x (math-float x))
  1322. X+     (if (or (math-negp (math-real-part a))
  1323. X+         (math-lessp-float (math-real-part x)
  1324. X+                   (math-add-float (math-real-part a)
  1325. X+                           '(float 1 0))))
  1326. X+     (math-inc-gamma-series a x)
  1327. X+       (math-sub (or math-current-gamma-value (calcFunc-gamma a))
  1328. X+         (math-inc-gamma-cfrac a x))))
  1329. X+ )
  1330. X+ (setq math-current-gamma-value nil)
  1331. X+ 
  1332. X+ (defun calcFunc-gammaG (a x)
  1333. X+   (math-inexact-result)
  1334. X+   (or (Math-numberp a) (math-reject-arg a 'numberp))
  1335. X+   (or (Math-numberp x) (math-reject-arg x 'numberp))
  1336. X+   (math-with-extra-prec 2
  1337. X+     (setq a (math-float a))
  1338. X+     (setq x (math-float x))
  1339. X+     (if (or (math-negp (math-real-part a))
  1340. X+         (math-lessp-float (math-real-part x)
  1341. X+                   (math-add-float (math-abs-approx a)
  1342. X+                           '(float 1 0))))
  1343. X+     (math-sub (or math-current-gamma-value (calcFunc-gamma a))
  1344. X+           (math-inc-gamma-series a x))
  1345. X+       (math-inc-gamma-cfrac a x)))
  1346. X+ )
  1347. X+ 
  1348. X+ (defun math-inc-gamma-series (a x)
  1349. X+   (math-mul (math-exp-raw (math-sub (math-mul a (math-ln-raw x)) x))
  1350. X+         (math-with-extra-prec 2
  1351. X+           (let ((start (math-div '(float 1 0) a)))
  1352. X+         (math-inc-gamma-series-step start start a x))))
  1353. X+ )
  1354. X+ 
  1355. X+ (defun math-inc-gamma-series-step (sum term a x)
  1356. X+   (math-working "gamma" sum)
  1357. X+   (setq a (math-add a '(float 1 0))
  1358. X+     term (math-div (math-mul term x) a))
  1359. X+   (let ((next (math-add sum term)))
  1360. X+     (if (math-nearly-equal sum next)
  1361. X+     next
  1362. X+       (math-inc-gamma-series-step next term a x)))
  1363. X+ )
  1364. X+ 
  1365. X+ (defun math-inc-gamma-cfrac (a x)
  1366. X+   (math-mul (math-exp-raw (math-sub (math-mul a (math-ln-raw x)) x))
  1367. X+         (math-inc-gamma-cfrac-step '(float 1 0) x '(float 0 0) '(float 1 0)
  1368. X+                        '(float 1 0) '(float 1 0) '(float 0 0)
  1369. X+                        a x))
  1370. X+ )
  1371. X+ 
  1372. X+ (defun math-inc-gamma-cfrac-step (a0 a1 b0 b1 n fac g a x)
  1373. X+   (let ((ana (math-sub n a))
  1374. X+     (anf (math-mul n fac)))
  1375. X+     (setq n (math-add n '(float 1 0))
  1376. X+       a0 (math-mul (math-add a1 (math-mul a0 ana)) fac)
  1377. X+       b0 (math-mul (math-add b1 (math-mul b0 ana)) fac)
  1378. X+       a1 (math-add (math-mul x a0) (math-mul anf a1))
  1379. X+       b1 (math-add (math-mul x b0) (math-mul anf b1)))
  1380. X+     (if (math-zerop a1)
  1381. X+     (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac g a x)
  1382. X+       (setq fac (math-div '(float 1 0) a1))
  1383. X+       (let ((next (math-mul b1 fac)))
  1384. X+     (math-working "gamma" next)
  1385. X+     (if (math-nearly-equal next g)
  1386. X+         next
  1387. X+       (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac next a x)))))
  1388. X+ )
  1389. X+ 
  1390. X+ 
  1391. X+ ;;; Error function.
  1392. X+ 
  1393. X+ (defun calcFunc-erf (x)
  1394. X+   (let ((math-current-gamma-value (math-sqrt-pi)))
  1395. X+     (math-to-same-complex-quad
  1396. X+      (math-div (calcFunc-gammag '(float 5 -1)
  1397. X+                 (math-sqr (math-to-complex-quad-one x)))
  1398. X+            math-current-gamma-value)
  1399. X+      x))
  1400. X+ )
  1401. X+ 
  1402. X+ (defun calcFunc-erfc (x)
  1403. X+   (if (math-posp x)
  1404. X+       (let ((math-current-gamma-value (math-sqrt-pi)))
  1405. X+     (math-div (calcFunc-gammaG '(float 5 -1) (math-sqr x))
  1406. X+           math-current-gamma-value))
  1407. X+     (math-add '(float 1 0) (calcFunc-erf (math-neg x))))
  1408. X+ )
  1409. X+ 
  1410. X+ (defun math-to-complex-quad-one (x)
  1411. X+   (if (eq (car-safe x) 'polar) (setq x (math-complex x)))
  1412. X+   (if (eq (car-safe x) 'cplx)
  1413. X+       (list 'cplx (math-abs (nth 1 x)) (math-abs (nth 2 x)))
  1414. X+     x)
  1415. X+ )
  1416. X+ 
  1417. X+ (defun math-to-same-complex-quad (x y)
  1418. X+   (if (eq (car-safe y) 'cplx)
  1419. X+       (if (eq (car-safe x) 'cplx)
  1420. X+       (list 'cplx
  1421. X+         (if (math-negp (nth 1 y)) (math-neg (nth 1 x)) (nth 1 x))
  1422. X+         (if (math-negp (nth 2 y)) (math-neg (nth 2 x)) (nth 2 x)))
  1423. X+     (if (math-negp (nth 1 y)) (math-neg x) x))
  1424. X+     (if (math-negp y)
  1425. X+     (if (eq (car-safe x) 'cplx)
  1426. X+         (list 'cplx (math-neg (nth 1 x)) (nth 2 x))
  1427. X+       (math-neg x))
  1428. X+       x))
  1429. X+ )
  1430. X+ 
  1431. X+ 
  1432. X+ ;;; Beta function.
  1433. X+ 
  1434. X+ (defun calcFunc-beta (a b)
  1435. X+   (if (math-num-integerp a)
  1436. X+       (let ((am (math-add a -1)))
  1437. X+     (or (math-numberp b) (math-reject-arg b 'numberp))
  1438. X+     (math-div 1 (math-mul b (math-choose (math-add b am) am))))
  1439. X+     (if (math-num-integerp b)
  1440. X+     (calcFunc-beta b a)
  1441. X+       (math-div (math-mul (calcFunc-gamma a) (calcFunc-gamma b))
  1442. X+         (calcFunc-gamma (math-add a b)))))
  1443. X+ )
  1444. X+ 
  1445. X+ 
  1446. X+ ;;; Incomplete beta function.
  1447. X+ 
  1448. X+ (defun calcFunc-betaI (x a b)
  1449. X+   (cond ((math-zerop x)
  1450. X+      '(float 0 0))
  1451. X+     ((math-equal-int x 1)
  1452. X+      '(float 1 0))
  1453. X+     ((or (math-zerop a)
  1454. X+          (and (math-num-integerp a)
  1455. X+           (math-negp a)))
  1456. X+      (if (or (math-zerop b)
  1457. X+          (and (math-num-integerp b)
  1458. X+               (math-negp b)))
  1459. X+          (math-reject-arg b 'nonzerop)
  1460. X+        '(float 1 0)))
  1461. X+     ((or (math-zerop b)
  1462. X+          (and (math-num-integerp b)
  1463. X+           (math-negp b)))
  1464. X+      '(float 0 0))
  1465. X+     ((not (math-numberp a)) (math-reject-arg a 'numberp))
  1466. X+     ((not (math-numberp b)) (math-reject-arg b 'numberp))
  1467. X+     ((math-inexact-result))
  1468. X+     (t (let ((math-current-beta-value (calcFunc-beta a b)))
  1469. X+          (math-div (calcFunc-betaB x a b) math-current-beta-value))))
  1470. X+ )
  1471. X+ 
  1472. X+ (defun calcFunc-betaB (x a b)
  1473. X+   (cond
  1474. X+    ((math-zerop x)
  1475. X+     '(float 0 0))
  1476. X+    ((math-equal-int x 1)
  1477. X+     (calcFunc-beta a b))
  1478. X+    ((not (math-numberp x)) (math-reject-arg x 'numberp))
  1479. X+    ((not (math-numberp a)) (math-reject-arg a 'numberp))
  1480. X+    ((not (math-numberp b)) (math-reject-arg b 'numberp))
  1481. X+    ((math-zerop a) (math-reject-arg a 'nonzerop))
  1482. X+    ((math-zerop b) (math-reject-arg b 'nonzerop))
  1483. X+    ((and (math-num-integerp b)
  1484. X+      (if (math-negp b)
  1485. X+          (math-reject-arg b 'range)
  1486. X+        (Math-natnum-lessp (setq b (math-trunc b)) 20)))
  1487. X+     (and calc-symbolic-mode (or (math-floatp a) (math-floatp b))
  1488. X+      (math-inexact-result))
  1489. X+     (math-mul
  1490. X+      (math-with-extra-prec 2
  1491. X+        (let* ((i 0)
  1492. X+           (term 1)
  1493. X+           (sum (math-div term a)))
  1494. X+      (while (< (setq i (1+ i)) b)
  1495. X+        (setq term (math-mul (math-div (math-mul term (- i b)) i) x)
  1496. X+          sum (math-add sum (math-div term (math-add a i))))
  1497. X+        (math-working "beta" sum))
  1498. X+      sum))
  1499. X+      (math-pow x a)))
  1500. X+    ((and (math-num-integerp a)
  1501. X+      (if (math-negp a)
  1502. X+          (math-reject-arg a 'range)
  1503. X+        (Math-natnum-lessp (setq a (math-trunc a)) 20)))
  1504. X+     (math-sub (or math-current-beta-value (calcFunc-beta a b))
  1505. X+           (calcFunc-betaB (math-sub 1 x) b a)))
  1506. X+    (t
  1507. X+     (math-inexact-result)
  1508. X+     (math-with-extra-prec 2
  1509. X+       (setq x (math-float x))
  1510. X+       (setq a (math-float a))
  1511. X+       (setq b (math-float b))
  1512. X+       (let ((bt (math-exp-raw (math-add (math-mul a (math-ln-raw x))
  1513. X+                     (math-mul b (math-ln-raw
  1514. X+                              (math-sub '(float 1 0)
  1515. X+                                    x)))))))
  1516. X+     (if (math-lessp x (math-div (math-add a '(float 1 0))
  1517. X+                     (math-add (math-add a b) '(float 2 0))))
  1518. X+         (math-div (math-mul bt (math-beta-cfrac a b x)) a)
  1519. X+       (math-sub (or math-current-beta-value (calcFunc-beta a b))
  1520. X+             (math-div (math-mul bt
  1521. X+                     (math-beta-cfrac b a (math-sub 1 x)))
  1522. X+                   b)))))))
  1523. X+ )
  1524. X+ (setq math-current-beta-value nil)
  1525. X+ 
  1526. X+ (defun math-beta-cfrac (a b x)
  1527. X+   (let ((qab (math-add a b))
  1528. X+     (qap (math-add a '(float 1 0)))
  1529. X+     (qam (math-add a '(float -1 0))))
  1530. X+     (math-beta-cfrac-step '(float 1 0)
  1531. X+               (math-sub '(float 1 0)
  1532. X+                     (math-div (math-mul qab x) qap))
  1533. X+               '(float 1 0) '(float 1 0)
  1534. X+               '(float 1 0)
  1535. X+               qab qap qam a b x))
  1536. X+ )
  1537. X+ 
  1538. X+ (defun math-beta-cfrac-step (az bz am bm m qab qap qam a b x)
  1539. X+   (let* ((two-m (math-mul m '(float 2 0)))
  1540. X+      (d (math-div (math-mul (math-mul (math-sub b m) m) x)
  1541. X+               (math-mul (math-add qam two-m) (math-add a two-m))))
  1542. X+      (ap (math-add az (math-mul d am)))
  1543. X+      (bp (math-add bz (math-mul d bm)))
  1544. X+      (d2 (math-neg
  1545. X+           (math-div (math-mul (math-mul (math-add a m) (math-add qab m)) x)
  1546. X+             (math-mul (math-add qap two-m) (math-add a two-m)))))
  1547. X+      (app (math-add ap (math-mul d2 az)))
  1548. X+      (bpp (math-add bp (math-mul d2 bz)))
  1549. X+      (next (math-div app bpp)))
  1550. X+     (math-working "beta" next)
  1551. X+     (if (math-nearly-equal next az)
  1552. X+     next
  1553. X+       (math-beta-cfrac-step next '(float 1 0)
  1554. X+                 (math-div ap bpp) (math-div bp bpp)
  1555. X+                 (math-add m '(float 1 0))
  1556. X+                 qab qap qam a b x)))
  1557. X+ )
  1558. X+ 
  1559. X+ 
  1560. X+ ;;; Bessel functions.
  1561. X+ 
  1562. X+ ;;; Should generalize this to handle arbitrary precision!
  1563. X+ 
  1564. X+ (defun calcFunc-besJ (v x)
  1565. X+   (or (math-numberp v) (math-reject-arg v 'numberp))
  1566. X+   (or (math-numberp x) (math-reject-arg x 'numberp))
  1567. X+   (let ((calc-internal-prec (min 8 calc-internal-prec)))
  1568. X+     (math-with-extra-prec 3
  1569. X+       (setq x (math-float (math-normalize x)))
  1570. X+       (setq v (math-float (math-normalize v)))
  1571. X+       (cond ((math-zerop x)
  1572. X+          (if (math-zerop v)
  1573. X+          '(float 1 0)
  1574. X+            '(float 0 0)))
  1575. X+         ((math-inexact-result))
  1576. X+         ((not (math-num-integerp v))
  1577. X+          (let ((start (math-div 1 (calcFunc-fact v))))
  1578. X+            (math-mul (math-besJ-series start start
  1579. X+                        0
  1580. X+                        (math-mul '(float -25 -2)
  1581. X+                              (math-sqr x))
  1582. X+                        v)
  1583. X+              (math-pow (math-div x 2) v))))
  1584. X+         ((math-negp (setq v (math-trunc v)))
  1585. X+          (if (math-oddp v)
  1586. X+          (math-neg (calcFunc-besJ (math-neg v) x))
  1587. X+            (calcFunc-besJ (math-neg v) x)))
  1588. X+         ((eq v 0)
  1589. X+          (math-besJ0 x))
  1590. X+         ((eq v 1)
  1591. X+          (math-besJ1 x))
  1592. X+         ((math-lessp v (math-abs-approx x))
  1593. X+          (let ((j 0)
  1594. X+            (bjm (math-besJ0 x))
  1595. X+            (bj (math-besJ1 x))
  1596. X+            (two-over-x (math-div 2 x))
  1597. X+            bjp)
  1598. X+            (while (< (setq j (1+ j)) v)
  1599. X+          (setq bjp (math-sub (math-mul (math-mul j two-over-x) bj)
  1600. X+                      bjm)
  1601. X+                bjm bj
  1602. X+                bj bjp))
  1603. X+            bj))
  1604. X+         (t
  1605. X+          (if (math-lessp 100 v) (math-reject-arg v 'range))
  1606. X+          (let* ((j (logior (+ v (math-isqrt-small (* 40 v))) 1))
  1607. X+             (two-over-x (math-div 2 x))
  1608. X+             (jsum nil)
  1609. X+             (bjp '(float 0 0))
  1610. X+             (sum '(float 0 0))
  1611. X+             (bj '(float 1 0))
  1612. X+             bjm ans)
  1613. X+            (while (> (setq j (1- j)) 0)
  1614. X+          (setq bjm (math-sub (math-mul (math-mul j two-over-x) bj)
  1615. X+                      bjp)
  1616. X+                bjp bj
  1617. X+                bj bjm)
  1618. X+          (if (> (nth 2 (math-abs-approx bj)) 10)
  1619. X+              (setq bj (math-mul bj '(float 1 -10))
  1620. X+                bjp (math-mul bjp '(float 1 -10))
  1621. X+                ans (and ans (math-mul ans '(float 1 -10)))
  1622. X+                sum (math-mul sum '(float 1 -10))))
  1623. X+          (or (setq jsum (not jsum))
  1624. X+              (setq sum (math-add sum bj)))
  1625. X+          (if (= j v)
  1626. X+              (setq ans bjp)))
  1627. X+            (math-div ans (math-sub (math-mul 2 sum) bj)))))))
  1628. X+ )
  1629. X+ 
  1630. X+ (defun math-besJ-series (sum term k zz vk)
  1631. X+   (math-working "besJ" sum)
  1632. X+   (setq k (1+ k)
  1633. X+     vk (math-add 1 vk)
  1634. X+     term (math-div (math-mul term zz) (math-mul k vk)))
  1635. X+   (let ((next (math-add sum term)))
  1636. X+     (if (math-nearly-equal next sum)
  1637. X+     next
  1638. X+       (math-besJ-series next term k zz vk)))
  1639. X+ )
  1640. X+ 
  1641. X+ (defun math-besJ0 (x &optional yflag)
  1642. X+   (cond ((and (not yflag) (math-negp (math-real-part x)))
  1643. X+      (math-besJ0 (math-neg x)))
  1644. X+     ((math-lessp '(float 8 0) (math-abs-approx x))
  1645. X+      (let* ((z (math-div '(float 8 0) x))
  1646. X+         (y (math-sqr z))
  1647. X+         (xx (math-add x '(float (bigneg 164 398 785) -9)))
  1648. X+         (a1 (math-poly-eval y
  1649. X+                     '((float (bigpos 211 887 093 2) -16)
  1650. X+                       (float (bigneg 639 370 073 2) -15)
  1651. X+                       (float (bigpos 407 510 734 2) -14)
  1652. X+                       (float (bigneg 627 628 098 1) -12)
  1653. X+                       (float 1 0))))
  1654. X+         (a2 (math-poly-eval y
  1655. X+                     '((float (bigneg 152 935 934) -16)
  1656. X+                       (float (bigpos 161 095 621 7) -16)
  1657. X+                       (float (bigneg 651 147 911 6) -15)
  1658. X+                       (float (bigpos 765 488 430 1) -13)
  1659. X+                       (float (bigneg 995 499 562 1) -11))))
  1660. X+         (sc (math-sin-cos-raw xx)))
  1661. X+            (if yflag
  1662. X+            (setq sc (cons (math-neg (cdr sc)) (car sc))))
  1663. X+            (math-mul (math-sqrt
  1664. X+               (math-div '(float (bigpos 722 619 636) -9) x))
  1665. X+              (math-sub (math-mul (cdr sc) a1)
  1666. X+                    (math-mul (car sc) (math-mul z a2))))))
  1667. X+      (t
  1668. X+       (let ((y (math-sqr x)))
  1669. X+         (math-div (math-poly-eval y
  1670. X+                       '((float (bigneg 456 052 849 1) -7)
  1671. X+                     (float (bigpos 017 233 739 7) -5)
  1672. X+                     (float (bigneg 418 442 121 1) -2)
  1673. X+                     (float (bigpos 407 196 516 6) -1)
  1674. X+                     (float (bigneg 354 590 362 13) 0)
  1675. X+                     (float (bigpos 574 490 568 57) 0)))
  1676. X+               (math-poly-eval y
  1677. X+                       '((float 1 0)
  1678. X+                     (float (bigpos 712 532 678 2) -7)
  1679. X+                     (float (bigpos 853 264 927 5) -5)
  1680. X+                     (float (bigpos 718 680 494 9) -3)
  1681. X+                     (float (bigpos 985 532 029 1) 0)
  1682. X+                     (float (bigpos 411 490 568 57) 0)))))))
  1683. X+ )
  1684. X+ 
  1685. X+ (defun math-besJ1 (x &optional yflag)
  1686. X+   (cond ((and (math-negp (math-real-part x)) (not yflag))
  1687. X+      (math-neg (math-besJ1 (math-neg x))))
  1688. X+     ((math-lessp '(float 8 0) (math-abs-approx x))
  1689. X+      (let* ((z (math-div '(float 8 0) x))
  1690. X+         (y (math-sqr z))
  1691. X+         (xx (math-add x '(float (bigneg 491 194 356 2) -9)))
  1692. X+         (a1 (math-poly-eval y
  1693. X+                     '((float (bigneg 019 337 240) -15)
  1694. X+                       (float (bigpos 174 520 457 2) -15)
  1695. X+                       (float (bigneg 496 396 516 3) -14)
  1696. X+                       (float 183105 -8)
  1697. X+                       (float 1 0))))
  1698. X+         (a2 (math-poly-eval y
  1699. X+                     '((float (bigpos 412 787 105) -15)
  1700. X+                       (float (bigneg 987 228 88) -14)
  1701. X+                       (float (bigpos 096 199 449 8) -15)
  1702. X+                       (float (bigneg 873 690 002 2) -13)
  1703. X+                       (float (bigpos 995 499 687 4) -11))))
  1704. X+         (sc (math-sin-cos-raw xx)))
  1705. X+        (if yflag
  1706. X+            (setq sc (cons (math-neg (cdr sc)) (car sc)))
  1707. X+          (if (math-negp x)
  1708. X+          (setq sc (cons (math-neg (car sc)) (math-neg (cdr sc))))))
  1709. X+        (math-mul (math-sqrt (math-div '(float (bigpos 722 619 636) -9) x))
  1710. X+              (math-sub (math-mul (cdr sc) a1)
  1711. X+                    (math-mul (car sc) (math-mul z a2))))))
  1712. X+     (t
  1713. X+      (let ((y (math-sqr x)))
  1714. X+        (math-mul
  1715. X+         x
  1716. X+         (math-div (math-poly-eval y
  1717. X+                       '((float (bigneg 606 036 016 3) -8)
  1718. X+                     (float (bigpos 826 044 157) -4)
  1719. X+                     (float (bigneg 439 611 972 2) -3)
  1720. X+                     (float (bigpos 531 968 423 2) -1)
  1721. X+                     (float (bigneg 235 059 895 7) 0)
  1722. X+                     (float (bigpos 232 614 362 72) 0)))
  1723. X+               (math-poly-eval y
  1724. X+                       '((float 1 0)
  1725. X+                     (float (bigpos 397 991 769 3) -7)
  1726. X+                     (float (bigpos 394 743 944 9) -5)
  1727. X+                     (float (bigpos 474 330 858 1) -2)
  1728. X+                     (float (bigpos 178 535 300 2) 0)
  1729. X+                     (float (bigpos 442 228 725 144)
  1730. X+                            0))))))))
  1731. X+ )
  1732. X+ 
  1733. X+ (defun calcFunc-besY (v x)
  1734. X+   (math-inexact-result)
  1735. X+   (or (math-numberp v) (math-reject-arg v 'numberp))
  1736. X+   (or (math-numberp x) (math-reject-arg x 'numberp))
  1737. X+   (let ((calc-internal-prec (min 8 calc-internal-prec)))
  1738. X+     (math-with-extra-prec 3
  1739. X+       (setq x (math-float (math-normalize x)))
  1740. X+       (setq v (math-float (math-normalize v)))
  1741. X+       (cond ((not (math-num-integerp v))
  1742. X+          (let ((sc (math-sin-cos-raw (math-mul v (math-pi)))))
  1743. X+            (math-div (math-sub (math-mul (calcFunc-besJ v x) (cdr sc))
  1744. X+                    (calcFunc-besJ (math-neg v) x))
  1745. X+              (car sc))))
  1746. X+         ((math-negp (setq v (math-trunc v)))
  1747. X+          (if (math-oddp v)
  1748. X+          (math-neg (calcFunc-besY (math-neg v) x))
  1749. X+            (calcFunc-besY (math-neg v) x)))
  1750. X+         ((eq v 0)
  1751. X+          (math-besY0 x))
  1752. X+         ((eq v 1)
  1753. X+          (math-besY1 x))
  1754. X+         (t
  1755. X+          (let ((j 0)
  1756. X+            (bym (math-besY0 x))
  1757. X+            (by (math-besY1 x))
  1758. X+            (two-over-x (math-div 2 x))
  1759. X+            byp)
  1760. X+            (while (< (setq j (1+ j)) v)
  1761. X+          (setq byp (math-sub (math-mul (math-mul j two-over-x) by)
  1762. X+                      bym)
  1763. X+                bym by
  1764. X+                by byp))
  1765. X+            by)))))
  1766. X+ )
  1767. X+ 
  1768. X+ (defun math-besY0 (x)
  1769. X+   (cond ((math-lessp (math-abs-approx x) '(float 8 0))
  1770. X+      (let ((y (math-sqr x)))
  1771. X+        (math-add
  1772. X+         (math-div (math-poly-eval y
  1773. X+                       '((float (bigpos 733 622 284 2) -7)
  1774. X+                     (float (bigneg 757 792 632 8) -5)
  1775. X+                     (float (bigpos 129 988 087 1) -2)
  1776. X+                     (float (bigneg 036 598 123 5) -1)
  1777. X+                     (float (bigpos 065 834 062 7) 0)
  1778. X+                     (float (bigneg 389 821 957 2) 0)))
  1779. X+               (math-poly-eval y
  1780. X+                       '((float 1 0)
  1781. X+                     (float (bigpos 244 030 261 2) -7)
  1782. X+                     (float (bigpos 647 472 474) -4)
  1783. X+                     (float (bigpos 438 466 189 7) -3)
  1784. X+                     (float (bigpos 648 499 452 7) -1)
  1785. X+                     (float (bigpos 269 544 076 40) 0))))
  1786. X+         (math-mul '(float (bigpos 772 619 636) -9)
  1787. X+               (math-mul (math-besJ0 x) (math-ln-raw x))))))
  1788. X+     ((math-negp (math-real-part x))
  1789. X+      (math-add (math-besJ0 (math-neg x) t)
  1790. X+            (math-mul '(cplx 0 2)
  1791. X+                  (math-besJ0 (math-neg x)))))
  1792. X+     (t
  1793. X+      (math-besJ0 x t)))
  1794. X+ )
  1795. X+ 
  1796. X+ (defun math-besY1 (x)
  1797. X+   (cond ((math-lessp (math-abs-approx x) '(float 8 0))
  1798. X+      (let ((y (math-sqr x)))
  1799. X+        (math-add
  1800. X+         (math-mul
  1801. X+          x
  1802. X+          (math-div (math-poly-eval y
  1803. X+                        '((float (bigpos 935 937 511 8) -6)
  1804. X+                      (float (bigneg 726 922 237 4) -3)
  1805. X+                      (float (bigpos 551 264 349 7) -1)
  1806. X+                      (float (bigneg 139 438 153 5) 1)
  1807. X+                      (float (bigpos 439 527 127) 4)
  1808. X+                      (float (bigneg 943 604 900 4) 3)))
  1809. X+                (math-poly-eval y
  1810. X+                        '((float 1 0)
  1811. X+                      (float (bigpos 885 632 549 3) -7)
  1812. X+                      (float (bigpos 605 042 102) -3)
  1813. X+                      (float (bigpos 002 904 245 2) -2)
  1814. X+                      (float (bigpos 367 650 733 3) 0)
  1815. X+                      (float (bigpos 664 419 244 4) 2)
  1816. X+                      (float (bigpos 057 958 249) 5)))))
  1817. X+         (math-mul '(float (bigpos 772 619 636) -9)
  1818. X+               (math-sub (math-mul (math-besJ1 x) (math-ln-raw x))
  1819. X+                 (math-div 1 x))))))
  1820. X+     ((math-negp (math-real-part x))
  1821. X+      (math-neg
  1822. X+       (math-add (math-besJ1 (math-neg x) t)
  1823. X+             (math-mul '(cplx 0 2)
  1824. X+                   (math-besJ1 (math-neg x))))))
  1825. X+     (t
  1826. X+      (math-besJ1 x t)))
  1827. X+ )
  1828. X+ 
  1829. X+ (defun math-poly-eval (x coefs)
  1830. X+   (let ((accum (car coefs)))
  1831. X+     (while (setq coefs (cdr coefs))
  1832. X+       (setq accum (math-add (car coefs) (math-mul accum x))))
  1833. X+     accum)
  1834. X+ )
  1835. X+ 
  1836. X+ 
  1837. X+ ;;;; Bernoulli and Euler polynomials and numbers.
  1838. X+ 
  1839. X+ (defun calcFunc-bern (n &optional x)
  1840. X+   (if (and x (not (math-zerop x)))
  1841. X+       (if (and calc-symbolic-mode (math-floatp x))
  1842. X+       (math-inexact-result)
  1843. X+     (math-build-polynomial-expr (math-bernoulli-coefs n) x))
  1844. X+     (or (math-num-natnump n) (math-reject-arg n 'natnump))
  1845. X+     (if (consp n)
  1846. X+     (progn
  1847. X+       (math-inexact-result)
  1848. X+       (math-float (math-bernoulli-number (math-trunc n))))
  1849. SHAR_EOF
  1850. echo "End of part 8, continue with part 9"
  1851. echo "9" > s2_seq_.tmp
  1852. exit 0
  1853.  
  1854.