# Column and row numbers start with 0

 

class Matrix(object):

    def __init__(self, rows, cols):

        self.rows = rows

        self.cols = cols

        # initialize matrix and fill with zeroes

        self.data = [[0 for _ in range(cols)] for _ in range(rows)]

 

    def sumRow(self, row):

        return sum(self.data[row])

 

    def sumCol(self, col):

        return sum([row[col] for row in self.data])

 

    def sumDiag(self, startCol):

        colList = [startCol+i for i in range(self.cols)]

        for i, item in enumerate(colList):

            if item > self.cols-1:

                colList[i] -= self.cols

        return sum([row[col] for row,col in zip(self.data, colList)])

 

    def sumDiag2(self, startCol):

        num = max(self.cols, self.rows)

        colList = [startCol+i for i in range(num)]

        rowList = range(num)

        for i in range(len(colList)):

            while colList[i] > self.cols-1:

                colList[i] -= self.cols

        for i in range(len(rowList)):

            while rowList[i] > self.rows-1:

                rowList[i] -= self.rows

        return sum([self.data[row][col] for row,col in zip(rowList, colList)])

       

    def __setitem__(self, pos, v):

        self.data[pos[0]][pos[1]] = v

 

    def __getitem__(self, pos):

        return self.data[pos[0]][pos[1]]

 

    def __iter__(self):

        for row in self.data:

            yield row

 

    def __str__(self):

        outStr = ""

        for i in range(self.rows):

            outStr += 'Row %s = %s\n' % (i, self.data[i])

        return outStr

 

    def __repr__(self):

        return 'Matrix(%d, %d)' % (self.rows, self.cols)

 

'''

>>> import random

>>> b = Matrix(4,8)

>>> for row in b:

... for i in range(b.cols):

...     row[i] = random.randrange(100)

...     

>>> b

Matrix(4, 8)

>>> print b

Row 0 = [77, 61, 7, 76, 66, 88, 74, 7]

Row 1 = [87, 2, 52, 42, 99, 55, 6, 16]

Row 2 = [19, 42, 95, 89, 17, 59, 71, 12]

Row 3 = [87, 12, 76, 55, 49, 56, 5, 0]

 

>>> b.sumCol(3)

262

>>> b.sumRow(0)

456

>>> sum([77, 61, 7, 76, 66, 88, 74, 7])

456

>>> b.sumDiag(0)

229

>>> b.sumDiag2(0)

421

>>> b.sumDiag2(3)

451

>>>

'''

 

'''

a = Matrix(4,12)

print a

a[3,3] = 55.75

print a

a[2,3] = 19.1

 

print a

print a[2,3]

 

print

for row in a:

    print row

   

print

x = 0.75

for row in a:

    for i in range(a.cols):

        x = x*1.25

        print x

        row[i] = x

 

print a   

'''