<s>[INST] Unifying a real-valued function over two branches I have a function that I want to evaluate numerically. These three forms listed below are all identical algebraically for nonnegative real $u$ (aside from removable discontinuities at $u=0$ and $u=1$), but none avoids complex numbers for its entire domain.
$$\begin{aligned}
f_1(u) &= \frac{1}{\sqrt{1-u^2}}\tan^{-1}\frac{\sqrt{1-u^2}}{u} \\
f_2(u) &= \frac{1}{\sqrt{1-u^2}}\left(\frac{\pi}{2}-\tan^{-1}\frac{u}{\sqrt{1-u^2}}\right) \\
f_3(u) &= \frac{1}{\sqrt{u^2-1}}\ln\left(u+\sqrt{u^2-1}\right)
\end{aligned}$$
The first two have imaginary values of the square root for $u>1$ and the third has imaginary values of the square root for $u<1$. None are well-conditioned numerically in close proximity to $u=1$.
Any suggestions on a different form for numeric evaluation?
 [/INST] Using the series expansion
$$\ln(x)=\sum_{n=0}^\infty\frac2{2n+1}\left(\frac{x-1}{x+1}\right)^{2n+1}$$
gives, according to the third representation,
\begin{align}f(x)&=\frac1{\sqrt{x^2-1}}\ln\left(x+\sqrt{x^2-1}\right)\\&=\frac1{\sqrt{x^2-1}}\sum_{n=0}^\infty\frac2{2n+1}\left(\frac{x-1+\sqrt{x^2-1}}{x+1+\sqrt{x^2-1}}\right)^{2n+1}\\&=\frac1{\sqrt{x^2-1}}\sum_{n=0}^\infty\frac2{2n+1}\left(\frac{\sqrt{x^2-1}}{x+1}\right)^{2n+1}\tag{Rationalize}\\&=\frac1{x+1}\sum_{n=0}^\infty\frac2{2n+1}\left(\frac{x^2-1}{(x+1)^2}\right)^n\\&=\frac1{x+1}\sum_{n=0}^\infty\frac2{2n+1}\left(\frac{x-1}{x+1}\right)^n\end{align}
which is convergent for all $x\ge0$ and is well-conditioned near $x=1$. It doesn't seem to be the case that there is a nice representation of $f$ over $\mathbb R_{\ge0}$, but for $x>1$ it is related to the inverse hyperbolic tangent function.
</s>