Ticket #5996: wigner.py

File wigner.py, 26.9 KB (added by jrasch, 10 months ago)
Line 
1r"""
2Calculate Wigner 3j, 6j, 9j, Clebsch-Gordan, Racah and Gaunt
3coefficients
4
5Collection of functions for calculating Wigner 3j, 6j, 9j,
6Clebsch-Gordan, Racah as well as Gaunt coefficients exactly, all
7evaluating to a rational number times the square root of a rational
8number [Rasch03].
9
10Please see the description of the individual functions for further
11details and examples.
12
13
14REFERENCES:
15
16- [Rasch03] 'Efficient Storage Scheme for Pre-calculated Wigner 3j, 6j
17  and Gaunt Coefficients', J. Rasch and A. C. H. Yu, SIAM
18  J. Sci. Comput. Volume 25, Issue 4, pp. 1416-1428 (2003)
19
20
21AUTHORS:
22
23- Jens Rasch (2009-03-24): initial version for Sage
24- Jens Rasch (2009-05-31): updated to sage-4.0
25
26"""
27
28#***********************************************************************
29#       Copyright (C) 2008 Jens Rasch <jyr2000@gmail.com>
30#
31#  Distributed under the terms of the GNU General Public License (GPL)
32#                  http://www.gnu.org/licenses/
33#***********************************************************************
34
35from sage.all import *
36# from sage.calculus.calculus   import sqrt,factorial
37# from sage.functions.constants import pi
38
39
40
41# This list of precomputed factorials is needed to massively
42# accelerate future calculations of the various coefficients
43_Factlist=[1]
44
45def _calc_factlist(nn):
46    if nn>=len(_Factlist):
47        for ii in range(len(_Factlist),nn+1):
48            _Factlist.append(_Factlist[ii-1]*ii)
49            #_Factlist.append(factorial(ii))
50    #return _Factlist
51
52
53def test_calc_factlist(nn):
54    r"""
55    Function calculates a list of precomputed factorials in order to
56    massively accelerate future calculations of the various
57    coefficients.
58
59    INPUT:
60
61     - nn Highest factorial to be computed
62
63    OUTPUT:
64
65    integer list of factorials
66
67
68    EXAMPLES:
69
70    Calculate list of factorials:
71
72        sage: test_calc_factlist(10)
73        [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
74    """
75    _calc_factlist(nn)
76    return _Factlist[:nn+1]
77
78
79
80def Wigner3j(j_1,j_2,j_3,m_1,m_2,m_3,prec=None):
81    r"""
82    Calculate the Wigner 3j symbol
83
84    \left({j_1 \atop m_1} {j_2 \atop m_2} {j_3 \atop m_3} \right)
85
86
87    NOTES:
88
89    The Wigner 3j symbol obeys the following symmetry rules:
90
91    - invariant under any permutation of the columns (with the
92      exception of a sign change where $J:=j_1+j_2+j_3$):
93      \begin{eqnarray}
94      \left({j_1 \atop m_1} {j_2 \atop m_2} {j_3 \atop m_3}\right)
95      &=&
96      \left({j_3 \atop m_3} {j_1 \atop m_1} {j_2 \atop m_2}\right)
97      =\left({j_2 \atop m_2} {j_3 \atop m_3} {j_1 \atop m_1}\right)
98      \qquad \mbox{cyclic}
99      &=&
100       (-)^{J}\left({j_3\atop m_3} {j_2\atop m_2} {j_1\atop m_1}\right)
101      =(-)^{J}\left({j_1\atop m_1} {j_3\atop m_3} {j_2\atop m_2}\right)
102      =(-)^{J}\left({j_2\atop m_2} {j_1\atop m_1} {j_3\atop m_3}\right)
103      \qquad\mbox{anti-cyclic}
104      \end{eqnarray}
105 
106    - invariant under space inflection, i. e.
107      \begin{eqnarray}
108      \left({{j_1}\atop{m_1}} {{j_2}\atop{m_2}} {{j_3}\atop{m_3}}\right)
109      =
110      (-)^{J}
111      \left({j_1 \atop -m_1} {j_2 \atop -m_2}{j_3 \atop -m_3}\right)
112      \end{eqnarray}
113 
114    - symmetric with respect to the 72 additional symmetries based on
115      the work by [Regge58]
116
117    - zero for $j_1$, $j_2$, $j_3$ not fulfilling triangle relation
118   
119    - zero for ${m_1}+{m_2}+{m_3}!= 0$
120
121    - zero for violating any one of the conditions
122      $j_1\ge|m_1|$,  $j_2\ge|m_2|$,  $j_3\ge|m_3|$
123
124
125    ALGORITHM:
126
127    This function uses the algorithm of [Edmonds74] to calculate the
128    value of the 3j symbol exactly. Note that the formula contains
129    alternating sums over large factorials and is therefore unsuitable
130    for finite precision arithmetic and only useful for a computer
131    algebra system [Rasch03].
132
133
134
135    INPUT:
136
137    j_1 - integer or half integer
138    j_2 - integer or half integer
139    j_3 - integer or half integer
140    m_1 - integer or half integer
141    m_2 - integer or half integer
142    m_3 - integer or half integer
143    prec - precision, default: None. Providing a precision can
144           drastically speed up the calculation.
145
146
147    OUTPUT:
148
149    The result will be a rational number times the square root of a
150    rational number, unless a precision is given.
151
152
153    EXAMPLES:
154
155    A couple of examples:
156
157        sage: Wigner3j(2,6,4,0,0,0)
158        sqrt(5/143)
159
160        sage: Wigner3j(2,6,4,0,0,1)
161        0
162
163        sage: Wigner3j(0.5,0.5,1,0.5,-0.5,0)
164        sqrt(1/6)
165
166        sage: Wigner3j(40,100,60,-10,60,-50)
167        95608/18702538494885*sqrt(21082735836735314343364163310/220491455010479533763)
168
169        sage: Wigner3j(2500,2500,5000,2488,2400,-4888 ,prec=64)
170        7.60424456883448589e-12
171
172
173    It is an error to have arguments that are not integer or half
174    integer values:
175
176        sage: Wigner3j(2.1,6,4,0,0,0)
177        Traceback (most recent call last):
178        ...
179        ValueError: j values must be integer or half integer
180
181        sage: Wigner3j(2,6,4,1,0,-1.1)
182        Traceback (most recent call last):
183        ...
184        ValueError: m values must be integer or half integer
185
186
187
188    REFERENCES:
189
190    - [Regge58] 'Symmetry Properties of Clebsch-Gordan Coefficients',
191      T. Regge, Nuovo Cimento, Volume 10, pp. 544 (1958)
192
193    - [Edmonds74] 'Angular Momentum in Quantum Mechanics',
194      A. R. Edmonds, Princeton University Press (1974)
195
196    - [Rasch03] 'Efficient Storage Scheme for Pre-calculated Wigner 3j,
197      6j and Gaunt Coefficients', J. Rasch and A. C. H. Yu, SIAM
198      J. Sci. Comput. Volume 25, Issue 4, pp. 1416-1428 (2003)
199
200
201    AUTHORS:
202
203    - Jens Rasch (2009-03-24): initial version
204
205    """
206
207    if int(j_1*2)!=j_1*2 or int(j_2*2)!=j_2*2 or int(j_3*2)!=j_3*2:
208        raise ValueError("j values must be integer or half integer")
209        #return 0
210    if int(m_1*2)!=m_1*2 or int(m_2*2)!=m_2*2 or int(m_3*2)!=m_3*2:
211        raise ValueError("m values must be integer or half integer")
212        #return 0
213    if (m_1+m_2+m_3<>0):
214        return 0
215    prefid=Integer((-1)**(int(j_1-j_2-m_3)))
216    m_3=-m_3
217    a1=j_1+j_2-j_3
218    if (a1<0):
219        return 0
220    a2=j_1-j_2+j_3
221    if (a2<0):
222        return 0
223    a3=-j_1+j_2+j_3
224    if (a3<0):
225        return 0
226    if (abs(m_1)>j_1) or (abs(m_2)>j_2) or (abs(m_3)>j_3):
227        return 0
228
229    maxfact=max(j_1+j_2+j_3+1,j_1+abs(m_1),j_2+abs(m_2),j_3+abs(m_3))
230    _calc_factlist(maxfact)
231
232    #try:
233    argsqrt=Integer(_Factlist[int(j_1+j_2-j_3)]\
234                     *_Factlist[int(j_1-j_2+j_3)]\
235             *_Factlist[int(-j_1+j_2+j_3)]\
236             *_Factlist[int(j_1-m_1)]*_Factlist[int(j_1+m_1)]\
237             *_Factlist[int(j_2-m_2)]*_Factlist[int(j_2+m_2)]\
238             *_Factlist[int(j_3-m_3)]*_Factlist[j_3+m_3])\
239             /_Factlist[int(j_1+j_2+j_3+1)]\
240    #except:
241    #    return 0
242
243    ressqrt=sqrt(argsqrt,prec)
244    if type(ressqrt)==sage.rings.complex_number.ComplexNumber:
245        ressqrt=ressqrt.real()
246   
247    imin=max(-j_3+j_1+m_2,-j_3+j_2-m_1,0)
248    imax=min(j_2+m_2,j_1-m_1,j_1+j_2-j_3)
249    sumres=0
250    for ii in range(imin,imax+1):                   
251        den=_Factlist[ii]*_Factlist[int(ii+j_3-j_1-m_2)]\
252             *_Factlist[int(j_2+m_2-ii)]*_Factlist[int(j_1-ii-m_1)]\
253             *_Factlist[int(ii+j_3-j_2+m_1)]\
254             *_Factlist[int(j_1+j_2-j_3-ii)]
255        sumres=sumres+Integer((-1)**ii)/den
256
257    res=ressqrt*sumres*prefid
258    return res
259
260
261def ClebschGordan(j_1,j_2,j_3,m_1,m_2,m_3,prec=None):
262    r"""
263    Calculates the Clebsch-Gordan coefficient
264
265    $\left< j_1 m_1 \; j_2 m_2 | j_3 m_3 \right>$
266
267
268    NOTES:
269
270    The Clebsch-Gordan coefficient will be evaluated via its relation
271    to Wigner 3j symbols:
272
273    \begin{eqnarray}
274    \left< j_1 m_1 \; j_2 m_2 | j_3 m_3 \right>=
275    (-1)^(j_1-j_2+m_3) \sqrt(2j_3+1)
276    \left({j_1 \atop m_1} {j_2 \atop m_2} {j_3 \atop -m_3}\right)
277    \end{eqnarray}
278
279    See also the documentation on Wigner 3j symbols which exhibit much
280    higher symmetry relations that the Clebsch-Gordan coefficient.
281
282
283    INPUT:
284
285    j_1 - integer or half integer
286    j_2 - integer or half integer
287    j_3 - integer or half integer
288    m_1 - integer or half integer
289    m_2 - integer or half integer
290    m_3 - integer or half integer
291    prec - precision, default: None. Providing a precision can
292           drastically speed up the calculation.
293
294
295    OUTPUT:
296
297    The result will be a rational number times the square root of a
298    rational number, unless a precision is given.
299
300
301    EXAMPLES:
302
303    A couple of examples:
304
305        sage: simplify(ClebschGordan(3/2,1/2,2, 3/2,1/2,2))
306        1
307       
308        sage: ClebschGordan(1.5,0.5,1, 1.5,-0.5,1)
309        1/2*sqrt(3)
310
311        sage: ClebschGordan(3/2,1/2,1, -1/2,1/2,0)
312        -sqrt(1/6)*sqrt(3)
313
314   
315    REFERENCES:
316
317    - [Edmonds74] 'Angular Momentum in Quantum Mechanics',
318      A. R. Edmonds, Princeton University Press (1974)
319
320    - [Rasch03] 'Efficient Storage Scheme for Pre-calculated Wigner 3j,
321      6j and Gaunt Coefficients', J. Rasch and A. C. H. Yu, SIAM
322      J. Sci. Comput. Volume 25, Issue 4, pp. 1416-1428 (2003)
323
324
325    AUTHORS:
326
327    - Jens Rasch (2009-03-24): initial version
328
329    """
330
331    res=(-1)**(int(j_1-j_2+m_3))*sqrt(2*j_3+1)\
332         *Wigner3j(j_1,j_2,j_3,m_1,m_2,-m_3,prec)
333    return res
334
335
336
337
338
339def _bigDeltacoeff(aa,bb,cc,prec=None):
340    r"""
341    Calculates the Delta coefficient of the 3 angular momenta for
342    Racah symbols. Also checks that the differences are of integer
343    value.
344
345    INPUT:
346
347     - aa - first angular momentum, integer or half integer
348     - bb - second angular momentum, integer or half integer
349     - cc - third angular momentum, integer or half integer
350     - prec - precision of the sqrt() calculation
351
352    OUTPUT:
353
354    double - Value of the Delta coefficient
355
356    """
357
358    if(int(aa+bb-cc)!=(aa+bb-cc)):
359        raise ValueError("j values must be integer or half integer and fulfil the triangle relation")
360        #return 0
361    if(int(aa+cc-bb)!=(aa+cc-bb)):
362        raise ValueError("j values must be integer or half integer and fulfil the triangle relation")
363        #return 0
364    if(int(bb+cc-aa)!=(bb+cc-aa)):
365        raise ValueError("j values must be integer or half integer and fulfil the triangle relation")
366        #return 0
367    if(aa+bb-cc)<0:
368        return 0
369    if(aa+cc-bb)<0:
370        return 0
371    if(bb+cc-aa)<0:
372        return 0
373 
374    maxfact=max(aa+bb-cc,aa+cc-bb,bb+cc-aa,aa+bb+cc+1)
375    _calc_factlist(maxfact)
376
377    argsqrt=Integer(_Factlist[int(aa+bb-cc)]*_Factlist[int(aa+cc-bb)]\
378                    *_Factlist[int(bb+cc-aa)])\
379                    /Integer(_Factlist[int(aa+bb+cc+1)])
380
381    ressqrt=sqrt(argsqrt,prec)
382    if type(ressqrt)==sage.rings.complex_number.ComplexNumber:
383        res=ressqrt.real()
384    else:
385        res=ressqrt
386    return res
387
388 
389def test_bigDeltacoeff(aa,bb,cc,prec=None):
390    r"""
391    Test for the Delta coefficient of the 3 angular momenta for
392    Racah symbols. Also checks that the differences are of integer
393    value.
394
395    INPUT:
396
397     - aa - first angular momentum, integer or half integer
398     - bb - second angular momentum, integer or half integer
399     - cc - third angular momentum, integer or half integer
400     - prec - precision of the sqrt() calculation
401
402    OUTPUT:
403
404    double - Value of the Delta coefficient
405
406    EXAMPLES:
407
408    Simple examples:
409
410        sage: test_bigDeltacoeff(1,1,1)
411        1/2*sqrt(1/6)
412
413    """
414
415    return _bigDeltacoeff(aa,bb,cc,prec)
416 
417
418
419
420def Racah(aa,bb,cc,dd,ee,ff,prec=None):
421    r"""
422    Calculate the Racah symbol
423
424    W(a,b,c,d;e,f)
425
426    NOTES:
427
428    The Racah symbol is related to the Wigner 6j symbol:
429    \begin{eqnarray}
430    \left({j_1 \atop j_4} {j_2 \atop j_5} {j_3 \atop j_6} \right)
431    =
432    (-1)^{j_1+j_2+j_4+j_5} W(j_1,j_2,j_5,j_4,j_3,j_6)
433    \end{eqnarray}
434
435    Please see the 6j symbol for its much richer symmetries and for
436    additional properties.
437
438
439    ALGORITHM:
440
441    This function uses the algorithm of [Edmonds74] to calculate the
442    value of the 6j symbol exactly. Note that the formula contains
443    alternating sums over large factorials and is therefore unsuitable
444    for finite precision arithmetic and only useful for a computer
445    algebra system [Rasch03].
446
447
448    INPUT:
449
450    a - integer or half integer
451    b - integer or half integer
452    c - integer or half integer
453    d - integer or half integer
454    e - integer or half integer
455    f - integer or half integer
456    prec - precision, default: None. Providing a precision can
457           drastically speed up the calculation.
458
459
460    OUTPUT:
461
462    The result will be a rational number times the square root of a
463    rational number, unless a precision is given.
464
465
466    EXAMPLES:
467
468    A couple of examples and test cases:
469
470        sage: Racah(3,3,3,3,3,3)
471        -1/14
472   
473
474    REFERENCES:
475
476    - [Edmonds74] 'Angular Momentum in Quantum Mechanics',
477      A. R. Edmonds, Princeton University Press (1974)
478
479    - [Rasch03] 'Efficient Storage Scheme for Pre-calculated Wigner 3j,
480      6j and Gaunt Coefficients', J. Rasch and A. C. H. Yu, SIAM
481      J. Sci. Comput. Volume 25, Issue 4, pp. 1416-1428 (2003)
482
483
484    AUTHORS:
485
486    - Jens Rasch (2009-03-24): initial version
487
488    """
489
490    prefac=_bigDeltacoeff(aa,bb,ee,prec)*_bigDeltacoeff(cc,dd,ee,prec)\
491            *_bigDeltacoeff(aa,cc,ff,prec)*_bigDeltacoeff(bb,dd,ff,prec)
492    if prefac==0:
493        return 0
494    imin=max(aa+bb+ee,cc+dd+ee,aa+cc+ff,bb+dd+ff)
495    imax=min(aa+bb+cc+dd,aa+dd+ee+ff,bb+cc+ee+ff)
496
497    maxfact=max(imax+1,aa+bb+cc+dd,aa+dd+ee+ff,bb+cc+ee+ff)
498    _calc_factlist(maxfact)
499
500    sumres=0
501    for kk in range(imin,imax+1):
502        den=_Factlist[int(kk-aa-bb-ee)]*_Factlist[int(kk-cc-dd-ee)]\
503            *_Factlist[int(kk-aa-cc-ff)]\
504            *_Factlist[int(kk-bb-dd-ff)]*_Factlist[int(aa+bb+cc+dd-kk)]\
505            *_Factlist[int(aa+dd+ee+ff-kk)]\
506            *_Factlist[int(bb+cc+ee+ff-kk)]
507        sumres=sumres+Integer((-1)**kk*_Factlist[kk+1])/den
508       
509    res=prefac*sumres*(-1)**(int(aa+bb+cc+dd))
510    return res
511
512
513
514
515
516def Wigner6j(j_1,j_2,j_3,j_4,j_5,j_6,prec=None):
517    r"""
518    Calculate the Wigner 6j symbol
519
520    \left({j_1 \atop j_4} {j_2 \atop j_5} {j_3 \atop j_6} \right)
521
522
523    NOTES:
524
525    The Wigner 6j symbol is related to the Racah symbol but exhibits
526    more symmetries as detailed below.
527    \begin{eqnarray}
528    \left({j_1 \atop j_4} {j_2 \atop j_5} {j_3 \atop j_6} \right)
529    =
530    (-1)^{j_1+j_2+j_4+j_5} W(j_1,j_2,j_5,j_4,j_3,j_6)
531    \end{eqnarray}
532
533    The Wigner 6j symbol obeys the following symmetry rules:
534
535    - Wigner $6j$ symbols are left invariant under any permutation of
536      the columns:
537      \begin{eqnarray}
538      \left({j_1 \atop j_4} {j_2 \atop j_5} {j_3 \atop j_6} \right)
539      &=&
540       \left({j_3 \atop j_6} {j_1 \atop j_4} {j_2 \atop j_5} \right)
541      =\left({j_2 \atop j_5} {j_3 \atop j_6} {j_2 \atop j_4} \right)
542      \qquad \mbox{cyclic} \\
543      &=&
544       \left({j_3 \atop j_6} {j_2 \atop j_5} {j_1 \atop j_4} \right)
545      =\left({j_1 \atop j_4} {j_3 \atop j_6} {j_2 \atop j_5} \right)
546      =\left({j_2 \atop j_5} {j_1 \atop j_4} {j_3 \atop j_6} \right)
547      \qquad \mbox{anti-cyclic}
548      \end{eqnarray}
549
550    - They are invariant under the exchange of the upper and lower
551      arguments in each of any two columns, i. e.
552      \begin{eqnarray}
553      \left({j_1 \atop j_4} {j_2 \atop j_5} {j_3 \atop j_6} \right)
554      =
555      \left({j_1 \atop j_4} {j_5 \atop j_2} {j_6 \atop j_3} \right)
556      =
557      \left({j_4 \atop j_1} {j_2 \atop j_5} {j_6 \atop j_3} \right)
558      =
559      \left({j_4 \atop j_1} {j_5 \atop j_2} {j_3 \atop j_6} \right)
560      \end{eqnarray}
561   
562    - additional 6 symmetries [Regge59] giving rise to 144 symmetries
563      in total
564
565    - only non-zero if any triple of $j$'s fulfil a triangle relation
566
567
568    ALGORITHM:
569
570    This function uses the algorithm of [Edmonds74] to calculate the
571    value of the 6j symbol exactly. Note that the formula contains
572    alternating sums over large factorials and is therefore unsuitable
573    for finite precision arithmetic and only useful for a computer
574    algebra system [Rasch03].
575
576
577    INPUT:
578
579    j_1 - integer or half integer
580    j_2 - integer or half integer
581    j_3 - integer or half integer
582    j_4 - integer or half integer
583    j_5 - integer or half integer
584    j_6 - integer or half integer
585    prec - precision, default: None. Providing a precision can
586           drastically speed up the calculation.
587
588
589    OUTPUT:
590
591    The result will be a rational number times the square root of a
592    rational number, unless a precision is given.
593
594
595    EXAMPLES:
596
597    A couple of examples and test cases:
598
599        sage: Wigner6j(3,3,3,3,3,3)
600        -1/14
601   
602        sage: Wigner6j(5,5,5,5,5,5)
603        1/52
604
605        sage: Wigner6j(6,6,6,6,6,6)
606        309/10868
607       
608        sage: Wigner6j(8,8,8,8,8,8)
609        -12219/965770
610
611        sage: Wigner6j(30,30,30,30,30,30)
612        36082186869033479581/87954851694828981714124
613       
614        sage: Wigner6j(0.5,0.5,1,0.5,0.5,1)
615        1/6
616
617        sage: Wigner6j(200,200,200,200,200,200, prec=1000)*1.0
618        0.000155903212413242
619
620
621    It is an error to have arguments that are not integer or half
622    integer values or do not fulfil the triangle relation:
623
624        sage: Wigner6j(2.5,2.5,2.5,2.5,2.5,2.5)
625        Traceback (most recent call last):
626        ...
627        ValueError: j values must be integer or half integer and fulfil the triangle relation
628
629        sage: Wigner6j(0.5,0.5,1.1,0.5,0.5,1.1)
630        Traceback (most recent call last):
631        ...
632        ValueError: j values must be integer or half integer and fulfil the triangle relation
633
634
635    REFERENCES:
636
637    - [Regge59] 'Symmetry Properties of Racah Coefficients',
638      T. Regge, Nuovo Cimento, Volume 11, pp. 116 (1959)
639
640    - [Edmonds74] 'Angular Momentum in Quantum Mechanics',
641      A. R. Edmonds, Princeton University Press (1974)
642
643    - [Rasch03] 'Efficient Storage Scheme for Pre-calculated Wigner 3j,
644      6j and Gaunt Coefficients', J. Rasch and A. C. H. Yu, SIAM
645      J. Sci. Comput. Volume 25, Issue 4, pp. 1416-1428 (2003)
646
647    """
648
649    res=(-1)**(int(j_1+j_2+j_4+j_5))*Racah(j_1,j_2,j_5,j_4,j_3,j_6,prec)
650    return res
651
652
653
654
655def Wigner9j(j_1,j_2,j_3,j_4,j_5,j_6,j_7,j_8,j_9,prec=None):
656    r"""
657    Calculate the Wigner 9j symbol
658
659    \left\{
660      \begin{matrix}
661        {j_1} & {j_2} & {j_3} \\
662        {j_4} & {j_5} & {j_6} \\
663        {j_7} & {j_8} & {j_9}
664      \end{matrix}
665    \right\}
666
667
668    ALGORITHM:
669
670    This function uses the algorithm of [Edmonds74] to calculate the
671    value of the 3j symbol exactly. Note that the formula contains
672    alternating sums over large factorials and is therefore unsuitable
673    for finite precision arithmetic and only useful for a computer
674    algebra system [Rasch03].
675
676
677    INPUT:
678
679    j_1 - integer or half integer
680    j_2 - integer or half integer
681    j_3 - integer or half integer
682    j_4 - integer or half integer
683    j_5 - integer or half integer
684    j_6 - integer or half integer
685    j_7 - integer or half integer
686    j_8 - integer or half integer
687    j_9 - integer or half integer
688    prec - precision, default: None. Providing a precision can
689           drastically speed up the calculation.
690
691
692    OUTPUT:
693
694    The result will be a rational number times the square root of a
695    rational number, unless a precision is given.
696
697
698    EXAMPLES:
699
700    A couple of examples and test cases, note that for speed reasons a
701    precision is given:
702
703        sage: Wigner9j(1,1,1, 1,1,1, 1,1,0 ,prec=64) # ==1/18
704        0.0555555555555555555
705
706        sage: Wigner9j(1,1,1, 1,1,1, 1,1,1)
707        0
708
709        sage: Wigner9j(1,1,1, 1,1,1, 1,1,2 ,prec=64) # ==1/18
710        0.0555555555555555556
711
712        sage: Wigner9j(1,2,1, 2,2,2, 1,2,1 ,prec=64) # ==-1/150
713        -0.00666666666666666667
714
715        sage: Wigner9j(3,3,2, 2,2,2, 3,3,2 ,prec=64) # ==157/14700
716        0.0106802721088435374
717
718        sage: Wigner9j(3,3,2, 3,3,2, 3,3,2 ,prec=64) # ==3221*sqrt(70)/(246960*sqrt(105)) - 365/(3528*sqrt(70)*sqrt(105))
719        0.00944247746651111739
720
721        sage: Wigner9j(3,3,1, 3.5,3.5,2, 3.5,3.5,1 ,prec=64) # ==3221*sqrt(70)/(246960*sqrt(105)) - 365/(3528*sqrt(70)*sqrt(105))
722        0.0110216678544351364
723
724        sage: Wigner9j(100,80,50, 50,100,70, 60,50,100 ,prec=1000)*1.0
725        1.05597798065761e-7
726
727        sage: Wigner9j(30,30,10, 30.5,30.5,20, 30.5,30.5,10 ,prec=1000)*1.0 # ==(80944680186359968990/95103769817469)*sqrt(1/682288158959699477295)
728        0.0000325841699408828
729
730        sage: Wigner9j(64,62.5,114.5, 61.5,61,112.5, 113.5,110.5,60, prec=1000)*1.0
731        -3.41407910055520e-39
732
733        sage: Wigner9j(15,15,15, 15,3,15, 15,18,10, prec=1000)*1.0
734        -0.0000778324615309539
735
736        sage: Wigner9j(1.5,1,1.5, 1,1,1, 1.5,1,1.5)
737        0
738
739
740    It is an error to have arguments that are not integer or half
741    integer values or do not fulfil the triangle relation:
742
743        sage: Wigner9j(0.5,0.5,0.5, 0.5,0.5,0.5, 0.5,0.5,0.5,prec=64)
744        Traceback (most recent call last):
745        ...
746        ValueError: j values must be integer or half integer and fulfil the triangle relation
747
748        sage: Wigner9j(1,1,1, 0.5,1,1.5, 0.5,1,2.5,prec=64)
749        Traceback (most recent call last):
750        ...
751        ValueError: j values must be integer or half integer and fulfil the triangle relation
752
753
754    REFERENCES:
755
756    - [Edmonds74] 'Angular Momentum in Quantum Mechanics',
757      A. R. Edmonds, Princeton University Press (1974)
758
759    - [Rasch03] 'Efficient Storage Scheme for Pre-calculated Wigner 3j,
760      6j and Gaunt Coefficients', J. Rasch and A. C. H. Yu, SIAM
761      J. Sci. Comput. Volume 25, Issue 4, pp. 1416-1428 (2003)
762
763    """
764
765    imin=0
766    imax=min(j_1+j_9,j_2+j_6,j_4+j_8)
767
768    sumres=0
769    for kk in range(imin,imax+1):
770        sumres=sumres+(2*kk+1)*Racah(j_1,j_2,j_9,j_6,j_3,kk,prec)\
771                *Racah(j_4,j_6,j_8,j_2,j_5,kk,prec)\
772                *Racah(j_1,j_4,j_9,j_8,j_7,kk,prec)
773    return sumres
774       
775
776
777
778def Gaunt(l_1,l_2,l_3,m_1,m_2,m_3,prec=None):
779    r"""
780    Calculate the Gaunt coefficient which is defined as the integral
781    over three spherical harmonics:
782
783    \begin{eqnarray}
784    Y{j_1 \atop m_1} {j_2 \atop m_2} {j_3 \atop m_3}
785    &=&
786    \int Y_{l_1,m_1}(\Omega)
787    Y_{l_2,m_2}(\Omega) Y_{l_3,m_3}(\Omega)\; d\Omega \\ 
788    &=&
789    \sqrt{\frac{(2l_1+1)(2l_2+1)(2l_3+1)}{4\pi}}
790    \left({l_1 \atop 0  } {l_2 \atop 0  } {l_3 \atop   0} \right)
791    \left({l_1 \atop m_1} {l_2 \atop m_2} {l_3 \atop m_3} \right)
792    \end{eqnarray}
793
794
795    NOTES:
796
797    The Gaunt coefficient obeys the following symmetry rules:
798
799    - invariant under any permutation of the columns
800      \begin{eqnarray}
801      Y{j_1 \atop m_1} {j_2 \atop m_2} {j_3 \atop m_3}
802      &=&
803       Y{j_3 \atop m_3} {j_1 \atop m_1} {j_2 \atop m_2}
804      =Y{j_2 \atop m_2} {j_3 \atop m_3} {j_1 \atop m_1}
805      \qquad \mbox{cyclic} \\
806      &=&
807       Y{j_3 \atop m_3} {j_2 \atop m_2} {j_1 \atop m_1}
808      =Y{j_1 \atop m_1} {j_3 \atop m_3} {j_2 \atop m_2}
809      =Y{j_2 \atop m_2} {j_1 \atop m_1} {j_3 \atop m_3}
810      \qquad\mbox{anti-cyclic} 
811      \end{eqnarray}
812     
813    - invariant under space inflection, i.e.
814      \begin{eqnarray}
815      Y{j_1 \atop m_1} {j_2 \atop m_2} {j_3 \atop m_3}
816      =
817      Y{j_1 \atop -m_1} {j_2 \atop -m_2} {j_3 \atop -m_3}
818      \end{eqnarray}
819     
820    - symmetric with respect to the 72 Regge symmetries as inherited
821      for the $3j$ symbols [Regge58]
822 
823    - zero for $l_1$, $l_2$, $l_3$ not fulfilling triangle relation
824 
825    - zero for violating any one of the conditions: $l_1\ge|m_1|$,
826      $l_2\ge|m_2|$, $l_3\ge|m_3|
827   
828    - non-zero only for an even sum of the $l_i$, i. e.
829      $J=l_1+l_2+l_3=2n$ for $n$ in $\mathbf{N}$
830
831
832    ALGORITHM:
833
834    This function uses the algorithm of [Liberatodebrito82] to
835    calculate the value of the Gaunt coefficient exactly. Note that
836    the formula contains alternating sums over large factorials and is
837    therefore unsuitable for finite precision arithmetic and only
838    useful for a computer algebra system [Rasch03].
839
840
841    INPUT:
842
843    j_1 - integer
844    j_2 - integer
845    j_3 - integer
846    m_1 - integer
847    m_2 - integer
848    m_3 - integer
849    prec - precision, default: None. Providing a precision can
850           drastically speed up the calculation.
851
852
853    OUTPUT:
854
855    The result will be a rational number times the square root of a
856    rational number, unless a precision is given.
857
858
859    EXAMPLES:
860
861        sage: Gaunt(1,0,1,1,0,-1)
862        -1/2/sqrt(pi)
863
864        sage: Gaunt(1,0,1,1,0,0)
865        0
866
867        sage: Gaunt(29,29,34,10,-5,-5)
868        1821867940156/215552371055153321*sqrt(22134)/sqrt(pi)
869
870        sage: Gaunt(20,20,40,1,-1,0)
871        28384503878959800/74029560764440771/sqrt(pi)
872
873        sage: Gaunt(12,15,5,2,3,-5)
874        91/124062*sqrt(36890)/sqrt(pi)
875
876        sage: Gaunt(10,10,12,9,3,-12)
877        -98/62031*sqrt(6279)/sqrt(pi)
878
879        sage: Gaunt(1000,1000,1200,9,3,-12).n(64)
880        0.00689500421922113448
881
882
883    It is an error to use non-integer values for $l$ and $m$:
884
885        sage: Gaunt(1.2,0,1.2,0,0,0)
886        Traceback (most recent call last):
887        ...
888        ValueError: l values must be integer
889
890        sage: Gaunt(1,0,1,1.1,0,-1.1)
891        Traceback (most recent call last):
892        ...
893        ValueError: m values must be integer
894
895
896
897    REFERENCES:
898
899    - [Regge58] 'Symmetry Properties of Clebsch-Gordan Coefficients',
900      T. Regge, Nuovo Cimento, Volume 10, pp. 544 (1958)
901
902    - [Liberatodebrito82] 'FORTRAN program for the integral of three
903      spherical harmonics', A. Liberato de Brito,
904      Comput. Phys. Commun., Volume 25, pp. 81-85 (1982)
905
906    - [Rasch03] 'Efficient Storage Scheme for Pre-calculated Wigner 3j,
907      6j and Gaunt Coefficients', J. Rasch and A. C. H. Yu, SIAM
908      J. Sci. Comput. Volume 25, Issue 4, pp. 1416-1428 (2003)
909
910
911    AUTHORS:
912
913    - Jens Rasch (2009-03-24): initial version for Sage
914
915    """
916
917    if int(l_1)!=l_1 or int(l_2)!=l_2 or int(l_3)!=l_3:
918        raise ValueError("l values must be integer")
919        #return 0
920    if int(m_1)!=m_1 or int(m_2)!=m_2 or int(m_3)!=m_3:
921        raise ValueError("m values must be integer")
922   
923    bigL=(l_1+l_2+l_3)/2
924    a1=l_1+l_2-l_3
925    if (a1<0):
926        return 0
927    a2=l_1-l_2+l_3
928    if (a2<0):
929        return 0
930    a3=-l_1+l_2+l_3
931    if (a3<0):
932        return 0
933    if Mod(2*bigL,2)<>0:
934#     if int(int(2*bigL)/2)<>int(bigL):
935        return 0
936    if (m_1+m_2+m_3)<>0:
937        return 0
938    if (abs(m_1)>l_1) or (abs(m_2)>l_2) or (abs(m_3)>l_3):
939        return 0
940
941    imin=max(-l_3+l_1+m_2,-l_3+l_2-m_1,0)
942    imax=min(l_2+m_2,l_1-m_1,l_1+l_2-l_3)
943
944    maxfact=max(l_1+l_2+l_3+1,imax+1)
945    _calc_factlist(maxfact)
946
947    argsqrt=(2*l_1+1)*(2*l_2+1)*(2*l_3+1)*_Factlist[l_1-m_1]\
948             *_Factlist[l_1+m_1]*_Factlist[l_2-m_2]*_Factlist[l_2+m_2]\
949             *_Factlist[l_3-m_3]*_Factlist[l_3+m_3]/(4*pi)
950    ressqrt=sqrt(argsqrt)          # sqrt(argsqrt,prec)
951    #if type(ressqrt)==sage.rings.complex_number.ComplexNumber:
952    #    ressqrt=ressqrt.real()
953    prefac=Integer(_Factlist[bigL]*_Factlist[l_2-l_1+l_3]\
954        *_Factlist[l_1-l_2+l_3]\
955        *_Factlist[l_1+l_2-l_3])/_Factlist[2*bigL+1]\
956        /(_Factlist[bigL-l_1]*_Factlist[bigL-l_2]*_Factlist[bigL-l_3])
957 
958    sumres=0
959    for ii in range(imin,imax+1):
960        den=_Factlist[ii]*_Factlist[ii+l_3-l_1-m_2]\
961             *_Factlist[l_2+m_2-ii]*_Factlist[l_1-ii-m_1]\
962             *_Factlist[ii+l_3-l_2+m_1]*_Factlist[l_1+l_2-l_3-ii]
963        sumres=sumres+Integer((-1)**ii)/den
964
965    res=ressqrt*prefac*sumres*(-1)**(bigL+l_3+m_1-m_2)
966    if prec!=None:
967        res=res.n(prec)
968    return res
969