/
test_power_method.py
58 lines (50 loc) · 2.01 KB
/
test_power_method.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
#!/usr/bin/env python3
## vi: tabstop=4 shiftwidth=4 softtabstop=4 expandtab
## ---------------------------------------------------------------------
##
## Copyright (C) 2019 by the adcc authors
##
## This file is part of adcc.
##
## adcc is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## adcc is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with adcc. If not, see <http://www.gnu.org/licenses/>.
##
## ---------------------------------------------------------------------
import unittest
import numpy as np
from numpy.testing import assert_allclose
from adcc.misc import expand_test_templates
from adcc.solver.power_method import default_print, power_method
from pytest import approx
from scipy.sparse.linalg import aslinearoperator
sizes = ["0004", "0050", "0200", "1000"]
@expand_test_templates(sizes)
class TestPowerMethod(unittest.TestCase):
def template_random_matrix(self, size):
size = int(size)
conv_tol = 1e-10
ev = np.random.randn(size)
ev[0] = abs(ev[0]) + 5
start = np.random.randn(len(ev))
start[0] += 0.001
res = power_method(aslinearoperator(np.diag(ev)), start,
conv_tol=conv_tol, callback=default_print,
explicit_symmetrisation=None,
max_iter=100)
ones = np.zeros(size)
ones[0] = 1 * np.sign(res.eigenvectors[0][0])
extrafac = 1
if size > 100:
extrafac = 3
assert_allclose(res.eigenvectors[0], ones, atol=conv_tol * 10 * extrafac)
assert approx(res.eigenvalues[0]) == ev[0]