{-# LANGUAGE BangPatterns        #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Math.Algebra.Jack
  (schur, jack, zonal)
  where
import Control.Lens               ( (.~), element )
import Data.Array                 ( Array, (!), (//), listArray )
import Data.Maybe                 ( fromJust, isJust )
import Math.Algebra.Jack.Internal ( _N, hookLengths, _betaratio, _isPartition, Partition )
import Numeric.SpecFunctions      ( factorial )

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

-- | Evaluation of zonal polynomial
zonal :: (Fractional a, Ord a) 
  => [a] -- ^ values of the variables
  -> Partition -- ^ partition of integers
  -> a
zonal :: [a] -> Partition -> a
zonal [a]
x Partition
lambda = a
c a -> a -> a
forall a. Num a => a -> a -> a
* a
jck
  where
    k :: Int
k = Partition -> Int
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum Partition
lambda
    jlambda :: a
jlambda = [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product (Partition -> a -> [a]
forall a. Fractional a => Partition -> a -> [a]
hookLengths Partition
lambda a
2)
    c :: a
c = a
2a -> Int -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Int
k a -> a -> a
forall a. Num a => a -> a -> a
* Double -> a
forall a b. (Real a, Fractional b) => a -> b
realToFrac (Int -> Double
factorial Int
k) a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
jlambda
    jck :: a
jck = [a] -> Partition -> a -> a
forall a. (Fractional a, Ord a) => [a] -> Partition -> a -> a
jack [a]
x Partition
lambda a
2

-- | Evaluation of Schur polynomial
schur :: forall a. Fractional a 
  => [a] -- ^ values of the variables
  -> Partition -- ^ partition of integers 
  -> a
schur :: [a] -> Partition -> a
schur [a]
x Partition
lambda =
  case Partition -> Bool
_isPartition Partition
lambda of
    Bool
False -> [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"lambda is not a valid integer partition"
    Bool
True -> Int -> Int -> Partition -> Array (Int, Int) (Maybe a) -> a
sch Int
n Int
1 Partition
lambda Array (Int, Int) (Maybe a)
forall a. Array (Int, Int) (Maybe a)
arr0
      where
        nll :: Int
nll = Partition -> Partition -> Int
_N Partition
lambda Partition
lambda
        n :: Int
n = [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
x
        arr0 :: Array (Int, Int) (Maybe a)
arr0 = ((Int, Int), (Int, Int)) -> [Maybe a] -> Array (Int, Int) (Maybe a)
forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray ((Int
1, Int
1), (Int
nll, Int
n)) (Int -> Maybe a -> [Maybe a]
forall a. Int -> a -> [a]
replicate (Int
nll Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
n) Maybe a
forall a. Maybe a
Nothing)
        sch :: Int -> Int -> [Int] -> Array (Int,Int) (Maybe a) -> a
        sch :: Int -> Int -> Partition -> Array (Int, Int) (Maybe a) -> a
sch Int
m Int
k Partition
nu Array (Int, Int) (Maybe a)
arr
          | Partition -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Partition
nu Bool -> Bool -> Bool
|| Partition -> Int
forall a. [a] -> a
head Partition
nu Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 Bool -> Bool -> Bool
|| Int
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = a
1
          | Partition -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Partition
nu Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
m Bool -> Bool -> Bool
&& Partition
nuPartition -> Int -> Int
forall a. [a] -> Int -> a
!!Int
m Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = a
0
          | Int
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 = [a] -> a
forall a. [a] -> a
head [a]
x a -> Int -> a
forall a b. (Num a, Integral b) => a -> b -> a
^ Partition -> Int
forall a. [a] -> a
head Partition
nu
          | Maybe a -> Bool
forall a. Maybe a -> Bool
isJust (Array (Int, Int) (Maybe a)
arr Array (Int, Int) (Maybe a) -> (Int, Int) -> Maybe a
forall i e. Ix i => Array i e -> i -> e
! (Partition -> Partition -> Int
_N Partition
lambda Partition
nu, Int
m)) = Maybe a -> a
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe a -> a) -> Maybe a -> a
forall a b. (a -> b) -> a -> b
$ Array (Int, Int) (Maybe a)
arr Array (Int, Int) (Maybe a) -> (Int, Int) -> Maybe a
forall i e. Ix i => Array i e -> i -> e
! (Partition -> Partition -> Int
_N Partition
lambda Partition
nu, Int
m)
          | Bool
otherwise = a
s
            where
              s :: a
s = a -> Int -> a
Fractional a => a -> Int -> a
go (Int -> Int -> Partition -> Array (Int, Int) (Maybe a) -> a
sch (Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Int
1 Partition
nu Array (Int, Int) (Maybe a)
arr) Int
k
              go :: Fractional a => a -> Int -> a
              go :: a -> Int -> a
go !a
ss Int
ii
                | Partition -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Partition
nu Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
ii Bool -> Bool -> Bool
|| Partition
nuPartition -> Int -> Int
forall a. [a] -> Int -> a
!!(Int
iiInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = a
ss
                | Bool
otherwise =
                  let u :: Int
u = Partition
nuPartition -> Int -> Int
forall a. [a] -> Int -> a
!!(Int
iiInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) in
                  if Partition -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Partition
nu Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ii Bool -> Bool -> Bool
&& Int
u Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
|| Int
u Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Partition
nu Partition -> Int -> Int
forall a. [a] -> Int -> a
!! Int
ii
                    then
                      let nu' :: Partition
nu' = (Int -> IndexedTraversal' Int Partition Int
forall (t :: * -> *) a.
Traversable t =>
Int -> IndexedTraversal' Int (t a) a
element (Int
iiInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) ((Int -> Identity Int) -> Partition -> Identity Partition)
-> Int -> Partition -> Partition
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Int
uInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Partition
nu in
                      if Int
u Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1
                        then
                          a -> Int -> a
Fractional a => a -> Int -> a
go (a
ss a -> a -> a
forall a. Num a => a -> a -> a
+ [a]
x[a] -> Int -> a
forall a. [a] -> Int -> a
!!(Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) a -> a -> a
forall a. Num a => a -> a -> a
* Int -> Int -> Partition -> Array (Int, Int) (Maybe a) -> a
sch Int
m Int
ii Partition
nu' Array (Int, Int) (Maybe a)
arr) (Int
ii Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
                        else
                          if Partition -> Int
forall a. [a] -> a
head Partition
nu' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
                            then
                              a -> Int -> a
Fractional a => a -> Int -> a
go (a
ss a -> a -> a
forall a. Num a => a -> a -> a
+ [a]
x[a] -> Int -> a
forall a. [a] -> Int -> a
!!(Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)) (Int
ii Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
                            else
                              let arr' :: Array (Int, Int) (Maybe a)
arr' = Array (Int, Int) (Maybe a)
arr Array (Int, Int) (Maybe a)
-> [((Int, Int), Maybe a)] -> Array (Int, Int) (Maybe a)
forall i e. Ix i => Array i e -> [(i, e)] -> Array i e
// [((Partition -> Partition -> Int
_N Partition
lambda Partition
nu, Int
m), a -> Maybe a
forall a. a -> Maybe a
Just a
ss)] in
                              a -> Int -> a
Fractional a => a -> Int -> a
go (a
ss a -> a -> a
forall a. Num a => a -> a -> a
+ [a]
x[a] -> Int -> a
forall a. [a] -> Int -> a
!!(Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) a -> a -> a
forall a. Num a => a -> a -> a
* Int -> Int -> Partition -> Array (Int, Int) (Maybe a) -> a
sch (Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Int
1 Partition
nu' Array (Int, Int) (Maybe a)
arr') (Int
ii Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
                    else
                      a -> Int -> a
Fractional a => a -> Int -> a
go a
ss (Int
iiInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)