Relative Condition Number of Atan(x) vs Atan2(y,x)

310 Views Asked by At

I'm trying to think through the sensitivity of atan2 to errors in its inputs and I've run into a disconnect that I don't quite understand.

I know that you can compute the relative condition number of a differential function $f$ as:

$$\kappa(\vec{x}) = \frac{\Vert\mathbb{J}_f(\vec{x})\Vert}{\Vert f(\vec{x})\Vert/\Vert \vec{x}\Vert}$$

Where the norm on the Jacobian is the induced norm.

For $atan(x)$ you end up with simply the derivative $\frac{1}{1+x^2}$ so it works out to: $$\kappa(x) = \frac{x}{(1+x^2)atan(x)}$$

Which is very nicely behaved:

If I look at it on the unit circle and compute K(y/x), it looks well behaved too:

However, looking at atan2(y,x) = atan(y/x), you end up with:

$$\mathbb{J}(y,x) = \{\frac{-y}{x^2+y^2}, \frac{x}{x^2+y^2}\}$$

It turns out the induced $L_2$ norm for this is 1, and in my particular case, my x and y values are indeed a sine and cosine pair so $\Vert <x,y>\Vert = \sqrt{x^2+y^2} = 1$

This leaves you with $\kappa(y,x) = \frac{1}{\vert atan2(y,x)\vert}$ which is less well behaved for positive x and y near zero:

I believe the math here but I'm struggling to interpret the results in a consistent way, since atan2 is fundamentally just atan with logic to interpret the sign correctly.

Why does atan2 seem to get very sensitive near zero and atan doesn't?

Edit: Made atan2 and related equations consistently (y,x) order.

2

There are 2 best solutions below

1
On BEST ANSWER

Condition number shows how relative perturbation of the argument(s) result in relative change in function value.

You show that atan is good conditioned, so any perturbation of $x$ cannot result in bigger relative perturbation of $\operatorname{atan} x$. I was suprised that for nonlinear functions $\kappa$ may be less than 1, for linear ones that is impossible.

Let's study how atan2 is sensitive to perturbation of $x$ and $y$ separately: $$ \kappa_x(y, x) = \frac{|\operatorname{atan2}(y, x+\delta x)-\operatorname{atan2}(y, x)| / |\delta x|}{|\operatorname{atan2}(y, x)| / \|(x, y)\|} = \frac{|y|}{|\operatorname{atan2}(y, x)|} $$ Mathematica defines atan2 with branch cut along $(-\infty, 0]$, so atan2 near $y = 0$, $x = 1$ behaves like $\operatorname{atan2}(y, x) \approx y$, so $\kappa_x \approx 1$.

Similarly $$ \kappa_y(y, x) = \frac{|x|}{|\operatorname{atan2}(y, x)|}. $$ Near $x = 1, y = 0$ we have $\kappa_y = |x/y| \to \infty$. There's no magic in this result. The relative perturbation of function is like $\delta y / y$, while relative perturbation of the argument is $\delta y/1$ (thanks to $x$). Sure thing $\delta y / y$ may be big, while $\delta y$ remain quite small.

The difference in $\operatorname{atan} \frac{y}{x}$ and $\operatorname{atan2}(y, x)$ the condition numbers essentially contain different $\|x\|$ in their definition, the former has $|y/x|$, while the latter has $\|(x, y)\| = 1$.

4
On

$$\operatorname{atan2}(y, x) = \arctan(\frac{y}{x}) + (\textit{quadrant fixup if } x < 0)$$

You're dividing by x. This is an ill-conditioned operation if x is near 0.

To formalize things a bit, take the division $q = \frac{y}{x}$ and perturb it to $\tilde{q} = \frac{y + \Delta y}{x + \Delta x}$.

$$\tilde{q} = \frac{y + \Delta y}{x + \Delta x} \frac{x - \Delta x}{x - \Delta x}$$ $$= \frac{xy - y\Delta{x} + x\Delta y - \Delta x \Delta y}{x^2 - (\Delta x)^2}$$

Let's assume that the deltas are small enough that for all practical purposes, $(\Delta x)^2 = 0$ and $\Delta x \Delta y = 0$. Then,

$$\tilde{q} \approx \frac{xy - y\Delta{x} + x\Delta y}{x^2}$$ $$= q - \frac{x\Delta y - y\Delta{x}}{x^2}$$ $$\text{error} = |q - \tilde{q}| \le \frac{1}{|x|}|\Delta y| + \frac{|y|}{x^2} |\Delta x|$$ $$\lim_{x \rightarrow 0} |q - \tilde{q}| = \infty$$