flopscope.

flopscope.numpy.require

fnp.require(a, dtype=None, requirements=None, *, like=None)[flopscope source][numpy source]

Return an ndarray of the provided type that satisfies requirements.

Adapted from NumPy docs np.require

Areacore
Typefree
NumPy Refnp.require
Cost
0
Flopscope Context

Return array 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

like:array_like, optional

Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like supports the __array_function__ protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.

Added in version 1.20.0.

Returns

out:ndarray

Array with specified requirements and type if given.

See also

Notes

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

Examples

>>> import flopscope.numpy as fnp
>>> x = flops.arange(6).reshape(2,3)
>>> x.flags
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
>>> y = flops.require(x, dtype=flops.float32, requirements=['A', 'O', 'W', 'F'])
>>> y.flags
  C_CONTIGUOUS : False
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False