module Music.Theory.Permutations.List where
import Data.List
import qualified Math.Combinatorics.Multiset as C
import qualified Music.Theory.Permutations as P
permutations_l :: [a] -> [[a]]
permutations_l :: forall a. [a] -> [[a]]
permutations_l [a]
i =
let f :: Permutation -> [a]
f Permutation
p = forall t. Permutation -> [t] -> [t]
P.apply_permutation Permutation
p [a]
i
in forall a b. (a -> b) -> [a] -> [b]
map Permutation -> [a]
f (Int -> [Permutation]
P.permutations_n (forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
i))
permutations_nk_l :: Eq e => Int -> Int -> [e] -> [[e]]
permutations_nk_l :: forall e. Eq e => Int -> Int -> [e] -> [[e]]
permutations_nk_l Int
n Int
k [e]
e =
if forall (t :: * -> *) a. Foldable t => t a -> Int
length [e]
e forall a. Eq a => a -> a -> Bool
/= Int
n
then forall a. HasCallStack => [Char] -> a
error [Char]
"permutations_nk_l"
else forall a. Eq a => [a] -> [a]
nub (forall a b. (a -> b) -> [a] -> [b]
map (forall a. Int -> [a] -> [a]
take Int
k) (forall a. [a] -> [[a]]
permutations_l [e]
e))
multiset_permutations :: Ord a => [a] -> [[a]]
multiset_permutations :: forall a. Ord a => [a] -> [[a]]
multiset_permutations = forall a. Multiset a -> [[a]]
C.permutations forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => [a] -> Multiset a
C.fromList
multiset_permutations_n :: Ord a => [a] -> Int
multiset_permutations_n :: forall a. Ord a => [a] -> Int
multiset_permutations_n [a]
x =
let occ :: [a] -> Permutation
occ = forall a b. (a -> b) -> [a] -> [b]
map forall (t :: * -> *) a. Foldable t => t a -> Int
length forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => [a] -> [[a]]
group forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => [a] -> [a]
sort
n :: Int
n = forall n. Integral n => n -> n
P.factorial (forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
x)
d :: Int
d = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall n. Integral n => n -> n
P.factorial forall a b. (a -> b) -> a -> b
$ [a] -> Permutation
occ [a]
x
in Int
n forall a. Integral a => a -> a -> a
`div` Int
d