Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def my_lu(A):
  4. # The upper triangular matrix U is saved in the upper part of the matrix M (including the diagonal)
  5. # The lower triangular matrix L is saved in the lower part of the matrix M (not including the diagonal)
  6. # Do NOT use `scipy.linalg.lu`!
  7. # You should not use pivoting
  8.  
  9. mA = np.copy(A)
  10. M = np.zeros((len(A), len(A)))
  11. for i in range(len(A) - 1):
  12. M[i, i:] = mA[i, i:]
  13. M[i + 1:, i] = mA[i + 1:, i] / mA[i, i]
  14. prod = np.einsum('i,j->ij', M[i + 1:, i], M[i, i + 1:])
  15. mA[i + 1:, i + 1:] -= prod
  16. M[i + 1:, i + 1:] = mA[i + 1:, i + 1:]
  17. return M
  18.  
  19.  
  20. def my_triangular_solve(M, b):
  21. # A = LU (L and U are stored in M)
  22. # A x = b (given A and b, find x)
  23. # M is a 2D numpy array
  24. # The upper triangular matrix U is stored in the upper part of the matrix M (including the diagonal)
  25. # The lower triangular matrix L is stored in the lower part of the matrix M (not including the diagonal)
  26. # b is a 1D numpy array
  27. # x is a 1D numpy array
  28. # Do not use `scipy.linalg.solve_triangular`
  29.  
  30. y = np.zeros(len(M))
  31. for i in range(len(y)):
  32. currSum = sum([M[i, j] * y[j] for j in range(i)])
  33. y[i] = b[i] - currSum
  34.  
  35. x = np.zeros(len(M))
  36. for i in range(len(x) - 1, -1, -1):
  37. currSum = sum([M[i, j] * x[j] for j in range(len(x) - 1, i - 1, -1)])
  38. x[i] = (y[i] - currSum) / M[i, i]
  39.  
  40. return x
  41.  
  42.  
  43. def fea_solve(Kpp, Kpf, Kfp, Kff, xp, Ff):
  44. # Use my_lu and my_triangular_solve
  45. M = my_lu(Kff)
  46. xf = my_triangular_solve(M, Ff - np.matmul(Kfp, xp))
  47. Fp = np.matmul(Kpp, xp) + np.matmul(Kpf, xf)
  48. return xf, Fp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement