Aufgabe 11: (Python-Klasse Matrix zum Rechnen mit Matrizen)
Schreiben Sie eine Klasse Matrix zur Repräsentation einer Matrix und implementieren Sie die Methoden für Addition, Subtraktion und Multiplikation von Matrizen.
Ihre Implementierung soll auch mit Zahlen vom Typ ModInt funktionieren.
class Matrix(list):
def __init__(self, lst):
self[:]=lst
def __add__(self, other):
def __sub__(self, other):
def __mul__(self, other):
@staticmethod
def make(m, n, z=0):
return Matrix([[z for j in range(n)] for i in range(m)])
def out(self):
m=len(self)
n=len(self[0])
for i in range(m):
for j in range(n):
print self[i][j],
print
print
if __name__=="__main__":
from ModInt import *
ModInt.n=17
lst0=[[ModInt(1),ModInt(2)], [ModInt(3),ModInt(4)]]
a=Matrix(lst0)
a.out()
lst1=[[ModInt(1)],[ModInt(3)]]
b=Matrix(lst1)
b.out()
c=a*b
c.out()