0% found this document useful (0 votes)
24 views2 pages

TD Simulation

The document contains code examples for performing various matrix operations in NumPy including adding matrices, multiplying a matrix by a scalar, matrix multiplication, finding the diagonal sum, row and column sums, checking if a matrix is triangular, transposing a matrix, and creating a matrix with 1s along the anti-diagonal.

Uploaded by

Mustapha mejri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

TD Simulation

The document contains code examples for performing various matrix operations in NumPy including adding matrices, multiplying a matrix by a scalar, matrix multiplication, finding the diagonal sum, row and column sums, checking if a matrix is triangular, transposing a matrix, and creating a matrix with 1s along the anti-diagonal.

Uploaded by

Mustapha mejri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#TD simulation numérique

#Exercice 1
-------------------------------------------------------------------
import numpy as np
def Saisie(l,c):
M=np.zeros((l,c))
for i in range(l):
for j in range(c):
M[i,j]=int(input("Donnez l'elt {},{}".format(i,j)))
return M
def Somme(A,B):
C=np.zeros(A.shape)
for i in range(C.shape[0]):
for j in range(C.shape[1]):
C[i,j]=A[i,j]+B[i,j]
return C
if __name__=='__main__':
la=int(input("Ligne A:"))
ca=int(input("Colonne A:"))
lb=int(input("Ligne B:"))
cb=int(input("Colonne B:"))
A=Saisie(la,ca)
B=Saisie(lc,cb)
S=Somme(A,B)
print(S)
#Exercice 2
-------------------------------------------------------------------
def mult_scalaire(A,s):
R=np.zeros(A.shape)
for i in range(R.shape[0]):
for j in range(R.shape[1]):
R[i,j]=s*A[i,j]
return R
#Exercice 3
-------------------------------------------------------------------
def multiplier(A,B):
R=np.zeros((A.shape[0],B.shape[1]))
for i in range(A.shape[0]):
for j in range(A.shape[1]):
S=0
for k in range(A.shape[1]):
S=S+A[i,k]*B[k,j]
R[i,j]=S
return R
#Exercice 4
-------------------------------------------------------------------
def diagonale(A):
n=A.shape[0]
S=0
for i in range(n):
S=S+A[i,j]
return S
#Exercice 5:
-------------------------------------------------------------------
def addition(M):
d={}
n,m=M.shape

1
for i in range(n):
d.update({"ligne"+str(i):sum(M[i])})
for j in range(m):
d.update({"colonne"+str(j):sum(M[;,j])})
return d
#Exercice 6
-------------------------------------------------------------------
def Triangulaire(A):
n=A.shape[0]
for i in range(n):
for j in range(i):
if A[i,j]!=0:
return False
return True
#Exercice 7
-------------------------------------------------------------------
def transpose(A):
n,m=A.shape
AT=np.zeros((m,n))
for i in range(m):
for j in range(n):
AT[i,j]=A[j,i]
return AT
#Exercice 8
-------------------------------------------------------------------
def mat1(n):
M=np.zeros((n,n))
for i in range(n):
for j in range(n):
if i+j==n-1:
M[i,j]=1
return M

You might also like