Kryptografie 2 Laboraufgaben

Computerlabor   28.05.2025

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 Multi­plikation von Matrizen.

Ihre Implementierung soll auch mit Zahlen vom Typ ModInt funktionieren.

 

# repraesentiert eine Matrix
class Matrix(list):

    # Konstruktor: erzeugt aus einer zweidimensionalen
    # Liste lst ein Objekt vom Typ Matrix
    def __init__(self, lst):
        self[:]=lst

    # addiert zwei Matrizen und gibt das Ergebnis
    # als neue Matrix zurueck
    def __add__(self, other):


    # subtrahiert zwei Matrizen und gibt das Ergebnis
    # als neue Matrix zurueck
    def __sub__(self, other):

        
    # multipliziert zwei Matrizen und gibt das Ergebnis
    # als neue Matrix zurueck
    def __mul__(self, other):
        

    # Factory-Methode: erzeugt eine m x n-Matrix
    # und belegt die Eintraege mit z
    @staticmethod
    def make(m, n, z=0):
        return Matrix([[z for j in range(n)] for i in range(m)])


    # gibt eine Matrix aus
    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


# Test
if __name__=="__main__":
    from ModInt import *
    ModInt.n=17
    # 2 x 2-Matrix:
    lst0=[[ModInt(1),ModInt(2)], [ModInt(3),ModInt(4)]]
    a=Matrix(lst0)
    a.out()
    # Spaltenvektor:
    lst1=[[ModInt(1)],[ModInt(3)]]
    b=Matrix(lst1)
    b.out()
    # Matrix-Vektor-Multiplikation:
    c=a*b
    c.out()

 

 

 

 

[up]

 


H.W. Lang   mail@hwlang.de   Impressum   Datenschutz
Diese Webseiten sind größtenteils während meiner Lehrtätigkeit an der Hochschule Flensburg entstanden