def test_nonunit_stride_from_python(): from pybind11_tests import ( double_row, double_col, double_complex, double_mat_cm, double_mat_rm, double_threec, double_threer) counting_mat = np.arange(9.0, dtype=np.float32).reshape((3, 3)) second_row = counting_mat[1, :] second_col = counting_mat[:, 1] np.testing.assert_array_equal(double_row(second_row), 2.0 * second_row) np.testing.assert_array_equal(double_col(second_row), 2.0 * second_row) np.testing.assert_array_equal(double_complex(second_row), 2.0 * second_row) np.testing.assert_array_equal(double_row(second_col), 2.0 * second_col) np.testing.assert_array_equal(double_col(second_col), 2.0 * second_col) np.testing.assert_array_equal(double_complex(second_col), 2.0 * second_col) counting_3d = np.arange(27.0, dtype=np.float32).reshape((3, 3, 3)) slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]] for slice_idx, ref_mat in enumerate(slices): np.testing.assert_array_equal(double_mat_cm(ref_mat), 2.0 * ref_mat) np.testing.assert_array_equal(double_mat_rm(ref_mat), 2.0 * ref_mat) # Mutator: double_threer(second_row) double_threec(second_col) np.testing.assert_array_equal(counting_mat, [[0., 2, 2], [6, 16, 10], [6, 14, 8]])
def test_nonunit_stride_from_python(): from pybind11_tests import double_row, double_col, double_mat_cm, double_mat_rm counting_mat = np.arange(9.0, dtype=np.float32).reshape((3, 3)) first_row = counting_mat[0, :] first_col = counting_mat[:, 0] assert np.array_equal(double_row(first_row), 2.0 * first_row) assert np.array_equal(double_col(first_row), 2.0 * first_row) assert np.array_equal(double_row(first_col), 2.0 * first_col) assert np.array_equal(double_col(first_col), 2.0 * first_col) counting_3d = np.arange(27.0, dtype=np.float32).reshape((3, 3, 3)) slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]] for slice_idx, ref_mat in enumerate(slices): assert np.array_equal(double_mat_cm(ref_mat), 2.0 * ref_mat) assert np.array_equal(double_mat_rm(ref_mat), 2.0 * ref_mat)
def test_negative_stride_from_python(msg): from pybind11_tests import ( double_row, double_col, double_complex, double_mat_cm, double_mat_rm, double_threec, double_threer) # 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. counting_mat = np.arange(9.0, dtype=np.float32).reshape((3, 3)) counting_mat = counting_mat[::-1, ::-1] second_row = counting_mat[1, :] second_col = counting_mat[:, 1] np.testing.assert_array_equal(double_row(second_row), 2.0 * second_row) np.testing.assert_array_equal(double_col(second_row), 2.0 * second_row) np.testing.assert_array_equal(double_complex(second_row), 2.0 * second_row) np.testing.assert_array_equal(double_row(second_col), 2.0 * second_col) np.testing.assert_array_equal(double_col(second_col), 2.0 * second_col) np.testing.assert_array_equal(double_complex(second_col), 2.0 * second_col) counting_3d = np.arange(27.0, dtype=np.float32).reshape((3, 3, 3)) counting_3d = counting_3d[::-1, ::-1, ::-1] slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]] for slice_idx, ref_mat in enumerate(slices): np.testing.assert_array_equal(double_mat_cm(ref_mat), 2.0 * ref_mat) np.testing.assert_array_equal(double_mat_rm(ref_mat), 2.0 * ref_mat) # Mutator: with pytest.raises(TypeError) as excinfo: double_threer(second_row) assert msg(excinfo.value) == """ double_threer(): incompatible function arguments. The following argument types are supported: 1. (arg0: numpy.ndarray[float32[1, 3], flags.writeable]) -> None Invoked with: array([ 5., 4., 3.], dtype=float32) """ with pytest.raises(TypeError) as excinfo: double_threec(second_col) assert msg(excinfo.value) == """