Ticket #1162 (closed defect: fixed)

Opened 7 months ago

Last modified 7 months ago

linalg.matrix_power crashes with nested list input

Reported by: dgoldsmith Owned by: somebody
Priority: normal Milestone:
Component: numpy.linalg Version:
Keywords: matrix_power, array_like Cc: d_l_goldsmith@…

Description

linalg.matrix_power raises an AttributeError? when a nested list is passed in as the array argument:

Microsoft Windows [Version 6.0.6002] Copyright (c) 2006 Microsoft Corporation. All rights reserved.

C:\Users\DG>python Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.

import numpy as np from numpy import linalg as LA LA.matrix_power([[1, 2], [3, 4]], 3)

Traceback (most recent call last):

File "<stdin>", line 1, in <module> File "C:\Python25\Lib\site-packages\numpy\core\defmatrix.py", line 118, in matrix_power

if len(M.shape) != 2 or M.shape[0] != M.shape[1]:

AttributeError?: 'list' object has no attribute 'shape'

Yet the doc states:

matrix_power(M, n)

Raise a square matrix to the (integer) power n.

:

Parameters ---------- M : array_like

I've corrected the doc, for now, but I assume this actually a code bug.

Change History

Changed 7 months ago by nils

>>> LA.matrix_power(np.array([[1, 2], [3, 4]]), 3)
array([[ 37,  54],
       [ 81, 118]])
>>> type([[1, 2], [3, 4]])
<type 'list'>
>>>
>>> LA.matrix_power(np.matrix([[1, 2], [3, 4]]), 3)
matrix([[ 37,  54],
        [ 81, 118]])

Changed 7 months ago by dgoldsmith

Nils: so?

DG

Changed 7 months ago by nils

Just a test that matrix_power works with matrices and arrays. Anyway lists should be accepted as well, since

Python 2.6 (r26:66714, Feb  3 2009, 20:49:49)
[GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy.linalg as la
>>> la.expm([[1, 2], [3, 4]])
array([[  51.9689562 ,   74.73656457],
       [ 112.10484685,  164.07380305]])

Changed 7 months ago by dgoldsmith

Oh, OK, I guess I should have included that, but I had confirmed that it worked in those cases. :-)

DG

Changed 7 months ago by pv

  • status changed from new to closed
  • resolution set to fixed

Fixed to accept lists in r7131

Note: See TracTickets for help on using tickets.