-
Notifications
You must be signed in to change notification settings - Fork 0
/
Permutation.hs
48 lines (40 loc) · 1.04 KB
/
Permutation.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module Permutation where
import Rotation (MatrixRotation, identity)
import qualified Rotation as Rotation
import qualified Numeric.LinearAlgebra as LA
type N = Integer
toNumbers :: MatrixRotation -> [N]
toNumbers = map (go 0 . LA.toList) . LA.toColumns
where
go n [] = n
go n (f:fs)
| f /= 0 = n
| otherwise = go (n + 1) fs
facs :: [N]
facs =
let n = 9 * 6
in map fac [n - 1, n - 2 .. 0]
where
fac 0 = 1
fac n = n * fac (n - 1)
multiplierMappings :: [N -> N]
multiplierMappings = map mappingFor [0..]
where
mappingFor :: Integer -> Integer -> Integer
mappingFor n m
| n == m = 0
| n < m = m
| otherwise = m + 1
fromRotation :: MatrixRotation -> N
fromRotation = sum . zipWith3 go facs multiplierMappings . toNumbers
where
go fac multiplierMapping n = fac * multiplierMapping n
-- toRotation :: N -> MatrixRotation
toRotation = go facs
where
go [] _ = []
go (f:fs) n
| n >= f =
let (d, r) = n `divMod` f
in d : go fs r
| otherwise = 0 : go fs n