blob: 362b0362628dd7e0dba0eec57a9739bb611050fb [file] [log] [blame]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02001import pytest
2
Jason Rhinelander2a757842017-01-24 11:26:51 -05003pytestmark = pytest.requires_numpy
4
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02005with pytest.suppress(ImportError):
6 import numpy as np
7
8
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02009def test_vectorize(capture):
10 from pybind11_tests import vectorized_func, vectorized_func2, vectorized_func3
11
12 assert np.isclose(vectorized_func3(np.array(3 + 7j)), [6 + 14j])
13
14 for f in [vectorized_func, vectorized_func2]:
15 with capture:
16 assert np.isclose(f(1, 2, 3), 6)
17 assert capture == "my_func(x:int=1, y:float=2, z:float=3)"
18 with capture:
19 assert np.isclose(f(np.array(1), np.array(2), 3), 6)
20 assert capture == "my_func(x:int=1, y:float=2, z:float=3)"
21 with capture:
22 assert np.allclose(f(np.array([1, 3]), np.array([2, 4]), 3), [6, 36])
23 assert capture == """
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 with capture:
Jason Rhinelanderb0292c12017-03-18 21:11:59 -030028 a = np.array([[1, 2], [3, 4]], order='F')
29 b = np.array([[10, 20], [30, 40]], order='F')
30 c = 3
31 result = f(a, b, c)
32 assert np.allclose(result, a * b * c)
33 assert result.flags.f_contiguous
34 # All inputs are F order and full or singletons, so we the result is in col-major order:
35 assert capture == """
36 my_func(x:int=1, y:float=10, z:float=3)
37 my_func(x:int=3, y:float=30, z:float=3)
38 my_func(x:int=2, y:float=20, z:float=3)
39 my_func(x:int=4, y:float=40, z:float=3)
40 """
41 with capture:
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020042 a, b, c = np.array([[1, 3, 5], [7, 9, 11]]), np.array([[2, 4, 6], [8, 10, 12]]), 3
43 assert np.allclose(f(a, b, c), a * b * c)
44 assert capture == """
45 my_func(x:int=1, y:float=2, z:float=3)
46 my_func(x:int=3, y:float=4, z:float=3)
47 my_func(x:int=5, y:float=6, z:float=3)
48 my_func(x:int=7, y:float=8, z:float=3)
49 my_func(x:int=9, y:float=10, z:float=3)
50 my_func(x:int=11, y:float=12, z:float=3)
51 """
52 with capture:
53 a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2
54 assert np.allclose(f(a, b, c), a * b * c)
55 assert capture == """
56 my_func(x:int=1, y:float=2, z:float=2)
57 my_func(x:int=2, y:float=3, z:float=2)
58 my_func(x:int=3, y:float=4, z:float=2)
59 my_func(x:int=4, y:float=2, z:float=2)
60 my_func(x:int=5, y:float=3, z:float=2)
61 my_func(x:int=6, y:float=4, z:float=2)
62 """
63 with capture:
64 a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2
65 assert np.allclose(f(a, b, c), a * b * c)
66 assert capture == """
67 my_func(x:int=1, y:float=2, z:float=2)
68 my_func(x:int=2, y:float=2, z:float=2)
69 my_func(x:int=3, y:float=2, z:float=2)
70 my_func(x:int=4, y:float=3, z:float=2)
71 my_func(x:int=5, y:float=3, z:float=2)
72 my_func(x:int=6, y:float=3, z:float=2)
73 """
Jason Rhinelanderae5a8f72017-03-15 00:57:56 -030074 with capture:
75 a, b, c = np.array([[1, 2, 3], [4, 5, 6]], order='F'), np.array([[2], [3]]), 2
76 assert np.allclose(f(a, b, c), a * b * c)
77 assert capture == """
78 my_func(x:int=1, y:float=2, z:float=2)
79 my_func(x:int=2, y:float=2, z:float=2)
80 my_func(x:int=3, y:float=2, z:float=2)
81 my_func(x:int=4, y:float=3, z:float=2)
82 my_func(x:int=5, y:float=3, z:float=2)
83 my_func(x:int=6, y:float=3, z:float=2)
84 """
85 with capture:
86 a, b, c = np.array([[1, 2, 3], [4, 5, 6]])[::, ::2], np.array([[2], [3]]), 2
87 assert np.allclose(f(a, b, c), a * b * c)
88 assert capture == """
89 my_func(x:int=1, y:float=2, z:float=2)
90 my_func(x:int=3, y:float=2, z:float=2)
91 my_func(x:int=4, y:float=3, z:float=2)
92 my_func(x:int=6, y:float=3, z:float=2)
93 """
94 with capture:
95 a, b, c = np.array([[1, 2, 3], [4, 5, 6]], order='F')[::, ::2], np.array([[2], [3]]), 2
96 assert np.allclose(f(a, b, c), a * b * c)
97 assert capture == """
98 my_func(x:int=1, y:float=2, z:float=2)
99 my_func(x:int=3, y:float=2, z:float=2)
100 my_func(x:int=4, y:float=3, z:float=2)
101 my_func(x:int=6, y:float=3, z:float=2)
102 """
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200103
104
Dean Moldovan665e8802016-08-12 22:28:31 +0200105def test_type_selection():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200106 from pybind11_tests import selective_func
107
Dean Moldovan665e8802016-08-12 22:28:31 +0200108 assert selective_func(np.array([1], dtype=np.int32)) == "Int branch taken."
109 assert selective_func(np.array([1.0], dtype=np.float32)) == "Float branch taken."
110 assert selective_func(np.array([1.0j], dtype=np.complex64)) == "Complex float branch taken."
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200111
112
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200113def test_docs(doc):
114 from pybind11_tests import vectorized_func
115
Dean Moldovan76e993a2016-12-13 00:59:28 +0100116 assert doc(vectorized_func) == """
Dean Moldovan16afbce2017-03-13 19:17:18 +0100117 vectorized_func(arg0: numpy.ndarray[int32], arg1: numpy.ndarray[float32], arg2: numpy.ndarray[float64]) -> object
Dean Moldovan76e993a2016-12-13 00:59:28 +0100118 """ # noqa: E501 line too long
Jason Rhinelanderae5a8f72017-03-15 00:57:56 -0300119
120
121def test_trivial_broadcasting():
Jason Rhinelanderb0292c12017-03-18 21:11:59 -0300122 from pybind11_tests import vectorized_is_trivial, trivial, vectorized_func
Jason Rhinelanderae5a8f72017-03-15 00:57:56 -0300123
Jason Rhinelanderb0292c12017-03-18 21:11:59 -0300124 assert vectorized_is_trivial(1, 2, 3) == trivial.c_trivial
125 assert vectorized_is_trivial(np.array(1), np.array(2), 3) == trivial.c_trivial
126 assert vectorized_is_trivial(np.array([1, 3]), np.array([2, 4]), 3) == trivial.c_trivial
127 assert trivial.c_trivial == vectorized_is_trivial(
Jason Rhinelanderae5a8f72017-03-15 00:57:56 -0300128 np.array([[1, 3, 5], [7, 9, 11]]), np.array([[2, 4, 6], [8, 10, 12]]), 3)
Jason Rhinelanderb0292c12017-03-18 21:11:59 -0300129 assert vectorized_is_trivial(
130 np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2) == trivial.non_trivial
131 assert vectorized_is_trivial(
132 np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2) == trivial.non_trivial
Jason Rhinelanderae5a8f72017-03-15 00:57:56 -0300133 z1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype='int32')
134 z2 = np.array(z1, dtype='float32')
135 z3 = np.array(z1, dtype='float64')
Jason Rhinelanderb0292c12017-03-18 21:11:59 -0300136 assert vectorized_is_trivial(z1, z2, z3) == trivial.c_trivial
137 assert vectorized_is_trivial(1, z2, z3) == trivial.c_trivial
138 assert vectorized_is_trivial(z1, 1, z3) == trivial.c_trivial
139 assert vectorized_is_trivial(z1, z2, 1) == trivial.c_trivial
140 assert vectorized_is_trivial(z1[::2, ::2], 1, 1) == trivial.non_trivial
141 assert vectorized_is_trivial(1, 1, z1[::2, ::2]) == trivial.c_trivial
142 assert vectorized_is_trivial(1, 1, z3[::2, ::2]) == trivial.non_trivial
143 assert vectorized_is_trivial(z1, 1, z3[1::4, 1::4]) == trivial.c_trivial
Jason Rhinelanderae5a8f72017-03-15 00:57:56 -0300144
145 y1 = np.array(z1, order='F')
146 y2 = np.array(y1)
147 y3 = np.array(y1)
Jason Rhinelanderb0292c12017-03-18 21:11:59 -0300148 assert vectorized_is_trivial(y1, y2, y3) == trivial.f_trivial
149 assert vectorized_is_trivial(y1, 1, 1) == trivial.f_trivial
150 assert vectorized_is_trivial(1, y2, 1) == trivial.f_trivial
151 assert vectorized_is_trivial(1, 1, y3) == trivial.f_trivial
152 assert vectorized_is_trivial(y1, z2, 1) == trivial.non_trivial
153 assert vectorized_is_trivial(z1[1::4, 1::4], y2, 1) == trivial.f_trivial
154 assert vectorized_is_trivial(y1[1::4, 1::4], z2, 1) == trivial.c_trivial
155
156 assert vectorized_func(z1, z2, z3).flags.c_contiguous
157 assert vectorized_func(y1, y2, y3).flags.f_contiguous
158 assert vectorized_func(z1, 1, 1).flags.c_contiguous
159 assert vectorized_func(1, y2, 1).flags.f_contiguous
160 assert vectorized_func(z1[1::4, 1::4], y2, 1).flags.f_contiguous
161 assert vectorized_func(y1[1::4, 1::4], z2, 1).flags.c_contiguous
Jason Rhinelanderf3ce00e2017-03-26 00:51:40 -0300162
163
164def test_passthrough_arguments(doc):
165 from pybind11_tests import vec_passthrough, NonPODClass
166
167 assert doc(vec_passthrough) == (
168 "vec_passthrough("
169 "arg0: float, arg1: numpy.ndarray[float64], arg2: numpy.ndarray[float64], "
170 "arg3: numpy.ndarray[int32], arg4: int, arg5: m.NonPODClass, arg6: numpy.ndarray[float64]"
171 ") -> object")
172
173 b = np.array([[10, 20, 30]], dtype='float64')
174 c = np.array([100, 200]) # NOT a vectorized argument
175 d = np.array([[1000], [2000], [3000]], dtype='int')
176 g = np.array([[1000000, 2000000, 3000000]], dtype='int') # requires casting
177 assert np.all(
178 vec_passthrough(1, b, c, d, 10000, NonPODClass(100000), g) ==
179 np.array([[1111111, 2111121, 3111131],
180 [1112111, 2112121, 3112131],
181 [1113111, 2113121, 3113131]]))
182
183
184def test_method_vectorization():
185 from pybind11_tests import VectorizeTestClass
186
187 o = VectorizeTestClass(3)
188 x = np.array([1, 2], dtype='int')
189 y = np.array([[10], [20]], dtype='float32')
190 assert np.all(o.method(x, y) == [[14, 15], [24, 25]])
191
192
193def test_array_collapse():
194 from pybind11_tests import vectorized_func
195
196 assert not isinstance(vectorized_func(1, 2, 3), np.ndarray)
197 assert not isinstance(vectorized_func(np.array(1), 2, 3), np.ndarray)
198 z = vectorized_func([1], 2, 3)
199 assert isinstance(z, np.ndarray)
200 assert z.shape == (1, )
201 z = vectorized_func(1, [[[2]]], 3)
202 assert isinstance(z, np.ndarray)
203 assert z.shape == (1, 1, 1)