-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStaticNumerics.py
336 lines (284 loc) · 9.09 KB
/
StaticNumerics.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# Done
# This is the non-reactive version of the numeric classes. To get the overloading right,
# we have to go through contortions using radd, rsub, and rmul so that an ordinary
# number doesn't screw up overloading in 1 + signal, 1 - signal, and 1 * signal.
# Not sure why rdiv isn't here.
#import g
import math
import random
import sys
from pythonfrp import Factory
from pythonfrp import Errors
from pythonfrp.Types import *
# This is a where we park signal functions.
pi = math.pi
twopi = 2 * pi
sCeiling = math.ceil
sFloor = math.floor
cos = math.cos
sin = math.sin
def sFraction(x):
return x - sFloor(x)
def degrees( v):
return v*(180/pi)
# This class is the 0 element in an arbitrary numeric
# class. It is used as the initial result of an integrator.
# Note that the destination is never changed.
class Zero:
def __str__(self):
return "0"
def __add__(self, y):
return y
zero = Zero()
def staticLerp(t, x, y):
return (1-t)*x + t*y
def staticLerpA(t, x, y):
x1 = x / twopi
y1 = y / twopi
x2 = twopi * (x1 - math.floor(x1))
y2 = twopi * (y1 - math.floor(y1))
if x2 < y2:
if y2 - x2 > pi:
return staticLerp(t, x2 + twopi, y2)
return staticLerp(t, x2, y2)
else:
if x2 - y2 > pi:
return staticLerp(t, x2-2 * pi, y2)
return staticLerp(t, x2, y2)
# Normalize an angle to the -pi to pi range
def sNormA(a):
a1 = a / twopi
a2 = twopi * (a1 - math.floor(a1))
return a2 if a2 <= pi else a2 - twopi
# The P2 class (2-d point)
# Note that P2 x Scalar works. Probably not P2 / scalar though.
class SP2:
def __init__(self, x, y):
self.x = x
self.y = y
self._type = p2Type
def __str__(self):
return "P2(%7.2f, %7.2f)" % (self.x, self.y)
def __add__(self, y):
if y is zero:
return self
if isinstance(y, SP2):
return addP2(self, y)
if isinstance(y, Factory.SFact):
return y + self
Errors.errorOnStaticTypes("Add", "SP2", y)
sys.exit()
def __radd__(self, y):
if y is zero:
return self
if isinstance(y, SP2):
return addP2(self, y)
Errors.errorOnStaticTypes("Add", "SP2", y)
def __sub__(self, y):
if y is zero:
return self
if isinstance(y, SP2):
return subP2(self, y)
if isinstance( y, Factory.SFact):
return Factory.Lift0F(self, p2Type) - y
Errors.errorOnStaticTypes("Sub", "SP2", y)
def __rsub__(self, y):
if y is zero:
return zero.sub(self, zero)
if isinstance(y, SP2):
return subP2(y, self)
Errors.errorOnStaticTypes("Sub", "SP2", y)
def __mul__(self, y):
if y is zero:
return zero
if isinstance(y, type(1)) or isinstance(y,type(1.0)):
return scaleP2(y, self)
if getPtype(y).includes(numType):
return scaleP2(y, self)
Errors.errorOnStaticTypes("Mul", "SP2", y)
def __rmul__(self, y):
if y is zero:
return zero
if isinstance(y, type(1)) or isinstance(y,type(1.0)):
return scaleP2(y, self)
if getPtype(y).includes(numType):
return scaleP2(y, self)
Errors.errorOnStaticTypes("Mul", "SP2", y)
def __div__(self, y):
if y is zero:
print("Universal Explosion")
return zero
if isinstance(y, type(1)) or isinstance(y,type(1.0)):
return scaleP2((1.0/y), self)
if getPtype(y).includes(numType):
return scaleP2(1.0/y, self)
Errors.errorOnStaticTypes("Div", "SP2", y)
def __abs__(self):
return absP2(self)
def __neg__(self):
return scaleP2(-1, self)
def interp(self, t, p2):
return SP2(staticLerp(t, self.x, p2.x),
staticLerp(t, self.y, p2.y))
def interpA(self, t, p2):
return SP2(staticLerpA(t, self.x, p2.x),
staticLerpA(t, self.y, p2.y))
# Used for integration
def readP2(str):
nums = parseNumbers(str)
return SP2(nums[0], nums[1])
# non-overloaded methods for P2 arithmentic
def addP2(a, b):
return SP2(a.x + b.x, a.y + b.y)
def subP2(a, b):
return SP2(a.x-b.x, a.y-b.y)
def scaleP2(s, a):
from . import Numerics
return Numerics.P2(s * a.x, s * a.y)
def absP2(a):
return math.sqrt(a.x * a.x + a.y * a.y)
def dotP2(a, b):
return SP2(a.x * b.x, a.y * b.y)
# The P3 class, similar to P2
class SP3:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
self._type = p3Type
def __str__(self):
return "P3(%7.2f, %7.2f, %7.2f)" % (self.x, self.y, self.z)
def __add__(self, y):
if y is zero:
return self
if isinstance(y, SP3):
return addP3(self, y)
if isinstance(y, Factory.SFact):
return y + self
Errors.errorOnStaticTypes("Add", "SP3", y)
def __radd__(self, y):
if y is zero:
return self
if isinstance(y, SP3):
return addP3(self, y)
if isinstance(y, Factory.SFact):
return y + self
Errors.errorOnStaticTypes("Add", "SP3", y)
def __sub__(self, y):
if y is zero:
return self
if isinstance(y, SP3):
return subP3(self, y)
if isinstance(y, Factory.SFact):
return Factory.Lift0F(self, p3Type) - y
Errors.errorOnStaticTypes("Sub", "SP3", y)
def __rsub__(self, y):
if y is zero:
return zero.sub(self, zero)
if isinstance(y, SP3):
return subP3(y, self)
Errors.errorOnStaticTypes("Sub", "SP3", y)
def __mul__(self, y):
if y is zero:
return zero
if isinstance(y, type(1)) or isinstance(y,type(1.0)):
return scaleP3(y, self)
#print(getPtype(y))
if getPtype(y).includes(numType):
return scaleP3(y, self)
Errors.errorOnStaticTypes("Mul", "SP3", y)
def __rmul__(self, y):
if y is zero:
return zero.rmul(self, y)
if isinstance(y, type(1)) or isinstance(y, type(1.5)):
return scaleP3(y, self)
#print(getPtype(y))
if getPtype(y).includes(numType):
return scaleP3(y, self)
Errors.errorOnStaticTypes("Mul", "SP3", y)
def __div__(self, y):
if y is zero:
print("Universal Explosion")
return zero
if isinstance(y, type(1)) or isinstance(y,type(1.0)):
return scaleP3((1.0/y), self)
if getPtype(y).includes(numType):
return scaleP3((1.0/y), self)
Errors.errorOnStaticTypes("Div", "SP2", y)
def __abs__(self):
return absP3(self)
def __neg__(self):
return scaleP3(-1, self)
def interp(self, t, p2):
return SP3(staticLerp(t, self.x, p2.x),
staticLerp(t, self.y, p2.y),
staticLerp(t, self.z, p2.z))
def readP3(str):
nums = parseNumbers(str)
return SP3(nums[0], nums[1], nums[2])
def crossProduct(a, b):
return SP3(a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x)
def normP3(p):
a = absP3(p)
if a < 0.0000001: # Avoid divide by 0
return SP3(0, 0, 0)
else:
return scaleP3(1 / a, p)
def addP3(a, p):
return SP3(a.x + p.x, a.y + p.y, a.z + p.z)
def subP3(a, p):
return SP3(a.x - p.x, a.y - p.y, a.z - p.z)
def scaleP3(s, a):
from . import Numerics
return Numerics.P3(a.x * s, a.y * s, a.z * s);
def absP3(a):
return math.sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
def dotP3(a, b):
return SP3(a.x * b.x, a.y * b.y, a.z * b.z)
# Construct a polar 2-D point
def SP2Polar(r, theta):
r = r + 0
theta = theta + 0
return SP2(r * math.cos(theta), r * math.sin(theta))
def SP3C(r, theta, z):
p = SP2Polar(r, theta)
return SP3(p.x, p.y, z)
# Conversions from tuple type.
def sFirst(p):
p.first
def sSecond(p):
p.second
# Random number stuff - static only!
def randomChoice(choices):
return random.choice(choices)
def random01():
return random.random()
def random11():
return 2 * random.random()-1
def randomRange(low, high=None):
if high is None:
return low * random01()
return low + random01() * (high-low)
def randomInt(low, high=None):
if high is None:
return random.randint(0, low)
return random.randint(low, high)
def shuffle(choices):
c = list(choices)
random.shuffle(c)
return c
def sStep(x):
if (x < 0):
return 0
else:
return 1
def sSmoothStep(x):
if (x < 0):
return 0
if (x > 1):
return 1
return x * x * (-2 * x + 3)
random.seed()
################################################################