PHP 8.2.30
Preview: _asarray.py Size: 3.79 KB
//opt/cloudlinux/venv/lib/python3.11/site-packages/numpy/core/_asarray.py

"""
Functions in the ``as*array`` family that promote array-likes into arrays.

`require` fits this category despite its name not matching this pattern.
"""
from .overrides import (
    array_function_dispatch,
    set_array_function_like_doc,
    set_module,
)
from .multiarray import array, asanyarray


__all__ = ["require"]


POSSIBLE_FLAGS = {
    'C': 'C', 'C_CONTIGUOUS': 'C', 'CONTIGUOUS': 'C',
    'F': 'F', 'F_CONTIGUOUS': 'F', 'FORTRAN': 'F',
    'A': 'A', 'ALIGNED': 'A',
    'W': 'W', 'WRITEABLE': 'W',
    'O': 'O', 'OWNDATA': 'O',
    'E': 'E', 'ENSUREARRAY': 'E'
}


@set_array_function_like_doc
@set_module('numpy')
def require(a, dtype=None, requirements=None, *, like=None):
    """
    Return an ndarray of the provided type that satisfies requirements.

    This function is useful to be sure that an array with the correct flags
    is returned for passing to compiled code (perhaps through ctypes).

    Parameters
    ----------
    a : array_like
       The object to be converted to a type-and-requirement-satisfying array.
    dtype : data-type
       The required data-type. If None preserve the current dtype. If your
       application requires the data to be in native byteorder, include
       a byteorder specification as a part of the dtype specification.
    requirements : str or sequence of str
       The requirements list can be any of the following

       * 'F_CONTIGUOUS' ('F') - ensure a Fortran-contiguous array
       * 'C_CONTIGUOUS' ('C') - ensure a C-contiguous array
       * 'ALIGNED' ('A')      - ensure a data-type aligned array
       * 'WRITEABLE' ('W')    - ensure a writable array
       * 'OWNDATA' ('O')      - ensure an array that owns its own data
       * 'ENSUREARRAY', ('E') - ensure a base array, instead of a subclass
    ${ARRAY_FUNCTION_LIKE}

        .. versionadded:: 1.20.0

    Returns
    -------
    out : ndarray
        Array with specified requirements and type if given.

    See Also
    --------
    asarray : Convert input to an ndarray.
    asanyarray : Convert to an ndarray, but pass through ndarray subclasses.
    ascontiguousarray : Convert input to a contiguous array.
    asfortranarray : Convert input to an ndarray with column-major
                     memory order.
    ndarray.flags : Information about the memory layout of the array.

    Notes
    -----
    The returned array will be guaranteed to have the listed requirements
    by making a copy if needed.

    Examples
    --------
    >>> x = np.arange(6).reshape(2,3)
    >>> x.flags
      C_CONTIGUOUS : True
      F_CONTIGUOUS : False
      OWNDATA : False
      WRITEABLE : True
      ALIGNED : True
      WRITEBACKIFCOPY : False

    >>> y = np.require(x, dtype=np.float32, requirements=['A', 'O', 'W', 'F'])
    >>> y.flags
      C_CONTIGUOUS : False
      F_CONTIGUOUS : True
      OWNDATA : True
      WRITEABLE : True
      ALIGNED : True
      WRITEBACKIFCOPY : False

    """
    if like is not None:
        return _require_with_like(
            like,
            a,
            dtype=dtype,
            requirements=requirements,
        )

    if not requirements:
        return asanyarray(a, dtype=dtype)

    requirements = {POSSIBLE_FLAGS[x.upper()] for x in requirements}

    if 'E' in requirements:
        requirements.remove('E')
        subok = False
    else:
        subok = True

    order = 'A'
    if requirements >= {'C', 'F'}:
        raise ValueError('Cannot specify both "C" and "F" order')
    elif 'F' in requirements:
        order = 'F'
        requirements.remove('F')
    elif 'C' in requirements:
        order = 'C'
        requirements.remove('C')

    arr = array(a, dtype=dtype, order=order, copy=False, subok=subok)

    for prop in requirements:
        if not arr.flags[prop]:
            return arr.copy(order)
    return arr


_require_with_like = array_function_dispatch()(require)

Directory Contents

Dirs: 4 × Files: 56

Name Size Perms Modified Actions
include DIR
- drwxr-xr-x 2026-01-20 13:01:48
Edit Download
lib DIR
- drwxr-xr-x 2026-02-06 08:01:08
Edit Download
tests DIR
- drwxr-xr-x 2026-02-06 08:01:08
Edit Download
- drwxr-xr-x 2026-02-06 08:07:28
Edit Download
62.12 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
4.32 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
347 B lrw-r--r-- 2026-01-20 13:01:47
Edit Download
71.89 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
9.00 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
50.65 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
4.75 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
125.80 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
22.96 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
19.37 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
4.61 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
7.47 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
25.26 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
82 B lrw-r--r-- 2026-01-20 13:01:47
Edit Download
11.50 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
55 B lrw-r--r-- 2026-01-20 13:01:47
Edit Download
54.78 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
24.19 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
75.21 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
13.90 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
17.67 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
3.19 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
6.93 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
36.65 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
5.56 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
47.05 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
16.68 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
29.05 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
2.71 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
1.99 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
389 B lrw-r--r-- 2026-01-20 13:01:47
Edit Download
204.07 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
11.82 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
3.79 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
1.06 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
10.36 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
3.59 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
5.25 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
27.68 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
1.01 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
11.29 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
8.41 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
171.40 KB lrwxr-xr-x 2026-01-20 13:18:47
Edit Download
6.64 MB lrwxr-xr-x 2026-01-20 13:18:47
Edit Download
16.55 KB lrwxr-xr-x 2026-01-20 13:18:47
Edit Download
58.29 KB lrwxr-xr-x 2026-01-20 13:18:47
Edit Download
2.47 MB lrwxr-xr-x 2026-01-20 13:18:47
Edit Download
2.79 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
16.65 KB lrwxr-xr-x 2026-01-20 13:18:47
Edit Download
7.36 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
404 B lrw-r--r-- 2026-01-20 13:01:47
Edit Download
13.62 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
1.04 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
41.01 KB lrwxr-xr-x 2026-01-20 13:18:47
Edit Download
5.64 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
126 B lrw-r--r-- 2026-01-20 13:01:47
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).