-
Notifications
You must be signed in to change notification settings - Fork 296
/
Copy pathwaves_test.py
86 lines (67 loc) · 2.08 KB
/
waves_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from assertpy import assert_that
import numpy
import pytest
from waves import bead_matrix
from waves import sorted_eigensystem
from waves import decompose
EPSILON = 1e-5
def normalize(v):
norm = numpy.linalg.norm(v)
if norm == 0:
return v
return v / norm
def test_bead_matrix_fail():
with pytest.raises(ValueError):
bead_matrix(dimension=3)
def test_bead_matrix():
expected_matrix = numpy.array(
[
[-2, 1, 0, 0, 0],
[1, -2, 1, 0, 0],
[0, 1, -2, 1, 0],
[0, 0, 1, -2, 1],
[0, 0, 0, 1, -2],
]
)
flattened_expected = expected_matrix.flatten()
flattened_actual = bead_matrix(dimension=5).flatten()
for (a, b) in zip(flattened_actual, flattened_expected):
assert_that(a).is_close_to(b, EPSILON)
def test_sorted_eigensystem():
matrix = numpy.array(
[
[1, 1, 2],
[-1, 3, 2],
[-1, 2, 3],
]
)
eigenvalues, eigenvectors = sorted_eigensystem(matrix)
expected_eigenvalues = [4, 2, 1]
expected_eigenvectors = numpy.array([
numpy.array([1, 1, 1]) / numpy.sqrt(3),
numpy.array([-3, -1, -1]) / numpy.sqrt(11),
numpy.array([-2, -2, 1]) / 3,
])
for (e1, e2) in zip(eigenvalues, expected_eigenvalues):
assert_that(e1).is_close_to(e2, EPSILON)
for (v1, v2) in zip(eigenvectors, expected_eigenvectors):
for (a, b) in zip(v1, v2):
assert_that(a).is_close_to(b, EPSILON)
def test_decompose():
eigenvectors = numpy.array(
[
numpy.array([1, 1, 1]) / numpy.sqrt(3),
numpy.array([-3, -1, -1]) / numpy.sqrt(11),
numpy.array([-2, -2, 1]) / 3,
]
)
vector = numpy.array([1, 2, 3])
decomposition = decompose(eigenvectors, vector)
expected_decomposition = {
0: (1 + 2 + 3) / 3**0.5,
1: (-3 - 2 - 3) / 11**0.5,
2: (-2 - 4 + 3) / 3,
}
for key in decomposition.keys():
assert_that(decomposition[key]).is_close_to(
expected_decomposition[key], EPSILON)