7.1.3.1 Matrix Objects

class Matrix
The Matrix class is the base class for all matrix classes. To create a Matrix, first create a MatrixSpace, then coerce a list of elements into the MatrixSpace. See the documentation of MatrixSpace for more details.
Matrix( parent)

Instances of class Matrix have the following methods (in addition to inherited methods and special methods):

add_multiple_of_column,$  $ add_multiple_of_row,$  $ augment,$  $ block_sum,$  $ change_ring,$  $ columns,$  $ commutator,$  $ copy,$  $ dense_matrix,$  $ dense_row,$  $ get,$  $ is_dense,$  $ is_immutable,$  $ is_sparse,$  $ is_square,$  $ iterates,$  $ linear_combination_of_rows,$  $ list,$  $ matrix_space,$  $ ncols,$  $ new_matrix,$  $ nonpivots,$  $ nonzero_positions,$  $ nonzero_positions_in_column,$  $ nonzero_positions_in_row,$  $ nrows,$  $ rescale_row,$  $ row,$  $ rows,$  $ set,$  $ set_immutable,$  $ sparse_columns,$  $ sparse_matrix,$  $ sparse_rows,$  $ stack,$  $ submatrix_from_columns,$  $ submatrix_from_rows,$  $ swap_columns,$  $ swap_rows,$  $ trace,$  $ vector_matrix_multiply

Further documentation:

add_multiple_of_column( i, j, s)

Replace column i by s times column j.

add_multiple_of_row( i, j, s)

Replace row i by s times row j.

augment( other)

Return the augmented matrix of the form [self | other].

block_sum( other)

Return the block matrix that has self and other on the diagonal: [self | 0 ] [ 0 | other ]

change_ring( ring)

Return the matrix obtained by coercing the entries of this matrix into the given ring.

columns( )

Returns the list of columns of self, as vectors.

sage: A = MatrixSpace(RationalField(), 2,3) (xrange(6))
sage: A
[0 1 2]
[3 4 5]
sage: A.columns()
[(0, 3), (1, 4), (2, 5)]

commutator( other)

Return the commutator self*other - other*self.

copy( )

Return a copy of this matrix. Changing the entries of the copy will not change the entries of this matrix.

dense_matrix( )

If this matrix is sparse, return a dense matrix with the same entries. If this matrix is dense, return this matrix (not a copy).

NOTE: The definition of"dense" and "sparse" in SAGE have nothing to do with the number of nonzero entries. Sparse and dense are properties of the underlying representation of the matrix.

sage: A = MatrixSpace(RationalField(),2, sparse=True)([1,2,0,1])
sage: A.is_sparse()
True
sage: B = A.dense_matrix()
sage: B.is_sparse()
False
sage: A*B
[1 4]
[0 1]
sage: A.parent()
Full MatrixSpace of 2 by 2 sparse matrices over Rational Field
sage: B.parent()
Full MatrixSpace of 2 by 2 dense matrices over Rational Field
sage: (A*B).parent()
Full MatrixSpace of 2 by 2 sparse matrices over Rational Field
sage: (B*A).parent()
Full MatrixSpace of 2 by 2 dense matrices over Rational Field

get( ij)

Entry access, but possibly without bounds checking (for efficiency).

INPUT:
    A[i, j] -- the i,j of A, and
    A[i]    -- the i-th row of A.

iterates( v, n)

Let $ A$ be this matrix and $ v$ be a free module element. Return a vector whose rows are the entries of the following vectors:

$\displaystyle v, v A, v A^2, \ldots, v A^{n-1}.
$

INPUT:
    v -- free module element
    n -- nonnegative integer

sage: A = MatrixSpace(IntegerRing(), 2)([1,1,3,5]); A
[1 1]
[3 5]
sage: v = FreeModule(IntegerRing(), 2)([1,0])
sage: A.iterates(v,0)
[]
sage: A.iterates(v,5)
[  1   0]
[  1   1]
[  4   6]
[ 22  34]
[124 192]

new_matrix( [nrows=True], [ncols=True], [entries=0], [coerce_entries=None], [copy=None])

Create a matrix in the parent of this space with the given number of rows, columns, etc.

WARNING: This function called with no arguments returns the 0 matrix by default, not the matrix self.

nonpivots( )

Return the list of i such that the i-th column of self is NOT a pivot column of the reduced row echelon form of self.

OUTPUT: list - sorted list of integers

nonzero_positions( )

Returns the set of pairs (i,j) such that self[i,j] != 0.

nonzero_positions_in_column( i)

Return the integers j such that self[j,i] is nonzero, i.e., such that the j-th position of the i-th column is nonzero.

nonzero_positions_in_row( i)

Return the integers j such that self[i,j] is nonzero, i.e., such that the j-th position of the i-th row is nonzero.

rescale_row( i, s)

Replace i-th row of self by s times i-th row of self.

sparse_matrix( )

If this matrix is dense, return a sparse matrix with the same entries. If this matrix is sparse, return this matrix (not a copy).

NOTE: The definition of "dense" and "sparse" in SAGE have nothing to do with the number of nonzero entries. Sparse and dense are properties of the underlying representation of the matrix.

sage: A = MatrixSpace(RationalField(),2, sparse=False)([1,2,0,1])
sage: A.is_sparse()
False
sage: B = A.sparse_matrix()
sage: B.is_sparse()
True
sage: A
[1 2]
[0 1]
sage: B
[1 2]
[0 1]
sage: A*B
[1 4]
[0 1]
sage: A.parent()
Full MatrixSpace of 2 by 2 dense matrices over Rational Field
sage: B.parent()
Full MatrixSpace of 2 by 2 sparse matrices over Rational Field
sage: (A*B).parent()
Full MatrixSpace of 2 by 2 dense matrices over Rational Field
sage: (B*A).parent()
Full MatrixSpace of 2 by 2 sparse matrices over Rational Field

stack( other)

Return the augmented matrix self on top of other: [ self ] [ other ]

sage: M = Matrix(RationalField(), 2, 3, range(6))
sage: N = Matrix(RationalField(), 1, 3, [10,11,12])
sage: M.stack(N)
[ 0  1  2]
[ 3  4  5]
[10 11 12]

submatrix_from_columns( columns)

Return the submatrix of self of columns col[i] for i in the list of columns.

submatrix_from_rows( rows)

Return the submatrix of self of rows row[i] for i in the list of rows.

swap_columns( c1, c2)

Swap columns c1 and c2 of self.

swap_rows( r1, r2)

Swap rows r1 and r2 of self.

trace( )

Return the trace of self, which is the sum of the diagonal entries of self.

INPUT:
    self -- a square matrix
OUTPUT:
    element of the base ring of self

vector_matrix_multiply( v)

Returns the vector times matrix product.

INPUT:
     v -- a free module element.

OUTPUT:
    The the vector times matrix product v*A.

sage: MS = MatrixSpace(RationalField(), 2,2)
sage: B = MS.matrix([1,2,1,2])
sage: V = VectorSpace(RationalField(), 2)
sage: v = V([1,2])
sage: B.vector_matrix_multiply(v)     # computes v*B
(3, 6)
sage: Bt = B.transpose()
sage: Bt.vector_matrix_multiply(v)    # computes B*v
(5, 5)

Instances of class Matrix also have the following special methods:

__abs__,$  $ __cmp__,$  $ __getitem__,$  $ __mod__,$  $ __mul__,$  $ __neg__,$  $ __pos__,$  $ __pow__,$  $ __repr__,$  $ __rmul__,$  $ __setitem__,$  $ __str__,$  $ _add,$  $ _dict,$  $ _div,$  $ _entries,$  $ _latex_,$  $ _latex_sparse,$  $ _Matrix__is_compatible,$  $ _pari_,$  $ _require_mutable,$  $ _right_scalar_multiply,$  $ _scalar_multiply,$  $ _sub

Further documentation:

__getitem__( ij)

INPUT:
    A[i, j] -- the i,j of A, and
    A[i]    -- the i-th row of A.

__pow__( n)

sage: MS = MatrixSpace(RationalField(), 3, 3)
sage: A = MS([0, 0, 1, 1, 0, '-2/11', 0, 1, '-3/11'])
sage: A * A**(-1) == 1
True
sage: A**4
[      -3/11     -13/121   1436/1331]
[    127/121   -337/1331 -4445/14641]
[    -13/121   1436/1331 -8015/14641]

__setitem__( ij, x)

INPUT:
    A[i, j] = value -- set the (i,j) entry of A
    A[i] = value    -- set the ith row of A

_entries( )

This must be defined in the derived class. It is the underlying representation of elements, which may be a list or dict or something else.

_latex_sparse( [variable=x])

Return a latex string that represents this matrix as a sparse matrix. The rows are printed as sums sum a_i x_i, where x is the variable.

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