module Music.Theory.Z.Castren_1994 where
import Data.Int
import Data.List
import Data.Maybe
import Data.Ratio
import qualified Music.Theory.List as List
import Music.Theory.Z
import qualified Music.Theory.Z.Forte_1973 as Forte
import qualified Music.Theory.Z.Sro as Sro
type Z12 = Int8
inv_sym :: [Z12] -> Bool
inv_sym :: [Z12] -> Bool
inv_sym [Z12]
x = [Z12]
x forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` forall a b. (a -> b) -> [a] -> [b]
map (\Z12
i -> forall a. Ord a => [a] -> [a]
sort (forall i (f :: * -> *).
(Integral i, Functor f) =>
Z i -> i -> f i -> f i
Sro.z_sro_tn forall i. Num i => Z i
z12 Z12
i (forall i (f :: * -> *).
(Integral i, Functor f) =>
Z i -> i -> f i -> f i
Sro.z_sro_invert forall i. Num i => Z i
z12 Z12
0 [Z12]
x))) [Z12
0..Z12
11]
sc_t_ti :: [Z12] -> Maybe ([Z12], [Z12])
sc_t_ti :: [Z12] -> Maybe ([Z12], [Z12])
sc_t_ti [Z12]
p =
if [Z12] -> Bool
inv_sym [Z12]
p
then forall a. Maybe a
Nothing
else forall a. a -> Maybe a
Just ([Z12]
p,forall i. Integral i => Z i -> [i] -> [i]
Forte.z_t_prime forall i. Num i => Z i
z12 (forall i (f :: * -> *).
(Integral i, Functor f) =>
Z i -> i -> f i -> f i
Sro.z_sro_invert forall i. Num i => Z i
z12 Z12
0 [Z12]
p))
t_sc_table :: [(Forte.SC_Name,[Z12])]
t_sc_table :: [(SC_Name, [Z12])]
t_sc_table =
let f :: [Z12] -> [(SC_Name, [Z12])]
f [Z12]
x = let nm :: SC_Name
nm = forall i. Integral i => [i] -> SC_Name
Forte.sc_name [Z12]
x
in case [Z12] -> Maybe ([Z12], [Z12])
sc_t_ti [Z12]
x of
Maybe ([Z12], [Z12])
Nothing -> [(SC_Name
nm,[Z12]
x)]
Just ([Z12]
p,[Z12]
q) -> [(SC_Name
nmforall a. [a] -> [a] -> [a]
++SC_Name
"A",[Z12]
p),(SC_Name
nmforall a. [a] -> [a] -> [a]
++SC_Name
"B",[Z12]
q)]
in forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap [Z12] -> [(SC_Name, [Z12])]
f forall n. Num n => [[n]]
Forte.scs
t_sc_name :: [Z12] -> Forte.SC_Name
t_sc_name :: [Z12] -> SC_Name
t_sc_name [Z12]
p =
let n :: Maybe (SC_Name, [Z12])
n = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\(SC_Name
_,[Z12]
q) -> forall i. Integral i => Z i -> [i] -> [i]
Forte.z_t_prime forall i. Num i => Z i
z12 [Z12]
p forall a. Eq a => a -> a -> Bool
== [Z12]
q) [(SC_Name, [Z12])]
t_sc_table
in forall a b. (a, b) -> a
fst (forall a. HasCallStack => Maybe a -> a
fromJust Maybe (SC_Name, [Z12])
n)
t_sc :: Forte.SC_Name -> [Z12]
t_sc :: SC_Name -> [Z12]
t_sc SC_Name
n = forall a b. (a, b) -> b
snd (forall a. HasCallStack => Maybe a -> a
fromJust (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\(SC_Name
m,[Z12]
_) -> SC_Name
n forall a. Eq a => a -> a -> Bool
== SC_Name
m) [(SC_Name, [Z12])]
t_sc_table))
t_scs :: [[Z12]]
t_scs :: [[Z12]]
t_scs = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd [(SC_Name, [Z12])]
t_sc_table
t_scs_n :: Integral i => i -> [[Z12]]
t_scs_n :: forall i. Integral i => i -> [[Z12]]
t_scs_n i
n = forall a. (a -> Bool) -> [a] -> [a]
filter ((forall a. Eq a => a -> a -> Bool
== i
n) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i a. Num i => [a] -> i
genericLength) [[Z12]]
t_scs
t_subsets :: [Z12] -> [Z12] -> [[Z12]]
t_subsets :: [Z12] -> [Z12] -> [[Z12]]
t_subsets [Z12]
x [Z12]
a = forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Eq a => [a] -> [a] -> Bool
`List.is_subset` [Z12]
x) (forall a b. (a -> b) -> [a] -> [b]
map forall a. Ord a => [a] -> [a]
sort (forall i (f :: * -> *).
(Integral i, Functor f) =>
Z i -> f i -> [f i]
Sro.z_sro_t_related forall i. Num i => Z i
z12 [Z12]
a))
ti_subsets :: [Z12] -> [Z12] -> [[Z12]]
ti_subsets :: [Z12] -> [Z12] -> [[Z12]]
ti_subsets [Z12]
x [Z12]
a = forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Eq a => [a] -> [a] -> Bool
`List.is_subset` [Z12]
x) (forall a. Eq a => [a] -> [a]
nub (forall a b. (a -> b) -> [a] -> [b]
map forall a. Ord a => [a] -> [a]
sort (forall (f :: * -> *) i.
(Eq (f i), Integral i, Functor f) =>
Z i -> f i -> [f i]
Sro.z_sro_ti_related forall i. Num i => Z i
z12 [Z12]
a)))
rle :: (Eq a,Integral i) => [a] -> [(i,a)]
rle :: forall a i. (Eq a, Integral i) => [a] -> [(i, a)]
rle =
let f :: [b] -> (a, b)
f [b]
x = (forall i a. Num i => [a] -> i
genericLength [b]
x,forall a. [a] -> a
head [b]
x)
in forall a b. (a -> b) -> [a] -> [b]
map forall {a} {b}. Num a => [b] -> (a, b)
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => [a] -> [[a]]
group
rle_decode :: (Integral i) => [(i,a)] -> [a]
rle_decode :: forall i a. Integral i => [(i, a)] -> [a]
rle_decode =
let f :: (i, a) -> [a]
f (i
i,a
j) = forall i a. Integral i => i -> a -> [a]
genericReplicate i
i a
j
in forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall {i} {a}. Integral i => (i, a) -> [a]
f
rle_length :: (Integral i) => [(i,a)] -> i
rle_length :: forall i a. Integral i => [(i, a)] -> i
rle_length = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst
t_n_class_vector :: (Num a, Integral i) => i -> [Z12] -> [a]
t_n_class_vector :: forall a i. (Num a, Integral i) => i -> [Z12] -> [a]
t_n_class_vector i
n [Z12]
x =
let a :: [[Z12]]
a = forall i. Integral i => i -> [[Z12]]
t_scs_n i
n
in forall a b. (a -> b) -> [a] -> [b]
map (forall i a. Num i => [a] -> i
genericLength forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Z12] -> [Z12] -> [[Z12]]
t_subsets [Z12]
x) [[Z12]]
a
ti_n_class_vector :: (Num b, Integral i) => i -> [Z12] -> [b]
ti_n_class_vector :: forall a i. (Num a, Integral i) => i -> [Z12] -> [a]
ti_n_class_vector i
n [Z12]
x =
let a :: [[Z12]]
a = forall i n. (Integral i, Num n) => i -> [[n]]
Forte.scs_n i
n
in forall a b. (a -> b) -> [a] -> [b]
map (forall i a. Num i => [a] -> i
genericLength forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Z12] -> [Z12] -> [[Z12]]
ti_subsets [Z12]
x) [[Z12]]
a
dyad_class_percentage_vector :: Integral i => [Z12] -> [i]
dyad_class_percentage_vector :: forall i. Integral i => [Z12] -> [i]
dyad_class_percentage_vector [Z12]
p =
let p' :: [i]
p' = forall i n. (Integral i, Num n) => Z i -> [i] -> [n]
Forte.z_icv forall i. Num i => Z i
z12 [Z12]
p
in forall a b. (a -> b) -> [a] -> [b]
map (forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [i]
p' forall a. Num a => a -> a -> a
*) [i]
p'
rel :: Integral i => [Z12] -> [Z12] -> Ratio i
rel :: forall i. Integral i => [Z12] -> [Z12] -> Ratio i
rel [Z12]
x [Z12]
y =
let x' :: [i]
x' = forall i. Integral i => [Z12] -> [i]
dyad_class_percentage_vector [Z12]
x
y' :: [i]
y' = forall i. Integral i => [Z12] -> [i]
dyad_class_percentage_vector [Z12]
y
in forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => a -> a
abs (forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (-) [i]
x' [i]
y')) forall a. Integral a => a -> a -> Ratio a
% i
2