{-# LANGUAGE BangPatterns        #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Math.Algebra.JackPol
  (schurPol, jackPol, zonalPol)
  where
import qualified Algebra.Ring as AR
import           Control.Lens               ( (.~), element )
import           Data.Array                 ( Array, (!), (//), listArray )
import           Data.Maybe                 ( fromJust, isJust )
import           Math.Algebra.Jack.Internal ( _betaratio', hookLengths, _N
                                            , _isPartition, Partition )
import           Math.Algebra.Hspray        ( (*^), (^**^), (^*^), (^+^)
                                            , constantSpray, lone, Spray )
import           Numeric.SpecFunctions      ( factorial )

-- | Symbolic Jack polynomial
jackPol :: forall a. (Fractional a, Ord a, AR.C a) 
  => Int -- ^ number of variables
  -> Partition -- ^ partition of integers
  -> a -- ^ alpha parameter
  -> Spray a
jackPol :: forall a.
(Fractional a, Ord a, C a) =>
Int -> Partition -> a -> Spray a
jackPol Int
n Partition
lambda a
alpha =
  case Partition -> Bool
_isPartition Partition
lambda Bool -> Bool -> Bool
&& a
alpha forall a. Ord a => a -> a -> Bool
> a
0 of
    Bool
False -> if Partition -> Bool
_isPartition Partition
lambda
      then forall a. HasCallStack => [Char] -> a
error [Char]
"alpha must be strictly positive"
      else forall a. HasCallStack => [Char] -> a
error [Char]
"lambda is not a valid integer partition"
    Bool
True -> Int
-> Int
-> Partition
-> Partition
-> Array (Int, Int) (Maybe (Spray a))
-> a
-> Spray a
jac (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Spray a]
x) Int
0 Partition
lambda Partition
lambda forall {a}. Array (Int, Int) (Maybe a)
arr0 a
1
      where
      nll :: Int
nll = Partition -> Partition -> Int
_N Partition
lambda Partition
lambda
      x :: [Spray a]
x = forall a b. (a -> b) -> [a] -> [b]
map forall a. C a => Int -> Spray a
lone [Int
1 .. Int
n] :: [Spray a]
      arr0 :: Array (Int, Int) (Maybe a)
arr0 = forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray ((Int
1, Int
1), (Int
nll, Int
n)) (forall a. Int -> a -> [a]
replicate (Int
nll forall a. Num a => a -> a -> a
* Int
n) forall a. Maybe a
Nothing)
      theproduct :: Int -> a
      theproduct :: Int -> a
theproduct Int
nu0 = if Int
nu0 forall a. Ord a => a -> a -> Bool
<= Int
1
        then forall a. C a => a
AR.one
        else forall a. C a => [a] -> a
AR.product forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\Int
i -> a
alpha forall a. Num a => a -> a -> a
* forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i forall a. Num a => a -> a -> a
+ a
1) [Int
1 .. Int
nu0forall a. Num a => a -> a -> a
-Int
1]
      jac :: Int -> Int -> Partition -> Partition -> Array (Int,Int) (Maybe (Spray a)) -> a -> Spray a
      jac :: Int
-> Int
-> Partition
-> Partition
-> Array (Int, Int) (Maybe (Spray a))
-> a
-> Spray a
jac Int
m Int
k Partition
mu Partition
nu Array (Int, Int) (Maybe (Spray a))
arr a
beta
        | forall (t :: * -> *) a. Foldable t => t a -> Bool
null Partition
nu Bool -> Bool -> Bool
|| forall a. [a] -> a
head Partition
nu forall a. Eq a => a -> a -> Bool
== Int
0 Bool -> Bool -> Bool
|| Int
m forall a. Eq a => a -> a -> Bool
== Int
0 = forall a. (C a, Eq a) => a -> Spray a
constantSpray a
1
        | forall (t :: * -> *) a. Foldable t => t a -> Int
length Partition
nu forall a. Ord a => a -> a -> Bool
> Int
m Bool -> Bool -> Bool
&& Partition
nuforall a. [a] -> Int -> a
!!Int
m forall a. Ord a => a -> a -> Bool
> Int
0 = forall a. (C a, Eq a) => a -> Spray a
constantSpray a
0
        | Int
m forall a. Eq a => a -> a -> Bool
== Int
1 = Int -> a
theproduct (forall a. [a] -> a
head Partition
nu) forall a. (C a, Eq a) => a -> Spray a -> Spray a
*^ (forall a. [a] -> a
head [Spray a]
x forall a. (C a, Eq a) => Spray a -> Int -> Spray a
^**^ forall a. [a] -> a
head Partition
nu) 
        | Int
k forall a. Eq a => a -> a -> Bool
== Int
0 Bool -> Bool -> Bool
&& forall a. Maybe a -> Bool
isJust (Array (Int, Int) (Maybe (Spray a))
arr forall i e. Ix i => Array i e -> i -> e
! (Partition -> Partition -> Int
_N Partition
lambda Partition
nu, Int
m)) =
                      forall a. HasCallStack => Maybe a -> a
fromJust forall a b. (a -> b) -> a -> b
$ Array (Int, Int) (Maybe (Spray a))
arr forall i e. Ix i => Array i e -> i -> e
! (Partition -> Partition -> Int
_N Partition
lambda Partition
nu, Int
m)
        | Bool
otherwise = Spray a
s
          where
            s :: Spray a
s = Spray a -> Int -> Spray a
go (a
beta forall a. (C a, Eq a) => a -> Spray a -> Spray a
*^ (Int
-> Int
-> Partition
-> Partition
-> Array (Int, Int) (Maybe (Spray a))
-> a
-> Spray a
jac (Int
mforall a. Num a => a -> a -> a
-Int
1) Int
0 Partition
nu Partition
nu Array (Int, Int) (Maybe (Spray a))
arr a
1 forall a. (C a, Eq a) => Spray a -> Spray a -> Spray a
^*^ (([Spray a]
xforall a. [a] -> Int -> a
!!(Int
mforall a. Num a => a -> a -> a
-Int
1)) forall a. (C a, Eq a) => Spray a -> Int -> Spray a
^**^ (forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum Partition
mu forall a. Num a => a -> a -> a
- forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum Partition
nu))))
                (forall a. Ord a => a -> a -> a
max Int
1 Int
k)
            go :: Spray a -> Int -> Spray a
            go :: Spray a -> Int -> Spray a
go !Spray a
ss Int
ii
              | forall (t :: * -> *) a. Foldable t => t a -> Int
length Partition
nu forall a. Ord a => a -> a -> Bool
< Int
ii Bool -> Bool -> Bool
|| Partition
nuforall a. [a] -> Int -> a
!!(Int
iiforall a. Num a => a -> a -> a
-Int
1) forall a. Eq a => a -> a -> Bool
== Int
0 = Spray a
ss
              | Bool
otherwise =
                let u :: Int
u = Partition
nuforall a. [a] -> Int -> a
!!(Int
iiforall a. Num a => a -> a -> a
-Int
1) in
                if forall (t :: * -> *) a. Foldable t => t a -> Int
length Partition
nu forall a. Eq a => a -> a -> Bool
== Int
ii Bool -> Bool -> Bool
&& Int
u forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
|| Int
u forall a. Ord a => a -> a -> Bool
> Partition
nuforall a. [a] -> Int -> a
!!Int
ii
                  then
                    let nu' :: Partition
nu' = (forall (t :: * -> *) a.
Traversable t =>
Int -> IndexedTraversal' Int (t a) a
element (Int
iiforall a. Num a => a -> a -> a
-Int
1) forall s t a b. ASetter s t a b -> b -> s -> t
.~ Int
uforall a. Num a => a -> a -> a
-Int
1) Partition
nu in
                    let gamma :: a
gamma = a
beta forall a. Num a => a -> a -> a
* forall a.
(Fractional a, C a) =>
Partition -> Partition -> Int -> a -> a
_betaratio' Partition
mu Partition
nu Int
ii a
alpha in
                    if Int
u forall a. Ord a => a -> a -> Bool
> Int
1
                      then
                        Spray a -> Int -> Spray a
go (Spray a
ss forall a. (C a, Eq a) => Spray a -> Spray a -> Spray a
^+^ Int
-> Int
-> Partition
-> Partition
-> Array (Int, Int) (Maybe (Spray a))
-> a
-> Spray a
jac Int
m Int
ii Partition
mu Partition
nu' Array (Int, Int) (Maybe (Spray a))
arr a
gamma) (Int
ii forall a. Num a => a -> a -> a
+ Int
1)
                      else
                        if forall a. [a] -> a
head Partition
nu' forall a. Eq a => a -> a -> Bool
== Int
0
                          then
                            Spray a -> Int -> Spray a
go (Spray a
ss forall a. (C a, Eq a) => Spray a -> Spray a -> Spray a
^+^ (a
gamma forall a. (C a, Eq a) => a -> Spray a -> Spray a
*^ ([Spray a]
xforall a. [a] -> Int -> a
!!(Int
mforall a. Num a => a -> a -> a
-Int
1) forall a. (C a, Eq a) => Spray a -> Int -> Spray a
^**^ forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum Partition
mu))) (Int
ii forall a. Num a => a -> a -> a
+ Int
1)
                          else
                            let arr' :: Array (Int, Int) (Maybe (Spray a))
arr' = Array (Int, Int) (Maybe (Spray a))
arr forall i e. Ix i => Array i e -> [(i, e)] -> Array i e
// [((Partition -> Partition -> Int
_N Partition
lambda Partition
nu, Int
m), forall a. a -> Maybe a
Just Spray a
ss)] in
                            let jck :: Spray a
jck = Int
-> Int
-> Partition
-> Partition
-> Array (Int, Int) (Maybe (Spray a))
-> a
-> Spray a
jac (Int
mforall a. Num a => a -> a -> a
-Int
1) Int
0 Partition
nu' Partition
nu' Array (Int, Int) (Maybe (Spray a))
arr' a
1 in
                            let jck' :: Spray a
jck' = a
gamma forall a. (C a, Eq a) => a -> Spray a -> Spray a
*^ (Spray a
jck forall a. (C a, Eq a) => Spray a -> Spray a -> Spray a
^*^ 
                                        ([Spray a]
xforall a. [a] -> Int -> a
!!(Int
mforall a. Num a => a -> a -> a
-Int
1) forall a. (C a, Eq a) => Spray a -> Int -> Spray a
^**^ (forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum Partition
mu forall a. Num a => a -> a -> a
- forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum Partition
nu'))) in
                            Spray a -> Int -> Spray a
go (Spray a
ss forall a. (C a, Eq a) => Spray a -> Spray a -> Spray a
^+^ Spray a
jck') (Int
iiforall a. Num a => a -> a -> a
+Int
1)
                  else
                    Spray a -> Int -> Spray a
go Spray a
ss (Int
iiforall a. Num a => a -> a -> a
+Int
1)

-- | Symbolic zonal polynomial
zonalPol :: (Fractional a, Ord a, AR.C a) 
  => Int -- ^ number of variables
  -> Partition -- ^ partition of integers
  -> Spray a
zonalPol :: forall a. (Fractional a, Ord a, C a) => Int -> Partition -> Spray a
zonalPol Int
n Partition
lambda = a
c forall a. (C a, Eq a) => a -> Spray a -> Spray a
*^ Spray a
jck
  where
    k :: Int
k = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum Partition
lambda
    jlambda :: a
jlambda = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product (forall a. Fractional a => Partition -> a -> [a]
hookLengths Partition
lambda a
2)
    c :: a
c = a
2forall a b. (Num a, Integral b) => a -> b -> a
^Int
k forall a. Num a => a -> a -> a
* forall a b. (Real a, Fractional b) => a -> b
realToFrac (Int -> Double
factorial Int
k) forall a. Fractional a => a -> a -> a
/ a
jlambda
    jck :: Spray a
jck = forall a.
(Fractional a, Ord a, C a) =>
Int -> Partition -> a -> Spray a
jackPol Int
n Partition
lambda a
2

-- | Symbolic Schur polynomial
schurPol :: 
  Int -- ^ number of variables
  -> Partition -- ^ partition of integers
  -> Spray Int
schurPol :: Int -> Partition -> Spray Int
schurPol Int
n Partition
lambda =
  case Partition -> Bool
_isPartition Partition
lambda of
    Bool
False -> forall a. HasCallStack => [Char] -> a
error [Char]
"lambda is not a valid integer partition"
    Bool
True -> Int
-> Int
-> Partition
-> Array (Int, Int) (Maybe (Spray Int))
-> Spray Int
sch Int
n Int
1 Partition
lambda forall {a}. Array (Int, Int) (Maybe a)
arr0
      where
        x :: [Spray Int]
x = forall a b. (a -> b) -> [a] -> [b]
map forall a. C a => Int -> Spray a
lone [Int
1 .. Int
n] :: [Spray Int]
        nll :: Int
nll = Partition -> Partition -> Int
_N Partition
lambda Partition
lambda
        arr0 :: Array (Int, Int) (Maybe a)
arr0 = forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray ((Int
1, Int
1), (Int
nll, Int
n)) (forall a. Int -> a -> [a]
replicate (Int
nll forall a. Num a => a -> a -> a
* Int
n) forall a. Maybe a
Nothing)
        sch :: Int -> Int -> [Int] -> Array (Int,Int) (Maybe (Spray Int)) -> Spray Int
        sch :: Int
-> Int
-> Partition
-> Array (Int, Int) (Maybe (Spray Int))
-> Spray Int
sch Int
m Int
k Partition
nu Array (Int, Int) (Maybe (Spray Int))
arr
          | forall (t :: * -> *) a. Foldable t => t a -> Bool
null Partition
nu Bool -> Bool -> Bool
|| forall a. [a] -> a
head Partition
nu forall a. Eq a => a -> a -> Bool
== Int
0 Bool -> Bool -> Bool
|| Int
m forall a. Eq a => a -> a -> Bool
== Int
0 = forall a. (C a, Eq a) => a -> Spray a
constantSpray Int
1
          | forall (t :: * -> *) a. Foldable t => t a -> Int
length Partition
nu forall a. Ord a => a -> a -> Bool
> Int
m Bool -> Bool -> Bool
&& Partition
nuforall a. [a] -> Int -> a
!!Int
m forall a. Ord a => a -> a -> Bool
> Int
0 = forall a. (C a, Eq a) => a -> Spray a
constantSpray Int
0
          | Int
m forall a. Eq a => a -> a -> Bool
== Int
1 = forall a. [a] -> a
head [Spray Int]
x forall a. (C a, Eq a) => Spray a -> Int -> Spray a
^**^ forall a. [a] -> a
head Partition
nu
          | forall a. Maybe a -> Bool
isJust (Array (Int, Int) (Maybe (Spray Int))
arr forall i e. Ix i => Array i e -> i -> e
! (Partition -> Partition -> Int
_N Partition
lambda Partition
nu, Int
m)) = forall a. HasCallStack => Maybe a -> a
fromJust forall a b. (a -> b) -> a -> b
$ Array (Int, Int) (Maybe (Spray Int))
arr forall i e. Ix i => Array i e -> i -> e
! (Partition -> Partition -> Int
_N Partition
lambda Partition
nu, Int
m)
          | Bool
otherwise = Spray Int
s
            where
              s :: Spray Int
s = Spray Int -> Int -> Spray Int
go (Int
-> Int
-> Partition
-> Array (Int, Int) (Maybe (Spray Int))
-> Spray Int
sch (Int
mforall a. Num a => a -> a -> a
-Int
1) Int
1 Partition
nu Array (Int, Int) (Maybe (Spray Int))
arr) Int
k
              go :: Spray Int -> Int -> Spray Int
              go :: Spray Int -> Int -> Spray Int
go !Spray Int
ss Int
ii
                | forall (t :: * -> *) a. Foldable t => t a -> Int
length Partition
nu forall a. Ord a => a -> a -> Bool
< Int
ii Bool -> Bool -> Bool
|| Partition
nuforall a. [a] -> Int -> a
!!(Int
iiforall a. Num a => a -> a -> a
-Int
1) forall a. Eq a => a -> a -> Bool
== Int
0 = Spray Int
ss
                | Bool
otherwise =
                  let u :: Int
u = Partition
nuforall a. [a] -> Int -> a
!!(Int
iiforall a. Num a => a -> a -> a
-Int
1) in
                  if forall (t :: * -> *) a. Foldable t => t a -> Int
length Partition
nu forall a. Eq a => a -> a -> Bool
== Int
ii Bool -> Bool -> Bool
&& Int
u forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
|| Int
u forall a. Ord a => a -> a -> Bool
> Partition
nu forall a. [a] -> Int -> a
!! Int
ii
                    then
                      let nu' :: Partition
nu' = (forall (t :: * -> *) a.
Traversable t =>
Int -> IndexedTraversal' Int (t a) a
element (Int
iiforall a. Num a => a -> a -> a
-Int
1) forall s t a b. ASetter s t a b -> b -> s -> t
.~ Int
uforall a. Num a => a -> a -> a
-Int
1) Partition
nu in
                      if Int
u forall a. Ord a => a -> a -> Bool
> Int
1
                        then
                          Spray Int -> Int -> Spray Int
go (Spray Int
ss forall a. (C a, Eq a) => Spray a -> Spray a -> Spray a
^+^ (([Spray Int]
xforall a. [a] -> Int -> a
!!(Int
mforall a. Num a => a -> a -> a
-Int
1)) forall a. (C a, Eq a) => Spray a -> Spray a -> Spray a
^*^ Int
-> Int
-> Partition
-> Array (Int, Int) (Maybe (Spray Int))
-> Spray Int
sch Int
m Int
ii Partition
nu' Array (Int, Int) (Maybe (Spray Int))
arr)) (Int
ii forall a. Num a => a -> a -> a
+ Int
1)
                        else
                          if forall a. [a] -> a
head Partition
nu' forall a. Eq a => a -> a -> Bool
== Int
0
                            then
                              Spray Int -> Int -> Spray Int
go (Spray Int
ss forall a. (C a, Eq a) => Spray a -> Spray a -> Spray a
^+^ ([Spray Int]
xforall a. [a] -> Int -> a
!!(Int
mforall a. Num a => a -> a -> a
-Int
1))) (Int
ii forall a. Num a => a -> a -> a
+ Int
1)
                            else
                              let arr' :: Array (Int, Int) (Maybe (Spray Int))
arr' = Array (Int, Int) (Maybe (Spray Int))
arr forall i e. Ix i => Array i e -> [(i, e)] -> Array i e
// [((Partition -> Partition -> Int
_N Partition
lambda Partition
nu, Int
m), forall a. a -> Maybe a
Just Spray Int
ss)] in
                              Spray Int -> Int -> Spray Int
go (Spray Int
ss forall a. (C a, Eq a) => Spray a -> Spray a -> Spray a
^+^ (([Spray Int]
xforall a. [a] -> Int -> a
!!(Int
mforall a. Num a => a -> a -> a
-Int
1)) forall a. (C a, Eq a) => Spray a -> Spray a -> Spray a
^*^ Int
-> Int
-> Partition
-> Array (Int, Int) (Maybe (Spray Int))
-> Spray Int
sch (Int
mforall a. Num a => a -> a -> a
-Int
1) Int
1 Partition
nu' Array (Int, Int) (Maybe (Spray Int))
arr')) (Int
ii forall a. Num a => a -> a -> a
+ Int
1)
                    else
                      Spray Int -> Int -> Spray Int
go Spray Int
ss (Int
iiforall a. Num a => a -> a -> a
+Int
1)