[pypy-svn] r70061 - in pypy/branch/micronumpy/pypy/module/micronumpy: . test
dan at codespeak.net
dan at codespeak.net
Thu Dec 10 21:48:56 CET 2009
Author: dan
Date: Thu Dec 10 21:48:55 2009
New Revision: 70061
Added:
pypy/branch/micronumpy/pypy/module/micronumpy/app_numarray.py
Modified:
pypy/branch/micronumpy/pypy/module/micronumpy/__init__.py
pypy/branch/micronumpy/pypy/module/micronumpy/numarray.py
pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py
pypy/branch/micronumpy/pypy/module/micronumpy/ufunc.py
Log:
Added test cases to fail and a fair amount of non-functional code.
Modified: pypy/branch/micronumpy/pypy/module/micronumpy/__init__.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/__init__.py (original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/__init__.py Thu Dec 10 21:48:55 2009
@@ -4,10 +4,14 @@
class Module(MixedModule):
applevel_name = 'numpy'
+ appleveldefs = {
+ 'array' : 'app_numarray.array',
+ }
interpleveldefs = {
'zeros' : 'numarray.zeros',
'minimum' : 'ufunc.minimum',
+ 'IntArray' : 'numarray.IntArray',
+ 'FloatArray' : 'numarray.FloatArray',
}
- appleveldefs = {}
Added: pypy/branch/micronumpy/pypy/module/micronumpy/app_numarray.py
==============================================================================
--- (empty file)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/app_numarray.py Thu Dec 10 21:48:55 2009
@@ -0,0 +1,27 @@
+types_list = [object, complex, float, int]
+
+def lowest_type(x):
+ result = object
+ for type in types_list:
+ if isinstance(x, type):
+ result = type
+ return result
+
+def lowest_common_type(xs):
+ types = [type(x) for x in xs]
+ result = int
+ for t in types:
+ if types_list.index(t) < types_list.index(result):
+ result = t
+ return result
+
+def array(xs, dtype=None):
+ import numpy
+ arrays = {
+ int: numpy.IntArray,
+ float: numpy.FloatArray,
+ #complex: ComplexNumArray,
+ }
+ #type = lowest_common_type(xs)
+ #return arrays[type](xs)
+ return numpy.zeros(len(xs), dtype=int) #FIXME
Modified: pypy/branch/micronumpy/pypy/module/micronumpy/numarray.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/numarray.py (original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/numarray.py Thu Dec 10 21:48:55 2009
@@ -1,50 +1,140 @@
-
from pypy.interpreter.baseobjspace import ObjSpace, W_Root, Wrappable
from pypy.interpreter.error import OperationError
from pypy.interpreter.typedef import TypeDef
from pypy.interpreter.gateway import interp2app, NoneNotWrapped
from pypy.rlib.debug import make_sure_not_resized
-class BaseNumArray(Wrappable):
- pass
+from pypy.rlib.objectmodel import specialize
-class NumArray(BaseNumArray):
- def __init__(self, space, dim, dtype):
- self.dim = dim
- self.space = space
- # ignore dtype for now
- self.storage = [0] * dim
- make_sure_not_resized(self.storage)
-
- def descr_getitem(self, index):
- space = self.space
- try:
- return space.wrap(self.storage[index])
- except IndexError:
- raise OperationError(space.w_IndexError,
- space.wrap("list index out of range"))
- descr_getitem.unwrap_spec = ['self', int]
+result_types = {
+ (int, int): int,
+ (int, float): float,
+ (float, int): float,
+ (float, float): float
+ }
- def descr_setitem(self, index, value):
- space = self.space
- try:
- self.storage[index] = value
- except IndexError:
- raise OperationError(space.w_IndexError,
- space.wrap("list index out of range"))
- return space.w_None
- descr_setitem.unwrap_spec = ['self', int, int]
-
- def descr_len(self):
- return self.space.wrap(len(self.storage))
- descr_len.unwrap_spec = ['self']
+class BaseNumArray(Wrappable):
+ pass
-NumArray.typedef = TypeDef(
- 'NumArray',
- __getitem__ = interp2app(NumArray.descr_getitem),
- __setitem__ = interp2app(NumArray.descr_setitem),
- __len__ = interp2app(NumArray.descr_len),
-)
+def iterable_type(space, w_xs):
+ xs = space.fixedview(w_xs)
+ type = int
+ for i in range(len(xs)):
+ type = result_types[type, xs[i]]
+ return type
+
+def int_unwrapper(space, w_x):
+ return space.int_w(space.int(w_x))
+
+def float_unwrapper(space, w_x):
+ return space.float_w(space.float(w_x))
+
+def create_numarray(type, unwrapper, name):
+ class NumArray(BaseNumArray):
+ def __init__(self, space, length, dtype):
+ #ignore dtype, irrelevant to optimized numarray implementations too
+ self.length = length
+ self.space = space
+ self.storage = [type(0.0)] * length
+ make_sure_not_resized(self.storage)
+
+
+ def _dup_size(self, type):
+ return self.__class__(space, result_types[self.type, type], self.length)
+
+ def create_scalar_op(unwrap, f):
+ def scalar_operation(self, w_x):
+ space = self.space
+ x = unwrap(w_x)
+ result = self._dup_size(type(x))
+ for i in range(self.length):
+ result[i] = f(self.storage[i], x)
+ return space.wrap(result)
+ return scalar_operation
+
+ def create_fixedview_op(unwrap, f):
+ def fixedview_operation(self, w_xs):
+ space = self.space
+
+ try:
+ xs = space.fixedview(w_xs, len(self.storage))
+ except UnpackValueError, e:
+ # w_xs is of the wrong size
+ raise OperationError(space.w_ValueError,
+ space.wrap("shape mismatch: objects cannot be broadcast to the same shape"))
+
+ result = self._dup_size(iterable_type(space, xs))
+
+ i = 0
+ for w_x in xs:
+ result[i] = f(self.storage[i], unwrap(w_x))
+ i += 1
+ return result
+ return fixedview_operation
+
+ #def mul_iterable(self, w_xs):
+ #return self.fixedview_operation(w_xs, mul)
+
+# def descr_mul(self, w_x):
+# space = self.space
+# if space.type(w_x) in [W_Int, W_Float]: #complex, long
+# try:
+# return self.mul_scalar(space.int_w(w_x))
+# except TypeError:
+# return self.mul_scalar(space.float_w(w_x))
+# else:
+# return self.mul_iterable(w_x)
+# descr_mul.unwrap_spec = ['self', W_Root]
+
+ def descr_getitem(self, index):
+ space = self.space
+ try:
+ return space.wrap(self.storage[index])
+ except IndexError:
+ raise OperationError(space.w_IndexError,
+ space.wrap("list index out of range"))
+ descr_getitem.unwrap_spec = ['self', int]
+
+ def descr_setitem(self, index, w_value):
+ space = self.space
+ try:
+ self.storage[index] = unwrapper(space, w_value)
+ except IndexError:
+ raise OperationError(space.w_IndexError,
+ space.wrap("list index out of range"))
+ return space.w_None
+ descr_setitem.unwrap_spec = ['self', int, W_Root]
+
+ def descr_len(self):
+ return self.space.wrap(len(self.storage))
+ descr_len.unwrap_spec = ['self']
+
+ def descr_init(xs): pass
+
+ NumArray.typedef = TypeDef(
+ name,
+ #__init__ = interp2app(descr_init), #FIXME
+ __getitem__ = interp2app(NumArray.descr_getitem),
+ __setitem__ = interp2app(NumArray.descr_setitem),
+ __len__ = interp2app(NumArray.descr_len),
+ )
+ return NumArray
+
+IntArray = create_numarray(int, int_unwrapper, 'IntArray')
+NumArray = IntArray # FIXME: compatibility for now
+FloatArray = create_numarray(float, float_unwrapper, 'FloatArray')
+
+arrays = {
+ int: IntArray,
+ float: FloatArray
+ }
+
+#def array(space, w_xs):
+# w_length = space.len(w_xs)
+# length = space.int_w(w_length)
+# #TODO: discover type
+# result = NumArray(space, type, length)
+#array.unwrap_spec = [ObjSpace, W_Root]
def compute_pos(space, indexes, dim):
current = 1
Modified: pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py (original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py Thu Dec 10 21:48:55 2009
@@ -4,7 +4,34 @@
class AppTestNumpy(object):
def setup_class(cls):
cls.space = gettestobjspace(usemodules=('micronumpy',))
-
+ cls.w_compare = cls.space.appexec([],
+ """():
+ def compare(a, b):
+ for x, y in zip(a, b):
+ if x != y: return False
+ return True
+ return compare""")
+
+ def create_type_test(type):
+ def test_type_array(self):
+ compare = self.compare
+ from numpy import array
+ data = [type(x) for x in xrange(4)]
+ ar = array(data)
+
+ assert compare(ar, data)
+ return test_type_array
+
+ test_int_array = create_type_test(int)
+ test_float_array = create_type_test(float)
+
+ def test_iterable_construction(self):
+ compare = self.compare
+ from numpy import array
+ ar = array(xrange(4))
+
+ assert compare(ar, xrange(4))
+
def test_zeroes(self):
from numpy import zeros
ar = zeros(3, dtype=int)
Modified: pypy/branch/micronumpy/pypy/module/micronumpy/ufunc.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/ufunc.py (original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/ufunc.py Thu Dec 10 21:48:55 2009
@@ -6,11 +6,11 @@
def minimum(space, w_a, w_b):
if not isinstance(w_a, NumArray) or not isinstance(w_b, NumArray):
raise OperationError(space.w_TypeError,
- space.wrap("expecting NumArrat object"))
- if w_a.dim != w_b.dim:
+ space.wrap("expecting NumArray object"))
+ if w_a.length != w_b.length:
raise OperationError(space.w_ValueError,
space.wrap("minimum of arrays of different length"))
- res = NumArray(space, w_a.dim, 'i')
+ res = NumArray(space, w_a.length, 'i')
for i in range(len(w_a.storage)):
one = w_a.storage[i]
two = w_b.storage[i]
More information about the Pypy-commit
mailing list