gsl_specfunc__coupling.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /* specfunc/coupling.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002 Gerard Jungman
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. /* Author: G. Jungman */
  20. #include "gsl__config.h"
  21. #include <stdlib.h>
  22. #include "gsl_math.h"
  23. #include "gsl_errno.h"
  24. #include "gsl_sf_gamma.h"
  25. #include "gsl_sf_coupling.h"
  26. #include "gsl_specfunc__error.h"
  27. inline
  28. static
  29. int locMax3(const int a, const int b, const int c)
  30. {
  31. int d = GSL_MAX(a, b);
  32. return GSL_MAX(d, c);
  33. }
  34. inline
  35. static
  36. int locMin3(const int a, const int b, const int c)
  37. {
  38. int d = GSL_MIN(a, b);
  39. return GSL_MIN(d, c);
  40. }
  41. inline
  42. static
  43. int locMin5(const int a, const int b, const int c, const int d, const int e)
  44. {
  45. int f = GSL_MIN(a, b);
  46. int g = GSL_MIN(c, d);
  47. int h = GSL_MIN(f, g);
  48. return GSL_MIN(e, h);
  49. }
  50. /* See: [Thompson, Atlas for Computing Mathematical Functions] */
  51. static
  52. int
  53. delta(int ta, int tb, int tc, gsl_sf_result * d)
  54. {
  55. gsl_sf_result f1, f2, f3, f4;
  56. int status = 0;
  57. status += gsl_sf_fact_e((ta + tb - tc)/2, &f1);
  58. status += gsl_sf_fact_e((ta + tc - tb)/2, &f2);
  59. status += gsl_sf_fact_e((tb + tc - ta)/2, &f3);
  60. status += gsl_sf_fact_e((ta + tb + tc)/2 + 1, &f4);
  61. if(status != 0) {
  62. OVERFLOW_ERROR(d);
  63. }
  64. d->val = f1.val * f2.val * f3.val / f4.val;
  65. d->err = 4.0 * GSL_DBL_EPSILON * fabs(d->val);
  66. return GSL_SUCCESS;
  67. }
  68. static
  69. int
  70. triangle_selection_fails(int two_ja, int two_jb, int two_jc)
  71. {
  72. return ((two_jb < abs(two_ja - two_jc)) || (two_jb > two_ja + two_jc));
  73. }
  74. static
  75. int
  76. m_selection_fails(int two_ja, int two_jb, int two_jc,
  77. int two_ma, int two_mb, int two_mc)
  78. {
  79. return (
  80. abs(two_ma) > two_ja
  81. || abs(two_mb) > two_jb
  82. || abs(two_mc) > two_jc
  83. || GSL_IS_ODD(two_ja + two_ma)
  84. || GSL_IS_ODD(two_jb + two_mb)
  85. || GSL_IS_ODD(two_jc + two_mc)
  86. || (two_ma + two_mb + two_mc) != 0
  87. );
  88. }
  89. /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
  90. int
  91. gsl_sf_coupling_3j_e (int two_ja, int two_jb, int two_jc,
  92. int two_ma, int two_mb, int two_mc,
  93. gsl_sf_result * result)
  94. {
  95. /* CHECK_POINTER(result) */
  96. if(two_ja < 0 || two_jb < 0 || two_jc < 0) {
  97. DOMAIN_ERROR(result);
  98. }
  99. else if ( triangle_selection_fails(two_ja, two_jb, two_jc)
  100. || m_selection_fails(two_ja, two_jb, two_jc, two_ma, two_mb, two_mc)
  101. ) {
  102. result->val = 0.0;
  103. result->err = 0.0;
  104. return GSL_SUCCESS;
  105. }
  106. else {
  107. int jca = (-two_ja + two_jb + two_jc) / 2,
  108. jcb = ( two_ja - two_jb + two_jc) / 2,
  109. jcc = ( two_ja + two_jb - two_jc) / 2,
  110. jmma = ( two_ja - two_ma) / 2,
  111. jmmb = ( two_jb - two_mb) / 2,
  112. jmmc = ( two_jc - two_mc) / 2,
  113. jpma = ( two_ja + two_ma) / 2,
  114. jpmb = ( two_jb + two_mb) / 2,
  115. jpmc = ( two_jc + two_mc) / 2,
  116. jsum = ( two_ja + two_jb + two_jc) / 2,
  117. kmin = locMax3 (0, jpmb - jmmc, jmma - jpmc),
  118. kmax = locMin3 (jcc, jmma, jpmb),
  119. k, sign = GSL_IS_ODD (kmin - jpma + jmmb) ? -1 : 1,
  120. status = 0;
  121. double sum_pos = 0.0, sum_neg = 0.0, norm, term;
  122. gsl_sf_result bc1, bc2, bc3, bcn1, bcn2, bcd1, bcd2, bcd3, bcd4;
  123. status += gsl_sf_choose_e (two_ja, jcc , &bcn1);
  124. status += gsl_sf_choose_e (two_jb, jcc , &bcn2);
  125. status += gsl_sf_choose_e (jsum+1, jcc , &bcd1);
  126. status += gsl_sf_choose_e (two_ja, jmma, &bcd2);
  127. status += gsl_sf_choose_e (two_jb, jmmb, &bcd3);
  128. status += gsl_sf_choose_e (two_jc, jpmc, &bcd4);
  129. if (status != 0) {
  130. OVERFLOW_ERROR (result);
  131. }
  132. norm = sqrt (bcn1.val * bcn2.val)
  133. / sqrt (bcd1.val * bcd2.val * bcd3.val * bcd4.val * ((double) two_jc + 1.0));
  134. for (k = kmin; k <= kmax; k++) {
  135. status += gsl_sf_choose_e (jcc, k, &bc1);
  136. status += gsl_sf_choose_e (jcb, jmma - k, &bc2);
  137. status += gsl_sf_choose_e (jca, jpmb - k, &bc3);
  138. if (status != 0) {
  139. OVERFLOW_ERROR (result);
  140. }
  141. term = bc1.val * bc2.val * bc3.val;
  142. if (sign < 0) {
  143. sum_neg += norm * term;
  144. } else {
  145. sum_pos += norm * term;
  146. }
  147. sign = -sign;
  148. }
  149. result->val = sum_pos - sum_neg;
  150. result->err = 2.0 * GSL_DBL_EPSILON * (sum_pos + sum_neg);
  151. result->err += 2.0 * GSL_DBL_EPSILON * (kmax - kmin) * fabs(result->val);
  152. return GSL_SUCCESS;
  153. }
  154. }
  155. #if ! defined (GSL_DISABLE_DEPRECATED)
  156. int
  157. gsl_sf_coupling_6j_INCORRECT_e(int two_ja, int two_jb, int two_jc,
  158. int two_jd, int two_je, int two_jf,
  159. gsl_sf_result * result)
  160. {
  161. return gsl_sf_coupling_6j_e(two_ja, two_jb, two_je, two_jd, two_jc, two_jf, result);
  162. }
  163. #endif
  164. int
  165. gsl_sf_coupling_6j_e(int two_ja, int two_jb, int two_jc,
  166. int two_jd, int two_je, int two_jf,
  167. gsl_sf_result * result)
  168. {
  169. /* CHECK_POINTER(result) */
  170. if( two_ja < 0 || two_jb < 0 || two_jc < 0
  171. || two_jd < 0 || two_je < 0 || two_jf < 0
  172. ) {
  173. DOMAIN_ERROR(result);
  174. }
  175. else if( triangle_selection_fails(two_ja, two_jb, two_jc)
  176. || triangle_selection_fails(two_ja, two_je, two_jf)
  177. || triangle_selection_fails(two_jb, two_jd, two_jf)
  178. || triangle_selection_fails(two_je, two_jd, two_jc)
  179. ) {
  180. result->val = 0.0;
  181. result->err = 0.0;
  182. return GSL_SUCCESS;
  183. }
  184. else {
  185. gsl_sf_result n1;
  186. gsl_sf_result d1, d2, d3, d4, d5, d6;
  187. double norm;
  188. int tk, tkmin, tkmax;
  189. double phase;
  190. double sum_pos = 0.0;
  191. double sum_neg = 0.0;
  192. double sumsq_err = 0.0;
  193. int status = 0;
  194. status += delta(two_ja, two_jb, two_jc, &d1);
  195. status += delta(two_ja, two_je, two_jf, &d2);
  196. status += delta(two_jb, two_jd, two_jf, &d3);
  197. status += delta(two_je, two_jd, two_jc, &d4);
  198. if(status != GSL_SUCCESS) {
  199. OVERFLOW_ERROR(result);
  200. }
  201. norm = sqrt(d1.val) * sqrt(d2.val) * sqrt(d3.val) * sqrt(d4.val);
  202. tkmin = locMax3(0,
  203. two_ja + two_jd - two_jc - two_jf,
  204. two_jb + two_je - two_jc - two_jf);
  205. tkmax = locMin5(two_ja + two_jb + two_je + two_jd + 2,
  206. two_ja + two_jb - two_jc,
  207. two_je + two_jd - two_jc,
  208. two_ja + two_je - two_jf,
  209. two_jb + two_jd - two_jf);
  210. phase = GSL_IS_ODD((two_ja + two_jb + two_je + two_jd + tkmin)/2)
  211. ? -1.0
  212. : 1.0;
  213. for(tk=tkmin; tk<=tkmax; tk += 2) {
  214. double term;
  215. double term_err;
  216. gsl_sf_result den_1, den_2;
  217. gsl_sf_result d1_a, d1_b;
  218. status = 0;
  219. status += gsl_sf_fact_e((two_ja + two_jb + two_je + two_jd - tk)/2 + 1, &n1);
  220. status += gsl_sf_fact_e(tk/2, &d1_a);
  221. status += gsl_sf_fact_e((two_jc + two_jf - two_ja - two_jd + tk)/2, &d1_b);
  222. status += gsl_sf_fact_e((two_jc + two_jf - two_jb - two_je + tk)/2, &d2);
  223. status += gsl_sf_fact_e((two_ja + two_jb - two_jc - tk)/2, &d3);
  224. status += gsl_sf_fact_e((two_je + two_jd - two_jc - tk)/2, &d4);
  225. status += gsl_sf_fact_e((two_ja + two_je - two_jf - tk)/2, &d5);
  226. status += gsl_sf_fact_e((two_jb + two_jd - two_jf - tk)/2, &d6);
  227. if(status != GSL_SUCCESS) {
  228. OVERFLOW_ERROR(result);
  229. }
  230. d1.val = d1_a.val * d1_b.val;
  231. d1.err = d1_a.err * fabs(d1_b.val) + fabs(d1_a.val) * d1_b.err;
  232. den_1.val = d1.val*d2.val*d3.val;
  233. den_1.err = d1.err * fabs(d2.val*d3.val);
  234. den_1.err += d2.err * fabs(d1.val*d3.val);
  235. den_1.err += d3.err * fabs(d1.val*d2.val);
  236. den_2.val = d4.val*d5.val*d6.val;
  237. den_2.err = d4.err * fabs(d5.val*d6.val);
  238. den_2.err += d5.err * fabs(d4.val*d6.val);
  239. den_2.err += d6.err * fabs(d4.val*d5.val);
  240. term = phase * n1.val / den_1.val / den_2.val;
  241. phase = -phase;
  242. term_err = n1.err / fabs(den_1.val) / fabs(den_2.val);
  243. term_err += fabs(term / den_1.val) * den_1.err;
  244. term_err += fabs(term / den_2.val) * den_2.err;
  245. if(term >= 0.0) {
  246. sum_pos += norm*term;
  247. }
  248. else {
  249. sum_neg -= norm*term;
  250. }
  251. sumsq_err += norm*norm * term_err*term_err;
  252. }
  253. result->val = sum_pos - sum_neg;
  254. result->err = 2.0 * GSL_DBL_EPSILON * (sum_pos + sum_neg);
  255. result->err += sqrt(sumsq_err / (0.5*(tkmax-tkmin)+1.0));
  256. result->err += 2.0 * GSL_DBL_EPSILON * (tkmax - tkmin + 2.0) * fabs(result->val);
  257. return GSL_SUCCESS;
  258. }
  259. }
  260. int
  261. gsl_sf_coupling_RacahW_e(int two_ja, int two_jb, int two_jc,
  262. int two_jd, int two_je, int two_jf,
  263. gsl_sf_result * result)
  264. {
  265. int status = gsl_sf_coupling_6j_e(two_ja, two_jb, two_je, two_jd, two_jc, two_jf, result);
  266. int phase_sum = (two_ja + two_jb + two_jc + two_jd)/2;
  267. result->val *= ( GSL_IS_ODD(phase_sum) ? -1.0 : 1.0 );
  268. return status;
  269. }
  270. int
  271. gsl_sf_coupling_9j_e(int two_ja, int two_jb, int two_jc,
  272. int two_jd, int two_je, int two_jf,
  273. int two_jg, int two_jh, int two_ji,
  274. gsl_sf_result * result)
  275. {
  276. /* CHECK_POINTER(result) */
  277. if( two_ja < 0 || two_jb < 0 || two_jc < 0
  278. || two_jd < 0 || two_je < 0 || two_jf < 0
  279. || two_jg < 0 || two_jh < 0 || two_ji < 0
  280. ) {
  281. DOMAIN_ERROR(result);
  282. }
  283. else if( triangle_selection_fails(two_ja, two_jb, two_jc)
  284. || triangle_selection_fails(two_jd, two_je, two_jf)
  285. || triangle_selection_fails(two_jg, two_jh, two_ji)
  286. || triangle_selection_fails(two_ja, two_jd, two_jg)
  287. || triangle_selection_fails(two_jb, two_je, two_jh)
  288. || triangle_selection_fails(two_jc, two_jf, two_ji)
  289. ) {
  290. result->val = 0.0;
  291. result->err = 0.0;
  292. return GSL_SUCCESS;
  293. }
  294. else {
  295. int tk;
  296. int tkmin = locMax3(abs(two_ja-two_ji), abs(two_jh-two_jd), abs(two_jb-two_jf));
  297. int tkmax = locMin3(two_ja + two_ji, two_jh + two_jd, two_jb + two_jf);
  298. double sum_pos = 0.0;
  299. double sum_neg = 0.0;
  300. double sumsq_err = 0.0;
  301. double phase;
  302. for(tk=tkmin; tk<=tkmax; tk += 2) {
  303. gsl_sf_result s1, s2, s3;
  304. double term;
  305. double term_err;
  306. int status = 0;
  307. status += gsl_sf_coupling_6j_e(two_ja, two_ji, tk, two_jh, two_jd, two_jg, &s1);
  308. status += gsl_sf_coupling_6j_e(two_jb, two_jf, tk, two_jd, two_jh, two_je, &s2);
  309. status += gsl_sf_coupling_6j_e(two_ja, two_ji, tk, two_jf, two_jb, two_jc, &s3);
  310. if(status != GSL_SUCCESS) {
  311. OVERFLOW_ERROR(result);
  312. }
  313. term = s1.val * s2.val * s3.val;
  314. term_err = s1.err * fabs(s2.val*s3.val);
  315. term_err += s2.err * fabs(s1.val*s3.val);
  316. term_err += s3.err * fabs(s1.val*s2.val);
  317. if(term >= 0.0) {
  318. sum_pos += (tk + 1) * term;
  319. }
  320. else {
  321. sum_neg -= (tk + 1) * term;
  322. }
  323. sumsq_err += ((tk+1) * term_err) * ((tk+1) * term_err);
  324. }
  325. phase = GSL_IS_ODD(tkmin) ? -1.0 : 1.0;
  326. result->val = phase * (sum_pos - sum_neg);
  327. result->err = 2.0 * GSL_DBL_EPSILON * (sum_pos + sum_neg);
  328. result->err += sqrt(sumsq_err / (0.5*(tkmax-tkmin)+1.0));
  329. result->err += 2.0 * GSL_DBL_EPSILON * (tkmax-tkmin + 2.0) * fabs(result->val);
  330. return GSL_SUCCESS;
  331. }
  332. }
  333. /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
  334. #include "gsl_specfunc__eval.h"
  335. double gsl_sf_coupling_3j(int two_ja, int two_jb, int two_jc,
  336. int two_ma, int two_mb, int two_mc)
  337. {
  338. EVAL_RESULT(gsl_sf_coupling_3j_e(two_ja, two_jb, two_jc,
  339. two_ma, two_mb, two_mc,
  340. &result));
  341. }
  342. #if ! defined (GSL_DISABLE_DEPRECATED)
  343. double gsl_sf_coupling_6j_INCORRECT(int two_ja, int two_jb, int two_jc,
  344. int two_jd, int two_je, int two_jf)
  345. {
  346. EVAL_RESULT(gsl_sf_coupling_6j_INCORRECT_e(two_ja, two_jb, two_jc,
  347. two_jd, two_je, two_jf,
  348. &result));
  349. }
  350. #endif
  351. double gsl_sf_coupling_6j(int two_ja, int two_jb, int two_jc,
  352. int two_jd, int two_je, int two_jf)
  353. {
  354. EVAL_RESULT(gsl_sf_coupling_6j_e(two_ja, two_jb, two_jc,
  355. two_jd, two_je, two_jf,
  356. &result));
  357. }
  358. double gsl_sf_coupling_RacahW(int two_ja, int two_jb, int two_jc,
  359. int two_jd, int two_je, int two_jf)
  360. {
  361. EVAL_RESULT(gsl_sf_coupling_RacahW_e(two_ja, two_jb, two_jc,
  362. two_jd, two_je, two_jf,
  363. &result));
  364. }
  365. double gsl_sf_coupling_9j(int two_ja, int two_jb, int two_jc,
  366. int two_jd, int two_je, int two_jf,
  367. int two_jg, int two_jh, int two_ji)
  368. {
  369. EVAL_RESULT(gsl_sf_coupling_9j_e(two_ja, two_jb, two_jc,
  370. two_jd, two_je, two_jf,
  371. two_jg, two_jh, two_ji,
  372. &result));
  373. }