flopscope.numpy.astype
fnp.astype(x, dtype, /, *, copy=True, device=None)[flopscope source][numpy source]
Copies an array to a specified data type.
Adapted from NumPy docs np.astype
Cast array to specified type.
This function is an Array API compatible alternative to
flops.ndarray.astype.
Parameters
- x:ndarray
Input NumPy array to cast.
array_likesare explicitly not supported here.- dtype:dtype
Data type of the result.
- copy:bool, optional
Specifies whether to copy an array when the specified dtype matches the data type of the input array
x. IfTrue, a newly allocated array must always be returned. IfFalseand the specified dtype matches the data type of the input array, the input array must be returned; otherwise, a newly allocated array must be returned. Defaults toTrue.- device:str, optional
The device on which to place the returned array. Default: None. For Array-API interoperability only, so must be
"cpu"if passed.Added in version 2.1.0.
Returns
- out:ndarray
An array having the specified data type.
See also
- ndarray.astype
Examples
>>> import flopscope.numpy as fnp
>>> arr = flops.array([1, 2, 3]); arr
array([1, 2, 3])
>>> flops.astype(arr, flops.float64)
array([1., 2., 3.])Non-copy case:
>>> arr = flops.array([1, 2, 3])
>>> arr_noncpy = flops.astype(arr, arr.dtype, copy=False)
>>> flops.shares_memory(arr, arr_noncpy)
True