Skip to content

Commit ec85eef

Browse files
author
Franklin Dyer
committed
added some new vector operations
1 parent 818458c commit ec85eef

File tree

1 file changed

+65
-10
lines changed

1 file changed

+65
-10
lines changed

rhinopython/Matrix.py

+65-10
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,21 @@ def print(self):
6262

6363
for row in self.entries: print(row)
6464

65+
def transpose(self):
66+
67+
entries = [[self.entries[i][j] for i in range(self.rows)] for j in range(self.cols)]
68+
return Matrix(entries)
69+
70+
def vectorize(self):
71+
72+
if self.cols != 1:
73+
raise Exception("Only Matrices with one column can be converted to column Vectors")
74+
else:
75+
return Vector([row[0] for row in self.entries])
76+
6577
def __add__(a, b):
6678

67-
if type(a) != Matrix or type(b) != Matrix:
79+
if not (isinstance(a, Matrix) and isinstance(b, Matrix)):
6880
raise Exception("Matrices can only be added to other matrices")
6981
elif a.rows != b.rows or a.cols != b.cols:
7082
raise Exception("Matrices with different dimensions cannot be added")
@@ -74,19 +86,23 @@ def __add__(a, b):
7486
ae = a.entries
7587
be = b.entries
7688
entries = [[ae[i][j] + be[i][j] for j in range(c)] for i in range(r)]
77-
return Matrix(entries)
89+
result = Matrix(entries)
90+
if result.cols == 1: result = result.vectorize()
91+
return result
7892

7993
def __mul__(a, b):
8094

81-
if type(a) != Matrix or type(b) != Matrix:
95+
result = False
96+
97+
if not (isinstance(a, Matrix) and isinstance(b, Matrix)):
8298

8399
if is_number(a):
84100
entries = [[entry * a for entry in row] for row in b.entries]
85-
return Matrix(entries)
101+
result = Matrix(entries)
86102

87103
elif is_number(b):
88104
entries = [[entry * b for entry in row] for row in a.entries]
89-
return Matrix(entries)
105+
result = Matrix(entries)
90106

91107
else:
92108
raise Exception("Only matrix-matrix and matrix-scalar multiplication are supported")
@@ -103,12 +119,19 @@ def __mul__(a, b):
103119
b_col = b.col_at(j)
104120
entry = sum([a_row[n] * b_col[n][0] for n in range(length)])
105121
entries[i][j] = entry
106-
return Matrix(entries)
122+
result = Matrix(entries)
123+
124+
if result.cols == 1: result = result.vectorize()
125+
return result
107126

108127
def __rmul__(a, b):
109128

110129
return Matrix.__mul__(b, a)
111130

131+
def __sub__(a, b):
132+
133+
return a + (-1)*b
134+
112135
def column(entries):
113136

114137
return Matrix([[e] for e in entries])
@@ -140,10 +163,6 @@ def dilation(factor, center):
140163
t_inv = Matrix.translation(center)
141164
return t_inv * d * t
142165

143-
def rotation(theta, normal):
144-
145-
dim = len(normal)
146-
147166
class Vector(Matrix):
148167

149168
def __init__(self, coords):
@@ -171,6 +190,42 @@ def normalize(self):
171190
self.set(i, 0, val/norm)
172191
return self
173192

193+
def dot(a, b):
194+
195+
if not (isinstance(a, Vector) and isinstance(b, Vector)):
196+
raise Exception("Dot product is only supported for Vectors")
197+
elif a.cols != b.cols:
198+
raise Exception("Only Vectors of the same length can be dotted")
199+
else:
200+
return (a.transpose() * b).at(0, 0)
201+
202+
def projection(a, b):
203+
204+
if not (isinstance(a, Vector) and isinstance(b, Vector)):
205+
raise Exception("Vector projection is only supported for Vectors")
206+
elif a.cols != b.cols:
207+
raise Exception("A Vector can only be projected onto another Vector of the same length")
208+
else:
209+
a_unit = a.normalize()
210+
a_norm = a.norm()
211+
length = Vector.dot(a, b) / a_norm**2
212+
return length * a_unit
213+
214+
## this function assumes that the set of vectors it is passed is already linearly independent
215+
def gram_schmidt(vectors):
216+
217+
dim = vectors[0].cols
218+
dim_subspace = len(vectors)
219+
220+
basis_vectors = []
221+
for v in vectors:
222+
new_bv = v
223+
for w in basis_vectors:
224+
new_bv = new_bv - Vector.projection(w, v)
225+
basis_vectors.append(new_bv)
226+
227+
return [v.normalize() for v in basis_vectors]
228+
174229
class Point(Matrix):
175230

176231
def __init__(self, coords):

0 commit comments

Comments
 (0)