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