New user self-registration is disabled due to spam. Please email eigen-core-team @ lists.tuxfamily.org if you need an account.
Before reporting a bug, please make sure that your Eigen version is up-to-date!
Bug 747 - Allow for negative stride
Summary: Allow for negative stride
Status: REVIEWNEEDED
Alias: None
Product: Eigen
Classification: Unclassified
Component: Core - general (show other bugs)
Version: unspecified
Hardware: All All
: Normal Unknown
Assignee: Nobody
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 3.4
  Show dependency treegraph
 
Reported: 2014-02-20 11:28 UTC by rick1987
Modified: 2019-02-22 12:16 UTC (History)
4 users (show)



Attachments
Enable negative strides (7.59 KB, patch)
2019-02-20 16:16 UTC, Gael Guennebaud
no flags Details | Diff

Description rick1987 2014-02-20 11:28:47 UTC
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.
Comment 1 Christoph Hertzberg 2014-02-20 11:33:21 UTC
You can use M.colwise().reverse().
But I agree that there should not be a strong reason to forbid negative strides.
Comment 2 Jason Rhinelander 2017-04-06 23:03:14 UTC
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.
Comment 3 Gael Guennebaud 2019-02-20 16:16:56 UTC
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.

Note You need to log in before you can comment on or make changes to this bug.