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
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
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
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
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
def impl(space, w_lhs, w_rhs): from pypy.module.micronumpy.interp_numarray import Call2, convert_to_array if space.issequence_w(w_lhs) or space.issequence_w(w_rhs): w_lhs_arr = convert_to_array(space, w_lhs) w_rhs_arr = convert_to_array(space, w_rhs) new_sig = w_lhs_arr.signature.transition(signature).transition(w_rhs_arr.signature) w_res = Call2(func, w_lhs_arr, w_rhs_arr, new_sig) w_lhs_arr.invalidates.append(w_res) w_rhs_arr.invalidates.append(w_res) return w_res else: return space.wrap(func(space.float_w(w_lhs), space.float_w(w_rhs)))
def descr_reduce(self, space, w_obj): 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")) assert isinstance(self, W_Ufunc2) obj = convert_to_array(space, w_obj) if isinstance(obj, Scalar): raise OperationError(space.w_TypeError, space.wrap("cannot reduce " "on a scalar")) size = obj.find_size() dtype = find_unaryop_result_dtype( space, obj.find_dtype(), promote_to_largest=True ) start = 0 if self.identity is None: if size == 0: raise operationerrfmt(space.w_ValueError, "zero-size array to " "%s.reduce without identity", self.name) value = obj.eval(0).convert_to(dtype) start += 1 else: value = self.identity.convert_to(dtype) new_sig = signature.Signature.find_sig([ self.reduce_signature, obj.signature ]) return self.reduce(new_sig, start, value, obj, dtype, size).wrap(space)
def reduce(self, space, w_obj, multidim): 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")) assert isinstance(self, W_Ufunc2) obj = convert_to_array(space, w_obj) if isinstance(obj, Scalar): raise OperationError(space.w_TypeError, space.wrap("cannot reduce " "on a scalar")) size = obj.find_size() dtype = find_unaryop_result_dtype( space, obj.find_dtype(), promote_to_largest=True ) start = obj.start_iter(obj.shape) shapelen = len(obj.shape) if shapelen > 1 and not multidim: raise OperationError(space.w_NotImplementedError, space.wrap("not implemented yet")) if self.identity is None: if size == 0: raise operationerrfmt(space.w_ValueError, "zero-size array to " "%s.reduce without identity", self.name) value = obj.eval(start).convert_to(dtype) start = start.next(shapelen) else: value = self.identity.convert_to(dtype) new_sig = signature.Signature.find_sig([ self.reduce_signature, obj.signature ]) return self.reduce_loop(new_sig, shapelen, start, value, obj, dtype).wrap(space)
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)
def impl(space, w_obj): from pypy.module.micronumpy.interp_numarray import Call1, convert_to_array if space.issequence_w(w_obj): w_obj_arr = convert_to_array(space, w_obj) w_res = Call1(func, w_obj_arr, w_obj_arr.signature.transition(signature)) w_obj_arr.invalidates.append(w_res) return w_res else: return space.wrap(func(space.float_w(w_obj)))
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
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
def call(self, space, args_w): from pypy.module.micronumpy.interp_numarray import (Call1, convert_to_array, Scalar) [w_obj] = args_w w_obj = convert_to_array(space, w_obj) res_dtype = find_unaryop_result_dtype(space, w_obj.find_dtype(), promote_to_float=self.promote_to_float, promote_bools=self.promote_bools, ) if isinstance(w_obj, Scalar): return self.func(res_dtype, w_obj.value.convert_to(res_dtype)) w_res = Call1(self.func, self.name, w_obj.shape, res_dtype, w_obj) w_obj.add_invalidates(w_res) return w_res
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)
def call(self, space, args_w): from pypy.module.micronumpy.interp_numarray import (Call1, convert_to_array, Scalar) [w_obj] = args_w w_obj = convert_to_array(space, w_obj) res_dtype = find_unaryop_result_dtype( space, w_obj.find_dtype(), promote_to_float=self.promote_to_float, promote_bools=self.promote_bools, ) if isinstance(w_obj, Scalar): return self.func(res_dtype, w_obj.value.convert_to(res_dtype)) w_res = Call1(self.func, self.name, w_obj.shape, res_dtype, w_obj) w_obj.add_invalidates(w_res) return w_res
def call(self, space, args_w): from pypy.module.micronumpy.interp_numarray import (Call1, convert_to_array, Scalar) [w_obj] = args_w 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 self.bool_result: res_dtype = interp_dtype.get_dtype_cache(space).w_booldtype else: res_dtype = calc_dtype if isinstance(w_obj, Scalar): return space.wrap(self.func(calc_dtype, w_obj.value.convert_to(calc_dtype))) 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
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)
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
def where(space, w_arr, w_x, w_y): """where(condition, [x, y]) Return elements, either from `x` or `y`, depending on `condition`. If only `condition` is given, return ``condition.nonzero()``. Parameters ---------- condition : array_like, bool When True, yield `x`, otherwise yield `y`. x, y : array_like, optional Values from which to choose. `x` and `y` need to have the same shape as `condition`. Returns ------- out : ndarray or tuple of ndarrays If both `x` and `y` are specified, the output array contains elements of `x` where `condition` is True, and elements from `y` elsewhere. If only `condition` is given, return the tuple ``condition.nonzero()``, the indices where `condition` is True. See Also -------- nonzero, choose Notes ----- If `x` and `y` are given and input arrays are 1-D, `where` is equivalent to:: [xv if c else yv for (c,xv,yv) in zip(condition,x,y)] Examples -------- >>> np.where([[True, False], [True, True]], ... [[1, 2], [3, 4]], ... [[9, 8], [7, 6]]) array([[1, 8], [3, 4]]) >>> np.where([[0, 1], [1, 0]]) (array([0, 1]), array([1, 0])) >>> x = np.arange(9.).reshape(3, 3) >>> np.where( x > 5 ) (array([2, 2, 2]), array([0, 1, 2])) >>> x[np.where( x > 3.0 )] # Note: result is 1D. array([ 4., 5., 6., 7., 8.]) >>> np.where(x < 5, x, -1) # Note: broadcasting. array([[ 0., 1., 2.], [ 3., 4., -1.], [-1., -1., -1.]]) NOTE: support for not passing x and y is unsupported """ arr = convert_to_array(space, w_arr) x = convert_to_array(space, w_x) y = convert_to_array(space, w_y) return WhereArray(space, arr, x, y)