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 [math]\displaystyle{ a }[/math] and [math]\displaystyle{ b }[/math], this length can be calculated as [math]\displaystyle{ a \oplus b = \sqrt{a^2+b^2}, }[/math] where [math]\displaystyle{ \oplus }[/math] denotes the Pythagorean addition operation.[1]
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 [math]\displaystyle{ E = mc^2 \oplus pc. }[/math] 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;[2] 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 [math]\displaystyle{ (x,y) }[/math] to polar coordinates [math]\displaystyle{ (r,\theta) }[/math]:[3][4] [math]\displaystyle{ \begin{align} r&=x\oplus y=\operatorname{hypot}(x,y)\\ \theta&=\operatorname{atan2}(y,x).\\ \end{align} }[/math]
If measurements [math]\displaystyle{ X,Y,Z,\dots }[/math] have independent errors [math]\displaystyle{ \Delta_X, \Delta_Y, \Delta_Z, \dots }[/math] respectively, the quadrature method gives the overall error, [math]\displaystyle{ \varDelta_o = \sqrt{{\varDelta_X}^2 + {\varDelta_Y}^2 + {\varDelta_Z}^2 + \cdots} }[/math] whereas the upper limit of the overall error is [math]\displaystyle{ \varDelta_u = \varDelta_X + \varDelta_Y + \varDelta_Z + \cdots }[/math] if the errors were not independent.[5]
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 [math]\displaystyle{ \sigma = 6 \oplus 3 \oplus 2 = \sqrt{6^2 + 3^2 + 2^2} = 7 }[/math] digital numbers,[6] showing the dominance of larger sources of noise.
The root mean square of a finite set of [math]\displaystyle{ n }[/math] numbers is just their Pythagorean sum, normalized to form a generalized mean by dividing by [math]\displaystyle{ \sqrt n }[/math].
Properties
The operation [math]\displaystyle{ \oplus }[/math] is associative and commutative,[7] and [math]\displaystyle{ \sqrt{x_1^2 + x_2^2 + \cdots + x_n^2} = x_1 \oplus x_2 \oplus \cdots \oplus x_n. }[/math] This means that the real numbers under [math]\displaystyle{ \oplus }[/math] form a commutative semigroup.
The real numbers under [math]\displaystyle{ \oplus }[/math] are not a group, because [math]\displaystyle{ \oplus }[/math] 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, [math]\displaystyle{ r = \sqrt{x^2 + y^2}, }[/math] the squares of very large or small values of [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math] 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.[8]
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).[9]
Since C++17, there has been an additional hypot function for 3D calculations:[10] [math]\displaystyle{ r = \sqrt{x^2 + y^2 + z^2}. }[/math]
Calculation order
The difficulty with the naive implementation is that [math]\displaystyle{ x^2+y^2 }[/math] 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 [math]\displaystyle{ |x|\ge|y| }[/math], and then to use the equivalent form [math]\displaystyle{ \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} }[/math]
The computation of [math]\displaystyle{ y/x }[/math] cannot overflow unless both [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math] are zero. If [math]\displaystyle{ y/x }[/math] underflows, the final result is equal to [math]\displaystyle{ |x| }[/math], 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 [math]\displaystyle{ |x| }[/math] cannot underflow, and overflows only when the result is too large to represent.[8] 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.[11]
More complex implementations avoid this by dividing the inputs into more cases:
- When [math]\displaystyle{ x }[/math] is much larger than [math]\displaystyle{ y }[/math], [math]\displaystyle{ x\oplus y\approx|x| }[/math], to within machine precision.
- When [math]\displaystyle{ x^2 }[/math] overflows, multiply both [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math] 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. 264).
- When [math]\displaystyle{ y^2 }[/math] underflows, scale as above but reverse the scaling factors to scale up the intermediate values.
- Otherwise, the naive algorithm is safe to use.
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.[8]
Programming language support
The function is present in many programming languages and libraries, including CSS,[12] C++11,[13] D,[14] Go,[15] JavaScript (since ES2015),[16] Julia,[17] Java (since version 1.5),[18] Kotlin,[19] MATLAB,[20] PHP,[21] Python,[22] Ruby,[23] Rust,[24] and Scala.[25]
See also
- Euclidean distance
- Alpha max plus beta min algorithm
- Metafont has Pythagorean addition and subtraction as built-in operations, under the names
++
and+-+
respectively.
References
- ↑ Moler, Cleve; Morrison, Donald (1983). "Replacing square roots by Pythagorean sums". IBM Journal of Research and Development 27 (6): 577–581. doi:10.1147/rd.276.0577.
- ↑ Johnson, David L. (2017). "12.2.3 Addition in Quadrature". Statistical Tools for the Comprehensive Practice of Industrial Hygiene and Environmental Health Sciences. John Wiley & Sons. p. 289. ISBN 9781119143017. https://books.google.com/books?id=tRWuDQAAQBAJ&pg=PA289.
- ↑ "SIN (3M): Trigonometric functions and their inverses". Unix Programmer's Manual: Reference Guide (4.3 Berkeley Software Distribution Virtual VAX-11 Version ed.). Department of Electrical Engineering and Computer Science, University of California, Berkeley. April 1986. https://archive.org/details/upm-ref-4.3bsd/page/n257.
- ↑ Beebe, Nelson H. F. (2017). The Mathematical-Function Computation Handbook: Programming Using the MathCW Portable Software Library. Springer. p. 70. ISBN 9783319641102. https://books.google.com/books?id=OjUyDwAAQBAJ&pg=PA70.
- ↑ D.B. Schneider, Error Analysis in Measuring Systems, Proceedings of the 1962 Standards Laboratory Conference, page 94
- ↑ J.T. Bushberg et al, The Essential Physics of Medical Imaging, section 10.2.7, Wolters Kluwer Health
- ↑ Falmagne, Jean-Claude (2015). "Deriving meaningful scientific laws from abstract, "gedanken" type, axioms: five examples". Aequationes Mathematicae 89 (2): 393–435. doi:10.1007/s00010-015-0339-1.
- ↑ 8.0 8.1 8.2 Borges, Carlos F. (2021). "Algorithm 1014: An Improved Algorithm for hypot(x, y)". ACM Transactions on Mathematical Software 47 (1): 9:1–9:12. doi:10.1145/3428446.
- ↑ "Floating point exception tracking and NAN propagation". 2020-04-27. p. 6. https://www.agner.org/optimize/nan_propagation.pdf#page=6.
- ↑ Common mathematical functions std::hypot, std::hypotf, std::hypotl
- ↑ Measured on ARM and x64 (Intel and AMD) for different compilers with maximum optimization for 32 bit and 64 bit floats.
- ↑ Cimpanu, Catalin. "CSS to get support for trigonometry functions" (in en). https://www.zdnet.com/article/css-to-get-support-for-trigonometry-functions/.
- ↑ "Hypot - C++ Reference". http://www.cplusplus.com/reference/cmath/hypot/.
- ↑ "STD.math - D Programming Language". https://dlang.org/phobos/std_math.html#.hypot.
- ↑ "Math package - math - PKG.go.dev". http://golang.org/pkg/math/#Hypot.
- ↑ "Math.hypot() - JavaScript | MDN". 21 February 2023. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/hypot.
- ↑ "Mathematics · the Julia Language". https://docs.julialang.org/en/v1/base/math/#Base.Math.hypot.
- ↑ "Math (Java 2 Platform SE 5.0)". http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html#hypot(double,%20double).
- ↑ "hypot - Kotlin Programming Language". https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.math/hypot.html.
- ↑ "Square root of sum of squares (Hypotenuse) - MATLAB hypot - MathWorks Benelux". http://nl.mathworks.com/help/matlab/ref/hypot.html.
- ↑ "PHP: Hypot - Manual". http://www.php.net/hypot.
- ↑ "Math — Mathematical functions — Python 3.9.7 documentation". https://docs.python.org/3/library/math.html#math.hypot.
- ↑ "Module: Math (Ruby 3.0.2)". http://ruby-doc.org/core/Math.html#method-c-hypot.
- ↑ "F64 - Rust". https://doc.rust-lang.org/std/primitive.f64.html#method.hypot.
- ↑ "Scala Standard Library 2.13.6 - scala.math". https://www.scala-lang.org/api/current/scala/math/index.html#hypot(x:Double,y:Double):Double.
Further reading
- Dubrulle, Augustin A. (1983). "A class of numerical methods for the computation of Pythagorean sums". IBM Journal of Research and Development 27 (6): 582–589. doi:10.1147/rd.276.0582. http://www.research.ibm.com/journal/rd/276/ibmrd2706Q.pdf..
Original source: https://en.wikipedia.org/wiki/Pythagorean addition.
Read more |