[eigen] Re: Raising double to integer powers

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Of course, shortly after having sent this message I figured it out, but it doesn't actually result in an increase in my throughput sadly. For posterity:

#include <Eigen/Dense>
#include <iostream>

using namespace Eigen;

struct myUnaryFunctor {
  const double m_base;
  myUnaryFunctor(double base): m_base(base) {};
  typedef double result_type;
  result_type operator()(const int &e) const
  {
      return pow(m_base, e);
  }
};

int main()
{
    auto e = Eigen::ArrayXi::LinSpaced(11010).eval();
    double base = 2.9;
    std::cout << e.unaryExpr(myUnaryFunctor(base)); 
}

On Mon, May 10, 2021 at 5:14 PM Ian Bell <ian.h.bell@xxxxxxxxx> wrote:
I have a simple question that I have spent ages trying to work around over the years and still haven't found a satisfactory solution. I have a single double value, and without any allocation, I need to generate an Eigen _expression_ that allows me to take a double to an array of integers. In my case, 25% of my code's execution time is spent working on this problem, and I think I should be able to get that contribution back to closer to zero. Initially I asked the question here: https://stackoverflow.com/questions/25354205/how-to-raise-double-to-array-powers-in-eigen

This code doesn't compile (e.g., on godbolt): 

#include <Eigen/Dense>

int main(){
    auto c = Eigen::ArrayXi::LinSpaced(11010);
    auto r = Eigen::pow(3.7, c);
}

but if I switch the ArrayXi for ArrayXd it compiles (because the scalar types match), but that is not what I want.  

Really, what I need is to call my own implementation of pow(double, int), because it is MUCH faster than pow(double, double).  

In the end, I would like some guidance on how I can craft an Eigen _expression_ for this use case that will not introduce any intermediate allocation.  

Thanks,
Ian


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/