PageRenderTime 65ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/maxima-5.27.0/src/cpoly.lisp

#
Lisp | 2020 lines | 1884 code | 62 blank | 74 comment | 0 complexity | 9664558b65e2882d388dcc44600e75cf MD5 | raw file
Possible License(s): GPL-2.0
  1. ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;;; The data in this file contains enhancments. ;;;;;
  4. ;;; ;;;;;
  5. ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
  6. ;;; All rights reserved ;;;;;
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  8. ;;; (c) Copyright 1981 Massachusetts Institute of Technology ;;;
  9. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  10. (in-package :maxima)
  11. (macsyma-module cpoly)
  12. ;;; This is a lisp version of algorithm 419 from the Communications of
  13. ;;; the ACM (p 97 vol 15 feb 1972) by Jenkins and Traub. That
  14. ;;; algorithm is followed very closely. Note the following
  15. ;;; modifications: arrays are indexed from 0 instead of 1. This means
  16. ;;; that the variables n and nn are one less than the acm verson. The
  17. ;;; zeros are put into the arrays pr-sl and pi-sl, rather than into
  18. ;;; their own arrays. The algorithm seems to benefit be taking are
  19. ;;; mre 0.01 times the published values.
  20. (declare-top (special $partswitch $keepfloat $demoivre $listconstvars
  21. $algebraic $ratfac $programmode))
  22. (declare-top (special *logbas* *infin* *are* *mre* *cr* *ci* *sr* *si*
  23. *tr* *ti* *zr* *zi* *n* *nn* *bool*
  24. *conv* *pvr* *pvi* *polysc* *polysc1*))
  25. (declare-top (special *pr-sl* *pi-sl* *shr-sl* *shi-sl* *qpr-sl* *qpi-sl* *hr-sl*
  26. *hi-sl* *qhr-sl* *qhi-sl*))
  27. (declare-top (special *u* *v* *a* *b* *c* *d* *a1* *a3* *a7* *e* *f* *g* *h*
  28. *szr* *szi* *lzr* *lzi* *nz* *ui* *vi* *s*))
  29. (defmvar $polyfactor nil
  30. "When T factor the polynomial over the real or complex numbers.")
  31. (defmfun $allroots (expr)
  32. (prog (degree *nn* var res $partswitch $keepfloat $demoivre $listconstvars
  33. $algebraic complex $ratfac den expr1)
  34. (setq $keepfloat t $listconstvars t $algebraic t)
  35. (setq expr1 (setq expr (meqhk expr)))
  36. (setq var (delete '$%i (cdr ($listofvars expr)) :test #'eq))
  37. (or var (setq var (list (gensym))))
  38. (cond ((not (= (length var) 1))
  39. (merror (intl:gettext "allroots: expected a polynomial in one variable; found variables ~M") `((mlist) ,@var)))
  40. ((setq var (car var))))
  41. (setq expr ($rat expr '$%i var)
  42. res (reverse (car (cdddar expr))))
  43. (do ((i (- (length res) (length (caddar expr))) (1- i)))
  44. ((= i 0))
  45. (setq res (cdr res)))
  46. (setq den (cddr expr) expr (cadr expr))
  47. ;;;check denominator is a complex number
  48. (cond ((numberp den) (setq den (list den 0)))
  49. ((eq (car den) (cadr res))
  50. (setq den (cddr den))
  51. (cond ((numberp (car den))
  52. (cond ((null (cddr den)) (setq den (list 0 (car den))))
  53. ((numberp (caddr den))
  54. (setq den (list (caddr den) (car den))))
  55. (t (cpoly-err expr1))))
  56. (t (cpoly-err expr1))))
  57. (t (cpoly-err expr1)))
  58. ;;;if the name variable has disappeared, this is caught here
  59. (setq *nn* 0)
  60. (cond ((numberp expr) (setq expr (list expr 0)))
  61. ((eq (car expr) (car res)) (setq *nn* 1))
  62. ((eq (car expr) (cadr res))
  63. (setq expr (cddr expr))
  64. (cond ((numberp (car expr))
  65. (cond ((null (cddr expr)) (setq expr (list 0 (car expr))))
  66. ((numberp (caddr expr))
  67. (setq expr (list (caddr expr) (car expr))))
  68. (t (cpoly-err expr1))))
  69. (t (cpoly-err expr1))))
  70. (t (cpoly-err expr1)))
  71. (cond ((= *nn* 0)
  72. (cond ($polyfactor
  73. (let ((*cr* 0.0) (*ci* 0.0))
  74. (cdivid-sl (float (car expr)) (float (cadr expr))
  75. (float (car den)) (float (cadr den)))
  76. (return (simplify (list '(mplus)
  77. (simplify (list '(mtimes) '$%i *ci*))
  78. *cr*)))))
  79. (t (return (list '(mlist simp)))))))
  80. (setq degree (cadr expr) *nn* (1+ degree))
  81. (setq *pr-sl* (make-array *nn* :initial-element 0.0))
  82. (setq *pi-sl* (make-array *nn* :initial-element 0.0))
  83. (or (catch 'notpoly
  84. (errset (do ((expr (cdr expr) (cddr expr)) (l) (%i (cadr res)))
  85. ((null expr))
  86. (setq l (- degree (car expr)) res (cadr expr))
  87. (cond ((numberp res) (setf (aref *pr-sl* l) (float res)))
  88. (t
  89. (or (eq (car res) %i)
  90. (throw 'notpoly nil))
  91. (setq res (cddr res))
  92. (setf (aref *pi-sl* l) (float (car res)))
  93. (setq res (caddr res))
  94. (and res (setf (aref *pr-sl* l) (float res)))
  95. (setq complex t))))))
  96. ;;this should catch expressions like sin(x)-x
  97. (cpoly-err expr1))
  98. (setq *shr-sl* (make-array *nn* :initial-element 0.0))
  99. (setq *shi-sl* (make-array *nn* :initial-element 0.0))
  100. (setq *qpr-sl* (make-array *nn* :initial-element 0.0))
  101. (setq *hr-sl* (make-array degree :initial-element 0.0))
  102. (setq *qhr-sl* (make-array degree :initial-element 0.0))
  103. (setq *qpi-sl* (make-array *nn* :initial-element 0.0))
  104. (when complex
  105. (setq *hi-sl* (make-array degree :initial-element 0.0))
  106. (setq *qhi-sl* (make-array degree :initial-element 0.0)))
  107. (setq *nn* degree)
  108. (if complex
  109. (setq res (errset (cpoly-sl degree)))
  110. (setq res (errset (rpoly-sl degree))))
  111. (unless res
  112. (mtell (intl:gettext "allroots: unexpected error; treat results with caution.~%")))
  113. (when (= *nn* degree)
  114. (merror (intl:gettext "allroots: no roots found.")))
  115. (setq res nil)
  116. (cond ((not (zerop *nn*))
  117. (mtell (intl:gettext "allroots: only ~S out of ~S roots found.~%") (- degree *nn*) degree)
  118. (setq expr 0.0)
  119. (do ((i 0 (1+ i)))
  120. ((> i *nn*))
  121. (setq expr
  122. (simplify
  123. (list '(mplus) expr
  124. (simplify (list '(mtimes)
  125. (simplify (list '(mplus)
  126. (simplify (list '(mtimes) '$%i (aref *pi-sl* i)))
  127. (aref *pr-sl* i)))
  128. (simplify (list '(mexpt) var (- *nn* i)))))))))
  129. (setq res (cons expr res)))
  130. ($polyfactor
  131. (setq expr (let ((*cr* 0.0) (*ci* 0.0))
  132. (cdivid-sl (aref *pr-sl* 0) (aref *pi-sl* 0)
  133. (float (car den))
  134. (float (cadr den)))
  135. (simplify (list '(mplus) (simplify (list '(mtimes) '$%i *ci*)) *cr*)))
  136. res (cons expr res))))
  137. (do ((i degree (1- i)))
  138. ((= i *nn*))
  139. (setq expr (simplify (list '(mplus)
  140. (simplify (list '(mtimes) '$%i (aref *pi-sl* i)))
  141. (aref *pr-sl* i))))
  142. (setq res
  143. (cond ($polyfactor (cons (cond ((or complex (zerop (aref *pi-sl* i)))
  144. (simplify (list '(mplus) var (simplify (list '(mminus) expr)))))
  145. (t (setq i (1- i))
  146. (simplify (list '(mplus)
  147. (simplify (list '(mexpt) var 2))
  148. (simplify (list '(mtimes) var
  149. (aref *pr-sl* i)))
  150. (aref *pr-sl* (1+ i))))))
  151. res))
  152. ((cons (let ((expr (simplify (list '(mequal) var expr))))
  153. (if $programmode expr (displine expr)))
  154. res)))))
  155. (return (simplify (if $polyfactor
  156. (cons '(mtimes) res)
  157. (cons '(mlist) (nreverse res)))))))
  158. (defun cpoly-err (expr)
  159. (merror (intl:gettext "allroots: expected a polynomial; found ~M") expr))
  160. (defun cpoly-sl (degree)
  161. (let ((*logbas* (log 2.0))
  162. (*infin* most-positive-flonum)
  163. (*are* flonum-epsilon)
  164. (*mre* 0.0)
  165. (xx (sqrt 0.5))
  166. (yy 0.0)
  167. (cosr (cos (float (* 94/180 pi))))
  168. (sinr (sin (float (* 94/180 pi))))
  169. (*cr* 0.0) (*ci* 0.0)
  170. (*sr* 0.0) (*si* 0.0)
  171. (*tr* 0.0) (*ti* 0.0)
  172. (*zr* 0.0) (*zi* 0.0)
  173. (bnd 0.0)
  174. (*n* 0)
  175. (*polysc* 0)
  176. (*polysc1* 0)
  177. (*conv* nil))
  178. (setq *mre* (* 2.0 (sqrt 2.0) *are*)
  179. yy (- xx))
  180. (do ((i degree (1- i)))
  181. ((not (and (zerop (aref *pr-sl* i)) (zerop (aref *pi-sl* i))))
  182. (setq *nn* i
  183. *n* (1- i))))
  184. (setq degree *nn*)
  185. (do ((i 0 (1+ i)))
  186. ((> i *nn*))
  187. (setf (aref *shr-sl* i) (cmod-sl (aref *pr-sl* i) (aref *pi-sl* i))))
  188. (if (> *nn* 0) (scale-sl))
  189. (do ()
  190. ((> 2 *nn*)
  191. (cdivid-sl (- (aref *pr-sl* 1)) (- (aref *pi-sl* 1))
  192. (aref *pr-sl* 0) (aref *pi-sl* 0))
  193. (setf (aref *pr-sl* 1) *cr*)
  194. (setf (aref *pi-sl* 1) *ci*)
  195. (setq *nn* 0))
  196. (do ((i 0 (1+ i)))
  197. ((> i *nn*))
  198. (setf (aref *shr-sl* i) (cmod-sl (aref *pr-sl* i) (aref *pi-sl* i))))
  199. (setq bnd (cauchy-sl))
  200. (catch 'newroot
  201. (do ((cnt1 1 (1+ cnt1)))
  202. ((> cnt1 2))
  203. (noshft-sl 5)
  204. (do ((cnt2 1 (1+ cnt2)))
  205. ((> cnt2 9))
  206. (setq xx (prog1
  207. (- (* cosr xx) (* sinr yy))
  208. (setq yy (+ (* sinr xx) (* cosr yy))))
  209. *sr* (* bnd xx)
  210. *si* (* bnd yy))
  211. (fxshft-sl (* 10 cnt2))
  212. (cond (*conv* (setf (aref *pr-sl* *nn*) *zr*)
  213. (setf (aref *pi-sl* *nn*) *zi*)
  214. (setq *nn* *n* *n* (1- *n*))
  215. (do ((i 0 (1+ i)))
  216. ((> i *nn*))
  217. (setf (aref *pr-sl* i) (aref *qpr-sl* i))
  218. (setf (aref *pi-sl* i) (aref *qpi-sl* i)))
  219. (throw 'newroot t))))))
  220. (or *conv* (return t)))
  221. (do ((i (1+ *nn*) (1+ i)))
  222. ((> i degree))
  223. (setf (aref *pr-sl* i) (scale-float (aref *pr-sl* i) *polysc1*))
  224. (setf (aref *pi-sl* i) (scale-float (aref *pi-sl* i) *polysc1*)))
  225. (do ((i 0 (1+ i)) (j (- *polysc* (* *polysc1* degree)) (+ j *polysc1*)))
  226. ((> i *nn*))
  227. (setf (aref *pr-sl* i) (scale-float (aref *pr-sl* i) j))
  228. (setf (aref *pi-sl* i) (scale-float (aref *pi-sl* i) j)))
  229. *nn*))
  230. (defun noshft-sl (l1)
  231. (do ((i 0 (1+ i))
  232. (xni (float *nn*) (1- xni))
  233. (t1 (/ (float *nn*))))
  234. ((> i *n*))
  235. (setf (aref *hr-sl* i) (* (aref *pr-sl* i) xni t1))
  236. (setf (aref *hi-sl* i) (* (aref *pi-sl* i) xni t1)))
  237. (do ((jj 1 (1+ jj)))
  238. ((> jj l1))
  239. (cond ((> (cmod-sl (aref *hr-sl* *n*) (aref *hi-sl* *n*))
  240. (* 10.0 *are* (cmod-sl (aref *pr-sl* *n*) (aref *pi-sl* *n*))))
  241. (cdivid-sl (- (aref *pr-sl* *nn*)) (- (aref *pi-sl* *nn*)) (aref *hr-sl* *n*) (aref *hi-sl* *n*))
  242. (setq *tr* *cr* *ti* *ci*)
  243. (do ((j *n* (1- j)) (t1) (t2))
  244. ((> 1 j))
  245. (setq t1 (aref *hr-sl* (1- j)) t2 (aref *hi-sl* (1- j)))
  246. (setf (aref *hr-sl* j) (- (+ (aref *pr-sl* j) (* t1 *tr*)) (* t2 *ti*)))
  247. (setf (aref *hi-sl* j) (+ (aref *pi-sl* j) (* t1 *ti*) (* t2 *tr*))))
  248. (setf (aref *hr-sl* 0) (aref *pr-sl* 0))
  249. (setf (aref *hi-sl* 0) (aref *pi-sl* 0)))
  250. (t (do ((j *n* (1- j)))
  251. ((> 1 j))
  252. (setf (aref *hr-sl* j) (aref *hr-sl* (1- j)))
  253. (setf (aref *hi-sl* j) (aref *hi-sl* (1- j))))
  254. (setf (aref *hr-sl* 0) 0.0)
  255. (setf (aref *hi-sl* 0) 0.0)))))
  256. (defun fxshft-sl (l2)
  257. (let ((test t)
  258. (pasd nil)
  259. (otr 0.0) (oti 0.0)
  260. (svsr 0.0) (svsi 0.0)
  261. (*bool* nil)
  262. (*pvr* 0.0) (*pvi* 0.0))
  263. (polyev-sl)
  264. (setq *conv* nil)
  265. (calct-sl)
  266. (do ((j 1 (1+ j)))
  267. ((> j l2))
  268. (setq otr *tr* oti *ti*)
  269. (nexth-sl)
  270. (calct-sl)
  271. (setq *zr* (+ *sr* *tr*) *zi* (+ *si* *ti*))
  272. (cond ((and (not *bool*) test (not (= j l2)))
  273. (cond ((> (* 0.5 (cmod-sl *zr* *zi*))
  274. (cmod-sl (- *tr* otr) (- *ti* oti)))
  275. (cond (pasd (do ((i 0 (1+ i)))
  276. ((> i *n*))
  277. (setf (aref *shr-sl* i) (aref *hr-sl* i))
  278. (setf (aref *shi-sl* i) (aref *hi-sl* i)))
  279. (setq svsr *sr* svsi *si*)
  280. (vrshft-sl 10.)
  281. (when *conv* (return nil))
  282. (setq test nil)
  283. (do ((i 0 (1+ i)))
  284. ((> i *n*))
  285. (setf (aref *hr-sl* i) (aref *shr-sl* i))
  286. (setf (aref *hi-sl* i) (aref *shi-sl* i)))
  287. (setq *sr* svsr *si* svsi)
  288. (polyev-sl)
  289. (calct-sl))
  290. ((setq pasd t))))
  291. ((setq pasd nil))))))
  292. (or *conv* (vrshft-sl 10))
  293. nil))
  294. (defun vrshft-sl (l3)
  295. (setq *conv* nil *sr* *zr* *si* *zi*)
  296. (do ((i 1 (1+ i))
  297. (bool1 nil)
  298. (mp) (ms) (omp) (relstp) (tp) (r1))
  299. ((> i l3))
  300. (polyev-sl)
  301. (setq mp (cmod-sl *pvr* *pvi*) ms (cmod-sl *sr* *si*))
  302. (cond ((> (* 20.0 (errev-sl ms mp)) mp)
  303. (setq *conv* t *zr* *sr* *zi* *si*)
  304. (return t)))
  305. (cond ((= i 1) (setq omp mp))
  306. ((or bool1 (> omp mp) (not (< relstp 0.05)))
  307. (if (> (* 0.1 mp) omp)
  308. (return t)
  309. (setq omp mp)))
  310. (t (setq tp relstp bool1 t)
  311. (when (> *are* relstp) (setq tp *are*))
  312. (setq r1 (sqrt tp)
  313. *sr* (prog1
  314. (- (* (1+ r1) *sr*) (* r1 *si*))
  315. (setq *si* (+ (* (1+ r1) *si*) (* r1 *sr*)))))
  316. (polyev-sl)
  317. (do ((j 1 (1+ j))) ((> j 5)) (calct-sl) (nexth-sl))
  318. (setq omp *infin*)))
  319. (calct-sl)
  320. (nexth-sl)
  321. (calct-sl)
  322. (or *bool*
  323. (setq relstp (/ (cmod-sl *tr* *ti*) (cmod-sl *sr* *si*))
  324. *sr* (+ *sr* *tr*)
  325. *si* (+ *si* *ti*)))))
  326. (defun calct-sl nil
  327. (do ((i 1 (1+ i))
  328. ($t)
  329. (hvr (setf (aref *qhr-sl* 0) (aref *hr-sl* 0)))
  330. (hvi (setf (aref *qhi-sl* 0) (aref *hi-sl* 0))))
  331. ((> i *n*)
  332. (setq *bool* (not (> (cmod-sl hvr hvi) (* 10.0 *are* (cmod-sl (aref *hr-sl* *n*) (aref *hi-sl* *n*))))))
  333. (cond ((not *bool*) (cdivid-sl (- *pvr*) (- *pvi*) hvr hvi) (setq *tr* *cr* *ti* *ci*))
  334. (t (setq *tr* 0.0 *ti* 0.0)))
  335. nil)
  336. (setq $t (- (+ (aref *hr-sl* i) (* hvr *sr*)) (* hvi *si*)))
  337. (setf (aref *qhi-sl* i) (setq hvi (+ (aref *hi-sl* i) (* hvr *si*) (* hvi *sr*))))
  338. (setf (aref *qhr-sl* i) (setq hvr $t))))
  339. (defun nexth-sl ()
  340. (cond (*bool* (do ((j 1 (1+ j)))
  341. ((> j *n*))
  342. (setf (aref *hr-sl* j) (aref *qhr-sl* (1- j)))
  343. (setf (aref *hi-sl* j) (aref *qhi-sl* (1- j))))
  344. (setf (aref *hr-sl* 0) 0.0)
  345. (setf (aref *hi-sl* 0) 0.0))
  346. (t (do ((j 1. (1+ j)) (t1) (t2))
  347. ((> j *n*))
  348. (setq t1 (aref *qhr-sl* (1- j)) t2 (aref *qhi-sl* (1- j)))
  349. (setf (aref *hr-sl* j) (- (+ (aref *qpr-sl* j) (* t1 *tr*)) (* t2 *ti*)))
  350. (setf (aref *hi-sl* j) (+ (aref *qpi-sl* j) (* t1 *ti*) (* t2 *tr*))))
  351. (setf (aref *hr-sl* 0) (aref *qpr-sl* 0))
  352. (setf (aref *hi-sl* 0) (aref *qpi-sl* 0))))
  353. nil)
  354. (defun polyev-sl ()
  355. (setq *pvr* (setf (aref *qpr-sl* 0) (aref *pr-sl* 0))
  356. *pvi* (setf (aref *qpi-sl* 0) (aref *pi-sl* 0)))
  357. (do ((i 1 (1+ i)) ($t))
  358. ((> i *nn*))
  359. (setq $t (- (+ (aref *pr-sl* i) (* *pvr* *sr*)) (* *pvi* *si*)))
  360. (setf (aref *qpi-sl* i) (setq *pvi* (+ (aref *pi-sl* i) (* *pvr* *si*) (* *pvi* *sr*))))
  361. (setf (aref *qpr-sl* i) (setq *pvr* $t))))
  362. (defun errev-sl (ms mp)
  363. (- (* (do ((j 0 (1+ j))
  364. (*e* (/ (* (cmod-sl (aref *qpr-sl* 0) (aref *qpi-sl* 0)) *mre*) (+ *are* *mre*))))
  365. ((> j *nn*) *e*)
  366. (setq *e* (+ (cmod-sl (aref *qpr-sl* j) (aref *qpi-sl* j)) (* *e* ms))))
  367. (+ *are* *mre*))
  368. (* mp *mre*)))
  369. ;; Compute a lower bound on the roots of the polynomial. Let the
  370. ;; polynomial be sum(a[k]*x^k,k,0,n). Then the lower bound is the
  371. ;; smallest real root of sum(|a[k]|*x^k,k,0,n-1)-a[n]. For our
  372. ;; purposes, this lower bound is computed to an accuracy of .005 or
  373. ;; so.
  374. (defun cauchy-sl ()
  375. (let ((x (exp (/ (- (log (aref *shr-sl* *nn*)) (log (aref *shr-sl* 0))) (float *nn*))))
  376. (xm 0.0))
  377. (setf (aref *shr-sl* *nn*) (- (aref *shr-sl* *nn*)))
  378. (cond ((not (zerop (aref *shr-sl* *n*)))
  379. (setq xm (- (/ (aref *shr-sl* *nn*) (aref *shr-sl* *n*))))
  380. (cond ((> x xm) (setq x xm)))))
  381. (do ((*f*))
  382. (nil)
  383. (setq xm (* 0.1 x) *f* (aref *shr-sl* 0))
  384. (do ((i 1 (1+ i))) ((> i *nn*)) (setq *f* (+ (aref *shr-sl* i) (* *f* xm))))
  385. (cond ((not (< 0.0 *f*)) (return t)))
  386. (setq x xm))
  387. (do ((dx x) (df) (*f*))
  388. ((> 5e-3 (abs (/ dx x))) x)
  389. (setq *f* (aref *shr-sl* 0) df *f*)
  390. (do ((i 1 (1+ i)))
  391. ((> i *n*))
  392. (setq *f* (+ (* *f* x) (aref *shr-sl* i)) df (+ (* df x) *f*)))
  393. (setq *f* (+ (* *f* x) (aref *shr-sl* *nn*)) dx (/ *f* df) x (- x dx)))))
  394. ;; Scale the coefficients of the polynomial to reduce the chance of
  395. ;; overflow or underflow. This is different from the algorithm given
  396. ;; in TOMS 419 and 493 which just scales the coefficients by some
  397. ;; fixed factor. Instead, this algorithm computes a scale factor for
  398. ;; the roots and adjusts the coefficients of the polynomial
  399. ;; appropriately.
  400. ;;
  401. ;; The scale factor for the roots is in *polysc1*. The roots are
  402. ;; scaled by 2^(*polysc1*). There is an additional scaling *polysc*
  403. ;; such that the coefficients of the polynomial are all scaled by
  404. ;; 2^(*polysc*).
  405. (defun scale-sl ()
  406. (do ((i 0 (1+ i)) (j 0) (x 0.0) (dx 0.0))
  407. ((> i *nn*)
  408. (setq x (/ x (float (- (1+ *nn*) j)))
  409. dx (/ (- (log (aref *shr-sl* *nn*)) (log (aref *shr-sl* 0))) (float *nn*))
  410. *polysc1* (floor (+ 0.5 (/ dx *logbas*)))
  411. x (+ x (* (float (* *polysc1* *nn*)) *logbas* 0.5))
  412. *polysc* (floor (+ 0.5 (/ x *logbas*)))))
  413. (cond ((zerop (aref *shr-sl* i)) (setq j (1+ j)))
  414. (t (setq x (+ x (log (aref *shr-sl* i)))))))
  415. (do ((i *nn* (1- i)) (j (- *polysc*) (+ j *polysc1*)))
  416. ((< i 0))
  417. (setf (aref *pr-sl* i) (scale-float (aref *pr-sl* i) j))
  418. (setf (aref *pi-sl* i) (scale-float (aref *pi-sl* i) j))))
  419. (defun cdivid-sl (ar ai br bi)
  420. (let ((r1 0.0))
  421. (cond ((and (zerop br) (zerop bi))
  422. (setq *cr* (setq *ci* *infin*)))
  423. ((> (abs bi) (abs br))
  424. (setq r1 (/ br bi)
  425. bi (+ bi (* br r1))
  426. br (+ ai (* ar r1))
  427. *cr* (/ br bi)
  428. br (- (* ai r1) ar)
  429. *ci* (/ br bi)))
  430. ((setq r1 (/ bi br)
  431. bi (+ br (* bi r1))
  432. br (+ ar (* ai r1))
  433. *cr* (/ br bi)
  434. br (- ai (* ar r1))
  435. *ci* (/ br bi)))))
  436. nil)
  437. (defun cmod-sl (ar ai)
  438. (setq ar (abs ar)
  439. ai (abs ai))
  440. (cond ((> ai ar) (setq ar (/ ar ai)) (* ai (sqrt (1+ (* ar ar)))))
  441. ((> ar ai) (setq ai (/ ai ar)) (* ar (sqrt (1+ (* ai ai)))))
  442. ((* (sqrt 2.0)
  443. ar))))
  444. ;;; This is the algorithm for doing real polynomials. It is algorithm
  445. ;;; 493 from acm toms vol 1 p 178 (1975) by jenkins. Note that array
  446. ;;; indexing starts from 0. The names of the arrays have been changed
  447. ;;; to be the same as for cpoly. The correspondence is: p - pr-sl, qp
  448. ;;; - qpr-sl, k - hr-sl, qk - qhr-sl, svk - shr-sl, temp - shi-sl.
  449. ;;; the roots *are* put in pr-sl and pi-sl. The variable *si* appears
  450. ;;; not to be used here
  451. (defun rpoly-sl (degree)
  452. (let ((*logbas* (log 2.0))
  453. (*infin* most-positive-flonum)
  454. (*are* flonum-epsilon)
  455. (*mre* 0.0)
  456. (xx (sqrt 0.5)) ;; sqrt(0.5)
  457. (yy 0.0)
  458. (cosr (cos (float (* 94/180 pi))))
  459. (sinr (sin (float (* 94/180 pi))))
  460. (aa 0.0)
  461. (cc 0.0)
  462. (bb 0.0)
  463. (bnd 0.0)
  464. (*sr* 0.0)
  465. (*u* 0.0)
  466. (*v* 0.0)
  467. (t1 0.0)
  468. (*szr* 0.0) (*szi* 0.0)
  469. (*lzr* 0.0) (*lzi* 0.0)
  470. (*nz* 0)
  471. (*n* 0)
  472. (*polysc* 0)
  473. (*polysc1* 0)
  474. (zerok 0)
  475. (conv1 t))
  476. (setq *mre* *are* yy (- xx))
  477. (do ((i degree (1- i))) ((not (zerop (aref *pr-sl* i))) (setq *nn* i *n* (1- i))))
  478. (setq degree *nn*)
  479. (do ((i 0 (1+ i))) ((> i *nn*)) (setf (aref *shr-sl* i) (abs (aref *pr-sl* i))))
  480. (if (> *nn* 0) (scale-sl))
  481. ;; Loop to find all roots
  482. (do nil
  483. ((< *nn* 3)
  484. (cond ((= *nn* 2)
  485. ;; Solve the final quadratic polynomial
  486. (quad-sl (aref *pr-sl* 0.) (aref *pr-sl* 1) (aref *pr-sl* 2))
  487. (cond ((and $polyfactor (not (zerop *szi*)))
  488. (setf (aref *pr-sl* 2) (/ (aref *pr-sl* 2) (aref *pr-sl* 0)))
  489. (setf (aref *pr-sl* 1) (/ (aref *pr-sl* 1) (aref *pr-sl* 0)))
  490. (setf (aref *pi-sl* 2) 1.0))
  491. (t (setf (aref *pr-sl* 2) *szr*)
  492. (setf (aref *pi-sl* 2) *szi*)
  493. (setf (aref *pr-sl* 1) *lzr*)
  494. (setf (aref *pi-sl* 1) *lzi*))))
  495. (t
  496. ;; Solve the final linear polynomial
  497. (setf (aref *pr-sl* 1) (- (/ (aref *pr-sl* 1) (aref *pr-sl* 0))))))
  498. (setq *nn* 0))
  499. ;; Calculate a lower bound on the modulus of the zeros.
  500. (do ((i 0 (1+ i))) ((> i *nn*)) (setf (aref *shr-sl* i) (abs (aref *pr-sl* i))))
  501. (setq bnd (cauchy-sl))
  502. ;; Compute derivative of polynomial. Save result in *hr-sl*.
  503. (do ((i 1 (1+ i)))
  504. ((> i *n*))
  505. (setf (aref *hr-sl* i) (/ (* (float (- *n* i)) (aref *pr-sl* i)) (float *n*))))
  506. (setf (aref *hr-sl* 0) (aref *pr-sl* 0))
  507. (setq aa (aref *pr-sl* *nn*) bb (aref *pr-sl* *n*) zerok (zerop (aref *hr-sl* *n*)))
  508. ;; Do 5 steps with no shift.
  509. (do ((jj 1 (1+ jj)))
  510. ((> jj 5.))
  511. (setq cc (aref *hr-sl* *n*))
  512. (cond (zerok (do ((j *n* (1- j)))
  513. ((< j 1))
  514. (setf (aref *hr-sl* j) (aref *hr-sl* (1- j))))
  515. (setf (aref *hr-sl* 0) 0.0)
  516. (setq zerok (zerop (aref *hr-sl* *n*))))
  517. (t (setq t1 (- (/ aa cc)))
  518. (do ((j *n* (1- j)))
  519. ((< j 1))
  520. (setf (aref *hr-sl* j) (+ (* t1 (aref *hr-sl* (1- j))) (aref *pr-sl* j))))
  521. (setf (aref *hr-sl* 0) (aref *pr-sl* 0))
  522. (setq zerok (not (> (abs (aref *hr-sl* *n*))
  523. (* (abs bb) *are* 10.0)))))))
  524. (do ((i 0 (1+ i))) ((> i *n*)) (setf (aref *shi-sl* i) (aref *hr-sl* i)))
  525. (do ((cnt 1 (1+ cnt)))
  526. ((> cnt 20.) (setq conv1 nil))
  527. (setq xx (prog1
  528. (- (* cosr xx) (* sinr yy))
  529. (setq yy (+ (* sinr xx) (* cosr yy))))
  530. *sr* (* bnd xx)
  531. *u* (* -2.0 *sr*)
  532. *v* bnd)
  533. (fxshfr-sl (* 20 cnt))
  534. (cond ((> *nz* 0)
  535. (setf (aref *pr-sl* *nn*) *szr*)
  536. (setf (aref *pi-sl* *nn*) *szi*)
  537. (cond ((= *nz* 2)
  538. (setf (aref *pr-sl* *n*) *lzr*)
  539. (setf (aref *pi-sl* *n*) *lzi*)
  540. (cond ((and $polyfactor (not (zerop *szi*)))
  541. (setf (aref *pr-sl* *nn*) *v*)
  542. (setf (aref *pr-sl* *n*) *u*)
  543. (setf (aref *pi-sl* *nn*) 1.0)))))
  544. (setq *nn* (- *nn* *nz*) *n* (1- *nn*))
  545. (do ((i 0 (1+ i))) ((> i *nn*)) (setf (aref *pr-sl* i) (aref *qpr-sl* i)))
  546. (return nil)))
  547. (do ((i 0 (1+ i))) ((> i *n*)) (setf (aref *hr-sl* i) (aref *shi-sl* i))))
  548. (or conv1 (return nil)))
  549. (cond ($polyfactor
  550. (do ((i degree (1- i)))
  551. ((= i *nn*))
  552. (cond ((zerop (aref *pi-sl* i))
  553. (setf (aref *pr-sl* i) (scale-float (aref *pr-sl* i) *polysc1*)))
  554. (t (setf (aref *pr-sl* i) (scale-float (aref *pr-sl* i) (* 2 *polysc1*)))
  555. (setq i (1- i))
  556. (setf (aref *pr-sl* i) (scale-float (aref *pr-sl* i) *polysc1*))))))
  557. (t (do ((i (1+ *nn*) (1+ i)))
  558. ((> i degree))
  559. (setf (aref *pr-sl* i) (scale-float (aref *pr-sl* i) *polysc1*))
  560. (setf (aref *pi-sl* i) (scale-float (aref *pi-sl* i) *polysc1*)))))
  561. (do ((i 0 (1+ i)) (j (- *polysc* (* *polysc1* degree)) (+ j *polysc1*)))
  562. ((> i *nn*))
  563. (setf (aref *pr-sl* i) (scale-float (aref *pr-sl* i) j)))))
  564. (defun fxshfr-sl (l2)
  565. (let ((*my-type* 0)
  566. (*a* 0.0) (*b* 0.0) (*c* 0.0) (*d* 0.0) (*e* 0.0) (*f* 0.0) (*g* 0.0) (*h* 0.0)
  567. (*a1* 0.0) (*a3* 0.0) (*a7* 0.0))
  568. (declare (special *my-type*))
  569. (setq *nz* 0)
  570. (quadsd-sl)
  571. (calcsc-sl)
  572. (do ((j 1 (1+ j))
  573. (betav 0.25)
  574. (betas 0.25)
  575. (oss *sr*)
  576. (ovv *v*)
  577. (tvv) (tss) (ss) (vv) (tv) (ts) (ots) (otv)
  578. (*ui*) (*vi*) (*s*) (svv) (svu) (iflag) (vpass) (spass) (vtry) (stry))
  579. ((> j l2))
  580. (nextk-sl)
  581. (calcsc-sl)
  582. (newest-sl)
  583. (setq vv *vi*
  584. ss 0.0)
  585. (or (zerop (aref *hr-sl* *n*))
  586. (setq ss (- (/ (aref *pr-sl* *nn*) (aref *hr-sl* *n*)))))
  587. (setq tv 1.0 ts 1.0)
  588. (cond ((not (or (= j 1) (= *my-type* 3)))
  589. (or (zerop vv) (setq tv (abs (/ (- vv ovv) vv))))
  590. (or (zerop ss) (setq ts (abs (/ (- ss oss) ss))))
  591. (setq tvv 1.0)
  592. (and (< tv otv) (setq tvv (* tv otv)))
  593. (setq tss 1.0)
  594. (and (< ts ots) (setq tss (* ts ots)))
  595. (setq vpass (< tvv betav) spass (< tss betas))
  596. (cond ((or spass vpass)
  597. (setq svu *u* svv *v*)
  598. (do ((i 0 (1+ i)))
  599. ((> i *n*)) (setf (aref *shr-sl* i)
  600. (aref *hr-sl* i)))
  601. (setq *s* ss vtry nil stry nil)
  602. (and (do ((*bool* (not (and spass (or (not vpass) (< tss tvv)))) t)
  603. (l50 nil nil))
  604. (nil)
  605. (cond (*bool* (quadit-sl)
  606. (and (> *nz* 0) (return t))
  607. (setq vtry t
  608. betav (* 0.25 betav))
  609. (cond ((or stry (not spass))
  610. (setq l50 t))
  611. (t (do ((i 0 (1+ i)))
  612. ((> i *n*))
  613. (setf (aref *hr-sl* i)
  614. (aref *shr-sl* i)))))))
  615. (cond ((not l50)
  616. (setq iflag (realit-sl))
  617. (and (> *nz* 0) (return t))
  618. (setq stry t betas (* 0.25 betas))
  619. (cond ((zerop iflag) (setq l50 t))
  620. (t (setq *ui* (- (+ *s* *s*))
  621. *vi* (* *s* *s*))))))
  622. (cond (l50 (setq *u* svu *v* svv)
  623. (do ((i 0 (1+ i)))
  624. ((> i *n*))
  625. (setf (aref *hr-sl* i)
  626. (aref *shr-sl* i)))
  627. (and (or (not vpass) vtry)
  628. (return nil)))))
  629. (return nil))
  630. (quadsd-sl)
  631. (calcsc-sl)))))
  632. (setq ovv vv
  633. oss ss
  634. otv tv
  635. ots ts))))
  636. (defun quadit-sl nil
  637. (setq *nz* 0 *u* *ui* *v* *vi*)
  638. (do ((tried) (j 0) (ee) (mp) (relstp) (omp) (ms))
  639. (nil)
  640. (quad-sl 1.0 *u* *v*)
  641. (and (> (abs (- (abs *szr*) (abs *lzr*))) (* 1e-2 (abs *lzr*)))
  642. (return nil))
  643. (quadsd-sl)
  644. (setf mp (+ (abs (- *a* (* *szr* *b*))) (abs (* *szi* *b*))))
  645. (setf ms (cmod-sl *szr* *szi*))
  646. (setf ee (errev-sl ms mp))
  647. (cond ((not (> mp (* 2e1 ee))) (setq *nz* 2)
  648. (return nil)))
  649. (setq j (1+ j))
  650. (and (> j 20) (return nil))
  651. (cond ((not (or (< j 2) (> relstp 1e-2) (< mp omp) tried))
  652. (and (< relstp *are*) (setq relstp *are*))
  653. (setq relstp (sqrt relstp)
  654. *u* (- *u* (* *u* relstp))
  655. *v* (+ *v* (* *v* relstp)))
  656. (quadsd-sl)
  657. (do ((i 1 (1+ i)))
  658. ((> i 5)) (calcsc-sl) (nextk-sl))
  659. (setq tried t j 0)))
  660. (setq omp mp)
  661. (calcsc-sl)
  662. (nextk-sl)
  663. (calcsc-sl)
  664. (newest-sl)
  665. (and (zerop *vi*) (return nil))
  666. (setq relstp (abs (/ (- *vi* *v*) *vi*)) *u* *ui* *v* *vi*)))
  667. (defun realit-sl ()
  668. (setq *nz* 0)
  669. (do ((j 0) (pv) (ee) (ms) (mp) (kv) (t1) (omp))
  670. (nil)
  671. (setq pv (aref *pr-sl* 0))
  672. (setf (aref *qpr-sl* 0) pv)
  673. (do ((i 1 (1+ i)))
  674. ((> i *nn*))
  675. (setq pv (+ (* pv *s*) (aref *pr-sl* i)))
  676. (setf (aref *qpr-sl* i) pv))
  677. (setq mp (abs pv)
  678. ms (abs *s*)
  679. ee (* (/ *mre* (+ *are* *mre*)) (abs (aref *qpr-sl* 0))))
  680. (do ((i 1 (1+ i)))
  681. ((> i *nn*)) (setq ee (+ (* ee ms) (abs (aref *qpr-sl* i)))))
  682. (cond ((not (> mp (* 2e1 (- (* (+ *are* *mre*) ee) (* *mre* mp)))))
  683. (setq *nz* 1 *szr* *s* *szi* 0.0)
  684. (return 0)))
  685. (setq j (1+ j))
  686. (and (> j 10) (return 0))
  687. (cond ((not (or (< j 2)
  688. (> (abs t1) (* 1e-3 (abs (- *s* t1))))
  689. (not (> mp omp))))
  690. (return 1)))
  691. (setq omp mp kv (aref *hr-sl* 0))
  692. (setf (aref *qhr-sl* 0) kv)
  693. (do ((i 1 (1+ i)))
  694. ((> i *n*))
  695. (setq kv (+ (* kv *s*) (aref *hr-sl* i)))
  696. (setf (aref *qhr-sl* i) kv))
  697. (cond ((> (abs kv) (* (abs (aref *hr-sl* *n*)) 1e1 *are*))
  698. (setq t1 (- (/ pv kv)))
  699. (setf (aref *hr-sl* 0) (aref *qpr-sl* 0))
  700. (do ((i 1 (1+ i)))
  701. ((> i *n*))
  702. (setf (aref *hr-sl* i)
  703. (+ (* t1 (aref *qhr-sl* (1- i))) (aref *qpr-sl* i)))))
  704. (t (setf (aref *hr-sl* 0) 0.0)
  705. (do ((i 1 (1+ i)))
  706. ((> i *n*)) (setf (aref *hr-sl* i) (aref *qhr-sl* (1- i))))))
  707. (setq kv (aref *hr-sl* 0))
  708. (do ((i 1 (1+ i)))
  709. ((> i *n*)) (setq kv (+ (* kv *s*) (aref *hr-sl* i))))
  710. (setq t1 0.0)
  711. (and (> (abs kv) (* (abs (aref *hr-sl* *n*)) 10.0 *are*))
  712. (setq t1 (- (/ pv kv))))
  713. (setq *s* (+ *s* t1))))
  714. (defun calcsc-sl ()
  715. (declare (special *my-type*))
  716. (setq *d* (aref *hr-sl* 0))
  717. (setf (aref *qhr-sl* 0) *d*)
  718. (setq *c* (- (aref *hr-sl* 1) (* *u* *d*)))
  719. (setf (aref *qhr-sl* 1) *c*)
  720. (do ((i 2 (1+ i))
  721. (c0))
  722. ((> i *n*))
  723. (setq c0 (- (aref *hr-sl* i) (* *u* *c*) (* *v* *d*)))
  724. (setf (aref *qhr-sl* i) c0)
  725. (setq *d* *c* *c* c0))
  726. (cond ((not (or (> (abs *c*) (* (abs (aref *hr-sl* *n*)) 1e2 *are*))
  727. (> (abs *d*) (* (abs (aref *hr-sl* (1- *n*))) 1e2 *are*))))
  728. (setq *my-type* 3))
  729. ((not (< (abs *d*) (abs *c*)))
  730. (setq *my-type* 2
  731. *e* (/ *a* *d*)
  732. *f* (/ *c* *d*)
  733. *g* (* *u* *b*)
  734. *h* (* *v* *b*)
  735. *a3* (+ (* (+ *a* *g*) *e*) (* *h* (/ *b* *d*)))
  736. *a1* (- (* *b* *f*) *a*)
  737. *a7* (+ (* (+ *f* *u*) *a*) *h*)))
  738. (t (setq *my-type* 1
  739. *e* (/ *a* *c*)
  740. *f* (/ *d* *c*)
  741. *g* (* *u* *e*)
  742. *h* (* *v* *b*)
  743. *a3* (+ (* *a* *e*) (* (+ (/ *h* *c*) *g*) *b*))
  744. *a1* (- *b* (* *a* (/ *d* *c*)))
  745. *a7* (+ *a* (* *g* *d*) (* *h* *f*)))))
  746. nil)
  747. (defun nextk-sl ()
  748. (declare (special *my-type*))
  749. (cond ((= *my-type* 3)
  750. (setf (aref *hr-sl* 0) 0.0)
  751. (setf (aref *hr-sl* 1) 0.0)
  752. (do ((i 2 (1+ i)))
  753. ((> i *n*)) (setf (aref *hr-sl* i) (aref *qhr-sl* (- i 2)))))
  754. ((> (abs *a1*) (* (abs (if (= *my-type* 1) *b* *a*)) 1e1 *are*))
  755. (setq *a7* (/ *a7* *a1*) *a3* (/ *a3* *a1*))
  756. (setf (aref *hr-sl* 0) (aref *qpr-sl* 0))
  757. (setf (aref *hr-sl* 1) (- (aref *qpr-sl* 1) (* *a7* (aref *qpr-sl* 0))))
  758. (do ((i 2 (1+ i)))
  759. ((> i *n*))
  760. (setf (aref *hr-sl* i)
  761. (+ (* *a3* (aref *qhr-sl* (- i 2)))
  762. (- (* *a7* (aref *qpr-sl* (1- i))))
  763. (aref *qpr-sl* i)))))
  764. (t (setf (aref *hr-sl* 0) 0.0)
  765. (setf (aref *hr-sl* 1) (- (* *a7* (aref *qpr-sl* 0))))
  766. (do ((i 2 (1+ i)))
  767. ((> i *n*))
  768. (setf (aref *hr-sl* i)
  769. (- (* *a3* (aref *qhr-sl* (- i 2)))
  770. (* *a7* (aref *qpr-sl* (1- i))))))))
  771. nil)
  772. (defun newest-sl ()
  773. (declare (special *my-type*))
  774. (let ((a4 0.0) (a5 0.0)
  775. (b1 0.0) (b2 0.0)
  776. (c1 0.0) (c2 0.0) (c3 0.0) (c4 0.0))
  777. (cond ((= *my-type* 3)
  778. (setq *ui* 0.0 *vi* 0.0))
  779. (t
  780. (if (= *my-type* 2)
  781. (setq a4 (+ (* (+ *a* *g*) *f*) *h*)
  782. a5 (+ (* (+ *f* *u*) *c*) (* *v* *d*)))
  783. (setq a4 (+ *a* (* *u* *b*) (* *h* *f*))
  784. a5 (+ *c* (* (+ *u* (* *v* *f*)) *d*))))
  785. (setq b1 (- (/ (aref *hr-sl* *n*) (aref *pr-sl* *nn*)))
  786. b2 (- (/ (+ (aref *hr-sl* (1- *n*)) (* b1 (aref *pr-sl* *n*))) (aref *pr-sl* *nn*)))
  787. c1 (* *v* b2 *a1*)
  788. c2 (* b1 *a7*)
  789. c3 (* b1 b1 *a3*)
  790. c4 (- c1 c2 c3)
  791. c1 (+ a5 (* b1 a4) (- c4)))
  792. (if (zerop c1)
  793. (setq *ui* 0.0 *vi* 0.0)
  794. (setq *ui* (- *u* (/ (+ (* *u* (+ c3 c2))
  795. (* *v* (+ (* b1 *a1*) (* b2 *a7*))))
  796. c1))
  797. *vi* (* *v* (1+ (/ c4 c1)))))))
  798. nil))
  799. ;; Divide the polynomial in *pr-sl* by the quadratic x^2 + (*u*)*x +
  800. ;; (*v*). Place the quotient in *qpr-sl* and the remainder in *a* and
  801. ;; *b*. I (rtoy) think the remainder polynomial is (*b*)*x + (*a*).
  802. (defun quadsd-sl ()
  803. (setq *b* (aref *pr-sl* 0))
  804. (setf (aref *qpr-sl* 0) *b*)
  805. (setq *a* (- (aref *pr-sl* 1) (* *u* *b*)))
  806. (setf (aref *qpr-sl* 1) *a*)
  807. (do ((i 2 (1+ i))
  808. (c0))
  809. ((> i *nn*))
  810. (setq c0 (- (aref *pr-sl* i) (* *u* *a*) (* *v* *b*)))
  811. (setf (aref *qpr-sl* i) c0)
  812. (setq *b* *a*
  813. *a* c0)))
  814. ;; Compute the zeros of the quadratic a0*z^2+b1*z+c0. The larger zero
  815. ;; is returned in *szr* and *szi*. The smaller zero is in *lzr* and
  816. ;; *lzi*.
  817. (defun quad-sl (a0 b1 c0)
  818. (setq *szr* 0.0 *szi* 0.0 *lzr* 0.0 *lzi* 0.0)
  819. (let ((b0 0.0)
  820. (l0 0.0)
  821. (*e* 0.0))
  822. ;; Handle the degenerate cases of a0 = 0 or c0 = 0 first.
  823. (cond ((zerop a0) (or (zerop b1) (setq *szr* (- (/ c0 b1)))))
  824. ((zerop c0) (setq *lzr* (- (/ b1 a0))))
  825. (t
  826. ;; Quadratic formula.
  827. (setq b0 (/ b1 2.0))
  828. (cond ((< (abs b0) (abs c0))
  829. (setq *e* a0)
  830. (and (< c0 0.0) (setq *e* (- a0)))
  831. (setq *e* (- (* b0 (/ b0 (abs c0))) *e*)
  832. l0 (* (sqrt (abs *e*)) (sqrt (abs c0)))))
  833. (t (setq *e* (- 1.0 (* (/ a0 b0) (/ c0 b0)))
  834. l0 (* (sqrt (abs *e*)) (abs b0)))))
  835. (cond ((< *e* 0.0)
  836. (setq *szr* (- (/ b0 a0))
  837. *lzr* *szr*
  838. *szi* (abs (/ l0 a0))
  839. *lzi* (- *szi*)))
  840. (t (or (< b0 0.0) (setq l0 (- l0)))
  841. (setq *lzr* (/ (- l0 b0) a0))
  842. (or (zerop *lzr*) (setq *szr* (/ c0 *lzr* a0)))))))
  843. nil))
  844. ;; This is a very straightforward conversion of $allroots to use
  845. ;; bfloats instead of floats.
  846. (defun bf-cpoly-err (expr)
  847. (merror (intl:gettext "bfallroots: expected a polynomial; found ~M") expr))
  848. (defun fpzerop (x)
  849. (equal '(0 0) x))
  850. ;; (ar+%i*ai)/(br+%i*bi) -> cr+%i*ci.
  851. (defun bf-cdivid-sl (ar ai br bi)
  852. (cond ((and (fpzerop br)
  853. (fpzerop bi))
  854. ;; Division by zero. Should we do something else besides set
  855. ;; both parts to be "infinity"?
  856. (setq *cr* (setq *ci* *infin*)))
  857. ((fpgreaterp (fpabs bi) (fpabs br))
  858. (let ((r1 (fpquotient br bi)))
  859. (setq bi (fpplus bi (fptimes* br r1))
  860. br (fpplus ai (fptimes* ar r1))
  861. *cr* (fpquotient br bi)
  862. br (fpdifference (fptimes* ai r1) ar)
  863. *ci* (fpquotient br bi))))
  864. (t
  865. (let ((r1 (fpquotient bi br)))
  866. (setq bi (fpplus br (fptimes* bi r1))
  867. br (fpplus ar (fptimes* ai r1))
  868. *cr* (fpquotient br bi)
  869. br (fpdifference ai (fptimes* ar r1))
  870. *ci* (fpquotient br bi))))))
  871. (defun fpsqrt (x)
  872. (fproot (bcons x) 2))
  873. (defun bf-cmod-sl (ar ai)
  874. (let ((ar (fpabs ar))
  875. (ai (fpabs ai)))
  876. (cond ((fpgreaterp ai ar)
  877. (setq ar (fpquotient ar ai))
  878. (fptimes* ai
  879. (fpsqrt (fpplus (fpone) (fptimes* ar ar)))))
  880. ((fpgreaterp ar ai)
  881. (setq ai (fpquotient ai ar))
  882. (fptimes* ar (fpsqrt (fpplus (fpone) (fptimes* ai ai)))))
  883. ((fptimes* (fpsqrt (intofp 2))
  884. ar)))))
  885. (defun bf-calct-sl nil
  886. (do ((i 1 (1+ i))
  887. (tt)
  888. (hvr (setf (aref *qhr-sl* 0) (aref *hr-sl* 0)))
  889. (hvi (setf (aref *qhi-sl* 0) (aref *hi-sl* 0))))
  890. ((> i *n*)
  891. (setq *bool*
  892. (not (fpgreaterp (bf-cmod-sl hvr hvi)
  893. (fptimes* (intofp 10)
  894. (fptimes* *are*
  895. (bf-cmod-sl (aref *hr-sl* *n*)
  896. (aref *hi-sl* *n*)))))))
  897. (cond ((not *bool*)
  898. (bf-cdivid-sl (fpminus *pvr*) (fpminus *pvi*) hvr hvi)
  899. (setq *tr* *cr*
  900. *ti* *ci*))
  901. (t (setq *tr* (intofp 0) *ti* (intofp 0))))
  902. nil)
  903. (setq tt (fpdifference (fpplus (aref *hr-sl* i)
  904. (fptimes* hvr *sr*))
  905. (fptimes* hvi *si*)))
  906. (setf (aref *qhi-sl* i)
  907. (setq hvi (fpplus (aref *hi-sl* i)
  908. (fpplus (fptimes* hvr *si*)
  909. (fptimes* hvi *sr*)))))
  910. (setf (aref *qhr-sl* i) (setq hvr tt))))
  911. (defun bf-nexth-sl ()
  912. (cond (*bool*
  913. (do ((j 1 (1+ j)))
  914. ((> j *n*))
  915. (setf (aref *hr-sl* j) (aref *qhr-sl* (1- j)))
  916. (setf (aref *hi-sl* j) (aref *qhi-sl* (1- j))))
  917. (setf (aref *hr-sl* 0) (intofp 0))
  918. (setf (aref *hi-sl* 0) (intofp 0)))
  919. (t
  920. (do ((j 1. (1+ j))
  921. (t1)
  922. (t2))
  923. ((> j *n*))
  924. (setq t1 (aref *qhr-sl* (1- j))
  925. t2 (aref *qhi-sl* (1- j)))
  926. (setf (aref *hr-sl* j)
  927. (fpdifference (fpplus (aref *qpr-sl* j)
  928. (fptimes* t1 *tr*))
  929. (fptimes* t2 *ti*)))
  930. (setf (aref *hi-sl* j)
  931. (fpplus (aref *qpi-sl* j)
  932. (fpplus (fptimes* t1 *ti*)
  933. (fptimes* t2 *tr*)))))
  934. (setf (aref *hr-sl* 0) (aref *qpr-sl* 0))
  935. (setf (aref *hi-sl* 0) (aref *qpi-sl* 0))))
  936. nil)
  937. (defun bf-polyev-sl ()
  938. (setq *pvr* (setf (aref *qpr-sl* 0) (aref *pr-sl* 0))
  939. *pvi* (setf (aref *qpi-sl* 0) (aref *pi-sl* 0)))
  940. (do ((i 1 (1+ i))
  941. (tt))
  942. ((> i *nn*))
  943. (setq tt (fpdifference (fpplus (aref *pr-sl* i) (fptimes* *pvr* *sr*))
  944. (fptimes* *pvi* *si*)))
  945. (setf (aref *qpi-sl* i)
  946. (setq *pvi* (fpplus (aref *pi-sl* i)
  947. (fpplus (fptimes* *pvr* *si*)
  948. (fptimes* *pvi* *sr*)))))
  949. (setf (aref *qpr-sl* i)
  950. (setq *pvr* tt))))
  951. (defun bf-errev-sl (ms mp)
  952. (fpdifference
  953. (fptimes* (do ((j 0 (1+ j))
  954. (e (fpquotient (fptimes* (bf-cmod-sl (aref *qpr-sl* 0) (aref *qpi-sl* 0))
  955. *mre*)
  956. (fpplus *are* *mre*))))
  957. ((> j *nn*) e)
  958. (setq e (fpplus (bf-cmod-sl (aref *qpr-sl* j) (aref *qpi-sl* j))
  959. (fptimes* e ms))))
  960. (fpplus *are* *mre*))
  961. (fptimes* mp *mre*)))
  962. (defun bf-cauchy-sl ()
  963. (let ((x (fpexp (fpquotient (fpdifference (fplog (aref *shr-sl* *nn*))
  964. (fplog (aref *shr-sl* 0)))
  965. (intofp *nn*))))
  966. (xm (intofp 0)))
  967. (setf (aref *shr-sl* *nn*) (fpminus (aref *shr-sl* *nn*)))
  968. (cond ((not (fpzerop (aref *shr-sl* *n*)))
  969. (setq xm (fpminus (fpquotient (aref *shr-sl* *nn*)
  970. (aref *shr-sl* *n*))))
  971. (cond ((fpgreaterp x xm) (setq x xm)))))
  972. (do ((f))
  973. (nil)
  974. (setq xm (fptimes* (intofp 0.1) x)
  975. f (aref *shr-sl* 0))
  976. (do ((i 1 (1+ i)))
  977. ((> i *nn*))
  978. (setq f (fpplus (aref *shr-sl* i)
  979. (fptimes* f xm))))
  980. ;;(cond ((not (< 0.0 f)) (return t)))
  981. (when (fpgreaterp (intofp 0) f)
  982. (return t))
  983. (setq x xm))
  984. (do ((dx x)
  985. (df)
  986. (f))
  987. ((fpgreaterp (intofp 5e-3)
  988. (fpabs (fpquotient dx x)))
  989. x)
  990. (setq f (aref *shr-sl* 0)
  991. df f)
  992. (do ((i 1 (1+ i)))
  993. ((> i *n*))
  994. (setq f (fpplus (fptimes* f x)
  995. (aref *shr-sl* i))
  996. df (fpplus (fptimes* df x)
  997. f)))
  998. (setq f (fpplus (fptimes* f x)
  999. (aref *shr-sl* *nn*))
  1000. dx (fpquotient f df)
  1001. x (fpdifference x dx)))))
  1002. (defun bf-scale-float (bf scale)
  1003. (destructuring-bind (mantissa exp)
  1004. bf
  1005. (if (zerop mantissa)
  1006. (list mantissa 0)
  1007. (list mantissa
  1008. (+ exp scale)))))
  1009. (defun bf-scale-sl ()
  1010. (do ((i 0 (1+ i))
  1011. (j 0)
  1012. (x (intofp 0))
  1013. (dx (intofp 0)))
  1014. ((> i *nn*)
  1015. (setq x (fpquotient x (intofp (- (1+ *nn*) j)))
  1016. dx (fpquotient (fpdifference (fplog (aref *shr-sl* *nn*))
  1017. (fplog (aref *shr-sl* 0)))
  1018. (intofp *nn*))
  1019. *polysc1* (fpentier (bcons (fpplus (cdr bfhalf)
  1020. (fpquotient dx *logbas*))))
  1021. x (fpplus x (fptimes* (intofp (* *polysc1* *nn*))
  1022. (fptimes* *logbas*
  1023. (cdr bfhalf))))
  1024. *polysc* (fpentier (bcons (fpplus (cdr bfhalf) (fpquotient x *logbas*))))))
  1025. (cond ((equalp (aref *shr-sl* i) (intofp 0))
  1026. (setq j (1+ j)))
  1027. (t
  1028. (setq x (fpplus x (fplog (aref *shr-sl* i)))))))
  1029. (do ((i *nn* (1- i))
  1030. (j (- *polysc*) (+ j *polysc1*)))
  1031. ((< i 0))
  1032. (setf (aref *pr-sl* i) (bf-scale-float (aref *pr-sl* i) j))
  1033. (setf (aref *pi-sl* i) (bf-scale-float (aref *pi-sl* i) j)))
  1034. nil)
  1035. (defun bf-noshft-sl (l1)
  1036. (do ((i 0 (1+ i))
  1037. (xni (intofp *nn*) (fpdifference xni (intofp 1)))
  1038. (t1 (fpquotient (fpone) (intofp *nn*))))
  1039. ((> i *n*))
  1040. (setf (aref *hr-sl* i) (fptimes* (aref *pr-sl* i)
  1041. (fptimes* xni t1)))
  1042. (setf (aref *hi-sl* i) (fptimes* (aref *pi-sl* i)
  1043. (fptimes* xni t1))))
  1044. (do ((jj 1 (1+ jj)))
  1045. ((> jj l1))
  1046. (cond ((fpgreaterp (bf-cmod-sl (aref *hr-sl* *n*) (aref *hi-sl* *n*))
  1047. (fptimes* (intofp 10)
  1048. (fptimes* *are*
  1049. (bf-cmod-sl (aref *pr-sl* *n*)
  1050. (aref *pi-sl* *n*)))))
  1051. (bf-cdivid-sl (fpminus (aref *pr-sl* *nn*))
  1052. (fpminus (aref *pi-sl* *nn*))
  1053. (aref *hr-sl* *n*)
  1054. (aref *hi-sl* *n*))
  1055. (setq *tr* *cr*
  1056. *ti* *ci*)
  1057. (do ((j *n* (1- j)) (t1) (t2))
  1058. ((> 1 j))
  1059. (setq t1 (aref *hr-sl* (1- j))
  1060. t2 (aref *hi-sl* (1- j)))
  1061. (setf (aref *hr-sl* j) (fpdifference (fpplus (aref *pr-sl* j)
  1062. (fptimes* t1 *tr*))
  1063. (fptimes* t2 *ti*)))
  1064. (setf (aref *hi-sl* j) (fpplus (aref *pi-sl* j)
  1065. (fpplus (fptimes* t1 *ti*)
  1066. (fptimes* t2 *tr*)))))
  1067. (setf (aref *hr-sl* 0) (aref *pr-sl* 0))
  1068. (setf (aref *hi-sl* 0) (aref *pi-sl* 0)))
  1069. (t (do ((j *n* (1- j)))
  1070. ((> 1 j))
  1071. (setf (aref *hr-sl* j) (aref *hr-sl* (1- j)))
  1072. (setf (aref *hi-sl* j) (aref *hi-sl* (1- j))))
  1073. (setf (aref *hr-sl* 0) (intofp 0))
  1074. (setf (aref *hi-sl* 0) (intofp 0))))))
  1075. (defun bf-vrshft-sl (l3)
  1076. (setq *conv* nil
  1077. *sr* *zr*
  1078. *si* *zi*)
  1079. (do ((i 1 (1+ i))
  1080. (bool1 nil)
  1081. (mp)
  1082. (ms)
  1083. (omp)
  1084. (relstp)
  1085. (tp)
  1086. (r1))
  1087. ((> i l3))
  1088. (bf-polyev-sl)
  1089. (setq mp (bf-cmod-sl *pvr* *pvi*)
  1090. ms (bf-cmod-sl *sr* *si*))
  1091. (cond ((fpgreaterp (fptimes* (intofp 20) (bf-errev-sl ms mp))
  1092. mp)
  1093. (setq *conv* t
  1094. *zr* *sr*
  1095. *zi* *si*)
  1096. (return t)))
  1097. (cond ((= i 1)
  1098. (setq omp mp))
  1099. ((or bool1
  1100. (fpgreaterp omp mp)
  1101. ;;(not (< relstp 0.05))
  1102. (fpgreaterp relstp (cdr bfhalf)))
  1103. (if (fpgreaterp (fptimes* (intofp 0.1) mp)
  1104. omp)
  1105. (return t)
  1106. (setq omp mp)))
  1107. (t
  1108. (setq tp relstp
  1109. bool1 t)
  1110. (when (fpgreaterp *are* relstp)
  1111. (setq tp *are*))
  1112. (setq r1 (fpsqrt tp)
  1113. *sr* (prog1
  1114. (fpdifference (fptimes* (fpplus (fpone) r1)
  1115. *sr*)
  1116. (fptimes* r1 *si*))
  1117. (setq *si* (fpplus (fptimes* (fpplus (fpone) r1)
  1118. *si*)
  1119. (fptimes* r1 *sr*)))))
  1120. (bf-polyev-sl)
  1121. (do ((j 1 (1+ j)))
  1122. ((> j 5))
  1123. (bf-calct-sl)
  1124. (bf-nexth-sl))
  1125. (setq omp *infin*)))
  1126. (bf-calct-sl)
  1127. (bf-nexth-sl)
  1128. (bf-calct-sl)
  1129. (or *bool*
  1130. (setq relstp (fpquotient (bf-cmod-sl *tr* *ti*)
  1131. (bf-cmod-sl *sr* *si*))
  1132. *sr* (fpplus *sr* *tr*)
  1133. *si* (fpplus *si* *ti*)))))
  1134. (defun bf-fxshft-sl (l2)
  1135. (let ((test t)
  1136. (pasd nil)
  1137. (otr (intofp 0))
  1138. (oti (intofp 0))
  1139. (svsr (intofp 0))
  1140. (svsi (intofp 0))
  1141. (*bool* nil)
  1142. (*pvr* (intofp 0))
  1143. (*pvi* (intofp 0)))
  1144. (bf-polyev-sl)
  1145. (setq *conv* nil)
  1146. (bf-calct-sl)
  1147. (do ((j 1 (1+ j)))
  1148. ((> j l2))
  1149. (setq otr *tr*
  1150. oti *ti*)
  1151. (bf-nexth-sl)
  1152. (bf-calct-sl)
  1153. (setq *zr* (fpplus *sr* *tr*)
  1154. *zi* (fpplus *si* *ti*))
  1155. (cond ((and (not *bool*)
  1156. test
  1157. (not (= j l2)))
  1158. (cond ((fpgreaterp (fptimes* (cdr bfhalf) (bf-cmod-sl *zr* *zi*))
  1159. (bf-cmod-sl (fpdifference *tr* otr)
  1160. (fpdifference *ti* oti)))
  1161. (cond (pasd
  1162. (do ((i 0 (1+ i)))
  1163. ((> i *n*))
  1164. (setf (aref *shr-sl* i) (aref *hr-sl* i))
  1165. (setf (aref *shi-sl* i) (aref *hi-sl* i)))
  1166. (setq svsr *sr* svsi *si*)
  1167. (bf-vrshft-sl 10.)
  1168. (when *conv* (return nil))
  1169. (setq test nil)
  1170. (do ((i 0 (1+ i)))
  1171. ((> i *n*))
  1172. (setf (aref *hr-sl* i) (aref *shr-sl* i))
  1173. (setf (aref *hi-sl* i) (aref *shi-sl* i)))
  1174. (setq *sr* svsr *si* svsi)
  1175. (bf-polyev-sl)
  1176. (bf-calct-sl))
  1177. ((setq pasd t))))
  1178. ((setq pasd nil))))))
  1179. (or *conv* (bf-vrshft-sl 10))
  1180. nil))
  1181. (defun bf-cpoly-sl (degree)
  1182. (let ( ;; Log of our floating point base. (Do we need this much accuracy?)
  1183. (*logbas* (fplog (intofp 2)))
  1184. ;; "Largest" bfloat. What should we use?
  1185. (*infin* (intofp most-positive-flonum))
  1186. ;; bfloat epsilon. 2^(-fpprec)
  1187. (*are* (bf-scale-float (intofp 2) (- fpprec)))
  1188. (*mre* (intofp 0))
  1189. (xx (fproot bfhalf 2))
  1190. (yy (intofp 0))
  1191. ;; cos(94deg). Probably don't need full bfloat precision here.
  1192. (cosr (intofp -0.0697564737441253007759588351941433286009032016527965250436172961370711270667891229125378568280742923028942076107741717160209821557740512756197740925891665208235244345674420755726285778495732000059330205461129612198466216775458241726113210999152981126990497403794217445425671287263223529689424188857433131142804))
  1193. ;; sin(94deg). Probably don't need full bfloat precision here.
  1194. (sinr (intofp 0.9975640502598242476131626806442550263693776603842215362259956088982191814110818430852892116754760376426967121558233963175758546629687044962793968705262246733087781690124900795021134880736278349857522534853084644420433826380899280074903993378273609249428279246573946968632240992430211366514177713203298481315))
  1195. (*cr* (intofp 0))
  1196. (*ci* (intofp 0))
  1197. (*sr* (intofp 0))
  1198. (*si* (intofp 0))
  1199. (*tr* (intofp 0))
  1200. (*ti* (intofp 0))
  1201. (*zr* (intofp 0))
  1202. (*zi* (intofp 0))
  1203. (bnd (intofp 0))
  1204. (*n* 0)
  1205. (*polysc* 0)
  1206. (*polysc1* 0)
  1207. (*conv* nil))
  1208. (setq *mre* (fptimes* (intofp 2)
  1209. (fptimes* (fpsqrt (intofp 2)) *are*))
  1210. yy (fpminus xx))
  1211. (do ((i degree (1- i)))
  1212. ((not (and (equalp (intofp 0) (aref *pr-sl* i))
  1213. (equalp (intofp 0) (aref *pi-sl* i))))
  1214. (setq *nn* i
  1215. *n* (1- i))))
  1216. (setq degree *nn*)
  1217. (do ((i 0 (1+ i)))
  1218. ((> i *nn*))
  1219. (setf (aref *shr-sl* i) (bf-cmod-sl (aref *pr-sl* i) (aref *pi-sl* i))))
  1220. (if (> *nn* 0) (bf-scale-sl))
  1221. (do ()
  1222. ((> 2 *nn*)
  1223. (bf-cdivid-sl (fpminus (aref *pr-sl* 1))
  1224. (fpminus (aref *pi-sl* 1))
  1225. (aref *pr-sl* 0)
  1226. (aref *pi-sl* 0))
  1227. (setf (aref *pr-sl* 1) *cr*)
  1228. (setf (aref *pi-sl* 1) *ci*)
  1229. (setq *nn* 0))
  1230. (do ((i 0 (1+ i)))
  1231. ((> i *nn*))
  1232. (setf (aref *shr-sl* i) (bf-cmod-sl (aref *pr-sl* i) (aref *pi-sl* i))))
  1233. (setq bnd (bf-cauchy-sl))
  1234. (catch 'newroot
  1235. (do ((cnt1 1 (1+ cnt1)))
  1236. ((> cnt1 2))
  1237. (bf-noshft-sl 5)
  1238. (do ((cnt2 1 (1+ cnt2)))
  1239. ((> cnt2 9))
  1240. (setq xx (prog1
  1241. (fpdifference (fptimes* cosr xx)
  1242. (fptimes* sinr yy))
  1243. (setq yy (fpplus (fptimes* sinr xx)
  1244. (fptimes* cosr yy))))
  1245. *sr* (fptimes* bnd xx)
  1246. *si* (fptimes* bnd yy))
  1247. (bf-fxshft-sl (* 10 cnt2))
  1248. (cond (*conv* (setf (aref *pr-sl* *nn*) *zr*)
  1249. (setf (aref *pi-sl* *nn*) *zi*)
  1250. (setq *nn* *n* *n* (1- *n*))
  1251. (do ((i 0 (1+ i)))
  1252. ((> i *nn*))
  1253. (setf (aref *pr-sl* i) (aref *qpr-sl* i))
  1254. (setf (aref *pi-sl* i) (aref *qpi-sl* i)))
  1255. (throw 'newroot t))))))
  1256. (or *conv* (return t)))
  1257. (do ((i (1+ *nn*) (1+ i)))
  1258. ((> i degree))
  1259. (setf (aref *pr-sl* i) (bf-scale-float (aref *pr-sl* i) *polysc1*))
  1260. (setf (aref *pi-sl* i) (bf-scale-float (aref *pi-sl* i) *polysc1*)))
  1261. (do ((i 0 (1+ i)) (j (- *polysc* (* *polysc1* degree)) (+ j *polysc1*)))
  1262. ((> i *nn*))
  1263. (setf (aref *pr-sl* i) (bf-scale-float (aref *pr-sl* i) j))
  1264. (setf (aref *pi-sl* i) (bf-scale-float (aref *pi-sl* i) j)))
  1265. *nn*))
  1266. (defmfun $bfallroots (expr)
  1267. (prog (degree *nn* var res $partswitch
  1268. ($keepfloat t)
  1269. $demoivre
  1270. ($listconstvars t)
  1271. ($algebraic t) complex $ratfac den expr1)
  1272. (setq expr1 (setq expr (meqhk expr)))
  1273. (setq var (delete '$%i (cdr ($listofvars expr)) :test #'eq))
  1274. (or var (setq var (list (gensym))))
  1275. (cond ((not (= (length var) 1))
  1276. (merror (intl:gettext "bfallroots: expected a polynomial in one variable; found variables ~M") `((mlist) ,@var)))
  1277. ((setq var (car var))))
  1278. (setq expr ($rat expr '$%i var)
  1279. res (reverse (car (cdddar expr))))
  1280. (do ((i (- (length res)
  1281. (length (caddar expr)))
  1282. (1- i)))
  1283. ((= i 0))
  1284. (setq res (cdr res)))
  1285. (setq den (cddr expr)
  1286. expr (cadr expr))
  1287. ;; Check denominator is a complex number
  1288. (cond ((numberp den) (setq den (list den 0)))
  1289. ((eq (car den) (cadr res))
  1290. (setq den (cddr den))
  1291. (cond ((numberp (car den))
  1292. (cond ((null (cddr den))
  1293. (setq den (list 0 (car den))))
  1294. ((numberp (caddr den))
  1295. (setq den (list (caddr den) (car den))))
  1296. (t (bf-cpoly-err expr1))))
  1297. (t (bf-cpoly-err expr1))))
  1298. (t (bf-cpoly-err expr1)))
  1299. ;; If the name variable has disappeared, this is caught here
  1300. (setq *nn* 0)
  1301. (cond ((numberp expr)
  1302. (setq expr (list expr 0)))
  1303. ((eq (car expr) (car res))
  1304. (setq *nn* 1))
  1305. ((eq (car expr) (cadr res))
  1306. (setq expr (cddr expr))
  1307. (cond ((numberp (car expr))
  1308. (cond ((null (cddr expr))
  1309. (setq expr (list 0 (car expr))))
  1310. ((numberp (caddr expr))
  1311. (setq expr (list (caddr expr) (car expr))))
  1312. (t
  1313. (bf-cpoly-err expr1))))
  1314. (t (bf-cpoly-err expr1))))
  1315. (t (bf-cpoly-err expr1)))
  1316. (cond ((= *nn* 0)
  1317. (cond ($polyfactor
  1318. (let ((*cr* (intofp 0))
  1319. (*ci* (intofp 0)))
  1320. (bf-cdivid-sl (cdr ($bfloat (car expr)))
  1321. (cdr ($bfloat (cadr expr)))
  1322. (cdr ($bfloat (car den)))
  1323. (cdr ($bfloat (cadr den))))
  1324. (return (add (bcons *cr*)
  1325. (mul '$%i (bcons *ci*))))))
  1326. (t (return (list '(mlist simp)))))))
  1327. (setq degree (cadr expr) *nn* (1+ degree))
  1328. (setq *pr-sl* (make-array *nn* :initial-element (intofp 0)))
  1329. (setq *pi-sl* (make-array *nn* :initial-element (intofp 0)))
  1330. (or (catch 'notpoly
  1331. (errset (do ((expr (cdr expr) (cddr expr)) (l) (%i (cadr res)))
  1332. ((null expr))
  1333. (setq l (- degree (car expr)) res (cadr expr))
  1334. (cond ((numberp res)
  1335. (setf (aref *pr-sl* l) (cdr ($bfloat res))))
  1336. (t
  1337. (or (eq (car res) %i)
  1338. (throw 'notpoly nil))
  1339. (setq res (cddr res))
  1340. (setf (aref *pi-sl* l) (cdr ($bfloat (car res))))
  1341. (setq res (caddr res))
  1342. (and res (setf (aref *pr-sl* l) (cdr ($bfloat res))))
  1343. (setq complex t))))))
  1344. ;; This should catch expressions like sin(x)-x
  1345. (bf-cpoly-err expr1))
  1346. (setq *shr-sl* (make-array *nn* :initial-element (intofp 0)))
  1347. (setq *shi-sl* (make-array *nn* :initial-element (intofp 0)))
  1348. (setq *qpr-sl* (make-array *nn* :initial-element (intofp 0)))
  1349. (setq *hr-sl* (make-array degree :initial-element (intofp 0)))
  1350. (setq *qhr-sl* (make-array degree :initial-element (intofp 0)))
  1351. (setq *qpi-sl* (make-array *nn* :initial-element (intofp 0)))
  1352. (when complex
  1353. (setq *hi-sl* (make-array degree :initial-element (intofp 0)))
  1354. (setq *qhi-sl* (make-array degree :initial-element (intofp 0))))
  1355. (setq *nn* degree)
  1356. (if complex
  1357. (setq res (errset (bf-cpoly-sl degree)))
  1358. (setq res (bf-rpoly-sl degree)))
  1359. (unless res
  1360. (mtell (intl:gettext "bfallroots: unexpected error; treat results with caution.~%")))
  1361. (when (= *nn* degree)
  1362. (merror (intl:gettext "bfallroots: no roots found.")))
  1363. (setq res nil)
  1364. (cond ((not (zerop *nn*))
  1365. (mtell (intl:gettext "bfallroots: only ~S out of ~S roots found.~%") (- degree *nn*) degree)
  1366. (setq expr (bcons (intofp 0)))
  1367. (do ((i 0 (1+ i)))
  1368. ((> i *nn*))
  1369. (setq expr
  1370. (simplify
  1371. (list '(mplus) expr
  1372. (simplify (list '(mtimes)
  1373. (simplify (list '(mplus)
  1374. (simplify (list '(mtimes) '$%i
  1375. (bcons (aref *pi-sl* i))))
  1376. (bcons (aref *pr-sl* i))))
  1377. (simplify (list '(mexpt) var (- *nn* i)))))))))
  1378. (setq res (cons expr res)))
  1379. ($polyfactor
  1380. (setq expr (let ((*cr* (intofp 0))
  1381. (*ci* (intofp 0)))
  1382. (bf-cdivid-sl (aref *pr-sl* 0)
  1383. (aref *pi-sl* 0)
  1384. (cdr ($bfloat (car den)))
  1385. (cdr ($bfloat (cadr den))))
  1386. (add (bcons *cr*) (mul '$%i (bcons *ci*))))
  1387. res (cons expr res))))
  1388. (do ((i degree (1- i)))
  1389. ((= i *nn*))
  1390. ;; zr+%i*zi, where zr and zi parts of the root.
  1391. (setq expr (add (bcons (aref *pr-sl* i))
  1392. (mul '$%i (bcons (aref *pi-sl* i)))))
  1393. (setq res
  1394. (cond ($polyfactor
  1395. (cons (cond ((or complex (fpzerop (aref *pi-sl* i)))
  1396. (add var (mul -1 expr)))
  1397. (t
  1398. (setq i (1- i))
  1399. (simplify (list '(mplus)
  1400. (simplify (list '(mexpt) var 2))
  1401. (simplify (list '(mtimes) var
  1402. (bcons (aref *pr-sl* i))))
  1403. (bcons (aref *pr-sl* (1+ i)))))))
  1404. res))
  1405. (t
  1406. (cons (let ((expr (simplify (list '(mequal) var expr))))
  1407. (if $programmode expr (displine expr)))
  1408. res)))))
  1409. (return (simplify (if $polyfactor
  1410. (cons '(mtimes) res)
  1411. (cons '(mlist) (nreverse res)))))))
  1412. (defun bf-rpoly-sl (degree)
  1413. (let ((*logbas* (fplog (intofp 2)))
  1414. (*infin* (intofp most-positive-flonum))
  1415. (*are* (bf-scale-float (intofp 2) (- fpprec)))
  1416. (*mre* (intofp 0))
  1417. (xx (fproot bfhalf 2))
  1418. (yy (intofp 0))
  1419. ;; cos(94deg)
  1420. (cosr (intofp
  1421. -0.0697564737441253007759588351941433286009032016527965250436172961370711270667891229125378568280742923028942076107741717160209821557740512756197740925891665208235244345674420755726285778495732000059330205461129612198466216775458241726113210999152981126990497403794217445425671287263223529689424188857433131142804))
  1422. ;; sin(94deg)
  1423. (sinr (intofp
  1424. 0.9975640502598242476131626806442550263693776603842215362259956088982191814110818430852892116754760376426967121558233963175758546629687044962793968705262246733087781690124900795021134880736278349857522534853084644420433826380899280074903993378273609249428279246573946968632240992430211366514177713203298481315))
  1425. (aa (intofp 0))
  1426. (cc (intofp 0))
  1427. (bb (intofp 0))
  1428. (bnd (intofp 0))
  1429. (*sr* (intofp 0))
  1430. (*u* (intofp 0))
  1431. (*v* (intofp 0))
  1432. (t1 (intofp 0))
  1433. (*szr* (intofp 0))
  1434. (*szi* (intofp 0))
  1435. (*lzr* (intofp 0))
  1436. (*lzi* (intofp 0))
  1437. (*nz* 0)
  1438. (*n* 0)
  1439. (*polysc* 0)
  1440. (*polysc1* 0)
  1441. (zerok 0)
  1442. (conv1 t))
  1443. (setq *mre* *are*
  1444. yy (fpminus xx))
  1445. (do ((i degree (1- i)))
  1446. ((not (fpzerop (aref *pr-sl* i)))
  1447. (setq *nn* i *n* (1- i))))
  1448. (setq degree *nn*)
  1449. (do ((i 0 (1+ i)))
  1450. ((> i *nn*))
  1451. (setf (aref *shr-sl* i) (fpabs (aref *pr-sl* i))))
  1452. (if (> *nn* 0) (bf-scale-sl))
  1453. (do nil
  1454. ((< *nn* 3)
  1455. (cond ((= *nn* 2)
  1456. (bf-quad-sl (aref *pr-sl* 0.) (aref *pr-sl* 1) (aref *pr-sl* 2))
  1457. (cond ((and $polyfactor (not (fpzerop *szi*)))
  1458. (setf (aref *pr-sl* 2) (fpquotient (aref *pr-sl* 2)
  1459. (aref *pr-sl* 0)))
  1460. (setf (aref *pr-sl* 1) (fpquotient (aref *pr-sl* 1)
  1461. (aref *pr-sl* 0)))
  1462. (setf (aref *pi-sl* 2) (intofp 1)))
  1463. (t (setf (aref *pr-sl* 2) *szr*)
  1464. (setf (aref *pi-sl* 2) *szi*)
  1465. (setf (aref *pr-sl* 1) *lzr*)
  1466. (setf (aref *pi-sl* 1) *lzi*))))
  1467. (t
  1468. (setf (aref *pr-sl* 1) (fpminus (fpquotient (aref *pr-sl* 1)
  1469. (aref *pr-sl* 0))))))
  1470. (setq *nn* 0))
  1471. (do ((i 0 (1+ i)))
  1472. ((> i *nn*))
  1473. (setf (aref *shr-sl* i) (fpabs (aref *pr-sl* i))))
  1474. (setq bnd (bf-cauchy-sl))
  1475. (do ((i 1 (1+ i)))
  1476. ((> i *n*))
  1477. (setf (aref *hr-sl* i)
  1478. (fpquotient (fptimes* (intofp (- *n* i))
  1479. (aref *pr-sl* i))
  1480. (intofp *n*))))
  1481. (setf (aref *hr-sl* 0) (aref *pr-sl* 0))
  1482. (setq aa (aref *pr-sl* *nn*)
  1483. bb (aref *pr-sl* *n*)
  1484. zerok (fpzerop (aref *hr-sl* *n*)))
  1485. (do ((jj 1 (1+ jj)))
  1486. ((> jj 5.))
  1487. (setq cc (aref *hr-sl* *n*))
  1488. (cond (zerok (do ((j *n* (1- j)))
  1489. ((< j 1))
  1490. (setf (aref *hr-sl* j) (aref *hr-sl* (1- j))))
  1491. (setf (aref *hr-sl* 0) (intofp 0))
  1492. (setq zerok (fpzerop (aref *hr-sl* *n*))))
  1493. (t
  1494. (setq t1 (fpminus (fpquotient aa cc)))
  1495. (do ((j *n* (1- j)))
  1496. ((< j 1))
  1497. (setf (aref *hr-sl* j)
  1498. (fpplus (fptimes* t1 (aref *hr-sl* (1- j)))
  1499. (aref *pr-sl* j))))
  1500. (setf (aref *hr-sl* 0) (aref *pr-sl* 0))
  1501. (setq zerok (not (fpgreaterp (fpabs (aref *hr-sl* *n*))
  1502. (fptimes* (fpabs bb)
  1503. (fptimes* *are* (intofp 10)))))))))
  1504. (do ((i 0 (1+ i)))
  1505. ((> i *n*))
  1506. (setf (aref *shi-sl* i) (aref *hr-sl* i)))
  1507. (do ((cnt 1 (1+ cnt)))
  1508. ((> cnt 20.)
  1509. (setq conv1 nil))
  1510. (setq xx (prog1
  1511. (fpdifference (fptimes* cosr xx)
  1512. (fptimes* sinr yy))
  1513. (setq yy (fpplus (fptimes* sinr xx)
  1514. (fptimes* cosr yy))))
  1515. *sr* (fptimes* bnd xx)
  1516. *u* (fptimes* (intofp -2) *sr*)
  1517. *v* bnd)
  1518. (bf-fxshfr-sl (* 20 cnt))
  1519. (cond ((> *nz* 0)
  1520. (setf (aref *pr-sl* *nn*) *szr*)
  1521. (setf (aref *pi-sl* *nn*) *szi*)
  1522. (cond ((= *nz* 2)
  1523. (setf (aref *pr-sl* *n*) *lzr*)
  1524. (setf (aref *pi-sl* *n*) *lzi*)
  1525. (cond ((and $polyfactor (not (fpzerop *szi*)))
  1526. (setf (aref *pr-sl* *nn*) *v*)
  1527. (setf (aref *pr-sl* *n*) *u*)
  1528. (setf (aref *pi-sl* *nn*) (intofp 1))))))
  1529. (setq *nn* (- *nn* *nz*) *n* (1- *nn*))
  1530. (do ((i 0 (1+ i))) ((> i *nn*)) (setf (aref *pr-sl* i) (aref *qpr-sl* i)))
  1531. (return nil)))
  1532. (do ((i 0 (1+ i))) ((> i *n*)) (setf (aref *hr-sl* i) (aref *shi-sl* i))))
  1533. (or conv1 (return nil)))
  1534. (cond ($polyfactor
  1535. (do ((i degree (1- i)))
  1536. ((= i *nn*))
  1537. (cond ((fpzerop (aref *pi-sl* i))
  1538. (setf (aref *pr-sl* i) (bf-scale-float (aref *pr-sl* i) *polysc1*)))
  1539. (t (setf (aref *pr-sl* i) (bf-scale-float (aref *pr-sl* i) (* 2 *polysc1*)))
  1540. (setq i (1- i))
  1541. (setf (aref *pr-sl* i) (bf-scale-float (aref *pr-sl* i) *polysc1*))))))
  1542. (t (do ((i (1+ *nn*) (1+ i)))
  1543. ((> i degree))
  1544. (setf (aref *pr-sl* i) (bf-scale-float (aref *pr-sl* i) *polysc1*))
  1545. (setf (aref *pi-sl* i) (bf-scale-float (aref *pi-sl* i) *polysc1*)))))
  1546. (do ((i 0 (1+ i)) (j (- *polysc* (* *polysc1* degree)) (+ j *polysc1*)))
  1547. ((> i *nn*))
  1548. (setf (aref *pr-sl* i) (bf-scale-float (aref *pr-sl* i) j)))
  1549. t))
  1550. (defun bf-fxshfr-sl (l2)
  1551. (let ((*my-type* 0)
  1552. (*a* (intofp 0))
  1553. (*b* (intofp 0))
  1554. (*c* (intofp 0))
  1555. (*d* (intofp 0))
  1556. (*e* (intofp 0))
  1557. (*f* (intofp 0))
  1558. (*g* (intofp 0))
  1559. (*h* (intofp 0))
  1560. (*a1* (intofp 0))
  1561. (*a3* (intofp 0))
  1562. (*a7* (intofp 0)))
  1563. (declare (special *my-type*))
  1564. (setq *nz* 0)
  1565. (bf-quadsd-sl)
  1566. (bf-calcsc-sl)
  1567. (do ((j 1 (1+ j))
  1568. (betav (intofp 0.25))
  1569. (betas (intofp 0.25))
  1570. (oss *sr*)
  1571. (ovv *v*)
  1572. (tvv) (tss) (ss) (vv) (tv) (ts) (ots) (otv)
  1573. (*ui*) (*vi*) (*s*) (svv) (svu) (iflag) (vpass) (spass) (vtry) (stry))
  1574. ((> j l2))
  1575. (bf-nextk-sl)
  1576. (bf-calcsc-sl)
  1577. (bf-newest-sl)
  1578. (setq vv *vi*
  1579. ss (intofp 0))
  1580. (or (fpzerop (aref *hr-sl* *n*))
  1581. (setq ss (fpminus (fpquotient (aref *pr-sl* *nn*)
  1582. (aref *hr-sl* *n*)))))
  1583. (setq tv (intofp 1)
  1584. ts (intofp 1))
  1585. (cond ((not (or (= j 1)
  1586. (= *my-type* 3)))
  1587. (or (fpzerop vv)
  1588. (setq tv (fpabs (fpquotient (fpdifference vv ovv)
  1589. vv))))
  1590. (or (fpzerop ss)
  1591. (setq ts (fpabs (fpquotient (fpdifference ss oss)
  1592. ss))))
  1593. (setq tvv (intofp 1))
  1594. (and (not (fpgreaterp tv otv))
  1595. (setq tvv (fptimes* tv otv)))
  1596. (setq tss (intofp 1))
  1597. (and (not (fpgreaterp ts ots))
  1598. (setq tss (fptimes* ts ots)))
  1599. (setq vpass (not (fpgreaterp tvv betav))
  1600. spass (not (fpgreaterp tss betas)))
  1601. (cond ((or spass vpass)
  1602. (setq svu *u* svv *v*)
  1603. (do ((i 0 (1+ i)))
  1604. ((> i *n*))
  1605. (setf (aref *shr-sl* i)
  1606. (aref *hr-sl* i)))
  1607. (setq *s* ss vtry nil stry nil)
  1608. (and (do ((bool (not (and spass (or (not vpass)
  1609. (not (fpgreaterp tss tvv)))))
  1610. t)
  1611. (l50 nil nil))
  1612. (nil)
  1613. (cond (bool
  1614. (bf-quadit-sl)
  1615. (and (> *nz* 0) (return t))
  1616. (setq vtry t
  1617. betav (fptimes* (intofp 0.25) betav))
  1618. (cond ((or stry (not spass))
  1619. (setq l50 t))
  1620. (t (do ((i 0 (1+ i)))
  1621. ((> i *n*))
  1622. (setf (aref *hr-sl* i)
  1623. (aref *shr-sl* i)))))))
  1624. (cond ((not l50)
  1625. (setq iflag (bf-realit-sl))
  1626. (and (> *nz* 0) (return t))
  1627. (setq stry t
  1628. betas (fptimes* (intofp 0.25) betas))
  1629. (cond ((zerop iflag)
  1630. (setq l50 t))
  1631. (t
  1632. (setq *ui* (fpminus (fpplus *s* *s*))
  1633. *vi* (fptimes* *s* *s*))))))
  1634. (cond (l50
  1635. (setq *u* svu *v* svv)
  1636. (do ((i 0 (1+ i)))
  1637. ((> i *n*))
  1638. (setf (aref *hr-sl* i)
  1639. (aref *shr-sl* i)))
  1640. (and (or (not vpass) vtry)
  1641. (return nil)))))
  1642. (return nil))
  1643. (bf-quadsd-sl)
  1644. (bf-calcsc-sl)))))
  1645. (setq ovv vv
  1646. oss ss
  1647. otv tv
  1648. ots ts))))
  1649. (defun bf-quadit-sl nil
  1650. (setq *nz* 0 *u* *ui* *v* *vi*)
  1651. (do ((tried)
  1652. (j 0)
  1653. (ee)
  1654. (mp)
  1655. (relstp)
  1656. (omp)
  1657. (ms))
  1658. (nil)
  1659. (bf-quad-sl (intofp 1) *u* *v*)
  1660. (and (fpgreaterp (fpabs (fpdifference (fpabs *szr*)
  1661. (fpabs *lzr*)))
  1662. (fptimes* (intofp 1e-2) (fpabs *lzr*)))
  1663. (return nil))
  1664. (bf-quadsd-sl)
  1665. (setq mp (fpplus (fpabs (fpdifference *a* (fptimes* *szr* *b*)))
  1666. (fpabs (fptimes* *szi* *b*)))
  1667. ms (bf-cmod-sl *szr* *szi*)
  1668. ee (bf-errev-sl ms mp))
  1669. (cond ((not (fpgreaterp mp (fptimes* (intofp 2e1) ee)))
  1670. (setq *nz* 2)
  1671. (return nil)))
  1672. (setq j (1+ j))
  1673. (and (> j 20) (return nil))
  1674. (cond ((not (or (< j 2)
  1675. (fpgreaterp relstp (intofp 1e-2))
  1676. (not (fpgreaterp mp omp))
  1677. tried))
  1678. (and (not (fpgreaterp relstp *are*))
  1679. (setq relstp *are*))
  1680. (setq relstp (fpsqrt relstp)
  1681. *u* (fpdifference *u* (fptimes* *u* relstp))
  1682. *v* (fpplus *v* (fptimes* *v* relstp)))
  1683. (bf-quadsd-sl)
  1684. (do ((i 1 (1+ i)))
  1685. ((> i 5))
  1686. (bf-calcsc-sl)
  1687. (bf-nextk-sl))
  1688. (setq tried t j 0)))
  1689. (setq omp mp)
  1690. (bf-calcsc-sl)
  1691. (bf-nextk-sl)
  1692. (bf-calcsc-sl)
  1693. (bf-newest-sl)
  1694. (and (fpzerop *vi*) (return nil))
  1695. (setq relstp (fpabs (fpquotient (fpdifference *vi* *v*)
  1696. *vi*))
  1697. *u* *ui*
  1698. *v* *vi*)))
  1699. (defun bf-realit-sl ()
  1700. (setq *nz* 0)
  1701. (do ((j 0)
  1702. (pv)
  1703. (ee)
  1704. (ms)
  1705. (mp)
  1706. (kv)
  1707. (t1)
  1708. (omp))
  1709. (nil)
  1710. (setq pv (aref *pr-sl* 0))
  1711. (setf (aref *qpr-sl* 0) pv)
  1712. (do ((i 1 (1+ i)))
  1713. ((> i *nn*))
  1714. (setq pv (fpplus (fptimes* pv *s*)
  1715. (aref *pr-sl* i)))
  1716. (setf (aref *qpr-sl* i) pv))
  1717. (setq mp (fpabs pv)
  1718. ms (fpabs *s*)
  1719. ee (fptimes* (fpquotient *mre* (fpplus *are* *mre*))
  1720. (fpabs (aref *qpr-sl* 0))))
  1721. (do ((i 1 (1+ i)))
  1722. ((> i *nn*))
  1723. (setq ee (fpplus (fptimes* ee ms)
  1724. (fpabs (aref *qpr-sl* i)))))
  1725. (cond ((not (fpgreaterp mp
  1726. (fptimes* (intofp 2e1)
  1727. (fpdifference (fptimes* (fpplus *are* *mre*)
  1728. ee)
  1729. (fptimes* *mre* mp)))))
  1730. (setq *nz* 1 *szr* *s* *szi* (intofp 0))
  1731. (return 0)))
  1732. (setq j (1+ j))
  1733. (and (> j 10) (return 0))
  1734. (cond ((not (or (< j 2)
  1735. (fpgreaterp (fpabs t1)
  1736. (fptimes* (intofp 1e-3) (fpabs (fpdifference *s* t1))))
  1737. (not (fpgreaterp mp omp))))
  1738. (return 1)))
  1739. (setq omp mp kv (aref *hr-sl* 0))
  1740. (setf (aref *qhr-sl* 0) kv)
  1741. (do ((i 1 (1+ i)))
  1742. ((> i *n*))
  1743. (setq kv (fpplus (fptimes* kv *s*)
  1744. (aref *hr-sl* i)))
  1745. (setf (aref *qhr-sl* i) kv))
  1746. (cond ((fpgreaterp (fpabs kv)
  1747. (fptimes* (fpabs (aref *hr-sl* *n*))
  1748. (fptimes* (intofp 1e1) *are*)))
  1749. (setq t1 (fpminus (fpquotient pv kv)))
  1750. (setf (aref *hr-sl* 0) (aref *qpr-sl* 0))
  1751. (do ((i 1 (1+ i)))
  1752. ((> i *n*))
  1753. (setf (aref *hr-sl* i)
  1754. (fpplus (fptimes* t1 (aref *qhr-sl* (1- i)))
  1755. (aref *qpr-sl* i)))))
  1756. (t
  1757. (setf (aref *hr-sl* 0) (intofp 0))
  1758. (do ((i 1 (1+ i)))
  1759. ((> i *n*))
  1760. (setf (aref *hr-sl* i) (aref *qhr-sl* (1- i))))))
  1761. (setq kv (aref *hr-sl* 0))
  1762. (do ((i 1 (1+ i)))
  1763. ((> i *n*))
  1764. (setq kv (fpplus (fptimes* kv *s*)
  1765. (aref *hr-sl* i))))
  1766. (setq t1 (intofp 0))
  1767. (and (fpgreaterp (fpabs kv)
  1768. (fptimes* (fpabs (aref *hr-sl* *n*))
  1769. (fptimes* (intofp 10) *are*)))
  1770. (setq t1 (fpminus (fpquotient pv kv))))
  1771. (setq *s* (fpplus *s* t1))))
  1772. (defun bf-calcsc-sl ()
  1773. (declare (special *my-type*))
  1774. (setq *d* (aref *hr-sl* 0))
  1775. (setf (aref *qhr-sl* 0) *d*)
  1776. (setq *c* (fpdifference (aref *hr-sl* 1)
  1777. (fptimes* *u* *d*)))
  1778. (setf (aref *qhr-sl* 1) *c*)
  1779. (do ((i 2 (1+ i))
  1780. (c0))
  1781. ((> i *n*))
  1782. (setq c0 (fpdifference (fpdifference (aref *hr-sl* i)
  1783. (fptimes* *u* *c*))
  1784. (fptimes* *v* *d*)))
  1785. (setf (aref *qhr-sl* i) c0)
  1786. (setq *d* *c* *c* c0))
  1787. (cond ((not (or (fpgreaterp (fpabs *c*)
  1788. (fptimes* (fpabs (aref *hr-sl* *n*))
  1789. (fptimes* (intofp 100) *are*)))
  1790. (fpgreaterp (fpabs *d*)
  1791. (fptimes* (fpabs (aref *hr-sl* (1- *n*)))
  1792. (fptimes* (intofp 100) *are*)))))
  1793. (setq *my-type* 3))
  1794. ((not (not (fpgreaterp (fpabs *d*) (fpabs *c*))))
  1795. (setq *my-type* 2
  1796. *e* (fpquotient *a* *d*)
  1797. *f* (fpquotient *c* *d*)
  1798. *g* (fptimes* *u* *b*)
  1799. *h* (fptimes* *v* *b*)
  1800. *a3* (fpplus (fptimes* (fpplus *a* *g*) *e*)
  1801. (fptimes* *h* (fpquotient *b* *d*)))
  1802. *a1* (fpdifference (fptimes* *b* *f*)
  1803. *a*)
  1804. *a7* (fpplus (fptimes* (fpplus *f* *u*) *a*)
  1805. *h*)))
  1806. (t (setq *my-type* 1
  1807. *e* (fpquotient *a* *c*)
  1808. *f* (fpquotient *d* *c*)
  1809. *g* (fptimes* *u* *e*)
  1810. *h* (fptimes* *v* *b*)
  1811. *a3* (fpplus (fptimes* *a* *e*)
  1812. (fptimes* (fpplus (fpquotient *h* *c*)
  1813. *g*)
  1814. *b*))
  1815. *a1* (fpdifference *b*
  1816. (fptimes* *a* (fpquotient *d* *c*)))
  1817. *a7* (fpplus *a*
  1818. (fpplus (fptimes* *g* *d*)
  1819. (fptimes* *h* *f*))))))
  1820. nil)
  1821. (defun bf-nextk-sl ()
  1822. (declare (special *my-type*))
  1823. (cond ((= *my-type* 3)
  1824. (setf (aref *hr-sl* 0) (intofp 0))
  1825. (setf (aref *hr-sl* 1) (intofp 0))
  1826. (do ((i 2 (1+ i)))
  1827. ((> i *n*))
  1828. (setf (aref *hr-sl* i) (aref *qhr-sl* (- i 2)))))
  1829. ((fpgreaterp (fpabs *a1*)
  1830. (fptimes* (fpabs (if (= *my-type* 1) *b* *a*))
  1831. (fptimes* (intofp 1e1) *are*)))
  1832. (setq *a7* (fpquotient *a7* *a1*)
  1833. *a3* (fpquotient *a3* *a1*))
  1834. (setf (aref *hr-sl* 0) (aref *qpr-sl* 0))
  1835. (setf (aref *hr-sl* 1)
  1836. (fpdifference (aref *qpr-sl* 1)
  1837. (fptimes* *a7* (aref *qpr-sl* 0))))
  1838. (do ((i 2 (1+ i)))
  1839. ((> i *n*))
  1840. (setf (aref *hr-sl* i)
  1841. (fpplus (fptimes* *a3* (aref *qhr-sl* (- i 2)))
  1842. (fpplus (fpminus (fptimes* *a7* (aref *qpr-sl* (1- i))))
  1843. (aref *qpr-sl* i))))))
  1844. (t
  1845. (setf (aref *hr-sl* 0) (intofp 0))
  1846. (setf (aref *hr-sl* 1) (fpminus (fptimes* *a7* (aref *qpr-sl* 0))))
  1847. (do ((i 2 (1+ i)))
  1848. ((> i *n*))
  1849. (setf (aref *hr-sl* i)
  1850. (fpdifference (fptimes* *a3* (aref *qhr-sl* (- i 2)))
  1851. (fptimes* *a7* (aref *qpr-sl* (1- i))))))))
  1852. nil)
  1853. (defun bf-newest-sl ()
  1854. (declare (special *my-type*))
  1855. (let ((a4 (intofp 0))
  1856. (a5 (intofp 0))
  1857. (b1 (intofp 0))
  1858. (b2 (intofp 0))
  1859. (c1 (intofp 0))
  1860. (c2 (intofp 0))
  1861. (c3 (intofp 0))
  1862. (c4 (intofp 0)))
  1863. (cond ((= *my-type* 3)
  1864. (setq *ui* (intofp 0)
  1865. *vi* (intofp 0)))
  1866. (t
  1867. (if (= *my-type* 2)
  1868. (setq a4 (fpplus (fptimes* (fpplus *a* *g*)
  1869. *f*)
  1870. *h*)
  1871. a5 (fpplus (fptimes* (fpplus *f* *u*)
  1872. *c*)
  1873. (fptimes* *v* *d*)))
  1874. (setq a4 (fpplus *a*
  1875. (fpplus (fptimes* *u* *b*)
  1876. (fptimes* *h* *f*)))
  1877. a5 (fpplus *c*
  1878. (fptimes* (fpplus *u* (fptimes* *v* *f*))
  1879. *d*))))
  1880. (setq b1 (fpminus (fpquotient (aref *hr-sl* *n*)
  1881. (aref *pr-sl* *nn*)))
  1882. b2 (fpminus (fpquotient (fpplus (aref *hr-sl* (1- *n*))
  1883. (fptimes* b1 (aref *pr-sl* *n*)))
  1884. (aref *pr-sl* *nn*)))
  1885. c1 (fptimes* (fptimes* *v* b2) *a1*)
  1886. c2 (fptimes* b1 *a7*)
  1887. c3 (fptimes* (fptimes* b1 b1) *a3*)
  1888. c4 (fpdifference (fpdifference c1 c2) c3)
  1889. c1 (fpplus (fpplus a5 (fptimes* b1 a4))
  1890. (fpminus c4)))
  1891. (if (fpzerop c1)
  1892. (setq *ui* (intofp 0)
  1893. *vi* (intofp 0))
  1894. (setq *ui* (fpdifference
  1895. *u*
  1896. (fpquotient (fpplus (fptimes* *u* (fpplus c3 c2))
  1897. (fptimes* *v*
  1898. (fpplus (fptimes* b1 *a1*)
  1899. (fptimes* b2 *a7*))))
  1900. c1))
  1901. *vi* (fptimes* *v*
  1902. (fpplus (fpone) (fpquotient c4 c1)))))))
  1903. nil))
  1904. (defun bf-quadsd-sl ()
  1905. (setq *b* (aref *pr-sl* 0))
  1906. (setf (aref *qpr-sl* 0) *b*)
  1907. (setq *a* (fpdifference (aref *pr-sl* 1)
  1908. (fptimes* *u* *b*)))
  1909. (setf (aref *qpr-sl* 1) *a*)
  1910. (do ((i 2 (1+ i))
  1911. (c0))
  1912. ((> i *nn*))
  1913. (setq c0 (fpdifference (fpdifference (aref *pr-sl* i)
  1914. (fptimes* *u* *a*))
  1915. (fptimes* *v* *b*)))
  1916. (setf (aref *qpr-sl* i) c0)
  1917. (setq *b* *a*
  1918. *a* c0)))
  1919. (defun bf-quad-sl (a0 b1 c0)
  1920. (setq *szr* (intofp 0)
  1921. *szi* (intofp 0)
  1922. *lzr* (intofp 0)
  1923. *lzi* (intofp 0))
  1924. (let ((b0 (intofp 0))
  1925. (l0 (intofp 0))
  1926. (*e* (intofp 0)))
  1927. (cond ((fpzerop a0)
  1928. (or (fpzerop b1)
  1929. (setq *szr* (fpminus (fpquotient c0 b1)))))
  1930. ((fpzerop c0)
  1931. (setq *lzr* (fpminus (fpquotient b1 a0))))
  1932. (t
  1933. (setq b0 (fpquotient b1 (intofp 2)))
  1934. (cond ((not (fpgreaterp (fpabs b0) (fpabs c0)))
  1935. (setq *e* a0)
  1936. (and (not (fpgreaterp c0 (intofp 0)))
  1937. (setq *e* (fpminus a0)))
  1938. (setq *e* (fpdifference (fptimes* b0 (fpquotient b0 (fpabs c0)))
  1939. *e*)
  1940. l0 (fptimes* (fpsqrt (fpabs *e*))
  1941. (fpsqrt (fpabs c0)))))
  1942. (t (setq *e* (fpdifference (intofp 1)
  1943. (fptimes* (fpquotient a0 b0)
  1944. (fpquotient c0 b0)))
  1945. l0 (fptimes* (fpsqrt (fpabs *e*))
  1946. (fpabs b0)))))
  1947. (cond ((not (fpgreaterp *e* (intofp 0)))
  1948. (setq *szr* (fpminus (fpquotient b0 a0))
  1949. *lzr* *szr*
  1950. *szi* (fpabs (fpquotient l0 a0))
  1951. *lzi* (fpminus *szi*)))
  1952. (t (or (not (fpgreaterp b0 (intofp 0)))
  1953. (setq l0 (fpminus l0)))
  1954. (setq *lzr* (fpquotient (fpdifference l0 b0) a0))
  1955. (or (fpzerop *lzr*)
  1956. (setq *szr* (fpquotient (fpquotient c0 *lzr*) a0)))))))
  1957. nil))