Package pygeodesy :: Module fmath |
[frames] | no frames]

# Module fmath

Utilities using precision floating point summation.

Version: 22.10.02

Classes
| Fdot Precision dot product.
| Fhorner Precision polynomial evaluation using the Horner form.
| Fhypot Precision hypotenuse of summation.
| Fpolynomial Precision polynomial evaluation.
| Fpowers Precision summation or powers, optimized for `power=2`.
| Fn_rt Precision n-th root of summation.
| Fcbrt Precision cubic root of summation.
| Fsqrt Precision square root of summation.

Functions
cbrt(x) Compute the cube root x**(1/3). |
cbrt2(x) Compute the cube root squared x**(2/3). |
euclid(x, y) Appoximate the norm sqrt(x**2 + y**2) by max(abs(x), abs(y)) + min(abs(x), abs(y)) * 0.4142.... |
euclid_(*xs) Appoximate the norm sqrt(sum(x**2 for x in xs)) by cascaded euclid. |
facos1(x) Fast approximation of pygeodesy.acos1`(x)`. |
fasin1(x) Fast approximation of pygeodesy.asin1`(x)`. |
fatan(x) Fast approximation of `atan(x)`. |
fatan1(x) Fast approximation of `atan(x)` for `0 <= x <= 1`, unchecked. |
fatan2(y, x) Fast approximation of `atan2(y, x)`. |
favg(v1, v2, f=0.5) Return the average of two values. |
fdot(a, *b) Return the precision dot product sum(a[i] * b[i] for i=0..len(a)). |
fdot3(a, b, c, start=0) Return the precision dot product start + sum(a[i] * b[i] * c[i] for i=0..len(a)). |
fhorner(x, *cs) Evaluate the polynomial sum(cs[i] * x**i for i=0..len(cs)) using the Horner form. |
fidw(xs, ds, beta=2) Interpolate using using Inverse Distance Weighting (IDW). |
fmean(xs) Compute the accurate mean sum(xs[i] for i=0..len(xs)) / len(xs). |
fmean_(*xs) Compute the accurate mean sum(xs[i] for i=0..len(xs)) / len(xs). |
fpolynomial(x, *cs, **over) Evaluate the polynomial sum(cs[i] * x**i for i=0..len(cs)) [/ over]. |
fpowers(x, n, alts=0) Return a series of powers [x**i for i=1..n]. |
fprod(xs, start=1) Iterable product, like `math.prod` or `numpy.prod`. |
frange(start, number, step=1) Generate a range of `float`s. |
value |
freduce(function, sequence, initial=...) Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. |
fremainder(x, y) Remainder in range `[-y / 2, y / 2]`. |
hypot(x, y) Return the Euclidean distance, sqrt(x*x + y*y). |
hypot_(*xs) Compute the norm sqrt(sum(x**2 for x in xs)). |
hypot1(x) Compute the norm sqrt(1 + x**2). |
hypot2(x, y) Compute the squared norm x**2 + y**2. |
hypot2_(*xs) Compute the squared norm `sum(x**2 for x in xs)`. |
norm2(x, y) Normalize a 2-dimensional vector. |
norm_(*xs) Normalize all n-dimensional vector components. |
sqrt0(x) Compute the square root iff `x >` EPS02. |
sqrt3(x) Compute the square root, cubed sqrt(x)**3 or sqrt(x**3). |
sqrt_a(h, b) Compute `a` side of a right-angled triangle from `sqrt(h**2 - b**2)`. |

Variables
| __all__ = `_ALL_LAZY.fmath`

Function Details

### cbrt (x) |
Compute the cube root x**(1/3). Arguments: `x` - Value (`scalar`). Returns: Cubic root (`float`). See Also: Functions cbrt2 and sqrt3.

### cbrt2 (x) |
Compute the cube root squared x**(2/3). Arguments: `x` - Value (`scalar`). Returns: Cube root squared (`float`). See Also: Functions cbrt and sqrt3.

### euclid (x, y) |
Appoximate the norm sqrt(x**2 + y**2) by max(abs(x), abs(y)) + min(abs(x), abs(y)) * 0.4142.... Arguments: `x` - X component (`scalar`). `y` - Y component (`scalar`). Returns: Appoximate norm (`float`). See Also: Function euclid_.

### euclid_ (*xs) |
Appoximate the norm sqrt(sum(x**2 for x in xs)) by cascaded euclid. Arguments: `xs` - X arguments, positional (`scalar`s). Returns: Appoximate norm (`float`). See Also: Function euclid.

### facos1 (x) |
Fast approximation of pygeodesy.acos1`(x)`. See Also: ShaderFastLibs.h.

### fasin1 (x) |
Fast approximation of pygeodesy.asin1`(x)`. See Also: facos1.

### fatan1 (x) |
Fast approximation of `atan(x)` for `0 <= x <= 1`, unchecked. See Also: ShaderFastLibs.h and Efficient approximations for the arctangent function, IEEE Signal Processing Magazine, 111, May 2006.

### fatan2 (y, x) |
Fast approximation of `atan2(y, x)`. See Also: fastApproximateAtan(x, y) and fatan1.

### favg (v1, v2, f=0.5) |
Return the average of two values. Arguments: `v1` - One value (`scalar`). `v2` - Other value (`scalar`). `f` - Optional fraction (`float`). Returns: v1 + f * (v2 - v1) (`float`).

### fdot (a, *b) |
Return the precision dot product sum(a[i] * b[i] for i=0..len(a)). Arguments: `a` - Iterable, list, tuple, etc. (`scalar`s). `b` - All positional arguments (`scalar`s). Returns: Dot product (`float`). Raises: `LenError` - Unequal `len(a)` and `len(b)`. See Also: Class Fdot and Algorithm 5.10 DotK.

### fdot3 (a, b, c, start=0) |
Return the precision dot product start + sum(a[i] * b[i] * c[i] for i=0..len(a)). Arguments: `a` - Iterable, list, tuple, etc. (`scalar`s). `b` - Iterable, list, tuple, etc. (`scalar`s). `c` - Iterable, list, tuple, etc. (`scalar`s). `start` - Optional bias (`scalar`). Returns: Dot product (`float`). Raises: `LenError` - Unequal `len(a)`, `len(b)` and/or `len(c)`. `OverflowError` - Partial `2sum` overflow.

### fhorner (x, *cs) |
Evaluate the polynomial sum(cs[i] * x**i for i=0..len(cs)) using the Horner form. Arguments: `x` - Polynomial argument (`scalar`). `cs` - Polynomial coeffients (`scalar`s). Returns: Horner value (`float`). Raises: `OverflowError` - Partial `2sum` overflow. `TypeError` - Non-scalar `x`. `ValueError` - No `cs` coefficients or `x` is not finite. See Also: Function fpolynomial and class Fhorner.

### fidw (xs, ds, beta=2) |
Interpolate using using Inverse Distance Weighting (IDW). Arguments: `xs` - Known values (`scalar`s). `ds` - Non-negative distances (`scalar`s). `beta` - Inverse distance power (`int`, 0, 1, 2, or 3). Returns: Interpolated value `x` (`float`). Raises: `LenError` - Unequal or zero `len(ds)` and `len(xs)`. `ValueError` - Invalid `beta`, negative `ds` value, weighted `ds` below EPS. Note: Using `beta=0` returns the mean of `xs`.

### fmean (xs) |
Compute the accurate mean sum(xs[i] for i=0..len(xs)) / len(xs). Arguments: `xs` - Values (`scalar` or Fsum instances). Returns: Mean value (`float`). Raises: `OverflowError` - Partial `2sum` overflow. `ValueError` - No `xs` values.

### fmean_ (*xs) |
Compute the accurate mean sum(xs[i] for i=0..len(xs)) / len(xs). Arguments: `xs` - Values (`scalar` or Fsum instances). Returns: Mean value (`float`). Raises: `OverflowError` - Partial `2sum` overflow. `ValueError` - No `xs` values.

### fpolynomial (x, *cs, **over) |
Evaluate the polynomial sum(cs[i] * x**i for i=0..len(cs)) [/ over]. Arguments: `x` - Polynomial argument (`scalar`). `cs` - Polynomial coeffients (`scalar`s), all positional. `over` - Optional, final divisor (`scalar` Returns: Polynomial value (`float`). Raises: `OverflowError` - Partial `2sum` overflow. `TypeError` - Non-scalar `x`. `ValueError` - No `cs` coefficients or `x` is not finite. See Also: Function fhorner and class Fpolynomial.

### fpowers (x, n, alts=0) |
Return a series of powers [x**i for i=1..n]. Arguments: `x` - Value (`scalar`). `n` - Highest exponent (`int`). `alts` - Only alternating powers, starting with this exponent (`int`). Returns: Powers of `x` (`float`s or `int`s). Raises: `TypeError` - Non-scalar `x` or `n` not `int`. `ValueError` - Non-finite `x` or non-positive `n`.

### fprod (xs, start=1) |
Iterable product, like `math.prod` or `numpy.prod`. Arguments: `xs` - Terms to be multiplied, an iterable, list, tuple, etc. (`scalar`s). `start` - Initial term, also the value returned for an empty `xs` (`scalar`). Returns: The product (`float`). See Also: NumPy.prod.

### frange (start, number, step=1) |
Generate a range of `float`s. Arguments: `start` - First value (`float`). `number` - The number of `float`s to generate (`int`). `step` - Increment value (`float`). Returns: A generator (`float`s). See Also: NumPy.prod.

### freduce (function, sequence, initial=...) |
Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. Returns: value

### fremainder (x, y) |
Remainder in range `[-y / 2, y / 2]`. Arguments: `x` - Numerator (`scalar`). `y` - Modulus, denominator (`scalar`). Returns: Remainder (`scalar`, preserving signed 0.0) or `NAN` for any non-finite `x`. Raises: `ValueError` - Infinite or near-zero `y`. See Also: Karney's Math.remainder and Python 3.7+ math.remainder.

### hypot_ (*xs) |
Compute the norm sqrt(sum(x**2 for x in xs)). Similar to Python 3.8+ n-dimension math.hypot, but exceptions, `nan` and `infinite` values are handled differently. Arguments: `xs` - X arguments (`scalar`s), all positional. Returns: Norm (`float`). Raises: `OverflowError` - Partial `2sum` overflow. `ValueError` - Invalid or no `xs` values. Note: The Python 3.8+ Euclidian distance math.dist between 2 n-dimensional points p1 and p2 can be computed as hypot_(*((c1 - c2) for c1, c2 in zip(p1, p2))), provided p1 and p2 have the same, non-zero length n.

### hypot1 (x) |
Compute the norm sqrt(1 + x**2). Arguments: `x` - Argument (`scalar`). Returns: Norm (`float`).

### hypot2 (x, y) |
Compute the squared norm x**2 + y**2. Arguments: `x` - X argument (`scalar`). `y` - Y argument (`scalar`). Returns: `x**2 + y**2` (`float`).

### hypot2_ (*xs) |
Compute the squared norm `sum(x**2 for x in xs)`. Arguments: `xs` - X arguments (`scalar`s), all positional. Returns: Squared norm (`float`). Raises: `OverflowError` - Partial `2sum` overflow. `ValueError` - Invalid or no `xs` value. See Also: Function hypot_.

### norm2 (x, y) |
Normalize a 2-dimensional vector. Arguments: `x` - X component (`scalar`). `y` - Y component (`scalar`). Returns: 2-Tuple `(x, y)`, normalized. Raises: `ValueError` - Invalid `x` or `y` or zero norm.

### norm_ (*xs) |
Normalize all n-dimensional vector components. Arguments: `xs` - Components (`scalar`s), all positional. Returns: Yield each component, normalized. Raises: `ValueError` - Invalid or insufficent `xs` or zero norm.

### sqrt0 (x) |
Compute the square root iff `x >` EPS02. Arguments: `x` - Value (`scalar`). Returns: Square root (`float`) or `0.0`. Note: Any `x <` EPS02 including `x < 0` returns `0.0`.

### sqrt3 (x) |
Compute the square root, cubed sqrt(x)**3 or sqrt(x**3). Arguments: `x` - Value (`scalar`). Returns: Square root cubed (`float`). Raises: `ValueError` - Negative `x`. See Also: Functions cbrt and cbrt2.

### sqrt_a (h, b) |
Compute `a` side of a right-angled triangle from `sqrt(h**2 - b**2)`. Arguments: `h` - Hypotenuse or outer annulus radius (`scalar`). `b` - Triangle side or inner annulus radius (`scalar`). Returns: `copysign(a, h)` or `unsigned 0.0` (`float`). Raises: `TypeError` - Non-scalar `h` or `b`. `ValueError` - If `abs(h) < abs(b)`. See Also: Inner tangent chord d of an annulus and function annulus_area.