def execute(self, interp): w_lhs = self.lhs.execute(interp) if isinstance(self.rhs, SliceConstant): w_rhs = self.rhs.wrap(interp.space) else: w_rhs = self.rhs.execute(interp) if not isinstance(w_lhs, W_NDimArray): # scalar dtype = get_dtype_cache(interp.space).w_float64dtype w_lhs = W_NDimArray.new_scalar(interp.space, dtype, w_lhs) assert isinstance(w_lhs, W_NDimArray) if self.name == '+': w_res = w_lhs.descr_add(interp.space, w_rhs) elif self.name == '*': w_res = w_lhs.descr_mul(interp.space, w_rhs) elif self.name == '-': w_res = w_lhs.descr_sub(interp.space, w_rhs) elif self.name == '**': w_res = w_lhs.descr_pow(interp.space, w_rhs) elif self.name == '->': if isinstance(w_rhs, FloatObject): w_rhs = IntObject(int(w_rhs.floatval)) assert isinstance(w_lhs, W_NDimArray) w_res = w_lhs.descr_getitem(interp.space, w_rhs) else: raise NotImplementedError if (not isinstance(w_res, W_NDimArray) and not isinstance(w_res, boxes.W_GenericBox)): dtype = get_dtype_cache(interp.space).w_float64dtype w_res = W_NDimArray.new_scalar(interp.space, dtype, w_res) return w_res
def _PyArray_FromAny(space, w_obj, w_dtype, min_depth, max_depth, requirements, context): """ This is the main function used to obtain an array from any nested sequence, or object that exposes the array interface, op. The parameters allow specification of the required dtype, the minimum (min_depth) and maximum (max_depth) number of dimensions acceptable, and other requirements for the array. The dtype argument needs to be a PyArray_Descr structure indicating the desired data-type (including required byteorder). The dtype argument may be NULL, indicating that any data-type (and byteorder) is acceptable. Unless FORCECAST is present in flags, this call will generate an error if the data type cannot be safely obtained from the object. If you want to use NULL for the dtype and ensure the array is notswapped then use PyArray_CheckFromAny. A value of 0 for either of the depth parameters causes the parameter to be ignored. Any of the following array flags can be added (e.g. using |) to get the requirements argument. If your code can handle general (e.g. strided, byte-swapped, or unaligned arrays) then requirements may be 0. Also, if op is not already an array (or does not expose the array interface), then a new array will be created (and filled from op using the sequence protocol). The new array will have NPY_DEFAULT as its flags member. The context argument is passed to the __array__ method of op and is only used if the array is constructed that way. Almost always this parameter is NULL. """ if requirements not in (0, NPY_DEFAULT): raise OperationError( space.w_NotImplementedError, space.wrap( '_PyArray_FromAny called with not-implemented requirements argument' )) w_array = array(space, w_obj, w_dtype=w_dtype, copy=False) if min_depth != 0 and len(w_array.get_shape()) < min_depth: raise OperationError( space.w_ValueError, space.wrap('object of too small depth for desired array')) elif max_depth != 0 and len(w_array.get_shape()) > max_depth: raise OperationError( space.w_ValueError, space.wrap('object of too deep for desired array')) elif w_array.is_scalar(): # since PyArray_DATA() fails on scalars, create a 1D array and set empty # shape. So the following combination works for *reading* scalars: # PyObject *arr = PyArray_FromAny(obj); # int nd = PyArray_NDIM(arr); # void *data = PyArray_DATA(arr); impl = w_array.implementation w_array = W_NDimArray.from_shape(space, [1], impl.dtype) w_array.implementation.setitem(0, impl.getitem(impl.start + 0)) w_array.implementation.shape = [] return w_array
def simple_new(space, nd, dims, typenum, order=CORDER, owning=False, w_subtype=None): shape, dtype = get_shape_and_dtype(space, nd, dims, typenum) return W_NDimArray.from_shape(space, shape, dtype)
def _PyArray_FromAny(space, w_obj, w_dtype, min_depth, max_depth, requirements, context): """ This is the main function used to obtain an array from any nested sequence, or object that exposes the array interface, op. The parameters allow specification of the required dtype, the minimum (min_depth) and maximum (max_depth) number of dimensions acceptable, and other requirements for the array. The dtype argument needs to be a PyArray_Descr structure indicating the desired data-type (including required byteorder). The dtype argument may be NULL, indicating that any data-type (and byteorder) is acceptable. Unless FORCECAST is present in flags, this call will generate an error if the data type cannot be safely obtained from the object. If you want to use NULL for the dtype and ensure the array is notswapped then use PyArray_CheckFromAny. A value of 0 for either of the depth parameters causes the parameter to be ignored. Any of the following array flags can be added (e.g. using |) to get the requirements argument. If your code can handle general (e.g. strided, byte-swapped, or unaligned arrays) then requirements may be 0. Also, if op is not already an array (or does not expose the array interface), then a new array will be created (and filled from op using the sequence protocol). The new array will have NPY_DEFAULT as its flags member. The context argument is passed to the __array__ method of op and is only used if the array is constructed that way. Almost always this parameter is NULL. """ if requirements not in (0, NPY_DEFAULT): raise OperationError(space.w_NotImplementedError, space.wrap( '_PyArray_FromAny called with not-implemented requirements argument')) w_array = array(space, w_obj, w_dtype=w_dtype, copy=False) if min_depth !=0 and len(w_array.get_shape()) < min_depth: raise OperationError(space.w_ValueError, space.wrap( 'object of too small depth for desired array')) elif max_depth !=0 and len(w_array.get_shape()) > max_depth: raise OperationError(space.w_ValueError, space.wrap( 'object of too deep for desired array')) elif w_array.is_scalar(): # since PyArray_DATA() fails on scalars, create a 1D array and set empty # shape. So the following combination works for *reading* scalars: # PyObject *arr = PyArray_FromAny(obj); # int nd = PyArray_NDIM(arr); # void *data = PyArray_DATA(arr); impl = w_array.implementation w_array = W_NDimArray.from_shape(space, [1], impl.dtype) w_array.implementation.setitem(0, impl.getitem(impl.start + 0)) w_array.implementation.shape = [] return w_array
def simple_new_from_data(space, nd, dims, typenum, data, order=CORDER, owning=False, w_subtype=None): shape, dtype = get_shape_and_dtype(space, nd, dims, typenum) storage = rffi.cast(RAW_STORAGE_PTR, data) return W_NDimArray.from_shape_and_storage(space, shape, storage, dtype, order=order, owning=owning, w_subtype=w_subtype)
def scalar(space): dtype = get_dtype_cache(space).w_float64dtype return W_NDimArray.new_scalar(space, dtype, space.wrap(10.))
def iarray(space, shape, order=NPY.CORDER): dtype = get_dtype_cache(space).w_int64dtype return W_NDimArray.from_shape(space, shape, dtype, order=order)
def simple_new_from_data(space, nd, dims, typenum, data, order='C', owning=False, w_subtype=None): shape, dtype = get_shape_and_dtype(space, nd, dims, typenum) storage = rffi.cast(RAW_STORAGE_PTR, data) return W_NDimArray.from_shape_and_storage(space, shape, storage, dtype, order=order, owning=owning, w_subtype=w_subtype)
def simple_new(space, nd, dims, typenum, order='C', owning=False, w_subtype=None): shape, dtype = get_shape_and_dtype(space, nd, dims, typenum) return W_NDimArray.from_shape(space, shape, dtype)
def execute(self, interp): arr = self.args[0].execute(interp) if not isinstance(arr, W_NDimArray): raise ArgumentNotAnArray if self.name in SINGLE_ARG_FUNCTIONS: if len(self.args) != 1 and self.name != 'sum': raise ArgumentMismatch if self.name == "sum": if len(self.args) > 1: var = self.args[1] if isinstance(var, DtypeClass): w_res = arr.descr_sum(interp.space, None, var.execute(interp)) else: w_res = arr.descr_sum(interp.space, self.args[1].execute(interp)) else: w_res = arr.descr_sum(interp.space) elif self.name == "prod": w_res = arr.descr_prod(interp.space) elif self.name == "max": w_res = arr.descr_max(interp.space) elif self.name == "min": w_res = arr.descr_min(interp.space) elif self.name == "any": w_res = arr.descr_any(interp.space) elif self.name == "all": w_res = arr.descr_all(interp.space) elif self.name == "cumsum": w_res = arr.descr_cumsum(interp.space) elif self.name == "logical_xor_reduce": logical_xor = ufuncs.get(interp.space).logical_xor w_res = logical_xor.reduce(interp.space, arr, None) elif self.name == "unegative": neg = ufuncs.get(interp.space).negative w_res = neg.call(interp.space, [arr], None, 'unsafe', None) elif self.name == "cos": cos = ufuncs.get(interp.space).cos w_res = cos.call(interp.space, [arr], None, 'unsafe', None) elif self.name == "flat": w_res = arr.descr_get_flatiter(interp.space) elif self.name == "argsort": w_res = arr.descr_argsort(interp.space) elif self.name == "tostring": arr.descr_tostring(interp.space) w_res = None else: assert False # unreachable code elif self.name in TWO_ARG_FUNCTIONS: if len(self.args) != 2: raise ArgumentMismatch arg = self.args[1].execute(interp) if not isinstance(arg, W_NDimArray): raise ArgumentNotAnArray if self.name == "dot": w_res = arr.descr_dot(interp.space, arg) elif self.name == 'multiply': w_res = arr.descr_mul(interp.space, arg) elif self.name == 'take': w_res = arr.descr_take(interp.space, arg) elif self.name == "searchsorted": w_res = arr.descr_searchsorted(interp.space, arg, interp.space.newtext('left')) else: assert False # unreachable code elif self.name in THREE_ARG_FUNCTIONS: if len(self.args) != 3: raise ArgumentMismatch arg1 = self.args[1].execute(interp) arg2 = self.args[2].execute(interp) if not isinstance(arg1, W_NDimArray): raise ArgumentNotAnArray if not isinstance(arg2, W_NDimArray): raise ArgumentNotAnArray if self.name == "where": w_res = where(interp.space, arr, arg1, arg2) else: assert False # unreachable code elif self.name in TWO_ARG_FUNCTIONS_OR_NONE: if len(self.args) != 2: raise ArgumentMismatch arg = self.args[1].execute(interp) if self.name == 'view': w_res = arr.descr_view(interp.space, arg) elif self.name == 'astype': w_res = arr.descr_astype(interp.space, arg) elif self.name == 'reshape': w_arg = self.args[1] assert isinstance(w_arg, ArrayConstant) order = -1 w_res = arr.reshape(interp.space, w_arg.wrap(interp.space), order) else: assert False else: raise WrongFunctionName if isinstance(w_res, W_NDimArray): return w_res if isinstance(w_res, FloatObject): dtype = get_dtype_cache(interp.space).w_float64dtype elif isinstance(w_res, IntObject): dtype = get_dtype_cache(interp.space).w_int64dtype elif isinstance(w_res, BoolObject): dtype = get_dtype_cache(interp.space).w_booldtype elif isinstance(w_res, boxes.W_GenericBox): dtype = w_res.get_dtype(interp.space) else: dtype = None return W_NDimArray.new_scalar(interp.space, dtype, w_res)
def array(space, shape, order='C'): dtype = get_dtype_cache(space).w_float64dtype return W_NDimArray.from_shape(space, shape, dtype, order=order)
def execute(self, interp): arr = self.args[0].execute(interp) if not isinstance(arr, W_NDimArray): raise ArgumentNotAnArray if self.name in SINGLE_ARG_FUNCTIONS: if len(self.args) != 1 and self.name != 'sum': raise ArgumentMismatch if self.name == "sum": if len(self.args)>1: w_res = arr.descr_sum(interp.space, self.args[1].execute(interp)) else: w_res = arr.descr_sum(interp.space) elif self.name == "prod": w_res = arr.descr_prod(interp.space) elif self.name == "max": w_res = arr.descr_max(interp.space) elif self.name == "min": w_res = arr.descr_min(interp.space) elif self.name == "any": w_res = arr.descr_any(interp.space) elif self.name == "all": w_res = arr.descr_all(interp.space) elif self.name == "cumsum": w_res = arr.descr_cumsum(interp.space) elif self.name == "logical_xor_reduce": logical_xor = ufuncs.get(interp.space).logical_xor w_res = logical_xor.reduce(interp.space, arr, None) elif self.name == "unegative": neg = ufuncs.get(interp.space).negative w_res = neg.call(interp.space, [arr], None, None, None) elif self.name == "cos": cos = ufuncs.get(interp.space).cos w_res = cos.call(interp.space, [arr], None, None, None) elif self.name == "flat": w_res = arr.descr_get_flatiter(interp.space) elif self.name == "argsort": w_res = arr.descr_argsort(interp.space) elif self.name == "tostring": arr.descr_tostring(interp.space) w_res = None else: assert False # unreachable code elif self.name in TWO_ARG_FUNCTIONS: if len(self.args) != 2: raise ArgumentMismatch arg = self.args[1].execute(interp) if not isinstance(arg, W_NDimArray): raise ArgumentNotAnArray if self.name == "dot": w_res = arr.descr_dot(interp.space, arg) elif self.name == 'take': w_res = arr.descr_take(interp.space, arg) elif self.name == "searchsorted": w_res = arr.descr_searchsorted(interp.space, arg, interp.space.wrap('left')) else: assert False # unreachable code elif self.name in THREE_ARG_FUNCTIONS: if len(self.args) != 3: raise ArgumentMismatch arg1 = self.args[1].execute(interp) arg2 = self.args[2].execute(interp) if not isinstance(arg1, W_NDimArray): raise ArgumentNotAnArray if not isinstance(arg2, W_NDimArray): raise ArgumentNotAnArray if self.name == "where": w_res = where(interp.space, arr, arg1, arg2) else: assert False elif self.name in TWO_ARG_FUNCTIONS_OR_NONE: if len(self.args) != 2: raise ArgumentMismatch arg = self.args[1].execute(interp) if self.name == 'view': w_res = arr.descr_view(interp.space, arg) elif self.name == 'astype': w_res = arr.descr_astype(interp.space, arg) else: assert False else: raise WrongFunctionName if isinstance(w_res, W_NDimArray): return w_res if isinstance(w_res, FloatObject): dtype = get_dtype_cache(interp.space).w_float64dtype elif isinstance(w_res, IntObject): dtype = get_dtype_cache(interp.space).w_int64dtype elif isinstance(w_res, BoolObject): dtype = get_dtype_cache(interp.space).w_booldtype elif isinstance(w_res, boxes.W_GenericBox): dtype = w_res.get_dtype(interp.space) else: dtype = None return W_NDimArray.new_scalar(interp.space, dtype, w_res)