Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:55

0001 import pytest
0002 
0003 from pybind11_tests import numpy_vectorize as m
0004 
0005 np = pytest.importorskip("numpy")
0006 
0007 
0008 def test_vectorize(capture):
0009     assert np.isclose(m.vectorized_func3(np.array(3 + 7j)), [6 + 14j])
0010 
0011     for f in [m.vectorized_func, m.vectorized_func2]:
0012         with capture:
0013             assert np.isclose(f(1, 2, 3), 6)
0014         assert capture == "my_func(x:int=1, y:float=2, z:float=3)"
0015         with capture:
0016             assert np.isclose(f(np.array(1), np.array(2), 3), 6)
0017         assert capture == "my_func(x:int=1, y:float=2, z:float=3)"
0018         with capture:
0019             assert np.allclose(f(np.array([1, 3]), np.array([2, 4]), 3), [6, 36])
0020         assert (
0021             capture
0022             == """
0023             my_func(x:int=1, y:float=2, z:float=3)
0024             my_func(x:int=3, y:float=4, z:float=3)
0025         """
0026         )
0027         with capture:
0028             a = np.array([[1, 2], [3, 4]], order="F")
0029             b = np.array([[10, 20], [30, 40]], order="F")
0030             c = 3
0031             result = f(a, b, c)
0032             assert np.allclose(result, a * b * c)
0033             assert result.flags.f_contiguous
0034         # All inputs are F order and full or singletons, so we the result is in col-major order:
0035         assert (
0036             capture
0037             == """
0038             my_func(x:int=1, y:float=10, z:float=3)
0039             my_func(x:int=3, y:float=30, z:float=3)
0040             my_func(x:int=2, y:float=20, z:float=3)
0041             my_func(x:int=4, y:float=40, z:float=3)
0042         """
0043         )
0044         with capture:
0045             a, b, c = (
0046                 np.array([[1, 3, 5], [7, 9, 11]]),
0047                 np.array([[2, 4, 6], [8, 10, 12]]),
0048                 3,
0049             )
0050             assert np.allclose(f(a, b, c), a * b * c)
0051         assert (
0052             capture
0053             == """
0054             my_func(x:int=1, y:float=2, z:float=3)
0055             my_func(x:int=3, y:float=4, z:float=3)
0056             my_func(x:int=5, y:float=6, z:float=3)
0057             my_func(x:int=7, y:float=8, z:float=3)
0058             my_func(x:int=9, y:float=10, z:float=3)
0059             my_func(x:int=11, y:float=12, z:float=3)
0060         """
0061         )
0062         with capture:
0063             a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2
0064             assert np.allclose(f(a, b, c), a * b * c)
0065         assert (
0066             capture
0067             == """
0068             my_func(x:int=1, y:float=2, z:float=2)
0069             my_func(x:int=2, y:float=3, z:float=2)
0070             my_func(x:int=3, y:float=4, z:float=2)
0071             my_func(x:int=4, y:float=2, z:float=2)
0072             my_func(x:int=5, y:float=3, z:float=2)
0073             my_func(x:int=6, y:float=4, z:float=2)
0074         """
0075         )
0076         with capture:
0077             a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2
0078             assert np.allclose(f(a, b, c), a * b * c)
0079         assert (
0080             capture
0081             == """
0082             my_func(x:int=1, y:float=2, z:float=2)
0083             my_func(x:int=2, y:float=2, z:float=2)
0084             my_func(x:int=3, y:float=2, z:float=2)
0085             my_func(x:int=4, y:float=3, z:float=2)
0086             my_func(x:int=5, y:float=3, z:float=2)
0087             my_func(x:int=6, y:float=3, z:float=2)
0088         """
0089         )
0090         with capture:
0091             a, b, c = (
0092                 np.array([[1, 2, 3], [4, 5, 6]], order="F"),
0093                 np.array([[2], [3]]),
0094                 2,
0095             )
0096             assert np.allclose(f(a, b, c), a * b * c)
0097         assert (
0098             capture
0099             == """
0100             my_func(x:int=1, y:float=2, z:float=2)
0101             my_func(x:int=2, y:float=2, z:float=2)
0102             my_func(x:int=3, y:float=2, z:float=2)
0103             my_func(x:int=4, y:float=3, z:float=2)
0104             my_func(x:int=5, y:float=3, z:float=2)
0105             my_func(x:int=6, y:float=3, z:float=2)
0106         """
0107         )
0108         with capture:
0109             a, b, c = np.array([[1, 2, 3], [4, 5, 6]])[::, ::2], np.array([[2], [3]]), 2
0110             assert np.allclose(f(a, b, c), a * b * c)
0111         assert (
0112             capture
0113             == """
0114             my_func(x:int=1, y:float=2, z:float=2)
0115             my_func(x:int=3, y:float=2, z:float=2)
0116             my_func(x:int=4, y:float=3, z:float=2)
0117             my_func(x:int=6, y:float=3, z:float=2)
0118         """
0119         )
0120         with capture:
0121             a, b, c = (
0122                 np.array([[1, 2, 3], [4, 5, 6]], order="F")[::, ::2],
0123                 np.array([[2], [3]]),
0124                 2,
0125             )
0126             assert np.allclose(f(a, b, c), a * b * c)
0127         assert (
0128             capture
0129             == """
0130             my_func(x:int=1, y:float=2, z:float=2)
0131             my_func(x:int=3, y:float=2, z:float=2)
0132             my_func(x:int=4, y:float=3, z:float=2)
0133             my_func(x:int=6, y:float=3, z:float=2)
0134         """
0135         )
0136 
0137 
0138 def test_type_selection():
0139     assert m.selective_func(np.array([1], dtype=np.int32)) == "Int branch taken."
0140     assert m.selective_func(np.array([1.0], dtype=np.float32)) == "Float branch taken."
0141     assert (
0142         m.selective_func(np.array([1.0j], dtype=np.complex64))
0143         == "Complex float branch taken."
0144     )
0145 
0146 
0147 def test_docs(doc):
0148     assert (
0149         doc(m.vectorized_func)
0150         == """
0151         vectorized_func(arg0: numpy.ndarray[numpy.int32], arg1: numpy.ndarray[numpy.float32], arg2: numpy.ndarray[numpy.float64]) -> object
0152     """  # noqa: E501 line too long
0153     )
0154 
0155 
0156 def test_trivial_broadcasting():
0157     trivial, vectorized_is_trivial = m.trivial, m.vectorized_is_trivial
0158 
0159     assert vectorized_is_trivial(1, 2, 3) == trivial.c_trivial
0160     assert vectorized_is_trivial(np.array(1), np.array(2), 3) == trivial.c_trivial
0161     assert (
0162         vectorized_is_trivial(np.array([1, 3]), np.array([2, 4]), 3)
0163         == trivial.c_trivial
0164     )
0165     assert trivial.c_trivial == vectorized_is_trivial(
0166         np.array([[1, 3, 5], [7, 9, 11]]), np.array([[2, 4, 6], [8, 10, 12]]), 3
0167     )
0168     assert (
0169         vectorized_is_trivial(np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2)
0170         == trivial.non_trivial
0171     )
0172     assert (
0173         vectorized_is_trivial(np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2)
0174         == trivial.non_trivial
0175     )
0176     z1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype="int32")
0177     z2 = np.array(z1, dtype="float32")
0178     z3 = np.array(z1, dtype="float64")
0179     assert vectorized_is_trivial(z1, z2, z3) == trivial.c_trivial
0180     assert vectorized_is_trivial(1, z2, z3) == trivial.c_trivial
0181     assert vectorized_is_trivial(z1, 1, z3) == trivial.c_trivial
0182     assert vectorized_is_trivial(z1, z2, 1) == trivial.c_trivial
0183     assert vectorized_is_trivial(z1[::2, ::2], 1, 1) == trivial.non_trivial
0184     assert vectorized_is_trivial(1, 1, z1[::2, ::2]) == trivial.c_trivial
0185     assert vectorized_is_trivial(1, 1, z3[::2, ::2]) == trivial.non_trivial
0186     assert vectorized_is_trivial(z1, 1, z3[1::4, 1::4]) == trivial.c_trivial
0187 
0188     y1 = np.array(z1, order="F")
0189     y2 = np.array(y1)
0190     y3 = np.array(y1)
0191     assert vectorized_is_trivial(y1, y2, y3) == trivial.f_trivial
0192     assert vectorized_is_trivial(y1, 1, 1) == trivial.f_trivial
0193     assert vectorized_is_trivial(1, y2, 1) == trivial.f_trivial
0194     assert vectorized_is_trivial(1, 1, y3) == trivial.f_trivial
0195     assert vectorized_is_trivial(y1, z2, 1) == trivial.non_trivial
0196     assert vectorized_is_trivial(z1[1::4, 1::4], y2, 1) == trivial.f_trivial
0197     assert vectorized_is_trivial(y1[1::4, 1::4], z2, 1) == trivial.c_trivial
0198 
0199     assert m.vectorized_func(z1, z2, z3).flags.c_contiguous
0200     assert m.vectorized_func(y1, y2, y3).flags.f_contiguous
0201     assert m.vectorized_func(z1, 1, 1).flags.c_contiguous
0202     assert m.vectorized_func(1, y2, 1).flags.f_contiguous
0203     assert m.vectorized_func(z1[1::4, 1::4], y2, 1).flags.f_contiguous
0204     assert m.vectorized_func(y1[1::4, 1::4], z2, 1).flags.c_contiguous
0205 
0206 
0207 def test_passthrough_arguments(doc):
0208     assert doc(m.vec_passthrough) == (
0209         "vec_passthrough("
0210         + ", ".join(
0211             [
0212                 "arg0: float",
0213                 "arg1: numpy.ndarray[numpy.float64]",
0214                 "arg2: numpy.ndarray[numpy.float64]",
0215                 "arg3: numpy.ndarray[numpy.int32]",
0216                 "arg4: int",
0217                 "arg5: m.numpy_vectorize.NonPODClass",
0218                 "arg6: numpy.ndarray[numpy.float64]",
0219             ]
0220         )
0221         + ") -> object"
0222     )
0223 
0224     b = np.array([[10, 20, 30]], dtype="float64")
0225     c = np.array([100, 200])  # NOT a vectorized argument
0226     d = np.array([[1000], [2000], [3000]], dtype="int")
0227     g = np.array([[1000000, 2000000, 3000000]], dtype="int")  # requires casting
0228     assert np.all(
0229         m.vec_passthrough(1, b, c, d, 10000, m.NonPODClass(100000), g)
0230         == np.array(
0231             [
0232                 [1111111, 2111121, 3111131],
0233                 [1112111, 2112121, 3112131],
0234                 [1113111, 2113121, 3113131],
0235             ]
0236         )
0237     )
0238 
0239 
0240 def test_method_vectorization():
0241     o = m.VectorizeTestClass(3)
0242     x = np.array([1, 2], dtype="int")
0243     y = np.array([[10], [20]], dtype="float32")
0244     assert np.all(o.method(x, y) == [[14, 15], [24, 25]])
0245 
0246 
0247 def test_array_collapse():
0248     assert not isinstance(m.vectorized_func(1, 2, 3), np.ndarray)
0249     assert not isinstance(m.vectorized_func(np.array(1), 2, 3), np.ndarray)
0250     z = m.vectorized_func([1], 2, 3)
0251     assert isinstance(z, np.ndarray)
0252     assert z.shape == (1,)
0253     z = m.vectorized_func(1, [[[2]]], 3)
0254     assert isinstance(z, np.ndarray)
0255     assert z.shape == (1, 1, 1)
0256 
0257 
0258 def test_vectorized_noreturn():
0259     x = m.NonPODClass(0)
0260     assert x.value == 0
0261     m.add_to(x, [1, 2, 3, 4])
0262     assert x.value == 10
0263     m.add_to(x, 1)
0264     assert x.value == 11
0265     m.add_to(x, [[1, 1], [2, 3]])
0266     assert x.value == 18