ellpkl.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* ellpkl.c
  2. *
  3. * Complete elliptic integral of the first kind
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * long double m1, y, ellpkl();
  10. *
  11. * y = ellpkl( m1 );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Approximates the integral
  18. *
  19. *
  20. *
  21. * pi/2
  22. * -
  23. * | |
  24. * | dt
  25. * K(m) = | ------------------
  26. * | 2
  27. * | | sqrt( 1 - m sin t )
  28. * -
  29. * 0
  30. *
  31. * where m = 1 - m1, using the approximation
  32. *
  33. * P(x) - log x Q(x).
  34. *
  35. * The argument m1 is used rather than m so that the logarithmic
  36. * singularity at m = 1 will be shifted to the origin; this
  37. * preserves maximum accuracy.
  38. *
  39. * K(0) = pi/2.
  40. *
  41. * ACCURACY:
  42. *
  43. * Relative error:
  44. * arithmetic domain # trials peak rms
  45. * IEEE 0,1 10000 1.1e-19 3.3e-20
  46. *
  47. * ERROR MESSAGES:
  48. *
  49. * message condition value returned
  50. * ellpkl domain x<0, x>1 0.0
  51. *
  52. */
  53. /* ellpkl.c */
  54. /*
  55. Cephes Math Library, Release 2.3: October, 1995
  56. Copyright 1984, 1987, 1995 by Stephen L. Moshier
  57. */
  58. #include <math.h>
  59. #if UNK
  60. static long double P[13] = {
  61. 1.247539729154838838628E-6L,
  62. 2.149421654232011240659E-4L,
  63. 2.265267575136470585139E-3L,
  64. 6.723088676584254248821E-3L,
  65. 8.092066790639263075808E-3L,
  66. 5.664069509748147028621E-3L,
  67. 4.579865994050801042865E-3L,
  68. 5.797368411662027645234E-3L,
  69. 8.767698209432225911803E-3L,
  70. 1.493761594388688915057E-2L,
  71. 3.088514457872042326871E-2L,
  72. 9.657359027999314232753E-2L,
  73. 1.386294361119890618992E0L,
  74. };
  75. static long double Q[12] = {
  76. 5.568631677757315398993E-5L,
  77. 1.036110372590318802997E-3L,
  78. 5.500459122138244213579E-3L,
  79. 1.337330436245904844528E-2L,
  80. 2.033103735656990487115E-2L,
  81. 2.522868345512332304268E-2L,
  82. 3.026786461242788135379E-2L,
  83. 3.738370118296930305919E-2L,
  84. 4.882812208418620146046E-2L,
  85. 7.031249999330222751046E-2L,
  86. 1.249999999999978263154E-1L,
  87. 4.999999999999999999924E-1L,
  88. };
  89. static long double C1 = 1.386294361119890618834L; /* log(4) */
  90. #endif
  91. #if IBMPC
  92. static short P[] = {
  93. 0xf098,0xad01,0x2381,0xa771,0x3feb, XPD
  94. 0xd6ed,0xea22,0x1922,0xe162,0x3ff2, XPD
  95. 0x3733,0xe2f1,0xe226,0x9474,0x3ff6, XPD
  96. 0x3031,0x3c9d,0x5aff,0xdc4d,0x3ff7, XPD
  97. 0x9a46,0x4310,0x968e,0x8494,0x3ff8, XPD
  98. 0xbe4c,0x3ff2,0xa8a7,0xb999,0x3ff7, XPD
  99. 0xf35c,0x0eaf,0xb355,0x9612,0x3ff7, XPD
  100. 0xbc56,0x8fd4,0xd9dd,0xbdf7,0x3ff7, XPD
  101. 0xc01e,0x867f,0x6444,0x8fa6,0x3ff8, XPD
  102. 0x4ba3,0x6392,0xe6fd,0xf4bc,0x3ff8, XPD
  103. 0x62c3,0xbb12,0xd7bc,0xfd02,0x3ff9, XPD
  104. 0x08fe,0x476c,0x5fdf,0xc5c8,0x3ffb, XPD
  105. 0x79ad,0xd1cf,0x17f7,0xb172,0x3fff, XPD
  106. };
  107. static short Q[] = {
  108. 0x96a4,0x8474,0xba33,0xe990,0x3ff0, XPD
  109. 0xe5a7,0xa50e,0x1854,0x87ce,0x3ff5, XPD
  110. 0x8999,0x72e3,0x3205,0xb43d,0x3ff7, XPD
  111. 0x3255,0x13eb,0xb438,0xdb1b,0x3ff8, XPD
  112. 0xb717,0x497f,0x4691,0xa68d,0x3ff9, XPD
  113. 0x30be,0x8c6b,0x624b,0xceac,0x3ff9, XPD
  114. 0xa858,0x2a0d,0x5014,0xf7f4,0x3ff9, XPD
  115. 0x8615,0xbfa6,0xa6df,0x991f,0x3ffa, XPD
  116. 0x103c,0xa076,0xff37,0xc7ff,0x3ffa, XPD
  117. 0xf508,0xc515,0xffff,0x8fff,0x3ffb, XPD
  118. 0x1af5,0xfffb,0xffff,0xffff,0x3ffb, XPD
  119. 0x0000,0x0000,0x0000,0x8000,0x3ffe, XPD
  120. };
  121. static unsigned short ac1[] = {
  122. 0x79ac,0xd1cf,0x17f7,0xb172,0x3fff, XPD
  123. };
  124. #define C1 (*(long double *)ac1)
  125. #endif
  126. #ifdef MIEEE
  127. static long P[39] = {
  128. 0x3feb0000,0xa7712381,0xad01f098,
  129. 0x3ff20000,0xe1621922,0xea22d6ed,
  130. 0x3ff60000,0x9474e226,0xe2f13733,
  131. 0x3ff70000,0xdc4d5aff,0x3c9d3031,
  132. 0x3ff80000,0x8494968e,0x43109a46,
  133. 0x3ff70000,0xb999a8a7,0x3ff2be4c,
  134. 0x3ff70000,0x9612b355,0x0eaff35c,
  135. 0x3ff70000,0xbdf7d9dd,0x8fd4bc56,
  136. 0x3ff80000,0x8fa66444,0x867fc01e,
  137. 0x3ff80000,0xf4bce6fd,0x63924ba3,
  138. 0x3ff90000,0xfd02d7bc,0xbb1262c3,
  139. 0x3ffb0000,0xc5c85fdf,0x476c08fe,
  140. 0x3fff0000,0xb17217f7,0xd1cf79ad,
  141. };
  142. static long Q[36] = {
  143. 0x3ff00000,0xe990ba33,0x847496a4,
  144. 0x3ff50000,0x87ce1854,0xa50ee5a7,
  145. 0x3ff70000,0xb43d3205,0x72e38999,
  146. 0x3ff80000,0xdb1bb438,0x13eb3255,
  147. 0x3ff90000,0xa68d4691,0x497fb717,
  148. 0x3ff90000,0xceac624b,0x8c6b30be,
  149. 0x3ff90000,0xf7f45014,0x2a0da858,
  150. 0x3ffa0000,0x991fa6df,0xbfa68615,
  151. 0x3ffa0000,0xc7ffff37,0xa076103c,
  152. 0x3ffb0000,0x8fffffff,0xc515f508,
  153. 0x3ffb0000,0xffffffff,0xfffb1af5,
  154. 0x3ffe0000,0x80000000,0x00000000,
  155. };
  156. static unsigned long ac1[] = {
  157. 0x3fff0000,0xb17217f7,0xd1cf79ac
  158. };
  159. #define C1 (*(long double *)ac1)
  160. #endif
  161. #ifdef ANSIPROT
  162. extern long double polevll ( long double, void *, int );
  163. extern long double logl ( long double );
  164. #else
  165. long double polevll(), logl();
  166. #endif
  167. extern long double MACHEPL, MAXNUML;
  168. long double ellpkl(x)
  169. long double x;
  170. {
  171. if( (x < 0.0L) || (x > 1.0L) )
  172. {
  173. mtherr( "ellpkl", DOMAIN );
  174. return( 0.0L );
  175. }
  176. if( x > MACHEPL )
  177. {
  178. return( polevll(x,P,12) - logl(x) * polevll(x,Q,11) );
  179. }
  180. else
  181. {
  182. if( x == 0.0L )
  183. {
  184. mtherr( "ellpkl", SING );
  185. return( MAXNUML );
  186. }
  187. else
  188. {
  189. return( C1 - 0.5L * logl(x) );
  190. }
  191. }
  192. }