-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransformations.py
More file actions
239 lines (200 loc) · 7.25 KB
/
Transformations.py
File metadata and controls
239 lines (200 loc) · 7.25 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
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
from sympy import sin, cos, tan, asin, acos, atan, symbols, solve, sympify, pprint, pretty
from sympy import pi, Eq, Function, exp, simplify, solveset, S
from sympy.parsing.sympy_parser import parse_expr
from sympy.abc import x, theta
from sympy.solvers import solve
from sympy import Symbol
from sympy.geometry import *
from sympy.parsing.sympy_parser import parse_expr
# plotting imports
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox
# transformations class
class Trans():
# instantiate variables
x = Symbol('x')
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
# init ran on creation
def __init__(self, expression):
self.f = expression
self.flist = [self.f]
self.fig, self.ax = plt.subplots()
plt.subplots_adjust(bottom=0.3)
self.t = np.arange(-5.0, 5.0, 0.01)
self.s = self.t ** 2
# test method to test that creation worked
def test(self):
return "Testing class: \nClass Creation Successful"
# convert string to sympy format
def tosymp(self, expr):
return parse_expr(expr)
# for update matplotlib use press for keypresses
def press(self, event):
print('press', event.key)
sys.stdout.flush()
if event.key == 'pageup':
print("[Pageup pressed]")
self.reflect(axis="x")
self.submit()
elif event.key == 'pagedown':
print("[Pagedown pressed]")
self.reflect(axis="y")
self.submit()
elif event.key == '1':
print("[1 pressed]")
self.stretch(axis="x", val=2)
self.submit(recenter=False)
elif event.key == '2':
print("[2 pressed]")
self.stretch(axis="x", val=(0.5))
self.submit(recenter=False)
elif event.key == '3':
print("[1 pressed]")
self.stretch(axis="y", val=2)
self.submit(recenter=False)
elif event.key == '4':
print("[2 pressed]")
self.stretch(axis="y", val=(0.5))
self.submit(recenter=False)
elif event.key == 'up':
print("[Up pressed]")
self.translate(axis="y", val=1)
self.submit()
elif event.key == 'down':
print("[Down pressed]")
self.translate(axis="y", val=(-1))
self.submit()
elif event.key == 'left':
print("[Left pressed]")
self.translate(axis="x", val=1)
self.submit()
elif event.key == 'right':
print("[Right pressed]")
self.translate(axis="x", val=(-1))
self.submit()
elif event.key == ' ':
print("[Space pressed]")
self.submit(recenter=True)
# plot function using matplotlib
def plot(self, update=False):
self.l, = plt.plot(self.t, self.s, lw=2)
if (update == True):
self.fig.canvas.mpl_connect('key_press_event', self.press)
self.axbox = plt.axes([0, 10, 0, 20])
# self.axbox = plt.axes([0.1, 0.05, 0.8, 0.075])
# convert x to self.t
xt = self.flist[len(self.flist)-1]
xt = xt.replace("x","(self.t)")
ydata = eval(xt)
self.l.set_ydata(ydata)
self.ax.set_ylim(np.min(ydata), np.max(ydata))
xr = np.linspace(0.2,10,100)
#fig, ax = plt.subplots()
#self.ax.plot(xr, 1/xr)
#self.ax.plot(xr, np.log(xr))
#self.ax.set_aspect('equal')
self.ax.grid(True, which='both')
self.ax.axhline(y=0, color='k')
self.ax.axvline(x=0, color='k')
plt.draw()
plt.show()
# redraw plot without restarting
def submit(self, recenter=True):
# convert x to self.t
xt = self.flist[len(self.flist)-1]
xt = xt.replace("x","(self.t)")
ydata = eval(xt)
self.l.set_ydata(ydata)
if recenter == True:
self.ax.set_ylim(np.min(ydata), np.max(ydata))
plt.draw()
# get full list in string format
def ListToString(self, header=True):
if header == True:
retString = "\nFull list of values: \n"
else:
retString = ""
for i in range(len(self.flist)):
if (i+1 != len(self.flist)):
retString = retString + (str((i + 1)) + ". " + str(self.tosymp(self.flist[i]))) + "\n"
else:
retString = retString + (str((i + 1)) + ". " + str(self.tosymp(self.flist[i])))
return retString
# get only final value in string format
def toString(self):
return ("\nCurrent value: \n" + str(self.tosymp(self.flist[len(self.flist) - 1])))
# return copy of entire list
def getList(self):
return self.flist
# get final value in list
def getFinal(self):
return self.flist[len(self.flist)-1]
# get first value in list
def getInitial(self):
return self.flist[0]
# gte value at index
def getIndex(self, index=0):
return self.flist[index]
# set list
def setList(self, newList=["0"]):
self.flist = newList
# set index of list
def setIndex(self, index=0, value=1):
self.flist[index] = value
# translate by value
def translate(self, axis="x", val=1):
vertorhor = "vertically"
if axis == "x":
vertorhor = "horizontally"
else:
vertorhor = "vertically"
print("\nTranslating " + str(vertorhor) + " by " + str(val) + "...")
curr = self.flist[len(self.flist)-1]
# if x then hor
if (axis == "x"):
toadd = curr
symptoadd = self.tosymp(toadd)
print(symptoadd.args)
symptoadd = symptoadd.replace(x,val+x)
self.flist.append(str(symptoadd))
# else if y then vert
else:
toadd = curr
symptoadd = self.tosymp(toadd)
symptoadd = symptoadd + val
self.flist.append(str(symptoadd))
# reflect over x or y axis
def reflect(self, axis="x"):
print("\nReflecting over the " + str(axis) + "-axis...")
curr = self.flist[len(self.flist)-1]
# if x then flip over x axis
if (axis == "x"):
toadd = curr
symptoadd = self.tosymp(toadd)
symptoadd = symptoadd * (-1)
self.flist.append(str(symptoadd))
# else if y flip over y axis
else:
toadd = curr
symptoadd = self.tosymp(toadd)
symptoadd = symptoadd.replace(x,-1*x)
self.flist.append(str(symptoadd))
def stretch(self, axis="x", val="2"):
print("\nStretching over the " + str(axis) + "-axis...")
curr = self.flist[len(self.flist)-1]
# if x then flip over x axis
if (axis == "x"):
toadd = curr
symptoadd = self.tosymp(toadd)
symptoadd = symptoadd * (val)
self.flist.append(str(symptoadd))
# else if y flip over y axis
else:
toadd = curr
symptoadd = self.tosymp(toadd)
symptoadd = symptoadd.replace(x,val*x)
self.flist.append(str(symptoadd))