Ticket #5996: trac_5996.patch

File trac_5996.patch, 29.3 KB (added by AlexGhitza, 10 months ago)
  • sage/functions/all.py

    # HG changeset patch
    # User Jens Rasch <jyr2000@gmail.com>
    # Date 1243853619 -36000
    # Node ID 8e99750f6fc707f29f5fb64867e14cbad159abe0
    # Parent  fd89fee664d87b20ab77bb6e248d9a71c17affe4
    trac 5996: Wigner, Clebsch-Gordan, Racah, and Gaunt coefficients
    
    diff -r fd89fee664d8 -r 8e99750f6fc7 sage/functions/all.py
    a b  
    5656from spike_function import spike_function 
    5757 
    5858from prime_pi import prime_pi 
     59 
     60from wigner import (Wigner3j, ClebschGordan, Racah, Wigner6j, 
     61                    Wigner9j, Gaunt) 
  • (a) /dev/null vs. (b) b/sage/functions/wigner.py

    diff -r fd89fee664d8 -r 8e99750f6fc7 sage/functions/wigner.py
    a b  
     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.symbolic.constants import pi 
     36from sage.rings.integer import Integer 
     37from sage.rings.complex_number import ComplexNumber 
     38from sage.rings.integer_mod import Mod 
     39 
     40# This list of precomputed factorials is needed to massively 
     41# accelerate future calculations of the various coefficients 
     42_Factlist=[1] 
     43 
     44def _calc_factlist(nn): 
     45    if nn>=len(_Factlist): 
     46        for ii in range(len(_Factlist),nn+1): 
     47            _Factlist.append(_Factlist[ii-1]*ii) 
     48            #_Factlist.append(factorial(ii)) 
     49    #return _Factlist 
     50 
     51 
     52def test_calc_factlist(nn): 
     53    r""" 
     54    Function calculates a list of precomputed factorials in order to 
     55    massively accelerate future calculations of the various 
     56    coefficients. 
     57 
     58    INPUT: 
     59 
     60     - nn Highest factorial to be computed 
     61 
     62    OUTPUT: 
     63 
     64    integer list of factorials 
     65 
     66 
     67    EXAMPLES: 
     68 
     69    Calculate list of factorials: 
     70 
     71        sage: from sage.functions.wigner import test_calc_factlist 
     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=argsqrt.sqrt(prec) 
     244    if type(ressqrt) is 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))*(2*j_3+1).sqrt(prec)\ 
     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=argsqrt.sqrt(prec) 
     382    if type(ressqrt) is 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: from sage.functions.wigner import test_bigDeltacoeff 
     411        sage: test_bigDeltacoeff(1,1,1) 
     412        1/2*sqrt(1/6) 
     413 
     414    """ 
     415 
     416    return _bigDeltacoeff(aa,bb,cc,prec) 
     417   
     418 
     419 
     420 
     421def Racah(aa,bb,cc,dd,ee,ff,prec=None): 
     422    r""" 
     423    Calculate the Racah symbol 
     424 
     425    W(a,b,c,d;e,f) 
     426 
     427    NOTES: 
     428 
     429    The Racah symbol is related to the Wigner 6j symbol: 
     430    \begin{eqnarray} 
     431    \left({j_1 \atop j_4} {j_2 \atop j_5} {j_3 \atop j_6} \right) 
     432    = 
     433    (-1)^{j_1+j_2+j_4+j_5} W(j_1,j_2,j_5,j_4,j_3,j_6) 
     434    \end{eqnarray} 
     435 
     436    Please see the 6j symbol for its much richer symmetries and for 
     437    additional properties. 
     438 
     439 
     440    ALGORITHM: 
     441 
     442    This function uses the algorithm of [Edmonds74] to calculate the 
     443    value of the 6j symbol exactly. Note that the formula contains 
     444    alternating sums over large factorials and is therefore unsuitable 
     445    for finite precision arithmetic and only useful for a computer 
     446    algebra system [Rasch03]. 
     447 
     448 
     449    INPUT: 
     450 
     451    a - integer or half integer 
     452    b - integer or half integer 
     453    c - integer or half integer 
     454    d - integer or half integer 
     455    e - integer or half integer 
     456    f - integer or half integer 
     457    prec - precision, default: None. Providing a precision can 
     458           drastically speed up the calculation. 
     459 
     460 
     461    OUTPUT: 
     462 
     463    The result will be a rational number times the square root of a 
     464    rational number, unless a precision is given. 
     465 
     466 
     467    EXAMPLES: 
     468 
     469    A couple of examples and test cases: 
     470 
     471        sage: Racah(3,3,3,3,3,3) 
     472        -1/14 
     473     
     474 
     475    REFERENCES: 
     476 
     477    - [Edmonds74] 'Angular Momentum in Quantum Mechanics', 
     478      A. R. Edmonds, Princeton University Press (1974) 
     479 
     480    - [Rasch03] 'Efficient Storage Scheme for Pre-calculated Wigner 3j, 
     481      6j and Gaunt Coefficients', J. Rasch and A. C. H. Yu, SIAM 
     482      J. Sci. Comput. Volume 25, Issue 4, pp. 1416-1428 (2003) 
     483 
     484 
     485    AUTHORS: 
     486 
     487    - Jens Rasch (2009-03-24): initial version 
     488 
     489    """ 
     490 
     491    prefac=_bigDeltacoeff(aa,bb,ee,prec)*_bigDeltacoeff(cc,dd,ee,prec)\ 
     492            *_bigDeltacoeff(aa,cc,ff,prec)*_bigDeltacoeff(bb,dd,ff,prec) 
     493    if prefac==0: 
     494        return 0 
     495    imin=max(aa+bb+ee,cc+dd+ee,aa+cc+ff,bb+dd+ff) 
     496    imax=min(aa+bb+cc+dd,aa+dd+ee+ff,bb+cc+ee+ff) 
     497 
     498    maxfact=max(imax+1,aa+bb+cc+dd,aa+dd+ee+ff,bb+cc+ee+ff) 
     499    _calc_factlist(maxfact) 
     500 
     501    sumres=0 
     502    for kk in range(imin,imax+1): 
     503        den=_Factlist[int(kk-aa-bb-ee)]*_Factlist[int(kk-cc-dd-ee)]\ 
     504            *_Factlist[int(kk-aa-cc-ff)]\ 
     505            *_Factlist[int(kk-bb-dd-ff)]*_Factlist[int(aa+bb+cc+dd-kk)]\ 
     506            *_Factlist[int(aa+dd+ee+ff-kk)]\ 
     507            *_Factlist[int(bb+cc+ee+ff-kk)] 
     508        sumres=sumres+Integer((-1)**kk*_Factlist[kk+1])/den 
     509         
     510    res=prefac*sumres*(-1)**(int(aa+bb+cc+dd)) 
     511    return res 
     512 
     513 
     514 
     515 
     516 
     517def Wigner6j(j_1,j_2,j_3,j_4,j_5,j_6,prec=None): 
     518    r""" 
     519    Calculate the Wigner 6j symbol 
     520 
     521    \left({j_1 \atop j_4} {j_2 \atop j_5} {j_3 \atop j_6} \right) 
     522 
     523 
     524    NOTES: 
     525 
     526    The Wigner 6j symbol is related to the Racah symbol but exhibits 
     527    more symmetries as detailed below. 
     528    \begin{eqnarray} 
     529    \left({j_1 \atop j_4} {j_2 \atop j_5} {j_3 \atop j_6} \right) 
     530    = 
     531    (-1)^{j_1+j_2+j_4+j_5} W(j_1,j_2,j_5,j_4,j_3,j_6) 
     532    \end{eqnarray} 
     533 
     534    The Wigner 6j symbol obeys the following symmetry rules: 
     535 
     536    - Wigner $6j$ symbols are left invariant under any permutation of 
     537      the columns: 
     538      \begin{eqnarray} 
     539      \left({j_1 \atop j_4} {j_2 \atop j_5} {j_3 \atop j_6} \right) 
     540      &=& 
     541       \left({j_3 \atop j_6} {j_1 \atop j_4} {j_2 \atop j_5} \right) 
     542      =\left({j_2 \atop j_5} {j_3 \atop j_6} {j_2 \atop j_4} \right) 
     543      \qquad \mbox{cyclic} \\ 
     544      &=& 
     545       \left({j_3 \atop j_6} {j_2 \atop j_5} {j_1 \atop j_4} \right) 
     546      =\left({j_1 \atop j_4} {j_3 \atop j_6} {j_2 \atop j_5} \right) 
     547      =\left({j_2 \atop j_5} {j_1 \atop j_4} {j_3 \atop j_6} \right) 
     548      \qquad \mbox{anti-cyclic}  
     549      \end{eqnarray} 
     550 
     551    - They are invariant under the exchange of the upper and lower 
     552      arguments in each of any two columns, i. e. 
     553      \begin{eqnarray} 
     554      \left({j_1 \atop j_4} {j_2 \atop j_5} {j_3 \atop j_6} \right) 
     555      = 
     556      \left({j_1 \atop j_4} {j_5 \atop j_2} {j_6 \atop j_3} \right) 
     557      = 
     558      \left({j_4 \atop j_1} {j_2 \atop j_5} {j_6 \atop j_3} \right) 
     559      = 
     560      \left({j_4 \atop j_1} {j_5 \atop j_2} {j_3 \atop j_6} \right) 
     561      \end{eqnarray} 
     562     
     563    - additional 6 symmetries [Regge59] giving rise to 144 symmetries 
     564      in total 
     565 
     566    - only non-zero if any triple of $j$'s fulfil a triangle relation 
     567 
     568 
     569    ALGORITHM: 
     570 
     571    This function uses the algorithm of [Edmonds74] to calculate the 
     572    value of the 6j symbol exactly. Note that the formula contains 
     573    alternating sums over large factorials and is therefore unsuitable 
     574    for finite precision arithmetic and only useful for a computer 
     575    algebra system [Rasch03]. 
     576 
     577 
     578    INPUT: 
     579 
     580    j_1 - integer or half integer 
     581    j_2 - integer or half integer 
     582    j_3 - integer or half integer 
     583    j_4 - integer or half integer 
     584    j_5 - integer or half integer 
     585    j_6 - integer or half integer 
     586    prec - precision, default: None. Providing a precision can 
     587           drastically speed up the calculation. 
     588 
     589 
     590    OUTPUT: 
     591 
     592    The result will be a rational number times the square root of a 
     593    rational number, unless a precision is given. 
     594 
     595 
     596    EXAMPLES: 
     597 
     598    A couple of examples and test cases: 
     599 
     600        sage: Wigner6j(3,3,3,3,3,3) 
     601        -1/14 
     602     
     603        sage: Wigner6j(5,5,5,5,5,5) 
     604        1/52 
     605 
     606        sage: Wigner6j(6,6,6,6,6,6) 
     607        309/10868 
     608         
     609        sage: Wigner6j(8,8,8,8,8,8) 
     610        -12219/965770 
     611 
     612        sage: Wigner6j(30,30,30,30,30,30) 
     613        36082186869033479581/87954851694828981714124 
     614         
     615        sage: Wigner6j(0.5,0.5,1,0.5,0.5,1) 
     616        1/6 
     617 
     618        sage: Wigner6j(200,200,200,200,200,200, prec=1000)*1.0 
     619        0.000155903212413242 
     620 
     621 
     622    It is an error to have arguments that are not integer or half 
     623    integer values or do not fulfil the triangle relation: 
     624 
     625        sage: Wigner6j(2.5,2.5,2.5,2.5,2.5,2.5) 
     626        Traceback (most recent call last): 
     627        ... 
     628        ValueError: j values must be integer or half integer and fulfil the triangle relation 
     629 
     630        sage: Wigner6j(0.5,0.5,1.1,0.5,0.5,1.1) 
     631        Traceback (most recent call last): 
     632        ... 
     633        ValueError: j values must be integer or half integer and fulfil the triangle relation 
     634 
     635 
     636    REFERENCES: 
     637 
     638    - [Regge59] 'Symmetry Properties of Racah Coefficients', 
     639      T. Regge, Nuovo Cimento, Volume 11, pp. 116 (1959) 
     640 
     641    - [Edmonds74] 'Angular Momentum in Quantum Mechanics', 
     642      A. R. Edmonds, Princeton University Press (1974) 
     643 
     644    - [Rasch03] 'Efficient Storage Scheme for Pre-calculated Wigner 3j, 
     645      6j and Gaunt Coefficients', J. Rasch and A. C. H. Yu, SIAM 
     646      J. Sci. Comput. Volume 25, Issue 4, pp. 1416-1428 (2003) 
     647 
     648    """ 
     649 
     650    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) 
     651    return res 
     652 
     653 
     654 
     655 
     656def Wigner9j(j_1,j_2,j_3,j_4,j_5,j_6,j_7,j_8,j_9,prec=None): 
     657    r""" 
     658    Calculate the Wigner 9j symbol 
     659 
     660    \left\{ 
     661      \begin{matrix} 
     662        {j_1} & {j_2} & {j_3} \\ 
     663        {j_4} & {j_5} & {j_6} \\ 
     664        {j_7} & {j_8} & {j_9}  
     665      \end{matrix}  
     666    \right\} 
     667 
     668 
     669    ALGORITHM: 
     670 
     671    This function uses the algorithm of [Edmonds74] to calculate the 
     672    value of the 3j symbol exactly. Note that the formula contains 
     673    alternating sums over large factorials and is therefore unsuitable 
     674    for finite precision arithmetic and only useful for a computer 
     675    algebra system [Rasch03]. 
     676 
     677 
     678    INPUT: 
     679 
     680    j_1 - integer or half integer 
     681    j_2 - integer or half integer 
     682    j_3 - integer or half integer 
     683    j_4 - integer or half integer 
     684    j_5 - integer or half integer 
     685    j_6 - integer or half integer 
     686    j_7 - integer or half integer 
     687    j_8 - integer or half integer 
     688    j_9 - integer or half integer 
     689    prec - precision, default: None. Providing a precision can 
     690           drastically speed up the calculation. 
     691 
     692 
     693    OUTPUT: 
     694 
     695    The result will be a rational number times the square root of a 
     696    rational number, unless a precision is given. 
     697 
     698 
     699    EXAMPLES: 
     700 
     701    A couple of examples and test cases, note that for speed reasons a 
     702    precision is given: 
     703 
     704        sage: Wigner9j(1,1,1, 1,1,1, 1,1,0 ,prec=64) # ==1/18 
     705        0.0555555555555555555 
     706 
     707        sage: Wigner9j(1,1,1, 1,1,1, 1,1,1) 
     708        0 
     709 
     710        sage: Wigner9j(1,1,1, 1,1,1, 1,1,2 ,prec=64) # ==1/18 
     711        0.0555555555555555556 
     712 
     713        sage: Wigner9j(1,2,1, 2,2,2, 1,2,1 ,prec=64) # ==-1/150 
     714        -0.00666666666666666667 
     715 
     716        sage: Wigner9j(3,3,2, 2,2,2, 3,3,2 ,prec=64) # ==157/14700 
     717        0.0106802721088435374 
     718 
     719        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)) 
     720        0.00944247746651111739 
     721 
     722        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)) 
     723        0.0110216678544351364 
     724 
     725        sage: Wigner9j(100,80,50, 50,100,70, 60,50,100 ,prec=1000)*1.0 
     726        1.05597798065761e-7 
     727 
     728        sage: Wigner9j(30,30,10, 30.5,30.5,20, 30.5,30.5,10 ,prec=1000)*1.0 # ==(80944680186359968990/95103769817469)*sqrt(1/682288158959699477295) 
     729        0.0000325841699408828 
     730 
     731        sage: Wigner9j(64,62.5,114.5, 61.5,61,112.5, 113.5,110.5,60, prec=1000)*1.0 
     732        -3.41407910055520e-39 
     733 
     734        sage: Wigner9j(15,15,15, 15,3,15, 15,18,10, prec=1000)*1.0 
     735        -0.0000778324615309539 
     736 
     737        sage: Wigner9j(1.5,1,1.5, 1,1,1, 1.5,1,1.5) 
     738        0 
     739 
     740 
     741    It is an error to have arguments that are not integer or half 
     742    integer values or do not fulfil the triangle relation: 
     743 
     744        sage: Wigner9j(0.5,0.5,0.5, 0.5,0.5,0.5, 0.5,0.5,0.5,prec=64) 
     745        Traceback (most recent call last): 
     746        ... 
     747        ValueError: j values must be integer or half integer and fulfil the triangle relation 
     748 
     749        sage: Wigner9j(1,1,1, 0.5,1,1.5, 0.5,1,2.5,prec=64) 
     750        Traceback (most recent call last): 
     751        ... 
     752        ValueError: j values must be integer or half integer and fulfil the triangle relation 
     753 
     754 
     755    REFERENCES: 
     756 
     757    - [Edmonds74] 'Angular Momentum in Quantum Mechanics', 
     758      A. R. Edmonds, Princeton University Press (1974) 
     759 
     760    - [Rasch03] 'Efficient Storage Scheme for Pre-calculated Wigner 3j, 
     761      6j and Gaunt Coefficients', J. Rasch and A. C. H. Yu, SIAM 
     762      J. Sci. Comput. Volume 25, Issue 4, pp. 1416-1428 (2003) 
     763 
     764    """ 
     765 
     766    imin=0 
     767    imax=min(j_1+j_9,j_2+j_6,j_4+j_8) 
     768 
     769    sumres=0 
     770    for kk in range(imin,imax+1): 
     771        sumres=sumres+(2*kk+1)*Racah(j_1,j_2,j_9,j_6,j_3,kk,prec)\ 
     772                *Racah(j_4,j_6,j_8,j_2,j_5,kk,prec)\ 
     773                *Racah(j_1,j_4,j_9,j_8,j_7,kk,prec) 
     774    return sumres 
     775         
     776 
     777 
     778 
     779def Gaunt(l_1,l_2,l_3,m_1,m_2,m_3,prec=None): 
     780    r""" 
     781    Calculate the Gaunt coefficient which is defined as the integral 
     782    over three spherical harmonics: 
     783 
     784    \begin{eqnarray} 
     785    Y{j_1 \atop m_1} {j_2 \atop m_2} {j_3 \atop m_3}  
     786    &=&  
     787    \int Y_{l_1,m_1}(\Omega) 
     788    Y_{l_2,m_2}(\Omega) Y_{l_3,m_3}(\Omega)\; d\Omega \\   
     789    &=& 
     790    \sqrt{\frac{(2l_1+1)(2l_2+1)(2l_3+1)}{4\pi}} 
     791    \left({l_1 \atop 0  } {l_2 \atop 0  } {l_3 \atop   0} \right) 
     792    \left({l_1 \atop m_1} {l_2 \atop m_2} {l_3 \atop m_3} \right) 
     793    \end{eqnarray} 
     794 
     795 
     796    NOTES: 
     797 
     798    The Gaunt coefficient obeys the following symmetry rules: 
     799 
     800    - invariant under any permutation of the columns 
     801      \begin{eqnarray} 
     802      Y{j_1 \atop m_1} {j_2 \atop m_2} {j_3 \atop m_3}  
     803      &=& 
     804       Y{j_3 \atop m_3} {j_1 \atop m_1} {j_2 \atop m_2}  
     805      =Y{j_2 \atop m_2} {j_3 \atop m_3} {j_1 \atop m_1}  
     806      \qquad \mbox{cyclic} \\ 
     807      &=& 
     808       Y{j_3 \atop m_3} {j_2 \atop m_2} {j_1 \atop m_1}  
     809      =Y{j_1 \atop m_1} {j_3 \atop m_3} {j_2 \atop m_2}  
     810      =Y{j_2 \atop m_2} {j_1 \atop m_1} {j_3 \atop m_3}  
     811      \qquad\mbox{anti-cyclic}   
     812      \end{eqnarray} 
     813       
     814    - invariant under space inflection, i.e. 
     815      \begin{eqnarray} 
     816      Y{j_1 \atop m_1} {j_2 \atop m_2} {j_3 \atop m_3}  
     817      = 
     818      Y{j_1 \atop -m_1} {j_2 \atop -m_2} {j_3 \atop -m_3}  
     819      \end{eqnarray} 
     820       
     821    - symmetric with respect to the 72 Regge symmetries as inherited 
     822      for the $3j$ symbols [Regge58] 
     823   
     824    - zero for $l_1$, $l_2$, $l_3$ not fulfilling triangle relation 
     825   
     826    - zero for violating any one of the conditions: $l_1\ge|m_1|$, 
     827      $l_2\ge|m_2|$, $l_3\ge|m_3| 
     828     
     829    - non-zero only for an even sum of the $l_i$, i. e. 
     830      $J=l_1+l_2+l_3=2n$ for $n$ in $\mathbf{N}$ 
     831 
     832 
     833    ALGORITHM: 
     834 
     835    This function uses the algorithm of [Liberatodebrito82] to 
     836    calculate the value of the Gaunt coefficient exactly. Note that 
     837    the formula contains alternating sums over large factorials and is 
     838    therefore unsuitable for finite precision arithmetic and only 
     839    useful for a computer algebra system [Rasch03]. 
     840 
     841 
     842    INPUT: 
     843 
     844    j_1 - integer 
     845    j_2 - integer 
     846    j_3 - integer 
     847    m_1 - integer 
     848    m_2 - integer 
     849    m_3 - integer 
     850    prec - precision, default: None. Providing a precision can 
     851           drastically speed up the calculation. 
     852 
     853 
     854    OUTPUT: 
     855 
     856    The result will be a rational number times the square root of a 
     857    rational number, unless a precision is given. 
     858 
     859 
     860    EXAMPLES: 
     861 
     862        sage: Gaunt(1,0,1,1,0,-1) 
     863        -1/2/sqrt(pi) 
     864 
     865        sage: Gaunt(1,0,1,1,0,0) 
     866        0 
     867 
     868        sage: Gaunt(29,29,34,10,-5,-5) 
     869        1821867940156/215552371055153321*sqrt(22134)/sqrt(pi) 
     870 
     871        sage: Gaunt(20,20,40,1,-1,0) 
     872        28384503878959800/74029560764440771/sqrt(pi) 
     873 
     874        sage: Gaunt(12,15,5,2,3,-5) 
     875        91/124062*sqrt(36890)/sqrt(pi) 
     876 
     877        sage: Gaunt(10,10,12,9,3,-12) 
     878        -98/62031*sqrt(6279)/sqrt(pi) 
     879 
     880        sage: Gaunt(1000,1000,1200,9,3,-12).n(64) 
     881        0.00689500421922113448 
     882 
     883 
     884    It is an error to use non-integer values for $l$ and $m$: 
     885 
     886        sage: Gaunt(1.2,0,1.2,0,0,0) 
     887        Traceback (most recent call last): 
     888        ... 
     889        ValueError: l values must be integer 
     890 
     891        sage: Gaunt(1,0,1,1.1,0,-1.1) 
     892        Traceback (most recent call last): 
     893        ... 
     894        ValueError: m values must be integer 
     895 
     896 
     897 
     898    REFERENCES: 
     899 
     900    - [Regge58] 'Symmetry Properties of Clebsch-Gordan Coefficients', 
     901      T. Regge, Nuovo Cimento, Volume 10, pp. 544 (1958) 
     902 
     903    - [Liberatodebrito82] 'FORTRAN program for the integral of three 
     904      spherical harmonics', A. Liberato de Brito, 
     905      Comput. Phys. Commun., Volume 25, pp. 81-85 (1982) 
     906 
     907    - [Rasch03] 'Efficient Storage Scheme for Pre-calculated Wigner 3j, 
     908      6j and Gaunt Coefficients', J. Rasch and A. C. H. Yu, SIAM 
     909      J. Sci. Comput. Volume 25, Issue 4, pp. 1416-1428 (2003) 
     910 
     911 
     912    AUTHORS: 
     913 
     914    - Jens Rasch (2009-03-24): initial version for Sage 
     915 
     916    """ 
     917 
     918    if int(l_1)!=l_1 or int(l_2)!=l_2 or int(l_3)!=l_3: 
     919        raise ValueError("l values must be integer") 
     920        #return 0 
     921    if int(m_1)!=m_1 or int(m_2)!=m_2 or int(m_3)!=m_3: 
     922        raise ValueError("m values must be integer") 
     923     
     924    bigL=(l_1+l_2+l_3)/2 
     925    a1=l_1+l_2-l_3 
     926    if (a1<0): 
     927        return 0 
     928    a2=l_1-l_2+l_3 
     929    if (a2<0): 
     930        return 0 
     931    a3=-l_1+l_2+l_3 
     932    if (a3<0): 
     933        return 0 
     934    if Mod(2*bigL,2)<>0: 
     935#     if int(int(2*bigL)/2)<>int(bigL): 
     936        return 0 
     937    if (m_1+m_2+m_3)<>0: 
     938        return 0 
     939    if (abs(m_1)>l_1) or (abs(m_2)>l_2) or (abs(m_3)>l_3): 
     940        return 0 
     941 
     942    imin=max(-l_3+l_1+m_2,-l_3+l_2-m_1,0) 
     943    imax=min(l_2+m_2,l_1-m_1,l_1+l_2-l_3) 
     944 
     945    maxfact=max(l_1+l_2+l_3+1,imax+1) 
     946    _calc_factlist(maxfact) 
     947 
     948    argsqrt=(2*l_1+1)*(2*l_2+1)*(2*l_3+1)*_Factlist[l_1-m_1]\ 
     949             *_Factlist[l_1+m_1]*_Factlist[l_2-m_2]*_Factlist[l_2+m_2]\ 
     950             *_Factlist[l_3-m_3]*_Factlist[l_3+m_3]/(4*pi) 
     951    ressqrt=argsqrt.sqrt()          # sqrt(argsqrt,prec) 
     952    #if type(ressqrt) is ComplexNumber: 
     953    #    ressqrt=ressqrt.real() 
     954    prefac=Integer(_Factlist[bigL]*_Factlist[l_2-l_1+l_3]\ 
     955        *_Factlist[l_1-l_2+l_3]\ 
     956        *_Factlist[l_1+l_2-l_3])/_Factlist[2*bigL+1]\ 
     957        /(_Factlist[bigL-l_1]*_Factlist[bigL-l_2]*_Factlist[bigL-l_3]) 
     958   
     959    sumres=0 
     960    for ii in range(imin,imax+1): 
     961        den=_Factlist[ii]*_Factlist[ii+l_3-l_1-m_2]\ 
     962             *_Factlist[l_2+m_2-ii]*_Factlist[l_1-ii-m_1]\ 
     963             *_Factlist[ii+l_3-l_2+m_1]*_Factlist[l_1+l_2-l_3-ii] 
     964        sumres=sumres+Integer((-1)**ii)/den 
     965 
     966    res=ressqrt*prefac*sumres*(-1)**(bigL+l_3+m_1-m_2) 
     967    if prec!=None: 
     968        res=res.n(prec) 
     969    return res 
     970   
  • sage/numerical/all.py

    diff -r fd89fee664d8 -r 8e99750f6fc7 sage/numerical/all.py
    a b  
    1 from optimize import (find_root, find_maximum_on_interval, find_minimum_on_interval,minimize,minimize_constrained, linear_program, find_fit) 
     1from optimize import (find_root, find_maximum_on_interval, 
     2                      find_minimum_on_interval,minimize,minimize_constrained, 
     3                      linear_program, find_fit)