Beispiel #1
0
 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 == '->':
         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, interp_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
Beispiel #2
0
 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 == "unegative":
             neg = interp_ufuncs.get(interp.space).negative
             w_res = neg.call(interp.space, [arr])
         elif self.name == "cos":
             cos = interp_ufuncs.get(interp.space).cos
             w_res = cos.call(interp.space, [arr])
         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)
         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)
         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, interp_boxes.W_GenericBox):
         dtype = w_res.get_dtype(interp.space)
     else:
         dtype = None
     return W_NDimArray.new_scalar(interp.space, dtype, w_res)