diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2015-01-03 17:11:32 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-01-04 08:17:46 -0700 |
commit | ad2d26442a4cf39ca378040f56ee928e673ad42a (patch) | |
tree | a34132b2c193f4b4ba8d45c20056924811eb55c9 /numpy/core/fromnumeric.py | |
parent | 7fbc43b98d59ef982671b456cebc229425ae7e4e (diff) | |
download | numpy-ad2d26442a4cf39ca378040f56ee928e673ad42a.tar.gz |
BUG: Make diag, diagonal return 1-D arrays for matrix arguments.
This is an ugly hack to preserve backwards compatibility for code
that uses matrices. It is needed since both diag and diagonal have
been changed to preserve subtypes otherwise.
Note that a.diagonal() still returns matrices when a is a matrix.
Diffstat (limited to 'numpy/core/fromnumeric.py')
-rw-r--r-- | numpy/core/fromnumeric.py | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index 55789d780..da61fc187 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -6,6 +6,7 @@ from __future__ import division, absolute_import, print_function import types import warnings +import numpy as np from .. import VisibleDeprecationWarning from . import multiarray as mu from . import umath as um @@ -13,6 +14,7 @@ from . import numerictypes as nt from .numeric import asarray, array, asanyarray, concatenate from . import _methods + _dt_ = nt.sctype2char @@ -1199,9 +1201,9 @@ def diagonal(a, offset=0, axis1=0, axis2=1): just ignore all of the above. If you depend on the current behavior, then we suggest copying the - returned array explicitly, i.e., use ``np.diagonal(a).copy()`` instead of - just ``np.diagonal(a)``. This will work with both past and future versions - of NumPy. + returned array explicitly, i.e., use ``np.diagonal(a).copy()`` instead + of just ``np.diagonal(a)``. This will work with both past and future + versions of NumPy. Parameters ---------- @@ -1220,11 +1222,13 @@ def diagonal(a, offset=0, axis1=0, axis2=1): Returns ------- array_of_diagonals : ndarray - If `a` is 2-D, a 1-D array of the same type as `a` containing the - diagonal is returned (or 2-D matrix for matrix input). - If the dimension of `a` is larger, then an array of diagonals is - returned, "packed" from left-most dimension to right-most (e.g., - if `a` is 3-D, then the diagonals are "packed" along rows). + If `a` is 2-D and not a matrix, a 1-D array of the same type as `a` + containing the diagonal is returned. If `a` is a matrix, a 1-D + array containing the diagonal is returned in order to maintain + backward compatibility. If the dimension of `a` is greater than + two, then an array of diagonals is returned, "packed" from + left-most dimension to right-most (e.g., if `a` is 3-D, then the + diagonals are "packed" along rows). Raises ------ @@ -1273,7 +1277,11 @@ def diagonal(a, offset=0, axis1=0, axis2=1): [5, 7]]) """ - return asanyarray(a).diagonal(offset, axis1, axis2) + if isinstance(a, np.matrix): + # Make diagonal of matrix 1-D to preserve backward compatibility. + return asarray(a).diagonal(offset, axis1, axis2) + else: + return asanyarray(a).diagonal(offset, axis1, axis2) def trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None): |