Skip to content

Commit a22dd2d

Browse files
aburchjagerman
authored andcommitted
correct stride in matrix example and test
This also matches the Eigen example for the row-major case. This also enhances one of the tests to trigger a failure (and fixes it in the PR). (This isn't really a flaw in pybind itself, but rather fixes wrong code in the test code and docs).
1 parent d2757d0 commit a22dd2d

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

docs/advanced/pycpp/numpy.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ completely avoid copy operations with Python expressions like
4141
py::format_descriptor<float>::format(), /* Python struct-style format descriptor */
4242
2, /* Number of dimensions */
4343
{ m.rows(), m.cols() }, /* Buffer dimensions */
44-
{ sizeof(float) * m.rows(), /* Strides (in bytes) for each index */
44+
{ sizeof(float) * m.cols(), /* Strides (in bytes) for each index */
4545
sizeof(float) }
4646
);
4747
});

tests/test_buffers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ TEST_SUBMODULE(buffers, m) {
107107
return py::buffer_info(
108108
m.data(), /* Pointer to buffer */
109109
{ m.rows(), m.cols() }, /* Buffer dimensions */
110-
{ sizeof(float) * size_t(m.rows()), /* Strides (in bytes) for each index */
110+
{ sizeof(float) * size_t(m.cols()), /* Strides (in bytes) for each index */
111111
sizeof(float) }
112112
);
113113
})

tests/test_buffers.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,21 @@ def test_from_python():
3636
# https://bitbucket.org/pypy/pypy/issues/2444
3737
@pytest.unsupported_on_pypy
3838
def test_to_python():
39-
mat = m.Matrix(5, 5)
40-
assert memoryview(mat).shape == (5, 5)
39+
mat = m.Matrix(5, 4)
40+
assert memoryview(mat).shape == (5, 4)
4141

4242
assert mat[2, 3] == 0
43-
mat[2, 3] = 4
43+
mat[2, 3] = 4.0
44+
mat[3, 2] = 7.0
4445
assert mat[2, 3] == 4
46+
assert mat[3, 2] == 7
47+
assert struct.unpack_from('f', mat, (3 * 4 + 2) * 4) == (7, )
48+
assert struct.unpack_from('f', mat, (2 * 4 + 3) * 4) == (4, )
4549

4650
mat2 = np.array(mat, copy=False)
47-
assert mat2.shape == (5, 5)
48-
assert abs(mat2).sum() == 4
49-
assert mat2[2, 3] == 4
51+
assert mat2.shape == (5, 4)
52+
assert abs(mat2).sum() == 11
53+
assert mat2[2, 3] == 4 and mat2[3, 2] == 7
5054
mat2[2, 3] = 5
5155
assert mat2[2, 3] == 5
5256

@@ -58,7 +62,7 @@ def test_to_python():
5862
del mat2 # holds a mat reference
5963
pytest.gc_collect()
6064
assert cstats.alive() == 0
61-
assert cstats.values() == ["5x5 matrix"]
65+
assert cstats.values() == ["5x4 matrix"]
6266
assert cstats.copy_constructions == 0
6367
# assert cstats.move_constructions >= 0 # Don't invoke any
6468
assert cstats.copy_assignments == 0

0 commit comments

Comments
 (0)