How can I create this matrix using python ?
I've already created S , T , X ,W ,Y and Z as well as the first and the last line of L.
How can I create this matrix using python ?
I've already created S , T , X ,W ,Y and Z as well as the first and the last line of L.
Something like this (it's a draft!). Make a class that stores 3 lists (diagonal, upper diagonal and lower diagonal) and expose a way of editing those values.
class TBMatrix:
def __init__(self,size):
self._size = size #Has to be square I guess?
self._diagonal = [None for i in range(0,size)]
self._upper_diagonal = [None for i in range(0,size - 2)]
self._lower_diagonal = [None for i in range(0,size - 2)]
def get(self,row,col):
if row == col:
return self._diagonal[row]
if row == col - 1:
return self._lower_diagonal[col]
if row == col + 1:
return self._upper_diagonal[row]
return 0 #or None, if you want a matrix that contains objects
def set(self,row,col,value):
if row == col:
self._diagonal[row] = value
elif row == col - 1:
self._lower_diagonal[col] = value
elif row == col + 1:
self._upper_diagonal[row] = value
else:
#No effect, maybe you want to throw an exception?
pass
This is a quick draft, you'll need to do a bunch of checks to make sure there is no trying to assign an index outside of the list sizes. But this should get you started.
Another alternative is to override the __getitem__
and __setitem__
to return a row full of 0's or None
except where it need to hold a spot for self._diagonal
, self._upper_diagonal
and self._lower_diagonal
. But it just seems more complicated.