ellikl.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* ellikl.c
  2. *
  3. * Incomplete elliptic integral of the first kind
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * long double phi, m, y, ellikl();
  10. *
  11. * y = ellikl( 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 30000 3.6e-18 4.1e-19
  44. *
  45. *
  46. */
  47. /*
  48. Cephes Math Library Release 2.3: November, 1995
  49. Copyright 1984, 1987, 1995 by Stephen L. Moshier
  50. */
  51. /* Incomplete elliptic integral of first kind */
  52. #include <math.h>
  53. #ifdef ANSIPROT
  54. extern long double sqrtl ( long double );
  55. extern long double fabsl ( long double );
  56. extern long double logl ( long double );
  57. extern long double tanl ( long double );
  58. extern long double atanl ( long double );
  59. extern long double floorl ( long double );
  60. extern long double ellpkl ( long double );
  61. long double ellikl ( long double, long double );
  62. #else
  63. long double sqrtl(), fabsl(), logl(), tanl(), atanl(), floorl(), ellpkl();
  64. long double ellikl();
  65. #endif
  66. extern long double PIL, PIO2L, MACHEPL, MAXNUML;
  67. long double ellikl( phi, m )
  68. long double phi, m;
  69. {
  70. long double a, b, c, e, temp, t, K;
  71. int d, mod, sign, npio2;
  72. if( m == 0.0L )
  73. return( phi );
  74. a = 1.0L - m;
  75. if( a == 0.0L )
  76. {
  77. if( fabsl(phi) >= PIO2L )
  78. {
  79. mtherr( "ellikl", SING );
  80. return( MAXNUML );
  81. }
  82. return( logl( tanl( 0.5L*(PIO2L + phi) ) ) );
  83. }
  84. npio2 = floorl( phi/PIO2L );
  85. if( npio2 & 1 )
  86. npio2 += 1;
  87. if( npio2 )
  88. {
  89. K = ellpkl( a );
  90. phi = phi - npio2 * PIO2L;
  91. }
  92. else
  93. K = 0.0L;
  94. if( phi < 0.0L )
  95. {
  96. phi = -phi;
  97. sign = -1;
  98. }
  99. else
  100. sign = 0;
  101. b = sqrtl(a);
  102. t = tanl( phi );
  103. if( fabsl(t) > 10.0L )
  104. {
  105. /* Transform the amplitude */
  106. e = 1.0L/(b*t);
  107. /* ... but avoid multiple recursions. */
  108. if( fabsl(e) < 10.0L )
  109. {
  110. e = atanl(e);
  111. if( npio2 == 0 )
  112. K = ellpkl( a );
  113. temp = K - ellikl( e, m );
  114. goto done;
  115. }
  116. }
  117. a = 1.0L;
  118. c = sqrtl(m);
  119. d = 1;
  120. mod = 0;
  121. while( fabsl(c/a) > MACHEPL )
  122. {
  123. temp = b/a;
  124. phi = phi + atanl(t*temp) + mod * PIL;
  125. mod = (phi + PIO2L)/PIL;
  126. t = t * ( 1.0L + temp )/( 1.0L - temp * t * t );
  127. c = 0.5L * ( a - b );
  128. temp = sqrtl( a * b );
  129. a = 0.5L * ( a + b );
  130. b = temp;
  131. d += d;
  132. }
  133. temp = (atanl(t) + mod * PIL)/(d * a);
  134. done:
  135. if( sign < 0 )
  136. temp = -temp;
  137. temp += npio2 * K;
  138. return( temp );
  139. }