shichif.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* shichif.c
  2. *
  3. * Hyperbolic sine and cosine integrals
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float x, Chi, Shi;
  10. *
  11. * shichi( x, &Chi, &Shi );
  12. *
  13. *
  14. * DESCRIPTION:
  15. *
  16. * Approximates the integrals
  17. *
  18. * x
  19. * -
  20. * | | cosh t - 1
  21. * Chi(x) = eul + ln x + | ----------- dt,
  22. * | | t
  23. * -
  24. * 0
  25. *
  26. * x
  27. * -
  28. * | | sinh t
  29. * Shi(x) = | ------ dt
  30. * | | t
  31. * -
  32. * 0
  33. *
  34. * where eul = 0.57721566490153286061 is Euler's constant.
  35. * The integrals are evaluated by power series for x < 8
  36. * and by Chebyshev expansions for x between 8 and 88.
  37. * For large x, both functions approach exp(x)/2x.
  38. * Arguments greater than 88 in magnitude return MAXNUM.
  39. *
  40. *
  41. * ACCURACY:
  42. *
  43. * Test interval 0 to 88.
  44. * Relative error:
  45. * arithmetic function # trials peak rms
  46. * IEEE Shi 20000 3.5e-7 7.0e-8
  47. * Absolute error, except relative when |Chi| > 1:
  48. * IEEE Chi 20000 3.8e-7 7.6e-8
  49. */
  50. /*
  51. Cephes Math Library Release 2.2: July, 1992
  52. Copyright 1984, 1987, 1992 by Stephen L. Moshier
  53. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  54. */
  55. #include <math.h>
  56. /* x exp(-x) shi(x), inverted interval 8 to 18 */
  57. static float S1[] = {
  58. -3.56699611114982536845E-8,
  59. 1.44818877384267342057E-7,
  60. 7.82018215184051295296E-7,
  61. -5.39919118403805073710E-6,
  62. -3.12458202168959833422E-5,
  63. 8.90136741950727517826E-5,
  64. 2.02558474743846862168E-3,
  65. 2.96064440855633256972E-2,
  66. 1.11847751047257036625E0
  67. };
  68. /* x exp(-x) shi(x), inverted interval 18 to 88 */
  69. static float S2[] = {
  70. 1.69050228879421288846E-8,
  71. 1.25391771228487041649E-7,
  72. 1.16229947068677338732E-6,
  73. 1.61038260117376323993E-5,
  74. 3.49810375601053973070E-4,
  75. 1.28478065259647610779E-2,
  76. 1.03665722588798326712E0
  77. };
  78. /* x exp(-x) chin(x), inverted interval 8 to 18 */
  79. static float C1[] = {
  80. 1.31458150989474594064E-8,
  81. -4.75513930924765465590E-8,
  82. -2.21775018801848880741E-7,
  83. 1.94635531373272490962E-6,
  84. 4.33505889257316408893E-6,
  85. -6.13387001076494349496E-5,
  86. -3.13085477492997465138E-4,
  87. 4.97164789823116062801E-4,
  88. 2.64347496031374526641E-2,
  89. 1.11446150876699213025E0
  90. };
  91. /* x exp(-x) chin(x), inverted interval 18 to 88 */
  92. static float C2[] = {
  93. -3.00095178028681682282E-9,
  94. 7.79387474390914922337E-8,
  95. 1.06942765566401507066E-6,
  96. 1.59503164802313196374E-5,
  97. 3.49592575153777996871E-4,
  98. 1.28475387530065247392E-2,
  99. 1.03665693917934275131E0
  100. };
  101. /* Sine and cosine integrals */
  102. #define EUL 0.57721566490153286061
  103. extern float MACHEPF, MAXNUMF;
  104. #define fabsf(x) ( (x) < 0 ? -(x) : (x) )
  105. #ifdef ANSIC
  106. float logf(float ), expf(float), chbevlf(float, float *, int);
  107. #else
  108. float logf(), expf(), chbevlf();
  109. #endif
  110. int shichif( float xx, float *si, float *ci )
  111. {
  112. float x, k, z, c, s, a;
  113. short sign;
  114. x = xx;
  115. if( x < 0.0 )
  116. {
  117. sign = -1;
  118. x = -x;
  119. }
  120. else
  121. sign = 0;
  122. if( x == 0.0 )
  123. {
  124. *si = 0.0;
  125. *ci = -MAXNUMF;
  126. return( 0 );
  127. }
  128. if( x >= 8.0 )
  129. goto chb;
  130. z = x * x;
  131. /* Direct power series expansion */
  132. a = 1.0;
  133. s = 1.0;
  134. c = 0.0;
  135. k = 2.0;
  136. do
  137. {
  138. a *= z/k;
  139. c += a/k;
  140. k += 1.0;
  141. a /= k;
  142. s += a/k;
  143. k += 1.0;
  144. }
  145. while( fabsf(a/s) > MACHEPF );
  146. s *= x;
  147. goto done;
  148. chb:
  149. if( x < 18.0 )
  150. {
  151. a = (576.0/x - 52.0)/10.0;
  152. k = expf(x) / x;
  153. s = k * chbevlf( a, S1, 9 );
  154. c = k * chbevlf( a, C1, 10 );
  155. goto done;
  156. }
  157. if( x <= 88.0 )
  158. {
  159. a = (6336.0/x - 212.0)/70.0;
  160. k = expf(x) / x;
  161. s = k * chbevlf( a, S2, 7 );
  162. c = k * chbevlf( a, C2, 7 );
  163. goto done;
  164. }
  165. else
  166. {
  167. if( sign )
  168. *si = -MAXNUMF;
  169. else
  170. *si = MAXNUMF;
  171. *ci = MAXNUMF;
  172. return(0);
  173. }
  174. done:
  175. if( sign )
  176. s = -s;
  177. *si = s;
  178. *ci = EUL + logf(x) + c;
  179. return(0);
  180. }