-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathhelperFunctions.py
90 lines (66 loc) · 2.9 KB
/
helperFunctions.py
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
import numpy as np
from functools import reduce, partial
from mpmath import mp
from mpmath import pi as Pi
from mpmath import sin as Sin
from mpmath import asin as Asin
from mpmath import sqrt as Sqrt
from multiprocessing import Pool
import datetime
dyadicMap = lambda x : (2 * x) % 1
# --------------------------------------
tau = 12
# --------------------------------------
# --------------------------------------
# convert between binary / decimal basis
# --------------------------------------
def decimalToBinary(decimalInitial, targetBinaryPrecision = tau):
return reduce(lambda acc, _: [dyadicMap(acc[0]), acc[1] + ('0' if acc[0] < 0.5 else '1')],
range(targetBinaryPrecision),
[decimalInitial, ''])[1]
def binaryToDecimal(binaryInitial):
return reduce(lambda acc, val: acc + int(val[1]) / mp.power(2, (val[0] + 1)),
enumerate(binaryInitial),
mp.mpf(0.0))
def binaryReducer(val):
return int(val[1]) / mp.power(2, (val[0] + 1))
def binaryToDecFaster(binaryInitial):
with Pool(8) as p:
tt = p.map(binaryReducer, enumerate(binaryInitial))
res = mp.mpf(0)
for _ in tt:
res += _
return res
# --------------------------------------
# --------------------------------------
phiInv = lambda z: np.arcsin(np.sqrt(z)) / (2.0 * np.pi)
decimalToBinary_phiInv = lambda z: decimalToBinary(phiInv(z))
phi = lambda theta: Sin(theta * Pi * 2.0) ** 2
# --------------------------------------
# --------------------------------------
# decoding functions
# --------------------------------------
def dyadicDecoder(decimalInitial, k):
return (2 ** (k * tau) * decimalInitial) % 1
def logisticDecoder(decimalInitial, k):
return float(Sin(2 ** (k * tau) * Asin(Sqrt(decimalInitial))) ** 2)
def findInitialCondition(trainData):
conjugateInitial_binary = ''.join(map(decimalToBinary_phiInv, trainData))
necessaryPrecision = len(conjugateInitial_binary)
assert tau * len(trainData) == necessaryPrecision
# data is passed through sequentially so no need to worry
# plus, all samples have the same size anyway
# to be safe, these global settings should be handled more carefully with context managers
mp.prec = necessaryPrecision
print('significance = %d bits ; %d digits (base-10) ; ratio = %.3f\n' % (mp.prec, mp.dps, mp.prec / mp.dps))
# conjugateInitial = binaryToDecimal(conjugateInitial_binary)
conjugateInitial = binaryToDecFaster(conjugateInitial_binary)
decimalInitial = phi(conjugateInitial)
return decimalInitial
# --------------------------------------
def generateData(decimalInitial, howManyPoints):
p_logisticDecoder = partial(logisticDecoder, decimalInitial)
with Pool(8) as p:
decodedValues = p.map(p_logisticDecoder, range(howManyPoints))
return decodedValues
# --------------------------------------