scipy.special.exprel#

scipy.special.exprel(x, out=None) = <ufunc 'exprel'>#

Relative error exponential, (exp(x) - 1)/x.

When x is near zero, exp(x) is near 1, so the numerical calculation of exp(x) - 1 can suffer from catastrophic loss of precision. exprel(x) is implemented to avoid the loss of precision that occurs when x is near zero.

Parameters:
xndarray

Input array. x must contain real numbers.

outndarray, optional

Optional output array for the function values

Returns:
scalar or ndarray

(exp(x) - 1)/x, computed element-wise.

See also

expm1

Notes

Added in version 0.17.0.

Array API Standard Support

exprel has support for Python Array API Standard compatible backends in addition to NumPy. The following combinations of backend and device (or other capability) are supported.

Library

CPU

GPU

NumPy

n/a

CuPy

n/a

PyTorch

JAX

Dask

n/a

For the NumPy backend, this function supports all NumPy ufunc keyword arguments. Other backends may support out, but none of the other ufunc kwargs. out is typically supported for CuPy and PyTorch, but not currently in cases where SciPy relies on a generic Array API implementation or, for PyTorch on CPU, falls back to the NumPy backend. out is never supported for JAX because JAX arrays are immutable. exprel does not currently support out for the PyTorch backend.

See Support for the array API standard for more information.

Examples

>>> import numpy as np
>>> from scipy.special import exprel
>>> exprel(0.01)
1.0050167084168056
>>> exprel([-0.25, -0.1, 0, 0.1, 0.25])
array([ 0.88479687,  0.95162582,  1.        ,  1.05170918,  1.13610167])

Compare exprel(5e-9) to the naive calculation. The exact value is 1.00000000250000000416....

>>> exprel(5e-9)
1.0000000025
>>> (np.exp(5e-9) - 1)/5e-9
0.99999999392252903