[Numpy-svn] r2943 - in trunk/numpy: . lib numarray

numpy-svn at scipy.org numpy-svn at scipy.org
Wed Aug 2 11:38:05 CDT 2006


Author: oliphant
Date: 2006-08-02 11:37:59 -0500 (Wed, 02 Aug 2006)
New Revision: 2943

Added:
   trunk/numpy/numarray/numclass.py
   trunk/numpy/numarray/numerictypes.py
Modified:
   trunk/numpy/lib/twodim_base.py
   trunk/numpy/matlib.py
   trunk/numpy/numarray/__init__.py
Log:
Add diagflat.  Begin to add numarray compatibility

Modified: trunk/numpy/lib/twodim_base.py
===================================================================
--- trunk/numpy/lib/twodim_base.py	2006-08-01 20:23:15 UTC (rev 2942)
+++ trunk/numpy/lib/twodim_base.py	2006-08-02 16:37:59 UTC (rev 2943)
@@ -2,7 +2,7 @@
 
 """
 
-__all__ = ['diag','eye','fliplr','flipud','rot90','tri','triu','tril',
+__all__ = ['diag','diagflat','eye','fliplr','flipud','rot90','tri','triu','tril',
            'vander','histogram2d']
 
 from numpy.core.numeric import asanyarray, int_, equal, subtract, arange, \
@@ -81,7 +81,26 @@
     else:
         raise ValueError, "Input must be 1- or 2-d."
 
-
+def diagflat(v,k=0):
+    try:
+        wrap = v.__array_wrap__
+    except AttributeError:
+        wrap = None
+    v = asarray(v).ravel()
+    s = len(v)
+    n = s + abs(k)
+    res = zeros((n,n), v.dtype)
+    if (k>=0):
+        i = arange(0,n-k)
+        fi = i+k+i*n
+    else:
+        i = arange(0,n+k)
+        fi = i+(i-k)*n
+    res.flat[fi] = v
+    if not wrap:
+        return res
+    return wrap(res)
+    
 def tri(N, M=None, k=0, dtype=float):
     """ returns a N-by-M array where all the diagonals starting from
         lower left corner up to the k-th are all ones.

Modified: trunk/numpy/matlib.py
===================================================================
--- trunk/numpy/matlib.py	2006-08-01 20:23:15 UTC (rev 2942)
+++ trunk/numpy/matlib.py	2006-08-02 16:37:59 UTC (rev 2943)
@@ -47,3 +47,5 @@
     if isinstance(args[0], tuple):
        args = args[0]
     return asmatrix(N.random.rand(*args))
+
+    

Modified: trunk/numpy/numarray/__init__.py
===================================================================
--- trunk/numpy/numarray/__init__.py	2006-08-01 20:23:15 UTC (rev 2942)
+++ trunk/numpy/numarray/__init__.py	2006-08-02 16:37:59 UTC (rev 2943)
@@ -1,2 +1,5 @@
 from util import *
+from numclass import *
+
 __all__ = util.__all__
+__all__ += numclass.__all__

Added: trunk/numpy/numarray/numclass.py
===================================================================
--- trunk/numpy/numarray/numclass.py	2006-08-01 20:23:15 UTC (rev 2942)
+++ trunk/numpy/numarray/numclass.py	2006-08-02 16:37:59 UTC (rev 2943)
@@ -0,0 +1,133 @@
+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

Added: trunk/numpy/numarray/numerictypes.py
===================================================================
--- trunk/numpy/numarray/numerictypes.py	2006-08-01 20:23:15 UTC (rev 2942)
+++ trunk/numpy/numarray/numerictypes.py	2006-08-02 16:37:59 UTC (rev 2943)
@@ -0,0 +1,508 @@
+"""numerictypes: Define the numeric type objects
+
+This module is designed so 'from numerictypes import *' is safe.
+Exported symbols include:
+
+  Dictionary with all registered number types (including aliases):
+    typeDict
+
+  Numeric type objects:
+    Bool
+    Int8 Int16 Int32 Int64
+    UInt8 UInt16 UInt32 UInt64
+    Float32 Double64
+    Complex32 Complex64
+
+  Numeric type classes:
+    NumericType
+      BooleanType
+      SignedType
+      UnsignedType
+      IntegralType
+        SignedIntegralType
+        UnsignedIntegralType
+      FloatingType
+      ComplexType
+
+$Id: numerictypes.py,v 1.55 2005/12/01 16:22:03 jaytmiller Exp $
+"""
+
+MAX_ALIGN = 8
+MAX_INT_SIZE = 8
+
+import numpy
+LP64 = numpy.intp(0).itemsize == 8
+
+HasUInt64 = 0
+try:
+    numpy.int64(0)
+except:
+    HasUInt64 = 0
+
+#from typeconv import typeConverters as _typeConverters
+#import numinclude
+#from _numerictype import _numerictype, typeDict
+import types as _types
+import copy as _copy
+import sys as _sys
+
+# Enumeration of numarray type codes
+typeDict = {}
+
+_tAny       = 0
+_tBool      = 1
+_tInt8      = 2
+_tUInt8     = 3
+_tInt16     = 4
+_tUInt16    = 5
+_tInt32     = 6
+_tUInt32    = 7
+_tInt64     = 8
+_tUInt64    = 9
+_tFloat32   = 10
+_tFloat64   = 11
+_tComplex32 = 12
+_tComplex64 = 13
+_tObject    = 14
+
+def IsType(rep):
+    """Determines whether the given object or string, 'rep', represents
+    a numarray type."""
+    return isinstance(rep, NumericType) or typeDict.has_key(rep)
+
+def _register(name, type, force=0):
+    """Register the type object.  Raise an exception if it is already registered
+    unless force is true.
+    """
+    if typeDict.has_key(name) and not force:
+        raise ValueError("Type %s has already been registered" % name)
+    typeDict[name] = type
+    return type
+
+
+class NumericType(object):
+    """Numeric type class
+
+    Used both as a type identification and the repository of
+    characteristics and conversion functions.
+    """
+    def __new__(type, name, bytes, default, typeno):
+        """__new__() implements a 'quasi-singleton pattern because attempts
+        to create duplicate types return the first created instance of that
+        particular type parameterization,  i.e. the second time you try to
+        create "Int32",  you get the original Int32, not a new one.
+        """
+        if typeDict.has_key(name):
+            self = typeDict[name]
+            if self.bytes != bytes or self.default != default or \
+                   self.typeno != typeno:
+                raise ValueError("Redeclaration of existing NumericType "\
+                                 "with different parameters.")
+            return self
+        else:
+            self = object.__new__(type)
+            self.name = "no name"
+            self.bytes = None
+            self.default = None
+            self.typeno = -1
+            return self
+            
+    def __init__(self, name, bytes, default, typeno):
+        if not isinstance(name, str):
+            raise TypeError("name must be a string")
+        self.name = name
+        self.bytes = bytes
+        self.default = default
+        self.typeno = typeno
+        self._conv = None
+        _register(self.name, self)
+
+    def __getnewargs__(self):
+        """support the pickling protocol."""
+        return (self.name, self.bytes, self.default, self.typeno)
+
+    def __getstate__(self):
+        """support pickling protocol... no __setstate__ required."""
+        False
+
+class BooleanType(NumericType):
+    pass
+
+class SignedType:
+    """Marker class used for signed type check"""
+    pass
+
+class UnsignedType:
+    """Marker class used for unsigned type check"""
+    pass
+
+class IntegralType(NumericType):
+    pass
+
+class SignedIntegralType(IntegralType, SignedType):
+    pass
+
+class UnsignedIntegralType(IntegralType, UnsignedType):
+    pass
+
+class FloatingType(NumericType):
+    pass
+        
+class ComplexType(NumericType):
+    pass
+
+class AnyType(NumericType):
+    pass
+
+class ObjectType(NumericType):
+    pass
+
+# C-API Type Any
+
+Any = AnyType("Any", None, None, _tAny)
+
+Object = ObjectType("Object", None, None, _tObject)
+    
+# Numeric Types:
+
+Bool  = BooleanType("Bool", 1, 0, _tBool)
+Int8  = SignedIntegralType( "Int8", 1, 0, _tInt8)
+Int16 = SignedIntegralType("Int16", 2, 0, _tInt16)
+Int32 = SignedIntegralType("Int32", 4, 0, _tInt32)
+Int64 = SignedIntegralType("Int64", 8, 0, _tInt64)
+    
+Float32  = FloatingType("Float32", 4, 0.0, _tFloat32)
+Float64  = FloatingType("Float64", 8, 0.0, _tFloat64)
+
+UInt8  = UnsignedIntegralType( "UInt8", 1, 0, _tUInt8)
+UInt16 = UnsignedIntegralType("UInt16", 2, 0, _tUInt16)
+UInt32 = UnsignedIntegralType("UInt32", 4, 0, _tUInt32)
+UInt64 = UnsignedIntegralType("UInt64", 8, 0, _tUInt64)
+
+Complex32  = ComplexType("Complex32", 8,  complex(0.0), _tComplex32)
+Complex64  = ComplexType("Complex64", 16, complex(0.0), _tComplex64)
+
+# Aliases
+
+Byte = _register("Byte",   Int8)
+Short = _register("Short",  Int16)
+Int = _register("Int",    Int32)
+if LP64:
+    Long = _register("Long", Int64)
+    if HasUInt64:
+        _register("ULong",  UInt64)
+        MaybeLong = _register("MaybeLong", Int64)
+else:
+    Long = _register("Long", Int32)
+    _register("ULong", UInt32)
+    MaybeLong = _register("MaybeLong", Int32)    
+    
+
+_register("UByte",  UInt8)
+_register("UShort", UInt16)
+_register("UInt",   UInt32)
+Float = _register("Float",  Float64)
+Complex = _register("Complex",  Complex64)
+
+# short forms
+
+_register("b1", Bool)
+_register("u1", UInt8)
+_register("u2", UInt16)
+_register("u4", UInt32)
+_register("i1", Int8)
+_register("i2", Int16)
+_register("i4", Int32)
+
+_register("i8", Int64)
+if HasUInt64:
+    _register("u8", UInt64)
+    
+_register("f4", Float32)
+_register("f8", Float64)
+_register("c8", Complex32)
+_register("c16", Complex64)
+
+# NumPy forms
+
+_register("1", Int8)
+_register("B", Bool)
+_register("c", Int8)
+_register("b", UInt8)
+_register("s", Int16)
+_register("w", UInt16)
+_register("i", Int32)
+_register("N", Int64)
+_register("u", UInt32)
+_register("U", UInt64)
+
+if LP64:
+    _register("l", Int64)
+else:
+    _register("l", Int32)
+    
+_register("d", Float64)
+_register("f", Float32)
+_register("D", Complex64)
+_register("F", Complex32)
+
+# scipy.base forms
+
+def _scipy_alias(scipy_type, numarray_type):
+    _register(scipy_type, eval(numarray_type))
+    globals()[scipy_type] = globals()[numarray_type]
+
+_scipy_alias("bool_", "Bool")
+_scipy_alias("bool8", "Bool")
+_scipy_alias("int8", "Int8")
+_scipy_alias("uint8", "UInt8")
+_scipy_alias("int16", "Int16")
+_scipy_alias("uint16", "UInt16")
+_scipy_alias("int32", "Int32")
+_scipy_alias("uint32", "UInt32")
+_scipy_alias("int64", "Int64")
+_scipy_alias("uint64", "UInt64")
+
+_scipy_alias("float64", "Float64")
+_scipy_alias("float32", "Float32")
+_scipy_alias("complex128", "Complex64")
+_scipy_alias("complex64", "Complex32")
+
+# The rest is used by numeric modules to determine conversions
+
+# Ranking of types from lowest to highest (sorta)
+if not HasUInt64:
+    genericTypeRank = ['Bool','Int8','UInt8','Int16','UInt16',
+                       'Int32', 'UInt32', 'Int64',
+                       'Float32','Float64', 'Complex32', 'Complex64',  'Object']
+else:
+    genericTypeRank = ['Bool','Int8','UInt8','Int16','UInt16',
+                       '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 ]
+
+# The next line is not platform independent XXX Needs to be generalized
+if not LP64:
+    pythonTypeMap  = {
+        int:("Int32","int"),
+        long:("Int64","int"),
+        float:("Float64","float"),
+        complex:("Complex64","complex")}
+    
+    scalarTypeMap = {
+        int:"Int32",
+        long:"Int64",
+        float:"Float64",
+        complex:"Complex64"}
+else:
+    pythonTypeMap  = {
+        int:("Int64","int"),
+        long:("Int64","int"),
+        float:("Float64","float"),
+        complex:("Complex64","complex")}
+
+    scalarTypeMap = {
+        int:"Int64",
+        long:"Int64",
+        float:"Float64",
+        complex:"Complex64"}
+
+if _sys.version_info >= (2,3,0):
+    pythonTypeMap.update({bool:("Bool","bool") })
+    scalarTypeMap.update({bool:"Bool"})
+
+# Generate coercion matrix
+
+def _initGenericCoercions():
+    global genericCoercions
+    genericCoercions = {}
+    
+    # vector with ...
+    for ntype1 in genericTypeRank:
+        nt1 = typeDict[ntype1]
+        rank1 = genericTypeRank.index(ntype1)
+        ntypesize1, inttype1, signedtype1 = nt1.bytes, \
+                    isinstance(nt1, IntegralType), isinstance(nt1, SignedIntegralType)
+        for ntype2 in genericTypeRank:
+            # vector
+            nt2 = typeDict[ntype2]
+            ntypesize2, inttype2, signedtype2 = nt2.bytes, \
+                    isinstance(nt2, IntegralType), isinstance(nt2, SignedIntegralType)
+            rank2 = genericTypeRank.index(ntype2)
+            if (signedtype1 != signedtype2) and inttype1 and inttype2:
+                # mixing of signed and unsigned ints is a special case
+                # If unsigned same size or larger, final size needs to be bigger
+                #   if possible
+                if signedtype1:
+                    if ntypesize2 >= ntypesize1:
+                        size = min(2*ntypesize2, MAX_INT_SIZE)
+                    else:
+                        size = ntypesize1
+                else:
+                    if ntypesize1 >= ntypesize2:
+                        size = min(2*ntypesize1, MAX_INT_SIZE)
+                    else:
+                        size = ntypesize2
+                outtype = "Int"+str(8*size)                
+            else:
+                if rank1 >= rank2:
+                    outtype = ntype1
+                else:
+                    outtype = ntype2
+            genericCoercions[(ntype1, ntype2)] = outtype
+            
+        for ntype2 in pythonTypeRank:
+            # scalar
+            mapto, kind = pythonTypeMap[ntype2]
+            if ((inttype1 and kind=="int") or (not inttype1 and kind=="float")):
+                # both are of the same "kind" thus vector type dominates
+                outtype = ntype1
+            else:
+                rank2 = genericTypeRank.index(mapto)
+                if rank1 >= rank2:
+                    outtype = ntype1
+                else:
+                    outtype = mapto
+            genericCoercions[(ntype1, ntype2)] = outtype
+            genericCoercions[(ntype2, ntype1)] = outtype
+            
+    # scalar-scalar
+    for ntype1 in pythonTypeRank:
+        maptype1 = scalarTypeMap[ntype1]
+        genericCoercions[(ntype1,)] = maptype1
+        for ntype2 in pythonTypeRank:
+            maptype2 = scalarTypeMap[ntype2]
+            genericCoercions[(ntype1, ntype2)] = genericCoercions[(maptype1, maptype2)]
+            
+    # Special cases more easily dealt with outside of the loop
+    genericCoercions[("Complex32", "Float64")] = "Complex64"
+    genericCoercions[("Float64", "Complex32")] = "Complex64"
+    genericCoercions[("Complex32", "Int64")] = "Complex64"
+    genericCoercions[("Int64", "Complex32")] = "Complex64"
+    genericCoercions[("Complex32", "UInt64")] = "Complex64"
+    genericCoercions[("UInt64", "Complex32")] = "Complex64"
+
+    genericCoercions[("Int64","Float32")] = "Float64"
+    genericCoercions[("Float32", "Int64")] = "Float64"
+    genericCoercions[("UInt64","Float32")] = "Float64"
+    genericCoercions[("Float32", "UInt64")] = "Float64"
+
+    genericCoercions[(float, "Bool")] = "Float64"
+    genericCoercions[("Bool", float)] = "Float64"
+
+    genericCoercions[(float,float,float)] = "Float64" # for scipy.special
+    genericCoercions[(int,int,float)] = "Float64" # for scipy.special
+
+_initGenericCoercions()
+
+# If complex is subclassed, the following may not be necessary
+genericPromotionExclusions = {
+    'Bool': (),
+    'Int8': (),
+    'Int16': (),
+    'Int32': ('Float32','Complex32'),
+    'UInt8': (),
+    'UInt16': (),
+    'UInt32': ('Float32','Complex32'),
+    'Int64' : ('Float32','Complex32'),
+    'UInt64' : ('Float32','Complex32'),
+    'Float32': (),
+    'Float64': ('Complex32',),
+    'Complex32':(),
+    'Complex64':()
+} # e.g., don't allow promotion from Float64 to Complex32 or Int64 to Float32
+
+# Numeric typecodes
+typecodes = {'Integer': '1silN',
+             'UnsignedInteger': 'bBwuU',
+             'Float': 'fd',
+             'Character': 'c',
+             'Complex': 'FD' }
+
+if HasUInt64:
+    _MaximumType = {
+        Bool :  UInt64,
+        
+        Int8  : Int64,
+        Int16 : Int64,
+        Int32 : Int64,
+        Int64 : Int64,
+        
+        UInt8  : UInt64,
+        UInt16 : UInt64,
+        UInt32 : UInt64,
+        UInt8  : UInt64,
+        
+        Float32 : Float64,
+        Float64 : Float64,
+        
+        Complex32 : Complex64,
+        Complex64 : Complex64    
+        }
+else:
+        _MaximumType = {
+        Bool :  Int64,
+        
+        Int8  : Int64,
+        Int16 : Int64,
+        Int32 : Int64,
+        Int64 : Int64,
+        
+        UInt8  : Int64,
+        UInt16 : Int64,
+        UInt32 : Int64,
+        UInt8  : Int64,
+       
+        Float32 : Float64,
+        Float64 : Float64,
+        
+        Complex32 : Complex64,
+        Complex64 : Complex64    
+        }
+
+def MaximumType(t):
+    """returns the type of highest precision of the same general kind as 't'"""
+    return _MaximumType[t]
+
+
+def getType(type):
+    """Return the numeric type object for type
+
+    type may be the name of a type object or the actual object
+    """
+    if isinstance(type, NumericType):
+        return type
+    try:
+        return typeDict[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)
+    
+_scipy_dtypechar = {
+    Int8 : 'b',
+    UInt8 : 'B',
+    Int16 : 'h',
+    UInt16 : 'H',
+    Int32 : 'i',
+    UInt32 : 'I',
+    Int64 : 'q',
+    UInt64 : 'Q',
+    Float32 : 'f',
+    Float64 : 'd',
+    Complex32 : 'F',  # Note the switchup here:
+    Complex64 : 'D'   #   numarray.Complex32 == scipy.complex64, etc.
+    }
+
+_scipy_dtypechar_inverse = {}
+for key,value in _scipy_dtypechar.items():
+    _scipy_dtypechar_inverse[value] = key
+
+    



More information about the Numpy-svn mailing list