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