As far as I understood currently inner and outer strides must be positive (see eigen_assert(innerStride>=0 && outerStride>=0) at line 77 of Stride.h) but I don't see the reason for this. For my application I need to copy a matrix after flipping the rows from top to bottom. This could be done by using a map with a negative stride but it is currently not allowed. I tried removing the above mentioned line from Stride.h and using a negative stride and everything seems to work fine.. Is there any reason not to use negative strides? If so is there another way I can do the row flipping? (my current workaround is to copy line-by-line in a for loop). Thanks and regards.
You can use M.colwise().reverse(). But I agree that there should not be a strong reason to forbid negative strides.
Hi, I'm a developer of pybind11, a library for interfacing C++ and Python. One of our features is the ability to bind numpy arrays as Eigen::Ref's for python code calling C++ code, and similarly converting Eigen matrix-like objects into referencing numpy arrays when returning Eigen types back to Python code. Someone is currently working on adding support for negative strides, which pybind11 doesn't currently handle but numpy allows; allowing negative strides in Eigen would open up more cases where direct data mapping can be used without requiring copying when crossing the Python/C++ barrier. It's not a major issue (negative strides are probably fairly rare in practice), but it would be nice to have in Eigen if indeed removing the assertion doesn't cause any issues.
Created attachment 933 [details] Enable negative strides Removing this assert seems to be enough! See the unit tests to be sure we interpret negative strides the same way. There is however a gotcha: -1 cannot be used as a compile-time stride because we used Dynamic==-1 to mean runtime. Still worth it IMO. Making reverse() and other expressions expose negative strides is another topic.
-- GitLab Migration Automatic Message -- This bug has been migrated to gitlab.com's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.com/libeigen/eigen/issues/747.