test_eigen.py revision 11986
1import pytest
2
3with pytest.suppress(ImportError):
4    import numpy as np
5
6    ref = np.array([[ 0,  3,  0,  0,  0, 11],
7                    [22,  0,  0,  0, 17, 11],
8                    [ 7,  5,  0,  1,  0, 11],
9                    [ 0,  0,  0,  0,  0, 11],
10                    [ 0,  0, 14,  0,  8, 11]])
11
12
13def assert_equal_ref(mat):
14    np.testing.assert_array_equal(mat, ref)
15
16
17def assert_sparse_equal_ref(sparse_mat):
18    assert_equal_ref(sparse_mat.todense())
19
20
21@pytest.requires_eigen_and_numpy
22def test_fixed():
23    from pybind11_tests import fixed_r, fixed_c, fixed_passthrough_r, fixed_passthrough_c
24
25    assert_equal_ref(fixed_c())
26    assert_equal_ref(fixed_r())
27    assert_equal_ref(fixed_passthrough_r(fixed_r()))
28    assert_equal_ref(fixed_passthrough_c(fixed_c()))
29    assert_equal_ref(fixed_passthrough_r(fixed_c()))
30    assert_equal_ref(fixed_passthrough_c(fixed_r()))
31
32
33@pytest.requires_eigen_and_numpy
34def test_dense():
35    from pybind11_tests import dense_r, dense_c, dense_passthrough_r, dense_passthrough_c
36
37    assert_equal_ref(dense_r())
38    assert_equal_ref(dense_c())
39    assert_equal_ref(dense_passthrough_r(dense_r()))
40    assert_equal_ref(dense_passthrough_c(dense_c()))
41    assert_equal_ref(dense_passthrough_r(dense_c()))
42    assert_equal_ref(dense_passthrough_c(dense_r()))
43
44
45@pytest.requires_eigen_and_numpy
46def test_nonunit_stride_from_python():
47    from pybind11_tests import double_row, double_col, double_mat_cm, double_mat_rm
48
49    counting_mat = np.arange(9.0, dtype=np.float32).reshape((3, 3))
50    first_row = counting_mat[0, :]
51    first_col = counting_mat[:, 0]
52    assert np.array_equal(double_row(first_row), 2.0 * first_row)
53    assert np.array_equal(double_col(first_row), 2.0 * first_row)
54    assert np.array_equal(double_row(first_col), 2.0 * first_col)
55    assert np.array_equal(double_col(first_col), 2.0 * first_col)
56
57    counting_3d = np.arange(27.0, dtype=np.float32).reshape((3, 3, 3))
58    slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]]
59    for slice_idx, ref_mat in enumerate(slices):
60        assert np.array_equal(double_mat_cm(ref_mat), 2.0 * ref_mat)
61        assert np.array_equal(double_mat_rm(ref_mat), 2.0 * ref_mat)
62
63
64@pytest.requires_eigen_and_numpy
65def test_nonunit_stride_to_python():
66    from pybind11_tests import diagonal, diagonal_1, diagonal_n, block
67
68    assert np.all(diagonal(ref) == ref.diagonal())
69    assert np.all(diagonal_1(ref) == ref.diagonal(1))
70    for i in range(-5, 7):
71        assert np.all(diagonal_n(ref, i) == ref.diagonal(i)), "diagonal_n({})".format(i)
72
73    assert np.all(block(ref, 2, 1, 3, 3) == ref[2:5, 1:4])
74    assert np.all(block(ref, 1, 4, 4, 2) == ref[1:, 4:])
75    assert np.all(block(ref, 1, 4, 3, 2) == ref[1:4, 4:])
76
77
78@pytest.requires_eigen_and_numpy
79def test_eigen_ref_to_python():
80    from pybind11_tests import cholesky1, cholesky2, cholesky3, cholesky4, cholesky5, cholesky6
81
82    chols = [cholesky1, cholesky2, cholesky3, cholesky4, cholesky5, cholesky6]
83    for i, chol in enumerate(chols, start=1):
84        mymat = chol(np.array([[1, 2, 4], [2, 13, 23], [4, 23, 77]]))
85        assert np.all(mymat == np.array([[1, 0, 0], [2, 3, 0], [4, 5, 6]])), "cholesky{}".format(i)
86
87
88@pytest.requires_eigen_and_numpy
89def test_special_matrix_objects():
90    from pybind11_tests import incr_diag, symmetric_upper, symmetric_lower
91
92    assert np.all(incr_diag(7) == np.diag([1, 2, 3, 4, 5, 6, 7]))
93
94    asymm = np.array([[ 1,  2,  3,  4],
95                      [ 5,  6,  7,  8],
96                      [ 9, 10, 11, 12],
97                      [13, 14, 15, 16]])
98    symm_lower = np.array(asymm)
99    symm_upper = np.array(asymm)
100    for i in range(4):
101        for j in range(i + 1, 4):
102            symm_lower[i, j] = symm_lower[j, i]
103            symm_upper[j, i] = symm_upper[i, j]
104
105    assert np.all(symmetric_lower(asymm) == symm_lower)
106    assert np.all(symmetric_upper(asymm) == symm_upper)
107
108
109@pytest.requires_eigen_and_numpy
110def test_dense_signature(doc):
111    from pybind11_tests import double_col, double_row, double_mat_rm
112
113    assert doc(double_col) == """
114        double_col(arg0: numpy.ndarray[float32[m, 1]]) -> numpy.ndarray[float32[m, 1]]
115    """
116    assert doc(double_row) == """
117        double_row(arg0: numpy.ndarray[float32[1, n]]) -> numpy.ndarray[float32[1, n]]
118    """
119    assert doc(double_mat_rm) == """
120        double_mat_rm(arg0: numpy.ndarray[float32[m, n]]) -> numpy.ndarray[float32[m, n]]
121    """
122
123
124@pytest.requires_eigen_and_scipy
125def test_sparse():
126    from pybind11_tests import sparse_r, sparse_c, sparse_passthrough_r, sparse_passthrough_c
127
128    assert_sparse_equal_ref(sparse_r())
129    assert_sparse_equal_ref(sparse_c())
130    assert_sparse_equal_ref(sparse_passthrough_r(sparse_r()))
131    assert_sparse_equal_ref(sparse_passthrough_c(sparse_c()))
132    assert_sparse_equal_ref(sparse_passthrough_r(sparse_c()))
133    assert_sparse_equal_ref(sparse_passthrough_c(sparse_r()))
134
135
136@pytest.requires_eigen_and_scipy
137def test_sparse_signature(doc):
138    from pybind11_tests import sparse_passthrough_r, sparse_passthrough_c
139
140    assert doc(sparse_passthrough_r) == """
141        sparse_passthrough_r(arg0: scipy.sparse.csr_matrix[float32]) -> scipy.sparse.csr_matrix[float32]
142    """  # noqa: E501 line too long
143    assert doc(sparse_passthrough_c) == """
144        sparse_passthrough_c(arg0: scipy.sparse.csc_matrix[float32]) -> scipy.sparse.csc_matrix[float32]
145    """  # noqa: E501 line too long
146