I am trying to write a python matrix module (2.7). (I know about numpy, this is just for fun.)
My code is:
from numbers import Number import itertools test2DMat = [[1,2,3],[4,5,6],[7,8,9]] test3DMat = [[[1,2,3],[4,5,6],[7,8,9]],[[2,3,4],[5,6,7],[8,9,0]],[[9,8,7],[6,5,4],[3,2,1]]] class Dim(list): def __new__(cls,inDim):
Current Functionality:
So far I have written several classes, Matrix , Dim and Vec . Matrix and Vec are both subclasses of Dim . When creating a matrix, you would first need to make a list of lists, and they would create a matrix such as:
>>> startingList = [[1,2,3],[4,5,6],[7,8,9]] >>> matrix.Matrix(startingList) [[1,2,3],[4,5,6],[7,8,9]]
This should create a Matrix . The created Matrix must contain several Dim all of the same length. Each of these Dim must contain several Dim all of the same length, etc. The last Dim that contains numbers should only contain numbers and should be Vec instead of Dim .
Problem:
All this works for lists. If I, however, would instead use an iterator object (e.g. returned by iter() ), this does not work the way I want.
For instance:
>>> startingList = [[1,2,3],[4,5,6],[7,8,9]] >>> matrix.Matrix(iter(startingList)) []
My thoughts:
I'm sure this happens because in Dim.__new__ I repeat the iteration of input, which, when the same number is then passed to Matrix.__init__ , is already renamed and therefore will be empty, resulting in an empty matrix.
I tried copying the iterator using itertools.tee() , but this also does not work, because I do not actually call Matrix.__init__ , it gets an implicit expression when Matrix.__new__ returned, and therefore I can not name it with different parameters than those passed to Matrix.__init__ . All that I came up with is the same problem.
Is there a way to preserve the existing functionality and also allow the call of matrix.Matrix() with an iterator object?