-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_of_linear_transformations.py
194 lines (127 loc) · 6.57 KB
/
copy_of_linear_transformations.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
# -*- coding: utf-8 -*-
"""Copy of Linear_transformations.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1sWUkY6d2hV_Hu0XWB59QidDATs9ts_AZ
#**Lab 8 - Linear Transformations**
Enter your code in the spaces provided. Do not change any of the variable names or function names that are already provided for you. In places where we specify the name of the return value, make sure that your code produces the a value with the correct name.
"""
# Do not edit this cell.
LabID="Lab8"
try:
from graderHelp import ISGRADEPLOT
except ImportError:
ISGRADEPLOT = True
"""**Enter your name, section number, and BYU NetID**"""
# Enter your first and last names in between the quotation marks.
first_name="Madison"
last_name="Wozniak"
# Enter your Math 215 section number in between the quotation marks.
section_number="001"
# Enter your BYU NetID in between the quotation marks. NOT YOUR BYU ID NUMBER!
BYUNetID="mwozniak"
"""**Import NumPy**"""
import numpy as np
"""**Problem 1**"""
# This function reads in a vector and outputs the transformed vector Ax.
def transform(x):
A = np.array([[2,1],[1,-3],[0,1]])
Transformed = np.matmul(A,x)
return Transformed
transform([1,2])
"""**Downloading and visualizing our data**
The simplest way to load the data into Colab is to first download it as a .csv file to your local computer by clicking the link
https://drive.google.com/uc?export=download&id=1tWus0zXDSahrms0dl8TTV0Pl8bbWlx9j
This will allow you to download the data as a .csv file. In the top left corner of this screen you should see a little file folder icon. Selecting it opens a new window to the left of the notebook with three tabs: "Upload", "Refresh", and "Mount Drive". Select "Upload". This should bring up a window that allows you to select the file "cougar.csv" from your local machine, which will upload the file to your notebook. You will need to do this again if you decide to close your notebook and reopen it at a later time.
Once you've uploaded your file, convert it to a NumPy array called "cougar" by executing the following cell.
"""
import pandas as pd
from matplotlib import pyplot as plt
df = pd.read_csv('cougar.csv')
cougar=df.values
cougar
"""Now let's define a function that we can use to plot the array cougar that we've just created (or any other array of points which we'd like to visualize)."""
def showplot(H):
# This function displays the image produce by the collection of coordinates given in H
cougarplot=plt.plot(H[0,:],H[1,:],'k.',markersize=3.5)
plt.axis([-1.5,1.5,-1.5,1.5])
plt.gca().set_aspect("equal")
plt.show()
return None
# Let's test the function above by plotting the data in our NumPy array cougar
showplot(cougar)
"""**Problem 2**"""
# This function should take a matrix of coordinates, and output a matrix which corresponds to the image that has been stetched in the
# horizantal direction by a factor of a and in the verticle direction by a factor of b.
def stretch(image,a,b):
stretcher = np.array([[a,0],[0,b]])
return np.matmul(stretcher,image)
# This function should take a matrix of coordinates, and output a matrix which corresponds to the image that has been sheared by a horizantal factor of a and
# a vertical factor of b
def shear(image,a,b):
shearer = np.array([[1,a],[b,1]])
return np.matmul(shearer,image)
# This function should take a matrix of coordinates, and output a matrix which corresponds to the image that has been reflected in the
# line spanned by the vector [a,b]^T
def reflect(image,a,b):
reflector = (1/((a**2)+(b**2)))*np.array([[(a**2)-(b**2),2*a*b],[2*a*b,(b**2)-(a**2)]])
return np.matmul(reflector,image)
# This function should take a matrix of coordinates, and output a matrix which corresponds to the image that has been rotated in the
# counterclockwise direction by an angle of theta radians.
def rotate(image,theta):
rotator = np.array([[np.cos(theta),-np.sin(theta)],[np.sin(theta),np.cos(theta)]])
return np.matmul(rotator,image)
"""**Problem 3**"""
iden=np.array([[1,0],[0,1]]) # The identity matrix may be helpful here.
comp_matrix= stretch(reflect(rotate(stretch(iden,1,2),-np.pi/4),2,-3),1/2,1)
"""The following code can be copied to your practice notebook and used to visualize the linear transformations in the following questions."""
from matplotlib import pyplot as plt
import numpy as np
def plot_transformation(mat):
plt.cla()
e1=np.array([1,0])
e2=np.array([0,1])
v3=e1+e2
plot_lims=abs(np.array([[0,1],mat@e1,mat@v3,mat@e2])).max()
trans_square=plt.Polygon([mat@e1,mat@v3,mat@e2,[0,0]],ec=(0,0,1,1),fc=(0,0,1,0.5))
orig_square=plt.Polygon([e1,v3,e2,[0,0]],fc=(1,0,0,0.5),ec=(1,0,0,1))
plt.gca().add_patch(trans_square)
plt.gca().add_patch(orig_square)
plt.axis('scaled')
plt.ylim(-1.05*plot_lims,1.05*plot_lims)
plt.xlim(-1.05*plot_lims,1.05*plot_lims)
ax=plt.gca()
ax.spines['top'].set_position(('data',0))
ax.spines['right'].set_position(('data',0))
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
plt.show()
return
"""**Problem 4**"""
def area(A):
v = np.array(A[:,0])
w = np.array(A[:,1])
angle = np.arccos(np.dot(v,w)/(np.linalg.norm(v)*np.linalg.norm(w)))
return np.linalg.norm(v)*np.linalg.norm(w)*np.sin(angle)
D = np.array([[1,3],[2,-1]])
area(D)
"""**Problem 5**"""
# Save the values of the determinants of the matrices A,B,C, and D from Problem 5.
detM=10
detN=-5
detP=3
detQ=-0.2
"""**Problem 6**"""
# Save the values of the matrix A and the area of the ellipse from Problem 6.
ellipse_matrix=np.array([[1.5,1],[0.5,-2.75]])
ellipse_area=4.625*np.pi
"""**Problem 7**"""
# Save the values of the determinants of the matrices B1,B3,B6, and B0 from Problem 5.
detB1=18
detB3=3
detB6=0.00006
detB0=0
"""**STOP! BEFORE YOU SUBMIT THIS LAB:** Go to the "Runtime" menu at the top of this page, and select "Restart and run all". If any of the cells produce error messages, you will either need to fix the error(s) or delete the code that is causing the error(s). Then use "Restart and run all" again to see if there are any new errors. Repeat this until no new error messages show up.
**You are not ready to submit until you are able to select "Restart and run all" without any new error messages showing up. Your code will not be able to be graded if there are any error messages.**
To submit your lab for grading you must first download it to your compute as .py file. In the "File" menu select "Download .py". The resulting file can then be uploaded to http://www.math.byu.edu:30000 for grading.
"""