test_eigen.py 6.67 KB
Newer Older
Dean Moldovan's avatar
Dean Moldovan committed
1
2
3
4
5
import pytest

with pytest.suppress(ImportError):
    import numpy as np

6
7
8
9
10
    ref = np.array([[ 0,  3,  0,  0,  0, 11],
                    [22,  0,  0,  0, 17, 11],
                    [ 7,  5,  0,  1,  0, 11],
                    [ 0,  0,  0,  0,  0, 11],
                    [ 0,  0, 14,  0,  8, 11]])
Dean Moldovan's avatar
Dean Moldovan committed
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


def assert_equal_ref(mat):
    np.testing.assert_array_equal(mat, ref)


def assert_sparse_equal_ref(sparse_mat):
    assert_equal_ref(sparse_mat.todense())


@pytest.requires_eigen_and_numpy
def test_fixed():
    from pybind11_tests import fixed_r, fixed_c, fixed_passthrough_r, fixed_passthrough_c

    assert_equal_ref(fixed_c())
    assert_equal_ref(fixed_r())
    assert_equal_ref(fixed_passthrough_r(fixed_r()))
    assert_equal_ref(fixed_passthrough_c(fixed_c()))
    assert_equal_ref(fixed_passthrough_r(fixed_c()))
    assert_equal_ref(fixed_passthrough_c(fixed_r()))


@pytest.requires_eigen_and_numpy
def test_dense():
    from pybind11_tests import dense_r, dense_c, dense_passthrough_r, dense_passthrough_c

    assert_equal_ref(dense_r())
    assert_equal_ref(dense_c())
    assert_equal_ref(dense_passthrough_r(dense_r()))
    assert_equal_ref(dense_passthrough_c(dense_c()))
    assert_equal_ref(dense_passthrough_r(dense_c()))
    assert_equal_ref(dense_passthrough_c(dense_r()))

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@pytest.requires_eigen_and_numpy
def test_partially_fixed():
    from pybind11_tests import partial_passthrough_four_rm_r, partial_passthrough_four_rm_c, partial_passthrough_four_cm_r, partial_passthrough_four_cm_c

    ref2 = np.array([[0,1,2,3], [4,5,6,7], [8,9,10,11], [12,13,14,15]])
    np.testing.assert_array_equal(partial_passthrough_four_rm_r(ref2), ref2)
    np.testing.assert_array_equal(partial_passthrough_four_rm_c(ref2), ref2)
    np.testing.assert_array_equal(partial_passthrough_four_rm_r(ref2[:, 1]), ref2[:, [1]])
    np.testing.assert_array_equal(partial_passthrough_four_rm_c(ref2[0, :]), ref2[[0], :])
    np.testing.assert_array_equal(partial_passthrough_four_rm_r(ref2[:, (0, 2)]), ref2[:, (0,2)])
    np.testing.assert_array_equal(partial_passthrough_four_rm_c(ref2[(3,1,2), :]), ref2[(3,1,2), :])

    np.testing.assert_array_equal(partial_passthrough_four_cm_r(ref2), ref2)
    np.testing.assert_array_equal(partial_passthrough_four_cm_c(ref2), ref2)
    np.testing.assert_array_equal(partial_passthrough_four_cm_r(ref2[:, 1]), ref2[:, [1]])
    np.testing.assert_array_equal(partial_passthrough_four_cm_c(ref2[0, :]), ref2[[0], :])
    np.testing.assert_array_equal(partial_passthrough_four_cm_r(ref2[:, (0, 2)]), ref2[:, (0,2)])
    np.testing.assert_array_equal(partial_passthrough_four_cm_c(ref2[(3,1,2), :]), ref2[(3,1,2), :])
Dean Moldovan's avatar
Dean Moldovan committed
62
63
64
65
66
67
68
69

@pytest.requires_eigen_and_numpy
def test_nonunit_stride_from_python():
    from pybind11_tests import double_row, double_col, double_mat_cm, double_mat_rm

    counting_mat = np.arange(9.0, dtype=np.float32).reshape((3, 3))
    first_row = counting_mat[0, :]
    first_col = counting_mat[:, 0]
70
71
72
73
    np.testing.assert_array_equal(double_row(first_row), 2.0 * first_row)
    np.testing.assert_array_equal(double_col(first_row), 2.0 * first_row)
    np.testing.assert_array_equal(double_row(first_col), 2.0 * first_col)
    np.testing.assert_array_equal(double_col(first_col), 2.0 * first_col)
Dean Moldovan's avatar
Dean Moldovan committed
74
75
76
77

    counting_3d = np.arange(27.0, dtype=np.float32).reshape((3, 3, 3))
    slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]]
    for slice_idx, ref_mat in enumerate(slices):
78
79
        np.testing.assert_array_equal(double_mat_cm(ref_mat), 2.0 * ref_mat)
        np.testing.assert_array_equal(double_mat_rm(ref_mat), 2.0 * ref_mat)
Dean Moldovan's avatar
Dean Moldovan committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130


@pytest.requires_eigen_and_numpy
def test_nonunit_stride_to_python():
    from pybind11_tests import diagonal, diagonal_1, diagonal_n, block

    assert np.all(diagonal(ref) == ref.diagonal())
    assert np.all(diagonal_1(ref) == ref.diagonal(1))
    for i in range(-5, 7):
        assert np.all(diagonal_n(ref, i) == ref.diagonal(i)), "diagonal_n({})".format(i)

    assert np.all(block(ref, 2, 1, 3, 3) == ref[2:5, 1:4])
    assert np.all(block(ref, 1, 4, 4, 2) == ref[1:, 4:])
    assert np.all(block(ref, 1, 4, 3, 2) == ref[1:4, 4:])


@pytest.requires_eigen_and_numpy
def test_eigen_ref_to_python():
    from pybind11_tests import cholesky1, cholesky2, cholesky3, cholesky4, cholesky5, cholesky6

    chols = [cholesky1, cholesky2, cholesky3, cholesky4, cholesky5, cholesky6]
    for i, chol in enumerate(chols, start=1):
        mymat = chol(np.array([[1, 2, 4], [2, 13, 23], [4, 23, 77]]))
        assert np.all(mymat == np.array([[1, 0, 0], [2, 3, 0], [4, 5, 6]])), "cholesky{}".format(i)


@pytest.requires_eigen_and_numpy
def test_special_matrix_objects():
    from pybind11_tests import incr_diag, symmetric_upper, symmetric_lower

    assert np.all(incr_diag(7) == np.diag([1, 2, 3, 4, 5, 6, 7]))

    asymm = np.array([[ 1,  2,  3,  4],
                      [ 5,  6,  7,  8],
                      [ 9, 10, 11, 12],
                      [13, 14, 15, 16]])
    symm_lower = np.array(asymm)
    symm_upper = np.array(asymm)
    for i in range(4):
        for j in range(i + 1, 4):
            symm_lower[i, j] = symm_lower[j, i]
            symm_upper[j, i] = symm_upper[i, j]

    assert np.all(symmetric_lower(asymm) == symm_lower)
    assert np.all(symmetric_upper(asymm) == symm_upper)


@pytest.requires_eigen_and_numpy
def test_dense_signature(doc):
    from pybind11_tests import double_col, double_row, double_mat_rm

131
132
133
134
135
136
137
138
139
    assert doc(double_col) == """
        double_col(arg0: numpy.ndarray[float32[m, 1]]) -> numpy.ndarray[float32[m, 1]]
    """
    assert doc(double_row) == """
        double_row(arg0: numpy.ndarray[float32[1, n]]) -> numpy.ndarray[float32[1, n]]
    """
    assert doc(double_mat_rm) == """
        double_mat_rm(arg0: numpy.ndarray[float32[m, n]]) -> numpy.ndarray[float32[m, n]]
    """
Dean Moldovan's avatar
Dean Moldovan committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157


@pytest.requires_eigen_and_scipy
def test_sparse():
    from pybind11_tests import sparse_r, sparse_c, sparse_passthrough_r, sparse_passthrough_c

    assert_sparse_equal_ref(sparse_r())
    assert_sparse_equal_ref(sparse_c())
    assert_sparse_equal_ref(sparse_passthrough_r(sparse_r()))
    assert_sparse_equal_ref(sparse_passthrough_c(sparse_c()))
    assert_sparse_equal_ref(sparse_passthrough_r(sparse_c()))
    assert_sparse_equal_ref(sparse_passthrough_c(sparse_r()))


@pytest.requires_eigen_and_scipy
def test_sparse_signature(doc):
    from pybind11_tests import sparse_passthrough_r, sparse_passthrough_c

158
159
160
161
162
163
    assert doc(sparse_passthrough_r) == """
        sparse_passthrough_r(arg0: scipy.sparse.csr_matrix[float32]) -> scipy.sparse.csr_matrix[float32]
    """  # noqa: E501 line too long
    assert doc(sparse_passthrough_c) == """
        sparse_passthrough_c(arg0: scipy.sparse.csc_matrix[float32]) -> scipy.sparse.csc_matrix[float32]
    """  # noqa: E501 line too long