Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/my_matrix.py
Last active December 19, 2015 15:39
Show Gist options
  • Save zeffii/5977834 to your computer and use it in GitHub Desktop.
Save zeffii/5977834 to your computer and use it in GitHub Desktop.
# my_matrix.py
class Matrix:
def __init__(self, T, N):
self.T = T # All fields (list)
self.N = N # (n*n)
self.as_2d_list = self.vec_to_mat()
def vec_to_mat(self):
rows, cols = self.N
return [[self.T[(j*cols)+i] for i in range(cols)] for j in range(rows)]
def __str__(self):
type_mat = "Matrix({}*{}) = \n".format(*self.N)
rows, cols = self.N
vals_mat = str(self.vec_to_mat())
mat_rep = ''.join([type_mat, vals_mat])
return mat_rep.replace("],", "],\n")
def __mul__(self, vec):
assert len(vec) == self.N[0]
ret_list = []
for i, n in enumerate(self.as_2d_list):
ret_list.append(sum([vec[i]*c for c in n]))
print("rawrrr, good")
return tuple(ret_list)
v = (1.2, 0.2, 0.6)
a = Matrix(T=[1.0, 0.3, 0.3, 2.0, 4.0, 0.3, 1.0, 0.2, 0.5], N=(3,3))
print(a)
print(a.as_2d_list)
print(v)
print(a*v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment