matrice.py

Created by maeldepallens

Created on September 14, 2022

3.76 KB


# Type your text here
def print_matrice(matrice) :
    n = dim_matrice(matrice)[0]
    p = dim_matrice(matrice)[1]
    for i in range(n) :
        ligne = "("
        for j in range(p) :
            if len(str(matrice[i][j])) == 1:
                ligne += "   "+str(matrice[i][j])+"   "
            elif len(str(matrice[i][j])) == 2:
                ligne += "  "+str(matrice[i][j])+"   "
            elif  len(str(matrice[i][j])) == 3:
                ligne += "  "+str(matrice[i][j])+"  "
            elif len(str(matrice[i][j])) == 4:
                ligne += "  "+str(matrice[i][j])+" "
            else :
                ligne += " "+str(matrice[i][j])+" "
        ligne+=")"
        print(ligne)

def crea_matrice(n,p):
    matrice = []
    for i in range(n) :
        liste = []
        for i in range(p):
            liste.append(0)
        matrice.append(liste)
    return matrice

def matrice(n,p) :
    matrice = crea_matrice(n,p)
    for i in range(n) :
        for j in range(p):
            coeff = int(input("coefficient ligne "+str(i+1)+" colonne "+str(j+1)+" : "))
            matrice[i][j] = coeff
    return matrice


def dim_matrice(matrice) :
    if def_matrice(matrice) :
        return [len(matrice),len(matrice[0])]
    else :
        return "Erreur"

def def_matrice(matrice) :
    for i in range(len(matrice)-1) :
        if len(matrice[i]) != len(matrice[i+1]) :
            return False
    return True

def matrice_diagonale(matrice,n,p) :
    for i in range(n) :
        for j in range(p) :
            if i!=j and matrice[i][j] != 0 :
                return False
    return True


def add_matrice(A,B) :
    if dim_matrice(A) == dim_matrice(B) :
        C =  crea_matrice(dim_matrice(A)[0],dim_matrice(A)[1])
        for i in range(dim_matrice(A)[0]) :
            for j in range(dim_matrice(A)[1]) :
                C[i][j] = A[i][j]+B[i][j]
        return C


def matrice_fois_k(k,matrice) :
    for i in range(len(matrice) ):
        for j in range(len(matrice[0])):
            matrice[i][j] = k*matrice[i][j]
    return matrice


def multiplication_matrice(A,B) :
    if dim_matrice(A)[1] == dim_matrice(B)[0] :
        C = crea_matrice(dim_matrice(A)[0],dim_matrice(B)[1])
        for i in range(dim_matrice(A)[0]) :
            for j in range(dim_matrice(B)[1]) :
                S = 0
                S1 = "C("+str(i+1)+","+str(j+1)+")  = "
                S2 = "C("+str(i+1)+","+str(j+1)+")  = "
                print("C("+str(i+1)+","+str(j+1)+")  = Σ(k=1,"+str(dim_matrice(A)[1])+") a("+str(i+1)+",k)*b(k,"+str(j+1)+")")
                
                for k in range(dim_matrice(A)[1]) :
                    S1 +="a("+str(i+1)+","+str(k+1)+")*b("+str(k+1)+","+str(j+1)+")  + "
                    S2 += str(A[i][k])+"*"+str(B[k][j])+" + "
                    S+=A[i][k]*B[k][j]
                print(S1[:-2])
                print(S2[:-2])
                print( "C("+str(i+1)+","+str(j+1)+")  = ",S)
                C[i][j] = S
                print("")
        
        print_matrice(C)
    else :
        print("le produit matriciel n'est pas définie impossible")


def tansposer(matrice) :
    new_matrice = crea_matrice(dim_matrice(matrice)[1],dim_matrice(matrice)[0])
    for i in range(dim_matrice(matrice)[0]) :
        for j in range(dim_matrice(matrice)[1]) :
            new_matrice[j][i] = matrice[i][j]
    return new_matrice



print("bonjour binevenue dans le programme pour calculer une multiplication entre 2 matrice : ")
print("")
print("")
n = int(input("nombre de ligne 1er matrices : "))
p =  int(input("nombre de colonne 1er matrices : "))
print("")
A = matrice(n,p)
print("")
print("")
n2 = int(input("nombre de ligne 2e matrices : "))
p2 =  int(input("nombre de colonne 2e matrices : "))
print("")
B = matrice(n2,p2)
print("")
print("")
multiplication_matrice(A,B)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.