[Numpy-discussion] diag, diagonal, ravel and all that

Charles R Harris charlesr.harris at gmail.com
Sun Jan 4 09:56:28 EST 2015


On Sun, Jan 4, 2015 at 1:44 AM, Ralf Gommers <ralf.gommers at gmail.com> wrote:

>
>
> On Sun, Jan 4, 2015 at 1:28 AM, Charles R Harris <
> charlesr.harris at gmail.com> wrote:
>
>>
>>
>> On Sat, Jan 3, 2015 at 4:54 PM, Sturla Molden <sturla.molden at gmail.com>
>> wrote:
>>
>>> On 03/01/15 03:04, Charles R Harris wrote:
>>>
>>> > The diag, diagonal, and ravel functions have recently been changed to
>>> > preserve subtypes. However, this causes lots of backward compatibility
>>> > problems for matrix users, in particular, scipy.sparse. One possibility
>>> > for fixing this is to special case matrix and so that these functions
>>> > continue to return 1-d arrays for matrix instances. This is kind of
>>> ugly
>>> > as `a..ravel` will still return a matrix when a is a matrix, an ugly
>>> > inconsistency. This may be a case where  practicality beats beauty.
>>> >
>>> > Thoughts?
>>>
>>
> I think it makes sense to special-case matrix here. Arguable, ravel() is
> an operation that should return a 1-D array (ndarray or other array-like
> object). np.matrix doesn't allow 1-D objects, hence can't be returned.
>
> The method is also documented to return a 1-D array, so maybe the
> matrix.ravel method is wrong here:
>
>     In [1]: x = np.matrix(np.eye(3))
>
>     In [2]: x.ravel()
>     Out[2]: matrix([[ 1.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  1.]])  # 2-D
>
>     In [3]: print(x.ravel.__doc__)
>     a.ravel([order])
>
>         Return a flattened array.
>
>         Refer to `numpy.ravel` for full documentation.
>

Just to clarify the previous behavior for matrix m.

1) m.diagonal() and m.ravel() both return matrices
2) diagonal(m) and ravel(m) both return 1-D arrays

Currently in master, which is incompatible with scipy master

1) m.diagonal() and m.ravel() both return matrices
2) diagonal(m) and ravel(m) both return matrices

There is a PR to revert to the previous behavior. Another option is to
change m.ravel() to return a 1-D array and leave diagonal(m) returning a
matrix. The incompatibilites with diagonal didn't seem to be as troublesome.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150104/6634ffd0/attachment.html>


More information about the NumPy-Discussion mailing list