"vscode:/vscode.git/clone" did not exist on "44596bc4ee9e3d82067eb424732ab54aa59dfa63"
test_numpy_vectorize.py 9.48 KB
Newer Older
1
# -*- coding: utf-8 -*-
Dean Moldovan's avatar
Dean Moldovan committed
2
import pytest
3
from pybind11_tests import numpy_vectorize as m
Dean Moldovan's avatar
Dean Moldovan committed
4

5
np = pytest.importorskip("numpy")
Dean Moldovan's avatar
Dean Moldovan committed
6
7
8


def test_vectorize(capture):
9
    assert np.isclose(m.vectorized_func3(np.array(3 + 7j)), [6 + 14j])
Dean Moldovan's avatar
Dean Moldovan committed
10

11
    for f in [m.vectorized_func, m.vectorized_func2]:
Dean Moldovan's avatar
Dean Moldovan committed
12
13
14
15
16
17
18
19
        with capture:
            assert np.isclose(f(1, 2, 3), 6)
        assert capture == "my_func(x:int=1, y:float=2, z:float=3)"
        with capture:
            assert np.isclose(f(np.array(1), np.array(2), 3), 6)
        assert capture == "my_func(x:int=1, y:float=2, z:float=3)"
        with capture:
            assert np.allclose(f(np.array([1, 3]), np.array([2, 4]), 3), [6, 36])
20
21
22
        assert (
            capture
            == """
Dean Moldovan's avatar
Dean Moldovan committed
23
24
25
            my_func(x:int=1, y:float=2, z:float=3)
            my_func(x:int=3, y:float=4, z:float=3)
        """
26
        )
27
        with capture:
28
29
            a = np.array([[1, 2], [3, 4]], order="F")
            b = np.array([[10, 20], [30, 40]], order="F")
30
31
32
33
34
            c = 3
            result = f(a, b, c)
            assert np.allclose(result, a * b * c)
            assert result.flags.f_contiguous
        # All inputs are F order and full or singletons, so we the result is in col-major order:
35
36
37
        assert (
            capture
            == """
38
39
40
41
42
            my_func(x:int=1, y:float=10, z:float=3)
            my_func(x:int=3, y:float=30, z:float=3)
            my_func(x:int=2, y:float=20, z:float=3)
            my_func(x:int=4, y:float=40, z:float=3)
        """
43
        )
Dean Moldovan's avatar
Dean Moldovan committed
44
        with capture:
45
46
47
48
49
            a, b, c = (
                np.array([[1, 3, 5], [7, 9, 11]]),
                np.array([[2, 4, 6], [8, 10, 12]]),
                3,
            )
Dean Moldovan's avatar
Dean Moldovan committed
50
            assert np.allclose(f(a, b, c), a * b * c)
51
52
53
        assert (
            capture
            == """
Dean Moldovan's avatar
Dean Moldovan committed
54
55
56
57
58
59
60
            my_func(x:int=1, y:float=2, z:float=3)
            my_func(x:int=3, y:float=4, z:float=3)
            my_func(x:int=5, y:float=6, z:float=3)
            my_func(x:int=7, y:float=8, z:float=3)
            my_func(x:int=9, y:float=10, z:float=3)
            my_func(x:int=11, y:float=12, z:float=3)
        """
61
        )
Dean Moldovan's avatar
Dean Moldovan committed
62
63
64
        with capture:
            a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2
            assert np.allclose(f(a, b, c), a * b * c)
65
66
67
        assert (
            capture
            == """
Dean Moldovan's avatar
Dean Moldovan committed
68
69
70
71
72
73
74
            my_func(x:int=1, y:float=2, z:float=2)
            my_func(x:int=2, y:float=3, z:float=2)
            my_func(x:int=3, y:float=4, z:float=2)
            my_func(x:int=4, y:float=2, z:float=2)
            my_func(x:int=5, y:float=3, z:float=2)
            my_func(x:int=6, y:float=4, z:float=2)
        """
75
        )
Dean Moldovan's avatar
Dean Moldovan committed
76
77
78
        with capture:
            a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2
            assert np.allclose(f(a, b, c), a * b * c)
79
80
81
        assert (
            capture
            == """
Dean Moldovan's avatar
Dean Moldovan committed
82
83
84
85
86
87
88
            my_func(x:int=1, y:float=2, z:float=2)
            my_func(x:int=2, y:float=2, z:float=2)
            my_func(x:int=3, y:float=2, z:float=2)
            my_func(x:int=4, y:float=3, z:float=2)
            my_func(x:int=5, y:float=3, z:float=2)
            my_func(x:int=6, y:float=3, z:float=2)
        """
89
        )
90
        with capture:
91
92
93
94
95
            a, b, c = (
                np.array([[1, 2, 3], [4, 5, 6]], order="F"),
                np.array([[2], [3]]),
                2,
            )
96
            assert np.allclose(f(a, b, c), a * b * c)
97
98
99
        assert (
            capture
            == """
100
101
102
103
104
105
106
            my_func(x:int=1, y:float=2, z:float=2)
            my_func(x:int=2, y:float=2, z:float=2)
            my_func(x:int=3, y:float=2, z:float=2)
            my_func(x:int=4, y:float=3, z:float=2)
            my_func(x:int=5, y:float=3, z:float=2)
            my_func(x:int=6, y:float=3, z:float=2)
        """
107
        )
108
109
110
        with capture:
            a, b, c = np.array([[1, 2, 3], [4, 5, 6]])[::, ::2], np.array([[2], [3]]), 2
            assert np.allclose(f(a, b, c), a * b * c)
111
112
113
        assert (
            capture
            == """
114
115
116
117
118
            my_func(x:int=1, y:float=2, z:float=2)
            my_func(x:int=3, y:float=2, z:float=2)
            my_func(x:int=4, y:float=3, z:float=2)
            my_func(x:int=6, y:float=3, z:float=2)
        """
119
        )
120
        with capture:
121
122
123
124
125
            a, b, c = (
                np.array([[1, 2, 3], [4, 5, 6]], order="F")[::, ::2],
                np.array([[2], [3]]),
                2,
            )
126
            assert np.allclose(f(a, b, c), a * b * c)
127
128
129
        assert (
            capture
            == """
130
131
132
133
134
            my_func(x:int=1, y:float=2, z:float=2)
            my_func(x:int=3, y:float=2, z:float=2)
            my_func(x:int=4, y:float=3, z:float=2)
            my_func(x:int=6, y:float=3, z:float=2)
        """
135
        )
Dean Moldovan's avatar
Dean Moldovan committed
136
137


138
def test_type_selection():
139
140
    assert m.selective_func(np.array([1], dtype=np.int32)) == "Int branch taken."
    assert m.selective_func(np.array([1.0], dtype=np.float32)) == "Float branch taken."
141
142
143
144
    assert (
        m.selective_func(np.array([1.0j], dtype=np.complex64))
        == "Complex float branch taken."
    )
Dean Moldovan's avatar
Dean Moldovan committed
145
146
147


def test_docs(doc):
148
149
150
    assert (
        doc(m.vectorized_func)
        == """
151
        vectorized_func(arg0: numpy.ndarray[numpy.int32], arg1: numpy.ndarray[numpy.float32], arg2: numpy.ndarray[numpy.float64]) -> object
152
    """  # noqa: E501 line too long
153
    )
154
155
156


def test_trivial_broadcasting():
157
    trivial, vectorized_is_trivial = m.trivial, m.vectorized_is_trivial
158

159
160
    assert vectorized_is_trivial(1, 2, 3) == trivial.c_trivial
    assert vectorized_is_trivial(np.array(1), np.array(2), 3) == trivial.c_trivial
161
162
163
164
    assert (
        vectorized_is_trivial(np.array([1, 3]), np.array([2, 4]), 3)
        == trivial.c_trivial
    )
165
    assert trivial.c_trivial == vectorized_is_trivial(
166
167
168
169
170
171
172
173
174
175
176
177
178
        np.array([[1, 3, 5], [7, 9, 11]]), np.array([[2, 4, 6], [8, 10, 12]]), 3
    )
    assert (
        vectorized_is_trivial(np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2)
        == trivial.non_trivial
    )
    assert (
        vectorized_is_trivial(np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2)
        == trivial.non_trivial
    )
    z1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype="int32")
    z2 = np.array(z1, dtype="float32")
    z3 = np.array(z1, dtype="float64")
179
180
181
182
183
184
185
186
    assert vectorized_is_trivial(z1, z2, z3) == trivial.c_trivial
    assert vectorized_is_trivial(1, z2, z3) == trivial.c_trivial
    assert vectorized_is_trivial(z1, 1, z3) == trivial.c_trivial
    assert vectorized_is_trivial(z1, z2, 1) == trivial.c_trivial
    assert vectorized_is_trivial(z1[::2, ::2], 1, 1) == trivial.non_trivial
    assert vectorized_is_trivial(1, 1, z1[::2, ::2]) == trivial.c_trivial
    assert vectorized_is_trivial(1, 1, z3[::2, ::2]) == trivial.non_trivial
    assert vectorized_is_trivial(z1, 1, z3[1::4, 1::4]) == trivial.c_trivial
187

188
    y1 = np.array(z1, order="F")
189
190
    y2 = np.array(y1)
    y3 = np.array(y1)
191
192
193
194
195
196
197
198
    assert vectorized_is_trivial(y1, y2, y3) == trivial.f_trivial
    assert vectorized_is_trivial(y1, 1, 1) == trivial.f_trivial
    assert vectorized_is_trivial(1, y2, 1) == trivial.f_trivial
    assert vectorized_is_trivial(1, 1, y3) == trivial.f_trivial
    assert vectorized_is_trivial(y1, z2, 1) == trivial.non_trivial
    assert vectorized_is_trivial(z1[1::4, 1::4], y2, 1) == trivial.f_trivial
    assert vectorized_is_trivial(y1[1::4, 1::4], z2, 1) == trivial.c_trivial

199
200
201
202
203
204
    assert m.vectorized_func(z1, z2, z3).flags.c_contiguous
    assert m.vectorized_func(y1, y2, y3).flags.f_contiguous
    assert m.vectorized_func(z1, 1, 1).flags.c_contiguous
    assert m.vectorized_func(1, y2, 1).flags.f_contiguous
    assert m.vectorized_func(z1[1::4, 1::4], y2, 1).flags.f_contiguous
    assert m.vectorized_func(y1[1::4, 1::4], z2, 1).flags.c_contiguous
205
206
207


def test_passthrough_arguments(doc):
208
    assert doc(m.vec_passthrough) == (
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
        "vec_passthrough("
        + ", ".join(
            [
                "arg0: float",
                "arg1: numpy.ndarray[numpy.float64]",
                "arg2: numpy.ndarray[numpy.float64]",
                "arg3: numpy.ndarray[numpy.int32]",
                "arg4: int",
                "arg5: m.numpy_vectorize.NonPODClass",
                "arg6: numpy.ndarray[numpy.float64]",
            ]
        )
        + ") -> object"
    )

    b = np.array([[10, 20, 30]], dtype="float64")
225
    c = np.array([100, 200])  # NOT a vectorized argument
226
227
    d = np.array([[1000], [2000], [3000]], dtype="int")
    g = np.array([[1000000, 2000000, 3000000]], dtype="int")  # requires casting
228
    assert np.all(
229
230
231
232
233
234
235
236
237
        m.vec_passthrough(1, b, c, d, 10000, m.NonPODClass(100000), g)
        == np.array(
            [
                [1111111, 2111121, 3111131],
                [1112111, 2112121, 3112131],
                [1113111, 2113121, 3113131],
            ]
        )
    )
238
239
240


def test_method_vectorization():
241
    o = m.VectorizeTestClass(3)
242
243
    x = np.array([1, 2], dtype="int")
    y = np.array([[10], [20]], dtype="float32")
244
245
246
247
    assert np.all(o.method(x, y) == [[14, 15], [24, 25]])


def test_array_collapse():
248
249
250
    assert not isinstance(m.vectorized_func(1, 2, 3), np.ndarray)
    assert not isinstance(m.vectorized_func(np.array(1), 2, 3), np.ndarray)
    z = m.vectorized_func([1], 2, 3)
251
    assert isinstance(z, np.ndarray)
252
    assert z.shape == (1,)
253
    z = m.vectorized_func(1, [[[2]]], 3)
254
255
    assert isinstance(z, np.ndarray)
    assert z.shape == (1, 1, 1)
256
257
258
259
260
261
262
263
264
265
266


def test_vectorized_noreturn():
    x = m.NonPODClass(0)
    assert x.value == 0
    m.add_to(x, [1, 2, 3, 4])
    assert x.value == 10
    m.add_to(x, 1)
    assert x.value == 11
    m.add_to(x, [[1, 1], [2, 3]])
    assert x.value == 18