# 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. Like the more familiar addition and multiplication operations of arithmetic, it is both associative and commutative.
This operation can be used in the conversion of Cartesian coordinates to polar coordinates, and in the calculation of Euclidean distance. It also provides a simple notation and terminology for the diameter of a cuboid, the energy-momentum relation in physics, and the overall noise from independent sources of noise. In its applications to signal processing and propagation of measurement uncertainty, the same operation is also called addition in quadrature. A scaled version of this operation gives the quadratic mean or root mean square.
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. Donald Knuth has written that "Most of the square root operations in computer programs could probably be avoided if [Pythagorean addition] were more widely available, because people seem to want square roots primarily when they are computing distances."

## Definition

Hypotenuse calculator :
| a | 3 |
| b | 4 |
| c = a ⊕ b | 5 5 |
According to the Pythagorean theorem, for a right triangle with side lengths $a$ and $b$, the length of the hypotenuse can be calculated as √(a$^2$+b$^2$).{\textstyle {\sqrt {a^{2}+b^{2}}}.} This formula defines the Pythagorean addition operation, denoted here as $\oplus$: for any two real numbers $a$ and $b$, the result of this operation is defined to be $a\oplus b={\sqrt {a^{2}+b^{2}{\vphantom}}}}.$ For instance, the special right triangle based on the Pythagorean triple $(3,4,5)$ gives $3\oplus 4=5$. However, the integer result of this example is unusual: for other integer arguments, Pythagorean addition can produce a quadratic irrational number as its result.

## Properties

The operation $\oplus$ is associative and commutative. Therefore, if three or more numbers are to be combined with this operation, the order of combination makes no difference to the result: $x_{1}\oplus x_{2}\oplus \cdots \oplus x_{n}={\sqrt {x_{1}^{2}+x_{2}^{2}+\cdots +x_{n}^{2}}}.$ Additionally, on the non-negative real numbers, zero is an identity element for Pythagorean addition. On numbers that can be negative, the Pythagorean sum with zero gives the absolute value: $x\oplus 0=|x|.$ The three properties of associativity, commutativity, and having an identity element (on the non-negative numbers) are the defining properties of a commutative monoid.

## Applications
### Distance and diameter

The Euclidean distance between two points in the Euclidean plane, given by their Cartesian coordinates $(x_{1},y_{1})$ and $(x_{2},y_{2})$, is $(x_{1}-x_{2})\oplus (y_{1}-y_{2}).$ In the same way, the distance between three-dimensional points $(x_{1},y_{1},z_{1})$ and $(x_{2},y_{2},z_{2})$ can be found by repeated Pythagorean addition as $(x_{1}-x_{2})\oplus (y_{1}-y_{2})\oplus (z_{1}-z_{2}).$
Repeated Pythagorean addition can also find the diagonal length of a rectangle and the diameter of a rectangular cuboid. For a rectangle with sides $a$ and $b$, the diagonal length is $a\oplus b$. For a cuboid, the diameter is the longest distance between two points, the length of the body diagonal of the cuboid. For a cuboid with side lengths $a$, $b$, and $c$, this length is $a\oplus b\oplus c$.

### Coordinate conversion

Pythagorean addition (and its implementation as the hypot function) is often used together with the atan2 function (a two-parameter form of the arctangent) to convert from Cartesian coordinates $(x,y)$ to polar coordinates $(r,\theta )$: ${\begin{aligned}r&=x\oplus y={\mathsf {hypot}}(x,y)\\\theta &={\mathsf {atan2}}(y,x).\\\end{aligned}}$

### Quadratic mean and spread of deviation

The root mean square or quadratic mean of a finite set of $n$ numbers is ${\tfrac {1}{\sqrt {n}}}$ times their Pythagorean sum. This is a generalized mean of the numbers.
The standard deviation of a collection of observations is the quadratic mean of their individual deviations from the mean. When two or more independent random variables are added, the standard deviation of their sum is the Pythagorean sum of their standard deviations. Thus, the Pythagorean sum itself can be interpreted as giving the amount of overall noise when combining independent sources of noise.
If the engineering tolerances of different parts of an assembly are treated as independent noise, they can be combined using a Pythagorean sum. In experimental sciences such as physics, addition in quadrature is often used to combine different sources of measurement uncertainty. However, this method of propagation of uncertainty applies only when there is no correlation between sources of uncertainty, and it has been criticized for conflating experimental noise with systematic errors.

### Other

The energy-momentum relation in physics, describing the energy of a moving particle, can be expressed as the Pythagorean sum $E=mc^{2}\oplus pc,$ where $m$ is the rest mass of a particle, $p$ is its momentum, $c$ is the speed of light, and $E$ is the particle's resulting relativistic energy.
When combining signals, it can be a useful design technique to arrange for the combined signals to be orthogonal in polarization or phase, so that they add in quadrature. In early radio engineering, this idea was used to design directional antennas, allowing signals to be received while nullifying the interference from signals coming from other directions. When the same technique is applied in software to obtain a directional signal from a radio or ultrasound phased array, Pythagorean addition may be used to combine the signals. Other recent applications of this idea include improved efficiency in the frequency conversion of lasers.
In the psychophysics of haptic perception, Pythagorean addition has been proposed as a model for the perceived intensity of vibration when two kinds of vibration are combined.
In image processing, the Sobel operator for edge detection consists of a convolution step to determine the gradient of an image followed by a Pythagorean sum at each pixel to determine the magnitude of the gradient.

## Implementation

In a 1983 paper, Cleve Moler and Donald Morrison described an iterative method for computing Pythagorean sums, without taking square roots. This was soon recognized to be an instance of Halley's method, and extended to analogous operations on matrices.
Although many modern implementations of this operation instead compute Pythagorean sums by reducing the problem to the square root function, they do so in a way that has been designed to avoid errors arising from the limited-precision calculations performed on computers. 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. This may to an inaccurate result caused by arithmetic underflow and overflow, although when overflow and underflow do not occur the output is within two ulp of the exact result. Common implementations of the hypot function rearrange this calculation in a way that avoids the problem of overflow and underflow and are even more precise.
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).

### 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|\geq |y|$, and then to use the equivalent form $r=|x|{\sqrt {1+\left({\frac {y}{x}}\right)^{2}}}.$
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.
One drawback of this rearrangement is the additional division by $x$, which increases both the time and inaccuracy of the computation. More complex implementations avoid these costs by dividing the inputs into more cases:
* 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$^{−64}$ for IEEE single precision), use the naive algorithm which will now not overflow, and multiply the result by the (large) inverse (e.g. 2$^{64}$).
* 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.
Additional techniques allow the result to be computed more accurately than the naive algorithm, e.g. to less than one ulp. Researchers have also developed analogous algorithms for computing Pythagorean sums of more than two values.

### Fast approximation

The alpha max plus beta min algorithm is a high-speed approximation of Pythagorean addition using only comparison, multiplication, and addition, producing a value whose error is less than 4% of the correct result. It is computed as $a\oplus b\approx \alpha \cdot \max(a,b)+\beta \cdot \min(a,b)$ for a careful choice of parameters $\alpha$ and $\beta$.

### Programming language support

Pythagorean addition function is present as the hypot function in many programming languages and their libraries. These include: CSS, D, Fortran, Go, JavaScript (since ES2015), Julia, MATLAB, PHP, and Python. C++11 includes a two-argument version of hypot, and a three-argument version for $x\oplus y\oplus z$ has been included since C++17. The Java implementation of hypot can be used by its interoperable JVM-based languages including Apache Groovy, Clojure, Kotlin, and Scala. Similarly, the version of hypot included with Ruby extends to Ruby-based domain-specific languages such as Progress Chef. In Rust, hypot is implemented as a method of floating point objects rather than as a two-argument function.
Metafont has Pythagorean addition and subtraction as built-in operations, under the symbols ++ and +-+ respectively.

## History

The Pythagorean theorem on which this operation is based was studied in ancient Greek mathematics, and may have been known earlier in Egyptian mathematics and Babylonian mathematics; see Pythagorean theorem § History. However, its use for computing distances in Cartesian coordinates could not come until after René Descartes invented these coordinates in 1637; the formula for distance from these coordinates was published by Alexis Clairaut in 1731.
The terms "Pythagorean addition" and "Pythagorean sum" for this operation have been used at least since the 1950s, and its use in signal processing as "addition in quadrature" goes back at least to 1919.
From the 1920s to the 1940s, before the widespread use of computers, multiple designers of slide rules included square-root scales in their devices, allowing Pythagorean sums to be calculated mechanically. Researchers have also investigated analog circuits for approximating the value of Pythagorean sums.