1 | initial version |
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.