-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinalg_functions_blank.py
368 lines (252 loc) · 8.65 KB
/
linalg_functions_blank.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# my_vector = Vector([1,2,3])
# print(my_vector) = vector: 1,2,3
# my_vector1 == my_vector2 : True or False
from math import sqrt, acos, pi
#####
# VECTOR CLASS
#####
class Vector(object):
CANNOT_NORMALIZE_ZERO_VECTOR = 'cannot normalize zero vector'
VECTOR_LENGTHS_NOT_EQUAL = 'vector lengths not equal'
NO_UNIQUE_PARALLEL_COMPONENT = 'no unique parallel component!'
def __init__(self, coordinates):
try:
if not coordinates:
raise ValueError
self.coordinates = tuple(coordinates)
self.dimension = len(coordinates)
except ValueError:
raise ValueError('coordinates must be non-empty')
except TypeError:
raise TypeError('coordinates must be an iterable')
def __str__(self):
return 'vector: {}'.format(self.coordinates)
def __eq__(self, v):
return self.coordinates == v.coordinates
# vector addition
def plus(self, vec):
try:
if len(self.coordinates) != len(vec.coordinates):
raise ValueError
# result =
return(Vector(result))
except ValueError:
raise ValueError('vector lengths not equal')
# vector subtraction
def minus(self, vec):
try:
if len(self.coordinates) != len(vec.coordinates):
raise ValueError
# result =
return(Vector(result))
except ValueError:
raise ValueError('vector lengths not equal')
# scalar multiplication
def scalarmultiply(self, scalar):
# result =
return(Vector(result))
def scalarmult(self, scalar):
return(self.scalarmultiply(scalar))
# vector magnitude: square root of sum of squares
def magnitude(self):
# CODE HERE
return(result)
# normalized vector (inputs considered points)
def normalized(self):
try:
# CODE HERE
return(Vector(result))
except ZeroDivisionError:
raise Exception('cannot normalize zero vector')
# direction unit vector (as values)
def direction(self):
# CODE HERE
return(dvec.coordinates)
# vector inner/dot product (sum of piecewise multiplication)
def inner(self, vec):
try:
if len(self.coordinates) != len(vec.coordinates):
raise ValueError
# CODE HERE
return(sum(temp))
except ValueError:
raise ValueError('vector lengths not equal')
def dot(self, vec):
return(self.inner(vec))
# angle between two vectors as cosine
def cosineto(self, vec):
try:
if len(self.coordinates) != len(vec.coordinates):
raise ValueError
# CODE HERE
if mags == 0:
print('notice: attempting to find cosine of 0 vector')
return(0.0)
else:
return(self.dot(vec) / mags)
except ValueError:
raise ValueError('vector lengths not equal')
# angle between two vectors as radians
def radiansto(self, vec):
try:
if len(self.coordinates) != len(vec.coordinates):
raise ValueError
# CODE HERE
return(rads)
except ValueError:
raise ValueError('vector lengths not equal')
except Exception as e:
if str(e) == self.CANNOT_NORMALIZE_ZERO_VECTOR:
raise Exception('cannot compute angle with zero vector')
def radsto(self, vec):
return(self.radiansto(vec))
# angle between two vectors as degrees
# degrees = rads * 180 / pi
def degreesto(self, vec):
return(self.radiansto(vec)*180/pi)
def degsto(self, vec):
return(self.degreesto(vec))
# check for parallel (cos = -1, 1)
def isparallel(self, vec):
return
# check for orthogonal (cos = 0)
def isorthogonal(self, vec):
return
# projection of v to b: proj_b(v) = (v dot unit_b) * unit_b
def projection(self, vec):
try:
return
except Exception as e:
if str(e) == self.CANNOT_NORMALIZE_ZERO_VECTOR:
raise Exception('no unique parallel component!')
else:
raise e
def componentparallelto(self, vec):
return
# orthogonal of v from b: v minus proj_b(v)
def orthogonal(self, vec):
try:
return
except Exception as e:
if str(e) == self.NO_UNIQUE_PARALLEL_COMPONENT:
raise Exception('no unique orthogonal component!')
else:
raise e
def componentorthogonalto(self, vec):
return(self.orthogonal(vec))
# cross product:
def cross(self, vec):
try:
if not (1 < len(self.coordinates) < 4) or not (1 < len(
self.coordinates) < 4):
raise ValueError
elif len(self.coordinates) != len(vec.coordinates):
raise ValueError
# CODE HERE
return
except ValueError:
raise ValueError('vector lengths must be equal; dim 2 or 3')
# area of triangle spanned by two vectors = 1/2 mag (a cross b)
def trianglearea(self, vec):
return
#####
# LINE CLASS
#####
# if separating classes by file...
# from vector import Vector
class Line(object):
NON_NONZERO_ELMTS_FOUND_MSG = 'No nonzero elements found'
def __init__(self, normal_vector=None, constant_term=None):
self.dimension = 2
if not normal_vector:
all_zeros = ['0']*self.dimension
normal_vector = Vector(all_zeros)
self.normal_vector = normal_vector
if not constant_term:
constant_term = 0.0
self.constant_term = constant_term
# quiz questions 1
print('addition, subtraction and scalar multiplication')
a = Vector([8.218, -9.341])
b = Vector([-1.129, 2.111])
print("Q1:",a.plus(b).coordinates)
a = Vector([7.119, 8.215])
b = Vector([-8.223, 0.878])
print("Q2:",a.minus(b).coordinates)
a = Vector([1.671,-1.012,-0.318])
b = 7.41
print("Q3:",a.scalarmult(b).coordinates)
print('')
# quiz questions 2
print('magnitude and direction')
a = Vector([-0.221,7.437])
print("mag of",a.coordinates,":",a.magnitude())
a = Vector([8.813,-1.331,-6.247])
print("mag of",a.coordinates,":",a.magnitude())
a = Vector([5.581,-2.136])
print("dir of",a.coordinates,":",a.direction())
a = Vector([1.996,3.108,-4.554])
print("dir of",a.coordinates,":",a.direction())
print('')
#quiz questions 3
print('dot product and angle')
a = Vector([7.887,4.138])
b = Vector([-8.802, 6.776])
print("Q1 dot:",a.dot(b))
a = Vector([-5.955, -4.904, -1.874])
b = Vector([-4.496, -8.755, 7.103])
print("Q2 dot:",a.dot(b))
a = Vector([3.183,-7.627])
b = Vector([-2.668,5.319])
print("Q3 rads:",a.radiansto(b))
a = Vector([7.35,0.221,5.188])
b = Vector([2.751,8.259,3.985])
print("Q4 degs:", a.degreesto(b))
print('')
# quiz questions 4
print('parallelism & orthogonality')
a = Vector([-7.579,-7.88])
b = Vector([22.737,23.64])
print("Para:",a.isparallel(b),"Orth:",a.isright(b))
a = Vector([-2.029,9.97,4.172])
b = Vector([-9.231,-6.639,-7.245])
print("Para:",a.isparallel(b),"Orth:",a.isright(b))
a = Vector([-2.328,-7.284,-1.214])
b = Vector([-1.821,1.072,-2.94])
print("Para:",a.isparallel(b),"Orth:",a.isright(b))
a = Vector([2.118,4.827])
b = Vector([0.0,0.0])
print("Para:",a.isparallel(b),"Orth:",a.isright(b))
print('')
# quiz questions 5
print('vector projections & orthogonals')
v = Vector([3.039, 1.879])
b = Vector([0.825, 2.036])
print("A proj_b(v):",v.projection(b).coordinates)
v = Vector([-9.88, -3.264, -8.159])
b = Vector([-2.155, -9.353, -9.473])
print("B orth_b(v):",v.orthogonal(b).coordinates)
v = Vector([3.009, -6.172, 3.692, -2.51])
b = Vector([6.404, -9.144, 2.759, 8.718])
print("C proj_b(v):",v.projection(b).coordinates)
print("C orth_b(v):",v.orthogonal(b).coordinates)
print('')
# quiz questions 6
print('cross products')
a = Vector([8.462, 7.893, -8.187])
b = Vector([6.984, -5.975, 4.778])
c = a.cross(b)
print('cross product vector:', c.coordinates)
print('check dot prods == 0:', 'a',a.dot(c), 'b',b.dot(c))
a = Vector([-8.987, -9.838, 5.031])
b = Vector([-4.268, -1.861, -8.866])
print('area of parallelogram:', a.trianglearea(b) * 2)
print('area of parallelogram:', a.cross(b).magnitude())
a = Vector([1.5, 9.547, 3.691])
b = Vector([-6.007, 0.124, 5.772])
print('area of triangle:', a.trianglearea(b))
print('area of triangle:', a.cross(b).magnitude()*0.5)
a = Vector([8.462, 7.893])
b = Vector([6.984, -5.975])
c = a.cross(b)
print('2D vector test!!:', c.coordinates)