From ad2d26442a4cf39ca378040f56ee928e673ad42a Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Sat, 3 Jan 2015 17:11:32 -0700 Subject: 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. --- numpy/core/fromnumeric.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'numpy/core/fromnumeric.py') 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): -- cgit v1.2.1 From ea927d961dfc32b6963aed3b3a10bc51c12543df Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Sat, 3 Jan 2015 17:55:43 -0700 Subject: BUG: Make ravel function return 1-D arrays for matrix argument. This is a backward compatibility hack to avoid breaking scipy.sparse after fixing ravel to respect subtypes. Subtypes are still respected except in the case of matrices and subclasses of matrices. --- numpy/core/fromnumeric.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'numpy/core/fromnumeric.py') diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index da61fc187..2a527a4a4 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -1376,8 +1376,10 @@ def ravel(a, order='C'): Returns ------- y : array_like - Array of the same type as `a`, and of shape ``(a.size,)`` - or ``(1, a.size)`` for matrices. + If `a` is a matrix, y is a 1-D ndarray, otherwise y is an array of + the same subtype as `a`. The shape of the returned array is + ``(a.size,)``. Matrices are special cased for backward + compatibility. See Also -------- @@ -1435,7 +1437,10 @@ def ravel(a, order='C'): array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) """ - return asanyarray(a).ravel(order) + if isinstance(a, np.matrix): + return asarray(a).ravel(order) + else: + return asanyarray(a).ravel(order) def nonzero(a): -- cgit v1.2.1