[pypy-svn] r18646 - pypy/dist/pypy/module/Numeric
ludal at codespeak.net
ludal at codespeak.net
Sat Oct 15 18:15:29 CEST 2005
Author: ludal
Date: Sat Oct 15 18:15:27 2005
New Revision: 18646
Modified:
pypy/dist/pypy/module/Numeric/__init__.py
pypy/dist/pypy/module/Numeric/app_numeric.py
pypy/dist/pypy/module/Numeric/interp_numeric.py
Log:
(ludal,aft)
finished merging scalar, and multi indices versions
array function
Modified: pypy/dist/pypy/module/Numeric/__init__.py
==============================================================================
--- pypy/dist/pypy/module/Numeric/__init__.py (original)
+++ pypy/dist/pypy/module/Numeric/__init__.py Sat Oct 15 18:15:27 2005
@@ -13,6 +13,7 @@
# 'array' : 'interp_numeric.w_array',
'zeros' : 'interp_numeric.w_zeros',
'array' : 'interp_numeric.array',
+ 'ArrayType' : 'space.gettypeobject(interp_numeric.W_Array.typedef)',
'TOWER_TYPES' : 'space.wrap(interp_numeric.TOWER_TYPES)',
'TOWER_TYPES_VALUES' :'space.wrap(interp_numeric.TOWER_TYPES_VALUES)'
}
Modified: pypy/dist/pypy/module/Numeric/app_numeric.py
==============================================================================
--- pypy/dist/pypy/module/Numeric/app_numeric.py (original)
+++ pypy/dist/pypy/module/Numeric/app_numeric.py Sat Oct 15 18:15:27 2005
@@ -1,6 +1,6 @@
-from Numeric import zeros,array
+from Numeric import zeros,array,ArrayType
from Numeric import Float,TOWER_TYPES,TOWER_TYPES_VALUES
@@ -21,9 +21,10 @@
"""
#first we check the really simple, empty or minimal arrays
+print "Checking scalars"
assert (0,)==array(()).shape
assert ()==array((1)).shape
-assert array(()).isArray() and array((1)).isArray()
+assert isinstance( array(()), ArrayType) and isinstance( array((1)), ArrayType )
#next we check the typecodes on these small examples
assert 'l'==array(()).typecode()
@@ -31,13 +32,15 @@
assert 'd'==array((1.0)).typecode()
#we are not supporting complex numbers or any other objects yet
-assertRaises(lambda :array((1j)),ValueError)
-assertRaises(lambda :array((1j,)),ValueError)
-assertRaises(lambda :array(('a')),ValueError)
+print "checking unsupported types"
+assertRaises(lambda :array((1j)),TypeError)
+assertRaises(lambda :array((1j,)),TypeError)
+assertRaises(lambda :array(('a')),TypeError)
#now check accessing of values on empty array, and a scalar
#assertRaises(lambda :array(())[0],IndexError
+print "DONE"
Modified: pypy/dist/pypy/module/Numeric/interp_numeric.py
==============================================================================
--- pypy/dist/pypy/module/Numeric/interp_numeric.py (original)
+++ pypy/dist/pypy/module/Numeric/interp_numeric.py Sat Oct 15 18:15:27 2005
@@ -34,7 +34,9 @@
n = 1
for d in dims:
n *= d
- assert d>0
+ assert d>=0
+ if n==0:
+ return 1
return n
def is_integer( space, w_obj ):
@@ -121,6 +123,9 @@
pass
return self.set_slice( space, multi_idx, w_value )
+ def descr_typecode(self,space):
+ return self.typecode(space)
+
def fget_shape( space, self ):
return space.newtuple( [ self.space.wrap( i ) for i in self.dims ] )
@@ -133,6 +138,10 @@
idx += self.strides[i]*idx_tuple[i]
return idx
+ def typecode(self,space):
+ raise OperationError( space.w_NotImplemented,
+ space.wrap("calling abstract base class" ))
+
class W_Array_Float(W_Array):
@@ -181,128 +190,152 @@
array = W_Array_Float( space, dims, strides, self.storage )
return space.wrap(array)
-
-descr___getitem__ = interp2app( W_Array.descr___getitem__, unwrap_spec=['self', ObjSpace, W_Root ] )
-descr___setitem__ = interp2app( W_Array.descr___setitem__, unwrap_spec=['self', ObjSpace, W_Root, W_Root ] )
-
-W_Array.typedef = TypeDef("array",
- shape = GetSetProperty( W_Array.fget_shape, cls=W_Array),
- __getitem__ = descr___getitem__,
- __setitem__ = descr___setitem__,
- )
-
-W_Array_Float.typedef = TypeDef("array_float", W_Array.typedef,
- )
-
-def w_zeros( space, w_dim_tuple, type_str ):
- dims = []
- for w_int in space.unpackiterable(w_dim_tuple):
- dims.append( space.unwrap( w_int ) )
- if type_str == 'd':
- return space.wrap(W_Array_Float( space, dims ))
- raise OperationError( space.w_ValueError, space.wrap('Unknown type code') )
-
-w_zeros.unwrap_spec = [ ObjSpace, W_Root, str ]
-
-
-
-
-"""
-"""
-class W_NumericArray(Wrappable):
-
- def __init__(self, space, dims ,value=None ):
- self.space = space
- self.dims = dims
- self.value= value
-
- def check_space_true(self, space, w_index):
- if not space.is_true(space.isinstance( w_index, space.w_int )):
- raise NotImplementedError
- idx = space.unwrap( w_index )
- assert isinstance( idx, int )
- return idx
-
- def isArray(self,space):
- return self.space.wrap(True)
-
def typecode(self,space):
- code='l'
- if isinstance(self.value,float):
- code='d'
- return self.space.wrap(code)
-
- def descr___getitem__( self, space, w_index ):
- return self.get_single_item( space, [ self.check_space_true( space, w_index)])
-
- def descr___setitem__( self, space, w_index, w_value ):
- return self.set_single_item( space, [ self.check_space_true( space, w_index) ], w_value )
+ return space.wrap('d')
- def fget_shape( space, self ):
- return space.newtuple( [ self.space.wrap( i ) for i in self.dims ] )
+class W_Array_Integer(W_Array):
- def fset_shape( space, self, w_tuple ):
- pass
-
- def get_array_offset( self, idx_tuple ):
- if len(idx_tuple)>len(self.dims):
- # TODO raise OperationError
- raise RuntimeError
- idx = 0
- for i in range(len(idx_tuple)):
- idx += self.strides[i]*idx_tuple[i]
- return idx
+ def __init__(self, space, dims, strides=None, storage=None ):
+ W_Array.__init__(self, space, dims, strides )
+ self.storage = []
+ if storage is not None:
+ assert isinstance(storage, list)
+ # TODO return proper exception here
+ # assert len(storage)==storage_size ### can't check that because of slicing
+ assert isinstance(storage[0], int)
+ self.storage = storage
+ else:
+ storage_size = get_storage_size(dims)
+ self.storage = [0]*storage_size
def get_single_item( self, space, idx_tuple ):
if len(idx_tuple)!=len(self.dims):
# TODO raise OperationError or remove this and make it a pre-condition
raise RuntimeError
+
idx = self.get_array_offset( idx_tuple )
return space.wrap( self.storage[idx] )
def set_single_item( self, space, idx_tuple, w_value ):
idx = self.get_array_offset( idx_tuple )
- value = space.float_w( w_value )
+ value = space.int_w( w_value )
self.storage[idx] = value
+ def get_slice( self, space, multi_idx ):
+ # compute dim of extracted array
+ dims = []
+ strides = []
+ last_stride = 1
+ offset = self.base_offset
+ for i in range(len(multi_idx)):
+ idx = multi_idx[i]
+ if isinstance( idx, SliceIndex):
+ start, stop, step = idx.indices( self.dims[i] )
+ dim = (stop-start)//step
+ stride = self.strides[i]*step
+ dims.append( dim )
+ strides.append( step )
+ elif isinstance( idx, IntIndex ):
+ offset+= idx.index*self.strides[i]
+ array = W_Array_Float( space, dims, strides, self.storage )
+ return space.wrap(array)
+ def typecode(self,space):
+ return space.wrap('l')
-descr___getitem__ = interp2app( W_NumericArray.descr___getitem__, unwrap_spec=['self', ObjSpace, W_Root ] )
-descr___setitem__ = interp2app( W_NumericArray.descr___setitem__, unwrap_spec=['self', ObjSpace, W_Root, W_Root ] )
-
-isArray = interp2app( W_NumericArray.isArray, unwrap_spec=['self', ObjSpace ] )
-typecode = interp2app( W_NumericArray.typecode, unwrap_spec=['self', ObjSpace ] )
-
-
+descr___getitem__ = interp2app( W_Array.descr___getitem__, unwrap_spec=['self', ObjSpace, W_Root ] )
+descr___setitem__ = interp2app( W_Array.descr___setitem__, unwrap_spec=['self', ObjSpace, W_Root, W_Root ] )
+descr_typecode = interp2app( W_Array.descr_typecode, unwrap_spec=['self', ObjSpace ] )
-W_NumericArray.typedef = TypeDef("W_NumericArray",
- shape = GetSetProperty( W_NumericArray.fget_shape, cls=W_NumericArray),
- isArray = isArray,
- typecode= typecode,
+W_Array.typedef = TypeDef("array",
+ shape = GetSetProperty( W_Array.fget_shape, cls=W_Array),
__getitem__ = descr___getitem__,
__setitem__ = descr___setitem__,
+ typecode = descr_typecode,
)
+#W_Array_Float.typedef = TypeDef("array", W_Array.typedef,)
+#W_Array_Integer.typedef = TypeDef("array", W_Array.typedef, )
+
+
+def w_zeros( space, w_dim_tuple, type_str ):
+ dims = []
+ for w_int in space.unpackiterable(w_dim_tuple):
+ dims.append( space.unwrap( w_int ) )
+ if type_str == 'd':
+ return space.wrap(W_Array_Float( space, dims ))
+ raise OperationError( space.w_ValueError, space.wrap('Unknown type code') )
+
+w_zeros.unwrap_spec = [ ObjSpace, W_Root, str ]
+
+
TOWER_TYPES_VALUES=[(int,1),(float,1.0),(complex,2.0+3j)] #we will work with these types to start with
TOWER_TYPES_VALUES=TOWER_TYPES_VALUES[0:2] #cannot unpack complex values yet at interp level.
TOWER_TYPES=[typ for typ,val in TOWER_TYPES_VALUES]
+
+
def OpError(space,w_arg):
- raise OperationError( space.w_ValueError,space.wrap('Cannot unwrap this <%s>'%str(w_arg)))
+ raise OperationError( space.w_TypeError,space.wrap('Cannot unwrap this <%s>'%str(w_arg)))
+
+
+def int_array_from_iterable( space, w_it ):
+ N = space.int_w(space.len( w_it ))
+ storage = N*[0]
+ i = 0
+ for w_obj in space.unpackiterable( w_it ):
+ storage[i] = space.int_w( w_it )
+ i+=1
+
+def float_array_from_iterable( space, w_it ):
+ N = space.int_w(space.len( w_it ))
+ storage = N*[0.]
+ i = 0
+ for w_obj in space.unpackiterable( w_it ):
+ storage[i] = space.float_w( w_it )
+ i+=1
def array( space, w_arg):
- try:
- arg=space.unwrap( w_arg)
- except:
- OpError(space,w_arg)
- if arg in ((),[]):
- return W_NumericArray(space,(0,))
- if type(arg) not in (int,float):
- OpError(space,w_arg)
+ TYPES = "ldD"
+ if (space.is_true( space.isinstance( w_arg, space.w_list ) ) or
+ space.is_true( space.isinstance( w_arg, space.w_tuple ) ) ):
+ if not space.is_true( w_arg ):
+ return W_Array_Integer(space,[0,])
+ ty = -1
+ for w_obj in space.unpackiterable( w_arg ):
+ if ty< 0 and space.is_true( space.isinstance( w_arg, space.w_int ) ):
+ ty = 0
+ if ty< 1 and space.is_true( space.isinstance( w_arg, space.w_float ) ):
+ ty = 1
+## elif ty<2 and space.is_true( space.isinstance( w_arg, space.w_complex ) ):
+## ty = 2
+ else:
+ raise OperationError( space.w_TypeError,
+ space.wrap("Only int, float and complex currently supported") )
+ if ty == 2:
+ break
+ if ty==0:
+ return int_array_from_iterable( space, w_arg )
+ elif ty==1:
+ return float_array_from_iterable( space, w_arg )
+ elif ty==2:
+ raise OperationError( space.w_NotImplementedError,
+ space.wrap("complex array not implemented") )
+ else:
+ raise OperationError( space.w_TypeError,
+ space.wrap("Unknown array type") )
+
+ elif space.is_true( space.isinstance( w_arg, space.w_int ) ):
+ ivalue = space.int_w( w_arg )
+ return W_Array_Integer(space, [], storage=[ivalue])
+ elif space.is_true( space.isinstance( w_arg, space.w_float ) ):
+ fvalue = space.float_w( w_arg )
+ return W_Array_Float(space, [], storage=[fvalue])
else:
- return W_NumericArray(space,(),value=arg)
+ OpError(space,w_arg)
+
array.unwrap_spec = [ ObjSpace, W_Root ]
More information about the Pypy-commit
mailing list