[Numpy-svn] r2712 - in trunk/numpy: core core/include/numpy core/src core/tests lib oldnumeric
numpy-svn at scipy.org
numpy-svn at scipy.org
Fri Jun 30 16:04:47 EDT 2006
Author: oliphant
Date: 2006-06-30 15:04:28 -0500 (Fri, 30 Jun 2006)
New Revision: 2712
Added:
trunk/numpy/oldnumeric/olddefaults.py
Modified:
trunk/numpy/core/include/numpy/arrayobject.h
trunk/numpy/core/ma.py
trunk/numpy/core/numeric.py
trunk/numpy/core/src/arrayobject.c
trunk/numpy/core/src/multiarraymodule.c
trunk/numpy/core/tests/test_multiarray.py
trunk/numpy/lib/mlab.py
trunk/numpy/lib/twodim_base.py
trunk/numpy/oldnumeric/__init__.py
Log:
Make the default array type float.
Modified: trunk/numpy/core/include/numpy/arrayobject.h
===================================================================
--- trunk/numpy/core/include/numpy/arrayobject.h 2006-06-30 13:36:13 UTC (rev 2711)
+++ trunk/numpy/core/include/numpy/arrayobject.h 2006-06-30 20:04:28 UTC (rev 2712)
@@ -1320,6 +1320,9 @@
} 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.
Modified: trunk/numpy/core/ma.py
===================================================================
--- trunk/numpy/core/ma.py 2006-06-30 13:36:13 UTC (rev 2711)
+++ trunk/numpy/core/ma.py 2006-06-30 20:04:28 UTC (rev 2712)
@@ -1547,17 +1547,16 @@
"""
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)
Modified: trunk/numpy/core/numeric.py
===================================================================
--- trunk/numpy/core/numeric.py 2006-06-30 13:36:13 UTC (rev 2711)
+++ trunk/numpy/core/numeric.py 2006-06-30 20:04:28 UTC (rev 2712)
@@ -293,8 +293,8 @@
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 @@
# 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 @@
#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)
Modified: trunk/numpy/core/src/arrayobject.c
===================================================================
--- trunk/numpy/core/src/arrayobject.c 2006-06-30 13:36:13 UTC (rev 2711)
+++ trunk/numpy/core/src/arrayobject.c 2006-06-30 20:04:28 UTC (rev 2712)
@@ -5240,7 +5240,7 @@
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 @@
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 @@
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 @@
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 */
Modified: trunk/numpy/core/src/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarraymodule.c 2006-06-30 13:36:13 UTC (rev 2711)
+++ trunk/numpy/core/src/multiarraymodule.c 2006-06-30 20:04:28 UTC (rev 2712)
@@ -4207,7 +4207,7 @@
/* default */
if (obj == Py_None) {
- *at = PyArray_DescrFromType(PyArray_LONG);
+ *at = PyArray_DescrFromType(PyArray_DEFAULT);
return PY_SUCCEED;
}
@@ -4668,7 +4668,7 @@
{
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 @@
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 @@
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 @@
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 @@
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 @@
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);
Modified: trunk/numpy/core/tests/test_multiarray.py
===================================================================
--- trunk/numpy/core/tests/test_multiarray.py 2006-06-30 13:36:13 UTC (rev 2711)
+++ trunk/numpy/core/tests/test_multiarray.py 2006-06-30 20:04:28 UTC (rev 2712)
@@ -66,7 +66,7 @@
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 @@
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
Modified: trunk/numpy/lib/mlab.py
===================================================================
--- trunk/numpy/lib/mlab.py 2006-06-30 13:36:13 UTC (rev 2711)
+++ trunk/numpy/lib/mlab.py 2006-06-30 20:04:28 UTC (rev 2712)
@@ -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
Modified: trunk/numpy/lib/twodim_base.py
===================================================================
--- trunk/numpy/lib/twodim_base.py 2006-06-30 13:36:13 UTC (rev 2711)
+++ trunk/numpy/lib/twodim_base.py 2006-06-30 20:04:28 UTC (rev 2712)
@@ -40,13 +40,14 @@
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 @@
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 @@
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
Modified: trunk/numpy/oldnumeric/__init__.py
===================================================================
--- trunk/numpy/oldnumeric/__init__.py 2006-06-30 13:36:13 UTC (rev 2711)
+++ trunk/numpy/oldnumeric/__init__.py 2006-06-30 20:04:28 UTC (rev 2712)
@@ -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
Added: trunk/numpy/oldnumeric/olddefaults.py
===================================================================
--- trunk/numpy/oldnumeric/olddefaults.py 2006-06-30 13:36:13 UTC (rev 2711)
+++ trunk/numpy/oldnumeric/olddefaults.py 2006-06-30 20:04:28 UTC (rev 2712)
@@ -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)
+
More information about the Numpy-svn
mailing list