Skip to content

Instantly share code, notes, and snippets.

@guissalustiano
Created January 27, 2022 11:21
Show Gist options
  • Save guissalustiano/b5c6e4fbe9fa50240ce9ea53b3fb8504 to your computer and use it in GitHub Desktop.
Save guissalustiano/b5c6e4fbe9fa50240ce9ea53b3fb8504 to your computer and use it in GitHub Desktop.
Testes para o EP1 de numerico da Comp EPUSP
import numpy as np
from numpy import isclose
from ex1 import eig_pot
from ex2 import crit_sassenfeld, solve_sor, eig_potinv
def assertVector(a, b, **kwargs):
for exp, act in zip(a, b):
assert isclose(exp, act, **kwargs)
def test_pot():
# Exemplo do professor
a = np.array([[-2, -4, 2],
[-2, 1, 2],
[ 4, 2, 5]])
eigval, _ = np.linalg.eig(a)
eigval = np.sort(np.abs(eigval))[::-1]
expected = eigval[0]
actual, _ = eig_pot(a)
assert isclose(expected, actual)
def test_sassenfeld_false():
a = np.array([[2, 5, 2],
[3, -2, 4],
[ -6, 1, 7]])
b = np.array([-38, 17, -12])
assert not crit_sassenfeld(a, b)
def test_sassenfeld_true():
# Exemplo do professor
a = np.array([[2, 1, -0.2, 0.2],
[0.6, 3, -0.6, -0.3],
[-0.1, -0.2, 1, 0.2],
[0.4, 1.2, 0.8, 4]])
b = np.array([0.4, 7.8, 1, 10])
assert crit_sassenfeld(a, b)
## Exercicio 2
def test_sor():
a = np.array([[4, 1, 1],
[-2, 5, 1],
[ 3, 1, 6]])
b = np.array([5, 0, -6.5])
expected = np.linalg.solve(a, b)
actual = solve_sor(a, b, w=1)
assertVector(expected, actual)
def test_sor2():
# Wikipedia example
a = np.array([[ 4, -1, -6, 0],
[-5, -4, 10, 8],
[ 0, 9, 4, -2],
[ 1, 0, -7, 5]])
b = np.array([2, 21, -12, -6])
expected = np.linalg.solve(a, b)
actual = solve_sor(a, b, w=0.5)
assertVector(expected, actual)
def test_sor3():
# Youtube Reindolf example
a = np.array([[ 3, -1, 1],
[ 3, 6, 2],
[ 3, 3, 7]])
b = np.array([1, 0, 4])
expected = np.linalg.solve(a, b)
actual = solve_sor(a, b)
assertVector(expected, actual)
def test_pot_inv():
a = np.array([[ 4, -1, -6, 0],
[-5, -4, 10, 8],
[ 0, 9, 4, -2],
[ 1, 0, -7, 5]])
eigval = np.linalg.eigvals(a)
eigval = np.sort(np.abs(eigval))
expected = eigval[0]
actual, _ = eig_potinv(a, w=0.5)
assert isclose(expected, actual)
def test_pot_inv2():
a = np.array([[-2, -4, 2],
[-2, 1, 2],
[ 4, 2, 5]])
eigval = np.linalg.eigvals(a)
eigval = np.sort(np.abs(eigval))
expected = eigval[0]
actual, _ = eig_potinv(a)
assert isclose(expected, actual)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment