xp_real

xp_real(x: Any, /, *, xp: ModuleType | None = None) Any[source][source]
Return the real part of a complex array or the array itself if it’s

not complex.

Parameters:
  • x (Array) – The input array.

  • xp (module, optional) –

    The array API namespace to use. If not provided, the namespace is

    inferred from the array.

Returns:

The real part of the input array if it has a complex data type,

otherwise the input array.

Return type:

Array

Notes

This is a convenience wrapper of xp.real that allows non-complex input; see data-apis/array-api#824.

Examples

>>> import numpy as np
>>> x = np.array([1+2j, 3+4j])
>>> xp_real(x, xp=np)
array([1., 3.])
>>> y = np.array([1, 2, 3])
>>> xp_real(y, xp=np)  # Non-complex input is returned as is
array([1, 2, 3])