1# -*- coding: utf-8 -*-
2import pytest
3
4from pybind11_tests import numpy_vectorize as m
5
6np = pytest.importorskip("numpy")
7
8
9def test_vectorize(capture):
10    assert np.isclose(m.vectorized_func3(np.array(3 + 7j)), [6 + 14j])
11
12    for f in [m.vectorized_func, m.vectorized_func2]:
13        with capture:
14            assert np.isclose(f(1, 2, 3), 6)
15        assert capture == "my_func(x:int=1, y:float=2, z:float=3)"
16        with capture:
17            assert np.isclose(f(np.array(1), np.array(2), 3), 6)
18        assert capture == "my_func(x:int=1, y:float=2, z:float=3)"
19        with capture:
20            assert np.allclose(f(np.array([1, 3]), np.array([2, 4]), 3), [6, 36])
21        assert (
22            capture
23            == """
24            my_func(x:int=1, y:float=2, z:float=3)
25            my_func(x:int=3, y:float=4, z:float=3)
26        """
27        )
28        with capture:
29            a = np.array([[1, 2], [3, 4]], order="F")
30            b = np.array([[10, 20], [30, 40]], order="F")
31            c = 3
32            result = f(a, b, c)
33            assert np.allclose(result, a * b * c)
34            assert result.flags.f_contiguous
35        # All inputs are F order and full or singletons, so we the result is in col-major order:
36        assert (
37            capture
38            == """
39            my_func(x:int=1, y:float=10, z:float=3)
40            my_func(x:int=3, y:float=30, z:float=3)
41            my_func(x:int=2, y:float=20, z:float=3)
42            my_func(x:int=4, y:float=40, z:float=3)
43        """
44        )
45        with capture:
46            a, b, c = (
47                np.array([[1, 3, 5], [7, 9, 11]]),
48                np.array([[2, 4, 6], [8, 10, 12]]),
49                3,
50            )
51            assert np.allclose(f(a, b, c), a * b * c)
52        assert (
53            capture
54            == """
55            my_func(x:int=1, y:float=2, z:float=3)
56            my_func(x:int=3, y:float=4, z:float=3)
57            my_func(x:int=5, y:float=6, z:float=3)
58            my_func(x:int=7, y:float=8, z:float=3)
59            my_func(x:int=9, y:float=10, z:float=3)
60            my_func(x:int=11, y:float=12, z:float=3)
61        """
62        )
63        with capture:
64            a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2
65            assert np.allclose(f(a, b, c), a * b * c)
66        assert (
67            capture
68            == """
69            my_func(x:int=1, y:float=2, z:float=2)
70            my_func(x:int=2, y:float=3, z:float=2)
71            my_func(x:int=3, y:float=4, z:float=2)
72            my_func(x:int=4, y:float=2, z:float=2)
73            my_func(x:int=5, y:float=3, z:float=2)
74            my_func(x:int=6, y:float=4, z:float=2)
75        """
76        )
77        with capture:
78            a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2
79            assert np.allclose(f(a, b, c), a * b * c)
80        assert (
81            capture
82            == """
83            my_func(x:int=1, y:float=2, z:float=2)
84            my_func(x:int=2, y:float=2, z:float=2)
85            my_func(x:int=3, y:float=2, z:float=2)
86            my_func(x:int=4, y:float=3, z:float=2)
87            my_func(x:int=5, y:float=3, z:float=2)
88            my_func(x:int=6, y:float=3, z:float=2)
89        """
90        )
91        with capture:
92            a, b, c = (
93                np.array([[1, 2, 3], [4, 5, 6]], order="F"),
94                np.array([[2], [3]]),
95                2,
96            )
97            assert np.allclose(f(a, b, c), a * b * c)
98        assert (
99            capture
100            == """
101            my_func(x:int=1, y:float=2, z:float=2)
102            my_func(x:int=2, y:float=2, z:float=2)
103            my_func(x:int=3, y:float=2, z:float=2)
104            my_func(x:int=4, y:float=3, z:float=2)
105            my_func(x:int=5, y:float=3, z:float=2)
106            my_func(x:int=6, y:float=3, z:float=2)
107        """
108        )
109        with capture:
110            a, b, c = np.array([[1, 2, 3], [4, 5, 6]])[::, ::2], np.array([[2], [3]]), 2
111            assert np.allclose(f(a, b, c), a * b * c)
112        assert (
113            capture
114            == """
115            my_func(x:int=1, y:float=2, z:float=2)
116            my_func(x:int=3, y:float=2, z:float=2)
117            my_func(x:int=4, y:float=3, z:float=2)
118            my_func(x:int=6, y:float=3, z:float=2)
119        """
120        )
121        with capture:
122            a, b, c = (
123                np.array([[1, 2, 3], [4, 5, 6]], order="F")[::, ::2],
124                np.array([[2], [3]]),
125                2,
126            )
127            assert np.allclose(f(a, b, c), a * b * c)
128        assert (
129            capture
130            == """
131            my_func(x:int=1, y:float=2, z:float=2)
132            my_func(x:int=3, y:float=2, z:float=2)
133            my_func(x:int=4, y:float=3, z:float=2)
134            my_func(x:int=6, y:float=3, z:float=2)
135        """
136        )
137
138
139def test_type_selection():
140    assert m.selective_func(np.array([1], dtype=np.int32)) == "Int branch taken."
141    assert m.selective_func(np.array([1.0], dtype=np.float32)) == "Float branch taken."
142    assert (
143        m.selective_func(np.array([1.0j], dtype=np.complex64))
144        == "Complex float branch taken."
145    )
146
147
148def test_docs(doc):
149    assert (
150        doc(m.vectorized_func)
151        == """
152        vectorized_func(arg0: numpy.ndarray[numpy.int32], arg1: numpy.ndarray[numpy.float32], arg2: numpy.ndarray[numpy.float64]) -> object
153    """  # noqa: E501 line too long
154    )
155
156
157def test_trivial_broadcasting():
158    trivial, vectorized_is_trivial = m.trivial, m.vectorized_is_trivial
159
160    assert vectorized_is_trivial(1, 2, 3) == trivial.c_trivial
161    assert vectorized_is_trivial(np.array(1), np.array(2), 3) == trivial.c_trivial
162    assert (
163        vectorized_is_trivial(np.array([1, 3]), np.array([2, 4]), 3)
164        == trivial.c_trivial
165    )
166    assert trivial.c_trivial == vectorized_is_trivial(
167        np.array([[1, 3, 5], [7, 9, 11]]), np.array([[2, 4, 6], [8, 10, 12]]), 3
168    )
169    assert (
170        vectorized_is_trivial(np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2)
171        == trivial.non_trivial
172    )
173    assert (
174        vectorized_is_trivial(np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2)
175        == trivial.non_trivial
176    )
177    z1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype="int32")
178    z2 = np.array(z1, dtype="float32")
179    z3 = np.array(z1, dtype="float64")
180    assert vectorized_is_trivial(z1, z2, z3) == trivial.c_trivial
181    assert vectorized_is_trivial(1, z2, z3) == trivial.c_trivial
182    assert vectorized_is_trivial(z1, 1, z3) == trivial.c_trivial
183    assert vectorized_is_trivial(z1, z2, 1) == trivial.c_trivial
184    assert vectorized_is_trivial(z1[::2, ::2], 1, 1) == trivial.non_trivial
185    assert vectorized_is_trivial(1, 1, z1[::2, ::2]) == trivial.c_trivial
186    assert vectorized_is_trivial(1, 1, z3[::2, ::2]) == trivial.non_trivial
187    assert vectorized_is_trivial(z1, 1, z3[1::4, 1::4]) == trivial.c_trivial
188
189    y1 = np.array(z1, order="F")
190    y2 = np.array(y1)
191    y3 = np.array(y1)
192    assert vectorized_is_trivial(y1, y2, y3) == trivial.f_trivial
193    assert vectorized_is_trivial(y1, 1, 1) == trivial.f_trivial
194    assert vectorized_is_trivial(1, y2, 1) == trivial.f_trivial
195    assert vectorized_is_trivial(1, 1, y3) == trivial.f_trivial
196    assert vectorized_is_trivial(y1, z2, 1) == trivial.non_trivial
197    assert vectorized_is_trivial(z1[1::4, 1::4], y2, 1) == trivial.f_trivial
198    assert vectorized_is_trivial(y1[1::4, 1::4], z2, 1) == trivial.c_trivial
199
200    assert m.vectorized_func(z1, z2, z3).flags.c_contiguous
201    assert m.vectorized_func(y1, y2, y3).flags.f_contiguous
202    assert m.vectorized_func(z1, 1, 1).flags.c_contiguous
203    assert m.vectorized_func(1, y2, 1).flags.f_contiguous
204    assert m.vectorized_func(z1[1::4, 1::4], y2, 1).flags.f_contiguous
205    assert m.vectorized_func(y1[1::4, 1::4], z2, 1).flags.c_contiguous
206
207
208def test_passthrough_arguments(doc):
209    assert doc(m.vec_passthrough) == (
210        "vec_passthrough("
211        + ", ".join(
212            [
213                "arg0: float",
214                "arg1: numpy.ndarray[numpy.float64]",
215                "arg2: numpy.ndarray[numpy.float64]",
216                "arg3: numpy.ndarray[numpy.int32]",
217                "arg4: int",
218                "arg5: m.numpy_vectorize.NonPODClass",
219                "arg6: numpy.ndarray[numpy.float64]",
220            ]
221        )
222        + ") -> object"
223    )
224
225    b = np.array([[10, 20, 30]], dtype="float64")
226    c = np.array([100, 200])  # NOT a vectorized argument
227    d = np.array([[1000], [2000], [3000]], dtype="int")
228    g = np.array([[1000000, 2000000, 3000000]], dtype="int")  # requires casting
229    assert np.all(
230        m.vec_passthrough(1, b, c, d, 10000, m.NonPODClass(100000), g)
231        == np.array(
232            [
233                [1111111, 2111121, 3111131],
234                [1112111, 2112121, 3112131],
235                [1113111, 2113121, 3113131],
236            ]
237        )
238    )
239
240
241def test_method_vectorization():
242    o = m.VectorizeTestClass(3)
243    x = np.array([1, 2], dtype="int")
244    y = np.array([[10], [20]], dtype="float32")
245    assert np.all(o.method(x, y) == [[14, 15], [24, 25]])
246
247
248def test_array_collapse():
249    assert not isinstance(m.vectorized_func(1, 2, 3), np.ndarray)
250    assert not isinstance(m.vectorized_func(np.array(1), 2, 3), np.ndarray)
251    z = m.vectorized_func([1], 2, 3)
252    assert isinstance(z, np.ndarray)
253    assert z.shape == (1,)
254    z = m.vectorized_func(1, [[[2]]], 3)
255    assert isinstance(z, np.ndarray)
256    assert z.shape == (1, 1, 1)
257
258
259def test_vectorized_noreturn():
260    x = m.NonPODClass(0)
261    assert x.value == 0
262    m.add_to(x, [1, 2, 3, 4])
263    assert x.value == 10
264    m.add_to(x, 1)
265    assert x.value == 11
266    m.add_to(x, [[1, 1], [2, 3]])
267    assert x.value == 18
268