xp_ravel

xp_ravel(x: Any, /, *, xp: ModuleType | None = None) Any[source][source]

Return a flattened array.

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

  • xp (module, optional) –

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

    inferred from the array.

Returns:

A 1-D array containing the elements of the input array.

Return type:

Array

Notes

This function is equivalent to np.ravel written in terms of the array API. Even though it’s one line, it comes up so often that it’s worth having this function for readability.

Examples

>>> import numpy as np
>>> x = np.array([[1, 2], [3, 4]])
>>> xp_ravel(x, xp=np)
array([1, 2, 3, 4])
>>> # Equivalent to NumPy's ravel
>>> np.ravel(x)
array([1, 2, 3, 4])