-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
108 lines (62 loc) · 1.84 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#GAUSS - SEIDEL
"""
Author: @JoseMinuche
"""
#Libraries.
import numpy as np
"""
How to install -> README
"""
#Gauss - Seidel Procedure.
def gauss_seidel(A, B, X0, tolera, iteramax):
tamano = np.shape(A)
n = tamano[0]
m = tamano[1]
#initial value
X = np.copy(X0)
difference = np.ones(n, dtype=float)
error = 2 * tolera
itera = 0
while not(error <= tolera or itera > iteramax):
#rows
for i in range(0,n,1):
#columns
sums = 0
for j in range(0,m,1):
# except diagonal A
if (i != j):
sums = sums - A[i,j] * X[j]
new_ = (B[i] + sums) / A[i,i]
difference[i] = np.abs(new_ - X[i])
X[i] = new_
error = np.max(difference)
itera = itera + 1
#Anwser X column
X = np.transpose([X])
#Check if it does not converge
if (itera>iteramax):
X=0
#Check Answer
verifica = np.dot(A,X)
return(X, verifica, itera)
#Create A matrix
def create_A_matrix (dimension):
A = np.ones((dimension, dimension), dtype = float)
for i in range(0, dimension):
value1 = input("Enter A matrix values for the row: ")
A[i,0] = value1
for j in range(1, dimension):
value2 = input("Enter A matrix values for the columns of the row: ")
A[i, j] = value2
return A
#Create B matrix
def create_B_matrix(dimension):
B = np.ones((dimension,1), dtype = float)
for s in range(0, dimension):
value3 = input("Enter B matrix values: ")
B[s, 0] = value3
return B
#Create X0 matrix
def create_X0_matrix(dimension):
X0 = np.zeros((dimension), dtype = float)
return X0