-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
104 lines (84 loc) · 2.79 KB
/
utils.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
import numpy as np
import sympy as sp
from parameters import EPSILON, PREC, EUCSPACEDIM
import math as m
import decimal
decimal.getcontext().prec = PREC
spatialFrame = np.concatenate([np.zeros((1, 3)), np.identity(3)])
def _fixFloat(inp):
if(isinstance(inp, np.ndarray)):
inp[np.isclose(inp, np.zeros(inp.shape), atol=EPSILON)] = 0
inp[np.isclose(inp, np.ones(inp.shape), atol=EPSILON)] = 1
else:
if(abs(inp) <= EPSILON): inp = 0
elif(abs(1 - abs(inp)) <= EPSILON): inp = -1 if inp < 0 else 1
diff = inp - _fixFloat(inp)
if(isinstance(diff, np.ndarray)):
if(np.any(diff > 0)):
print(diff)
elif(diff > 0):
print(diff)
return inp
def fixFloat(inp):
# recursively fix an array
if(isinstance(inp, np.ndarray) or\
isinstance(inp, list) or isinstance(inp, tuple)):
converted = []
for item in inp:
converted += [fixFloat(item)]
#
if(isinstance(inp, np.ndarray)):
return np.asarray(converted)
elif(isinstance(inp, tuple)):
return tuple(converted)
else:
return converted
else:
fixed = round(inp, PREC + 1)
return fixed
def p2t(point):
# convert a point coordinate into a tuple
return tuple([num for num in point])
def solve(eqs, symbols):
# try to solve symbolically
solution = sp.solve(eqs)
solved = not isinstance(solution, list)
if(solved):
symbVals = [solution[symb] for symb in symbols]
symbVals = np.asarray(symbVals).astype(np.float)
return symbVals
# try to solve numerically
initVal = np.random.rand(len(symbols))
for i in [9, 8, 7, 6, 5, 4]:
try:
solution = sp.nsolve(eqs, symbols, initVal, prec=i)
solution = np.asarray(solution).reshape(-1).astype(np.float)
except:
continue
assert len(solution) == len(symbols), "multiple solutions found"
symbVals = solution
return symbVals
def str2num(inObj):
if(isinstance(inObj, tuple) or isinstance(inObj, list)):
container = []
for obj in inObj:
container += [str2num(obj)]
if(isinstance(inObj, tuple)): # convert to same type as input
container = tuple(container)
return container
else:
if(isinstance(inObj, str)):
try:
return float(inObj)
except:
return inObj
else:
return inObj
def exhaustiveSubclasses(targClass):
result = [targClass]
subclasses = targClass.__subclasses__()
if(len(subclasses) != 0):
for subclass in subclasses:
moreSubclassses = exhaustiveSubclasses(subclass)
result += moreSubclassses
return result