-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw3.py
173 lines (137 loc) · 3.49 KB
/
hw3.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
'''
Use this space to copy and paste functions you wrote in
the last class. For example:
'''
def dot(v1: list, v2: list) -> float:
'''
Parameters
----------
- v1: list
- v2: list
Returns
-------
float
Returns the dot product of v1 and v2
'''
result = 0.0
for element in range(0,len(v1)):
result += v1[element]*v2[element]
return result
# solution starts here:
def is_scalar_multiple(v1, v2):
'''
Parameters
----------
- v1: list
- v2: list
Returns
-------
True if v1 and v2 are linearly dependent, and False otherwise
'''
#basicly: go through each element of v1 and v2 and check if the same scalar multiple can be applied to each element to make them equal
#if the vectors are not the same length, they cannot be scalar multiples
if len(v1) != len(v2):
return False
#check if either vector is all zeros
for vec in [v1,v2]:
allZero = True
for i in vec:
if i != 0:
allZero = False
break
if allZero:
return True
for i in range(len(v1)):
if v1[i] == 0 and v2[i] == 0:
continue
if v1[i] == 0 or v2[i] == 0:
return False
if v1[0] / v2[0] != v1[i] / v2[i]:
return False
return True
def count_vectors(vectors):
'''
Parameters
----------
- vectors: a list of vectors (not necessarily of the same
dimension)
Returns
-------
A dictionary whose keys are the vectors, and whose values
are the number of occurances of each vector.
For example:
count_vectors([(1,0), (1,0), (2,3), (0,0), (0,)])
= {(1,0) : 2, (2,3) : 1, (0,0): 1, (0,): 1}
'''
#start by creating dictionary
dict = {}
#go through vecs and put into dict depending on if the vec is already there or not
for vec in vectors:
if vec not in dict:
dict[vec] = 1
else:
dict[vec] += 1
return dict
def reverse_dictionary(d):
'''
Parameters
----------
- d: dictionary whose values are immutable
Returns
-------
reversed dictionary, whose keys are the values of d,
and whose values are lists of keys of d.
For example:
reverse_dictionary({1 : a, 2: b, 3: b})
={a : 1, b: [2,3]}
'''
#grab the list of keys which will become the values of the new dict
keys = list(d.keys())
#grab the list of values which will become the keys of the new dict
values = list(d.values())
#create new dict
new_dict = {}
# and new keys to the new dict
for value in values:
new_dict[value] = []
#populate new dict with old dict keys
for key in keys:
new_dict[d[key]].append(key)
return new_dict
def normalize(v):
'''
Parameters
----------
- v: list representing a nonzero vector
Returns
-------
- normalized_v: list
Returns v/|v|
'''
#find the magnitude of the vector
magnitude = dot(v,v)**0.5
if magnitude == 0 or magnitude == 1:
return v
#divide each element by magnitude
for i in range(len(v)):
v[i] = v[i]/magnitude
return v
def orthogonal_projection(v, w):
'''
Parameters
----------
- v: list
- w: list (nonzero)
Returns
-------
The orthogonal projection of v onto w. Returns
two vectors, x and y such that:
1. v = x + y
2. x is parallel to w
3. y is perpendicular to w
'''
#get parallel vect by multiplying the norm of w by the dot product of v and w
parrel_vector = [i*dot(v,w) for i in normalize(w)]
#get perpendic vect by subtracting the parallel vect from v
perpendic_vector = [v[i] - parrel_vector[i] for i in range(len(v))]
return parrel_vector, perpendic_vector