MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_negative_stride_from_python

Function test_negative_stride_from_python

tests/test_eigen_matrix.py:175–221  ·  view source on GitHub ↗

Eigen doesn't support (as of yet) negative strides. When a function takes an Eigen matrix by copy or const reference, we can pass a numpy array that has negative strides. Otherwise, an exception will be thrown as Eigen will not be able to map the numpy array.

(msg)

Source from the content-addressed store, hash-verified

173
174
175def test_negative_stride_from_python(msg):
176 """Eigen doesn't support (as of yet) negative strides. When a function takes an Eigen matrix by
177 copy or const reference, we can pass a numpy array that has negative strides. Otherwise, an
178 exception will be thrown as Eigen will not be able to map the numpy array."""
179
180 counting_mat = np.arange(9.0, dtype=np.float32).reshape((3, 3))
181 counting_mat = counting_mat[::-1, ::-1]
182 second_row = counting_mat[1, :]
183 second_col = counting_mat[:, 1]
184 np.testing.assert_array_equal(m.double_row(second_row), 2.0 * second_row)
185 np.testing.assert_array_equal(m.double_col(second_row), 2.0 * second_row)
186 np.testing.assert_array_equal(m.double_complex(second_row), 2.0 * second_row)
187 np.testing.assert_array_equal(m.double_row(second_col), 2.0 * second_col)
188 np.testing.assert_array_equal(m.double_col(second_col), 2.0 * second_col)
189 np.testing.assert_array_equal(m.double_complex(second_col), 2.0 * second_col)
190
191 counting_3d = np.arange(27.0, dtype=np.float32).reshape((3, 3, 3))
192 counting_3d = counting_3d[::-1, ::-1, ::-1]
193 slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]]
194 for ref_mat in slices:
195 np.testing.assert_array_equal(m.double_mat_cm(ref_mat), 2.0 * ref_mat)
196 np.testing.assert_array_equal(m.double_mat_rm(ref_mat), 2.0 * ref_mat)
197
198 # Mutator:
199 with pytest.raises(TypeError) as excinfo:
200 m.double_threer(second_row)
201 assert (
202 msg(excinfo.value)
203 == """
204 double_threer(): incompatible function arguments. The following argument types are supported:
205 1. (arg0: typing.Annotated[numpy.typing.NDArray[numpy.float32], "[1, 3]", "flags.writeable"]) -> None
206
207 Invoked with: """
208 + repr(np.array([5.0, 4.0, 3.0], dtype="float32"))
209 )
210
211 with pytest.raises(TypeError) as excinfo:
212 m.double_threec(second_col)
213 assert (
214 msg(excinfo.value)
215 == """
216 double_threec(): incompatible function arguments. The following argument types are supported:
217 1. (arg0: typing.Annotated[numpy.typing.NDArray[numpy.float32], "[3, 1]", "flags.writeable"]) -> None
218
219 Invoked with: """
220 + repr(np.array([7.0, 4.0, 1.0], dtype="float32"))
221 )
222
223
224def test_block_runtime_error_type_caster_eigen_ref_made_a_copy():

Callers

nothing calls this directly

Calls 4

msgFunction · 0.85
reprFunction · 0.85
reshapeMethod · 0.80
arrayMethod · 0.80

Tested by

no test coverage detected