New user self-registration is disabled due to spam. Please email eigen-core-team @ if you need an account.
Before reporting a bug, please make sure that your Eigen version is up-to-date!
Bug 467 - Modulo Operator for 1D
Summary: Modulo Operator for 1D
Alias: None
Product: Eigen
Classification: Unclassified
Component: Core - general (show other bugs)
Version: unspecified
Hardware: All All
: Normal Feature Request
Assignee: Nobody
Depends on:
Blocks: 3.x
  Show dependency treegraph
Reported: 2012-05-24 20:15 UTC by Pantelis
Modified: 2019-03-19 08:36 UTC (History)
4 users (show)


Description Pantelis 2012-05-24 20:15:44 UTC

would it possible to have a modulo (%) operator on Array objects?

All best,
Comment 1 Rasmus Munk Larsen 2019-03-18 22:20:31 UTC
Gael, Christoph, what are your thoughts about this? In the tensor library we have defined operators scalar_fmod_op, scalar_mod_op, and scalar_mod2_op:

Should we move that to core? fmod is not trivial to implement correctly, so perhaps we could defer vectorizing it, but having a vectorized integer modulo operator seems like low-hanging fruit.
Comment 2 Rasmus Munk Larsen 2019-03-18 23:41:21 UTC
The lack of vectorized division on x86 makes it not very attractive, of course.
Comment 3 Christoph Hertzberg 2019-03-19 08:36:34 UTC
Why not. I think the easiest way to vectorize this for integers on x86 is to cast to double, divide, and cast back (and calculate the remainder).
We could also vectorize pdiv<int32> that way.
And in theory, a constant divider could be vectorized by some multiplication and shifting and subtracting.

For floats/doubles I guess it is relatively simple to get a "good-enough" solution. But getting things like `fmod(MAX_FLOAT, 99.f)` accurate is not trivial, of course. We could implement something which works correctly in most cases and falls down to a non-vectorized path otherwise (similar to our range-reduction in sincos).

API-wise, I'm not sure if we should overload `%` for that or just provide functions (also with one argument a Scalar):
  mod(Array,Array), Array::mod(Array), Matrix::cwiseMod(Matrix)
I'm preferring the latter, since % is not overloaded for float and double either. And overloading it only for Array<int> seems unnecessarily complicated.

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