Advertisement
Guest User

devoir mn

a guest
Mar 30th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.63 KB | None | 0 0
  1. import numpy as np
  2. from time import time
  3.  
  4. t0 = time()
  5.  
  6. def exercice1():
  7.     """retourne la matrice, le second membre et la solution du systeme lineaire de l'exercice 1"""
  8.     # on suppose que numpy est importe sous le nom np (import numpy as np)
  9.     A = np.array([[1,2,3,4],[1,4,9,16],[1,8,27,64],[1,16,81,256]],float)
  10.     b = np.array([2,10,44,190],float)
  11.     x = np.array([-1,1,-1,1],float)
  12.     return A,b,x
  13.  
  14. def lu_fact1(A):
  15.     # version non vectorisee
  16.     # A est une matrice n x n dont on calcule
  17.     # la factorisation LU simple
  18.     # on renvoie 2 objets: le tableau LU qui
  19.     # contient la factorisation de maniere compacte
  20.     # et un entier donnant le statut de calcul
  21.     # istat = -1 si A n'est pas carree (NB: neanmoins on pourrait effectuer
  22.     #            une factorisation sur une matrice rectangulaire)
  23.     #          0 OK
  24.     #          k > 0 : le pivot naturel a l'etape k est nul
  25.     LU = A.copy()
  26.     n,m = np.shape(A)
  27.     if n != m:
  28.         istat = -1
  29.     else:
  30.         istat = 0
  31.         for k in range(n-1):
  32.             if LU[k,k] == 0:
  33.                 istat = k+1
  34.                 break
  35.             for i in range(k+1,n):
  36.                 LU[i,k] = LU[i,k]/LU[k,k]
  37.                 for j in range(k+1,n):
  38.                     LU[i,j] = LU[i,j] - LU[i,k]*LU[k,j]
  39.     return LU, istat
  40.  
  41. def lu_fact(A):
  42.     # version vectorisee
  43.     # A est une matrice n x n dont on calcule
  44.     # la factorisation LU simple
  45.     # on renvoie 2 objets: le tableau LU qui
  46.     # contient la factorisation de maniere compacte
  47.     # et un entier donnant le statut de calcul
  48.     # istat = -1 si A n'est pas carree (NB: neanmoins on pourrait effectuer
  49.     #            une factorisation sur une matrice rectangulaire)
  50.     #          0 OK
  51.     #          k > 0 : le pivot naturel a l'etape k est nul
  52.     LU = A.copy()
  53.     n,m = np.shape(A)
  54.     if n != m:
  55.         istat = -1
  56.     else:
  57.         istat = 0
  58.         for k in range(n-1):
  59.             if LU[k,k] == 0:
  60.                 istat = k+1
  61.                 break
  62.             LU[k+1:,k] = LU[k+1:,k]/LU[k,k]
  63.             LU[k+1:,k+1:] = LU[k+1:,k+1:] - np.outer(LU[k+1:,k],LU[k,k+1:])
  64.     return LU, istat
  65.  
  66. def lu_solve1(LU,b):
  67.     # version non vectorisee
  68.     # resoud Ax=b connaissant la factorisation LU de A
  69.     y = b.copy()
  70.     n = len(y)
  71.     # resolution de Ly = b
  72.     for i in range(n):
  73.         for j in range(i):
  74.             y[i] = y[i] - LU[i,j]*y[j]
  75.     # resolution de Ux = y
  76.     x = y   # pas de copie juste un nouveau nom...
  77.     for i in range(n-1,-1,-1):
  78.         for j in range(i+1,n):
  79.             x[i] = x[i] - LU[i,j]*x[j]
  80.         x[i] = x[i]/LU[i,i]
  81.     return x
  82.  
  83. def lu_solve(LU,b):
  84.     # version vectorisee
  85.     # resoud Ax=b connaissant la factorisation LU de A
  86.     y = b.copy()
  87.     n = len(y)
  88.     # resolution de Ly = b
  89.     for i in range(n):
  90.         y[i] = y[i] - LU[i,:i]@y[:i]
  91.     # resolution de Ux = y
  92.     x = y
  93.     for i in range(n-1,-1,-1):
  94.         x[i] = (x[i] - LU[i,i+1:]@x[i+1:])/LU[i,i]
  95.     return x
  96.  
  97. def random_mat_diag_dom(n):
  98.     """fabrique une matrice aléatoire n x n à diagonale str. dominante"""
  99.     # on suppose que numpy est importe sous le nom np (import numpy as np)
  100.     A = np.random.rand(n,n)
  101.     for i in range(n) :
  102.         A[i,i] += np.sum(A[i,:])
  103.     return A
  104.  
  105. n=1000
  106. A = random_mat_diag_dom(n)
  107. LU, istat = lu_fact(A)
  108. L = np.tril(LU,-1) + np.identity(n)
  109. U = np.triu(LU)
  110. Ac = L@U
  111. # Ac est théoriquement égale à A en arithmétique exacte
  112. residu = np.linalg.norm(Ac - A,1)/np.linalg.norm(A,1)
  113.  
  114.  
  115. b = np.random.rand(n)
  116. x = lu_solve(LU,b)
  117. residu = np.linalg.norm(A@x-b,1)/np.linalg.norm(b)
  118.  
  119. t = time() - t0
  120. print(t)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement