diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-08-08 07:49:10 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-08-08 07:49:10 +0000 |
commit | 3e3dd665d89496da053f76f120d504e9f1bf1699 (patch) | |
tree | 46d7ffc835856c2520958562081fdbf67a476dd4 /numpy/numarray | |
parent | 50a50441a148a9ea3fbc1629e065d88a4f28a419 (diff) | |
download | numpy-3e3dd665d89496da053f76f120d504e9f1bf1699.tar.gz |
Add numarray compatibility.
Diffstat (limited to 'numpy/numarray')
-rw-r--r-- | numpy/numarray/__init__.py | 21 | ||||
-rw-r--r-- | numpy/numarray/alter_code1.py | 2 | ||||
-rw-r--r-- | numpy/numarray/compat.py | 6 | ||||
-rw-r--r-- | numpy/numarray/functions.py | 86 | ||||
-rw-r--r-- | numpy/numarray/numclass.py | 133 | ||||
-rw-r--r-- | numpy/numarray/numerictypes.py | 56 | ||||
-rw-r--r-- | numpy/numarray/ufuncs.py | 21 |
7 files changed, 175 insertions, 150 deletions
diff --git a/numpy/numarray/__init__.py b/numpy/numarray/__init__.py index 862fe4008..e48401433 100644 --- a/numpy/numarray/__init__.py +++ b/numpy/numarray/__init__.py @@ -1,5 +1,22 @@ from util import * -from numclass import * +from numerictypes import * +from functions import * +from ufuncs import * + +import util +import numerictypes +import functions +import ufuncs +import compat __all__ = util.__all__ -__all__ += numclass.__all__ +__all__ += numerictypes.__all__ +__all__ += functions.__all__ +__all__ += ufuncs.__all__ +__all__ += compat.__all__ + +del util +del numerictypes +del functions +del ufuncs +del compat diff --git a/numpy/numarray/alter_code1.py b/numpy/numarray/alter_code1.py index 07b74ada7..259672b76 100644 --- a/numpy/numarray/alter_code1.py +++ b/numpy/numarray/alter_code1.py @@ -28,7 +28,7 @@ Makes the following changes: - self.new(type) --> empty(self.shape, type) - .repeat(r) --> .repeat(r, axis=0) - .size() --> .size - - .type() -- numarray.type(self.dtype) + - .type() -- numarray.typefrom(self) - .typecode() --> .dtype.char - .stddev() --> .std() - .togglebyteorder() --> self.dtype=self.dtype.newbyteorder() diff --git a/numpy/numarray/compat.py b/numpy/numarray/compat.py new file mode 100644 index 000000000..c9fd3ef86 --- /dev/null +++ b/numpy/numarray/compat.py @@ -0,0 +1,6 @@ + +__all__ = ['NewAxis'] + +from numpy import newaxis + +NewAxis = newaxis diff --git a/numpy/numarray/functions.py b/numpy/numarray/functions.py new file mode 100644 index 000000000..dbea74f81 --- /dev/null +++ b/numpy/numarray/functions.py @@ -0,0 +1,86 @@ + +# missing Numarray defined names (in from numarray import *) +##__all__ = ['ArrayType', 'CLIP', 'ClassicUnpickler', 'Complex32_fromtype', +## 'Complex64_fromtype', 'ComplexArray', 'EarlyEOFError', 'Error', +## 'FileSeekWarning', 'MAX_ALIGN', 'MAX_INT_SIZE', 'MAX_LINE_WIDTH', +## 'MathDomainError', 'NDArray', 'NewArray', 'NewAxis', 'NumArray', +## 'NumError', 'NumOverflowError', 'PRECISION', 'Py2NumType', +## 'PyINT_TYPES', 'PyLevel2Type', 'PyNUMERIC_TYPES', 'PyREAL_TYPES', +## 'RAISE', 'SLOPPY', 'STRICT', 'SUPPRESS_SMALL', 'SizeMismatchError', +## 'SizeMismatchWarning', 'SuitableBuffer', 'USING_BLAS', +## 'UnderflowError', 'UsesOpPriority', 'WARN', 'WRAP', 'all', +## 'allclose', 'alltrue', 'and_', 'any', 'arange', 'argmax', +## 'argmin', 'argsort', 'around', 'array2list', 'array_equal', +## 'array_equiv', 'array_repr', 'array_str', 'arrayprint', +## 'arrayrange', 'average', 'choose', 'clip', +## 'codegenerator', 'compress', 'concatenate', 'conjugate', +## 'copy', 'copy_reg', 'diagonal', 'divide_remainder', +## 'dotblas', 'e', 'explicit_type', 'flush_caches', 'fromfile', +## 'fromfunction', 'fromlist', 'fromstring', 'generic', +## 'genericCoercions', 'genericPromotionExclusions', 'genericTypeRank', +## 'getShape', 'getTypeObject', 'handleError', 'identity', 'indices', +## 'info', 'innerproduct', 'inputarray', 'isBigEndian', +## 'kroneckerproduct', 'lexsort', 'libnumarray', 'libnumeric', +## 'load', 'make_ufuncs', 'math', 'memory', +## 'numarrayall', 'numarraycore', 'numerictypes', 'numinclude', +## 'operator', 'os', 'outerproduct', 'pi', 'put', 'putmask', +## 'pythonTypeMap', 'pythonTypeRank', 'rank', 'repeat', +## 'reshape', 'resize', 'round', 'safethread', 'save', 'scalarTypeMap', +## 'scalarTypes', 'searchsorted', 'session', 'shape', 'sign', 'size', +## 'sometrue', 'sort', 'swapaxes', 'sys', 'take', 'tcode', +## 'tensormultiply', 'tname', 'trace', 'transpose', 'typeDict', +## 'typecode', 'typecodes', 'typeconv', 'types', 'ufunc', +## 'ufuncFactory', 'value', ] + + +__all__ = ['asarray', 'ones', 'zeros', 'array', 'where'] +__all__ += ['vdot', 'dot', 'matrixmultiply', 'ravel', 'indices', + 'arange', 'concatenate'] + +from numpy import dot as matrixmultiply, dot, vdot, ravel + +def array(sequence=None, typecode=None, copy=1, savespace=0, + type=None, shape=None, dtype=None): + dtype = type2dtype(typecode, type, dtype) + if sequence is None: + if shape is None: + return None + if dtype is None: + dtype = 'l' + return N.empty(shape, dtype) + arr = N.array(sequence, dtype, copy=copy) + if shape is not None: + arr.shape = shape + return arr + +def asarray(seq, type=None, typecode=None, dtype=None): + if seq is None: + return None + dtype = type2dtype(typecode, type, dtype) + return N.array(seq, dtype, copy=0) + +def ones(shape, type=None, typecode=None, dtype=None): + dtype = type2dtype(typecode, type, dtype) + return N.ones(shape, dtype) + +def zeros(shape, type=None, typecode=None, dtype=None): + dtype = type2dtype(typecode, type, dtype) + return N.zeros(shape, dtype) + +def where(condition, x=None, y=None, out=None): + if x is None and y is None: + arr = N.where(condition) + else: + arr = N.where(condition, x, y) + if out is not None: + out[...] = arr + return out + return arr + +def indices(shape, type=None): + return N.indices(shape, type) + +def arange(a1, a2=None, stride=1, type=None, shape=None, + typecode=None, dtype=None): + dtype = type2dtype(typecode, type, dtype) + return N.arange(a1, a2, stride, dtype) diff --git a/numpy/numarray/numclass.py b/numpy/numarray/numclass.py deleted file mode 100644 index 979efa9e0..000000000 --- a/numpy/numarray/numclass.py +++ /dev/null @@ -1,133 +0,0 @@ -from numpy.core.multiarray import ndarray -import numerictypes as _nt -import numpy as N -import sys as _sys - -__all__ = ['NumArray'] - -class NumArray(ndarray): - def __new__(klass, shape=None, type=None, buffer=None, - byteoffset=0, bytestride=None, byteorder=_sys.byteorder, - aligned=1, real=None, imag=None): - - type = _nt.getType(type) - dtype = N.dtype(type._dtype) - if byteorder in ['little', 'big']: - if byteorder is not _sys.byteorder: - dtype = dtype.newbyteorder() - else: - raise ValueError("byteorder must be 'little' or 'big'") - - if buffer is None: - self = ndarray.__new__(klass, shape, dtype) - else: - self = ndarray.__new__(klass, shape, dtype, buffer=buffer, - offset=byteoffset, strides=bytestride) - - self._type = type - - if real is not None: - self.real = real - - if imag is not None: - self.imag = imag - - self._byteorder = byteorder - - return self - - def argmax(self, axis=-1): - return ndarray.argmax(self, axis) - - def argmin(self, axis=-1): - return ndarray.argmax(self, axis) - - def argsort(self, axis=-1, kind='quicksort'): - return ndarray.argmax(self, axis, kind) - - def astype(self, type=None): - return self.astype(_getdtype(type)) - - def byteswap(self): - ndarray.byteswap(self, True) - - def byteswapped(self): - return ndarray.byteswap(self, False) - - def getdtypechar(self): - return self.dtype.char - - def getimag(self): - return self.imag - - getimaginary = getimag - - imaginary = property(getimaginary, None, "") - - def getreal(self): - return self.real - - def is_c_array(self): - return self.dtype.isnative and self.flags.carray - - def is_f_array(self): - return self.dtype.isnative and self.flags.farray - - def is_fortran_contiguous(self): - return self.flags.contiguous - - def new(self, type=None): - if type is not None: - dtype = _getdtype(type) - return N.empty(self.shape, dtype) - else: - return N.empty_like(self) - - def setimag(self, value): - self.imag = value - - setimaginary = setimag - - def setreal(self, value): - self.real = value - - def sinfo(self): - self.info() - - def sort(self, axis=-1, kind='quicksort'): - ndarray.sort(self, axis, kind) - - def spacesaver(self): - return False - - def stddev(self): - return self.std() - - def sum(self, type=None): - dtype = _getdtype(type) - return ndarray.sum(self, dtype=dtype) - - def togglebyteorder(self): - self.dtype = self.dtype.newbyteorder() - - def type(self): - return self._type - - def typecode(self): - return _numtypecode[self.dtype.char] - - dtypechar = property(getdtypechar, None, "") - - def info(self): - print "class: ", self.__class__ - print "shape: ", self.shape - print "strides: ", self.strides - print "byteoffset: 0" - print "bytestride: ", self.strides[0] - print "itemsize: ", self.itemsize - print "aligned: ", self.flags.isaligned - print "contiguous: ", self.flags.contiguous - print "buffer: ", self.data - print "data pointer:", self._as_paramater_ - print "byteorder: ", self._byteorder - print "byteswap: ", not self.dtype.isnative diff --git a/numpy/numarray/numerictypes.py b/numpy/numarray/numerictypes.py index b573e6c29..549fb8ca2 100644 --- a/numpy/numarray/numerictypes.py +++ b/numpy/numarray/numerictypes.py @@ -27,13 +27,25 @@ Exported symbols include: $Id: numerictypes.py,v 1.55 2005/12/01 16:22:03 jaytmiller Exp $ """ +__all__ = ['NumericType','HasUInt64','typeDict','IsType', + 'BooleanType', 'SignedType', 'UnsignedType', 'IntegralType', + 'SignedIntegralType', 'UnsignedIntegralType', 'FloatingType', + 'ComplexType', 'AnyType', 'ObjectType', 'Any', 'Object', + 'Bool', 'Int8', 'Int16', 'Int32', 'Int64', 'Float32', + 'Float64', 'UInt8', 'UInt16', 'UInt32', 'UInt64', + 'Complex32', 'Complex64', 'Byte', 'Short', 'Int','Long', + 'Float', 'Complex', 'genericTypeRank', 'pythonTypeRank', + 'pythonTypeMap', 'scalarTypeMap', 'genericCoercions', + 'typecodes', 'genericPromotionExclusions','MaximumType', + 'getType','scalarTypes', 'typefrom'] + MAX_ALIGN = 8 MAX_INT_SIZE = 8 import numpy LP64 = numpy.intp(0).itemsize == 8 -HasUInt64 = 0 +HasUInt64 = 1 try: numpy.int64(0) except: @@ -182,6 +194,24 @@ UInt64 = UnsignedIntegralType("UInt64", 8, 0, _tUInt64) Complex32 = ComplexType("Complex32", 8, complex(0.0), _tComplex32) Complex64 = ComplexType("Complex64", 16, complex(0.0), _tComplex64) +Object.dtype = 'O' +Bool.dtype = '?' +Int8.dtype = 'i1' +Int16.dtype = 'i2' +Int32.dtype = 'i4' +Int64.dtype = 'i8' + +UInt8.dtype = 'u1' +UInt16.dtype = 'u2' +UInt32.dtype = 'u4' +UInt64.dtype = 'u8' + +Float32.dtype = 'f4' +Float64.dtype = 'f8' + +Complex32.dtype = 'c8' +Complex64.dtype = 'c16' + # Aliases Byte = _register("Byte", Int8) @@ -192,10 +222,12 @@ if LP64: if HasUInt64: _register("ULong", UInt64) MaybeLong = _register("MaybeLong", Int64) + __all__.append('MaybeLong') else: Long = _register("Long", Int32) _register("ULong", UInt32) - MaybeLong = _register("MaybeLong", Int32) + MaybeLong = _register("MaybeLong", Int32) + __all__.append('MaybeLong') _register("UByte", UInt8) @@ -280,10 +312,7 @@ else: 'Int32', 'UInt32', 'Int64', 'UInt64', 'Float32','Float64', 'Complex32', 'Complex64', 'Object'] -if _sys.version_info >= (2,3,0): - pythonTypeRank = [ bool, int, long, float, complex ] -else: - pythonTypeRank = [ int, long, float, complex ] +pythonTypeRank = [ bool, int, long, float, complex ] # The next line is not platform independent XXX Needs to be generalized if not LP64: @@ -311,9 +340,8 @@ else: float:"Float64", complex:"Complex64"} -if _sys.version_info >= (2,3,0): - pythonTypeMap.update({bool:("Bool","bool") }) - scalarTypeMap.update({bool:"Bool"}) +pythonTypeMap.update({bool:("Bool","bool") }) +scalarTypeMap.update({bool:"Bool"}) # Generate coercion matrix @@ -481,10 +509,7 @@ def getType(type): except KeyError: raise TypeError("Not a numeric type") -if _sys.version_info >= (2,3): - scalarTypes = (bool,int,long,float,complex) -else: - scalarTypes = (int,long,float,complex) +scalarTypes = (bool,int,long,float,complex) _scipy_dtypechar = { Int8 : 'b', @@ -505,4 +530,7 @@ _scipy_dtypechar_inverse = {} for key,value in _scipy_dtypechar.items(): _scipy_dtypechar_inverse[value] = key - + +def typefrom(obj): + return _scipy_dtypechar_inverse[obj.dtype.char] + diff --git a/numpy/numarray/ufuncs.py b/numpy/numarray/ufuncs.py new file mode 100644 index 000000000..b6e12cfe1 --- /dev/null +++ b/numpy/numarray/ufuncs.py @@ -0,0 +1,21 @@ + +__all__ = ['abs', 'absolute', 'add', 'arccos', 'arccosh', 'arcsin', 'arcsinh', + 'arctan', 'arctan2', 'arctanh', 'bitwise_and', 'bitwise_not', + 'bitwise_or', 'bitwise_xor', 'ceil', 'cos', 'cosh', 'cumproduct', + 'cumsum', 'divide', 'equal', 'exp', 'fabs', 'floor', 'floor_divide', + 'fmod', 'greater', 'greater_equal', 'hypot', 'ieeemask', 'isnan', + 'less', 'less_equal', 'log', 'log10', 'logical_and', 'logical_not', + 'logical_or', 'logical_xor', 'lshift', 'maximum', 'minimum', + 'minus', 'multiply', 'negative', 'nonzero', 'not_equal', + 'power', 'product', 'remainder', 'rshift', 'sin', 'sinh', 'sqrt', + 'subtract', 'sum', 'tan', 'tanh', 'true_divide'] + +from numpy import abs, absolute, add, arccos, arccosh, arcsin, arcsinh, \ + arctan, arctan2, arctanh, bitwise_and, bitwise_not, bitwise_or, \ + bitwise_xor, ceil, cos, cosh, cumproduct, cumsum, divide, equal, \ + exp, fabs, floor, floor_divide, fmod, greater, greater_equal, hypot, \ + ieeemask, isnan, less, less_equal, log, log10, logical_and, \ + logical_not, logical_or, logical_xor, lshift, maximum, minimum, \ + minus, multiply, negative, nonzero, not_equal, power, product, \ + remainder, rshift, sin, sinh, sqrt, subtract, sum, tan, tanh, \ + true_divide |