-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_permanent.py
95 lines (81 loc) · 2.7 KB
/
test_permanent.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
87
88
89
90
91
92
93
94
95
from math import factorial
import numpy as np
import numpy.testing as npt
import permanent
import pytest
ATOL = 0e0
RTOL = 1e-4
FNS = [
permanent.opt,
permanent.combinatoric,
permanent.glynn,
permanent.ryser,
]
@pytest.mark.parametrize(
"args",
[
(np.arange(1, 5, dtype=float).reshape(2, 2), 10),
(np.arange(1, 5, dtype=int).reshape(2, 2), 10),
(np.arange(1, 5, dtype=complex).reshape(2, 2), 10),
(np.arange(1, 10, dtype=float).reshape(3, 3), 450),
(np.arange(1, 17, dtype=float).reshape(4, 4), 55456),
(np.arange(1, 50, dtype=float).reshape(7, 7), 5373548250000),
# Rectangular matrices
(np.arange(1, 7, dtype=float).reshape(2, 3), 58),
(np.arange(1, 9, dtype=float).reshape(2, 4), 190),
(np.arange(1, 15, dtype=float).reshape(2, 7), 1820),
(np.arange(1, 36, dtype=float).reshape(5, 7), 1521238320),
(np.arange(1, 43, dtype=float).reshape(6, 7), 117681979920),
# Special matrices
(np.ones((10, 10), dtype=float), factorial(10)),
(np.ones((12, 12), dtype=float), factorial(12)),
(np.identity(10, dtype=float), 1),
(np.identity(5, dtype=float), 1),
(np.diag(np.diag(np.ones((3, 7), dtype=float))), 1),
],
)
@pytest.mark.parametrize("fn", FNS)
def test_compute_permanent(fn, args):
r"""Ensure that the correct permanent is computed."""
npt.assert_allclose(fn(args[0]), args[1], atol=ATOL, rtol=RTOL)
@pytest.mark.parametrize(
"arg",
[
np.ones((2, 65)),
np.ones((65, 2)),
np.ones((65, 65)),
],
)
@pytest.mark.parametrize("fn", FNS)
def test_dim_gt_64_raises(fn, arg):
r"""Ensure that matrices with any dimension > 64 raise a ValueError."""
with npt.assert_raises(ValueError):
fn(arg)
@pytest.mark.parametrize(
"arg",
[
np.ones((2, 23), dtype=int),
np.ones((23, 2), dtype=int),
np.ones((43, 64), dtype=int),
np.ones((64, 43), dtype=int),
],
)
@pytest.mark.parametrize("fn", FNS)
def test_int_dim_diff_gt_20_raises(fn, arg):
r"""Ensure that integer matrices with difference in dimensions > 20 raise a ValueError."""
with npt.assert_raises(ValueError):
fn(arg)
@pytest.mark.parametrize(
"arg",
[
np.ones((4, 4), dtype=bool),
np.array([[object() for _ in range(4)] for _ in range(4)]),
np.array([[{"hi": 1} for _ in range(4)] for _ in range(4)]),
np.array([[chr(x) for x in range(65, 69)] for _ in range(4)]),
],
)
@pytest.mark.parametrize("fn", FNS)
def test_invalid_type_raises(fn, arg):
r"""Ensure that matrices with an invalid dtype raise a ValueError."""
with npt.assert_raises(TypeError):
fn(arg)