Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def calculate_error(diff, norm=0):
  4. if norm == 0: # Infinity norm
  5. return np.max(np.abs(diff))
  6. elif norm == 1:
  7. return np.sum(np.abs(diff))
  8. else:
  9. return np.sum(np.abs(diff) ** norm) ** (1 / norm)
  10.  
  11.  
  12. def jacobi_iterative_method(A, b, x0=None, max_iterations=1000, termination_error=1e-4, error_norm=0):
  13. x = x0 if x0 is not None else np.zeros_like(b) # set initial estimate
  14. x = x.astype(float) # Cast the array to float type
  15.  
  16. if not (np.size(A, 0) == np.size(A, 1) == np.size(b, 0) == np.size(x, 0)) or np.size(x, 1) != 1:
  17. raise ValueError("The size of the input matrix does not match")
  18.  
  19. current_error = calculate_error(A.dot(x) - b, error_norm)
  20. iteration = 0
  21.  
  22. if current_error <= termination_error:
  23. return x, current_error, iteration
  24.  
  25. D = np.diag(np.diag(A)) # Containing the diagonal component of A
  26. R = A - D # Containing component of A except diagonal component
  27. invD = np.linalg.inv(D) # Inverse matrix of D
  28. T = -invD.dot(R)
  29. C = invD.dot(b)
  30.  
  31. # Core iterates
  32. while iteration < max_iterations:
  33. iteration = iteration + 1
  34. x = T.dot(x) + C
  35. current_error = calculate_error(A.dot(x) - b, error_norm)
  36. if current_error < termination_error:
  37. break
  38.  
  39. print("Jacobi iteration:")
  40. print(f"A =\n{A}\nb =\n{b}\n")
  41. print("Solution:")
  42. print(f"x =\n{x}\n")
  43. print(f"current_error = {current_error}\niteration = {iteration}\n")
  44. return x, current_error, iteration
  45.  
  46.  
  47. def gauss_seidel_iterative_method(A, b, x0=None, max_iterations=1000, termination_error=1e-4, error_norm=0):
  48. x = x0 if x0 is not None else np.zeros_like(b) # set initial estimate
  49. x = x.astype(float) # Cast the array to float type
  50.  
  51. if not (np.size(A, 0) == np.size(A, 1) == np.size(b, 0) == np.size(x, 0)) or np.size(x, 1) != 1:
  52. raise ValueError("The size of the input matrix does not match")
  53.  
  54. current_error = calculate_error(A.dot(x) - b, error_norm)
  55. iteration = 0
  56. collum = np.size(b, 0)
  57.  
  58. if current_error <= termination_error:
  59. return x, current_error, iteration
  60.  
  61. D = np.diag(np.diag(A)) # Containing the diagonal component of A
  62. R = A - D # Containing component of A except diagonal component
  63. invD = np.linalg.inv(D) # Inverse matrix of D
  64. T = -invD.dot(R)
  65. C = invD.dot(b)
  66.  
  67. # Core iterates
  68. while iteration < max_iterations:
  69. iteration = iteration + 1
  70. for i in range(collum):
  71. x[i] = T[i].dot(x) + C[i]
  72. # x = T.dot(x) + C
  73. current_error = calculate_error(A.dot(x) - b, error_norm)
  74. if current_error < termination_error:
  75. break
  76.  
  77. print("Gauss-Seidel iteration:")
  78. print(f"A =\n{A}\nb =\n{b}\n")
  79. print("Solution:")
  80. print(f"x =\n{x}\n")
  81. print(f"current_error = {current_error}\niteration = {iteration}\n")
  82. return x, current_error, iteration
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement