def test_index_of_single_item_c(self): a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'C') r = a._index_of_single_item(self.space, self.newtuple(1, 2, 2)) assert r == 1 * 3 * 5 + 2 * 3 + 2 s = a.create_slice([(0, 10, 1, 10), (2, 0, 0, 1)]) r = s._index_of_single_item(self.space, self.newtuple(1, 0)) assert r == a._index_of_single_item(self.space, self.newtuple(1, 2, 0)) r = s._index_of_single_item(self.space, self.newtuple(1, 1)) assert r == a._index_of_single_item(self.space, self.newtuple(1, 2, 1))
def descr__new__string_box(space, w_subtype, w_arg): from pypy.module.micronumpy.interp_numarray import W_NDimArray from pypy.module.micronumpy.interp_dtype import new_string_dtype arg = space.str_w(space.str(w_arg)) arr = W_NDimArray([1], new_string_dtype(space, len(arg))) for i in range(len(arg)): arr.storage[i] = arg[i] return W_StringBox(arr, 0, arr.dtype)
def f(n): if NonConstant(False): dtype = float64_dtype else: dtype = int32_dtype ar = W_NDimArray(n, [n], dtype=dtype) i = 0 while i < n: ar.get_concrete().setitem(i, int32_dtype.box(7)) i += 1 v = ar.descr_add(space, ar).descr_sum(space) assert isinstance(v, IntObject) return v.intval
def test_slice_signature(self, space): float64_dtype = get_dtype_cache(space).w_float64dtype ar = W_NDimArray(10, [10], dtype=float64_dtype) v1 = ar.descr_getitem(space, space.wrap(slice(1, 3, 1))) v2 = ar.descr_getitem(space, space.wrap(slice(4, 6, 1))) assert v1.find_sig() is v2.find_sig() v3 = v2.descr_add(space, v1) v4 = v1.descr_add(space, v2) assert v3.find_sig() is v4.find_sig() v5 = ar.descr_add(space, ar).descr_getitem(space, space.wrap(slice(1, 3, 1))) v6 = ar.descr_add(space, ar).descr_getitem(space, space.wrap(slice(1, 4, 1))) assert v5.find_sig() is v6.find_sig()
def test_slice_signature(self, space): float64_dtype = get_dtype_cache(space).w_float64dtype ar = W_NDimArray([10], dtype=float64_dtype) v1 = ar.descr_getitem(space, space.wrap(slice(1, 3, 1))) v2 = ar.descr_getitem(space, space.wrap(slice(4, 6, 1))) assert v1.find_sig() is v2.find_sig() v3 = v2.descr_add(space, v1) v4 = v1.descr_add(space, v2) assert v3.find_sig() is v4.find_sig() v5 = ar.descr_add(space, ar).descr_getitem(space, space.wrap(slice(1, 3, 1))) v6 = ar.descr_add(space, ar).descr_getitem(space, space.wrap(slice(1, 4, 1))) assert v5.find_sig() is v6.find_sig()
def test_slice_of_slice_c(self): a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), order='C') s = a.create_slice([(5, 0, 0, 1)]) assert s.start == 15 * 5 s2 = s.create_slice([(3, 0, 0, 1)]) assert s2.shape == [3] assert s2.strides == [1] assert s2.parent is a assert s2.backstrides == [2] assert s2.start == 5 * 15 + 3 * 3 s = a.create_slice([(1, 5, 3, 2)]) s2 = s.create_slice([(0, 2, 1, 2), (2, 0, 0, 1)]) assert s2.shape == [2, 3] assert s2.strides == [45, 1] assert s2.backstrides == [45, 2] assert s2.start == 1 * 15 + 2 * 3
def test_slice_of_slice_f(self): a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'F') s = a.create_slice([(5, 0, 0, 1)]) assert s.start == 5 s2 = s.create_slice([(3, 0, 0, 1)]) assert s2.shape == [3] assert s2.strides == [50] assert s2.parent is a assert s2.backstrides == [100] assert s2.start == 35 s = a.create_slice([(1, 5, 3, 2)]) s2 = s.create_slice([(0, 2, 1, 2), (2, 0, 0, 1)]) assert s2.shape == [2, 3] assert s2.strides == [3, 50] assert s2.backstrides == [3, 100] assert s2.start == 1 * 15 + 2 * 3
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
def test_create_slice_f(self): a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'F') s = a.create_slice([(3, 0, 0, 1)]) assert s.start == 3 assert s.strides == [10, 50] assert s.backstrides == [40, 100] s = a.create_slice([(1, 9, 2, 4)]) assert s.start == 1 assert s.strides == [2, 10, 50] assert s.backstrides == [6, 40, 100] s = a.create_slice([(1, 5, 3, 2), (1, 2, 1, 1), (1, 0, 0, 1)]) assert s.shape == [2, 1] assert s.strides == [3, 10] assert s.backstrides == [3, 0] s = a.create_slice([(0, 10, 1, 10), (2, 0, 0, 1)]) assert s.start == 20 assert s.shape == [10, 3]
def test_create_slice_c(self): a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'C') s = a.create_slice([(3, 0, 0, 1)]) assert s.start == 45 assert s.strides == [3, 1] assert s.backstrides == [12, 2] s = a.create_slice([(1, 9, 2, 4)]) assert s.start == 15 assert s.strides == [30, 3, 1] assert s.backstrides == [90, 12, 2] s = a.create_slice([(1, 5, 3, 2), (1, 2, 1, 1), (1, 0, 0, 1)]) assert s.start == 19 assert s.shape == [2, 1] assert s.strides == [45, 3] assert s.backstrides == [45, 0] s = a.create_slice([(0, 10, 1, 10), (2, 0, 0, 1)]) assert s.start == 6 assert s.shape == [10, 3]
def descr__new__unicode_box(space, w_subtype, w_arg): from pypy.module.micronumpy.interp_numarray import W_NDimArray from pypy.module.micronumpy.interp_dtype import new_unicode_dtype arg = space.unicode_w(unicode_from_object(space, w_arg)) arr = W_NDimArray([1], new_unicode_dtype(space, len(arg))) # XXX not this way, we need store #for i in range(len(arg)): # arr.storage[i] = arg[i] return W_UnicodeBox(arr, 0, arr.dtype)
def test_binop_signature(self, space): float64_dtype = get_dtype_cache(space).w_float64dtype bool_dtype = get_dtype_cache(space).w_booldtype ar = W_NDimArray(10, [10], dtype=float64_dtype) ar2 = W_NDimArray(10, [10], dtype=float64_dtype) v1 = ar.descr_add(space, ar) v2 = ar.descr_add(space, Scalar(float64_dtype, W_Float64Box(2.0))) sig1 = v1.find_sig() sig2 = v2.find_sig() assert v1 is not v2 assert sig1.left.iter_no == sig1.right.iter_no assert sig2.left.iter_no != sig2.right.iter_no assert sig1.left.array_no == sig1.right.array_no sig1b = ar2.descr_add(space, ar).find_sig() assert sig1b.left.array_no != sig1b.right.array_no assert sig1b is not sig1 v3 = ar.descr_add(space, Scalar(float64_dtype, W_Float64Box(1.0))) sig3 = v3.find_sig() assert sig2 is sig3 v4 = ar.descr_add(space, ar) assert v1.find_sig() is v4.find_sig() bool_ar = W_NDimArray(10, [10], dtype=bool_dtype) v5 = ar.descr_add(space, bool_ar) assert v5.find_sig() is not v1.find_sig() assert v5.find_sig() is not v2.find_sig() v6 = ar.descr_add(space, bool_ar) assert v5.find_sig() is v6.find_sig() v7 = v6.descr_add(space, v6) sig7 = v7.find_sig() assert sig7.left.left.iter_no == sig7.right.left.iter_no assert sig7.left.left.iter_no != sig7.right.right.iter_no assert sig7.left.right.iter_no == sig7.right.right.iter_no v1.forced_result = ar assert v1.find_sig() is not sig1
def _fromstring_bin(space, s, count, length, dtype): from pypy.module.micronumpy.interp_numarray import W_NDimArray itemsize = dtype.itemtype.get_element_size() if count == -1: count = length / itemsize if length % itemsize != 0: raise operationerrfmt(space.w_ValueError, "string length %d not divisable by item size %d", length, itemsize) if count * itemsize > length: raise OperationError(space.w_ValueError, space.wrap( "string is smaller than requested size")) a = W_NDimArray(count, [count], dtype=dtype) for i in range(count): val = dtype.itemtype.runpack_str(s[i*itemsize:i*itemsize + itemsize]) a.dtype.setitem(a.storage, i, val) return space.wrap(a)
def coerce(self, space, dtype, w_item): from pypy.module.micronumpy.interp_numarray import W_NDimArray if isinstance(w_item, interp_boxes.W_VoidBox): return w_item # we treat every sequence as sequence, no special support # for arrays if not space.issequence_w(w_item): raise OperationError(space.w_TypeError, space.wrap( "expected sequence")) if len(self.offsets_and_fields) != space.int_w(space.len(w_item)): raise OperationError(space.w_ValueError, space.wrap( "wrong length")) items_w = space.fixedview(w_item) # XXX optimize it out one day, but for now we just allocate an # array arr = W_NDimArray([1], dtype) for i in range(len(items_w)): subdtype = dtype.fields[dtype.fieldnames[i]][1] ofs, itemtype = self.offsets_and_fields[i] w_item = items_w[i] w_box = itemtype.coerce(space, subdtype, w_item) itemtype.store(arr, 1, 0, ofs, w_box) return interp_boxes.W_VoidBox(arr, 0, arr.dtype)
gotit = True except OperationError, e: if not e.match(space, space.w_ValueError): raise if not gotit: val = dtype.itemtype.default_fromstring(space) nextidx = length items.append(val) num_items += 1 idx = nextidx + 1 if count > num_items: raise OperationError(space.w_ValueError, space.wrap( "string is smaller than requested size")) a = W_NDimArray(num_items, [num_items], dtype=dtype) for i, val in enumerate(items): a.dtype.setitem(a.storage, i, val) return space.wrap(a) def _fromstring_bin(space, s, count, length, dtype): from pypy.module.micronumpy.interp_numarray import W_NDimArray itemsize = dtype.itemtype.get_element_size() if count == -1: count = length / itemsize if length % itemsize != 0: raise operationerrfmt(space.w_ValueError, "string length %d not divisable by item size %d", length, itemsize)
def test_strides_c(self): a = W_NDimArray(100, [10, 5, 3], MockDtype(), 'C') assert a.strides == [15, 3, 1] assert a.backstrides == [135, 12, 2]
gotit = True except OperationError, e: if not e.match(space, space.w_ValueError): raise if not gotit: val = dtype.itemtype.default_fromstring(space) nextidx = length items.append(val) num_items += 1 idx = nextidx + 1 if count > num_items: raise OperationError(space.w_ValueError, space.wrap( "string is smaller than requested size")) a = W_NDimArray([num_items], dtype=dtype) ai = a.create_iter() for val in items: a.dtype.setitem(a, ai.offset, val) ai = ai.next(1) return space.wrap(a) def _fromstring_bin(space, s, count, length, dtype): from pypy.module.micronumpy.interp_numarray import W_NDimArray itemsize = dtype.itemtype.get_element_size() assert itemsize >= 0 if count == -1: count = length / itemsize if length % itemsize != 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
def test_negative_step_c(self): a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), order='C') s = a.create_slice([(9, -1, -2, 5)]) assert s.start == 135 assert s.strides == [-30, 3, 1] assert s.backstrides == [-120, 12, 2]
def test_negative_step_f(self): a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'F') s = a.create_slice([(9, -1, -2, 5)]) assert s.start == 9 assert s.strides == [-2, 10, 50] assert s.backstrides == [-8, 40, 100]
def test_strides_f(self): a = W_NDimArray(100, [10, 5, 3], MockDtype(), 'F') assert a.strides == [1, 10, 50] assert a.backstrides == [9, 40, 100]
def test_binop_signature(self, space): float64_dtype = get_dtype_cache(space).w_float64dtype bool_dtype = get_dtype_cache(space).w_booldtype ar = W_NDimArray([10], dtype=float64_dtype) ar2 = W_NDimArray([10], dtype=float64_dtype) v1 = ar.descr_add(space, ar) v2 = ar.descr_add(space, Scalar(float64_dtype, W_Float64Box(2.0))) sig1 = v1.find_sig() sig2 = v2.find_sig() assert v1 is not v2 assert sig1.left.iter_no == sig1.right.iter_no assert sig2.left.iter_no != sig2.right.iter_no assert sig1.left.array_no == sig1.right.array_no sig1b = ar2.descr_add(space, ar).find_sig() assert sig1b.left.array_no != sig1b.right.array_no assert sig1b is not sig1 v3 = ar.descr_add(space, Scalar(float64_dtype, W_Float64Box(1.0))) sig3 = v3.find_sig() assert sig2 is sig3 v4 = ar.descr_add(space, ar) assert v1.find_sig() is v4.find_sig() bool_ar = W_NDimArray([10], dtype=bool_dtype) v5 = ar.descr_add(space, bool_ar) assert v5.find_sig() is not v1.find_sig() assert v5.find_sig() is not v2.find_sig() v6 = ar.descr_add(space, bool_ar) assert v5.find_sig() is v6.find_sig() v7 = v6.descr_add(space, v6) sig7 = v7.find_sig() assert sig7.left.left.iter_no == sig7.right.left.iter_no assert sig7.left.left.iter_no != sig7.right.right.iter_no assert sig7.left.right.iter_no == sig7.right.right.iter_no v1.forced_result = ar assert v1.find_sig() is not sig1