Skip to content

Instantly share code, notes, and snippets.

Created July 11, 2013 17:14
Show Gist options
  • Save anonymous/5977331 to your computer and use it in GitHub Desktop.
Save anonymous/5977331 to your computer and use it in GitHub Desktop.
test
# 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")
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment