{-# LANGUAGE CPP, BangPatterns, ScopedTypeVariables, PatternSynonyms, ViewPatterns #-}
module Math.Combinat.Partitions.Integer.Naive where
import Data.List
import Control.Monad ( liftM , replicateM )
import Math.Combinat.Classes
import Math.Combinat.ASCII as ASCII
import Math.Combinat.Numbers (factorial,binomial,multinomial)
import Math.Combinat.Helper
import Data.Array
import System.Random
import Math.Combinat.Partitions.Integer.IntList
import Math.Combinat.Partitions.Integer.Count ( countPartitions )
newtype Partition = Partition [Int] deriving (Eq,Ord,Show,Read)
instance HasNumberOfParts Partition where
numberOfParts (Partition p) = length p
isEmptyPartition :: Partition -> Bool
isEmptyPartition (Partition p) = null p
emptyPartition :: Partition
emptyPartition = Partition []
instance CanBeEmpty Partition where
empty = emptyPartition
isEmpty = isEmptyPartition
partitionHeight :: Partition -> Int
partitionHeight (Partition part) = case part of
(p:_) -> p
[] -> 0
partitionWidth :: Partition -> Int
partitionWidth (Partition part) = length part
instance HasHeight Partition where
height = partitionHeight
instance HasWidth Partition where
width = partitionWidth
heightWidth :: Partition -> (Int,Int)
heightWidth part = (height part, width part)
partitionWeight :: Partition -> Int
partitionWeight (Partition part) = sum' part
instance HasWeight Partition where
weight = partitionWeight
dualPartition :: Partition -> Partition
dualPartition (Partition part) = Partition (_dualPartition part)
instance HasDuality Partition where
dual = dualPartition
elements :: Partition -> [(Int,Int)]
elements (Partition part) = _elements part
pattern Nil :: Partition
pattern Nil <- (isEmpty -> True) where
Nil = empty
pattern Cons :: Int -> Partition -> Partition
pattern Cons x xs <- (unconsPartition -> Just (x,xs)) where
Cons x (Partition xs) = Partition (x:xs)
pattern Partition_ :: [Int] -> Partition
pattern Partition_ xs = Partition xs
pattern Head :: Int -> Partition
pattern Head h <- (head . toDescList -> h)
pattern Tail :: Partition -> Partition
pattern Tail xs <- (Partition . tail . toDescList -> xs)
pattern Length :: Int -> Partition
pattern Length n <- (partitionWidth -> n)
toExponentialForm :: Partition -> [(Int,Int)]
toExponentialForm = _toExponentialForm . toDescList
fromExponentialForm :: [(Int,Int)] -> Partition
fromExponentialForm = Partition . _fromExponentialForm where
diffSequence :: Partition -> [Int]
diffSequence = go . toDescList where
go (x:ys@(y:_)) = (x-y) : go ys
go [x] = [x]
go [] = []
unconsPartition :: Partition -> Maybe (Int,Partition)
unconsPartition (Partition xs) = case xs of
(y:ys) -> Just (y, Partition ys)
[] -> Nothing
toDescList :: Partition -> [Int]
toDescList (Partition xs) = xs
dominates :: Partition -> Partition -> Bool
dominates (Partition qs) (Partition ps)
= and $ zipWith (>=) (sums (qs ++ repeat 0)) (sums ps)
where
sums = scanl (+) 0
isSubPartitionOf :: Partition -> Partition -> Bool
isSubPartitionOf (Partition ps) (Partition qs) = and $ zipWith (<=) ps (qs ++ repeat 0)
isSuperPartitionOf :: Partition -> Partition -> Bool
isSuperPartitionOf (Partition qs) (Partition ps) = and $ zipWith (<=) ps (qs ++ repeat 0)
pieriRule :: Partition -> Int -> [Partition]
pieriRule (Partition lambda) n = map Partition (_pieriRule lambda n) where
dualPieriRule :: Partition -> Int -> [Partition]
dualPieriRule lam n = map dualPartition $ pieriRule (dualPartition lam) n