summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/arrayprint.py3
-rw-r--r--numpy/core/oldnumeric.py29
-rw-r--r--numpy/core/src/multiarraymodule.c44
3 files changed, 34 insertions, 42 deletions
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py
index a649d0211..fd33a44ac 100644
--- a/numpy/core/arrayprint.py
+++ b/numpy/core/arrayprint.py
@@ -16,6 +16,7 @@ import sys
import numeric as _gen
import numerictypes as _nt
import umath as _uf
+from oldnumeric import ravel
_nc = _gen
# The following functions are emergency substitutes for numeric functions
@@ -138,7 +139,7 @@ def _array2string(a, max_line_width, precision, suppress_small, separator=' ',
data = _leading_trailing(a)
else:
summary_insert = ""
- data = a.ravel()
+ data = ravel(a)
try:
format_function = a._format
diff --git a/numpy/core/oldnumeric.py b/numpy/core/oldnumeric.py
index 26058f0c9..3bc1b13de 100644
--- a/numpy/core/oldnumeric.py
+++ b/numpy/core/oldnumeric.py
@@ -177,14 +177,19 @@ def take(a, indices, axis=0):
return _wrapit(a, 'take', indices, axis)
return take(indices, axis)
-def reshape(a, newshape, order=False):
- """Change the shape of a to newshape. Return a new view object.
+# not deprecated --- copy if necessary, view otherwise
+def reshape(a, newshape, order='C'):
+ """Change the shape of a to newshape. Return a new view object if possible
+ otherwise return a copy.
"""
try:
reshape = a.reshape
except AttributeError:
return _wrapit(a, 'reshape', newshape, order=order)
- return reshape(newshape, order=order)
+ try:
+ return reshape(newshape, order=order)
+ except ValueError:
+ return a.copy(order).reshape(newshape, order=None)
def choose(a, choices):
try:
@@ -239,9 +244,9 @@ def swapaxes(a, axis1, axis2):
return swapaxes(axis1, axis2)
def transpose(a, axes=None):
- """transpose(a, axes=None) returns array with dimensions permuted
- according to axes. If axes is None (default) returns array with
- dimensions reversed.
+ """transpose(a, axes=None) returns a view of the array with
+ dimensions permuted according to axes. If axes is None
+ (default) returns array with dimensions reversed.
"""
try:
transpose = a.transpose
@@ -346,11 +351,17 @@ def trace(a, offset=0, axis1=0, axis2=1, dtype=None):
"""
return asarray(a).trace(offset, axis1, axis2, dtype)
-def ravel(m):
+# not deprecated --- always returns a 1-d array. Copy-if-necessary.
+def ravel(m,order='C'):
"""ravel(m) returns a 1d array corresponding to all the elements of it's
- argument.
+ argument. The new array is a view of m if possible, otherwise it is
+ a copy.
"""
- return asarray(m).ravel()
+ a = asarray(m)
+ try:
+ return a.ravel(order)
+ except ValueError:
+ return a.copy(order).ravel(None)
def nonzero(a):
"""nonzero(a) returns the indices of the elements of a which are not zero,
diff --git a/numpy/core/src/multiarraymodule.c b/numpy/core/src/multiarraymodule.c
index c5e08e67b..838a09c72 100644
--- a/numpy/core/src/multiarraymodule.c
+++ b/numpy/core/src/multiarraymodule.c
@@ -30,6 +30,7 @@ static PyObject *typeDict=NULL; /* Must be explicitly loaded */
static PyObject *_numpy_internal=NULL; /* A Python module for callbacks */
static int _multiarray_module_loaded=0;
+
static PyArray_Descr *
_arraydescr_fromobj(PyObject *obj)
{
@@ -180,18 +181,8 @@ PyArray_Ravel(PyArrayObject *a, PyArray_ORDER fortran)
PyArray_Dims newdim = {NULL,1};
intp val[1] = {-1};
- if (fortran == PyArray_ANYORDER)
- fortran = PyArray_ISFORTRAN(a);
-
newdim.ptr = val;
- if (!fortran && PyArray_ISCONTIGUOUS(a)) {
- return PyArray_Newshape(a, &newdim, PyArray_CORDER);
- }
- else if (fortran && PyArray_ISFORTRAN(a)) {
- return PyArray_Newshape(a, &newdim, PyArray_FORTRANORDER);
- }
- else
- return PyArray_Flatten(a, fortran);
+ return PyArray_Newshape(a, &newdim, fortran);
}
static double
@@ -458,7 +449,7 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims,
intp *dimensions = newdims->ptr;
PyArrayObject *ret;
int n = newdims->len;
- Bool same, incref;
+ Bool same;
intp *strides = NULL;
intp newstrides[MAX_DIMS];
@@ -485,24 +476,17 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims,
if (i==0) strides=newstrides;
if (strides==NULL) {
- if ((n == 0) ||
- (PyArray_ISCONTIGUOUS(self) && (fortran != PyArray_FORTRANORDER)) ||
- (PyArray_ISFORTRAN(self) && (fortran != PyArray_CORDER))) {
- incref = TRUE;
- }
- else {
- PyObject *tmp;
- tmp = PyArray_NewCopy(self, fortran);
- if (tmp==NULL) return NULL;
- self = (PyArrayObject *)tmp;
- incref = FALSE;
+ if ((n != 0) &&
+ (!PyArray_ISCONTIGUOUS(self) || (fortran == PyArray_FORTRANORDER)) &&
+ (!PyArray_ISFORTRAN(self) || (fortran == PyArray_CORDER))) {
+ PyErr_SetString(PyExc_ValueError,
+ "cannot return a view from reshape.");
+ return NULL;
}
-
if (_fix_unknown_dimension(newdims, PyArray_SIZE(self)) < 0)
- goto fail;
+ return NULL;
}
else {
- incref = TRUE;
/* replace any 0-valued strides with
appropriate value to preserve contiguousness
*/
@@ -534,17 +518,13 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims,
self->data,
self->flags, (PyObject *)self);
- if (ret== NULL) goto fail;
+ if (ret== NULL) return NULL;
- if (incref) Py_INCREF(self);
+ Py_INCREF(self);
ret->base = (PyObject *)self;
PyArray_UpdateFlags(ret, CONTIGUOUS | FORTRAN);
return (PyObject *)ret;
-
- fail:
- if (!incref) {Py_DECREF(self);}
- return NULL;
}