예제 #1
0
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))
예제 #2
0
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))
예제 #3
0
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))
예제 #4
0
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))])
예제 #5
0
  return res

def rueckwaerts(LU_T, v):
  # L_T * w = v
  res = np.zeros(len(LU_T))
  for y in range(len(LU_T)-1, -1, -1):
    res[y] = v[y] - sum([ LU_T[y,x] * res[x] for x in range(y+1, len(LU_T))])
  return res

if __name__ == '__main__':
  A = np.array([[0,0,0,1],
                [2,1,2,0],
                [4,4,0,0],
                [2,3,1,0]])

  c = np.array([152,154,56,17])

  print(f'A: {A}')
  print(f'c: {c}')

  LU, p = zerlegung(A, first_non_zero)
  print(f'LU: {LU}')
  print(f'p: {p}')
  v = vorwaerts(LU.T, c)
  print(f'v: {v}')
  w = rueckwaerts(LU.T, v)
  z = permutation(p, w)

  print('Ergebnis:')
  print(f'z: {z}')
예제 #6
0

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)
예제 #7
0
# -*- 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))
예제 #8
0
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)