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 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)