Pythagorean addition

In mathematics, Pythagorean addition is a binary operation on the real numbers that computes the length of the hypotenuse of a right triangle, given its two sides. According to the Pythagorean theorem, for a triangle with sides $$a$$ and $$b$$, this length can be calculated as $$a \oplus b = \sqrt{a^2+b^2},$$ where $$\oplus$$ denotes the Pythagorean addition operation.

This operation can be used in the conversion of Cartesian coordinates to polar coordinates. It also provides a simple notation and terminology for some formulas when its summands are complicated; for example, the energy-momentum relation in physics becomes $$E = mc^2 \oplus pc.$$ It is implemented in many programming libraries as the hypot function, in a way designed to avoid errors arising due to limited-precision calculations performed on computers. In its applications to signal processing and propagation of measurement uncertainty, the same operation is also called addition in quadrature; it is related to the quadratic mean or "root mean square".

Applications


Pythagorean addition (and its implementation as the hypot function) is often used together with the atan2 function to convert from Cartesian coordinates $$(x,y)$$ to polar coordinates $$(r,\theta)$$: $$ \begin{align} r&=x\oplus y=\operatorname{hypot}(x,y)\\ \theta&=\operatorname{atan2}(y,x).\\ \end{align} $$

If measurements $$X,Y,Z,\dots$$ have independent errors $$\Delta_X, \Delta_Y, \Delta_Z, \dots$$ respectively, the quadrature method gives the overall error, $$\varDelta_o = \sqrt{{\varDelta_X}^2 + {\varDelta_Y}^2 + {\varDelta_Z}^2 + \cdots}$$ whereas the upper limit of the overall error is $$\varDelta_u = \varDelta_X + \varDelta_Y + \varDelta_Z + \cdots$$ if the errors were not independent.

This is equivalent of finding the magnitude of the resultant of adding orthogonal vectors, each with magnitude equal to the uncertainty, using the Pythagorean theorem.

In signal processing, addition in quadrature is used to find the overall noise from independent sources of noise. For example, if an image sensor gives six digital numbers of shot noise, three of dark current noise and two of Johnson–Nyquist noise under a specific condition, the overall noise is $$\sigma = 6 \oplus 3 \oplus 2 = \sqrt{6^2 + 3^2 + 2^2} = 7$$ digital numbers, showing the dominance of larger sources of noise.

The root mean square of a finite set of $$n$$ numbers is just their Pythagorean sum, normalized to form a generalized mean by dividing by $$\sqrt n$$.

Properties
The operation $$\oplus$$ is associative and commutative, and $$\sqrt{x_1^2 + x_2^2 + \cdots + x_n^2} = x_1 \oplus x_2 \oplus \cdots \oplus x_n.$$ This means that the real numbers under $$\oplus$$ form a commutative semigroup.

The real numbers under $$\oplus$$ are not a group, because $$\oplus$$ can never produce a negative number as its result, whereas each element of a group must be the result of applying the group operation to itself and the identity element. On the non-negative numbers, it is still not a group, because Pythagorean addition of one number by a second positive number can only increase the first number, so no positive number can have an inverse element. Instead, it forms a commutative monoid on the non-negative numbers, with zero as its identity.

Implementation
Hypot is a mathematical function defined to calculate the length of the hypotenuse of a right-angle triangle. It was designed to avoid errors arising due to limited-precision calculations performed on computers. Calculating the length of the hypotenuse of a triangle is possible using the square root function on the sum of two squares, but hypot avoids problems that occur when squaring very large or very small numbers. If calculated using the natural formula, $$r = \sqrt{x^2 + y^2},$$ the squares of very large or small values of $$x$$ and $$y$$ may exceed the range of machine precision when calculated on a computer, leading to an inaccurate result caused by arithmetic underflow and overflow. The hypot function was designed to calculate the result without causing this problem.

If either input to hypot is infinite, the result is infinite. Because this is true for all possible values of the other input, the IEEE 754 floating-point standard requires that this remains true even when the other input is not a number (NaN).

Since C++17, there has been an additional hypot function for 3D calculations: $$r = \sqrt{x^2 + y^2 + z^2}.$$

Calculation order
The difficulty with the naive implementation is that $$x^2+y^2$$ may overflow or underflow, unless the intermediate result is computed with extended precision. A common implementation technique is to exchange the values, if necessary, so that $$|x|\ge|y|$$, and then to use the equivalent form $$\begin{align} r &= \sqrt{x^2 + y^2} \\ &= \sqrt{x^2 \!\left(1 + \left(\tfrac{y}{x}\right)^2\right)} \\ &= |x| \sqrt{1 + \left(\tfrac{y}{x}\right)^2} \left(= |x| + \frac{y}{|x|}\frac{y}{1 + \sqrt{1 + \left(\tfrac{y}{x}\right)^2 }}\right)\!. \end{align}$$

The computation of $$y/x$$ cannot overflow unless both $$x$$ and $$y$$ are zero. If $$y/x$$ underflows, the final result is equal to $$|x|$$, which is correct within the precision of the calculation. The square root is computed of a value between 1 and 2. Finally, the multiplication by $$|x|$$ cannot underflow, and overflows only when the result is too large to represent. This implementation has the downside that it requires an additional floating-point division, which can double the cost of the naive implementation, as multiplication and addition are typically far faster than division and square root. Typically, the implementation is slower by a factor of 2.5 to 3.

More complex implementations avoid this by dividing the inputs into more cases: However, this implementation is extremely slow when it causes incorrect jump predictions due to different cases. Additional techniques allow the result to be computed more accurately, e.g. to less than one ulp.
 * When $$x$$ is much larger than $$y$$, $$x\oplus y\approx|x|$$, to within machine precision.
 * When $$x^2$$ overflows, multiply both $$x$$ and $$y$$ by a small scaling factor (e.g. 2&minus;64 for IEEE single precision), use the naive algorithm which will now not overflow, and multiply the result by the (large) inverse (e.g. 264).
 * When $$y^2$$ underflows, scale as above but reverse the scaling factors to scale up the intermediate values.
 * Otherwise, the naive algorithm is safe to use.

Programming language support
The function is present in many programming languages and libraries, including CSS, C++11, D, Go, JavaScript (since ES2015), Julia, Java (since version 1.5), Kotlin, MATLAB, PHP, Python, Ruby, Rust, and Scala.