def save(self, output): file = open(output, "w") file.write(".".join(",".join(str(self.MATRIX[i][j]) for j in range(self.COL)) for i in range(self.ROW))) file.close() def load(input): file = open(input,"r") contents = file.read().split(".") file.close() M = [[contents[i].split(",")[j] for j in range(len(contents[i].split(",")))] for i in range(len(contents))] def nom(m): if (not "/" in m): return int(m) return int(m[:m.index("/")]) def den(m): if (not "/" in m): return 1 return int(m[m.index("/")+1:]) return Matrix([[Rational(nom(M[i][j]), den(M[i][j])) for j in range(len(M[i]))] for i in range(len(M))])
Input and output look like this
I could use other characters than , and .
OOP speaking, does it make sense to include those 2 functions inside the Matrix class?
Also, would it be reasonable to implement a Vector class as a Matrix of one row? Is a vector a matrix, in other words? Or make an entire separate class?
This post has been edited by carnivroar: 26 July 2012 - 12:30 PM