Skip to content

Commit cd36103

Browse files
committed
core: fix strides for Eigen::Ref
1 parent 19f4140 commit cd36103

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

include/eigenpy/numpy-allocator.hpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,15 @@ struct NumpyAllocator<Eigen::Ref<MatType, Options, Stride> > {
7070

7171
if (NumpyType::sharedMemory()) {
7272
const int Scalar_type_code = Register::getTypeCode<Scalar>();
73+
Eigen::DenseIndex inner_stride = mat.innerStride(),
74+
outer_stride = mat.outerStride();
75+
76+
const int elsize = call_PyArray_DescrFromType(Scalar_type_code)->elsize;
77+
npy_intp strides[2] = {elsize * inner_stride, elsize * outer_stride};
78+
7379
PyArrayObject *pyArray = (PyArrayObject *)call_PyArray_New(
7480
getPyArrayType(), static_cast<int>(nd), shape, Scalar_type_code,
75-
mat.data(), NPY_ARRAY_MEMORY_CONTIGUOUS | NPY_ARRAY_ALIGNED);
81+
strides, mat.data(), NPY_ARRAY_MEMORY_CONTIGUOUS | NPY_ARRAY_ALIGNED);
7682

7783
return pyArray;
7884
} else {
@@ -125,9 +131,15 @@ struct NumpyAllocator<const Eigen::Ref<const MatType, Options, Stride> > {
125131

126132
if (NumpyType::sharedMemory()) {
127133
const int Scalar_type_code = Register::getTypeCode<Scalar>();
134+
Eigen::DenseIndex inner_stride = mat.innerStride(),
135+
outer_stride = mat.outerStride();
136+
137+
const int elsize = call_PyArray_DescrFromType(Scalar_type_code)->elsize;
138+
npy_intp strides[2] = {elsize * inner_stride, elsize * outer_stride};
139+
128140
PyArrayObject *pyArray = (PyArrayObject *)call_PyArray_New(
129141
getPyArrayType(), static_cast<int>(nd), shape, Scalar_type_code,
130-
const_cast<Scalar *>(mat.data()),
142+
strides, const_cast<Scalar *>(mat.data()),
131143
NPY_ARRAY_MEMORY_CONTIGUOUS_RO | NPY_ARRAY_ALIGNED);
132144

133145
return pyArray;

0 commit comments

Comments
 (0)