test_numpy_vectorize.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 import pytest
3 from pybind11_tests import numpy_vectorize as m
4 
5 np = pytest.importorskip("numpy")
6 
7 
8 def 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 capture == """
21  my_func(x:int=1, y:float=2, z:float=3)
22  my_func(x:int=3, y:float=4, z:float=3)
23  """
24  with capture:
25  a = np.array([[1, 2], [3, 4]], order='F')
26  b = np.array([[10, 20], [30, 40]], order='F')
27  c = 3
28  result = f(a, b, c)
29  assert np.allclose(result, a * b * c)
30  assert result.flags.f_contiguous
31  # All inputs are F order and full or singletons, so we the result is in col-major order:
32  assert capture == """
33  my_func(x:int=1, y:float=10, z:float=3)
34  my_func(x:int=3, y:float=30, z:float=3)
35  my_func(x:int=2, y:float=20, z:float=3)
36  my_func(x:int=4, y:float=40, z:float=3)
37  """
38  with capture:
39  a, b, c = np.array([[1, 3, 5], [7, 9, 11]]), np.array([[2, 4, 6], [8, 10, 12]]), 3
40  assert np.allclose(f(a, b, c), a * b * c)
41  assert capture == """
42  my_func(x:int=1, y:float=2, z:float=3)
43  my_func(x:int=3, y:float=4, z:float=3)
44  my_func(x:int=5, y:float=6, z:float=3)
45  my_func(x:int=7, y:float=8, z:float=3)
46  my_func(x:int=9, y:float=10, z:float=3)
47  my_func(x:int=11, y:float=12, z:float=3)
48  """
49  with capture:
50  a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2
51  assert np.allclose(f(a, b, c), a * b * c)
52  assert capture == """
53  my_func(x:int=1, y:float=2, z:float=2)
54  my_func(x:int=2, y:float=3, z:float=2)
55  my_func(x:int=3, y:float=4, z:float=2)
56  my_func(x:int=4, y:float=2, z:float=2)
57  my_func(x:int=5, y:float=3, z:float=2)
58  my_func(x:int=6, y:float=4, z:float=2)
59  """
60  with capture:
61  a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2
62  assert np.allclose(f(a, b, c), a * b * c)
63  assert capture == """
64  my_func(x:int=1, y:float=2, z:float=2)
65  my_func(x:int=2, y:float=2, z:float=2)
66  my_func(x:int=3, y:float=2, z:float=2)
67  my_func(x:int=4, y:float=3, z:float=2)
68  my_func(x:int=5, y:float=3, z:float=2)
69  my_func(x:int=6, y:float=3, z:float=2)
70  """
71  with capture:
72  a, b, c = np.array([[1, 2, 3], [4, 5, 6]], order='F'), np.array([[2], [3]]), 2
73  assert np.allclose(f(a, b, c), a * b * c)
74  assert capture == """
75  my_func(x:int=1, y:float=2, z:float=2)
76  my_func(x:int=2, y:float=2, z:float=2)
77  my_func(x:int=3, y:float=2, z:float=2)
78  my_func(x:int=4, y:float=3, z:float=2)
79  my_func(x:int=5, y:float=3, z:float=2)
80  my_func(x:int=6, y:float=3, z:float=2)
81  """
82  with capture:
83  a, b, c = np.array([[1, 2, 3], [4, 5, 6]])[::, ::2], np.array([[2], [3]]), 2
84  assert np.allclose(f(a, b, c), a * b * c)
85  assert capture == """
86  my_func(x:int=1, y:float=2, z:float=2)
87  my_func(x:int=3, y:float=2, z:float=2)
88  my_func(x:int=4, y:float=3, z:float=2)
89  my_func(x:int=6, y:float=3, z:float=2)
90  """
91  with capture:
92  a, b, c = np.array([[1, 2, 3], [4, 5, 6]], order='F')[::, ::2], np.array([[2], [3]]), 2
93  assert np.allclose(f(a, b, c), a * b * c)
94  assert capture == """
95  my_func(x:int=1, y:float=2, z:float=2)
96  my_func(x:int=3, y:float=2, z:float=2)
97  my_func(x:int=4, y:float=3, z:float=2)
98  my_func(x:int=6, y:float=3, z:float=2)
99  """
100 
101 
103  assert m.selective_func(np.array([1], dtype=np.int32)) == "Int branch taken."
104  assert m.selective_func(np.array([1.0], dtype=np.float32)) == "Float branch taken."
105  assert m.selective_func(np.array([1.0j], dtype=np.complex64)) == "Complex float branch taken."
106 
107 
108 def test_docs(doc):
109  assert doc(m.vectorized_func) == """
110  vectorized_func(arg0: numpy.ndarray[numpy.int32], arg1: numpy.ndarray[numpy.float32], arg2: numpy.ndarray[numpy.float64]) -> object
111  """ # noqa: E501 line too long
112 
113 
115  trivial, vectorized_is_trivial = m.trivial, m.vectorized_is_trivial
116 
117  assert vectorized_is_trivial(1, 2, 3) == trivial.c_trivial
118  assert vectorized_is_trivial(np.array(1), np.array(2), 3) == trivial.c_trivial
119  assert vectorized_is_trivial(np.array([1, 3]), np.array([2, 4]), 3) == trivial.c_trivial
120  assert trivial.c_trivial == vectorized_is_trivial(
121  np.array([[1, 3, 5], [7, 9, 11]]), np.array([[2, 4, 6], [8, 10, 12]]), 3)
122  assert vectorized_is_trivial(
123  np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2) == trivial.non_trivial
124  assert vectorized_is_trivial(
125  np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2) == trivial.non_trivial
126  z1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype='int32')
127  z2 = np.array(z1, dtype='float32')
128  z3 = np.array(z1, dtype='float64')
129  assert vectorized_is_trivial(z1, z2, z3) == trivial.c_trivial
130  assert vectorized_is_trivial(1, z2, z3) == trivial.c_trivial
131  assert vectorized_is_trivial(z1, 1, z3) == trivial.c_trivial
132  assert vectorized_is_trivial(z1, z2, 1) == trivial.c_trivial
133  assert vectorized_is_trivial(z1[::2, ::2], 1, 1) == trivial.non_trivial
134  assert vectorized_is_trivial(1, 1, z1[::2, ::2]) == trivial.c_trivial
135  assert vectorized_is_trivial(1, 1, z3[::2, ::2]) == trivial.non_trivial
136  assert vectorized_is_trivial(z1, 1, z3[1::4, 1::4]) == trivial.c_trivial
137 
138  y1 = np.array(z1, order='F')
139  y2 = np.array(y1)
140  y3 = np.array(y1)
141  assert vectorized_is_trivial(y1, y2, y3) == trivial.f_trivial
142  assert vectorized_is_trivial(y1, 1, 1) == trivial.f_trivial
143  assert vectorized_is_trivial(1, y2, 1) == trivial.f_trivial
144  assert vectorized_is_trivial(1, 1, y3) == trivial.f_trivial
145  assert vectorized_is_trivial(y1, z2, 1) == trivial.non_trivial
146  assert vectorized_is_trivial(z1[1::4, 1::4], y2, 1) == trivial.f_trivial
147  assert vectorized_is_trivial(y1[1::4, 1::4], z2, 1) == trivial.c_trivial
148 
149  assert m.vectorized_func(z1, z2, z3).flags.c_contiguous
150  assert m.vectorized_func(y1, y2, y3).flags.f_contiguous
151  assert m.vectorized_func(z1, 1, 1).flags.c_contiguous
152  assert m.vectorized_func(1, y2, 1).flags.f_contiguous
153  assert m.vectorized_func(z1[1::4, 1::4], y2, 1).flags.f_contiguous
154  assert m.vectorized_func(y1[1::4, 1::4], z2, 1).flags.c_contiguous
155 
156 
158  assert doc(m.vec_passthrough) == (
159  "vec_passthrough(" + ", ".join([
160  "arg0: float",
161  "arg1: numpy.ndarray[numpy.float64]",
162  "arg2: numpy.ndarray[numpy.float64]",
163  "arg3: numpy.ndarray[numpy.int32]",
164  "arg4: int",
165  "arg5: m.numpy_vectorize.NonPODClass",
166  "arg6: numpy.ndarray[numpy.float64]"]) + ") -> object")
167 
168  b = np.array([[10, 20, 30]], dtype='float64')
169  c = np.array([100, 200]) # NOT a vectorized argument
170  d = np.array([[1000], [2000], [3000]], dtype='int')
171  g = np.array([[1000000, 2000000, 3000000]], dtype='int') # requires casting
172  assert np.all(
173  m.vec_passthrough(1, b, c, d, 10000, m.NonPODClass(100000), g) ==
174  np.array([[1111111, 2111121, 3111131],
175  [1112111, 2112121, 3112131],
176  [1113111, 2113121, 3113131]]))
177 
178 
180  o = m.VectorizeTestClass(3)
181  x = np.array([1, 2], dtype='int')
182  y = np.array([[10], [20]], dtype='float32')
183  assert np.all(o.method(x, y) == [[14, 15], [24, 25]])
184 
185 
187  assert not isinstance(m.vectorized_func(1, 2, 3), np.ndarray)
188  assert not isinstance(m.vectorized_func(np.array(1), 2, 3), np.ndarray)
189  z = m.vectorized_func([1], 2, 3)
190  assert isinstance(z, np.ndarray)
191  assert z.shape == (1, )
192  z = m.vectorized_func(1, [[[2]]], 3)
193  assert isinstance(z, np.ndarray)
194  assert z.shape == (1, 1, 1)
Annotation for documentation.
Definition: attr.h:33
bool isinstance(handle obj)
Definition: pytypes.h:384
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:03