def __arange_3(start, stop, step=1, dtype=None): _type = type(stop) start = _F.scalar(_type(start), dtype) limit = _F.scalar(stop, dtype) delta = _F.scalar(_type(step), dtype) x = _F.range(start, limit, delta) if dtype is not None and x.dtype != dtype: x = _F.cast(x, dtype) return x
def array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0, like=None): __order_assert(order) if isinstance(object, _F.Var): if copy: x = _Copy(object) else: x = object else: if not isinstance(object, _F._Sequence): x = _F.scalar(object, dtype) else: # get shape and dtype of sequence dst_shape, item_type = _F._list_shape_type(object) x = _F.const(object, dst_shape, dtype=item_type) # if give dtype, cast to dtype if dtype is not None and dtype != x.dtype: x = _F.cast(x, dtype) # if give ndmin, unsqueeze if ndmin > 0: dim = x.ndim if ndmin > dim: x = _F.unsqueeze(x, [i for i in range(ndmin - dim)]) return x
def full(shape, fill_value, dtype=None, order='C'): ''' full(shape, fill_value, dtype=None, order='C') Return a new var of given shape and type, filled with `fill_value`. Parameters ---------- shape : int or sequence of ints Shape of the new var, e.g., ``(2, 3)`` or ``2``. fill_value : scalar or var_like Fill value. dtype : data-type, optional The desired data-type for the var The default, None, means ``np.array(fill_value).dtype``. order : {'C', 'F'}, optional Compatible with numpy. Returns ------- out : var Var of `fill_value` with the given shape, dtype, and order. Examples -------- >>> np.full((2, 2), 10) var([[10, 10], [10, 10]]) ''' __order_assert(order) shape = __get_shape(shape) return _F.fill(_F._to_var(shape), _F.scalar(fill_value, dtype))
def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0): pow = linspace(start, stop, num, endpoint, False, dtype, axis) return _F.pow(_F.scalar(base, dtype), pow)
def signbit(x): x = array(x) return (sign(x) == _F.scalar(-1, x.dtype))
def sinc(x): x = array(x) _pi = _F.scalar(pi, x.dtype) return sin(_pi * x) / (_pi * x)
def log10(x): x = array(x) return log(x) / log(_F.scalar(10, x.dtype))
def log2(x): x = array(x) return log(x) / log(_F.scalar(2, x.dtype))
def exp2(x): x = array(x) return _F.scalar(2, x.dtype)**x
def full(shape, fill_value, dtype=None, order='C'): __order_assert(order) shape = __get_shape(shape) return _F.fill(_F._to_var(shape), _F.scalar(fill_value, dtype))
def count_nonzero(a, axis=None, keepdims=False): mask = not_equal(a, _F.scalar(0, a.dtype)) axis = _F._to_axis(axis) return sum(mask, axis, keepdims)
def argwhere(a): mask = not_equal(a, _F.scalar(0, a.dtype)) return _F.where(mask)
def cbrt(x): x = array(x) return power(x, _F.scalar(1. / 3, x.dtype))
def modf(x): x = array(x) out1, out2 = divmod(x, _F.scalar(1, x.dtype)) return (out2, out1)
def array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0, like=None): ''' array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0, like=None) Create an array. Parameters ---------- object : var_like An var, any object exposing the var interface, an object whose __array__ method returns an array, or any (nested) sequence. If object is a scalar, a 0-dimensional array containing object is returned. dtype : data-type, optional The desired data-type for the array. If not given, then the type will be determined as the minimum type required to hold the objects in the sequence. copy : bool, optional If true (default), then the object is copied. Otherwise, a copy will only be made if __array__ returns a copy, if obj is a nested sequence, or if a copy is needed to satisfy any of the other requirements (dtype, order, etc.). order : {'C', 'F', 'A', 'K'}, optional Compatible with numpy. subok : bool, optional Compatible with numpy. ndmin : int, optional Specifies the minimum number of dimensions that the resulting array should have. Ones will be pre-pended to the shape as needed to meet this requirement. like : var_like Compatible with numpy. Returns ------- out : var An var object satisfying the specified requirements. Examples -------- >>> np.array([1, 2, 3]) var([1, 2, 3]) >>> np.array([[1, 2], [3, 4]]) var([[1, 2], [3, 4]]) ''' __order_assert(order) if isinstance(object, _F.Var): if copy: x = _Copy(object) else: x = object else: if not isinstance(object, _F._Sequence): x = _F.scalar(object, dtype) else: # get shape and dtype of sequence dst_shape, item_type = _F._list_shape_type(object) x = _F.const(object, dst_shape, dtype=item_type) # if give dtype, cast to dtype if dtype is not None and dtype != x.dtype: x = _F.cast(x, dtype) # if give ndmin, unsqueeze if ndmin > 0: dim = x.ndim if ndmin > dim: x = _F.unsqueeze(x, [i for i in range(ndmin - dim)]) return x