summaryrefslogtreecommitdiff
path: root/numpy/core/oldnumeric.py
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2006-03-28 21:31:19 +0000
committerTravis Oliphant <oliphant@enthought.com>2006-03-28 21:31:19 +0000
commitffe633bdf4ca09b5ad084998c441dc1d109d2fcc (patch)
tree0a9a1783352d09fd0cee36df36e3ac303db77a69 /numpy/core/oldnumeric.py
parent4776dffd495de69f71c90ed13115c711e05ebde0 (diff)
downloadnumpy-ffe633bdf4ca09b5ad084998c441dc1d109d2fcc.tar.gz
.ravel and .reshape always return views and raise ValueError if that is impossible. The functions ravel and reshape return views or copies if the view is not possible.
Diffstat (limited to 'numpy/core/oldnumeric.py')
-rw-r--r--numpy/core/oldnumeric.py29
1 files changed, 20 insertions, 9 deletions
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,