Example #1
0
 def call(self, space, args_w):
     from pypy.module.micronumpy.interp_numarray import (Call2,
         convert_to_array, Scalar, shape_agreement, BaseArray)
     if len(args_w) > 2:
         [w_lhs, w_rhs, w_out] = args_w
     else:
         [w_lhs, w_rhs] = args_w
         w_out = None
     w_lhs = convert_to_array(space, w_lhs)
     w_rhs = convert_to_array(space, w_rhs)
     if space.is_w(w_out, space.w_None) or w_out is None:
         out = None
         calc_dtype = find_binop_result_dtype(space,
             w_lhs.find_dtype(), w_rhs.find_dtype(),
             int_only=self.int_only,
             promote_to_float=self.promote_to_float,
             promote_bools=self.promote_bools,
         )
     elif not isinstance(w_out, BaseArray):
         raise OperationError(space.w_TypeError, space.wrap(
                 'output must be an array'))
     else:
         out = w_out
         calc_dtype = out.find_dtype()
     if self.comparison_func:
         res_dtype = interp_dtype.get_dtype_cache(space).w_booldtype
     else:
         res_dtype = calc_dtype
     if isinstance(w_lhs, Scalar) and isinstance(w_rhs, Scalar):
         arr = self.func(calc_dtype,
             w_lhs.value.convert_to(calc_dtype),
             w_rhs.value.convert_to(calc_dtype)
         )
         if isinstance(out,Scalar):
             out.value = arr
         elif isinstance(out, BaseArray):
             out.fill(space, arr)
         else:
             out = arr
         return space.wrap(out)
     new_shape = shape_agreement(space, w_lhs.shape, w_rhs.shape)
     # Test correctness of out.shape
     if out and out.shape != shape_agreement(space, new_shape, out.shape):
         raise operationerrfmt(space.w_ValueError,
             'output parameter shape mismatch, could not broadcast [%s]' +
             ' to [%s]',
             ",".join([str(x) for x in new_shape]),
             ",".join([str(x) for x in out.shape]),
             )
     w_res = Call2(self.func, self.name,
                   new_shape, calc_dtype,
                   res_dtype, w_lhs, w_rhs, out)
     w_lhs.add_invalidates(space, w_res)
     w_rhs.add_invalidates(space, w_res)
     if out:
         w_res.get_concrete()
     return w_res
Example #2
0
 def call(self, space, args_w):
     from pypy.module.micronumpy.interp_numarray import (Call2,
         convert_to_array, Scalar, shape_agreement, BaseArray)
     if len(args_w) > 2:
         [w_lhs, w_rhs, w_out] = args_w
     else:
         [w_lhs, w_rhs] = args_w
         w_out = None
     w_lhs = convert_to_array(space, w_lhs)
     w_rhs = convert_to_array(space, w_rhs)
     if space.is_w(w_out, space.w_None) or w_out is None:
         out = None
         calc_dtype = find_binop_result_dtype(space,
             w_lhs.find_dtype(), w_rhs.find_dtype(),
             int_only=self.int_only,
             promote_to_float=self.promote_to_float,
             promote_bools=self.promote_bools,
         )
     elif not isinstance(w_out, BaseArray):
         raise OperationError(space.w_TypeError, space.wrap(
                 'output must be an array'))
     else:
         out = w_out
         calc_dtype = out.find_dtype()
     if self.comparison_func:
         res_dtype = interp_dtype.get_dtype_cache(space).w_booldtype
     else:
         res_dtype = calc_dtype
     if isinstance(w_lhs, Scalar) and isinstance(w_rhs, Scalar):
         arr = self.func(calc_dtype,
             w_lhs.value.convert_to(calc_dtype),
             w_rhs.value.convert_to(calc_dtype)
         )
         if isinstance(out,Scalar):
             out.value = arr
         elif isinstance(out, BaseArray):
             out.fill(space, arr)
         else:
             out = arr
         return space.wrap(out)
     new_shape = shape_agreement(space, w_lhs.shape, w_rhs.shape)
     # Test correctness of out.shape
     if out and out.shape != shape_agreement(space, new_shape, out.shape):
         raise operationerrfmt(space.w_ValueError,
             'output parameter shape mismatch, could not broadcast [%s]' +
             ' to [%s]',
             ",".join([str(x) for x in new_shape]),
             ",".join([str(x) for x in out.shape]),
             )
     w_res = Call2(self.func, self.name,
                   new_shape, calc_dtype,
                   res_dtype, w_lhs, w_rhs, out)
     w_lhs.add_invalidates(w_res)
     w_rhs.add_invalidates(w_res)
     if out:
         w_res.get_concrete()
     return w_res
Example #3
0
 def test_shape_agreement(self):
     assert shape_agreement(self.space, [3], [3]) == [3]
     assert shape_agreement(self.space, [1, 2, 3], [1, 2, 3]) == [1, 2, 3]
     py.test.raises(OperationError, shape_agreement, self.space, [2], [3])
     assert shape_agreement(self.space, [4, 4], []) == [4, 4]
     assert shape_agreement(self.space,
             [8, 1, 6, 1], [7, 1, 5]) == [8, 7, 6, 5]
     assert shape_agreement(self.space,
             [5, 2], [4, 3, 5, 2]) == [4, 3, 5, 2]
Example #4
0
 def test_shape_agreement(self):
     assert shape_agreement(self.space, [3], [3]) == [3]
     assert shape_agreement(self.space, [1, 2, 3], [1, 2, 3]) == [1, 2, 3]
     py.test.raises(OperationError, shape_agreement, self.space, [2], [3])
     assert shape_agreement(self.space, [4, 4], []) == [4, 4]
     assert shape_agreement(self.space, [8, 1, 6, 1],
                            [7, 1, 5]) == [8, 7, 6, 5]
     assert shape_agreement(self.space, [5, 2],
                            [4, 3, 5, 2]) == [4, 3, 5, 2]
Example #5
0
    def call(self, space, args_w):
        from pypy.module.micronumpy.interp_numarray import (Call2,
                                                            convert_to_array,
                                                            Scalar,
                                                            shape_agreement)

        [w_lhs, w_rhs] = args_w
        w_lhs = convert_to_array(space, w_lhs)
        w_rhs = convert_to_array(space, w_rhs)
        calc_dtype = find_binop_result_dtype(
            space,
            w_lhs.find_dtype(),
            w_rhs.find_dtype(),
            promote_to_float=self.promote_to_float,
            promote_bools=self.promote_bools,
        )
        if self.comparison_func:
            res_dtype = interp_dtype.get_dtype_cache(space).w_booldtype
        else:
            res_dtype = calc_dtype
        if isinstance(w_lhs, Scalar) and isinstance(w_rhs, Scalar):
            return self.func(calc_dtype, w_lhs.value.convert_to(calc_dtype),
                             w_rhs.value.convert_to(calc_dtype))

        new_shape = shape_agreement(space, w_lhs.shape, w_rhs.shape)
        w_res = Call2(self.func, self.name, new_shape, calc_dtype, res_dtype,
                      w_lhs, w_rhs)
        w_lhs.add_invalidates(w_res)
        w_rhs.add_invalidates(w_res)
        return w_res
Example #6
0
    def call(self, space, args_w):
        from pypy.module.micronumpy.interp_numarray import (Call2,
            convert_to_array, Scalar, shape_agreement)

        [w_lhs, w_rhs] = args_w
        w_lhs = convert_to_array(space, w_lhs)
        w_rhs = convert_to_array(space, w_rhs)
        calc_dtype = find_binop_result_dtype(space,
            w_lhs.find_dtype(), w_rhs.find_dtype(),
            promote_to_float=self.promote_to_float,
            promote_bools=self.promote_bools,
        )
        if self.comparison_func:
            res_dtype = space.fromcache(interp_dtype.W_BoolDtype)
        else:
            res_dtype = calc_dtype
        if isinstance(w_lhs, Scalar) and isinstance(w_rhs, Scalar):
            return self.func(calc_dtype,
                w_lhs.value.convert_to(calc_dtype),
                w_rhs.value.convert_to(calc_dtype)
            ).wrap(space)

        new_sig = signature.Signature.find_sig([
            self.signature, w_lhs.signature, w_rhs.signature
        ])
        new_shape = shape_agreement(space, w_lhs.shape, w_rhs.shape)
        w_res = Call2(new_sig, new_shape, calc_dtype,
                      res_dtype, w_lhs, w_rhs)
        w_lhs.add_invalidates(w_res)
        w_rhs.add_invalidates(w_res)
        return w_res
Example #7
0
    def call(self, space, args_w):
        from pypy.module.micronumpy.interp_numarray import (Call2,
            convert_to_array, Scalar, shape_agreement)

        [w_lhs, w_rhs] = args_w
        w_lhs = convert_to_array(space, w_lhs)
        w_rhs = convert_to_array(space, w_rhs)
        calc_dtype = find_binop_result_dtype(space,
            w_lhs.find_dtype(), w_rhs.find_dtype(),
            int_only=self.int_only,
            promote_to_float=self.promote_to_float,
            promote_bools=self.promote_bools,
        )
        if self.comparison_func:
            res_dtype = interp_dtype.get_dtype_cache(space).w_booldtype
        else:
            res_dtype = calc_dtype
        if isinstance(w_lhs, Scalar) and isinstance(w_rhs, Scalar):
            return space.wrap(self.func(calc_dtype,
                w_lhs.value.convert_to(calc_dtype),
                w_rhs.value.convert_to(calc_dtype)
            ))
        new_shape = shape_agreement(space, w_lhs.shape, w_rhs.shape)
        w_res = Call2(self.func, self.name,
                      new_shape, calc_dtype,
                      res_dtype, w_lhs, w_rhs)
        w_lhs.add_invalidates(w_res)
        w_rhs.add_invalidates(w_res)
        return w_res
Example #8
0
 def call(self, space, args_w):
     from pypy.module.micronumpy.interp_numarray import (Call1, BaseArray,
         convert_to_array, Scalar, shape_agreement)
     if len(args_w)<2:
         [w_obj] = args_w
         out = None
     else:
         [w_obj, out] = args_w
         if space.is_w(out, space.w_None):
             out = None
     w_obj = convert_to_array(space, w_obj)
     calc_dtype = find_unaryop_result_dtype(space,
                               w_obj.find_dtype(),
                               promote_to_float=self.promote_to_float,
                               promote_bools=self.promote_bools)
     if out:
         if not isinstance(out, BaseArray):
             raise OperationError(space.w_TypeError, space.wrap(
                                             'output must be an array'))
         res_dtype = out.find_dtype()
     elif self.bool_result:
         res_dtype = interp_dtype.get_dtype_cache(space).w_booldtype
     else:
         res_dtype = calc_dtype
     if isinstance(w_obj, Scalar):
         arr = self.func(calc_dtype, w_obj.value.convert_to(calc_dtype))
         if isinstance(out,Scalar):
             out.value = arr
         elif isinstance(out, BaseArray):
             out.fill(space, arr)
         else:
             out = arr
         return space.wrap(out)
     if out:
         assert isinstance(out, BaseArray) # For translation
         broadcast_shape =  shape_agreement(space, w_obj.shape, out.shape)
         if not broadcast_shape or broadcast_shape != out.shape:
             raise operationerrfmt(space.w_ValueError,
                 'output parameter shape mismatch, could not broadcast [%s]' +
                 ' to [%s]',
                 ",".join([str(x) for x in w_obj.shape]),
                 ",".join([str(x) for x in out.shape]),
                 )
         w_res = Call1(self.func, self.name, out.shape, calc_dtype,
                                      res_dtype, w_obj, out)
         #Force it immediately
         w_res.get_concrete()
     else:
         w_res = Call1(self.func, self.name, w_obj.shape, calc_dtype,
                                      res_dtype, w_obj)
     w_obj.add_invalidates(w_res)
     return w_res
Example #9
0
 def call(self, space, args_w):
     from pypy.module.micronumpy.interp_numarray import (Call1, BaseArray,
         convert_to_array, Scalar, shape_agreement)
     if len(args_w)<2:
         [w_obj] = args_w
         out = None
     else:
         [w_obj, out] = args_w
         if space.is_w(out, space.w_None):
             out = None
     w_obj = convert_to_array(space, w_obj)
     calc_dtype = find_unaryop_result_dtype(space,
                               w_obj.find_dtype(),
                               promote_to_float=self.promote_to_float,
                               promote_bools=self.promote_bools)
     if out:
         if not isinstance(out, BaseArray):
             raise OperationError(space.w_TypeError, space.wrap(
                                             'output must be an array'))
         res_dtype = out.find_dtype()
     elif self.bool_result:
         res_dtype = interp_dtype.get_dtype_cache(space).w_booldtype
     else:
         res_dtype = calc_dtype
     if isinstance(w_obj, Scalar):
         arr = self.func(calc_dtype, w_obj.value.convert_to(calc_dtype))
         if isinstance(out,Scalar):
             out.value = arr
         elif isinstance(out, BaseArray):
             out.fill(space, arr)
         else:
             out = arr
         return space.wrap(out)
     if out:
         assert isinstance(out, BaseArray) # For translation
         broadcast_shape =  shape_agreement(space, w_obj.shape, out.shape)
         if not broadcast_shape or broadcast_shape != out.shape:
             raise operationerrfmt(space.w_ValueError,
                 'output parameter shape mismatch, could not broadcast [%s]' +
                 ' to [%s]',
                 ",".join([str(x) for x in w_obj.shape]),
                 ",".join([str(x) for x in out.shape]),
                 )
         w_res = Call1(self.func, self.name, out.shape, calc_dtype,
                                      res_dtype, w_obj, out)
         #Force it immediately
         w_res.get_concrete()
     else:
         w_res = Call1(self.func, self.name, w_obj.shape, calc_dtype,
                                      res_dtype, w_obj)
     w_obj.add_invalidates(space, w_res)
     return w_res