               /** Racah coefficients, 6-j and 9-j symbols **/

/*K: angular momenta; 6-j symbols; 9-j symbols; racah coefficients */           
/*A: Jerome Gauntlett
/*S: University of Western Australia */
/*D: September 1985 */

/* Procedures in this file calculate 6-j symbols and Racah coefficients
     using recursion formulae. Values for W and Ninej functions are stored
     as they are calculated. */

/* It may be useful to apply a power simplification routine after the 
     calculation of the 6-j and 9-j coefficients. This is not incorporated
     in this file due the generally slow operation of these routines. */

/*: W[$a,$b,$c,$d,$e,$f] are the Wigner (or Racah) coefficients. */
/*R: L.C. Biedenharn and J.D. Louck: Angular Momentum in Quantum Mechanics */
/*W: Perhaps this name should be changed to Rac */

W_:Tier

Tri[$a,$b,$e] :: P[Abs[$a-$b]<=$e&$e<=($a+$b)&Abs[$b-$e]<=$a&$a<=($b+$e)& \
   	           Abs[$e-$a]<=$b&$b<=($e+$a)]

                  /* Recursion formula, AMQP 3.299 */

W[$a,$b,$c,$d_=$d>1/2,$e,$f_=P[$d=Sort[{$a,$b,$c,$d,$e,$f}][1]]] :: \
W[$a,$b,$c,$d,$e,$f] :  (\
  (sqrt[($b+$d-$f)($b+$f-$d+1)($d+$e-$c)($c+$e-$d+1)($c+$f-$a+1)($a+$c+$f+2)] \ 
    W[$a,$b,$c+1/2,$d-1/2,$e,$f+1/2] \
  -sqrt[($b+$d-$f)($b+$f-$d+1)($c+$d-$e)($c+$d+$e+1)($a+$c-$f)($a+$f-$c+1)] \  
    W[$a,$b,$c-1/2,$d-1/2,$e,$f+1/2] \
  +sqrt[($d+$f-$b)($b+$f+$d+1)($c+$d-$e)($c+$d+$e+1)($c+$f-$a)($a+$c+$f+1)] \  
    W[$a,$b,$c-1/2,$d-1/2,$e,$f-1/2] \
  +sqrt[($d+$f-$b)($b+$f+$d+1)($d+$e-$c)($c+$e-$d+1)($a+$f-$c)($a+$c-$f+1)] \  
    W[$a,$b,$c+1/2,$d-1/2,$e,$f-1/2])/((2$c+1)(2$f+1)2 $d);Ex[%%])

          /* Special values derived from AMQP 3.293, 3.278 */

W[$a,$b,$c,1/2,$e,$f] : 0
W[$a,$b,$c,0,$e,$f] :0
W[$a,$b,$d_=$d=$c+1/2,1/2,$c,$f_=($f=$b-1/2)&Tri[$a,$b,$c]&Tri[$a,$d,$f]] :: \
W[$a,$b,$d,1/2,$c,$f] : \
  -sqrt[($b-$c+$a)($c+1-$b+$a)/((2$b+1)(2$b)(2$c+1)(2$c+2))]
W[$a,$b,$d_=$d=$c-1/2,1/2,$c,$f_=($f=$b-1/2)&Tri[$a,$b,$c]&Tri[$a,$d,$f]] :: \
W[$a,$b,$d,1/2,$c,$f] : \
   sqrt[($b+$c-$a)($a+$b+$c+1)/((2$b+1)(2$c+1)4$b $c)]
W[$a,$b,$d_=$d=$c+1/2,1/2,$c,$f_=($f=$b+1/2)&Tri[$a,$b,$c]&Tri[$a,$d,$f]] :: \
W[$a,$b,$d,1/2,$c,$f] : \
   sqrt[($b+$c-$a+1)($a+$b+$c+2)/((2$b+1)(2$b+2)(2$c+1)(2$c+2))]
W[$a,$b,$d_=$d=$c-1/2,1/2,$c,$f_=($f=$b+1/2)&Tri[$a,$b,$c]&Tri[$a,$d,$f]] :: \
W[$a,$b,$d,1/2,$c,$f] : \
   sqrt[($b-$c+$a+1)($c-$b+$a)/((2$c+1) 2$c (2$b+1)(2$b+2))]
W[$a,$b,$c,0,$c,$b_=Tri[$a,$b,$c]] :: \
W[$a,$b,$c,0,$c,$b] : \
   sqrt[1/((2$b+1)(2$c+1))]
                        
			/* Canonicalization */
 
/* Get smallest argument into 4th slot using permutation symmetries
        6.2.4 and 6.2.5. */

W[$a,$b,$c,$d,$e,$f_=~P[$d=Sort[{$a,$b,$c,$d,$e,$f}][1]]]:: \
	(Lcl[%m]; %m:Sort[{$a,$b,$c,$d,$e,$f}][1];\
         Sel[ %m=$a,W[$d,$c,$b,$a,$e,$f],\
              %m=$b,W[$c,$d,$a,$b,$e,$f],\
	      %m=$c,W[$b,$a,$d,$c,$e,$f],\
              %m=$e,(-1)^($f+$e-$a-$d) W[$f,$b,$c,$e,$d,$a],\
	      %m=$f,(-1)^($f+$e-$a-$d) W[$e,$b,$c,$f,$a,$d] ])

      /* Coefficients that do not satisfy triangle conditions are zero.  */

W[$a,$b,$c,$d,$e,$f_=~P[Tri[$a,$b,$e]&Tri[$c,$d,$e]&Tri[$a,$c,$f]& \
			Tri[$b,$d,$f]]] :: 0

/*: Sixj[$a,$b,$c,$d,$e,$f] evaluates the 6-j symbol. */
Sixj[$a,$b,$c,$d,$e,$f] :: (-1)^($a+$b+$e+$d)W[$a,$b,$e,$d,$c,$f]
  
/*: Ninej[$a,$b,$c,$d,$e,$f,$g,$h,$i] evaluates the 9-j symbol in terms
  of a sum of Wigner coefficients. */
/*R: D.M. Brink and G.R. Satchler: Angular Momentum, appendix 3 */

Ninej_:Tier
Ninej[$a,$b,$c,$d,$e,$f,$g,$h,$i_=~P[Tri[$a,$b,$c]&Tri[$d,$e,$f]& \
                                      Tri[$g,$h,$i]&Tri[$a,$d,$g]& \
		                      Tri[$b,$e,$h]&Tri[$c,$f,$i]]] :: 0

Ninej[$a,$b,$c,$d,$e,$f,$g,$h,$i] :: Ninej[$a,$b,$c,$d,$e,$f,$g,$h,$i] : \
(Lcl[%m,%n]; %m : Min[($c+$g),($f+$h),($b+$d)];\
	     %n : Max[Abs[$c-$g],Abs[$f-$h],Abs[$b-$d]]; \
     (-1)^($a+$b+$c+$d+$e+$f+$g+$h+$i) \
     Sum[(2*%#k+1) W[$b,$d,$c,$g,%#k,$a]W[$b,$d,$h,$f,%#k,$e]\
		   W[$c,$g,$f,$h,%#k,$i],{%#k,%n,%m,1/2}];Ex[%%])

/* sqrt is used instead of the system defined function Sqrt. This rewrites
   square roots of composite numbers as powers of prime numbers with no
   powers in the denominator, greatly aiding the simplification of
   the 6-j and 9-j coefficients. */
sqrt[$x _= Numbp[$x] & $x > 0] :: (Ap[Mult,Map[Ap[$1^($2/2),$0],Nfac[$x]]];\
  Num[%%]/S[Den[%%],$4^$5 --> $4^Ceil[$5]/$4^(Ceil[$5]-$5)])
sqrt[1] : 1

_XRacah[Loaded] : 1

/*E:
SMP 1.5.0

#I[1]::  <XRacah 

#I[2]::  <XPownew

#I[3]::  Post :: TidyPow;

#I[4]::  W[2,5/2,2,3/2,3/2,3]

	  1/2
	 7
#O[4]:   ----
	  35

#I[5]::  W[2,3/2,2,3/2,3/2,2]

#O[5]:   0

#I[6]::  W[2,1,1,1,1,1]

#O[6]:   -1/6

#I[7]::  Ninej[2,2,2,3/2,1,1/2,3/2,1,3/2]

	    1/2
	 105
#O[7]:   ------
	  600

#I[8]::  Ninej[3,2,2,3/2,1,1/2,5/2,1,3/2]

	   1/2
	 10
#O[8]:   -----
	  300
*/
