openclboost-compute

How to use boost::compute::atan2?


I would like to compute the phase of a complex number using boost::compute

here is my attempt, I expect the result to be equal to atan2(0.5f):

namespace bc = boost::compute;

bc::vector<std::complex<float>> vec{ {1.0f, 2.0f} };
bc::vector<float> result(1);
bc::transform(vec.begin(), vec.end(), result.begin(), bc::atan2<float>());

but I get a compilation error claiming "Non-unary function invoked one argument"


Solution

  • I think you can also use Boost.Compute's lambda expressions for this:

      bc::vector<float2> input{ {1.0f, 2.0f}, {3.0f, 4.0f}, {5.0f, 6.0f} };
      bc::vector<float> output(3); 
    
      using boost::compute::lambda::atan2;
      using boost::compute::_1;
      using boost::compute::lambda::get;
    
      bc::transform(
        float2_input.begin(),
        float2_input.end(),
        float_output.begin(),
        atan2(get<1>(_1), get<0>(_1)),
        queue
      );
    

    float2 is bassically a complex in Boost.Compute. You can also check test_lambda.cpp.