Advertisement
Guest User

Untitled

a guest
May 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. from sympy import *
  2.  
  3. init_printing()
  4.  
  5. A = Matrix([[ 2,  3,  1],
  6.      [-1, -2, -2],
  7.      [ 2,  5,  3],
  8.      [ 1,  2,  0]])
  9.  
  10. b = Matrix([20, -20, -20, 10])
  11.  
  12. m = A.shape[0]
  13. n = A.shape[1]
  14.  
  15. rank = A.rank()
  16.  
  17. def G_Matrix(i, j, c, s):
  18.     G = Matrix.eye(m)
  19.     # Insert C and S
  20.     G[i, i] = c
  21.     G[i, j] = s
  22.     G[j, i] = -s
  23.     G[j, j] = c
  24.     return G
  25. R = A.QRdecomposition()[1].evalf()
  26. for j in range(n):
  27.     for i in range(j+1, m):
  28.         a = A[i, j]
  29.         if a != 0:
  30.             print(f"Step: i: {i + 1}; j: {j + 1}")
  31.             r = sqrt(A[j, j] ** 2 + a ** 2)
  32.             print(f"r: {r.evalf()}")
  33.             c = A[j, j] / r
  34.             print(f"c: {c.evalf()}")
  35.             s = a / r
  36.             print(f"s: {s.evalf()}")
  37.             rot_mat = G_Matrix(j, i, c, s)
  38.             A = rot_mat * A
  39.             b = rot_mat * b
  40.             print("\nG:\n")
  41.             pprint(A)
  42.             pprint("\n----------------------------------------------------------------------------\n")
  43.  
  44. print("x: \n")
  45. pprint(A.LDLsolve(b))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement