xp_float_to_complex

xp_float_to_complex(arr: Any, xp: ModuleType | None = None) Any[source][source]

Convert a floating-point array to a complex array.

Parameters:
  • arr (Array) – The input array with floating-point data type.

  • xp (module, optional) –

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

    inferred from the array.

Returns:

The input array converted to a complex data type. float32 is converted
to complex64, and float64 (and other real floating types) are

converted to complex128.

Return type:

Array

Notes

This function only converts arrays with floating-point data types. If the

input array already has a complex data type, it is returned unchanged.

Examples

>>> import numpy as np
>>> x = np.array([1.0, 2.0, 3.0], dtype=np.float32)
>>> y = xp_float_to_complex(x, xp=np)
>>> y.dtype
dtype('complex64')
>>> z = np.array([4.0, 5.0, 6.0], dtype=np.float64)
>>> w = xp_float_to_complex(z, xp=np)
>>> w.dtype
dtype('complex128')
>>> # Complex input is returned unchanged
>>> c = np.array([1+2j, 3+4j])
>>> xp_float_to_complex(c, xp=np) is c
True