def transform(m, e=0.001): m = [[e / row[i] for e in row]for i, row in enumerate(m)] a = [[-e for e in row[:-1]] for row in m] b = [row[-1] for row in m] a = [row[:i] + [0] + row[i+1:]for i, row in enumerate(a)] print_m(a) return a, b
def solution(A): n = len(A) for k in range(1, n): for j in range(k, n): m = A[j][k-1] / A[k-1][k-1] for i in range(n+1): A[j][i] = A[j][i] - m*A[k-1][i] print(f"----------{k:} step ---------------") print_m(A) print("-------------------------------") print() x = [0]*n for i in reversed(range(n)): x[i] = A[i][n] / A[i][i] for c in reversed(range(i + 1, n)): x[i] -= A[i][c]*x[c]/A[i][i] return x
print("-------------------------------") print() x = [0]*n for i in reversed(range(n)): x[i] = A[i][n] / A[i][i] for c in reversed(range(i + 1, n)): x[i] -= A[i][c]*x[c]/A[i][i] return x A = testMatrix a = [row[:-1] for row in A] b = [row[-1] for row in A] A_1 = linalg.inv(a) print("Your matrix:") print_m(testMatrix) print() print(solution(testMatrix)) print() print("pow(Matrix,-1)") print_m(A_1) print() A_max = max([sum(row)] for row in A) Inv_max = max([sum(row)] for row in A_1) B_max = max(b) d_x = Inv_max[0] * 0.001 _d_x = A_max[0] * Inv_max[0] * 0.001 / B_max print("absolute and relative error") print(d_x) print(_d_x)
while diff >= acc: temp = px[:] cx = [0.0] * len(b) for i,row in enumerate(a): for j,e in enumerate(row): cx[i] += e * px[j] cx[i] += b[i] px[i] = cx[i] diff = max([abs(e-temp[i]) for i, e in enumerate(cx)]) px = cx print(px,diff) return diff, px print("Your matrix") print_m(testMatrix) print("------------") a, b = transform(testMatrix) print("Transformed matrix :") print_m(a) print(np.matrix(b)) print("------------") print("Seidel method:") max_val, result = seidel(a, b) print(f"Max value is {max_val}") print("Result:") print(result) print("------------")
res = [] for n in range(len(u)): ort = [0 for i in range(len(u) - 1)] ort.insert(n, 1) y = lower(u_t, ort) x = upper(u, y) res.append(x) res = np.array(list(zip(*res[:]))) return np.array(res) if np.linalg.det(A) == 0: raise ValueError(" det A == 0 ") ASymmetrized, BSymmetrized = symmetrize(A, B) print("Symmetrized matrix :") print_m(ASymmetrized) print() print(BSymmetrized) print("------------------") U, U_T = U(ASymmetrized, BSymmetrized) print() print_m(U) print() print_m(U_T) print("------------------") print("Y-vect :") AnswerY = lower(U_T, BSymmetrized) print(AnswerY) print("------------------") print("X-vect :") AnswerX = upper(U, AnswerY)