roots.doc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. The REDUCE Root Finding Package
  2. Mod 1.91, 16 May 1990
  3. Stanley L. Kameny
  4. valley!stan@rand.org
  5. Introduction.
  6. The Root Finding package is designed so that it can be used as an
  7. independent package, or it can be integrated with and called by Solve.
  8. This document describes the package in its independent use. It can be
  9. used to find some or all of the roots of polynomials with real or
  10. complex coefficients, to the accuracy specified by the user.
  11. ------------------------------------------------------------------------
  12. Modules.
  13. The file roots.red. contains the following modules needed for use with
  14. REDUCE 3.4:
  15. ROOTS - header module
  16. BFDOER \
  17. BFDOER2 | - these modules can be loaded in any order, but
  18. COMPLXP | - after the math file.
  19. ALLROOT |
  20. REALROOT /
  21. The math module available with REDUCE 3.4, is also required, or else
  22. the roots file cannot be loaded or operated.
  23. ------------------------------------------------------------------------
  24. Top Level Functions
  25. The top level functions can be called either as symbolic operators from
  26. algebraic mode, or they can be called directly from symbolic mode with
  27. symbolic mode arguments. Outputs are expressed in forms that print out
  28. correctly in algebraic mode.
  29. Functions which refer to real roots only:
  30. Three top level functions refer only to real roots. Each of these
  31. functions can receive 1, 2 or 3 arguments.
  32. The first argument is the polynomial p, which can be complex and can
  33. have multiple or zero roots. If arg2 and arg3 are not present, all real
  34. roots are found. If the additional arguments are present, they restrict
  35. the region of consideration.
  36. If arguments are (p,arg2) then
  37. (Arg2 must be POSITIVE or NEGATIVE) If arg2=NEGATIVE then only
  38. negative roots of p are included; if arg2=POSITIVE then only positive
  39. roots of p are included. Zero roots are excluded.
  40. If arguments are (p,arg2,arg3)
  41. (Arg2 and Arg3 must be r (a real number) or EXCLUDE r or a member of
  42. the list {POSITIVE,NEGATIVE,INFINITY,-INFINITY}. EXCLUDE r causes the
  43. value r to be excluded from the region. The order of the sequence
  44. arg2, arg3 is unimportant. Assuming that arg2 <= arg3 if both are
  45. numeric, then
  46. {-INFINITY,INFINITY} is equivalent to {} and represents all roots;
  47. {arg2,NEGATIVE} represents -infinity < r < arg2;
  48. {arg2,POSITIVE) represents arg2 < r < infinity;
  49. In each of the following, replacing an arg with EXCLUDE arg converts
  50. the corresponding inclusive <= to the exclusive <
  51. {arg2,-INFINITY} represents -infinity < r <= arg2;
  52. {arg2,INFINITY} represents arg2 <= r < infinity;
  53. {arg2,arg3} represents arg2 <= r <= arg3;
  54. If zero is in the interval, zero root is included.
  55. REALROOTS --- This function finds the real roots of the polynomial p,
  56. using the REALROOT package to isolate real roots by the method of Sturm
  57. sequences, then polishing the root to the desired accuracy. Precision
  58. of computation is guaranteed to be sufficient to separate all real roots
  59. in the specified region. (cf. MULTIROOT for treatment of multiple
  60. roots.)
  61. ISOLATER --- This function produces a list of rational intervals, each
  62. containing a single real root of the polynomial p, within the specified
  63. region, but does not find the roots.
  64. RLROOTNO --- This function computes the number of real roots of p in
  65. the specified region, but does not find the roots.
  66. Functions which return both real and complex roots:
  67. ROOTS p; This is the main top level function of the roots package. It
  68. will find all roots, real and complex, of the the polynomial p to an
  69. accuracy sufficient to separate them. The value returned by ROOTS is a
  70. list of equations for all roots. In addition, ROOTS stores separate
  71. lists of real roots and complex roots in the global variables ROOTSREAL
  72. and ROOTSCOMPLEX.
  73. NEARESTROOT(p,s); This top level function uses an iterative method to
  74. find the root to which the method converges given the initial starting
  75. origin s, which can be complex. If there are several roots in the
  76. vicinity of s and s is not significantly closer to one root than it is
  77. to all others, the convergence could arrive at a root which is not truly
  78. the nearest root. This function should therefore be used only when the
  79. user is certain that there is only one root in the immediate vicinity of
  80. the starting point s.
  81. FIRSTROOT p; Equivalent to NEARESTROOT(p,0).
  82. Other top level function:
  83. CSIZE p; This function will determine the maximum coefficient size of
  84. the polynomial p. The initial precision used in root finding is at
  85. least 2+CSIZE p (in some cases significantly greater, as determined by
  86. the heuristic function CALCPREC.)
  87. GETROOT(n,rr); If rr has the form of the output of ROOTS, REALROOTS, or
  88. NEARESTROOTS; GETROOT returns the rational, real, or complex value of
  89. the root equation. Error occurs if n<1 or n>the number of roots in rr.
  90. MKPOLY rr; This function can be used to reconstruct a polynomial
  91. whose root equation list is rr and whose denominator is 1. Thus one can
  92. verify that
  93. if rr := ROOTS p; then rr1 := ROOTS MKPOLY rr; should result in
  94. rr1 = rr
  95. (This will be true if MULRITOOT and RATROOT are ON, and BIGFLOAT and
  96. FLOAT are off.)
  97. However, MKPOLY rr - NUM p = 0 will be true iff all roots of p
  98. have been computed exactly.
  99. Functions available for diagnostic or instructional use only:
  100. GFNEWT(p,r,cpx); This function will do a single pass through the
  101. function GFNEWTON for polynomial p and root r. If cpx=T, then any
  102. complex part of the root will be kept, no matter how small.
  103. GFROOT(p,r,cpx); This function will do a single pass through the
  104. function GFROOTFIND for polynomial p and root r. If cpx=T, then any
  105. complex part of the root will be kept, no matter how small.
  106. ROOTS2 p; The same as ROOTS p, except that if an abort occurs, the
  107. roots already found will be printed and then ROOTS2 will be applied to
  108. the deflated polynomial which exists at that point. (Note: there is no
  109. known polynomial on which ROOTS aborts.)
  110. ------------------------------------------------------------------------
  111. Switches Used in Input.
  112. The input of polynomials in algebraic mode is sensitive to the switches
  113. COMPLEX, FLOAT and BIGFLOAT. The correct choice of input method is
  114. important since incorrect choices will result in undesirable truncation
  115. or rounding of the input coefficients.
  116. Truncation or rounding will occur if FLOAT or BIGFLOAT is on and one of
  117. the following is true:
  118. a) a coefficient is entered in floating point form or rational form.
  119. b) COMPLEX is on and a coefficient is imaginary or complex.
  120. Therefore, to avoid undesirable truncation or rounding, then:
  121. c) both FLOAT and BIGFLOAT should be off and input should be in
  122. integer or rational form; or
  123. d) FLOAT can be on if it is acceptable to truncate or round input to
  124. the machine-dependent precision limit, which may be quite small; or
  125. e) BIGFLOAT can be on if PRECISION is set to a value large enough to
  126. prevent undesired rounding.
  127. integer and complex modes: (off float, bigfloat) any real polynomial
  128. can be input using integer coefficients of any size; integer or
  129. rational coefficients can be used to input any real or complex
  130. polynomial, independent of the setting of the switch COMPLEX. These are
  131. the most versatile input modes, since any real or complex polynomial can
  132. be input exactly.
  133. modes float and complex-float: (on float) polynomials can be input using
  134. integer coefficients of any size. Floating point coefficients will be
  135. truncated or rounded, to a size dependent upon the system. If complex
  136. is on, real coefficients can be input to any precision using integer
  137. form, but coefficients of imaginary parts of complex coefficients will
  138. be rounded or truncated.
  139. modes bigfloat and big-complex: (on bigfloat) the setting of precision
  140. determines the precision of all coefficients except for real
  141. coefficients input in integer form. Floating point coefficients will be
  142. truncated by the system to a size dependent upon the system, the same as
  143. floating point coefficients in float mode. If precision is set high
  144. enough, any real or complex polynomial can be input exactly provided
  145. that coefficients are input in integer or rational form.
  146. Internal and Output Use of Switches.
  147. REDUCE arithmetic mode switches BIGFLOAT, FLOAT, and COMPLEX. These
  148. switches are returned in the same state in which they were set
  149. initially, (barring catastrophic error.)
  150. COMPLEX --The Root Finding Package controls the switch COMPLEX
  151. internally, turning the switch on if it is processing a complex
  152. polynomial. (However, if COMPLEX is on, algebraic mode input may not
  153. work correctly in modes COMPLEX_FLOAT or BIG_COMPLEX, so it is best to
  154. use integer or rational input only. See example 62 of roots.tst for a
  155. way to get this to work.) For a polynomial with real coefficients, the
  156. starting point argument for NEARESTROOT can be given in algebraic mode
  157. in complex form as rl + im * I and will be handled correctly,
  158. independent of the setting of the switch COMPLEX. Complex roots will be
  159. computed and printed correctly regardless of the setting of the switch
  160. COMPLEX. However, if COMPLEX is off, the imaginary part will print out
  161. ahead of the real part, while the reverse order will be obtained if
  162. COMPLEX is on.
  163. FLOAT, BIGFLOAT --If the switch AUTOMODE (Default ON) is ON, the Root
  164. Finding package performs computations using the arithmetic mode that is
  165. required at the time, which may be integer, Gaussian integer, float,
  166. bigfloat, complex float or complex bigfloat. Switch BFTAG is used
  167. internally to govern the mode of computation and :prec: is adjusted
  168. whenever necessary. The initial position of switches FLOAT and BIGFLOAT
  169. are ignored. At output, these switches will emerge in their initial
  170. positions. Outputs will be printed out in float format only if the
  171. float format of the Lisp system will properly print out quantities of
  172. the required accuracy. Otherwise, the printout will be in bigfloat
  173. format. (See also the paragraph describing AUTOMODE.)
  174. Root Package Switches RATROOT, MULTIROOT, TRROOT, ROOTMSG,
  175. (plus diagnostic switch AUTOMODE.)
  176. (switches ISOROOT and ACCROOT, present in earlier versions, have been
  177. eliminated.)
  178. RATROOT --(Default OFF) If RATROOT is on all root equations are output
  179. in rational form. Assuming that the mode is COMPLEX (ie. FLOAT and
  180. BIGFLOAT are both off,) the root equations are guaranteed to be able to
  181. be input into REDUCE without truncation or rounding errors. (Cf. the
  182. function MKPOLY described above.)
  183. MULTIROOT --(Default ON) Whenever the polynomial has complex
  184. coefficients or has real coefficients and has multiple roots, as
  185. determined by the Sturm function, the function SQFRF is called
  186. automatically to factor the polynomial into square-free factors. If
  187. MULTIROOT is on, the multiplicity of the roots will be indicated in the
  188. output of ROOTS or REALROOTS by printing the root output repeatedly,
  189. according to its multiplicity. If MULTIROOT is off, each root will be
  190. printed once, and all roots should be normally be distinct. (Two
  191. identical roots should not appear. If the initial precision of the
  192. computation or the accuracy of the output was insufficient to separate
  193. two closely-spaced roots, the program attempts to increase accuracy
  194. and/or precision if it detects equal roots. If however, if the initial
  195. accuracy specified was too low, and it was possible to separate the
  196. roots, the program will abort.)
  197. TRROOT --(Default OFF) If switch TRROOT is on, trace messages are
  198. printed out during the course of root determination, to show the
  199. progress of solution.
  200. ROOTMSG --(Default OFF) If switch ROOTMSG is on in addition to switch
  201. TRROOT, additional messages are printed out to aid in following the
  202. progress of Laguerre and Newton complex iteration. These messages are
  203. intended for debugging use primarily.
  204. NOTE: the switch AUTOMODE is included mainly for diagnostic purposes.
  205. If it is changed from its default setting, the automatic determination
  206. of computation modes is bypassed, and correct root determination may not
  207. be achieved!
  208. AUTOMODE --(Default ON) If switch AUTOMODE is on, then, independent of
  209. the user setting of the switch BIGFLOAT, all floating point computations
  210. are carried out in floating point mode (rather than bigfloat) if the
  211. system floating point mode has sufficient precision at that point in the
  212. computation. If AUTOMODE is off and the user setting of BIGFLOAT is on,
  213. bigfloat computations are used for all floating point computations. The
  214. default setting of AUTOMODE is ON, in order to speed up computations and
  215. guarantee that the exact input polynomial is evaluated.
  216. ------------------------------------------------------------------------
  217. Operational Parameters and Parameter Setting.
  218. ROOTACC# --(Default 6) This parameter can be set using the function
  219. ROOTACC n; which causes ROOTACC# to be set to MAX(n,6). If ACCROOT is
  220. on, roots will be determined to a minimum of ROOTACC# significant
  221. places. (If roots are closely spaced, a higher number of significant
  222. places is computed where needed.)
  223. :PREC: --(Default 8) This REDUCE parameter is used to determine the
  224. precision of bigfloat computations. The function PRECISION n; causes
  225. :PREC: to be set to the value n+2 but returns the value n. The roots
  226. package, during its operation, will change the value of :PREC: but will
  227. restore the original value of :PREC: at termination except that the
  228. value of :PREC: is increased if necessary to allow the full output to be
  229. printed.
  230. ROOTPREC n; --The roots package normally sets the computation mode and
  231. precision automatically if AUTOMODE is on. However, if ROOTPREC n; is
  232. called and n>!!NFPD (where !!NFPD is the number of floating point
  233. digits in the Lisp system,) then all root computation will be done
  234. initially in bigfloat mode of minimum precision n. Automatic operation
  235. can be restored by input of ROOTPREC 0;.
  236. -----------------------------------------------------------------------
  237. Avoiding truncation of polynomials on input;
  238. The roots package will not internally truncate polynomials provided that
  239. the switch automode is on (or, if automode is off, provided that
  240. rootprec is not set to some value smaller than the number of significant
  241. figures needed to represent the polynomial precisely.) However, it is
  242. possible that a polynomial can be truncated by input reading functions
  243. of the embedding lisp system, particularly when input is given in
  244. floating point or bigfloat formats. (some lisp systems use the floating
  245. point input routines to input bigfloats.)
  246. To avoid any difficulties, input can be done in integer or Gaussian
  247. integer format, or mixed, with integers or rationals used to represent
  248. quantities of high precision. There are many examples of this in the
  249. test package. Note that use of bigfloat of high precision will not
  250. necessarily avoid truncation of coefficients if floating point input
  251. format is used. It is usually sufficient to let the roots package
  252. determine the precision needed to compute roots.
  253. The number of digits that can be safely represented in floating point in
  254. the lisp system are contained in the global variable !nfpd. Similarly,
  255. the maximum number of significant figures in floating point output are
  256. contained in the global variable !flim. The roots package computes
  257. these values, which are needed to control the logic of the program.
  258. The values of intermediate root iterations (that are printed when trroot
  259. is on) are given in bigfloat format even when the actual values are
  260. computed in floating point. This avoids intrusive rounding of root
  261. printout.