-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumerator.hs
134 lines (108 loc) · 4.1 KB
/
Enumerator.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
module Enumerator where
import Control.Applicative ((<$>))
import Data.List (inits, sortBy)
import Control.Monad (guard)
import Data.Ord (comparing)
import Data
--------------------------------------------------------------------------------
--- Enumerate and filter out possible representations for prime
--------------------------------------------------------------------------------
enumLimbs :: Prime -> [Limbs]
enumLimbs (Prime m _) =
map radix [(m + 31) `div` 32 .. 100] -- limit size
where
base n = map (ceiling.((fromIntegral m :: Double) / fromIntegral n *)) [0..]
radix n = take n $ zipWith (-) (tail $ base n) (base n)
checkConstraint :: Prime -> Limbs -> Bool
checkConstraint (Prime _ k) (a:b:rest) =
(2 :: Integer) ^ (a + b + c) > (fromIntegral k :: Integer) * 2 ^ (65 :: Integer) -- a stricter bound
where
c = last rest
checkConstraint _ _ = error "Undefined constrant"
genLimbs :: Prime -> [Limbs]
genLimbs p = filter (checkConstraint p) $ enumLimbs p
getLimbs :: Prime -> [Limbs]
getLimbs p@(Prime m _) = do
l <- genLimbs p
guard $ all (<= precalculationOverflowBound p) l
let h = genFormula p l
let bound = minimum $ map sumOverflowBound h
guard $ all (<= bound) l
guard $ sum l == m
return l
precalculationOverflowBound :: Prime -> Int -- we assume only *k
precalculationOverflowBound (Prime _ k) = floor $ logBase 2 ((2::Double)**32 / fromIntegral k)
sumOverflowBound :: [Term] -> Int
sumOverflowBound h = floor $ logBase 2 (sqrt ((2::Double)**64 / fromIntegral sumOfCoef))
where
sumOfCoef = foldr helper 0 h
helper (Term n _ _) acc = n + acc
genFormula :: Prime -> Limbs -> [[Term]]
genFormula (Prime m k) limbs = mulreduce f g
where
nLimbs = length limbs
radix = map sum (inits $ cycle limbs)
mulElements :: Element -> Element -> Term
mulElements x y = Term (2 ^ n) x y
where
base = radix !! index x + radix !! index y
n = base - last (takeWhile (<= base) radix)
wrapAround :: Term -> Term
wrapAround (Term _ x y) = Term n' x y
where
base = radix !! index x + radix !! index y - m
n = base - last (takeWhile (<= base) radix)
n' = k * 2 ^ n
add :: [[Term]] -> [[Term]] -> [[Term]]
add (x:xs) (y:ys) = (x ++ y) : add xs ys
add xs@(_:_) _ = xs
add _ ys@(_:_) = ys
add _ _ = []
multiply :: [Element] -> [Element] -> [[Term]]
multiply (x:xs) ys'@(y:ys) =
[mulElements x y] : add (multiply [x] ys) (multiply xs ys')
multiply _ _ = []
reduce :: [[Term]] -> [[Term]]
reduce xs = add p $ map wrapAround <$> q
where
(p, q) = splitAt nLimbs xs
mulreduce :: [Element] -> [Element] -> [[Term]]
mulreduce x y = reduce $ multiply x y
f, g :: [Element]
f = map (Element "f") [0..nLimbs-1]
g = map (Element "g") [0..nLimbs-1]
-- I only want the first representation!!
getFirst :: Prime -> (Limbs, Formula)
getFirst p = (limbs, genFormula p limbs)
where
limbs = head (getLimbs p)
--------------------------------------------------------------------------------
--- Enumerate evaluation order, 3 to 4 limbs as a group
--------------------------------------------------------------------------------
groupByN :: Int -> [a] -> [[a]]
groupByN _ [] = []
groupByN n xs = ys : groupByN n rest
where (ys, rest) = splitAt n xs
merge :: [a] -> [a] -> [a]
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys) = x : y : merge xs ys
evalOrders :: Int -> [[[Int]]]
evalOrders nlimbs = map postProcess [ groupByN 3 [0..nlimbs-1]
, groupByN 4 [0..nlimbs-1]
, step 3
, step 4]
where
step n = evens n ++ odds n
evens n = groupByN n $ takeWhile (<nlimbs) [0,2..]
odds n = groupByN n $ takeWhile (<nlimbs) [1,3..]
postProcess :: [[a]] -> [[a]]
postProcess = groupSmallParts . sortBy (comparing length)
--
groupSmallParts :: [[a]] -> [[a]]
groupSmallParts xs@(a:b:rest)
| length a + length b <= 4 = groupSmallParts $ (a ++ b) : rest
| otherwise = xs
groupSmallParts xs = xs
mapToTerm :: Formula -> [[[Int]]] -> [[[[Term]]]]
mapToTerm formula = map (map (map (formula!!)))