Exemplo n.º 1
0
 def do_axis_reduce(self, obj, dtype, dim, keepdims):
     from pypy.module.micronumpy.interp_numarray import AxisReduce,\
          W_NDimArray
     if keepdims:
         shape = obj.shape[:dim] + [1] + obj.shape[dim + 1:]
     else:
         shape = obj.shape[:dim] + obj.shape[dim + 1:]
     result = W_NDimArray(support.product(shape), shape, dtype)
     arr = AxisReduce(self.func, self.name, self.identity, obj.shape, dtype,
                      result, obj, dim)
     loop.compute(arr)
     return arr.left
Exemplo n.º 2
0
 def reduce(self, space, w_obj, multidim, promote_to_largest, dim,
            keepdims=False):
     from pypy.module.micronumpy.interp_numarray import convert_to_array, \
                                                        Scalar, ReduceArray
     if self.argcount != 2:
         raise OperationError(space.w_ValueError, space.wrap("reduce only "
             "supported for binary functions"))
     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
     if self.comparison_func:
         dtype = interp_dtype.get_dtype_cache(space).w_booldtype
     else:
         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:
         return self.do_axis_reduce(obj, dtype, dim, keepdims)
     arr = ReduceArray(self.func, self.name, self.identity, obj, dtype)
     return loop.compute(arr)
Exemplo n.º 3
0
 def do_axis_reduce(self, obj, dtype, axis, result):
     from pypy.module.micronumpy.interp_numarray import AxisReduce
     arr = AxisReduce(self.func, self.name, self.identity, obj.shape, dtype,
                      result, obj, axis)
     loop.compute(arr)
     return arr.left
Exemplo n.º 4
0
 def reduce(self, space, w_obj, multidim, promote_to_largest, axis,
            keepdims=False, out=None):
     from pypy.module.micronumpy.interp_numarray import convert_to_array, \
                                          Scalar, ReduceArray, W_NDimArray
     if self.argcount != 2:
         raise OperationError(space.w_ValueError, space.wrap("reduce only "
             "supported for binary functions"))
     assert isinstance(self, W_Ufunc2)
     obj = convert_to_array(space, w_obj)
     if axis >= len(obj.shape):
         raise OperationError(space.w_ValueError, space.wrap("axis(=%d) out of bounds" % axis))
     if isinstance(obj, Scalar):
         raise OperationError(space.w_TypeError, space.wrap("cannot reduce "
             "on a scalar"))
     size = obj.size
     if self.comparison_func:
         dtype = interp_dtype.get_dtype_cache(space).w_booldtype
     else:
         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 axis >= 0:
         if keepdims:
             shape = obj.shape[:axis] + [1] + obj.shape[axis + 1:]
         else:
             shape = obj.shape[:axis] + obj.shape[axis + 1:]
         if out:
             #Test for shape agreement
             if len(out.shape) > len(shape):
                 raise operationerrfmt(space.w_ValueError,
                     'output parameter for reduction operation %s' +
                     ' has too many dimensions', self.name)
             elif len(out.shape) < len(shape):
                 raise operationerrfmt(space.w_ValueError,
                     'output parameter for reduction operation %s' +
                     ' does not have enough dimensions', self.name)
             elif out.shape != shape:
                 raise operationerrfmt(space.w_ValueError,
                     'output parameter shape mismatch, expecting [%s]' +
                     ' , got [%s]',
                     ",".join([str(x) for x in shape]),
                     ",".join([str(x) for x in out.shape]),
                     )
             #Test for dtype agreement, perhaps create an itermediate
             #if out.dtype != dtype:
             #    raise OperationError(space.w_TypeError, space.wrap(
             #        "mismatched  dtypes"))
             return self.do_axis_reduce(obj, out.find_dtype(), axis, out)
         else:
             result = W_NDimArray(shape, dtype)
             return self.do_axis_reduce(obj, dtype, axis, result)
     if out:
         if len(out.shape)>0:
             raise operationerrfmt(space.w_ValueError, "output parameter "
                           "for reduction operation %s has too many"
                           " dimensions",self.name)
         arr = ReduceArray(self.func, self.name, self.identity, obj,
                                                         out.find_dtype())
         val = loop.compute(arr)
         assert isinstance(out, Scalar)
         out.value = val
     else:
         arr = ReduceArray(self.func, self.name, self.identity, obj, dtype)
         val = loop.compute(arr)
     return val
Exemplo n.º 5
0
 def do_axis_reduce(self, obj, dtype, axis, result):
     from pypy.module.micronumpy.interp_numarray import AxisReduce
     arr = AxisReduce(self.func, self.name, self.identity, obj.shape, dtype,
                      result, obj, axis)
     loop.compute(arr)
     return arr.left
Exemplo n.º 6
0
 def reduce(self, space, w_obj, multidim, promote_to_largest, axis,
            keepdims=False, out=None):
     from pypy.module.micronumpy.interp_numarray import convert_to_array, \
                                          Scalar, ReduceArray, W_NDimArray
     if self.argcount != 2:
         raise OperationError(space.w_ValueError, space.wrap("reduce only "
             "supported for binary functions"))
     assert isinstance(self, W_Ufunc2)
     obj = convert_to_array(space, w_obj)
     if axis >= len(obj.shape):
         raise OperationError(space.w_ValueError, space.wrap("axis(=%d) out of bounds" % axis))
     if isinstance(obj, Scalar):
         raise OperationError(space.w_TypeError, space.wrap("cannot reduce "
             "on a scalar"))
     size = obj.size
     if self.comparison_func:
         dtype = interp_dtype.get_dtype_cache(space).w_booldtype
     else:
         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 axis >= 0:
         if keepdims:
             shape = obj.shape[:axis] + [1] + obj.shape[axis + 1:]
         else:
             shape = obj.shape[:axis] + obj.shape[axis + 1:]
         if out:
             #Test for shape agreement
             if len(out.shape) > len(shape):
                 raise operationerrfmt(space.w_ValueError,
                     'output parameter for reduction operation %s' +
                     ' has too many dimensions', self.name)
             elif len(out.shape) < len(shape):
                 raise operationerrfmt(space.w_ValueError,
                     'output parameter for reduction operation %s' +
                     ' does not have enough dimensions', self.name)
             elif out.shape != shape:
                 raise operationerrfmt(space.w_ValueError,
                     'output parameter shape mismatch, expecting [%s]' +
                     ' , got [%s]',
                     ",".join([str(x) for x in shape]),
                     ",".join([str(x) for x in out.shape]),
                     )
             #Test for dtype agreement, perhaps create an itermediate
             #if out.dtype != dtype:
             #    raise OperationError(space.w_TypeError, space.wrap(
             #        "mismatched  dtypes"))
             return self.do_axis_reduce(obj, out.find_dtype(), axis, out)
         else:
             result = W_NDimArray(shape, dtype)
             return self.do_axis_reduce(obj, dtype, axis, result)
     if out:
         if len(out.shape)>0:
             raise operationerrfmt(space.w_ValueError, "output parameter "
                           "for reduction operation %s has too many"
                           " dimensions",self.name)
         arr = ReduceArray(self.func, self.name, self.identity, obj,
                                                         out.find_dtype())
         val = loop.compute(arr)
         assert isinstance(out, Scalar)
         out.value = val
     else:
         arr = ReduceArray(self.func, self.name, self.identity, obj, dtype)
         val = loop.compute(arr)
     return val