[pypy-svn] r46357 - in pypy/dist/pypy/rpython/numpy: . test
simonb at codespeak.net
simonb at codespeak.net
Wed Sep 5 20:22:31 CEST 2007
Author: simonb
Date: Wed Sep 5 20:22:30 2007
New Revision: 46357
Modified:
pypy/dist/pypy/rpython/numpy/aarray.py
pypy/dist/pypy/rpython/numpy/rarray.py
pypy/dist/pypy/rpython/numpy/test/test_array.py
Log:
get transpose to work, add a reshape method
Modified: pypy/dist/pypy/rpython/numpy/aarray.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/aarray.py (original)
+++ pypy/dist/pypy/rpython/numpy/aarray.py Wed Sep 5 20:22:30 2007
@@ -49,6 +49,15 @@
return SomeObject.getattr(s_array, s_attr)
return s
+ def method_reshape(self, s_tuple):
+ if not isinstance(s_tuple, SomeTuple):
+ raise AnnotatorError("reshape expects tuple arg")
+ for s_item in s_tuple.items:
+ if not isinstance(s_item, SomeInteger):
+ raise AnnotatorError("bad shape arg")
+ ndim = len(s_tuple.items)
+ return SomeArray(self.typecode, ndim)
+
def method_transpose(self):
return SomeArray(self.typecode, self.ndim)
@@ -143,7 +152,6 @@
valid_typecodes='bhilqBHILQfd'
class ArrayCallEntry(ExtRegistryEntry):
- "Annotation and rtyping of calls to numpy.array"
_about_ = numpy.array
def compute_result_annotation(self, s_list, s_dtype=None):
@@ -178,7 +186,6 @@
class EmptyCallEntry(ExtRegistryEntry):
- "Annotation and rtyping of calls to numpy.empty"
_about_ = numpy.empty
def compute_result_annotation(self, s_arg, s_dtype=None):
@@ -191,7 +198,7 @@
elif isinstance(s_arg, SomeInteger):
ndim = 1
else:
- raise AnnotatorError("shape arg not understood")
+ raise AnnotatorError("shape arg not understood: %s"%s_arg)
typecode = 'd'
if isinstance(s_dtype, SomeChar) and s_dtype.is_constant():
@@ -208,7 +215,6 @@
return v_result
class ZerosCallEntry(EmptyCallEntry):
- "Annotation and rtyping of calls to numpy.zeros"
_about_ = numpy.zeros
def specialize_call(self, hop, i_dtype=None):
Modified: pypy/dist/pypy/rpython/numpy/rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/rarray.py (original)
+++ pypy/dist/pypy/rpython/numpy/rarray.py Wed Sep 5 20:22:30 2007
@@ -34,6 +34,22 @@
return array
return ll_build_from_shape
+def gen_build_alias_shape(ndim):
+ unrolling_dims = unrolling_iterable(reversed(range(ndim)))
+ def ll_build_alias_shape(ARRAY, ao, shape):
+ array = ll_allocate(ARRAY, ndim)
+ itemsize = 1
+ for i in unrolling_dims:
+ attr = 'item%d'%i
+ size = getattr(shape, attr)
+ array.shape[i] = size
+ array.strides[i] = itemsize
+ itemsize *= size
+ array.data = ao.data
+ array.dataptr = ao.dataptr
+ return array
+ return ll_build_alias_shape
+
def gen_get_shape(ndim):
unrolling_dims = unrolling_iterable(range(ndim))
def ll_get_shape(ARRAY, TUPLE, array):
@@ -199,7 +215,6 @@
ndim = self.s_array.ndim
if isinstance(r_arg, TupleRepr):
r_tuple, v_tuple = r_arg, v_arg
- cTUPLE = inputconst(lltype.Void, r_tuple.lowleveltype.TO)
ll_build_from_shape = gen_build_from_shape(ndim, zero)
c_ndim = inputconst(lltype.Signed, ndim)
assert ndim == len(r_tuple.items_r)
@@ -216,6 +231,17 @@
cARRAY = hop.inputconst(Void, hop.r_result.ARRAY.TO)
return hop.gendirectcall(ll_transpose, cARRAY, v_self)
+ def rtype_method_reshape(self, hop):
+ r_result = hop.r_result
+ r_tuple = hop.args_r[1]
+ if not isinstance(r_tuple, TupleRepr):
+ raise TyperError()
+ ndim = len(r_tuple.items_r)
+ ll_build_alias_shape = gen_build_alias_shape(ndim)
+ [v_array, v_tuple] = hop.inputargs(self, r_tuple)
+ cARRAY = inputconst(lltype.Void, r_result.lowleveltype.TO)
+ return hop.llops.gendirectcall(ll_build_alias_shape, cARRAY, v_array, v_tuple)
+
def get_ndim(self, hop, v_array):
cname = inputconst(Void, 'ndim')
return hop.llops.genop('getfield', [v_array, cname], resulttype=Signed)
@@ -437,10 +463,15 @@
array.dataptr = direct_arrayitems(array.data)
return array
-def ll_transpose(ARRAY, a1):
- a2 = ll_build_alias(ARRAY, a1)
- # XX do something to a2
- return a2
+def ll_transpose(ARRAY, ao):
+ ndim = ao.ndim
+ array = ll_allocate(ARRAY, ndim)
+ array.data = ao.data # alias data
+ for i in range(ndim):
+ array.shape[i] = ao.shape[ndim-i-1]
+ array.strides[i] = ao.strides[ndim-i-1]
+ array.dataptr = ao.dataptr
+ return array
Modified: pypy/dist/pypy/rpython/numpy/test/test_array.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/test/test_array.py (original)
+++ pypy/dist/pypy/rpython/numpy/test/test_array.py Wed Sep 5 20:22:30 2007
@@ -148,24 +148,33 @@
assert s.typecode == 'd'
def test_annotate_array_attr(self):
- def f():
+ def fget():
a1 = numpy.array([1,2])
return a1.shape
t = TranslationContext()
a = t.buildannotator()
- s = a.build_types(f, [])
+ s = a.build_types(fget, [])
assert type(s) == SomeTuple
def test_annotate_array_method(self):
- def f():
- a1 = numpy.array([1,2])
- return a1.transpose()
+ def f_transpose():
+ a = numpy.zeros((3,4))
+ return a.transpose()
t = TranslationContext()
a = t.buildannotator()
- s = a.build_types(f, [])
+ s = a.build_types(f_transpose, [])
+ assert type(s) == SomeArray
+ assert s.ndim == 2
+
+ def f_reshape():
+ a = numpy.array(range(12))
+ return a.reshape((3,4))
+
+ s = a.build_types(f_reshape, [])
assert type(s) == SomeArray
+ assert s.ndim == 2
def test_annotate_indexing(self):
def f():
@@ -284,13 +293,27 @@
#assert len(res.data) == 3*4*5 # GcArray has nolength
def test_specialize_array_method(self):
- def f():
- a = numpy.array([1,2])
+ def f_transpose():
+ a = numpy.zeros((3,4))
return a.transpose()
- res = interpret(f, [])
- assert res.data[0] == 1
- assert res.data[1] == 2
+ res = interpret(f_transpose, [])
+ assert res.shape[0] == 4
+ assert res.shape[1] == 3
+
+ def f_reshape():
+ a = numpy.array(range(12))
+ b = a.reshape((3,4))
+ b[1,2] = 0
+ return b
+
+ res = interpret(f_reshape, [])
+ assert res.shape[0] == 3
+ assert res.shape[1] == 4
+ assert res.strides[0] == 4
+ assert res.strides[1] == 1
+ assert res.dataptr[5] == 5
+ assert res.dataptr[6] == 0
def test_specialize_view_0(self):
def f():
More information about the pypy-svn
mailing list