ssmit1986 / DualNumbers

Implementation of dual numbers for automatic differentiation of programs
MIT License
22 stars 5 forks source link

DualNumbers and ArcTan[x,y] #7

Open rudolfoldenbourg opened 6 months ago

rudolfoldenbourg commented 6 months ago

Dear Sjoerd, I continue to look for the best approach to minimize a mean squared error function that has many variables connected in part by trigonometric functions, among them ArcTan[x,y].

In using DualNumbers, I noticed that ArcTan[Dual[x,dx],Dual[y,dy]] remains unevaluated, both, the Standard and NonStandard part, while ArcTan[Dual[y,dy]/Dual[x,dx]] seems to evaluates to the correct result when x and y are both positive. As is well known, difficulties arise when x and/or y become negative, as the quadrants need to be considered for evaluating to the correct angle. Therefore, Mathematica implements the ArcTan[x,y] version of the function to keep track of the quadrants the sign of the x and y variables are pointing to.

Curiously, though, while the Standard part of ArcTan[Dual[y,dy]/Dual[x,dx]] follows the expectation of ArcTan[y/x] and gives the same result for x negative and y positive as for x positive and y negative, the NonStandard part of ArcTan[Dual[y,dy]/Dual[x,dx]] distinguishes between these two cases and gives the correct results for the partial derivatives:

Pasted Graphic

This also applies when both x and y are negative. While the standard part gives the same result, incorrectly, as if both variables were positive, the NonStandard part correctly switches sign between the partial derivatives for the two cases, either both variables negative or both positive:

Pasted Graphic 1

In my computations for minimizing the mean squared error function, it is important to be able to distinguish between angles from different quadrants.

Therefore, I would like to ask you a) if my analysis presented above is correct and b) if so, do you have suggestions for how to use DualNumbers when angles between -Pi and +Pi need to be correctly identified?

Thanks for any advice you can give.

Rudolf

SjoerdSmitWolfram commented 5 months ago

Hi Rudolf. I just wanted to let you know I did see your comment, but I'm busy right now to really work on this. If you have a suggestion and/or sources for how the 2-arg form of ArcTan with duals should evaluate, please feel free to post it. I'm not quite sure I have a good suggestion for it off the top of my head.

rudolfoldenbourg commented 5 months ago

Hi Sjoerd, I found a workaround by defining the function arcTanDual[x,y]:

image

The definition arcTanDual[x,y] works not only for DualNumbers x and y, but also if they are normal Reals.

Maybe something like this can be integrated more seamlessly into the paclet.

By the way, I was disappointed that in the new Mathematica version 14.0, your DualNumbers paclet was not yet integrated into the larger system. Maybe issues like the above still prevent that.