Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement four-quadrant inverse tangent #124

Open
DrNjitram opened this issue Nov 2, 2020 · 1 comment
Open

Implement four-quadrant inverse tangent #124

DrNjitram opened this issue Nov 2, 2020 · 1 comment

Comments

@DrNjitram
Copy link

DrNjitram commented Nov 2, 2020

Implement an alternative version of the inverse tangent, which allows one to calculate the angle of a point in the full range of angles (domain: [-pi pi] compared to the normal inverse tangent (domain [-pi/2 pi/2]).

More information: https://nl.mathworks.com/help/matlab/ref/atan2.html and https://en.wikipedia.org/wiki/Atan2

@DrNjitram
Copy link
Author

I have currently created a very simple version that works with the already implemented functions.
I am sure there is a faster way to do this, but this 'just works'.

public static FloatMatrix atan2(FloatMatrix x, FloatMatrix y){     

        FloatMatrix intermediate = sqrt(x.mul(x).add(y.mul(y)));

        FloatMatrix domainOne = atan(y.div(intermediate.add(x))).mul(2);
        FloatMatrix domainTwo = atan(intermediate.sub(x).div(y)).mul(2);

        FloatMatrix result = new FloatMatrix(x.getRows());

        for(int index : x.gt(0).findIndices()) result.put(index, domainOne.get(index));
        for(int index : x.le(0).and(y.ne(0)).findIndices()) result.put(index, domainTwo.get(index));
        for(int index : x.lt(0).and(y.eq(0)).findIndices()) result.put(index, (float) Math.PI);
        for(int index : x.eq(0).and(y.eq(0)).findIndices()) result.put(index, Float.NaN);

        return result;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant