ellikf.c 2.0 KB

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