Pythagorean addition
In mathematics, Pythagorean addition is the following binary operation on the real numbers:
This operation provides a simple notation and terminology when the summands are complicated; for example, the energy-momentum relation in physics becomes
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.
Applications[]
If measurements X, Y, Z, ... have independent errors ΔX, ΔY, ΔZ, ..., respectively, the quadrature method gives the overall error,
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 6 digital numbers of shot noise, 3 of dark current noise and 2 of Johnson–Nyquist noise under a specific condition, the overall noise,
Properties[]
The operation ⊕ is associative and commutative,[5] and
The only element which could potentially act as an identity element is 0, since an identity e must satisfy e⊕e = e. This yields the equation , but if e is nonzero that implies , so e could only be zero. Unfortunately 0 does not work as an identity element after all, since 0⊕(−1) = 1. This does indicate, however, that if the operation ⊕ is restricted to nonnegative real numbers, then 0 does act as an identity. Consequently, the operation ⊕ acting on the nonnegative real numbers forms a commutative monoid.
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(x, y) avoids problems that occur when squaring very large or very small numbers. If calculated using the natural formula,
The hypot function is often used together with the atan2 function to convert from Cartesian coordinates to polar coordinates:
If either input is infinite, the result is infinite, i.e.
Because this is true for all possible values of x, including infinity, the IEEE 754 floating-point standard requires that this definition also applies if x is not a number (NaN).[6]
Implementation[]
The difficulty with the naive implementation is that x2 or y2 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|, and then use the equivalent form[7]
The computation of y/x cannot overflow unless both x and y are 0. 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.
More complex implementations avoid this by dividing the inputs into more cases:
- x ≫ y: hypot(x, y) = |x|, to within machine precision.
- x2 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. 264).
- y2 underflows: 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, e.g. to less than one ulp.[8]
Programming language support[]
The function is present in several programming languages:
- C99
- CSS[9]
- C++11[10]
- D (programming language)[11]
- Fortran 2008
- Julia (programming language)[12]
- Swift (programming language)
- Python (programming language)[13]
- Apple's PowerPC Numerics[14]
- MATLAB[15]
- Pascal[16]
- PHP[17]
- Java (programming language) (since version 1.5)[18]
- Kotlin[19]
- Ruby[20]
- Go[21]
- Rust[22]
- JavaScript (since ES2015)[23]
- Some C90 and C++ libraries have provided a hypot function.[24][25][26]
- Scala[27]
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. CiteSeerX 10.1.1.90.5651. 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.
- ^ 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. MR 3340218. S2CID 121424613.
- ^ Fog, Agner (2020-04-27). "Floating point exception tracking and NAN propagation" (PDF). p. 6.
- ^ In some situations, last form reduces calculation errors (in ULPs).
- ^ Borges, Carlos F. (14 June 2019). "An Improved Algorithm for hypot(a,b)". arXiv:1904.09481 [math.NA].
- ^ Cimpanu, Catalin. "CSS to get support for trigonometry functions". ZDNet. Retrieved 2019-11-01.
- ^ "Hypot - C++ Reference".
- ^ "STD.math - D Programming Language".
- ^ "Mathematics · the Julia Language".
- ^ "Math — Mathematical functions — Python 3.9.7 documentation".
- ^ https://developer.apple.com/DOCUMENTATION/mac/PPCNumerics/PPCNumerics-141.html
- ^ "Square root of sum of squares (Hypotenuse) - MATLAB hypot - MathWorks Benelux".
- ^ "Hypot function".
- ^ "PHP: Hypot - Manual".
- ^ "Math (Java 2 Platform SE 5.0)".
- ^ "hypot - Kotlin Programming Language". Kotlin. Retrieved 2018-03-19.
- ^ "Module: Math (Ruby 3.0.2)".
- ^ "Math package - math - PKG.go.dev".
- ^ "F64 - Rust".
- ^ "Math.hypot() - JavaScript | MDN".
- ^ Single Unix Specification, Open Group, http://www.opengroup.org/onlinepubs/007908799/xsh/hypot.html
- ^ IBM, ILE C/C++ Run-Time Library Functions, http://publib.boulder.ibm.com/infocenter/iadthelp/v7r0/index.jsp?topic=/com.ibm.etools.iseries.langref.doc/rzan5mst144.htm
- ^ The GNU C Library, Mathematics, http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_17.html Archived 2009-03-05 at the Wayback Machine
- ^ "Scala Standard Library 2.13.6 - scala.math".
Further reading[]
- Dubrulle, Augustin A. (1983). "A class of numerical methods for the computation of Pythagorean sums" (PDF). IBM Journal of Research and Development. 27 (6): 582–589. CiteSeerX 10.1.1.94.3443. doi:10.1147/rd.276.0582..
- Operations on numbers
- Pythagorean theorem
- Numerical analysis