ellik.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* ellik.c
  2. *
  3. * Incomplete elliptic integral of the first kind
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double phi, m, y, ellik();
  10. *
  11. * y = ellik( 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 m in [0, 1] and phi as indicated.
  40. *
  41. * Relative error:
  42. * arithmetic domain # trials peak rms
  43. * IEEE -10,10 200000 7.4e-16 1.0e-16
  44. *
  45. *
  46. */
  47. /*
  48. Cephes Math Library Release 2.8: June, 2000
  49. Copyright 1984, 1987, 2000 by Stephen L. Moshier
  50. */
  51. /* Incomplete elliptic integral of first kind */
  52. #include <math.h>
  53. #ifdef ANSIPROT
  54. extern double sqrt ( double );
  55. extern double fabs ( double );
  56. extern double log ( double );
  57. extern double tan ( double );
  58. extern double atan ( double );
  59. extern double floor ( double );
  60. extern double ellpk ( double );
  61. double ellik ( double, double );
  62. #else
  63. double sqrt(), fabs(), log(), tan(), atan(), floor(), ellpk();
  64. double ellik();
  65. #endif
  66. extern double PI, PIO2, MACHEP, MAXNUM;
  67. double ellik( phi, m )
  68. double phi, m;
  69. {
  70. double a, b, c, e, temp, t, K;
  71. int d, mod, sign, npio2;
  72. if( m == 0.0 )
  73. return( phi );
  74. a = 1.0 - m;
  75. if( a == 0.0 )
  76. {
  77. if( fabs(phi) >= PIO2 )
  78. {
  79. mtherr( "ellik", SING );
  80. return( MAXNUM );
  81. }
  82. return( log( tan( (PIO2 + phi)/2.0 ) ) );
  83. }
  84. npio2 = floor( phi/PIO2 );
  85. if( npio2 & 1 )
  86. npio2 += 1;
  87. if( npio2 )
  88. {
  89. K = ellpk( a );
  90. phi = phi - npio2 * PIO2;
  91. }
  92. else
  93. K = 0.0;
  94. if( phi < 0.0 )
  95. {
  96. phi = -phi;
  97. sign = -1;
  98. }
  99. else
  100. sign = 0;
  101. b = sqrt(a);
  102. t = tan( phi );
  103. if( fabs(t) > 10.0 )
  104. {
  105. /* Transform the amplitude */
  106. e = 1.0/(b*t);
  107. /* ... but avoid multiple recursions. */
  108. if( fabs(e) < 10.0 )
  109. {
  110. e = atan(e);
  111. if( npio2 == 0 )
  112. K = ellpk( a );
  113. temp = K - ellik( e, m );
  114. goto done;
  115. }
  116. }
  117. a = 1.0;
  118. c = sqrt(m);
  119. d = 1;
  120. mod = 0;
  121. while( fabs(c/a) > MACHEP )
  122. {
  123. temp = b/a;
  124. phi = phi + atan(t*temp) + mod * PI;
  125. mod = (phi + PIO2)/PI;
  126. t = t * ( 1.0 + temp )/( 1.0 - temp * t * t );
  127. c = ( a - b )/2.0;
  128. temp = sqrt( a * b );
  129. a = ( a + b )/2.0;
  130. b = temp;
  131. d += d;
  132. }
  133. temp = (atan(t) + mod * PI)/(d * a);
  134. done:
  135. if( sign < 0 )
  136. temp = -temp;
  137. temp += npio2 * K;
  138. return( temp );
  139. }