diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-01-17 00:07:21 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-01-17 00:07:21 +0000 |
commit | 4593515985f1aa30cf07715ace5eeee6da848c97 (patch) | |
tree | ab13435f4bec383abc7c04ca7b0259b44dd2519d /numpy/core | |
parent | a397b5545b1a20e1b0e39881eb8cf8d2dd26b793 (diff) | |
download | numpy-4593515985f1aa30cf07715ace5eeee6da848c97.tar.gz |
Fixed up usage of dtype to be consistent with new dtype objects.
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/defchararray.py | 2 | ||||
-rw-r--r-- | numpy/core/ma.py | 18 | ||||
-rw-r--r-- | numpy/core/numerictypes.py | 38 | ||||
-rw-r--r-- | numpy/core/oldnumeric.py | 2 | ||||
-rw-r--r-- | numpy/core/src/arrayobject.c | 74 | ||||
-rw-r--r-- | numpy/core/src/umathmodule.c.src | 35 |
6 files changed, 105 insertions, 64 deletions
diff --git a/numpy/core/defchararray.py b/numpy/core/defchararray.py index 6b43d7f6f..70096441c 100644 --- a/numpy/core/defchararray.py +++ b/numpy/core/defchararray.py @@ -1,5 +1,5 @@ from numerictypes import character, string, unicode_, \ - obj2dtype, integer, object_ + integer, object_ from numeric import ndarray, broadcast, empty from numeric import array as narray import sys diff --git a/numpy/core/ma.py b/numpy/core/ma.py index c736b3e34..27831fdde 100644 --- a/numpy/core/ma.py +++ b/numpy/core/ma.py @@ -545,14 +545,16 @@ class MaskedArray (object): """array(data, dtype=None, copy=True, fortran=False, mask=nomask, fill_value=None) If data already a numeric array, its dtype becomes the default value of dtype. """ - tc = dtype + if dtype is None: + tc = None + else: + tc = numeric.dtype(dtype) need_data_copied = copy if isinstance(data, MaskedArray): c = data.data - ctc = c.dtype.char if tc is None: - tc = ctc - elif dtype2char(tc) != ctc: + tc = c.dtype + elif tc != c.dtype: need_data_copied = True if mask is nomask: mask = data.mask @@ -561,17 +563,17 @@ class MaskedArray (object): elif isinstance(data, ndarray): c = data - ctc = c.dtype.char if tc is None: - tc = ctc - elif dtype2char(tc) != ctc: + tc = c.dtype + elif tc != c.dtype: need_data_copied = True else: need_data_copied = False #because I'll do it now c = numeric.array(data, dtype=tc, copy=True, fortran=fortran) + tc = c.dtype if need_data_copied: - if tc == ctc: + if tc == c.dtype: self._data = numeric.array(c, dtype=tc, copy=True, fortran=fortran) else: self._data = c.astype(tc) diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py index 1955c370a..1ca432883 100644 --- a/numpy/core/numerictypes.py +++ b/numpy/core/numerictypes.py @@ -78,7 +78,7 @@ $Id: numerictypes.py,v 1.17 2005/09/09 22:20:06 teoliphant Exp $ """ # we add more at the bottom -__all__ = ['typeDict', 'typeNA', 'arraytypes', 'ScalarType', 'obj2dtype', 'cast', 'nbytes', 'dtype2char'] +__all__ = ['typeDict', 'typeNA', 'arraytypes', 'ScalarType', 'obj2arrtype', 'cast', 'nbytes', 'arrtype2char','maximum_arrtype', 'isarrtype'] from multiarray import typeinfo, ndarray, array, empty import types as _types @@ -226,13 +226,13 @@ def _set_up_aliases(): _set_up_aliases() # Now, construct dictionary to lookup character codes from types -_dtype2char_dict = {} +_arrtype2char_dict = {} def _construct_char_code_lookup(): for name in typeinfo.keys(): tup = typeinfo[name] if isinstance(tup, tuple): if tup[0] not in ['p','P']: - _dtype2char_dict[tup[-1]] = tup[0] + _arrtype2char_dict[tup[-1]] = tup[0] _construct_char_code_lookup() @@ -271,9 +271,9 @@ genericTypeRank = ['bool', 'int8', 'uint8', 'int16', 'uint16', 'complex32', 'complex64', 'complex128', 'complex160', 'complex192', 'complex256', 'complex512', 'object'] -def maximum_dtype(t): - """returns the type of highest precision of the same general kind as 't'""" - g = obj2dtype(t) +def maximum_arrtype(t): + """returns the arrtype of highest precision of the same general kind as 't'""" + g = obj2arrtype(t) if g is None: return t t = g @@ -298,16 +298,16 @@ def _python_type(t): t = type(t) return allTypes[_python_types.get(t, 'object_')] -def isdtype(rep): +def isarrtype(rep): """Determines whether the given object represents a numeric array type.""" try: - char = dtype2char(rep) + char = arrtype2char(rep) return True except (KeyError, ValueError): return False -def obj2dtype(rep, default=None): +def obj2arrtype(rep, default=None): try: if issubclass(rep, generic): return rep @@ -321,10 +321,10 @@ def obj2dtype(rep, default=None): return res -# This dictionary allows look up based on any alias for a type +# This dictionary allows look up based on any alias for an array type class _typedict(dict): def __getitem__(self, obj): - return dict.__getitem__(self, obj2dtype(obj)) + return dict.__getitem__(self, obj2arrtype(obj)) nbytes = _typedict() _alignment = _typedict() @@ -346,11 +346,11 @@ def _construct_lookups(): _construct_lookups() -def dtype2char(dtype): - dtype = obj2dtype(dtype) - if dtype is None: +def arrtype2char(arrtype): + arrtype = obj2arrtype(arrtype) + if arrtype is None: raise ValueError, "unrecognized type" - return _dtype2char_dict[dtype] + return _arrtype2char_dict[arrtype] # Create dictionary of casting functions that wrap sequences # indexed by type or type character @@ -360,9 +360,9 @@ cast = _typedict() ScalarType = [_types.IntType, _types.FloatType, _types.ComplexType, _types.LongType, _types.BooleanType, _types.StringType, _types.UnicodeType, _types.BufferType] -ScalarType.extend(_dtype2char_dict.keys()) +ScalarType.extend(_arrtype2char_dict.keys()) ScalarType = tuple(ScalarType) -for key in _dtype2char_dict.keys(): +for key in _arrtype2char_dict.keys(): cast[key] = lambda x, k=key : array(x, copy=False).astype(k) @@ -370,9 +370,9 @@ _unicodesize = array('u','U').itemsize # Create the typestring lookup dictionary _typestr = _typedict() -for key in _dtype2char_dict.keys(): +for key in _arrtype2char_dict.keys(): if issubclass(key, allTypes['flexible']): - _typestr[key] = _dtype2char_dict[key] + _typestr[key] = _arrtype2char_dict[key] else: _typestr[key] = empty((1,),key).dtype.str[1:] diff --git a/numpy/core/oldnumeric.py b/numpy/core/oldnumeric.py index 863bd36bc..dad521d84 100644 --- a/numpy/core/oldnumeric.py +++ b/numpy/core/oldnumeric.py @@ -34,7 +34,7 @@ from numeric import asarray, array, correlate, outer, concatenate from umath import sign, absolute, multiply import numeric as _nx import sys -_dt_ = nt.dtype2char +_dt_ = nt.arrtype2char import types diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index 00fd2ff53..3f155ad73 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -3078,21 +3078,27 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) n_ops.less_equal); case Py_EQ: /* Try to convert other to an array */ - array_other = PyArray_FromObject(other, - self->descr->type_num, 0, 0); - /* If not successful, then return the integer - object 0. This fixes code that used to - allow equality comparisons between arrays - and other objects which would give a result - of 0 - */ - if ((array_other == NULL) || \ - (array_other == Py_None)) { - Py_XDECREF(array_other); - PyErr_Clear(); - Py_INCREF(Py_False); - return Py_False; - } + if (!PyArray_Check(other)) { + array_other = PyArray_FromObject(other, + self->descr->type_num, 0, 0); + /* If not successful, then return the integer + object 0. This fixes code that used to + allow equality comparisons between arrays + and other objects which would give a result + of 0 + */ + if ((array_other == NULL) || \ + (array_other == Py_None)) { + Py_XDECREF(array_other); + PyErr_Clear(); + Py_INCREF(Py_False); + return Py_False; + } + } + else { + Py_INCREF(other); + array_other = other; + } result = PyArray_GenericBinaryFunction(self, array_other, n_ops.equal); @@ -3109,23 +3115,29 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) return result; case Py_NE: /* Try to convert other to an array */ - array_other = PyArray_FromObject(other, - self->descr->type_num, 0, 0); - /* If not successful, then objects cannot be - compared and cannot be equal, therefore, - return True; - */ - if ((array_other == NULL) || \ - (array_other == Py_None)) { - Py_XDECREF(array_other); - PyErr_Clear(); - Py_INCREF(Py_True); - return Py_True; - } - result = PyArray_GenericBinaryFunction(self, + if (!PyArray_Check(other)) { + array_other = PyArray_FromObject(other, + self->descr->type_num, 0, 0); + /* If not successful, then objects cannot be + compared and cannot be equal, therefore, + return True; + */ + if ((array_other == NULL) || \ + (array_other == Py_None)) { + Py_XDECREF(array_other); + PyErr_Clear(); + Py_INCREF(Py_True); + return Py_True; + } + } + else { + Py_INCREF(other); + array_other = other; + } + result = PyArray_GenericBinaryFunction(self, array_other, - n_ops.not_equal); - Py_DECREF(array_other); + n_ops.not_equal); + Py_DECREF(array_other); if (result == NULL) { PyErr_Clear(); Py_INCREF(Py_True); diff --git a/numpy/core/src/umathmodule.c.src b/numpy/core/src/umathmodule.c.src index 2ad3a9385..1375efaa0 100644 --- a/numpy/core/src/umathmodule.c.src +++ b/numpy/core/src/umathmodule.c.src @@ -1578,10 +1578,37 @@ static void /**begin repeat -#TYPE=(BYTE,UBYTE,SHORT,USHORT,INT,UINT,LONG,ULONG,LONGLONG,ULONGLONG)*6# -#typ=(byte, ubyte, short, ushort, int, uint, long, ulong, longlong, ulonglong)*6# -#OP= %*10, &*10, |*10, ^*10, <<*10, >>*10# -#kind=fmod*10, bitwise_and*10, bitwise_or*10, bitwise_xor*10, left_shift*10, right_shift*10# +#TYPE=BYTE,UBYTE,SHORT,USHORT,INT,UINT,LONG,ULONG,LONGLONG,ULONGLONG# +#typ=byte, ubyte, short, ushort, int, uint, long, ulong, longlong, ulonglong# +*/ +static void +@TYPE@_fmod(char **args, intp *dimensions, intp *steps, void *func) +{ + register intp i; + intp is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0]; + char *i1=args[0], *i2=args[1], *op=args[2]; + @typ@ x, y; + for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) { + x = *((@typ@ *)i1); + y = *((@typ@ *)i2); + if (y == 0) { + generate_divbyzero_error(); + *((@typ@ *)op) = 0; + } + else { + *((@typ@ *)op)= x % y; + } + + } +} +/**end repeat**/ + +/**begin repeat + +#TYPE=(BYTE,UBYTE,SHORT,USHORT,INT,UINT,LONG,ULONG,LONGLONG,ULONGLONG)*5# +#typ=(byte, ubyte, short, ushort, int, uint, long, ulong, longlong, ulonglong)*5# +#OP= &*10, |*10, ^*10, <<*10, >>*10# +#kind=bitwise_and*10, bitwise_or*10, bitwise_xor*10, left_shift*10, right_shift*10# */ static void |