Mathematical Utilities
Math functions that preserve PAL custom types.
This module provides wrappers around numpy math functions that preserve PAL’s custom types (StochasticScalar, etc.). Import as ‘pnp’ to mimic numpy usage patterns.
Type signatures are in maths.pyi.
- pal.maths.mean(x)[source]
Mean function that works with PAL types.
All PAL types implement the numpy array protocol, so this just delegates to numpy’s mean function which will dispatch to the appropriate __array_function__ or __array__ method.
- Return type:
- pal.maths.where(condition, x, y)[source]
Conditional selection that preserves PAL types.
- Return type:
- pal.maths.safe_divide(numerator, denominator, default)[source]
Divide numerator by denominator, returning default where denominator is 0.
Works with PAL types (StochasticScalar, FreqSevSims) and plain numpy arrays/scalars.
- Parameters:
- Return type:
- Returns:
The result of the division, with default substituted where division by zero would occur.
Examples
>>> from pal.stochastic_scalar import StochasticScalar >>> import pal.maths as pnp >>> x = StochasticScalar([10., 20., 30.]) >>> y = StochasticScalar([2., 0., 5.]) >>> result = pnp.safe_divide(x, y, 0.0) >>> result.values array([5., 0., 6.])