Skip to content

Commit

Permalink
Clean up scalar-types functions a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
teoliphant committed Aug 3, 2006
1 parent fad726e commit 6689502
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 39 deletions.
2 changes: 1 addition & 1 deletion numpy/core/__init__.py
Expand Up @@ -5,7 +5,7 @@
import multiarray
import umath
import numerictypes as nt
multiarray.set_typeDict(nt.typeDict)
multiarray.set_typeDict(nt.sctypeDict)
import _sort
from numeric import *
from fromnumeric import *
Expand Down
2 changes: 1 addition & 1 deletion numpy/core/numeric.py
Expand Up @@ -354,7 +354,7 @@ def isscalar(num):
if isinstance(num, generic):
return True
else:
return type(num) in ScalarType
return issctype(type(num))

_lkup = {
'0':'0000',
Expand Down
77 changes: 46 additions & 31 deletions numpy/core/numerictypes.py
Expand Up @@ -76,19 +76,19 @@
"""

# we add more at the bottom
__all__ = ['typeDict', 'typeNA', 'sctypes', 'ScalarType', 'obj2sctype',
__all__ = ['sctypeDict', 'sctypeNA', 'typeDict', 'typeNA', 'sctypes', 'ScalarType', 'obj2sctype',
'cast', 'nbytes', 'sctype2char', 'maximum_sctype', 'issctype',
'typecodes']

from multiarray import typeinfo, ndarray, array, empty
from multiarray import typeinfo, ndarray, array, empty, dtype
import types as _types

# we don't export these for import *, but we do want them accessible
# as numerictypes.bool, etc.
from __builtin__ import bool, int, long, float, complex, object, unicode, str

typeDict = {} # Contains all leaf-node numeric types with aliases
typeNA = {} # Contails all leaf-node types -> numarray type equivalences
sctypeDict = {} # Contains all leaf-node scalar types with aliases
sctypeNA = {} # Contails all leaf-node types -> numarray type equivalences
allTypes = {} # Collect the types we will add to the module here

def _evalname(name):
Expand Down Expand Up @@ -151,9 +151,9 @@ def _add_types():

# define C-name and insert typenum and typechar references also
allTypes[name] = typeobj
typeDict[name] = typeobj
typeDict[typeinfo[a][0]] = typeobj
typeDict[typeinfo[a][1]] = typeobj
sctypeDict[name] = typeobj
sctypeDict[typeinfo[a][0]] = typeobj
sctypeDict[typeinfo[a][1]] = typeobj

else: # generic class
allTypes[name] = typeinfo[a]
Expand All @@ -173,21 +173,21 @@ def _add_aliases():
if (name != 'longdouble' and name != 'clongdouble') or \
myname not in allTypes.keys():
allTypes[myname] = typeobj
typeDict[myname] = typeobj
sctypeDict[myname] = typeobj
if base == 'complex':
na_name = '%s%d' % (base.capitalize(), bit/2)
elif base == 'bool':
na_name = base.capitalize()
typeDict[na_name] = typeobj
sctypeDict[na_name] = typeobj
else:
na_name = "%s%d" % (base.capitalize(), bit)
typeDict[na_name] = typeobj
typeNA[na_name] = typeobj
typeNA[typeobj] = na_name
typeNA[typeinfo[a][0]] = na_name
sctypeDict[na_name] = typeobj
sctypeNA[na_name] = typeobj
sctypeNA[typeobj] = na_name
sctypeNA[typeinfo[a][0]] = na_name
if char != '':
typeDict[char] = typeobj
typeNA[char] = na_name
sctypeDict[char] = typeobj
sctypeNA[char] = na_name
_add_aliases()

# Integers handled so that
Expand All @@ -214,16 +214,16 @@ def _add_integer_aliases():
uintname = 'uint%d' % bits
allTypes[intname] = typeobj
allTypes[uintname] = utypeobj
typeDict[intname] = typeobj
typeDict[uintname] = utypeobj
typeDict[Intname] = typeobj
typeDict[UIntname] = utypeobj
typeNA[Intname] = typeobj
typeNA[UIntname] = utypeobj
typeNA[typeobj] = Intname
typeNA[utypeobj] = UIntname
typeNA[val[0]] = Intname
typeNA[uval[0]] = UIntname
sctypeDict[intname] = typeobj
sctypeDict[uintname] = utypeobj
sctypeDict[Intname] = typeobj
sctypeDict[UIntname] = utypeobj
sctypeNA[Intname] = typeobj
sctypeNA[UIntname] = utypeobj
sctypeNA[typeobj] = Intname
sctypeNA[utypeobj] = UIntname
sctypeNA[val[0]] = Intname
sctypeNA[uval[0]] = UIntname
_add_integer_aliases()

# We use these later
Expand Down Expand Up @@ -255,13 +255,13 @@ def _set_up_aliases():
('object_', 'object')]
for alias, t in type_pairs:
allTypes[alias] = allTypes[t]
typeDict[alias] = typeDict[t]
sctypeDict[alias] = sctypeDict[t]
# Remove aliases overriding python types and modules
for t in ['ulong', 'object', 'unicode', 'int', 'long', 'float',
'complex', 'bool', 'string']:
try:
del allTypes[t]
del typeDict[t]
del sctypeDict[t]
except KeyError:
pass
_set_up_aliases()
Expand Down Expand Up @@ -342,10 +342,14 @@ def _python_type(t):
def issctype(rep):
"""Determines whether the given object represents
a numeric array type."""
if not isinstance(rep, (type, dtype)):
return False
try:
char = sctype2char(rep)
return True
except (KeyError, ValueError, TypeError):
res = obj2sctype(rep)
if res:
return True
return False
except:
return False

def obj2sctype(rep, default=None):
Expand All @@ -354,11 +358,13 @@ def obj2sctype(rep, default=None):
return rep
except TypeError:
pass
if isinstance(rep, dtype):
return rep.type
if isinstance(rep, type):
return _python_type(rep)
if isinstance(rep, ndarray):
return rep.dtype.type
res = typeDict.get(rep, default)
res = sctypeDict.get(rep, default)
return res


Expand Down Expand Up @@ -417,6 +423,11 @@ def sctype2char(sctype):
else:
_typestr[key] = empty((1,),key).dtype.str[1:]

# Make sure all typestrings are in sctypeDict
for key, val in _typestr.items():
if val not in sctypeDict:
sctypeDict[val] = key

# Now add the types we've determined to this module
for key in allTypes:
globals()[key] = allTypes[key]
Expand All @@ -432,3 +443,7 @@ def sctype2char(sctype):
'AllInteger':'bBhHiIlLqQpP',
'AllFloat':'fdgFDG',
'All':'?bhilqpBHILQPfdgFDGSUVO'}

# backwards compatibility --- deprecated name
typeDict = sctypeDict
typeNA = sctypeNA
10 changes: 4 additions & 6 deletions numpy/core/src/arrayobject.c
Expand Up @@ -10877,14 +10877,13 @@ arraydescr_richcompare(PyArray_Descr *self, PyObject *other, int cmp_op)
}
switch (cmp_op) {
case Py_LT:
if (PyArray_CanCastTo(self, new))
if (!PyArray_EquivTypes(self, new) && PyArray_CanCastTo(self, new))
result = Py_True;
else
result = Py_False;
break;
case Py_LE:
if (PyArray_EquivTypes(self, new) ||
PyArray_CanCastTo(self, new))
if (PyArray_CanCastTo(self, new))
result = Py_True;
else
result = Py_False;
Expand All @@ -10902,14 +10901,13 @@ arraydescr_richcompare(PyArray_Descr *self, PyObject *other, int cmp_op)
result = Py_True;
break;
case Py_GT:
if (PyArray_CanCastTo(new, self))
if (!PyArray_EquivTypes(self, new) && PyArray_CanCastTo(new, self))
result = Py_True;
else
result = Py_False;
break;
case Py_GE:
if (PyArray_EquivTypes(self, new) ||
PyArray_CanCastTo(new, self))
if (PyArray_CanCastTo(new, self))
result = Py_True;
else
result = Py_False;
Expand Down

0 comments on commit 6689502

Please sign in to comment.