This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Eigen's atan/atan2 support

Tags: None
(comma "," separated)
chris5287
Registered Member
Posts
2
Karma
0

Eigen's atan/atan2 support

Fri Aug 02, 2013 8:19 pm
Does Eigen have support for calculating atan/atan2 on an Eigen Array? I can't seem to find any mention of it in the documentation.

Basically I have two arrays:
Code: Select all
Eigen::ArrayXd one, two;
that have the same number of rows and I want to calculate the atan2 for each:
Code: Select all
Eigen::ArrayXd answers = atan2(one, two);

If Eigen doesn't currently have support for this, how would I go about extending the Eigen's Array base class to implement this functionality (I presume I could use std::atan/std::atan2 to perform the calculations)?
davidje13
Registered Member
Posts
13
Karma
0
OS

Re: Eigen's atan/atan2 support

Sat Aug 03, 2013 6:01 pm
This bugtracker seems relevant: http://eigen.tuxfamily.org/bz/show_bug.cgi?id=80

The attached patch (from a month ago) implements atan. You could try applying it to the version you have, or filter out the relevant code, or wait for it to be in a release version.
chris5287
Registered Member
Posts
2
Karma
0

Re: Eigen's atan/atan2 support

Sun Aug 04, 2013 1:17 pm
davidje13 wrote:This bugtracker seems relevant: http://eigen.tuxfamily.org/bz/show_bug.cgi?id=80

It is relevant for atan, but I require atan2 support (See: http://stackoverflow.com/questions/283406/what-is-the-difference-between-atan-and-atan2-in-c)
davidje13
Registered Member
Posts
13
Karma
0
OS

Re: Eigen's atan/atan2 support

Sun Aug 04, 2013 11:19 pm
In fairness you asked for either one.

To emulate atan2, you could divide one by the other first, although you'd still need to figure out the quadrants somehow (if you need that). Your best option might be to loop through and do it manually, but I'm quite new to eigen myself so I'll leave assertions like that to the experts. Alternatively you could use complex numbers, since atan2 effectively gives the argument of a complex number. But again I don't think support for that is built-in so you'd still be looping through each element.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Eigen's atan/atan2 support

Tue Aug 06, 2013 9:43 pm
You can probably use binaryExpr and ptr_fun, something like: A.binaryExpr(B, std::ptr_fun(atan2))
buzzlightzyear
Registered Member
Posts
2
Karma
0

Re: Eigen's atan/atan2 support

Thu Aug 24, 2017 12:50 pm
Hi,

i know, this post is a little bit older... but the function A.binaryExpr(B, std::ptr_fun(atan2)) doesn't work for me... i get the following error:

Code: Select all
error: no matching function for call to ‘ptr_fun(<unresolved overloaded function type>)’


I am using g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609

Can someone help me in this dark hour? O_O

Thx
BL
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Eigen's atan/atan2 support

Thu Aug 24, 2017 10:20 pm
this is because there are multiple overloads of std::atan2, so either pass the C version, or make your own, or use a lambda:

Code: Select all
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;

double my_atan2(double a, double b) { return std::atan2(a,b); }

int main()
{
  Matrix4d A, B, C;
  C = A.binaryExpr(B, std::ptr_fun(::atan2));
  C = A.binaryExpr(B, std::ptr_fun(my_atan2));
  C = A.binaryExpr(B, [] (double a, double b) { return std::atan2(a,b);} );
}
buzzlightzyear
Registered Member
Posts
2
Karma
0

Re: Eigen's atan/atan2 support

Mon Aug 28, 2017 11:58 am
Yeah, it works! :-) Thank you very much,
greetings from Austria! :-)
Buzzzz
AndySpiros
Registered Member
Posts
4
Karma
0
OS

Re: Eigen's atan/atan2 support

Fri Sep 08, 2017 10:03 am
Out of curiosity, what is blocking Eigen from providing direct support to atan2 instead of having the users rely on the binaryExpr?


Bookmarks



Who is online

Registered users: Bing [Bot], daret, Google [Bot], sandyvee, Sogou [Bot]