1

i try this for matrix 2x2 and matrix 3x3 but i would like to use some loops like for or while.

from libmatrice import det2, det3, comatrice, transposee, inverse

reponse = input('Quelle est la dimension de la matrice à inverser (2 ou 3) ?\n')

determinant = 0

if reponse == '2':
    print('''Votre matrice est de dimension 2 (2 lignes et 2 colonnes)''')

    A11 = int(input('''Quel est votre premier chiffre ?\n'''))
    A12 = int(input('''Quel est votre deuxième chiffre ?\n'''))

    A21 = int(input('''Quel est votre troisième chiffre ?\n'''))
    A22 = int(input('''Quel est votre quatrième chiffre ?\n'''))

    determinant = det2(A11, A12, A21, A22)
    print('''Le déterminant de votre matrice 2x2 est : ''', determinant)

elif reponse == '3':
    print('''Votre matrice est de dimension 3 (3 lignes et 3 colonnes)''')

    A11 = int(input('''Quel est votre premier chiffre ?\n'''))
    A12 = int(input('''Quel est votre deuxième chiffre ?\n'''))
    A13 = int(input('''Quel est votre troisème chiffre ?\n'''))

    A21 = int(input('''Quel est votre quatrième chiffre ?\n'''))
    A22 = int(input('''Quel est votre cinquième chiffre ?\n'''))
    A23 = int(input('''Quel est votre sixième chiffre ?\n'''))

    A31 = int(input('''Quel est votre septième chiffre ?\n'''))
    A32 = int(input('''Quel est votre huitième chiffre ?\n'''))
    A33 = int(input('''Quel est votre neuvième chiffre ?\n'''))

    determinant = det3(A11, A12, A13, A21, A22, A23, A31, A32, A33)

print(determinant)

    comatrice = comatrice(A11, A12, A13, A21, A22, A23, A31, A32, A33) 

    print('''Votre comatrice est : ''', comatrice)

    detA11 = comatrice[0]
    detA12 = comatrice[1]
    detA13 = comatrice[2]
    detA21 = comatrice[3]
    detA22 = comatrice[4]
    detA23 = comatrice[5]
    detA31 = comatrice[6]
    detA32 = comatrice[7]
    detA33 = comatrice[8]

    transpose = transposee(detA11, detA21, detA31, detA12, detA22, detA32,detA13, detA23, detA33)

    TA11 = transpose[0]
    TA12 = transpose[1]
    TA13 = transpose[2]
    TA21 = transpose[3]
    TA22 = transpose[4]
    TA23 = transpose[5]
    TA31 = transpose[6]
    TA32 = transpose[7]
    TA33 = transpose[8]

    print('''La matrice inverse est : ''', inverse(determinant, TA11, TA21, TA31, TA12, TA22, TA32, TA13, TA23, TA33))

else:
    pass

And i also use a library but because i don't use loop, i don't need def reduit(matrix)

def det2(A11, A12, A21, A22):    
    diagonale_1 = A11*A22
    diagonale_2 = A12*A21
    resultat = diagonale_1 - diagonale_2
    return resultat

def det3(A11, A12, A13, A21, A22, A23, A31, A32, A33):
    #pour A11:
    diagonale_A11_1 = A22 * A33
    diagonale_A11_2 = A32 * A23
    resultat_A11 = diagonale_A11_1 - diagonale_A11_2
    #pour A12:
    diagonale_A12_1 = A21 * A33
    diagonale_A12_2 = A31 * A23
    resultat_A12 = diagonale_A12_1 - diagonale_A12_2
    #pour A13:
    diagonale_A13_1 = A21 * A32
    diagonale_A13_2 = A31 * A22
    resultat_A13 = diagonale_A13_1 - diagonale_A13_2
    #resultat final:
    resultat_final = A11 * resultat_A11 - A12  * resultat_A12 + A13 * resultat_A13
    return resultat_final


def reduit(matrice):
    pass

def comatrice(A11, A12, A13, A21, A22, A23, A31, A32, A33):
    detA11 = A22 * A33 - A32 * A23
    detA12 = -(A21 * A33 - A31 * A23)
    detA13 = A21 * A32 - A31 * A22
    detA21 = -(A12 * A33 - A32 * A13)
    detA22 = A11 * A33 - A31 * A13
    detA23 = -(A11 * A32 - A31 * A12)
    detA31 = A12 * A23 - A22 * A13
    detA32 = -(A11 * A23 - A21 * A13)
    detA33 = A11 * A22 - A21 * A12
    return detA11, detA12, detA13, detA21, detA22, detA23, detA31, detA32, detA33


def transposee(detA11, detA12, detA13, detA21, detA22, detA23, detA31, detA32, detA33):
    TA11 = detA11
    TA21 = detA12
    TA31 = detA13
    TA12 = detA21
    TA22 = detA22
    TA32 = detA23
    TA13 = detA31
    TA23 = detA32
    TA33 = detA33
    return TA11, TA21, TA31, TA12, TA22, TA32, TA13, TA23, TA33

def inverse(det, TA11, TA12, TA13, TA21, TA22, TA23, TA31, TA32, TA33):
    invTA11 = TA11 // det
    invTA21 = TA21 // det
    invTA31 = TA31 // det
    invTA12 = TA12 // det
    invTA22 = TA22 // det
    invTA32 = TA32 // det
    invTA13 = TA13 // det
    invTA23 = TA23 // det
    invTA33 = TA33 // det
    return invTA11, invTA12, invTA13, invTA21, invTA22, invTA23, invTA31, invTA32, invTA33

Do you have any idea in order to be more "clean" in my code in order to integrate some loop in my second code ? The first block of code is called matrice.py witch is the interface where the program in python is asking if it's a 2x2 dimensions or 3x3 dimensions.

The libmatrice is import from libmatrice.py

Tluabiht
  • 11
  • 1
  • 2
  • Hello @Tluabiht, welcome to StackOverflow ! As your question appears to be more related to code reviewing than programming, I would ask your question to this site instead https://codereview.stackexchange.com/ :) – Jacquot Nov 22 '18 at 13:35
  • Does this answer your question? [Matrix inversion without Numpy](https://stackoverflow.com/questions/32114054/matrix-inversion-without-numpy) – Yogev Neumann Nov 26 '22 at 23:14

1 Answers1

0

you can work with for loops of you represent your matrices as lists (of lists):

A = [[1, 2], [3, 4]]

def det2(A):
    return A[0][0]*A[1][1] - A[0][1]*A[1][0]

def inv2(A):
    d = det2(A)
    return [[A[1][1]/d, -A[0][1]/d], [-A[1][0]/d, A[0][0]/d]]


print(det2(A))  # 2
print(inv2(A))  # [[-2.0, 1.0], [1.5, -0.5]]

an for 3 dimensions:

B = [[1, 21, 3], [3, 4, 5], [5, 6, 9]]

def det3(B):
    ret = 0
    for i in range(3):
        pos = 1
        neg = 1
        for j in range(3):
            pos *= B[j][(i+j) % 3]
            neg *= B[j][(i-j) % 3]
        ret += (pos - neg)

    return ret

def inv3(B):
    ret = [3*[None] for _i in range(3)]
    det = det3(B)
    for i in range(3):
        for j in range(3):
            adj = [[n for ii, n in enumerate(row) if ii != i] 
                      for jj, row in enumerate(B) if jj != j]
            d = det2(adj)
            sgn = (-1)**(i+j)
            ret[i][j] = sgn * d / det
    return ret

print(det3(B))  # -42
print(inv3(B))
# [[-0.14285714285714285, 4.071428571428571, -2.2142857142857144], 
#  [0.047619047619047616, 0.14285714285714285, -0.09523809523809523], 
#  [0.047619047619047616, -2.357142857142857, 1.4047619047619047]]

note that the determinant for 2 dimensions is almost trivial; for the 3 dimensional one i used the rule of sarrus.

that might get you started on how to do the general case.

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111