struve.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* struve.c
  2. *
  3. * Struve function
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double v, x, y, struve();
  10. *
  11. * y = struve( v, x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Computes the Struve function Hv(x) of order v, argument x.
  18. * Negative x is rejected unless v is an integer.
  19. *
  20. * This module also contains the hypergeometric functions 1F2
  21. * and 3F0 and a routine for the Bessel function Yv(x) with
  22. * noninteger v.
  23. *
  24. *
  25. *
  26. * ACCURACY:
  27. *
  28. * Not accurately characterized, but spot checked against tables.
  29. *
  30. */
  31. /*
  32. Cephes Math Library Release 2.81: June, 2000
  33. Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
  34. */
  35. #include <math.h>
  36. #define DEBUG 0
  37. #ifdef ANSIPROT
  38. extern double gamma ( double );
  39. extern double pow ( double, double );
  40. extern double sqrt ( double );
  41. extern double yn ( int, double );
  42. extern double jv ( double, double );
  43. extern double fabs ( double );
  44. extern double floor ( double );
  45. extern double sin ( double );
  46. extern double cos ( double );
  47. double yv ( double, double );
  48. double onef2 (double, double, double, double, double * );
  49. double threef0 (double, double, double, double, double * );
  50. #else
  51. double gamma(), pow(), sqrt(), yn(), yv(), jv(), fabs(), floor();
  52. double sin(), cos();
  53. double onef2(), threef0();
  54. #endif
  55. static double stop = 1.37e-17;
  56. extern double MACHEP;
  57. double onef2( a, b, c, x, err )
  58. double a, b, c, x;
  59. double *err;
  60. {
  61. double n, a0, sum, t;
  62. double an, bn, cn, max, z;
  63. an = a;
  64. bn = b;
  65. cn = c;
  66. a0 = 1.0;
  67. sum = 1.0;
  68. n = 1.0;
  69. t = 1.0;
  70. max = 0.0;
  71. do
  72. {
  73. if( an == 0 )
  74. goto done;
  75. if( bn == 0 )
  76. goto error;
  77. if( cn == 0 )
  78. goto error;
  79. if( (a0 > 1.0e34) || (n > 200) )
  80. goto error;
  81. a0 *= (an * x) / (bn * cn * n);
  82. sum += a0;
  83. an += 1.0;
  84. bn += 1.0;
  85. cn += 1.0;
  86. n += 1.0;
  87. z = fabs( a0 );
  88. if( z > max )
  89. max = z;
  90. if( sum != 0 )
  91. t = fabs( a0 / sum );
  92. else
  93. t = z;
  94. }
  95. while( t > stop );
  96. done:
  97. *err = fabs( MACHEP*max /sum );
  98. #if DEBUG
  99. printf(" onef2 cancellation error %.5E\n", *err );
  100. #endif
  101. goto xit;
  102. error:
  103. #if DEBUG
  104. printf("onef2 does not converge\n");
  105. #endif
  106. *err = 1.0e38;
  107. xit:
  108. #if DEBUG
  109. printf("onef2( %.2E %.2E %.2E %.5E ) = %.3E %.6E\n", a, b, c, x, n, sum);
  110. #endif
  111. return(sum);
  112. }
  113. double threef0( a, b, c, x, err )
  114. double a, b, c, x;
  115. double *err;
  116. {
  117. double n, a0, sum, t, conv, conv1;
  118. double an, bn, cn, max, z;
  119. an = a;
  120. bn = b;
  121. cn = c;
  122. a0 = 1.0;
  123. sum = 1.0;
  124. n = 1.0;
  125. t = 1.0;
  126. max = 0.0;
  127. conv = 1.0e38;
  128. conv1 = conv;
  129. do
  130. {
  131. if( an == 0.0 )
  132. goto done;
  133. if( bn == 0.0 )
  134. goto done;
  135. if( cn == 0.0 )
  136. goto done;
  137. if( (a0 > 1.0e34) || (n > 200) )
  138. goto error;
  139. a0 *= (an * bn * cn * x) / n;
  140. an += 1.0;
  141. bn += 1.0;
  142. cn += 1.0;
  143. n += 1.0;
  144. z = fabs( a0 );
  145. if( z > max )
  146. max = z;
  147. if( z >= conv )
  148. {
  149. if( (z < max) && (z > conv1) )
  150. goto done;
  151. }
  152. conv1 = conv;
  153. conv = z;
  154. sum += a0;
  155. if( sum != 0 )
  156. t = fabs( a0 / sum );
  157. else
  158. t = z;
  159. }
  160. while( t > stop );
  161. done:
  162. t = fabs( MACHEP*max/sum );
  163. #if DEBUG
  164. printf(" threef0 cancellation error %.5E\n", t );
  165. #endif
  166. max = fabs( conv/sum );
  167. if( max > t )
  168. t = max;
  169. #if DEBUG
  170. printf(" threef0 convergence %.5E\n", max );
  171. #endif
  172. goto xit;
  173. error:
  174. #if DEBUG
  175. printf("threef0 does not converge\n");
  176. #endif
  177. t = 1.0e38;
  178. xit:
  179. #if DEBUG
  180. printf("threef0( %.2E %.2E %.2E %.5E ) = %.3E %.6E\n", a, b, c, x, n, sum);
  181. #endif
  182. *err = t;
  183. return(sum);
  184. }
  185. extern double PI;
  186. double struve( v, x )
  187. double v, x;
  188. {
  189. double y, ya, f, g, h, t;
  190. double onef2err, threef0err;
  191. f = floor(v);
  192. if( (v < 0) && ( v-f == 0.5 ) )
  193. {
  194. y = jv( -v, x );
  195. f = 1.0 - f;
  196. g = 2.0 * floor(f/2.0);
  197. if( g != f )
  198. y = -y;
  199. return(y);
  200. }
  201. t = 0.25*x*x;
  202. f = fabs(x);
  203. g = 1.5 * fabs(v);
  204. if( (f > 30.0) && (f > g) )
  205. {
  206. onef2err = 1.0e38;
  207. y = 0.0;
  208. }
  209. else
  210. {
  211. y = onef2( 1.0, 1.5, 1.5+v, -t, &onef2err );
  212. }
  213. if( (f < 18.0) || (x < 0.0) )
  214. {
  215. threef0err = 1.0e38;
  216. ya = 0.0;
  217. }
  218. else
  219. {
  220. ya = threef0( 1.0, 0.5, 0.5-v, -1.0/t, &threef0err );
  221. }
  222. f = sqrt( PI );
  223. h = pow( 0.5*x, v-1.0 );
  224. if( onef2err <= threef0err )
  225. {
  226. g = gamma( v + 1.5 );
  227. y = y * h * t / ( 0.5 * f * g );
  228. return(y);
  229. }
  230. else
  231. {
  232. g = gamma( v + 0.5 );
  233. ya = ya * h / ( f * g );
  234. ya = ya + yv( v, x );
  235. return(ya);
  236. }
  237. }
  238. /* Bessel function of noninteger order
  239. */
  240. double yv( v, x )
  241. double v, x;
  242. {
  243. double y, t;
  244. int n;
  245. y = floor( v );
  246. if( y == v )
  247. {
  248. n = v;
  249. y = yn( n, x );
  250. return( y );
  251. }
  252. t = PI * v;
  253. y = (cos(t) * jv( v, x ) - jv( -v, x ))/sin(t);
  254. return( y );
  255. }
  256. /* Crossover points between ascending series and asymptotic series
  257. * for Struve function
  258. *
  259. * v x
  260. *
  261. * 0 19.2
  262. * 1 18.95
  263. * 2 19.15
  264. * 3 19.3
  265. * 5 19.7
  266. * 10 21.35
  267. * 20 26.35
  268. * 30 32.31
  269. * 40 40.0
  270. */