Author: Maciej Fijalkowski <[email protected]>
Branch: numpy-refactor
Changeset: r57072:631db4d5bb2f
Date: 2012-09-01 21:20 +0200
http://bitbucket.org/pypy/pypy/changeset/631db4d5bb2f/
Log: Fix direct tests. Add binary operations.
diff --git a/pypy/module/micronumpy/arrayimpl/concrete.py
b/pypy/module/micronumpy/arrayimpl/concrete.py
--- a/pypy/module/micronumpy/arrayimpl/concrete.py
+++ b/pypy/module/micronumpy/arrayimpl/concrete.py
@@ -192,6 +192,13 @@
if len(view_w) < shape_len:
raise IndexError
if len(view_w) > shape_len:
+ # we can allow for one extra None
+ count = len(view_w)
+ for w_item in view_w:
+ if space.is_w(w_item, space.w_None):
+ count -= 1
+ if count == shape_len:
+ raise IndexError # but it's still not a single item
raise OperationError(space.w_IndexError,
space.wrap("invalid index"))
return self._lookup_by_index(space, view_w)
@@ -260,6 +267,8 @@
self.strides = strides
self.backstrides = backstrides
self.shape = shape
+ if isinstance(parent, SliceArray):
+ parent = parent.parent # one level only
self.parent = parent
self.storage = parent.storage
self.order = parent.order
diff --git a/pypy/module/micronumpy/interp_numarray.py
b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -8,9 +8,11 @@
from pypy.module.micronumpy.strides import find_shape_and_elems,\
get_shape_from_iterable
from pypy.module.micronumpy.interp_support import unwrap_axis_arg
+from pypy.module.micronumpy.appbridge import get_appbridge_cache
from pypy.tool.sourcetools import func_with_new_name
from pypy.rlib import jit
from pypy.rlib.objectmodel import instantiate
+from pypy.rlib.rstring import StringBuilder
def _find_shape(space, w_size):
if space.isinstance_w(w_size, space.w_int):
@@ -82,6 +84,28 @@
raise OperationError(space.w_TypeError, space.wrap(
"len() of unsized object"))
+ def descr_repr(self, space):
+ #cache = get_appbridge_cache(space)
+ #if cache.w_array_repr is None:
+ return space.wrap(self.dump_data())
+ #return space.call_function(cache.w_array_repr, self)
+
+ def dump_data(self):
+ i = self.create_iter()
+ first = True
+ dtype = self.get_dtype()
+ s = StringBuilder()
+ s.append('array([')
+ while not i.done():
+ if first:
+ first = False
+ else:
+ s.append(', ')
+ s.append(dtype.itemtype.str_format(i.getitem()))
+ i.next()
+ s.append('])')
+ return s.build()
+
def create_iter(self):
return self.implementation.create_iter()
@@ -146,7 +170,24 @@
return func_with_new_name(impl, "binop_%s_impl" % ufunc_name)
descr_add = _binop_impl("add")
-
+ descr_sub = _binop_impl("subtract")
+ descr_mul = _binop_impl("multiply")
+ descr_div = _binop_impl("divide")
+ descr_truediv = _binop_impl("true_divide")
+ descr_floordiv = _binop_impl("floor_divide")
+ descr_mod = _binop_impl("mod")
+ descr_pow = _binop_impl("power")
+ descr_lshift = _binop_impl("left_shift")
+ descr_rshift = _binop_impl("right_shift")
+ descr_and = _binop_impl("bitwise_and")
+ descr_or = _binop_impl("bitwise_or")
+ descr_xor = _binop_impl("bitwise_xor")
+
+ def descr_divmod(self, space, w_other):
+ w_quotient = self.descr_div(space, w_other)
+ w_remainder = self.descr_mod(space, w_other)
+ return space.newtuple([w_quotient, w_remainder])
+
descr_eq = _binop_impl("equal")
descr_ne = _binop_impl("not_equal")
descr_lt = _binop_impl("less")
@@ -215,7 +256,21 @@
__getitem__ = interp2app(W_NDimArray.descr_getitem),
__setitem__ = interp2app(W_NDimArray.descr_setitem),
+ __repr__ = interp2app(W_NDimArray.descr_repr),
+
__add__ = interp2app(W_NDimArray.descr_add),
+ __mul__ = interp2app(W_NDimArray.descr_mul),
+ __div__ = interp2app(W_NDimArray.descr_div),
+ __truediv__ = interp2app(W_NDimArray.descr_truediv),
+ __floordiv__ = interp2app(W_NDimArray.descr_floordiv),
+ __mod__ = interp2app(W_NDimArray.descr_mod),
+ __divmod__ = interp2app(W_NDimArray.descr_divmod),
+ __pow__ = interp2app(W_NDimArray.descr_pow),
+ __lshift__ = interp2app(W_NDimArray.descr_lshift),
+ __rshift__ = interp2app(W_NDimArray.descr_rshift),
+ __and__ = interp2app(W_NDimArray.descr_and),
+ __or__ = interp2app(W_NDimArray.descr_or),
+ __xor__ = interp2app(W_NDimArray.descr_xor),
__radd__ = interp2app(W_NDimArray.descr_radd),
@@ -278,7 +333,7 @@
raise operationerrfmt(space.w_NotImplementedError,
"copying over different dtypes unsupported")
if copy:
- return w_object.copy(space)
+ return w_object.descr_copy(space)
return w_object
dtype = decode_w_dtype(space, w_dtype)
shape, elems_w = find_shape_and_elems(space, w_object, dtype)
diff --git a/pypy/module/micronumpy/test/test_numarray.py
b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -4,7 +4,7 @@
from pypy.conftest import option
from pypy.interpreter.error import OperationError
from pypy.module.micronumpy.appbridge import get_appbridge_cache
-from pypy.module.micronumpy.interp_iter import Chunk, Chunks
+from pypy.module.micronumpy.iter import Chunk, Chunks
from pypy.module.micronumpy.interp_numarray import W_NDimArray
from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
@@ -14,12 +14,19 @@
def malloc(size):
return None
+ @staticmethod
+ def get_element_size():
+ return 1
+
def get_size(self):
return 1
def create_slice(a, chunks):
- return Chunks(chunks).apply(a)
+ return Chunks(chunks).apply(a).implementation
+
+def create_array(*args, **kwargs):
+ return W_NDimArray(*args, **kwargs).implementation
class TestNumArrayDirect(object):
def newslice(self, *args):
@@ -35,17 +42,17 @@
return self.space.newtuple(args_w)
def test_strides_f(self):
- a = W_NDimArray([10, 5, 3], MockDtype(), order='F').implementation
+ a = create_array([10, 5, 3], MockDtype(), order='F')
assert a.strides == [1, 10, 50]
assert a.backstrides == [9, 40, 100]
def test_strides_c(self):
- a = W_NDimArray([10, 5, 3], MockDtype(), order='C').implementation
+ a = create_array([10, 5, 3], MockDtype(), order='C')
assert a.strides == [15, 3, 1]
assert a.backstrides == [135, 12, 2]
def test_create_slice_f(self):
- a = W_NDimArray([10, 5, 3], MockDtype(), order='F').implementation
+ a = create_array([10, 5, 3], MockDtype(), order='F')
s = create_slice(a, [Chunk(3, 0, 0, 1)])
assert s.start == 3
assert s.strides == [10, 50]
@@ -63,7 +70,7 @@
assert s.shape == [10, 3]
def test_create_slice_c(self):
- a = W_NDimArray([10, 5, 3], MockDtype(), order='C')
+ a = create_array([10, 5, 3], MockDtype(), order='C')
s = create_slice(a, [Chunk(3, 0, 0, 1)])
assert s.start == 45
assert s.strides == [3, 1]
@@ -83,7 +90,7 @@
assert s.shape == [10, 3]
def test_slice_of_slice_f(self):
- a = W_NDimArray([10, 5, 3], MockDtype(), order='F')
+ a = create_array([10, 5, 3], MockDtype(), order='F')
s = create_slice(a, [Chunk(5, 0, 0, 1)])
assert s.start == 5
s2 = create_slice(s, [Chunk(3, 0, 0, 1)])
@@ -100,7 +107,7 @@
assert s2.start == 1 * 15 + 2 * 3
def test_slice_of_slice_c(self):
- a = W_NDimArray([10, 5, 3], MockDtype(), order='C')
+ a = create_array([10, 5, 3], MockDtype(), order='C')
s = create_slice(a, [Chunk(5, 0, 0, 1)])
assert s.start == 15 * 5
s2 = create_slice(s, [Chunk(3, 0, 0, 1)])
@@ -117,41 +124,21 @@
assert s2.start == 1 * 15 + 2 * 3
def test_negative_step_f(self):
- a = W_NDimArray([10, 5, 3], MockDtype(), order='F')
+ a = create_array([10, 5, 3], MockDtype(), order='F')
s = create_slice(a, [Chunk(9, -1, -2, 5)])
assert s.start == 9
assert s.strides == [-2, 10, 50]
assert s.backstrides == [-8, 40, 100]
def test_negative_step_c(self):
- a = W_NDimArray([10, 5, 3], MockDtype(), order='C')
+ a = create_array([10, 5, 3], MockDtype(), order='C')
s = create_slice(a, [Chunk(9, -1, -2, 5)])
assert s.start == 135
assert s.strides == [-30, 3, 1]
assert s.backstrides == [-120, 12, 2]
- def test_index_of_single_item_f(self):
- a = W_NDimArray([10, 5, 3], MockDtype(), order='F')
- r = a._index_of_single_item(self.space, self.newtuple(1, 2, 2))
- assert r == 1 + 2 * 10 + 2 * 50
- s = create_slice(a, [Chunk(0, 10, 1, 10), Chunk(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 test_index_of_single_item_c(self):
- a = W_NDimArray([10, 5, 3], MockDtype(), order='C')
- r = a._index_of_single_item(self.space, self.newtuple(1, 2, 2))
- assert r == 1 * 3 * 5 + 2 * 3 + 2
- s = create_slice(a, [Chunk(0, 10, 1, 10), Chunk(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 test_shape_agreement(self):
- from pypy.module.micronumpy.interp_numarray import shape_agreement
+ from pypy.module.micronumpy.strides import shape_agreement
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])
@@ -162,7 +149,7 @@
[5, 2], [4, 3, 5, 2]) == [4, 3, 5, 2]
def test_calc_new_strides(self):
- from pypy.module.micronumpy.interp_numarray import calc_new_strides
+ from pypy.module.micronumpy.strides import calc_new_strides
assert calc_new_strides([2, 4], [4, 2], [4, 2], "C") == [8, 2]
assert calc_new_strides([2, 4, 3], [8, 3], [1, 16], 'F') == [1, 2, 16]
assert calc_new_strides([2, 3, 4], [8, 3], [1, 16], 'F') is None
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit