def full( shape: Union[int, Tuple[int, ...]], fill_value: Union[int, float], *, dtype: Optional[Dtype] = None, device: Optional[Device] = None, ) -> Array: """ Array API compatible wrapper for :py:func:`np.full <numpy.full>`. See its docstring for more information. """ from ._array_object import Array _check_valid_dtype(dtype) if device is not None and not isinstance(device, _Device): raise ValueError(f"Unsupported device {device!r}") if device is None: device = _Device() # current device if isinstance(fill_value, Array) and fill_value.ndim == 0: fill_value = fill_value._array prev_device = runtime.getDevice() try: runtime.setDevice(device.id) res = np.full(shape, fill_value, dtype=dtype) finally: runtime.setDevice(prev_device) if res.dtype not in _all_dtypes: # This will happen if the fill value is not something that NumPy # coerces to one of the acceptable dtypes. raise TypeError("Invalid input to full") return Array._new(res)
def empty( shape: Union[int, Tuple[int, ...]], *, dtype: Optional[Dtype] = None, device: Optional[Device] = None, ) -> Array: """ Array API compatible wrapper for :py:func:`np.empty <numpy.empty>`. See its docstring for more information. """ from ._array_object import Array _check_valid_dtype(dtype) if device is not None and not isinstance(device, _Device): raise ValueError(f"Unsupported device {device!r}") if device is None: device = _Device() # current device with device: return Array._new(np.empty(shape, dtype=dtype))
def zeros( shape: Union[int, Tuple[int, ...]], *, dtype: Optional[Dtype] = None, device: Optional[Device] = None, ) -> Array: """ Array API compatible wrapper for :py:func:`np.zeros <numpy.zeros>`. See its docstring for more information. """ from ._array_object import Array _check_valid_dtype(dtype) if device is not None and not isinstance(device, _Device): raise ValueError(f"Unsupported device {device!r}") if device is None: device = _Device() # current device prev_device = runtime.getDevice() try: runtime.setDevice(device.id) return Array._new(np.zeros(shape, dtype=dtype)) finally: runtime.setDevice(prev_device)
copy: Optional[bool] = None, ) -> Array: """ Array API compatible wrapper for :py:func:`np.asarray <numpy.asarray>`. See its docstring for more information. """ # _array_object imports in this file are inside the functions to avoid # circular imports from ._array_object import Array _check_valid_dtype(dtype) if device is not None and not isinstance(device, _Device): raise ValueError(f"Unsupported device {device!r}") if device is None: device = _Device() # current device if copy is False: # Note: copy=False is not yet implemented in np.asarray raise NotImplementedError("copy=False is not yet implemented") if isinstance(obj, Array): if dtype is not None and obj.dtype != dtype: copy = True if copy is True: prev_device = runtime.getDevice() try: runtime.setDevice(device.id) obj = Array._new(np.array(obj._array, copy=True, dtype=dtype)) finally: runtime.setDevice(prev_device) return obj if dtype is None and isinstance(obj, int) and (obj > 2**64