TITLE: $\arctan$ identities: converting $c \cdot \arctan(\frac{n}{d}) \iff \arctan(\frac{n'}{d'})$
QUESTION [5 upvotes]: If one has an expression of the form $c \cdot \arctan(\frac{n}{d})$, it can be converted to an equivalent expression of the form $\arctan(\frac{n'}{d'})$ fairly easily, using
$c \cdot \arctan(\frac{n}{d}) = \underbrace{\arctan(\frac{n}{d}) + \arctan(\frac{n}{d}) + \dots + \arctan(\frac{n}{d})}_{\text{c}}$
with the identity
$\arctan(\frac{a}{b}) + \arctan(\frac{c}{d}) = \arctan(\frac{a \cdot d + c \cdot b}{b \cdot d - a \cdot c})$
It can be seen, for example, that $4 \cdot \arctan(\frac{1}{5}) = \arctan(\frac{120}{119})$.
The inverse conversion seems to be a bit more complicated. If one is given, for example $\arctan(\frac{237}{3116})$, what would the best method to determine its 'simplest representation'? I will define this as the form $c \cdot \arctan(\frac{n}{d})$, such that $c \in \mathbb{Z}$; $n, d \in \mathbb{N}$, and $d$ has the smallest value possible. For the example above, the simplest representation would be $2 \cdot \arctan(\frac{3}{79})$.
My current method is the following:

Approximate the fraction $\frac{n}{d}$ as a decimal value.
Take the $\tan$ of this value, and divide by a small integer factor $q$.
Take the $\arctan$ of this quotient, and approximate to the nearest fraction with denominator no larger than ~$10^{12}$ (or a few orders of magnitude less than floating point precision).
Continue until a simplification is found, or give up after $q$ becomes 'too large'.

This method works, sort of, but it has a lot of problems. It's very slow computationally, it's non-deterministic, and it often finds simplifications that are only approximations, and not exact identities.
I'm quite sure that there must be a better method for this, but I've been unable to find one. Thanks in advance.

REPLY [2 votes]: Suppose you want to simplify $X = \arctan(n/d)$.  Using complex numbers,
$$\exp(iX)  = \dfrac{d+in}{\sqrt{d^2 + n^2}}$$
Now factor $d+in$ over the Gaussian integers.  If you get a result like
$$d + in = (a_1 + i b_1)^{c_1} \ldots (a_k + i b_k)^{c_k}$$
(with $a_i$ and $b_i$ integers and $c_i$ positive integers)
you can conclude that 
$X = c_1 \arctan(b_1/a_1) + \ldots + c_k \arctan(b_k/a_k)  + n \pi$
for a suitable integer $n$.
Factoring over the Gaussian integers can be done in Maple with the function 
GIfactor in the GaussInt package.
For example, $3116 + 237 i$ factors as $i (1+2i)^{10}$, so
$\arctan(237/3116) = \pi/2 + 10 \arctan(2/1) + n \pi$: in this case $n=-4$
to put it in the interval $(-\pi/2, \pi/2)$.
Another random example: $237 + 524 i = (1+2i)(5+2i)(45-16i)$
(and in this case $n=0$) so 
$$\arctan\left(\frac{524}{237}\right) = \arctan(2) + \arctan\left(\frac{2}{5}\right) - \arctan\left(\frac{16}{45}\right) $$