-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNumerics.py
More file actions
108 lines (80 loc) · 3.16 KB
/
Numerics.py
File metadata and controls
108 lines (80 loc) · 3.16 KB
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
from pythonfrp import Interp
from pythonfrp.StaticNumerics import *
from pythonfrp.Factory import *
from pythonfrp.Types import *
import math
pi = math.pi
twopi = 2*pi
P3 = lift("SP3", SP3, types = [numType, numType, numType], outType = p3Type)
p3 = P3
P2 = lift("SP2", SP2, types = [numType, numType], outType = p2Type)
p2 = P2
P3C = lift('P3C',SP3C,[numType, numType, numType], outType = p3Type)
p3c = P3C
p2 = lift("p2", SP2, types = [numType, numType], outType = p2Type)
P2 = p2
getX = lift("getX", lambda v:v.x, [hasXYType], numType)
getY = lift("getY", lambda v:v.y, [hasXYType], numType)
getZ = lift("getZ", lambda v:v.z, [p3Type], numType)
radians = lift("radians", math.radians, [numType], numType)
# Delete this from elsewhere, use math.degrees in update functions
degrees = lift("degrees", math.degrees, [numType], numType)
sin = lift("sin", math.sin, [numType], numType)
asin = lift("asin", math.asin, [numType], numType)
cos = lift("cos", math.cos, [numType], numType)
acos = lift("acos", math.acos, [numType], numType)
tan = lift("tan", math.tan, [numType], numType)
atan2 = lift("atan2", math.atan2, [numType, numType], numType)
sqrt = lift("sqrt", math.sqrt, [numType], numType)
exp = lift("exp", math.exp, [numType], numType)
pow = lift("pow", math.pow, [numType,numType], numType)
log = lift("log", math.log, [numType], numType)
ceiling = lift("ceiling", sCeiling, [numType], numType)
floor = lift("floor", sFloor, [numType], numType)
fraction = lift("fraction", sFraction, [numType], numType)
max = lift("max", max, [numType,numType], numType)
min = lift("min", min, [numType,numType], numType)
# sections
add = lift("add", lambda x: lambda y: x+y, [numType], fnType)
sub = lift("sub", lambda x: lambda y: y-x, [numType], fnType)
times = lift("times", lambda x: lambda y: x*y, [numType], fnType)
div = lift("div", lambda x: lambda y: x/y, [numType], fnType)
#dot = lift(lambda x,y: genDot(x,y), "dot", infer="dot")
const = lift("const", lambda x: lambda y: x, [anyType], fnType)
string = lift("string", str, [anyType], stringType)
#norm = lift(normP3, 'norm', [P3Type], P3Type)
normA = lift("normA", sNormA, [numType], numType)
def dist(x, y):
return abs(x-y)
format = lift("format", lambda str, *a: str % a)
# Lifted conditional
def staticIf(test, x, y):
if test:
return x
return y
choose = lift("choose", staticIf)
# Interpolation functions
lerp = lift("lerp", Interp.lerpStatic)
interpolate = lift("interpolate", Interp.interpolateStatic)
def encodeNums(*n):
s = ""
r = ""
for num in n:
r = r + s + str(num)
s = ","
return r
def decodeNums(s, f):
nums = s.split(",")
return f(*map(lambda x: float(x.strip()), nums))
p3Type.encoder = lambda p: encodeNums(p.x, p.y, p.z)
p3Type.decoder = lambda s: decodeNums(s, p3)
p2Type.encoder = lambda p: encodeNums(p.x, p.y)
p2Type.decoder = lambda s: decodeNums(s, p2)
def cross(a, b):
a1 = getX(a)
a2 = getY(a)
a3 = getZ(a)
b1 = getX(b)
b2 = getY(b)
b3 = getZ(b)
return p3(a2*b3 - a3*b2,a3*b1-a1*b3,a1*b2-a2*b1)