# Python code to demonstrate matrix operations # importing numpy for matrix operations import numpy as np # initializing matrices x = np.array([[1, 2], [4, 5]]) y = np.array([[7, 8], [9, 10]]) print(x) print(y) #create a matrix based on aray a = np.arange(1, 21) np.reshape(a,(5, 4)) # add(), subtract() and divide() # using add() to add matrices print ("The element wise addition of matrix is : ") print (np.add(x,y)) print(x+y) # using subtract() to subtract matrices print ("The element wise subtraction of matrix is : ") print (np.subtract(x,y)) print (x-y) # using divide() to divide matrices print ("The element wise division of matrix is : ") print (np.divide(x,y)) print(x/y) # sqrt(), sum() and "T" # using sqrt() to print the square root of matrix print ("The element wise square root is : ") print (np.sqrt(x)) # using sum() to print summation of all elements of matrix print ("The summation of all matrix element is : ") print (np.sum(y)) np.cumsum(y)# somme cumulee # using sum(axis=0) print summation of each column of matrix print ("The column wise summation of all matrix is : ") print (np.sum(y,axis=0)) # using sum(axis=1) print summation of each row of matrix print ("The row wise summation of all matrix is : ") print (np.sum(y,axis=1)) np.mean(y) #Moyenne des colonnes : np.mean(y,axis=0) # moyenne en colonne np.var(x) #Moyenne des lignes : np.mean(y,axis=1) # moyenne en ligne # using "T" to transpose the matrix print ("The transpose of given matrix is : ") print (x.T) #OU print(np.transpose(x)) print(np.linalg.inv(x))# matrice inverse print(np.linalg.eig(x))#valeur propre et les vect propre de la matrice vals, vecs = np.linalg.eig(x) print("Valeurs propres:", vals) print("Vecteurs propres:\n", vecs) det = np.linalg.det(x)# calculating the determinant of matrix print(det) print("Trace de x:", np.trace(x))#somme daigonal np.shape(x)#dimintion of matrix len(x)# numbres of row len(x[0]) # numbres of colon m1=np.zeros((10,10)) print(m1) m2=np.ones((4,2)) m2 l=np.eye(3);l#la matrix identité b=np.array([ [1,2,3], [2,3,6], [6,5,8] ]) c=np.array([ [3,1,2], [6,1,1], [3,2,5] ]) print(b);print(c) print(2*b) print(2+b) print(b**2)#Puissance élément par élément # matrix multiplication print(b.dot(c)) print(np.dot(b,c)) print(np.matmul(b,c)) print(b *c) #Vertical concatenation of matrices b and c print(np.vstack((b,c))) print(np.concatenate((b,c), axis=0)) print(np.r_[b,c]) #Horizontal concatenation of matrices b and c print(np.hstack((b,c))) print(np.concatenate((b,c), axis=1)) print(np.c_[b,c]) #Résolution d’un système d’équations linéaires a = np.array([[3,1], [1,2]]) b = np.array([9,8]) x = np.linalg.solve(a, b) x #Pour vérifier que la solution est correcte : np.allclose(np.dot(a, x), b) np.dot(a,x) A=np.array([[2, 5, 7],[1, 2, 3], [7, 8, 7]]) A np.min(A) np.max(A) A[0,0]#renvoie le coefficient a_i-1,j-1 A[2,2] A[:,0]# accès première colonne A[0,]# accès première linge A[2,] ; A[2,:] print(A<3) print(A[A<3]) A[A<3]=10 print(A) A[0,0]=0 print(A) colonne=A[:,1] np.diagonal(A) np.diag([1, 2, 3], k=0) #TP # 1) Créer le vecteur a contenant les éléments de 1 à 20 # 2) Créer x1 avec première ligne : 1, 6, 11, 16 => donc a en colonne (by column) x1 = a.reshape((5, 4), order='F') # 'F' = Fortran-style (colonne) # 3) Créer x2 avec première ligne : 1, 2, 3, 4 => donc a en ligne (by row) x2 = a.reshape((5, 4), order='C') # 'C' = C-style (ligne) # 4) Transposé de x2 x3 = x2.T # 5) Produit matriciel entre x2 et x3 b = np.matmul(x2, x3) # 6) Dimension des matrices x1 et x3 dim_x1 = x1.shape dim_x3 = x3.shape # 7) Élément [3,2] de b (rappel: indexation commence à 0 en Python) element_3_2 = b[2, 1] # 8) Deuxième colonne de b col2_b = b[:, 1] # 9) Troisième et quatrième lignes de b rows_3_4_b = b[2:4, :] # 10) Supprimer la deuxième ligne de b b_no_row2 = np.delete(b, 1, axis=0) # 11) Supprimer la deuxième et quatrième colonnes de b b_no_cols_2_4 = np.delete(b, [1, 3], axis=1) # 12) Concaténation verticale de x1 et x2 v_concat = np.vstack((x1, x2)) # 13) Concaténation horizontale de x1 et x3 h_concat = np.hstack((x1, x3)) # 14) Somme de x1 par colonne sum_cols_x1 = np.sum(x1, axis=0) # 15) Somme de x1 par ligne sum_rows_x1 = np.sum(x1, axis=1) # 16) Créer mat2 mat2 = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 10] ]) # 17) mat2 est-elle inversible ? det_mat2 = np.linalg.det(mat2) is_invertible = det_mat2 != 0 # 18) Éléments propres de mat2 eigenvalues, eigenvectors = np.linalg.eig(mat2) # 19) Résoudre mat2 * x = B, avec B = [10, 7, 6] B = np.array([10, 7, 6]) x_solution = np.linalg.solve(mat2, B) # Résumé affichage (optionnel) print("Solution du système mat2 * x = B :", x_solution)