Home > Software engineering >  Does Eigen have arange function like numpy.arange() in Python
Does Eigen have arange function like numpy.arange() in Python

Time:05-03

Do you know if Eigen has its own arange function, and if not, why?

For now, I have written my own arange function using Eigen::VectorXd::LinSpaced()

/*
* Return evenly spaced values within a given interval.  
* Values are generated within the half-open interval [start, stop) 
* (in other words, the interval including start but excluding stop).
*/
template <typename Scalar>
Eigen::VectorXd arange(const Scalar start, const Scalar stop, const Scalar step) {
  if(step == 0)
    throw std::domain_error("Arange's step cannot be 0");
  Scalar delta = stop - start;
  size_t size = ceil(delta / step);
  return Eigen::VectorXd::LinSpaced(size, start, start (size-1)*step);
}

CodePudding user response:

There is indeed LinSpaced currently available for such an operation, but apparently no direct equivalent of arange in Eigen. There are working notes related to this (eg. range and iota) but so far nothing appear to be included in the code for that. In the new Eigen 3.4 with a recent C version, you can use std::iota since Eigen now support STL iterators. Thus, there is no real need for a feature like arange to be added in Eigen (in fact LinSpaced is often good enough for most users).

  • Related