ellief.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* ellief.c
  2. *
  3. * Incomplete elliptic integral of the second kind
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float phi, m, y, ellief();
  10. *
  11. * y = ellief( phi, m );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Approximates the integral
  18. *
  19. *
  20. * phi
  21. * -
  22. * | |
  23. * | 2
  24. * E(phi\m) = | sqrt( 1 - m sin t ) dt
  25. * |
  26. * | |
  27. * -
  28. * 0
  29. *
  30. * of amplitude phi and modulus m, using the arithmetic -
  31. * geometric mean algorithm.
  32. *
  33. *
  34. *
  35. * ACCURACY:
  36. *
  37. * Tested at random arguments with phi in [0, 2] and m in
  38. * [0, 1].
  39. * Relative error:
  40. * arithmetic domain # trials peak rms
  41. * IEEE 0,2 10000 4.5e-7 7.4e-8
  42. *
  43. *
  44. */
  45. /*
  46. Cephes Math Library Release 2.2: July, 1992
  47. Copyright 1984, 1987, 1992 by Stephen L. Moshier
  48. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  49. */
  50. /* Incomplete elliptic integral of second kind */
  51. #include <math.h>
  52. extern float PIF, PIO2F, MACHEPF;
  53. #define fabsf(x) ( (x) < 0 ? -(x) : (x) )
  54. #ifdef ANSIC
  55. float sqrtf(float), logf(float), sinf(float), tanf(float), atanf(float);
  56. float ellpef(float), ellpkf(float);
  57. #else
  58. float sqrtf(), logf(), sinf(), tanf(), atanf();
  59. float ellpef(), ellpkf();
  60. #endif
  61. float ellief( float phia, float ma )
  62. {
  63. float phi, m, a, b, c, e, temp;
  64. float lphi, t;
  65. int d, mod;
  66. phi = phia;
  67. m = ma;
  68. if( m == 0.0 )
  69. return( phi );
  70. if( m == 1.0 )
  71. return( sinf(phi) );
  72. lphi = phi;
  73. if( lphi < 0.0 )
  74. lphi = -lphi;
  75. a = 1.0;
  76. b = 1.0 - m;
  77. b = sqrtf(b);
  78. c = sqrtf(m);
  79. d = 1;
  80. e = 0.0;
  81. t = tanf( lphi );
  82. mod = (lphi + PIO2F)/PIF;
  83. while( fabsf(c/a) > MACHEPF )
  84. {
  85. temp = b/a;
  86. lphi = lphi + atanf(t*temp) + mod * PIF;
  87. mod = (lphi + PIO2F)/PIF;
  88. t = t * ( 1.0 + temp )/( 1.0 - temp * t * t );
  89. c = 0.5 * ( a - b );
  90. temp = sqrtf( a * b );
  91. a = 0.5 * ( a + b );
  92. b = temp;
  93. d += d;
  94. e += c * sinf(lphi);
  95. }
  96. b = 1.0 - m;
  97. temp = ellpef(b)/ellpkf(b);
  98. temp *= (atanf(t) + mod * PIF)/(d * a);
  99. temp += e;
  100. if( phi < 0.0 )
  101. temp = -temp;
  102. return( temp );
  103. }