Misplaced Pages

Pythagorean addition

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Defined for two real numbers as the square root of the sum of their squares

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 {\displaystyle a} and b {\displaystyle b} , this length can be calculated as a b = a 2 + b 2 , {\displaystyle a\oplus b={\sqrt {a^{2}+b^{2}}},} where {\displaystyle \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 = m c 2 p c . {\displaystyle 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 finds the length of the body diagonal of a rectangular cuboid, or equivalently the length of the vector sum of orthogonal vectors.

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

Repeated Pythagorean addition can find the diameter of a rectangular cuboid. This is the longest distance between two points, the length of the body diagonal of the cuboid. For a cuboid with side lengths a {\displaystyle a} , b {\displaystyle b} , and c {\displaystyle c} , this length is a b c {\displaystyle a\oplus b\oplus c} .

If measurements X , Y , Z , {\displaystyle X,Y,Z,\dots } have independent errors Δ X , Δ Y , Δ Z , {\displaystyle \Delta _{X},\Delta _{Y},\Delta _{Z},\dots } respectively, the quadrature method gives the overall error, Δ o = Δ X 2 + Δ Y 2 + Δ Z 2 + {\displaystyle \varDelta _{o}={\sqrt {{\varDelta _{X}}^{2}+{\varDelta _{Y}}^{2}+{\varDelta _{Z}}^{2}+\cdots }}} whereas the upper limit of the overall error is Δ u = Δ X + Δ Y + Δ Z + {\displaystyle \varDelta _{u}=\varDelta _{X}+\varDelta _{Y}+\varDelta _{Z}+\cdots } if the errors were not independent.

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 σ = 6 3 2 = 6 2 + 3 2 + 2 2 = 7 {\displaystyle \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 {\displaystyle n} numbers is 1 n {\displaystyle {\tfrac {1}{\sqrt {n}}}} times their Pythagorean sum. This is a generalized mean of the numbers.

Properties

The operation {\displaystyle \oplus } is associative and commutative, and x 1 2 + x 2 2 + + x n 2 = x 1 x 2 x n . {\displaystyle {\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 {\displaystyle \oplus } form a commutative semigroup.

The real numbers under {\displaystyle \oplus } are not a group, because {\displaystyle \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 = x 2 + y 2 , {\displaystyle r={\sqrt {x^{2}+y^{2}}},} the squares of very large or small values of x {\displaystyle x} and y {\displaystyle 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 = x 2 + y 2 + z 2 . {\displaystyle r={\sqrt {x^{2}+y^{2}+z^{2}}}.}

Calculation order

The difficulty with the naive implementation is that x 2 + y 2 {\displaystyle 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 | | y | {\displaystyle |x|\geq |y|} , and then to use the equivalent form r = | x | 1 + ( y x ) 2 . {\displaystyle r=|x|{\sqrt {1+\left({\frac {y}{x}}\right)^{2}}}.}

The computation of y / x {\displaystyle y/x} cannot overflow unless both x {\displaystyle x} and y {\displaystyle y} are zero. If y / x {\displaystyle y/x} underflows, the final result is equal to | x | {\displaystyle |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 | {\displaystyle |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:

  • When x {\displaystyle x} is much larger than y {\displaystyle y} , x y | x | {\displaystyle x\oplus y\approx |x|} , to within machine precision.
  • When x 2 {\displaystyle x^{2}} overflows, multiply both x {\displaystyle x} and y {\displaystyle y} by a small scaling factor (e.g. 2 for IEEE single precision), use the naive algorithm which will now not overflow, and multiply the result by the (large) inverse (e.g. 2).
  • When y 2 {\displaystyle 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.

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.

Programming language support

Pythagorean addition function is present as the hypot function in many programming languages and libraries, including CSS, C++11, D, Fortran (since Fortran 2008), Go, JavaScript (since ES2015), Julia, Java (since version 1.5), Kotlin, MATLAB, PHP, Python, Ruby, Rust, and Scala.

Metafont has Pythagorean addition and subtraction as built-in operations, under the names ++ and +-+ respectively.

See also

References

  1. Moler, Cleve; Morrison, Donald (1983). "Replacing square roots by Pythagorean sums". IBM Journal of Research and Development. 27 (6): 577–581. CiteSeerX 10.1.1.90.5651. doi:10.1147/rd.276.0577.
  2. 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.
  3. "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.
  4. Beebe, Nelson H. F. (2017). The Mathematical-Function Computation Handbook: Programming Using the MathCW Portable Software Library. Springer. p. 70. ISBN 9783319641102.
  5. D.B. Schneider, Error Analysis in Measuring Systems, Proceedings of the 1962 Standards Laboratory Conference, page 94
  6. J.T. Bushberg et al, The Essential Physics of Medical Imaging, section 10.2.7, Wolters Kluwer Health
  7. 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. MR 3340218. S2CID 121424613.
  8. ^ Borges, Carlos F. (2021). "Algorithm 1014: An Improved Algorithm for hypot(x, y)". ACM Transactions on Mathematical Software. 47 (1): 9:1–9:12. arXiv:1904.09481. doi:10.1145/3428446. S2CID 230588285.
  9. Fog, Agner (2020-04-27). "Floating point exception tracking and NAN propagation" (PDF). p. 6.
  10. Common mathematical functions std::hypot, std::hypotf, std::hypotl
  11. Measured on ARM and x64 (Intel and AMD) for different compilers with maximum optimization for 32 bit and 64 bit floats.
  12. Cimpanu, Catalin. "CSS to get support for trigonometry functions". ZDNet. Retrieved 2019-11-01.
  13. "Hypot - C++ Reference".
  14. "STD.math - D Programming Language".
  15. "The new features of Fortran 2008" (PDF).
  16. "Math package - math - PKG.go.dev".
  17. "Math.hypot() - JavaScript | MDN". 21 February 2023.
  18. "Mathematics · the Julia Language".
  19. "Math (Java 2 Platform SE 5.0)".
  20. "hypot - Kotlin Programming Language". Kotlin. Retrieved 2018-03-19.
  21. "Square root of sum of squares (Hypotenuse) - MATLAB hypot - MathWorks Benelux".
  22. "PHP: Hypot - Manual".
  23. "Math — Mathematical functions — Python 3.9.7 documentation".
  24. "Module: Math (Ruby 3.0.2)".
  25. "F64 - Rust".
  26. "Scala Standard Library 2.13.6 - scala.math".

Further reading

Categories: