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
matrix
as an element of a matrix space over
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
. Note that matrix indexing
is 0
-based in SAGE, so the top right entry is
, 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
.
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
is made immutable using
A.set_immutable()
the entries of
cannot be changed, and
can never be made mutable again. However, properies of
such as
its rank, characteristic polynomial, etc., are all cached so
computations involving
may be more efficient. Once
is made
immutable it cannot be changed back. However, one can obtain a
mutable copy of
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()).