# How to calculate $\arg(z_1z_2\cdots z_n)$ to minimize results error?

As in title, which method is the most optimal for numerical calculating value of: $\arg(z_1z_2\cdots z_n)$?

Method 1: one can first calculate $Z=z_1z_2\cdots z_n$ and then calculate $\arg(Z)$.

Method 2: using property $\arg(z_1z_2\cdots z_n) = \arg(z_1) + \dots +\arg(z_n)$

Or maybe something else?

• What your question have to do with numerical stability? Jan 5 '18 at 21:20
• How to calculate $\arg(z_1z_2\cdots z_n)$ to minimize results error? Jan 5 '18 at 21:39
• What form are your $z_{i}$ in (e.g. $a+bi$ with $a$ and $b$ in IEEE double precision, or polar form $z=re^{i\theta}$)? What are you willing to assume about your ability to compute $\arg(z_{i})$? Will you assume that you can accurately reduce the sum of the args by multiples of $2\pi$? Jan 5 '18 at 22:50
• Please edit the title of your question, it has nothing to do with numerical stability. Jan 7 '18 at 3:41

As Brian Borchers pointed out, the question only makes sense up to factors of $2\pi$. I'll assume that you want an answer in $[0, 2\pi)$ then.

If there are really big contrasts in the magnitudes $|z_1|, \ldots, |z_n|$ of the numbers you're looking at, you could get some bogus results because of cancellation errors. In that case, it might arguably be better to compute all the arguments first and then add the result, since the magnitude of the product isn't even what you care about. On the other hand, you can concoct a similar edge case for the second approach by taking $z_1 = i$, $z_2 = \ldots = z_n = e^{i\epsilon}$ for really small $\epsilon$, and all of those numbers have the same magnitude. So it might be a tossup depending on the nature of the inputs. You'll probably want to use pairwise summation or better yet Kahan summation, both of which can dramatically improve the accuracy of big sums.

No matter how you choose to go about the problem, measuring the results yourself will always be better than the advice of strangers on the internet. The program herbgrind is a tool for finding where floating-point expressions go awry. You can also try using a library for arbitrary-precision floating point arithmetic like GMP to check how well one approach works over the other.

Because $\mbox{arg}$ wraps around at $2\pi$, the problem is ill-conditioned. You can get a forward error of $2\pi$ (when the sum of the arguments is close to a multiple of $2\pi$) with a backward error that approaches 0. This is true of both of the methods you've proposed.

To sum the argument works faster and the calculus error is smaller. I think that that is the best way.

If we perform the product $Z = z_1 \times z_2 \times ... \times z_n$ the error will be:

$$dZ = z_2 z_3 ... z_n dz_1 + z_1 z_3 .... z_n dz_2 + ... + z_1 z_2 ... z_{n-1} dz_n$$

We conclude that:

$$dZ \approx Z \, (1/z_1 + 1/z_2 + ... 1/z_n) \, dz$$

where $dz$ is the approximate error of each factor.

The error in the argument of $Z$ will be proportional to this error.

If we sum the arguments, the error will be:

$$d \arg(Z) = \sum_{k=1}^n dz_k \approx n \, dz$$

• Can you actually prove this using stability analysis and flop counting? Otherwise this is more of a comment. Jan 6 '18 at 9:55