Polynomial.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../precompiled.h"
  21. #pragma hdrstop
  22. const float EPSILON = 1e-6f;
  23. /*
  24. =============
  25. idPolynomial::Laguer
  26. =============
  27. */
  28. int idPolynomial::Laguer( const idComplex *coef, const int degree, idComplex &x ) const {
  29. const int MT = 10, MAX_ITERATIONS = MT * 8;
  30. static const float frac[] = { 0.0f, 0.5f, 0.25f, 0.75f, 0.13f, 0.38f, 0.62f, 0.88f, 1.0f };
  31. int i, j;
  32. float abx, abp, abm, err;
  33. idComplex dx, cx, b, d, f, g, s, gps, gms, g2;
  34. for ( i = 1; i <= MAX_ITERATIONS; i++ ) {
  35. b = coef[degree];
  36. err = b.Abs();
  37. d.Zero();
  38. f.Zero();
  39. abx = x.Abs();
  40. for ( j = degree - 1; j >= 0; j-- ) {
  41. f = x * f + d;
  42. d = x * d + b;
  43. b = x * b + coef[j];
  44. err = b.Abs() + abx * err;
  45. }
  46. if ( b.Abs() < err * EPSILON ) {
  47. return i;
  48. }
  49. g = d / b;
  50. g2 = g * g;
  51. s = ( ( degree - 1 ) * ( degree * ( g2 - 2.0f * f / b ) - g2 ) ).Sqrt();
  52. gps = g + s;
  53. gms = g - s;
  54. abp = gps.Abs();
  55. abm = gms.Abs();
  56. if ( abp < abm ) {
  57. gps = gms;
  58. }
  59. if ( Max( abp, abm ) > 0.0f ) {
  60. dx = degree / gps;
  61. } else {
  62. dx = idMath::Exp( idMath::Log( 1.0f + abx ) ) * idComplex( idMath::Cos( i ), idMath::Sin( i ) );
  63. }
  64. cx = x - dx;
  65. if ( x == cx ) {
  66. return i;
  67. }
  68. if ( i % MT == 0 ) {
  69. x = cx;
  70. } else {
  71. x -= frac[i/MT] * dx;
  72. }
  73. }
  74. return i;
  75. }
  76. /*
  77. =============
  78. idPolynomial::GetRoots
  79. =============
  80. */
  81. int idPolynomial::GetRoots( idComplex *roots ) const {
  82. int i, j;
  83. idComplex x, b, c, *coef;
  84. coef = (idComplex *) _alloca16( ( degree + 1 ) * sizeof( idComplex ) );
  85. for ( i = 0; i <= degree; i++ ) {
  86. coef[i].Set( coefficient[i], 0.0f );
  87. }
  88. for ( i = degree - 1; i >= 0; i-- ) {
  89. x.Zero();
  90. Laguer( coef, i + 1, x );
  91. if ( idMath::Fabs( x.i ) < 2.0f * EPSILON * idMath::Fabs( x.r ) ) {
  92. x.i = 0.0f;
  93. }
  94. roots[i] = x;
  95. b = coef[i+1];
  96. for ( j = i; j >= 0; j-- ) {
  97. c = coef[j];
  98. coef[j] = b;
  99. b = x * b + c;
  100. }
  101. }
  102. for ( i = 0; i <= degree; i++ ) {
  103. coef[i].Set( coefficient[i], 0.0f );
  104. }
  105. for ( i = 0; i < degree; i++ ) {
  106. Laguer( coef, degree, roots[i] );
  107. }
  108. for ( i = 1; i < degree; i++ ) {
  109. x = roots[i];
  110. for ( j = i - 1; j >= 0; j-- ) {
  111. if ( roots[j].r <= x.r ) {
  112. break;
  113. }
  114. roots[j+1] = roots[j];
  115. }
  116. roots[j+1] = x;
  117. }
  118. return degree;
  119. }
  120. /*
  121. =============
  122. idPolynomial::GetRoots
  123. =============
  124. */
  125. int idPolynomial::GetRoots( float *roots ) const {
  126. int i, num;
  127. idComplex *complexRoots;
  128. switch( degree ) {
  129. case 0: return 0;
  130. case 1: return GetRoots1( coefficient[1], coefficient[0], roots );
  131. case 2: return GetRoots2( coefficient[2], coefficient[1], coefficient[0], roots );
  132. case 3: return GetRoots3( coefficient[3], coefficient[2], coefficient[1], coefficient[0], roots );
  133. case 4: return GetRoots4( coefficient[4], coefficient[3], coefficient[2], coefficient[1], coefficient[0], roots );
  134. }
  135. // The Abel-Ruffini theorem states that there is no general solution
  136. // in radicals to polynomial equations of degree five or higher.
  137. // A polynomial equation can be solved by radicals if and only if
  138. // its Galois group is a solvable group.
  139. complexRoots = (idComplex *) _alloca16( degree * sizeof( idComplex ) );
  140. GetRoots( complexRoots );
  141. for ( num = i = 0; i < degree; i++ ) {
  142. if ( complexRoots[i].i == 0.0f ) {
  143. roots[i] = complexRoots[i].r;
  144. num++;
  145. }
  146. }
  147. return num;
  148. }
  149. /*
  150. =============
  151. idPolynomial::ToString
  152. =============
  153. */
  154. const char *idPolynomial::ToString( int precision ) const {
  155. return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
  156. }
  157. /*
  158. =============
  159. idPolynomial::Test
  160. =============
  161. */
  162. void idPolynomial::Test( void ) {
  163. int i, num;
  164. float roots[4], value;
  165. idComplex complexRoots[4], complexValue;
  166. idPolynomial p;
  167. p = idPolynomial( -5.0f, 4.0f );
  168. num = p.GetRoots( roots );
  169. for ( i = 0; i < num; i++ ) {
  170. value = p.GetValue( roots[i] );
  171. assert( idMath::Fabs( value ) < 1e-4f );
  172. }
  173. p = idPolynomial( -5.0f, 4.0f, 3.0f );
  174. num = p.GetRoots( roots );
  175. for ( i = 0; i < num; i++ ) {
  176. value = p.GetValue( roots[i] );
  177. assert( idMath::Fabs( value ) < 1e-4f );
  178. }
  179. p = idPolynomial( 1.0f, 4.0f, 3.0f, -2.0f );
  180. num = p.GetRoots( roots );
  181. for ( i = 0; i < num; i++ ) {
  182. value = p.GetValue( roots[i] );
  183. assert( idMath::Fabs( value ) < 1e-4f );
  184. }
  185. p = idPolynomial( 5.0f, 4.0f, 3.0f, -2.0f );
  186. num = p.GetRoots( roots );
  187. for ( i = 0; i < num; i++ ) {
  188. value = p.GetValue( roots[i] );
  189. assert( idMath::Fabs( value ) < 1e-4f );
  190. }
  191. p = idPolynomial( -5.0f, 4.0f, 3.0f, 2.0f, 1.0f );
  192. num = p.GetRoots( roots );
  193. for ( i = 0; i < num; i++ ) {
  194. value = p.GetValue( roots[i] );
  195. assert( idMath::Fabs( value ) < 1e-4f );
  196. }
  197. p = idPolynomial( 1.0f, 4.0f, 3.0f, -2.0f );
  198. num = p.GetRoots( complexRoots );
  199. for ( i = 0; i < num; i++ ) {
  200. complexValue = p.GetValue( complexRoots[i] );
  201. assert( idMath::Fabs( complexValue.r ) < 1e-4f && idMath::Fabs( complexValue.i ) < 1e-4f );
  202. }
  203. p = idPolynomial( 5.0f, 4.0f, 3.0f, -2.0f );
  204. num = p.GetRoots( complexRoots );
  205. for ( i = 0; i < num; i++ ) {
  206. complexValue = p.GetValue( complexRoots[i] );
  207. assert( idMath::Fabs( complexValue.r ) < 1e-4f && idMath::Fabs( complexValue.i ) < 1e-4f );
  208. }
  209. }