32.3 Matrices over an arbitrary ring

Module: sage.matrix.docs

Matrices over an arbitrary ring

Author Log:

Elements of matrix spaces are of class Matrix (or a class derived from Matrix). They can be either sparse or dense, and can be defined over any base ring.

We create the $ 2\times 3$ matrix

$\displaystyle \left(\begin{matrix}1\&2\&3\\ 4\&5\&6 \end{matrix}\right)
$

as an element of a matrix space over $ \mathbf{Q}$ :

sage: M = MatrixSpace(QQ,2,3)
sage: A = M([1,2,3, 4,5,6]); A
[1 2 3]
[4 5 6]
sage: A.parent()
Full MatrixSpace of 2 by 3 dense matrices over Rational Field

Alternatively, we could create A more directly as follows (which would completely avoid having to create the matrix space):

sage: A = matrix(QQ, 2, [1,2,3, 4,5,6]); A
[1 2 3]
[4 5 6]

We next change the top-right entry of $ A$ . Note that matrix indexing is 0 -based in SAGE, so the top right entry is $ (0,2)$ , which should be thought of as ``row number 0 , column number 2''.

sage: A[0,2] = 389
sage: A
[  1   2 389]
[  4   5   6]

Also notice how matrices print. All columns have the same width and entries in a given column are right justified. Next we compute the reduced row echelon form of $ A$ .

sage: A.echelon_form()
[      1       0 -1933/3]
[      0       1  1550/3]

We save and load a matrix:

sage: A = matrix(Integers(8),3,range(9))
sage: loads(dumps(A)) == A
True

MUTABILITY: Matrices are either immutable or not. When initially created, matrices are typically mutable, so one can change their entries. Once a matrix $ A$ is made immutable using A.set_immutable() the entries of $ A$ cannot be changed, and $ A$ can never be made mutable again. However, properies of $ A$ such as its rank, characteristic polynomial, etc., are all cached so computations involving $ A$ may be more efficient. Once $ A$ is made immutable it cannot be changed back. However, one can obtain a mutable copy of $ A$ using A.copy().

sage: A = matrix(RR,2,[1,10,3.5,2])
sage: A.set_immutable()
sage: A.copy() is A
False

The echelon form method always returns immutable matrices with known rank.

sage: A = matrix(Integers(8),3,range(9))
sage: A.determinant()
0
sage: A[0,0] = 5
sage: A.determinant()
1
sage: A.set_immutable()
sage: A[0,0] = 5
Traceback (most recent call last):
...
ValueError: matrix is immutable; please change a copy instead (use
self.copy()).



Subsections
See About this document... for information on suggesting changes.