Esempio n. 1
0
    def do_axis_reduce(self, obj, dtype, dim):
        from pypy.module.micronumpy.interp_numarray import AxisReduce,\
             W_NDimArray

        shape = obj.shape[0:dim] + obj.shape[dim + 1:len(obj.shape)]
        size = 1
        for s in shape:
            size *= s
        result = W_NDimArray(size, shape, dtype)
        rightsig = obj.create_sig()
        # note - this is just a wrapper so signature can fetch
        #        both left and right, nothing more, especially
        #        this is not a true virtual array, because shapes
        #        don't quite match
        arr = AxisReduce(self.func, self.name, obj.shape, dtype, result, obj,
                         dim)
        scalarsig = ScalarSignature(dtype)
        sig = find_sig(
            AxisReduceSignature(self.func, self.name, dtype, scalarsig,
                                rightsig), arr)
        assert isinstance(sig, AxisReduceSignature)
        frame = sig.create_frame(arr)
        shapelen = len(obj.shape)
        if self.identity is not None:
            identity = self.identity.convert_to(dtype)
        else:
            identity = None
        self.reduce_axis_loop(frame, sig, shapelen, arr, identity)
        return result
Esempio n. 2
0
 def do_axis_reduce(self, obj, dtype, dim):
     from pypy.module.micronumpy.interp_numarray import AxisReduce,\
          W_NDimArray
     
     shape = obj.shape[0:dim] + obj.shape[dim + 1:len(obj.shape)]
     size = 1
     for s in shape:
         size *= s
     result = W_NDimArray(size, shape, dtype)
     rightsig = obj.create_sig()
     # note - this is just a wrapper so signature can fetch
     #        both left and right, nothing more, especially
     #        this is not a true virtual array, because shapes
     #        don't quite match
     arr = AxisReduce(self.func, self.name, obj.shape, dtype,
                      result, obj, dim)
     scalarsig = ScalarSignature(dtype)
     sig = find_sig(AxisReduceSignature(self.func, self.name, dtype,
                                        scalarsig, rightsig), arr)
     assert isinstance(sig, AxisReduceSignature)
     frame = sig.create_frame(arr)
     shapelen = len(obj.shape)
     if self.identity is not None:
         identity = self.identity.convert_to(dtype)
     else:
         identity = None
     self.reduce_axis_loop(frame, sig, shapelen, arr, identity)
     return result
Esempio n. 3
0
    def reduce(self, space, w_obj, multidim, promote_to_largest, w_dim):
        from pypy.module.micronumpy.interp_numarray import convert_to_array, \
                                                           Scalar
        if self.argcount != 2:
            raise OperationError(
                space.w_ValueError,
                space.wrap("reduce only "
                           "supported for binary functions"))
        dim = space.int_w(w_dim)
        assert isinstance(self, W_Ufunc2)
        obj = convert_to_array(space, w_obj)
        if dim >= len(obj.shape):
            raise OperationError(space.w_ValueError,
                                 space.wrap("axis(=%d) out of bounds" % dim))
        if isinstance(obj, Scalar):
            raise OperationError(space.w_TypeError,
                                 space.wrap("cannot reduce "
                                            "on a scalar"))

        size = obj.size
        dtype = find_unaryop_result_dtype(
            space,
            obj.find_dtype(),
            promote_to_float=self.promote_to_float,
            promote_to_largest=promote_to_largest,
            promote_bools=True)
        shapelen = len(obj.shape)
        if self.identity is None and size == 0:
            raise operationerrfmt(
                space.w_ValueError, "zero-size array to "
                "%s.reduce without identity", self.name)
        if shapelen > 1 and dim >= 0:
            res = self.do_axis_reduce(obj, dtype, dim)
            return space.wrap(res)
        scalarsig = ScalarSignature(dtype)
        sig = find_sig(
            ReduceSignature(self.func, self.name, dtype, scalarsig,
                            obj.create_sig()), obj)
        frame = sig.create_frame(obj)
        if self.identity is None:
            value = sig.eval(frame, obj).convert_to(dtype)
            frame.next(shapelen)
        else:
            value = self.identity.convert_to(dtype)
        return self.reduce_loop(shapelen, sig, frame, value, obj, dtype)
Esempio n. 4
0
    def reduce(self, space, w_obj, multidim, promote_to_largest, w_dim):
        from pypy.module.micronumpy.interp_numarray import convert_to_array, \
                                                           Scalar
        if self.argcount != 2:
            raise OperationError(space.w_ValueError, space.wrap("reduce only "
                "supported for binary functions"))
        dim = space.int_w(w_dim)
        assert isinstance(self, W_Ufunc2)
        obj = convert_to_array(space, w_obj)
        if dim >= len(obj.shape):
            raise OperationError(space.w_ValueError, space.wrap("axis(=%d) out of bounds" % dim))
        if isinstance(obj, Scalar):
            raise OperationError(space.w_TypeError, space.wrap("cannot reduce "
                "on a scalar"))

        size = obj.size
        dtype = find_unaryop_result_dtype(
            space, obj.find_dtype(),
            promote_to_float=self.promote_to_float,
            promote_to_largest=promote_to_largest,
            promote_bools=True
        )
        shapelen = len(obj.shape)
        if self.identity is None and size == 0:
            raise operationerrfmt(space.w_ValueError, "zero-size array to "
                    "%s.reduce without identity", self.name)
        if shapelen > 1 and dim >= 0:
            res = self.do_axis_reduce(obj, dtype, dim)
            return space.wrap(res)
        scalarsig = ScalarSignature(dtype)
        sig = find_sig(ReduceSignature(self.func, self.name, dtype,
                                       scalarsig,
                                       obj.create_sig()), obj)
        frame = sig.create_frame(obj)
        if self.identity is None:
            value = sig.eval(frame, obj).convert_to(dtype)
            frame.next(shapelen)
        else:
            value = self.identity.convert_to(dtype)
        return self.reduce_loop(shapelen, sig, frame, value, obj, dtype)
Esempio n. 5
0
 def find_sig(self, res_shape=None, arr=None):
     """ find a correct signature for the array
     """
     res_shape = res_shape or self.shape
     arr = arr or self
     return signature.find_sig(self.create_sig(), arr)
Esempio n. 6
0
 def find_sig(self, res_shape=None, arr=None):
     """ find a correct signature for the array
     """
     res_shape = res_shape or self.shape
     arr = arr or self
     return signature.find_sig(self.create_sig(), arr)