diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-06-30 20:04:28 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-06-30 20:04:28 +0000 |
commit | 7d4c3ed2a0caebf1ce9e0da3473fdbde005699e5 (patch) | |
tree | fa4801786b5c59f13deff0bdf1cf25c3c8656224 /numpy | |
parent | ec1662fb0182a87ebf39ec476109becfc7a8cdb1 (diff) | |
download | numpy-7d4c3ed2a0caebf1ce9e0da3473fdbde005699e5.tar.gz |
Make the default array type float.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/include/numpy/arrayobject.h | 3 | ||||
-rw-r--r-- | numpy/core/ma.py | 9 | ||||
-rw-r--r-- | numpy/core/numeric.py | 10 | ||||
-rw-r--r-- | numpy/core/src/arrayobject.c | 72 | ||||
-rw-r--r-- | numpy/core/src/multiarraymodule.c | 13 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 4 | ||||
-rw-r--r-- | numpy/lib/mlab.py | 6 | ||||
-rw-r--r-- | numpy/lib/twodim_base.py | 14 | ||||
-rw-r--r-- | numpy/oldnumeric/__init__.py | 6 | ||||
-rw-r--r-- | numpy/oldnumeric/olddefaults.py | 45 |
10 files changed, 145 insertions, 37 deletions
diff --git a/numpy/core/include/numpy/arrayobject.h b/numpy/core/include/numpy/arrayobject.h index 2cb61d9e9..bd30366fa 100644 --- a/numpy/core/include/numpy/arrayobject.h +++ b/numpy/core/include/numpy/arrayobject.h @@ -1320,6 +1320,9 @@ typedef struct { } PyArrayMapIterObject; +/* The default array type + */ +#define PyArray_DEFAULT PyArray_DOUBLE /* All sorts of useful ways to look into a PyArrayObject. These are the recommended over casting to PyArrayObject and accessing the members directly. diff --git a/numpy/core/ma.py b/numpy/core/ma.py index ae7f2f997..95c030746 100644 --- a/numpy/core/ma.py +++ b/numpy/core/ma.py @@ -1547,17 +1547,16 @@ def indices (dimensions, dtype=None): """ return array(numeric.indices(dimensions, dtype)) -def zeros (shape, dtype=int): - """zeros(n, dtype=int) = +def zeros (shape, dtype=float): + """zeros(n, dtype=float) = an array of all zeros of the given length or shape.""" return array(numeric.zeros(shape, dtype)) -def ones (shape, dtype=int): - """ones(n, dtype=int) = +def ones (shape, dtype=float): + """ones(n, dtype=float) = an array of all ones of the given length or shape.""" return array(numeric.ones(shape, dtype)) - def count (a, axis = None): "Count of the non-masked elements in a, or along a certain axis." a = masked_array(a) diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 610d6188b..ab5c5fcb3 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -293,8 +293,8 @@ set_string_function(array_repr, 1) little_endian = (sys.byteorder == 'little') -def indices(dimensions, dtype=int_): - """indices(dimensions,dtype=int_) returns an array representing a grid +def indices(dimensions, dtype=int): + """indices(dimensions,dtype=int) returns an array representing a grid of indices with row-only, and column-only variation. """ tmp = ones(dimensions, dtype) @@ -400,8 +400,8 @@ def load(file): # These are all essentially abbreviations # These might wind up in a special abbreviations module -def ones(shape, dtype=int_, order='C'): - """ones(shape, dtype=int_) returns an array of the given +def ones(shape, dtype=None, order='C'): + """ones(shape, dtype=None) returns an array of the given dimensions which is initialized to all ones. """ a = empty(shape, dtype, order) @@ -411,7 +411,7 @@ def ones(shape, dtype=int_, order='C'): #a+=1 return a -def identity(n,dtype=int_): +def identity(n,dtype=None): """identity(n) returns the identity 2-d array of shape n x n. """ a = array([1]+n*[0],dtype=dtype) diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index 75021c902..208b337fb 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -5240,7 +5240,7 @@ array_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) if (order == PyArray_FORTRANORDER) fortran = 1; if (descr == NULL) - descr = PyArray_DescrFromType(PyArray_LONG); + descr = PyArray_DescrFromType(PyArray_DEFAULT); type_num = descr->type_num; itemsize = descr->elsize; @@ -10225,17 +10225,65 @@ arraydescr_repr(PyArray_Descr *self) return s; } -static int -arraydescr_compare(PyArray_Descr *self, PyObject *other) +static PyObject * +arraydescr_richcompare(PyArray_Descr *self, PyObject *other, int cmp_op) { + PyArray_Descr *new=NULL; + PyObject *result = Py_NotImplemented; if (!PyArray_DescrCheck(other)) { - PyErr_SetString(PyExc_TypeError, - "not a dtype object."); - return -1; + if (PyArray_DescrConverter(other, &new) == PY_FAIL) + return NULL; } - if (PyArray_EquivTypes(self, (PyArray_Descr *)other)) return 0; - if (PyArray_CanCastTo(self, (PyArray_Descr *)other)) return -1; - return 1; + else { + new = (PyArray_Descr *)other; + Py_INCREF(new); + } + switch (cmp_op) { + case Py_LT: + if (PyArray_CanCastTo(self, new)) + result = Py_True; + else + result = Py_False; + break; + case Py_LE: + if (PyArray_EquivTypes(self, new) || + PyArray_CanCastTo(self, new)) + result = Py_True; + else + result = Py_False; + break; + case Py_EQ: + if (PyArray_EquivTypes(self, new)) + result = Py_True; + else + result = Py_False; + break; + case Py_NE: + if (PyArray_EquivTypes(self, new)) + result = Py_False; + else + result = Py_True; + break; + case Py_GT: + if (PyArray_CanCastTo(new, self)) + result = Py_True; + else + result = Py_False; + break; + case Py_GE: + if (PyArray_EquivTypes(self, new) || + PyArray_CanCastTo(new, self)) + result = Py_True; + else + result = Py_False; + break; + default: + result = Py_NotImplemented; + } + + Py_XDECREF(new); + Py_INCREF(result); + return result; } /************************************************************************* @@ -10310,7 +10358,7 @@ static PyTypeObject PyArrayDescr_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ - (cmpfunc)arraydescr_compare, /* tp_compare */ + 0, /* tp_compare */ (reprfunc)arraydescr_repr, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ @@ -10323,9 +10371,9 @@ static PyTypeObject PyArrayDescr_Type = { 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT, /* tp_flags */ 0, /* tp_doc */ - 0, /* tp_traverse */ + 0, /* tp_traverse */ 0, /* tp_clear */ - 0, /* tp_richcompare */ + (richcmpfunc)arraydescr_richcompare, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ diff --git a/numpy/core/src/multiarraymodule.c b/numpy/core/src/multiarraymodule.c index 49bcfca69..6bf6bfa4c 100644 --- a/numpy/core/src/multiarraymodule.c +++ b/numpy/core/src/multiarraymodule.c @@ -4207,7 +4207,7 @@ PyArray_DescrConverter(PyObject *obj, PyArray_Descr **at) /* default */ if (obj == Py_None) { - *at = PyArray_DescrFromType(PyArray_LONG); + *at = PyArray_DescrFromType(PyArray_DEFAULT); return PY_SUCCEED; } @@ -4668,7 +4668,7 @@ PyArray_Empty(int nd, intp *dims, PyArray_Descr *type, int fortran) { PyArrayObject *ret; - if (!type) type = PyArray_DescrFromType(PyArray_LONG); + if (!type) type = PyArray_DescrFromType(PyArray_DEFAULT); ret = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, type, nd, dims, NULL, NULL, @@ -4791,7 +4791,7 @@ PyArray_Zeros(int nd, intp *dims, PyArray_Descr *type, int fortran) PyArrayObject *ret; intp n; - if (!type) type = PyArray_DescrFromType(PyArray_LONG); + if (!type) type = PyArray_DescrFromType(PyArray_DEFAULT); ret = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, type, nd, dims, @@ -4887,7 +4887,7 @@ PyArray_FromString(char *data, intp slen, PyArray_Descr *dtype, Bool binary; if (dtype == NULL) - dtype=PyArray_DescrFromType(PyArray_LONG); + dtype=PyArray_DescrFromType(PyArray_DEFAULT); itemsize = dtype->elsize; if (itemsize == 0) { @@ -5325,7 +5325,7 @@ array_fromfile(PyObject *ignored, PyObject *args, PyObject *keywds) return NULL; } - if (type == NULL) type = PyArray_DescrFromType(PyArray_LONG); + if (type == NULL) type = PyArray_DescrFromType(PyArray_DEFAULT); if (PyString_Check(file)) { if (sep == "") mode="rb"; @@ -5473,7 +5473,7 @@ array_frombuffer(PyObject *ignored, PyObject *args, PyObject *keywds) return NULL; } if (type==NULL) - type = PyArray_DescrFromType(PyArray_LONG); + type = PyArray_DescrFromType(PyArray_DEFAULT); return PyArray_FromBuffer(obj, type, (intp)nin, (intp)offset); } @@ -5663,6 +5663,7 @@ PyArray_ArangeObj(PyObject *start, PyObject *stop, PyObject *step, PyArray_Descr if (!dtype) { PyArray_Descr *deftype; PyArray_Descr *newtype; + /* intentionally made to be PyArray_LONG default */ deftype = PyArray_DescrFromType(PyArray_LONG); newtype = PyArray_DescrFromObject(start, deftype); Py_DECREF(deftype); diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index a0c7db466..f29a8f760 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -66,7 +66,7 @@ class test_attributes(ScipyTestCase): def check_stridesattr(self): x = self.one def make_array(size, offset, strides): - return ndarray([size], buffer=x, + return ndarray([size], buffer=x, dtype=int, offset=offset*x.itemsize, strides=strides*x.itemsize) assert_equal(make_array(4, 4, -1), array([4, 3, 2, 1])) @@ -81,7 +81,7 @@ class test_attributes(ScipyTestCase): x = self.one def make_array(size, offset, strides): try: - r = ndarray([size], buffer=x, offset=offset*x.itemsize) + r = ndarray([size], dtype=int, buffer=x, offset=offset*x.itemsize) except: pass r.strides = strides=strides*x.itemsize diff --git a/numpy/lib/mlab.py b/numpy/lib/mlab.py index 33ee966e5..fd66ecd70 100644 --- a/numpy/lib/mlab.py +++ b/numpy/lib/mlab.py @@ -2,7 +2,11 @@ from numpy.core.numeric import * -from twodim_base import eye, tri, diag, fliplr, flipud, rot90, tril, triu +from twodim_base import diag, fliplr, flipud, rot90, tril, triu + +# use old defaults +from numpy.oldnumeric import eye, tri + from numpy.core.fromnumeric import amax as max, amin as min from function_base import msort, median, trapz, diff, cov, corrcoef, \ kaiser, blackman, bartlett, hanning, hamming, sinc, angle diff --git a/numpy/lib/twodim_base.py b/numpy/lib/twodim_base.py index 832d3f576..a063ddfea 100644 --- a/numpy/lib/twodim_base.py +++ b/numpy/lib/twodim_base.py @@ -40,13 +40,14 @@ def rot90(m, k=1): elif k == 2: return fliplr(flipud(m)) else: return fliplr(m.transpose()) # k==3 -def eye(N, M=None, k=0, dtype=int_): +def eye(N, M=None, k=0, dtype=float): """ eye returns a N-by-M 2-d array where the k-th diagonal is all ones, and everything else is zeros. """ if M is None: M = N m = equal(subtract.outer(arange(N), arange(M)),-k) - return m.astype(dtype) + if m.dtype != dtype: + return m.astype(dtype) def diag(v, k=0): """ returns a copy of the the k-th diagonal if v is a 2-d array @@ -81,20 +82,21 @@ def diag(v, k=0): raise ValueError, "Input must be 1- or 2-d." -def tri(N, M=None, k=0, dtype=int_): +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. """ if M is None: M = N m = greater_equal(subtract.outer(arange(N), arange(M)),-k) - return m.astype(dtype) + if m.dtype != dtype: + return m.astype(dtype) def tril(m, k=0): """ returns the elements on and below the k-th diagonal of m. k=0 is the main diagonal, k > 0 is above and k < 0 is below the main diagonal. """ m = asanyarray(m) - out = multiply(tri(m.shape[0], m.shape[1], k=k, dtype=m.dtype),m) + out = multiply(tri(m.shape[0], m.shape[1], k=k, dtype=int),m) return out def triu(m, k=0): @@ -102,7 +104,7 @@ def triu(m, k=0): main diagonal, k > 0 is above and k < 0 is below the main diagonal. """ m = asanyarray(m) - out = multiply((1-tri(m.shape[0], m.shape[1], k-1, m.dtype)),m) + out = multiply((1-tri(m.shape[0], m.shape[1], k-1, int)),m) return out # borrowed from John Hunter and matplotlib diff --git a/numpy/oldnumeric/__init__.py b/numpy/oldnumeric/__init__.py index 768b407df..439cfe907 100644 --- a/numpy/oldnumeric/__init__.py +++ b/numpy/oldnumeric/__init__.py @@ -1,15 +1,21 @@ from numpy import * from compat import * +from olddefaults import * import numpy import compat +import olddefaults __version__ = numpy.__version__ __all__ = ['__version__'] __all__ += numpy.__all__ __all__ += compat.__all__ +for name in olddefaults.__all__: + if name not in __all__: + __all__.append(name) del numpy del compat +del olddefaults diff --git a/numpy/oldnumeric/olddefaults.py b/numpy/oldnumeric/olddefaults.py new file mode 100644 index 000000000..d818ed57c --- /dev/null +++ b/numpy/oldnumeric/olddefaults.py @@ -0,0 +1,45 @@ +__all__ = ['ones', 'empty', 'identity', 'zeros', 'eye', 'tri'] + +import numpy.core.multiarray as mu +import numpy.core.numeric as nn + +def ones(shape, dtype=int, order='C'): + """ones(shape, dtype=int) returns an array of the given + dimensions which is initialized to all ones. + """ + a = mu.empty(shape, dtype, order) + a.fill(1) + return a + +def zeros(shape, dtype=int, order='C'): + """zeros(shape, dtype=int) returns an array of the given + dimensions which is initialized to all zeros + """ + return mu.zeros(shape, dtype, order) + +def identity(n,dtype=int): + """identity(n) returns the identity 2-d array of shape n x n. + """ + return nn.identity(n, dtype) + +def eye(N, M=None, k=0, dtype=int): + """ eye returns a N-by-M 2-d array where the k-th diagonal is all ones, + and everything else is zeros. + """ + if M is None: M = N + m = equal(subtract.outer(arange(N), arange(M)),-k) + if m.dtype != dtype: + return m.astype(dtype) + +def tri(N, M=None, k=0, dtype=int): + """ returns a N-by-M array where all the diagonals starting from + lower left corner up to the k-th are all ones. + """ + if M is None: M = N + m = greater_equal(subtract.outer(arange(N), arange(M)),-k) + if m.dtype != dtype: + return m.astype(dtype) + +def empty(shape, dtype=int, order='C'): + return mu.empty(shape, dtype, order) + |