Basic functions

xtensor provides the following basic functions for xexpressions and scalars:

Defined in xtensor/core/xmath.hpp

group Basic functions

Functions

template<class E>
inline auto abs(E &&e) noexcept -> detail::xfunction_type_t<math::abs_fun, E>

Absolute value function.

Returns an xfunction for the element-wise absolute value of e.

Parameters:

e – an xexpression

Returns:

an xfunction

template<class E>
inline auto fabs(E &&e) noexcept -> detail::xfunction_type_t<math::fabs_fun, E>

Absolute value function.

Returns an xfunction for the element-wise absolute value of e.

Parameters:

e – an xexpression

Returns:

an xfunction

template<class E1, class E2>
inline auto fmod(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t<math::fmod_fun, E1, E2>

Remainder of the floating point division operation.

Returns an xfunction for the element-wise remainder of the floating point division operation e1 / e2.

Note

e1 and e2 can’t be both scalars.

Parameters:
Returns:

an xfunction

template<class E1, class E2>
inline auto remainder(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t<math::remainder_fun, E1, E2>

Signed remainder of the division operation.

Returns an xfunction for the element-wise signed remainder of the floating point division operation e1 / e2.

Note

e1 and e2 can’t be both scalars.

Parameters:
Returns:

an xfunction

template<class E1, class E2, class E3>
inline auto fma(E1 &&e1, E2 &&e2, E3 &&e3) noexcept -> detail::xfunction_type_t<math::fma_fun, E1, E2, E3>

Fused multiply-add operation.

Returns an xfunction for e1 * e2 + e3 as if to infinite precision and rounded only once to fit the result type.

Note

e1, e2 and e3 can’t be scalars every three.

Parameters:
Returns:

an xfunction

template<class E1, class E2>
inline auto fmax(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t<math::fmax_fun, E1, E2>

Maximum function.

Returns an xfunction for the element-wise maximum of e1 and e2.

Note

e1 and e2 can’t be both scalars.

Parameters:
Returns:

an xfunction

template<class E1, class E2>
inline auto fmin(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t<math::fmin_fun, E1, E2>

Minimum function.

Returns an xfunction for the element-wise minimum of e1 and e2.

Note

e1 and e2 can’t be both scalars.

Parameters:
Returns:

an xfunction

template<class E1, class E2>
inline auto fdim(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t<math::fdim_fun, E1, E2>

Positive difference function.

Returns an xfunction for the element-wise positive difference of e1 and e2.

Note

e1 and e2 can’t be both scalars.

Parameters:
Returns:

an xfunction

template<class E>
inline auto deg2rad(E &&e) noexcept -> detail::xfunction_type_t<math::deg2rad, E>

Convert angles from degrees to radians.

Returns an xfunction for the element-wise corresponding angle in radians of e.

Parameters:

e – an xexpression

Returns:

an xfunction

template<class E>
inline auto radians(E &&e) noexcept -> detail::xfunction_type_t<math::deg2rad, E>

Convert angles from degrees to radians.

Returns an xfunction for the element-wise corresponding angle in radians of e.

Parameters:

e – an xexpression

Returns:

an xfunction

template<class E>
inline auto rad2deg(E &&e) noexcept -> detail::xfunction_type_t<math::rad2deg, E>

Convert angles from radians to degrees.

Returns an xfunction for the element-wise corresponding angle in degrees of e.

Parameters:

e – an xexpression

Returns:

an xfunction

template<class E>
inline auto degrees(E &&e) noexcept -> detail::xfunction_type_t<math::rad2deg, E>

Convert angles from radians to degrees.

Returns an xfunction for the element-wise corresponding angle in degrees of e.

Parameters:

e – an xexpression

Returns:

an xfunction

template<class E1, class E2>
inline auto maximum(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t<math::maximum<void>, E1, E2>

Elementwise maximum.

Returns an xfunction for the element-wise maximum between e1 and e2.

Parameters:
Returns:

an xfunction

template<class E1, class E2>
inline auto minimum(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t<math::minimum<void>, E1, E2>

Elementwise minimum.

Returns an xfunction for the element-wise minimum between e1 and e2.

Parameters:
Returns:

an xfunction

template<class T = void, class E, class X, class EVS = std::tuple<evaluation_strategy::lazy_type>, xtl::check_concept<std::negation<is_reducer_options<X>>, std::negation<xtl::is_integral<std::decay_t<X>>>> = 0>
inline auto amax(E &&e, X &&axes, EVS es = EVS())

Maximum element along given axis.

Returns an xreducer for the maximum of elements over given axes.

Parameters:
  • e – an xexpression

  • axes – the axes along which the maximum is found (optional)

  • es – evaluation strategy of the reducer

Returns:

an xreducer

template<class T = void, class E, class X, class EVS = std::tuple<evaluation_strategy::lazy_type>, xtl::check_concept<std::negation<is_reducer_options<X>>, std::negation<xtl::is_integral<std::decay_t<X>>>> = 0>
inline auto amin(E &&e, X &&axes, EVS es = EVS())

Minimum element along given axis.

Returns an xreducer for the minimum of elements over given axes.

Parameters:
  • e – an xexpression

  • axes – the axes along which the minimum is found (optional)

  • es – evaluation strategy of the reducer

Returns:

an xreducer

template<class E1, class E2, class E3>
inline auto clip(E1 &&e1, E2 &&lo, E3 &&hi) noexcept -> detail::xfunction_type_t<math::clamp_fun, E1, E2, E3>

Clip values between hi and lo.

Returns an xfunction for the element-wise clipped values between lo and hi

Parameters:
  • e1 – an xexpression or a scalar

  • lo – a scalar

  • hi – a scalar

Returns:

a xfunction

template<class E>
inline auto sign(E &&e) noexcept -> detail::xfunction_type_t<math::sign_fun, E>

Returns an element-wise indication of the sign of a number.

If the number is positive, returns +1. If negative, -1. If the number is zero, returns 0.

Parameters:

e – an xexpression

Returns:

an xfunction

template<class E1, class E2, class E3, typename T>
inline auto interp(const E1 &x, const E2 &xp, const E3 &fp, T left, T right)

Returns the one-dimensional piecewise linear interpolant to a function with given discrete data points (xp, fp), evaluated at x.

Parameters:
  • x – The x-coordinates at which to evaluate the interpolated values (sorted).

  • xp – The x-coordinates of the data points (sorted).

  • fp – The y-coordinates of the data points, same length as xp.

  • left – Value to return for x < xp[0].

  • right – Value to return for x > xp[-1]

Returns:

an one-dimensional xarray, same length as x.

template<class E1, class E2 = xt::placeholders::xtuph, class E3 = double>
inline auto unwrap(E1 &&p, E2 discontinuity = xnone(), std::ptrdiff_t axis = -1, E3 period = 2.0 * xt::numeric_constants<double>::PI)

Unwrap by taking the complement of large deltas with respect to the period.

https://numpy.org/doc/stable/reference/generated/numpy.unwrap.html

Parameters:
  • p – Input array.

  • discontinuity – Maximum discontinuity between values, default is period / 2. Values below period / 2 are treated as if they were period / 2. To have an effect different from the default, use discontinuity > period / 2.

  • axis – Axis along which unwrap will operate, default: the last axis.

  • period – Size of the range over which the input wraps. Default: \( 2 \pi \).

template<class E1, class E2, class E3>
inline auto interp(const E1 &x, const E2 &xp, const E3 &fp)

Returns the one-dimensional piecewise linear interpolant to a function with given discrete data points (xp, fp), evaluated at x.

Parameters:
  • x – The x-coordinates at which to evaluate the interpolated values (sorted).

  • xp – The x-coordinates of the data points (sorted).

  • fp – The y-coordinates of the data points, same length as xp.

Returns:

an one-dimensional xarray, same length as x.