home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18966 < prev    next >
Encoding:
Text File  |  1992-12-29  |  5.9 KB  |  147 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!spillman!tye
  3. From: tye@spillman.uucp (E. Tye McQueen)
  4. Subject: Re: 9th order polynomial
  5. Message-ID: <1992Dec29.103955.4864@spillman.uucp>
  6. Date: Tue, 29 Dec 1992 10:39:55 GMT
  7. References: <1992Dec10.154522.15893@lambda.msfc.nasa.gov> <1992Dec14.214016.149@ondec.lonestar.org>
  8. Organization: Spillman Data Systems
  9. Lines: 136
  10.  
  11. d_north@ondec.lonestar.org (David G. North, CCP) writes:
  12. )In article <1992Dec10.154522.15893@lambda.msfc.nasa.gov>,
  13. )  edmondso@lambda.msfc.nasa.gov (Rich Edmondson) writes:
  14. )> I need to use a 9th order polynomial to calibrate values on a system I
  15. )> am designing.  Does anyone know of an algorithm which is more
  16. )> efficient than recursive multiplication, given a list of polynomial
  17. )> coefficients?
  18. )Define recursive multiplication.
  19. )
  20. )How about an O*log2(n) algorithm--->
  21. )
  22. )double
  23. )di_exponentiate(d_src,i_pow)
  24. )  double d_src;
  25. )  int i_pow;
  26. ){
  27. )  double temp,accum;
  28. )  int i;
  29. )
  30. )  for (temp=d_src, accum=1.0, i=i_pow; i ; i>>=1, temp*=temp)
  31. )    if (i&1) accum *= temp;
  32. )  return(accum);
  33. )}
  34. [...]
  35.  
  36. Note that the order in which you perform the operations to
  37. compute a polynomial value can greatly affect the accuracy
  38. of your calculation (unless you are doing some non-floating-
  39. point arithmetic like integer arithmetic -- but then it can
  40. still affect whether or not you get overflow).
  41.  
  42. It's been too long so I can't recall whether computing it as
  43.  
  44.     ((((a*x+b)*x+c)*x+d)*x+e)*x+f
  45.  
  46. was a particularly good or particularly bad method.  It does
  47. have the advantage of reducing the number of multiplications
  48. required without requiring any extra space to hold intermediate
  49. values.
  50.  
  51. Note that if  a*x  is almost equal to  -b  then this method
  52. probably yields very poor results (subtracting nearly equal
  53. numbers is one of the best ways to *greatly* reduce the
  54. accuracy of a numeric computation and the rest of the above
  55. computation is likely to cause such inaccuracies to dominate
  56. the result -- gee, maybe it hasn't been as long as it
  57. intially felt).
  58.  
  59.     For example:
  60.         3.14159287 - 3.14159286 = 0.00000001
  61.     goes from about 9 (or 8) digits of accuracy right down
  62.     to only about one (or zero) digit of accuracy in one
  63.     simple step.  Unless this result gets added in to a
  64.     much larger result (adding it into a result even 10^5
  65.     times bigger than it only gets you back to around 4 or
  66.     5 digits of accuracy), your results may not even remotely
  67.     resemble the correct value.  So multiplying the result
  68.     by a possibly large x makes things worse.
  69.  
  70. Perhaps:
  71.  
  72.   int i;
  73.   double coef[]= { a, b, c, d, e, f };
  74.   double tot= 0, pow= 1;
  75.     for(  i= sizeof(coef)/sizeof(coef[0]) - 1;  0 <= i;  i--  ) {
  76.         tot += coef[i]*pow;
  77.         pow *= x;
  78.     }
  79.  
  80. (You can take ", f" out of the second line and change "0" to "f",
  81. and "1" to "x" in the third if you want to skip a step.)
  82.  
  83. Assuming x is not close to 1 and the coefficients are relatively
  84. close to 1, the result should be dominated by the high- or low-
  85. order terms, reducing the odds of nearly "cancelling" values
  86. during the addition, possibly increasing your accuracy.  Even
  87. if x *is* close to 1, you don't end up multiplying the (inaccurate)
  88. result of a "cancelling" addition so the inaccuracies are less
  89. likely to dominate the final result.
  90.  
  91. And, of course, results close to 0 are more likely to be inac-
  92. curate, but there is probably no way around that without avoid the
  93. computation itself.  So, if you are looking for roots, you may be
  94. surprised how big your tollerance has to be and how little accuracy
  95. you get.  But then you really should differentiate the polynomial
  96. and use a modified Newton's method in that case (ie. avoid the
  97. computation itself).
  98.  
  99. Gee, I think I understand this better now than I did when I took
  100. those danged courses.  Thinking something through for yourself
  101. can be so enlightening.
  102.  
  103. I'd suggest you:
  104.  
  105.     Find a good library routine that computes polynomials accurately
  106.     Find documentation for a good suite of numeric routines and read
  107.         how their polynomial computations are made
  108.     Find a good text on numeric(al) methods and read the chapter
  109.         on polynomials (sorry, I don't have one handy)
  110.     Ask your local college's numeric(al) methods expert (the Math
  111.         department should know where she is)
  112.     Take a nice course in numeric(al) methods (of course they'll
  113.         just laugh at polynomials and boldly assume that everything
  114.         is "almost" linear -- gawd I hate that)
  115.     Go check in news.answers for a FAQ that mentions polynomials
  116.         (it is fun and interesting reading anyway)
  117.     Go ask this question in sci.math.___ (sorry, I don't have a full
  118.         list of newsgroups handy and I don't recall what subgroups
  119.         lie under sci.math)
  120.     Relax and not worry about whether your results are even close
  121.         to the proper values (unfortunately, this is probably the
  122.         typical response)
  123.  
  124. In any case, I think I've given you two answers to your original
  125. question (on efficiency).  Sorry about giving you a harder question
  126. on accuracy.  But check into it, you'll become a rare asset (or
  127. at least rare).
  128.  
  129. If you have sparse coeffients and want to cut down on (floating-
  130. point) multiplies at all costs, you can use power-of-two tricks
  131. as the previous poster illustrated (though doing these tricks
  132. over for each power costs more than either of the above methods).
  133. But for order 9 that is probably not nearly worth it.
  134.  
  135. Sorry, I can't redirect follow-ups to a more appropriate group
  136. as our newsfeed is greatly restricted (still working on that
  137. dial-up PPP line to the local university to replace the long-
  138. distance call to uunet) and our newsreader won't let me follow-
  139. up to newgroups that we don't get.
  140.  
  141. Please direct your follow-ups to an appropriate group.
  142.  
  143.  tye@spillman.com                         Tye McQueen, E.
  144. ----------------------------------------------------------
  145.  Nothing is obvious unless you are overlooking something. 
  146. ----------------------------------------------------------
  147.