Ask Your Question
1

how to get the diagonal of a matrix?

asked 2010-08-23 08:13:36 -0600

Is there something like ?

sage: A = matrix(QQ,3,3,[3,-1,1,3,6,2,3,3,7])

sage: A.diagonal()

[3,6,7]

edit retag flag offensive close merge delete

Comments

I created http://trac.sagemath.org/sage_trac/ticket/9796 to track adding this.

Jason Grout gravatar imageJason Grout ( 2010-08-24 05:59:42 -0600 )edit

Rob Beezer posted a patch there relatively recently, if anyone wants to review it.

kcrisman gravatar imagekcrisman ( 2011-01-13 13:34:14 -0600 )edit

3 answers

Sort by ยป oldest newest most voted
2

answered 2010-08-24 06:03:06 -0600

Jason Grout gravatar image

updated 2010-08-24 06:03:58 -0600

You could also do something like:

   sage: A=matrix(QQ,3,3,[3,-1,1,3,6,2,3,3,7]) 
   sage: A.numpy().diagonal() 
   array([3, 6, 7])
edit flag offensive delete link more
1

answered 2010-08-23 08:22:27 -0600

kcrisman gravatar image

I don't believe so.

sage: [A[z][z] for z in range(A.nrows())]

[3, 6, 7]

could work. But I don't know that this is what you are looking for. Would you find this useful as an attribute? What output would be expected for a non-square matrix?

edit flag offensive delete link more

Comments

Note that you should use A[z,z] instead of A[z][z] to get the entry as it is quite a bit more efficient. A[z] needs to construct and return a whole row of the matrix before you can get the z'th entry of that row.

Mike Hansen gravatar imageMike Hansen ( 2010-08-23 08:42:04 -0600 )edit

Thanks, I wasn't sure which syntax was better to use.

kcrisman gravatar imagekcrisman ( 2010-08-24 08:40:35 -0600 )edit

kcrisman -- I definitely think somebody should add this as a function, given that it is in numpy. If you do, add a link to a trac ticket here.

William Stein gravatar imageWilliam Stein ( 2010-08-24 10:58:42 -0600 )edit
Mike Hansen gravatar imageMike Hansen ( 2010-08-24 11:12:40 -0600 )edit
1

answered 2010-08-23 10:57:07 -0600

Depending on whether you want to do numerics or exact linear algebra, you can use Numpy/Scipy's matrix object. (Numpy and Scipy are included in Sage.) For example:

sage: import numpy as np
sage: A = np.matrix([[3,-1,1],[3,6,2],[3,3,7]])
sage: A.diagonal()
matrix([[3, 6, 7]])

The diagonal() method also supports rectangular matrices. It does the obvious, which is to continue down the diagonal until a "side" of the matrix is hit. For example, if I add a row to the above matrix I get the following output:

sage: B = np.matrix([[3,-1,1],[3,6,2],[3,3,7],[0,0,0]])
sage: B.diagonal()
matrix([[3, 6, 7]])

Or if I add a column at the end:

sage: C = np.matrix([[3,-1,1,0],[3,6,2,0],[3,3,7,0]])
sage: C.diagonal()
matrix([[3, 6, 7]])

But again, Numpy/Scipy's specialty is in floating point arithmetic. (I.e. numerics) Thankfully, there is a decent amount of compatibility between Numpy's numpy.matrix object and Sage's sage.matrix object.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2010-08-23 08:13:36 -0600

Seen: 2,585 times

Last updated: Aug 24 '10