scipy.special.log1p#

scipy.special.log1p(x, out=None) = <wrapped_ufunc 'log1p'>#

Calculates log(1 + x) for use when x is near zero.

Parameters:
xarray_like

Real or complex valued input.

outndarray, optional

Optional output array for the function results.

Returns:
scalar or ndarray

Values of log(1 + x).

See also

expm1, cosm1

Notes

Array API Standard Support

log1p 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 typically 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. log1p does not currently support out for the PyTorch backend.

See Array API coverage for more information.

Examples

>>> import numpy as np
>>> import scipy.special as sc

It is more accurate than using log(1 + x) directly for x near 0. Note that in the below example 1 + 1e-17 == 1 to double precision.

>>> sc.log1p(1e-17)
1e-17
>>> np.log(1 + 1e-17)
0.0