def exercise_one(): A = np.array([[0, 0, 0, 1], [2, 1, 2, 0], [4, 4, 0, 0], [2, 3, 1, 0]]) b_one = np.array([3, 5, 4, 5]) b_two = np.array([4, 10, 12, 11]) LU, p = zerlegung(A) pb = permutation(p, b_one) y = vorwaerts(LU, pb) print(rueckwaerts(LU, y)) pb = permutation(p, b_two) y = vorwaerts(LU, pb) print(rueckwaerts(LU, y))
def householder(A, b): diag = [] orig_A = np.copy(A) temp_A = np.copy(A) Q_cumulative = np.identity(len(A)) for x in range(len(A)): n = len(A) - x a = A[x:, x] if len(a) == 1: diag.append(*a) break norm_a = norm(a, 2) d = -np.sign(a[0]) * norm_a v1 = a[0] - d norm_v_sq = -2 * v1 * d v = np.array([v1, *a[1:]])[:, np.newaxis] Q = np.identity(n) - 2 / (norm_v_sq) * v * v.T Q_ext = np.identity(len(A)) Q_ext[x:, x:] = Q Q_cumulative = np.dot(Q_ext, Q_cumulative) A = np.dot(Q_ext, A) diag.append(d) temp_A[x:, x] = v.reshape(n) Q = Q_cumulative.T R = np.dot(Q_cumulative, orig_A) y = np.dot(Q.T, b) x = vorwaerts(R, y) return x
def exercise_two(): for n in [5, 10, 15, 20]: A = np.array([[1 / (i + j - 1) for j in range(1, n + 1)] for i in range(1, n + 1)]) b = np.array([1 / (i + 1) for i in range(1, n + 1)]) LU, p = zerlegung(A) pb = permutation(p, b) y = vorwaerts(LU, pb) print('n:', n, '=>', rueckwaerts(LU, y))
def exercise_b(): for n in range(10, 16): print(f'n = {n}:') A, b = create_a_b_exercise_b(n) LU, p = zerlegung(A, greatest_non_zero) x = permutation(p, b) y = vorwaerts(LU, x) z = rueckwaerts(LU, y) print(nachiteration(A, b, LU, p, z, nachiterationen))
def hager(B): n = len(B) x = np.full(n, 1) / n LU, p = zerlegung(B, first_non_zero) while True: v = vorwaerts_T(LU.T, x) w = rueckwaerts_T(LU.T, v) y = permutation_T(p, w) xi = np.sign(y) v = vorwaerts(LU, permutation(p, xi)) z = np.array(rueckwaerts(LU, v)) if norm(z, np.inf) <= np.dot(z.T, x): return norm(y, 1) j = np.argmax(np.abs(z)) x = np.array([1 if k == j else 0 for k in range(len(x))])
def init_A_b(n): A = np.identity(n) for y in range(1, n): for x in range(y): A[y, x] = -1 A[:, -1] = 1 b = np.array([2 - n if i == n else 3 - i for i in range(n)]) return A, b if __name__ == '__main__': A = np.array([[20, 18, 44], [0, 40, 45], [-15, 24, -108]]) b = np.array([-4, -45, 78]) x = householder(A, b) print(x) for n in [40, 50, 60]: A, b = init_A_b(n) x = householder(A, b) print(f'n={n}: {x}') LU, p = zerlegung(A, greatest_non_zero) x = permutation(p, b) y = vorwaerts(LU, x) res = rueckwaerts(LU, y) print(res)
return np.add(np.dot(np.absolute(A), np.absolute(x̃)), np.absolute(b)); def calculate_ɛ(r̃, s̃): return np.amax([abs(r̃[i]) / abs(s̃[i]) for i in range(0, len(r̃))]) def calculate_machine_ɛ(): return (1 / 2) * (2 ** (1 - np.finfo(float).nmant)) δ_s = [10**-8, 10**-10,10**-12]; for δ in δ_s: print('δ: ', δ) A, b = init_for_delta(δ) LU, p = zerlegung(A, greatest_non_zero) p_b = permutation(p, b) y = vorwaerts(LU, p_b) x̃ = rueckwaerts(LU, y) print(x̃) print(correct_answer(δ)) r̃ = calculate_r̃(A, b, x̃); s̃ = calculate_s̃(A, b, x̃); ɛ = calculate_ɛ(r̃, s̃); kondition = np.linalg.cond(A); print(' ɛ: ', ɛ) print(' ~ɛ: ', kondition * calculate_machine_ɛ())
# -*- coding: utf-8 -*- import numpy as np from LUtils import zerlegung, permutation, vorwaerts, rueckwaerts A = np.array([[0, 0, 0, 1], [2, 1, 2, 0], [4, 4, 0, 0], [2, 3, 1, 0]]) u = np.array([0, 1, 2, 3]) v = np.array([0, 0, 0, 1]) b = np.array([3, 5, 4, 5]) LU, p = zerlegung(A) per = permutation(p, u) y = vorwaerts(LU, per) z = rueckwaerts(LU, y) val = 1 + np.dot(np.transpose(v), z) if val != 0: a = 1 / val per = permutation(p, b) y = vorwaerts(LU, per) z_dach = rueckwaerts(LU, y) print(z_dach - a * np.dot(np.dot(np.transpose(v), z_dach), z))
def with_pivot_search(n, beta): A, b = init_for_n(n, beta) LU, p = zerlegung(A, greatest_non_zero) x = permutation(p, b) y = vorwaerts(LU, x) return rueckwaerts(LU, y)