Module: sage.matrix.matrix0
Base class for matrices, part 0
NOTE: For design documentation see matrix/docs.py.
sage: matrix(2,[1,2,3,4]) [1 2] [3 4]
Module-level Functions
) |
) |
) |
Unpickle a matrix. This is only used internally by SAGE. Users should never call this function directly.
We illustrating saving and loading several different types of matrices.
OVER
:
sage: A = matrix(ZZ,2,range(4)) sage: loads(dumps(A)) [0 1] [2 3]
Sparse OVER
:
Dense over
:
Dense over finite field.
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.
We illustrate matrices and matrix spaces. Note that no actual matrix that you make should have class Matrix; the class should always be derived from Matrix.
sage: M = MatrixSpace(CDF,2,3); M Full MatrixSpace of 2 by 3 dense matrices over Complex Double Field sage: a = M([1,2,3, 4,5,6]); a [1.0 2.0 3.0] [4.0 5.0 6.0] sage: type(a) <type 'sage.matrix.matrix_complex_double_dense.Matrix_complex_double_dense' > sage: parent(a) Full MatrixSpace of 2 by 3 dense matrices over Complex Double Field
sage: matrix(CDF, 2,3, [1,2,3, 4,5,6]) [1.0 2.0 3.0] [4.0 5.0 6.0] sage: Mat(CDF,2,3)(range(1,7)) [1.0 2.0 3.0] [4.0 5.0 6.0]
sage: Q.<i,j,k> = QuaternionAlgebra(QQ, -1,-1) sage: matrix(Q,2,1,[1,2]) [1] [2]
Functions: act_on_polynomial,
add_multiple_of_column,
add_multiple_of_row,
base_ring,
change_ring,
commutator,
copy,
dict,
is_dense,
is_immutable,
is_invertible,
is_mutable,
is_sparse,
is_square,
is_symmetric,
iterates,
linear_combination_of_columns,
linear_combination_of_rows,
list,
mod,
ncols,
nonpivots,
nonzero_positions,
nonzero_positions_in_column,
nonzero_positions_in_row,
nrows,
pivots,
rank,
rescale_col,
rescale_row,
set_col_to_multiple_of_col,
set_immutable,
set_row_to_multiple_of_row,
str,
swap_columns,
swap_rows,
with_added_multiple_of_column,
with_added_multiple_of_row,
with_col_set_to_multiple_of_col,
with_rescaled_col,
with_rescaled_row,
with_row_set_to_multiple_of_row
) |
Returns the polynomial f(self*x).
Input:
sage: R.<x,y> = QQ[] sage: x, y = R.gens() sage: f = x**2 - y**2 sage: M = MatrixSpace(QQ, 2) sage: A = M([1,2,3,4]) sage: A.act_on_polynomial(f) -8*x^2 - 20*x*y - 12*y^2
) |
Add s times column j to column i.
We add -1 times the third column to the second column of an integer matrix, remembering to start numbering cols at zero:
sage: a = matrix(ZZ,2,3,range(6)); a [0 1 2] [3 4 5] sage: a.add_multiple_of_column(1,2,-1) sage: a [ 0 -1 2] [ 3 -1 5]
To add a rational multiple, we first need to change the base ring:
sage: a = a.change_ring(QQ) sage: a.add_multiple_of_column(1,0,1/3) sage: a [ 0 -1 2] [ 3 0 5]
If not, we get an error message:
sage: a.add_multiple_of_column(1,0,i) Traceback (most recent call last): ... TypeError: Multiplying column by Symbolic Ring element cannot be done over Rational Field, use change_ring or with_added_multiple_of_column instead.
) |
Add s times row j to row i.
We add -3 times the first row to the second row of an integer matrix, remembering to start numbering rows at zero:
sage: a = matrix(ZZ,2,3,range(6)); a [0 1 2] [3 4 5] sage: a.add_multiple_of_row(1,0,-3) sage: a [ 0 1 2] [ 3 1 -1]
To add a rational multiple, we first need to change the base ring:
sage: a = a.change_ring(QQ) sage: a.add_multiple_of_row(1,0,1/3) sage: a [ 0 1 2] [ 3 4/3 -1/3]
If not, we get an error message:
sage: a.add_multiple_of_row(1,0,i) Traceback (most recent call last): ... TypeError: Multiplying row by Symbolic Ring element cannot be done over Rational Field, use change_ring or with_added_multiple_of_row instead.
) |
Return the matrix obtained by coercing the entries of this matrix into the given ring.
Always returns a copy (unless self is immutable, in which case returns self).
sage: A = Matrix(QQ, 2, 2, [1/2, 1/3, 1/3, 1/4]) sage: A.parent() Full MatrixSpace of 2 by 2 dense matrices over Rational Field sage: A.change_ring(GF(25,'a')) [3 2] [2 4] sage: A.change_ring(GF(25,'a')).parent() Full MatrixSpace of 2 by 2 dense matrices over Finite Field in a of size 5^2 sage: A.change_ring(ZZ) Traceback (most recent call last): ... TypeError: matrix has denominators so can't change to ZZ.
) |
Return the commutator self*other - other*self.
sage: A = Matrix(QQ[['t']], 2, 2, range(4))
) |
Make a copy of self. If self is immutable, the copy will be mutable.
WARNING: The individual elements aren't themselves copied (though the list is copied). This shouldn't matter, since ring elements are (almost!) always immutable in SAGE.
sage: R.<x> = QQ['x'] sage: a = matrix(R,2,[x+1,2/3, x^2/2, 1+x^3]); a [ x + 1 2/3] [1/2*x^2 x^3 + 1] sage: b = a.copy() sage: b[0,0] = 5 sage: b [ 5 2/3] [1/2*x^2 x^3 + 1] sage: a [ x + 1 2/3] [1/2*x^2 x^3 + 1]
sage: b = copy(a) sage: f = b[0,0]; f[0] = 10 Traceback (most recent call last): ... IndexError: polynomials are immutable
) |
Dictionary of the elements of self with keys pairs (i,j) and values the nonzero entries of self.
It is safe to change the returned dictionary.
sage: R.<x,y> = QQ[] sage: a = matrix(R,2,[x,y,0, 0,0,2*x+y]); a [ x y 0] [ 0 0 2*x + y] sage: d = a.dict(); d {(0, 1): y, (1, 2): 2*x + y, (0, 0): x}
Notice that changing the returned list does not change a (the list is a copy):
sage: d[0,0] = 25 sage: a [ x y 0] [ 0 0 2*x + y]
) |
Returns True if this is a dense matrix.
In SAGE, being dense is a property of the underlying representation, not the number of nonzero entries.
sage: matrix(QQ,2,2,range(4)).is_dense() True sage: matrix(QQ,2,2,range(4),sparse=True).is_dense() False
) |
Return True if this matrix is immutable.
See the documentation for self.set_immutable for more details about mutability.
sage: A = Matrix(QQ['t','s'], 2, 2, range(4)) sage: A.is_immutable() False sage: A.set_immutable() sage: A.is_immutable() True
) |
Return True if this matrix is invertible.
The following matrix is invertible over
but not over
.
sage: A = MatrixSpace(ZZ, 2)(range(4)) sage: A.is_invertible() False sage: A.matrix_over_field().is_invertible() True
The inverse function is a constructor for matrices over the fraction field, so it can work even if A is not invertible.
sage: ~A # inverse of A [-3/2 1/2] [ 1 0]
The next matrix is invertible over
.
sage: A = MatrixSpace(IntegerRing(),2)([1,10,0,-1]) sage: A.is_invertible() True sage: ~A # compute the inverse [ 1 10] [ 0 -1]
The following nontrivial matrix is invertible over
.
sage: R.<x> = PolynomialRing(IntegerRing()) sage: A = MatrixSpace(R,2)([1,x,0,-1]) sage: A.is_invertible() True sage: ~A [ 1 x] [ 0 -1]
) |
Return True if this matrix is mutable.
See the documentation for self.set_immutable for more details about mutability.
sage: A = Matrix(QQ['t','s'], 2, 2, range(4)) sage: A.is_mutable() True sage: A.set_immutable() sage: A.is_mutable() False
) |
Return True if this is a sparse matrix.
In SAGE, being sparse is a property of the underlying representation, not the number of nonzero entries.
sage: matrix(QQ,2,2,range(4)).is_sparse() False sage: matrix(QQ,2,2,range(4),sparse=True).is_sparse() True
) |
Return True precisely if this matrix is square, i.e., has the same number of rows and columns.
sage: matrix(QQ,2,2,range(4)).is_square() True sage: matrix(QQ,2,3,range(6)).is_square() False
) |
Returns True if this is a symmetric matrix.
) |
Let
be this matrix and
be a free module element. If
rows is True, return a matrix whose rows are the entries of
the following vectors:
If rows is False, return a matrix whose columns are the entries of the following vectors:
Input:
sage: A = matrix(ZZ,2, [1,1,3,5]); A [1 1] [3 5] sage: v = vector([1,0]) sage: A.iterates(v,0) [] sage: A.iterates(v,5) [ 1 0] [ 1 1] [ 4 6] [ 22 34] [124 192]
Another example:
sage: a = matrix(ZZ,3,range(9)); a [0 1 2] [3 4 5] [6 7 8] sage: v = vector([1,0,0]) sage: a.iterates(v,4) [ 1 0 0] [ 0 1 2] [ 15 18 21] [180 234 288] sage: a.iterates(v,4,rows=False) [ 1 0 15 180] [ 0 3 42 558] [ 0 6 69 936]
) |
Return the linear combination of the columns of self given by the coefficients in the list v.
Input:
sage: a = matrix(QQ,2,3,range(6)); a [0 1 2] [3 4 5] sage: a.linear_combination_of_columns([1,1,1]) (3, 12) sage: a.linear_combination_of_columns([0,0,0]) (0, 0)
) |
Return the linear combination of the rows of self given by the coefficients in the list v.
Input:
sage: a = matrix(QQ,2,3,range(6)); a [0 1 2] [3 4 5] sage: a.linear_combination_of_rows([1,2]) (6, 9, 12) sage: a.linear_combination_of_rows([0,0]) (0, 0, 0)
) |
List of elements of self. It is safe to change the returned list.
sage: R.<x,y> = QQ[] sage: a = matrix(R,2,[x,y,x*y, y,x,2*x+y]); a [ x y x*y] [ y x 2*x + y] sage: v = a.list(); v [x, y, x*y, y, x, 2*x + y]
Notice that changing the returned list does not change a (the list is a copy):
sage: v[0] = 25 sage: a [ x y x*y] [ y x 2*x + y]
) |
Return matrix mod
, over the reduced ring.
sage: M = matrix(ZZ, 2, 2, [5, 9, 13, 15]) sage: M.mod(7) [5 2] [6 1] sage: parent(M.mod(7)) Full MatrixSpace of 2 by 2 dense matrices over Ring of integers modulo 7
) |
Return the number of columns of this matrix.
sage: M = MatrixSpace(QQ, 2, 3) sage: A = M([1,2,3, 4,5,6]) sage: A [1 2 3] [4 5 6] sage: A.ncols() 3 sage: A.nrows() 2
Author: Naqi Jaffery (2006-01-24): examples
) |
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:
sage: a = matrix(QQ,3,3,range(9)); a [0 1 2] [3 4 5] [6 7 8] sage: a.echelon_form() [ 1 0 -1] [ 0 1 2] [ 0 0 0] sage: a.nonpivots() [2]
) |
Returns the sorted list of pairs (i,j) such that self[i,j] != 0.
Input:
sage: a = matrix(QQ, 2,3, [1,2,0,2,0,0]); a [1 2 0] [2 0 0] sage: a.nonzero_positions() [(0, 0), (0, 1), (1, 0)] sage: a.nonzero_positions(copy=False) [(0, 0), (0, 1), (1, 0)] sage: a.nonzero_positions(column_order=True) [(0, 0), (1, 0), (0, 1)] sage: a = matrix(QQ, 2,3, [1,2,0,2,0,0], sparse=True); a [1 2 0] [2 0 0] sage: a.nonzero_positions() [(0, 0), (0, 1), (1, 0)] sage: a.nonzero_positions(copy=False) [(0, 0), (0, 1), (1, 0)] sage: a.nonzero_positions(column_order=True) [(0, 0), (1, 0), (0, 1)]
) |
Return a sorted list of 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.
Input:
sage: a = matrix(QQ, 3,2, [1,2,0,2,0,0]); a [1 2] [0 2] [0 0] sage: a.nonzero_positions_in_column(0) [0] sage: a.nonzero_positions_in_column(1) [0, 1]
You'll get an IndexError, if you select an invalid column:
sage: a.nonzero_positions_in_column(2) Traceback (most recent call last): ... IndexError: matrix column index out of range
) |
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.
Input:
sage: a = matrix(QQ, 3,2, [1,2,0,2,0,0]); a [1 2] [0 2] [0 0] sage: a.nonzero_positions_in_row(0) [0, 1] sage: a.nonzero_positions_in_row(1) [1] sage: a.nonzero_positions_in_row(2) []
) |
Return the number of rows of this matrix.
sage: M = MatrixSpace(QQ,6,7) sage: A = M([1,2,3,4,5,6,7, 22,3/4,34,11,7,5,3, 99,65,1/2,2/3,3/5,4/5,5/6, 9,8/9, 9/8,7/6,6/7,76,4, 0,9,8,7,6,5,4, 123,99,91,28,6,1024,1]) sage: A [ 1 2 3 4 5 6 7] [ 22 3/4 34 11 7 5 3] [ 99 65 1/2 2/3 3/5 4/5 5/6] [ 9 8/9 9/8 7/6 6/7 76 4] [ 0 9 8 7 6 5 4] [ 123 99 91 28 6 1024 1] sage: A.ncols() 7 sage: A.nrows() 6
Author: Naqi Jaffery (2006-01-24): examples
) |
Return the pivot column positions of this matrix as a list of Python integers.
This returns a list, of the position of the first nonzero entry in each row of the echelon form.
Output:
) |
Replace i-th col of self by s times i-th col of self.
Input:
We rescale the last column of a matrix over the rational numbers:
sage: a = matrix(QQ,2,3,range(6)); a [0 1 2] [3 4 5] sage: a.rescale_col(2,1/2); a [ 0 1 1] [ 3 4 5/2] sage: R.<x> = QQ[]
We rescale the last column of a matrix over a polynomial ring:
sage: a = matrix(R,2,3,[1,x,x^2,x^3,x^4,x^5]); a [ 1 x x^2] [x^3 x^4 x^5] sage: a.rescale_col(2,1/2); a [ 1 x 1/2*x^2] [ x^3 x^4 1/2*x^5]
We try and fail to rescale a matrix over the integers by a non-integer:
sage: a = matrix(ZZ,2,3,[0,1,2, 3,4,4]); a [0 1 2] [3 4 4] sage: a.rescale_col(2,1/2) Traceback (most recent call last): ... TypeError: Rescaling column by Rational Field element cannot be done over Integer Ring, use change_ring or with_rescaled_col instead.
To rescale the matrix by 1/2, you must change the base ring to the rationals:
sage: a = a.change_ring(QQ); a [0 1 2] [3 4 4] sage: a.rescale_col(2,1/2); a [0 1 1] [3 4 2]
) |
Replace i-th row of self by s times i-th row of self.
Input:
We rescale the second row of a matrix over the rational numbers:
sage: a = matrix(QQ,3,range(6)); a [0 1] [2 3] [4 5] sage: a.rescale_row(1,1/2); a [ 0 1] [ 1 3/2] [ 4 5]
We rescale the second row of a matrix over a polynomial ring:
sage: R.<x> = QQ[] sage: a = matrix(R,3,[1,x,x^2,x^3,x^4,x^5]);a [ 1 x] [x^2 x^3] [x^4 x^5] sage: a.rescale_row(1,1/2); a [ 1 x] [1/2*x^2 1/2*x^3] [ x^4 x^5]
We try and fail to rescale a matrix over the integers by a non-integer:
sage: a = matrix(ZZ,2,3,[0,1,2, 3,4,4]); a [0 1 2] [3 4 4] sage: a.rescale_row(1,1/2) Traceback (most recent call last): ... TypeError: Rescaling row by Rational Field element cannot be done over Integer Ring, use change_ring or with_rescaled_row instead.
To rescale the matrix by 1/2, you must change the base ring to the rationals:
sage: a = a.change_ring(QQ); a [0 1 2] [3 4 4] sage: a.rescale_col(1,1/2); a [ 0 1/2 2] [ 3 2 4]
) |
Set column i equal to s times column j.
We change the second column to -3 times the first column.
sage: a = matrix(ZZ,2,3,range(6)); a [0 1 2] [3 4 5] sage: a.set_col_to_multiple_of_col(1,0,-3) sage: a [ 0 0 2] [ 3 -9 5]
If we try to multiply a column by a rational number, we get an error message:
sage: a.set_col_to_multiple_of_col(1,0,1/2) Traceback (most recent call last): ... TypeError: Multiplying column by Rational Field element cannot be done over Integer Ring, use change_ring or with_col_set_to_multiple_of_col instead.
) |
Call this function to matrix a matrix immutable.
Matrices are always mutable by default, i.e., you can change
their entries using A[i,j] = x
. However, mutable
matrices aren't hasheable, so can't be used as keys in
dictionaries, etc. Also, often when implementing a class, you
might compute a matrix associated to it, e.g., the matrix of a
Hecke operator. If you return this matrix to the user you're
really returning a reference and the user could then change an
entry; this could be confusing. Thus you shoulds set such a
matrix immutable.
sage: A = Matrix(QQ, 2, 2, range(4)) sage: A.is_mutable() True sage: A[0,0] = 10 sage: A [10 1] [ 2 3]
Mutable matrices are not hasheable, so can't be used as keys for dictionaries:
sage: hash(A) Traceback (most recent call last): ... TypeError: mutable matrices are unhashable sage: v = {A:1} Traceback (most recent call last): ... TypeError: mutable matrices are unhashable
If we make A immutable it suddenly is hasheable.
sage: A.set_immutable() sage: A.is_mutable() False sage: A[0,0] = 10 Traceback (most recent call last): ... ValueError: matrix is immutable; please change a copy instead (use self.copy()). sage: hash(A) 12 sage: v = {A:1}; v {[10 1] [ 2 3]: 1}
) |
Set row i equal to s times row j.
We change the second row to -3 times the first row:
sage: a = matrix(ZZ,2,3,range(6)); a [0 1 2] [3 4 5] sage: a.set_row_to_multiple_of_row(1,0,-3) sage: a [ 0 1 2] [ 0 -3 -6]
If we try to multiply a row by a rational number, we get an error message:
sage: a.set_row_to_multiple_of_row(1,0,1/2) Traceback (most recent call last): ... TypeError: Multiplying row by Rational Field element cannot be done over Integer Ring, use change_ring or with_row_set_to_multiple_of_row instead.
) |
sage: R = PolynomialRing(QQ,6,'z') sage: a = matrix(2,3, R.gens()) sage: a.__repr__() '[z0 z1 z2]\n[z3 z4 z5]'
) |
Swap columns c1 and c2 of self.
We create a rational matrix:
sage: M = MatrixSpace(QQ,3,3) sage: A = M([1,9,-7,4/5,4,3,6,4,3]) sage: A [ 1 9 -7] [4/5 4 3] [ 6 4 3]
Since the first column is numbered zero, this swaps the second and third columns:
sage: A.swap_columns(1,2); A [ 1 -7 9] [4/5 3 4] [ 6 3 4]
) |
Swap rows r1 and r2 of self.
We create a rational matrix:
sage: M = MatrixSpace(QQ,3,3) sage: A = M([1,9,-7,4/5,4,3,6,4,3]) sage: A [ 1 9 -7] [4/5 4 3] [ 6 4 3]
Since the first row is numbered zero, this swaps the first and third rows:
sage: A.swap_rows(0,2); A [ 6 4 3] [4/5 4 3] [ 1 9 -7]
) |
Add s times column j to column i, returning new matrix.
We add -1 times the third column to the second column of an integer matrix, remembering to start numbering cols at zero:
sage: a = matrix(ZZ,2,3,range(6)); a [0 1 2] [3 4 5] sage: b = a.with_added_multiple_of_column(1,2,-1); b [ 0 -1 2] [ 3 -1 5]
The original matrix is unchanged:
sage: a [0 1 2] [3 4 5]
Adding a rational multiple is okay, and reassigning a variable is okay:
sage: a = a.with_added_multiple_of_column(0,1,1/3); a [ 1/3 1 2] [13/3 4 5]
) |
Add s times row j to row i, returning new matrix.
We add -3 times the first row to the second row of an integer matrix, remembering to start numbering rows at zero:
sage: a = matrix(ZZ,2,3,range(6)); a [0 1 2] [3 4 5] sage: b = a.with_added_multiple_of_row(1,0,-3); b [ 0 1 2] [ 3 1 -1]
The original matrix is unchanged:
sage: a [0 1 2] [3 4 5]
Adding a rational multiple is okay, and reassigning a variable is okay:
sage: a = a.with_added_multiple_of_row(0,1,1/3); a [ 1 7/3 11/3] [ 3 4 5]
) |
Set column i equal to s times column j, returning a new matrix.
We change the second column to -3 times the first column.
sage: a = matrix(ZZ,2,3,range(6)); a [0 1 2] [3 4 5] sage: b = a.with_col_set_to_multiple_of_col(1,0,-3); b [ 0 0 2] [ 3 -9 5]
Note that the original matrix is unchanged:
sage: a [0 1 2] [3 4 5]
Adding a rational multiple is okay, and reassigning a variable is okay:
sage: a = a.with_col_set_to_multiple_of_col(1,0,1/2); a [ 0 0 2] [ 3 3/2 5]
) |
Replaces i-th col of self by s times i-th col of self, returning new matrix.
We rescale the last column of a matrix over the integers:
sage: a = matrix(ZZ,2,3,range(6)); a [0 1 2] [3 4 5] sage: b = a.with_rescaled_col(2,-2); b [ 0 1 -4] [ 3 4 -10]
The original matrix is unchanged:
sage: a [0 1 2] [3 4 5]
Adding a rational multiple is okay, and reassigning a variable is okay:
sage: a = a.with_rescaled_col(1,1/3); a [ 0 1/3 2] [ 3 4/3 5]
) |
Replaces i-th row of self by s times i-th row of self, returning new matrix.
We rescale the second row of a matrix over the integers:
sage: a = matrix(ZZ,3,2,range(6)); a [0 1] [2 3] [4 5] sage: b = a.with_rescaled_row(1,-2); b [ 0 1] [-4 -6] [ 4 5]
The original matrix is unchanged:
sage: a [0 1] [2 3] [4 5]
Adding a rational multiple is okay, and reassigning a variable is okay:
sage: a = a.with_rescaled_row(2,1/3); a [ 0 1] [ 2 3] [4/3 5/3]
) |
Set row i equal to s times row j, returning a new matrix.
We change the second row to -3 times the first row:
sage: a = matrix(ZZ,2,3,range(6)); a [0 1 2] [3 4 5] sage: b = a.with_row_set_to_multiple_of_row(1,0,-3); b [ 0 1 2] [ 0 -3 -6]
Note that the original matrix is unchanged:
sage: a [0 1 2] [3 4 5]
Adding a rational multiple is okay, and reassigning a variable is okay:
sage: a = a.with_row_set_to_multiple_of_row(1,0,1/2); a [ 0 1 2] [ 0 1/2 1]
Special Functions: __delitem__,
__getitem__,
__getslice__,
__init__,
__invert__,
__iter__,
__mod__,
__neg__,
__pos__,
__pow__,
__reduce__,
__repr__,
__rmod__,
__rpow__,
__setitem__,
_clear_cache,
_dict,
_get_cache,
_latex_,
_list,
_matrix_,
_nonzero_positions_by_column,
_nonzero_positions_by_row,
_pickle,
_set_row_to_negative_of_row_of_A_using_subset_of_columns,
_test_pickle
) |
Return element, row, or slice of self.
Input:
USAGE: A[i, j] - the i,j of A, or A[i:j] - the i-th through (j-1)-st rows of A.
sage: A = Matrix(Integers(2006),2,2,[-1,2,3,4]) sage: A[0,0] 2005 sage: A[0] (2005, 2)
The returned row is immutable (mainly to avoid confusion):
sage: A[0][0] = 123 Traceback (most recent call last): ... ValueError: vector is immutable; please change a copy instead (use self.copy()) sage: A[0].is_immutable() True sage: a = MatrixSpace(ZZ,3)(range(9)); a [0 1 2] [3 4 5] [6 7 8] sage: a[1,2] 5 sage: a[0] (0, 1, 2) sage: a[4,7] Traceback (most recent call last): ... IndexError: matrix index out of range sage: a[-1,0] Traceback (most recent call last): ... IndexError: matrix index out of range
sage: a[2.7] Traceback (most recent call last): ... TypeError: 'sage.rings.real_mpfr.RealNumber' object cannot be interpreted as an index
sage: m=[(1, -2, -1, -1,9), (1, 8, 6, 2,2), (1, 1, -1, 1,4), (-1, 2, -2, -1,4)];M= matrix(m) sage: M [ 1 -2 -1 -1 9] [ 1 8 6 2 2] [ 1 1 -1 1 4] [-1 2 -2 -1 4]
Get The 2 x 2 submatrix of M, starting at row index and column index 1
sage: M[1:3,1:3] [ 8 6] [ 1 -1]
Get the 2 x 3 submatrix of M starting at row index and column index 1:
sage: M[1:3,[1..3]] [ 8 6 2] [ 1 -1 1]
Get the second column of M:
sage: M[1:,0] [ 1] [ 1] [-1]
Get the first row of M:
sage: M[0,:] [ 1 -2 -1 -1 9]
More examples:
sage: M[range(2),:] [ 1 -2 -1 -1 9] [ 1 8 6 2 2] sage: M[range(2),4] [9] [2] sage: M[range(3),range(5)] [ 1 -2 -1 -1 9] [ 1 8 6 2 2] [ 1 1 -1 1 4]
sage: M[3,range(5)] [-1 2 -2 -1 4] sage: M[3,:] [-1 2 -2 -1 4] sage: M[3,4] 4
sage: A = matrix(ZZ,3,4, [3, 2, -5, 0, 1, -1, 1, -4, 1, 0, 1, -3]); A [ 3 2 -5 0] [ 1 -1 1 -4] [ 1 0 1 -3]
sage: A[:,0:4:2] [ 3 -5] [ 1 1] [ 1 1]
sage: A[1:,0:4:2] [1 1] [1 1]
sage: A[2:-1:-1,:] [ 1 0 1 -3] [ 1 -1 1 -4] [ 3 2 -5 0]
sage: A[1:,3:-1:-1] [-4 1 -1 1] [-3 1 0 1]
sage: A[1:,3:-1:-2] [-4 -1] [-3 0]
sage: A[2:-1:-1,3:1:-1] [-3 1] [-4 1] [ 0 -5]
sage: A= matrix(3,4,[1, 0, -3, -1, 3, 0, -2, 1, -3, -5, -1, -5]) sage: A[range(2,-1,-1),:] [-3 -5 -1 -5] [ 3 0 -2 1] [ 1 0 -3 -1]
sage: A[range(2,-1,-1),range(3,-1,-1)] [-5 -1 -5 -3] [ 1 -2 0 3] [-1 -3 0 1]
sage: A = matrix(2, [1, 2, 3, 4]) sage: A[[0,0],[0,0]] Traceback (most recent call last): ... IndexError: duplicate values in index; use matrix_from_rows_and_columns() instead
sage: M = matrix(3, 4, range(12)) sage: M[0:0, 0:0] [] sage: M[0:0, 1:4] [] sage: M[2:3, 3:3] [] sage: M[range(2,2), :3] []
) |
Return this inverse of this matrix, as a matrix over the fraction field.
Raises a ZeroDivisionError
if the matrix has zero
determinant, and raises an ArithmeticError
, if the
inverse doesn't exist because the matrix is nonsquare. Also,
note, e.g., that the inverse of a matrix over
is always
a matrix defined over
(even if the entries are integers).
sage: A = MatrixSpace(ZZ, 2)([1,1,3,5]) sage: ~A [ 5/2 -1/2] [-3/2 1/2] sage: A.__invert__() [ 5/2 -1/2] [-3/2 1/2]
Even if the inverse lies in the base field, the result is still a matrix over the fraction field.
sage: I = MatrixSpace(ZZ,2)(1) # identity matrix sage: ~I [1 0] [0 1] sage: (~I).parent() Full MatrixSpace of 2 by 2 dense matrices over Rational Field
This is analogous to the situation for ring elements, e.g., for
we have:
sage: parent(~1) Rational Field
) |
) |
Return matrix mod
, returning again a matrix over the same
base ring.
Note:
Use A.Mod(p)
to obtain a matrix over the residue
class ring modulo
.
sage: M = Matrix(ZZ, 2, 2, [5, 9, 13, 15]) sage: M % 7 [5 2] [6 1] sage: parent(M % 7) Full MatrixSpace of 2 by 2 dense matrices over Integer Ring
) |
Return the negative of self.
sage: a = matrix(ZZ,2,range(4)) sage: a.__neg__() [ 0 -1] [-2 -3] sage: -a [ 0 -1] [-2 -3]
) |
Return +self, which is just self, of course.
sage: a = matrix(ZZ,2,range(4)) sage: +a [0 1] [2 3] sage: a.__pos__() [0 1] [2 3]
) |
sage: a = matrix(Integers(8),3,range(9)) sage: a == loads(dumps(a)) True
) |
) |
Clear anything cached about this matrix.
) |
Unsafe version of the dict method, mainly for internal use. This may return the dict of elements, but as an *unsafe* reference to the underlying dict of the object. It is might be dangerous if you change entries of the returned dict.
Using _dict is potentially fast and memory efficient, but very dangerous (at least for generic sparse matrices).
sage: a = matrix(QQ['x,y'],2,range(6), sparse=True); a [0 1 2] [3 4 5] sage: v = a._dict(); v {(0, 1): 1, (1, 2): 5, (1, 0): 3, (0, 2): 2, (1, 1): 4}
If you change a key of the dictionary, the corresponding entry of the matrix will be changed (but without clearing any caches of computing information about the matrix):
sage: v[0,1] = -2/3; v {(0, 1): -2/3, (1, 2): 5, (1, 0): 3, (0, 2): 2, (1, 1): 4} sage: a._dict() {(0, 1): -2/3, (1, 2): 5, (1, 0): 3, (0, 2): 2, (1, 1): 4} sage: a[0,1] -2/3
But the matrix doesn't know the entry changed, so it returns the cached version of its print representation:
sage: a [0 1 2] [3 4 5]
If we change an entry, the cache is cleared, and the correct print representation appears:
sage: a[1,2]=10 sage: a [ 0 -2/3 2] [ 3 4 10]
) |
) |
Return latex representation of this matrix.
sage: R = PolynomialRing(QQ,4,'z') sage: a = matrix(2,2, R.gens()) sage: b = a*a sage: latex(b) \left(\begin{array}{rr} z_{0}^{2} + z_{1} z_{2} \& z_{0} z_{1} + z_{1} z_{3} \\ z_{0} z_{2} + z_{2} z_{3} \& z_{1} z_{2} + z_{3}^{2} \end{array}\right)
) |
Unsafe version of the list method, mainly for internal use. This may return the list of elements, but as an *unsafe* reference to the underlying list of the object. It is might be dangerous if you change entries of the returned list.
Using _list is potentially fast and memory efficient, but very dangerous (at least for generic dense matrices).
sage: a = matrix(QQ['x,y'],2,range(6)); a [0 1 2] [3 4 5] sage: v = a._list(); v [0, 1, 2, 3, 4, 5]
If you change an entry of the list, the corresponding entry of the matrix will be changed (but without clearing any caches of computing information about the matrix):
sage: v[0] = -2/3; v [-2/3, 1, 2, 3, 4, 5] sage: a._list() [-2/3, 1, 2, 3, 4, 5]
Now the 0,0 entry of the matrix is
, which is weird.
sage: a[0,0] -2/3
See:
sage: a [-2/3 1 2] [ 3 4 5]
) |
sage: A = Matrix(ZZ[['t']], 2, 2, range(4)) sage: A.parent() Full MatrixSpace of 2 by 2 dense matrices over Power Series Ring in t over Integer Ring sage: A._matrix_(QQ[['t']]) [0 1] [2 3] sage: A._matrix_(QQ[['t']]).parent() Full MatrixSpace of 2 by 2 dense matrices over Power Series Ring in t over Rational Field
) |
Returns the list of pairs (i,j) such that self[i,j] != 0, but sorted by columns, i.e., column j=0 entries occur first, then column j=1 entries, etc.
It is safe to change the resulting list (unless you give the option copy=False).
) |
) |
) |
Set row i of self to -(row r of A), but where we only take the given column positions in that row of A. We do not zero out the other entries of self's row i either.
Input:
sage: a = matrix(QQ,2,3,range(6)); a [0 1 2] [3 4 5] sage: a._set_row_to_negative_of_row_of_A_using_subset_of_columns(0,a,1,[1,2]) sage: a [-4 -5 2] [ 3 4 5]
) |
See About this document... for information on suggesting changes.