Hello, I'm trying to put a new condition in the Gauss-Seidel method. In the case, A, q and x are three matrices, and w is the number of times the interaction is made:
def gauss(A, q, x, w):
L = np.tril(A)
U = A - L
for i in range(w):
x = np.dot(np.linalg.inv(L), q - np.dot(U, x))
print str(i).zfill(n),
print(x)
return x
#número de interações
w = 500
In this case, I am trying to add a new condition: what is the largest and smallest number that can appear in the output within the array. In this case, it would be:
for i in range(0,len(gauss(A, q, x, w))):
for j in range(0,len(gauss(A, q, x, w)[i])):
if gauss(A, q, x, w)[i][j] >= -1 * (10 **(-2)):
gauss(A, q, x, w)[i][j] = -1 * (10 **(-2))
if gauss(A, q, x, w)[i][j] >= 1 * (10 **(-2)):
gauss(A, q, x, w)[i][j] = 1 * (10 **(-2))
However, the program does not understand the command when adding in the rest and I do not know exactly if I am putting in the wrong order, if the command is wrong or if I should add all this out into a new function:
def gauss(A, q, x, w):
L = np.tril(A)
U = A - L
for i in range(w):
x = np.dot(np.linalg.inv(L), q - np.dot(U, x))
for i in range(0,len(gauss(A, q, x, w))):
for j in range(0,len(gauss(A, q, x, w)[i])):
if gauss(A, q, x, w)[i][j] >= -1 * (10 **(-2)):
gauss(A, q, x, w)[i][j] = -1 * (10 **(-2))
if gauss(A, q, x, w)[i][j] >= 1 * (10 **(-2)):
gauss(A, q, x, w)[i][j] = 1 * (10 **(-2))
print str(i).zfill(n),
print(x)
return x
w = 500
print gauss(A, q, x, w)
solve = np.linalg.solve(A, q)
print solve