Base class for matrices, part 1

For design documentation see sage.matrix.docs.

TESTS:

sage: A = Matrix(GF(5),3,3,srange(9))
sage: A == loads(dumps(A))
True
class sage.matrix.matrix1.Matrix
static __new__()
T.__new__(S, ...) -> a new object with type S, a subtype of T
_gap_init_()

Returns a string defining a gap representation of self.

EXAMPLES:

sage: A = MatrixSpace(QQ,3,3)([0,1,2,3,4,5,6,7,8])
sage: g=gap(A) # indirect doctest
sage: g
[ [ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ] ]
sage: g.CharacteristicPolynomial()
x_1^3-12*x_1^2-18*x_1
sage: A = MatrixSpace(CyclotomicField(4),2,2)([0,1,2,3])
sage: g=gap(A)
sage: g
[ [ !0, !1 ], [ !2, !3 ] ]
sage: g.IsMatrix()
true
_macaulay2_()

EXAMPLES:

sage: m = matrix(ZZ, [[1,2],[3,4]])
sage: macaulay2(m)                  #optional (indirect doctest)
| 1 2 |
| 3 4 |
sage: R.<x,y> = QQ[]
sage: m = matrix([[x,y],[1+x,1+y]])
sage: macaulay2(m)                  #optional
| x   y   |
| x+1 y+1 |
_magma_init_()

Return a string that evaluates in the given Magma session to this matrix.

EXAMPLES:

We first coerce a square matrix.

sage: A = MatrixSpace(QQ,3)([1,2,3,4/3,5/3,6/4,7,8,9])
sage: B = magma(A); B                       # (indirect doctest) optional - magma
[  1   2   3]
[4/3 5/3 3/2]
[  7   8   9]
sage: B.Type()                              # optional - magma
AlgMatElt
sage: B.Parent()                            # optional - magma
Full Matrix Algebra of degree 3 over Rational Field

We coerce a non-square matrix over \ZZ/8\ZZ.

sage: A = MatrixSpace(Integers(8),2,3)([-1,2,3,4,4,-2])   
sage: B = magma(A); B                       # optional - magma
[7 2 3]
[4 4 6]
sage: B.Type()                              # optional - magma
ModMatRngElt
sage: B.Parent()                            # optional - magma
Full RMatrixSpace of 2 by 3 matrices over IntegerRing(8)
sage: R.<x,y> = QQ[]
sage: A = MatrixSpace(R,2,2)([x+y,x-1,y+5,x*y]) 
sage: B = magma(A); B                       # optional - magma
[x + y x - 1]
[y + 5   x*y]
sage: R.<x,y> = ZZ[]
sage: A = MatrixSpace(R,2,2)([x+y,x-1,y+5,x*y]) 
sage: B = magma(A); B                       # optional - magma
[x + y x - 1]
[y + 5   x*y]

We coerce a matrix over a cyclotomic field, where the generator must be named during the coercion.

sage: K = CyclotomicField(9) ; z = K.0
sage: M = matrix(K,3,3,[0,1,3,z,z**4,z-1,z**17,1,0])
sage: M
[                 0                  1                  3]
[             zeta9            zeta9^4          zeta9 - 1]
[-zeta9^5 - zeta9^2                  1                  0]
sage: magma(M)                             # optional - magma
[                 0                  1                  3]
[             zeta9            zeta9^4          zeta9 - 1]
[-zeta9^5 - zeta9^2                  1                  0]
sage: magma(M**2) == magma(M)**2           # optional - magma
True
_maple_init_()

Return a Maple string representation of this matrix.

EXAMPLES:

sage: M = matrix(ZZ,2,range(4))             #optional
sage: maple(M)                              #optional (indirect doctest)
Matrix(2, 2, [[0,1],[2,3]])
sage: M = matrix(QQ,3,[1,2,3,4/3,5/3,6/4,7,8,9])    #optional
sage: maple(M)                                      #optional
Matrix(3, 3, [[1,2,3],[4/3,5/3,3/2],[7,8,9]])
sage: P.<x> = ZZ[]                          #optional
sage: M = matrix(P, 2, [-9*x^2-2*x+2, x-1, x^2+8*x, -3*x^2+5]) #optional
sage: maple(M)                             #optional
Matrix(2, 2, [[-9*x^2-2*x+2,x-1],[x^2+8*x,-3*x^2+5]])
_mathematica_init_()

Return Mathematica string representation of this matrix.

EXAMPLES:

sage: A = MatrixSpace(QQ,3)([1,2,3,4/3,5/3,6/4,7,8,9])
sage: g = mathematica(A); g                  # optional
{{1, 2, 3}, {4/3, 5/3, 3/2}, {7, 8, 9}}
sage: A._mathematica_init_()
'{{1/1, 2/1, 3/1}, {4/3, 5/3, 3/2}, {7/1, 8/1, 9/1}}'
sage: A = matrix([[1,2],[3,4]])
sage: g = mathematica(A); g                  # optional
{{1, 2}, {3, 4}}
sage: a = matrix([[pi, sin(x)], [cos(x), 1/e]]); a
[    pi sin(x)]
[cos(x) e^(-1)]
sage: a._mathematica_init_()
'{{Pi, Sin[x]}, {Cos[x], Exp[-1]}}'
_maxima_init_()

Return a string representation of this matrix in Maxima.

EXAMPLES:

sage: m = matrix(3,range(9)); m
[0 1 2]
[3 4 5]
[6 7 8]
sage: m._maxima_init_()
'matrix([0,1,2],[3,4,5],[6,7,8])'
sage: a = maxima(m); a
matrix([0,1,2],[3,4,5],[6,7,8])
sage: a.charpoly('x').expand()
-x^3+12*x^2+18*x
sage: m.charpoly()
x^3 - 12*x^2 - 18*x
_pari_()

Return the Pari matrix corresponding to self.

EXAMPLES:

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 = pari(a); b  # indirect doctest
[x + 1, 2/3; 1/2*x^2, x^3 + 1]
sage: a.determinant()
x^4 + x^3 - 1/3*x^2 + x + 1
sage: b.matdet()
x^4 + x^3 - 1/3*x^2 + x + 1

This function preserves precision for entries of inexact type (e.g. reals):

sage: R = RealField(4)       # 4 bits of precision
sage: a = matrix(R, 2, [1, 2, 3, 1]); a
[1.0 2.0]
[3.0 1.0]
sage: b = pari(a); b
[1.000000000, 2.000000000; 3.000000000, 1.000000000] # 32-bit
[1.00000000000000, 2.00000000000000; 3.00000000000000, 1.00000000000000] # 64-bit
sage: b[0][0].precision()    # in words
3
_pari_init_()

Return a string defining a GP representation of self.

EXAMPLES:

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 = gp(a); b   # indirect doctest
[x + 1, 2/3; 1/2*x^2, x^3 + 1]
sage: a.determinant()
x^4 + x^3 - 1/3*x^2 + x + 1
sage: b.matdet()
x^4 + x^3 - 1/3*x^2 + x + 1
_sage_input_()

Produce an expression which will reproduce this value when evaluated.

EXAMPLES:

sage: sage_input(matrix(QQ, 3, 3, [5..13])/7, verify=True)
# Verified
matrix(QQ, [[5/7, 6/7, 1], [8/7, 9/7, 10/7], [11/7, 12/7, 13/7]])
sage: sage_input(MatrixSpace(GF(5), 50, 50, sparse=True).random_element(density=0.002), verify=True)
# Verified
matrix(GF(5), 50, 50, {(7,43):4, (29,44):3, (35,4):4})
sage: from sage.misc.sage_input import SageInputBuilder
sage: matrix(RDF, [[3, 1], [4, 1]])._sage_input_(SageInputBuilder(), False)
{call: {atomic:matrix}({atomic:RDF}, {list: ({list: ({atomic:3}, {atomic:1})}, {list: ({atomic:4}, {atomic:1})})})}
sage: matrix(ZZ, 50, 50, {(9,17):1})._sage_input_(SageInputBuilder(), False)
{call: {atomic:matrix}({atomic:ZZ}, {atomic:50}, {atomic:50}, {dict: {{atomic:(9,17)}:{atomic:1}}})}

TESTS:

sage: sage_input(matrix(RR, 0, 3, []), verify=True)
# Verified
matrix(RR, 0, 3)
sage: sage_input(matrix(RR, 3, 0, []), verify=True)
# Verified
matrix(RR, 3, 0)
sage: sage_input(matrix(RR, 0, 0, []), verify=True)
# Verified
matrix(RR, 0, 0)
_scilab_()

Creates a ScilabElement object based on self and returns it.

EXAMPLES:

sage: a = matrix([[1,2,3],[4,5,6],[7,8,9]]); a # optional - scilab [1 2 3] [4 5 6] [7 8 9] sage: b = scilab(a); b # optional - scilab (indirect doctest)

1. 2. 3. 4. 5. 6. 7. 8. 9.

AUTHORS:

  • Ronan Paixao (2008-12-12)
_scilab_init_()

Returns a string defining a Scilab representation of self.

EXAMPLES:

sage: a = matrix([[1,2,3],[4,5,6],[7,8,9]]); a # optional - scilab [1 2 3] [4 5 6] [7 8 9] sage: a._scilab_init_() # optional - scilab ‘[1,2,3;4,5,6;7,8,9]’

AUTHORS:

  • Ronan Paixao (2008-12-12)
_singular_()
Tries to coerce this matrix to a singular matrix.
adjoint()

Returns the adjoint matrix of self (matrix of cofactors).

INPUT:

  • M - a square matrix

OUTPUT:

  • N - the adjoint matrix, such that N * M = M * N = M.parent(M.det())

ALGORITHM: Use PARI

EXAMPLES:

sage: M = Matrix(ZZ,2,2,[5,2,3,4]) ; M
[5 2]
[3 4]
sage: N = M.adjoint() ; N
[ 4 -2]
[-3  5]
sage: M * N
[14  0]
[ 0 14]
sage: N * M
[14  0]
[ 0 14]
sage: M = Matrix(QQ,2,2,[5/3,2/56,33/13,41/10]) ; M
[  5/3  1/28]
[33/13 41/10]
sage: N = M.adjoint() ; N
[ 41/10  -1/28]
[-33/13    5/3]
sage: M * N
[7363/1092         0]
[        0 7363/1092]

TODO: Only implemented for matrices over ZZ or QQ PARI can deal with more general base rings

augment()

Return the augmented matrix of the form:

[self | other].

EXAMPLES:

sage: M = MatrixSpace(QQ,2,2)
sage: A = M([1,2, 3,4])
sage: A
[1 2]
[3 4]
sage: N = MatrixSpace(QQ,2,1)
sage: B = N([9,8])
sage: B
[9]
[8]
sage: A.augment(B)
[1 2 9]
[3 4 8]
sage: B.augment(A)
[9 1 2]
[8 3 4]
sage: M = MatrixSpace(QQ,3,4)
sage: A = M([1,2,3,4, 0,9,8,7, 2/3,3/4,4/5,9/8])
sage: A
[  1   2   3   4]
[  0   9   8   7]
[2/3 3/4 4/5 9/8]
sage: N = MatrixSpace(QQ,3,2)
sage: B = N([1,2, 3,4, 4,5])
sage: B
[1 2]
[3 4]
[4 5]
sage: A.augment(B)
[  1   2   3   4   1   2]
[  0   9   8   7   3   4]
[2/3 3/4 4/5 9/8   4   5]
sage: B.augment(A)
[  1   2   1   2   3   4]
[  3   4   0   9   8   7]
[  4   5 2/3 3/4 4/5 9/8]

AUTHORS:

  • Naqi Jaffery (2006-01-24): examples
block_sum()

Return the block matrix that has self and other on the diagonal:

[ self     0 ]
[    0 other ]

EXAMPLES:

sage: A = matrix(QQ[['t']], 2, range(1, 5))
sage: A.block_sum(100*A)
[  1   2   0   0]
[  3   4   0   0]
[  0   0 100 200]
[  0   0 300 400]
column()

Return the i‘th column of this matrix as a vector.

This column is a dense vector if and only if the matrix is a dense matrix.

INPUT:

  • i - integer
  • from_list - bool (default: False); if true, returns the i‘th element of self.columns() (see columns()), which may be faster, but requires building a list of all columns the first time it is called after an entry of the matrix is changed.

EXAMPLES:

sage: a = matrix(2,3,range(6)); a
[0 1 2]
[3 4 5]
sage: a.column(1)
(1, 4)

If the column is negative, it wraps around, just like with list indexing, e.g., -1 gives the right-most column:

sage: a.column(-1)
(2, 5)

TESTS:

sage: a = matrix(2,3,range(6)); a
[0 1 2]
[3 4 5]
sage: a.column(3)
...
IndexError: column index out of range
sage: a.column(-4)
...
IndexError: column index out of range
columns()

Return a list of the columns of self.

INPUT:

  • copy - (default: True) if True, return a copy of the list

    of columns which is safe to change.

If self is sparse, returns columns as sparse vectors, and if self is dense returns them as dense vectors.

EXAMPLES:

sage: matrix(3, [1..9]).columns()
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
sage: matrix(RR, 2, [sqrt(2), pi, exp(1), 0]).columns()
[(1.41421356237310, 2.71828182845905), (3.14159265358979, 0.000000000000000)]
sage: matrix(RR, 0, 2, []).columns()
[(), ()]
sage: matrix(RR, 2, 0, []).columns()
[]
sage: m = matrix(RR, 3, 3, {(1,2): pi, (2, 2): -1, (0,1): sqrt(2)})
sage: parent(m.columns()[0])
Sparse vector space of dimension 3 over Real Field with 53 bits of precision
dense_columns()

Return list of the dense columns of self.

INPUT:

  • copy - (default: True) if True, return a copy so you can modify it safely

EXAMPLES:

An example over the integers:

sage: a = matrix(3,3,range(9)); a
[0 1 2]
[3 4 5]
[6 7 8]
sage: a.dense_columns()
[(0, 3, 6), (1, 4, 7), (2, 5, 8)]        

We do an example over a polynomial ring:

sage: R.<x> = QQ[ ]
sage: a = matrix(R, 2, [x,x^2, 2/3*x,1+x^5]); a
[      x     x^2]
[  2/3*x x^5 + 1]
sage: a.dense_columns()
[(x, 2/3*x), (x^2, x^5 + 1)]
sage: a = matrix(R, 2, [x,x^2, 2/3*x,1+x^5], sparse=True)
sage: c = a.dense_columns(); c
[(x, 2/3*x), (x^2, x^5 + 1)]
sage: parent(c[1])
Ambient free module of rank 2 over the principal ideal domain Univariate Polynomial Ring in x over Rational Field
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.

EXAMPLES:

sage: A = MatrixSpace(QQ,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

In Sage, the product of a sparse and a dense matrix is always dense:

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 dense matrices over Rational Field

TESTS:

Make sure that subdivisions are preserved when switching between dense and sparse matrices:

sage: a = matrix(ZZ, 3, range(9))
sage: a.subdivide([1,2],2)
sage: a.get_subdivisions()
([1, 2], [2])
sage: b = a.sparse_matrix().dense_matrix()
sage: b.get_subdivisions()
([1, 2], [2])
dense_rows()

Return list of the dense rows of self.

INPUT:

  • copy - (default: True) if True, return a copy so you can modify it safely (note that the individual vectors in the copy should not be modified since they are mutable!)

EXAMPLES:

sage: m = matrix(3, range(9)); m
[0 1 2]
[3 4 5]
[6 7 8]
sage: v = m.dense_rows(); v
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]
sage: v is m.dense_rows()
False
sage: m.dense_rows(copy=False) is m.dense_rows(copy=False)
True
sage: m[0,0] = 10
sage: m.dense_rows()
[(10, 1, 2), (3, 4, 5), (6, 7, 8)]
lift()

Return lift of self to the covering ring of the base ring R, which is by definition the ring returned by calling cover_ring() on R, or just R itself if the cover_ring method is not defined.

EXAMPLES:

sage: M = Matrix(Integers(7), 2, 2, [5, 9, 13, 15]) ; M
[5 2]
[6 1]
sage: M.lift()
[5 2]
[6 1]
sage: parent(M.lift())
Full MatrixSpace of 2 by 2 dense matrices over Integer Ring

The field QQ doesn’t have a cover_ring method:

sage: hasattr(QQ, 'cover_ring')
False

So lifting a matrix over QQ gives back the same exact matrix.

sage: B = matrix(QQ, 2, [1..4])
sage: B.lift()
[1 2]
[3 4]
sage: B.lift() is B
True
matrix_from_columns()

Return the matrix constructed from self using columns with indices in the columns list.

EXAMPLES:

sage: M = MatrixSpace(Integers(8),3,3)
sage: A = M(range(9)); A
[0 1 2]
[3 4 5]
[6 7 0]
sage: A.matrix_from_columns([2,1])
[2 1]
[5 4]
[0 7]
matrix_from_rows()

Return the matrix constructed from self using rows with indices in the rows list.

EXAMPLES:

sage: M = MatrixSpace(Integers(8),3,3)
sage: A = M(range(9)); A
[0 1 2]
[3 4 5]
[6 7 0]
sage: A.matrix_from_rows([2,1])
[6 7 0]
[3 4 5]
matrix_from_rows_and_columns()

Return the matrix constructed from self from the given rows and columns.

EXAMPLES:

sage: M = MatrixSpace(Integers(8),3,3)
sage: A = M(range(9)); A
[0 1 2]
[3 4 5]
[6 7 0]
sage: A.matrix_from_rows_and_columns([1], [0,2])
[3 5]
sage: A.matrix_from_rows_and_columns([1,2], [1,2])
[4 5]
[7 0]

Note that row and column indices can be reordered or repeated:

sage: A.matrix_from_rows_and_columns([2,1], [2,1])
[0 7]
[5 4]

For example here we take from row 1 columns 2 then 0 twice, and do this 3 times.

sage: A.matrix_from_rows_and_columns([1,1,1],[2,0,0])
[5 3 3]
[5 3 3]
[5 3 3]

AUTHORS:

  • Jaap Spies (2006-02-18)
  • Didier Deshommes: some Pyrex speedups implemented
matrix_over_field()

Return copy of this matrix, but with entries viewed as elements of the fraction field of the base ring (assuming it is defined).

EXAMPLES:

sage: A = MatrixSpace(IntegerRing(),2)([1,2,3,4])
sage: B = A.matrix_over_field()
sage: B
[1 2]
[3 4]
sage: B.parent()
Full MatrixSpace of 2 by 2 dense matrices over Rational Field
matrix_space()

Return the ambient matrix space of self.

INPUT:

  • nrows, ncols - (optional) number of rows and columns in returned matrix space.
  • sparse - whether the returned matrix space uses sparse or dense matrices.

EXAMPLES:

sage: m = matrix(3, [1..9])
sage: m.matrix_space()
Full MatrixSpace of 3 by 3 dense matrices over Integer Ring
sage: m.matrix_space(ncols=2)
Full MatrixSpace of 3 by 2 dense matrices over Integer Ring
sage: m.matrix_space(1)      
Full MatrixSpace of 1 by 3 dense matrices over Integer Ring
sage: m.matrix_space(1, 2, True) 
Full MatrixSpace of 1 by 2 sparse matrices over Integer Ring
new_matrix()

Create a matrix in the parent of this matrix with the given number of rows, columns, etc. The default parameters are the same as for self.

INPUT:

These three variables get sent to matrix_space():

  • nrows, ncols - number of rows and columns in returned matrix. If not specified, defaults to None and will give a matrix of the same size as self.
  • sparse - whether returned matrix is sparse or not. Defaults to same value as self.

The remaining three variables (coerce, entries, and copy) are used by sage.matrix.matrix_space.MatrixSpace() to construct the new matrix.

Warning

This function called with no arguments returns the zero matrix of the same dimension and sparseness of self.

EXAMPLES:

sage: A = matrix(ZZ,2,2,[1,2,3,4]); A [1 2] [3 4] sage: A.new_matrix() [0 0] [0 0] sage: A.new_matrix(1,1) [0] sage: A.new_matrix(3,3).parent() Full MatrixSpace of 3 by 3 dense matrices over Integer Ring
sage: A = matrix(RR,2,3,[1.1,2.2,3.3,4.4,5.5,6.6]); A
[1.10000000000000 2.20000000000000 3.30000000000000]
[4.40000000000000 5.50000000000000 6.60000000000000]
sage: A.new_matrix()
[0.000000000000000 0.000000000000000 0.000000000000000]
[0.000000000000000 0.000000000000000 0.000000000000000]
sage: A.new_matrix().parent()
Full MatrixSpace of 2 by 3 dense matrices over Real Field with 53 bits of precision
numpy()

Return the Numpy matrix associated to this matrix.

INPUT:

  • dtype - The desired data-type for the array. If not given, then the type will be determined as the minimum type required to hold the objects in the sequence.

EXAMPLES:

sage: a = matrix(3,range(12))
sage: a.numpy()
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
sage: a.numpy('f')
array([[  0.,   1.,   2.,   3.],
       [  4.,   5.,   6.,   7.],
       [  8.,   9.,  10.,  11.]], dtype=float32)
sage: a.numpy('d')
array([[  0.,   1.,   2.,   3.],
       [  4.,   5.,   6.,   7.],
       [  8.,   9.,  10.,  11.]])
sage: a.numpy('B')
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]], dtype=uint8)

Type numpy.typecodes for a list of the possible typecodes:

sage: import numpy
sage: sorted(numpy.typecodes.items())
[('All', '?bhilqpBHILQPfdgFDGSUVO'), ('AllFloat', 'fdgFDG'), ('AllInteger', 'bBhHiIlLqQpP'), ('Character', 'c'), ('Complex', 'FDG'), ('Float', 'fdg'), ('Integer', 'bhilqp'), ('UnsignedInteger', 'BHILQP')]
row()

Return the i‘th row of this matrix as a vector.

This row is a dense vector if and only if the matrix is a dense matrix.

INPUT:

  • i - integer
  • from_list - bool (default: False); if true, returns the i‘th element of self.rows() (see rows()), which may be faster, but requires building a list of all rows the first time it is called after an entry of the matrix is changed.

EXAMPLES:

sage: a = matrix(2,3,range(6)); a
[0 1 2]
[3 4 5]
sage: a.row(0)
(0, 1, 2)
sage: a.row(1)
(3, 4, 5)
sage: a.row(-1)  # last row
(3, 4, 5)

TESTS:

sage: a = matrix(2,3,range(6)); a
[0 1 2]
[3 4 5]
sage: a.row(2)
...
IndexError: row index out of range
sage: a.row(-3)
...
IndexError: row index out of range
rows()

Return a list of the rows of self.

INPUT:

  • copy - (default: True) if True, return a copy of the list of rows which is safe to change.

If self is sparse, returns rows as sparse vectors, and if self is dense returns them as dense vectors.

EXAMPLES:

sage: matrix(3, [1..9]).rows()
[(1, 2, 3), (4, 5, 6), (7, 8, 9)]
sage: matrix(RR, 2, [sqrt(2), pi, exp(1), 0]).rows()
[(1.41421356237310, 3.14159265358979), (2.71828182845905, 0.000000000000000)]
sage: matrix(RR, 0, 2, []).rows()
[]
sage: matrix(RR, 2, 0, []).rows()
[(), ()]
sage: m = matrix(RR, 3, 3, {(1,2): pi, (2, 2): -1, (0,1): sqrt(2)})
sage: parent(m.rows()[0])
Sparse vector space of dimension 3 over Real Field with 53 bits of precision
set_column()

Sets the entries of column col in self to be the entries of v.

EXAMPLES:

sage: A = matrix([[1,2],[3,4]]); A
[1 2]
[3 4]
sage: A.set_column(0, [0,0]); A
[0 2]
[0 4]
sage: A.set_column(1, [0,0]); A
[0 0]
[0 0]
sage: A.set_column(2, [0,0]); A
...
IndexError: index out of range
sage: A.set_column(0, [0,0,0])
...
ValueError: v must be of length 2
set_row()

Sets the entries of row row in self to be the entries of v.

EXAMPLES:

sage: A = matrix([[1,2],[3,4]]); A
[1 2]
[3 4]
sage: A.set_row(0, [0,0]); A
[0 0]
[3 4]
sage: A.set_row(1, [0,0]); A
[0 0]
[0 0]
sage: A.set_row(2, [0,0]); A
...
IndexError: index out of range
sage: A.set_row(0, [0,0,0])
...
ValueError: v must be of length 2
sparse_columns()

Return list of the sparse columns of self.

INPUT:

  • copy - (default: True) if True, return a copy so you can

    modify it safely

EXAMPLES:

sage: a = matrix(2,3,range(6)); a
[0 1 2]
[3 4 5]
sage: v = a.sparse_columns(); v
[(0, 3), (1, 4), (2, 5)]
sage: v[1].is_sparse()
True
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.

EXAMPLES:

sage: A = MatrixSpace(QQ,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 dense matrices over Rational Field
sparse_rows()

Return list of the sparse rows of self.

INPUT:

  • copy - (default: True) if True, return a copy so you can

    modify it safely

EXAMPLES:

sage: m = Mat(ZZ,3,3,sparse=True)(range(9)); m
[0 1 2]
[3 4 5]
[6 7 8]
sage: v = m.sparse_rows(); v
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]
sage: m.sparse_rows(copy=False) is m.sparse_rows(copy=False)
True
sage: v[1].is_sparse()
True
sage: m[0,0] = 10
sage: m.sparse_rows()
[(10, 1, 2), (3, 4, 5), (6, 7, 8)]
stack()

Return the augmented matrix self on top of other:

[ self  ]
[ other ]

EXAMPLES:

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

Return the matrix constructed from self using the specified range of rows and columns.

INPUT:

  • row, col - index of the starting row and column. Indices start at zero.
  • nrows, ncols - (optional) number of rows and columns to take. If not provided, take all rows below and all columns to the right of the starting entry.

SEE ALSO:

The functions matrix_from_rows(), matrix_from_columns(), and matrix_from_rows_and_columns() allow one to select arbitrary subsets of rows and/or columns.

EXAMPLES:

Take the 3 \times 3 submatrix starting from entry (1,1) in a 4 \times 4 matrix:

sage: m = matrix(4, [1..16])
sage: m.submatrix(1, 1)
[ 6  7  8]
[10 11 12]
[14 15 16]

Same thing, except take only two rows:

sage: m.submatrix(1, 1, 2)
[ 6  7  8]
[10 11 12]

And now take only one column:

sage: m.submatrix(1, 1, 2, 1)
[ 6]
[10]

You can take zero rows or columns if you want:

sage: m.submatrix(1, 1, 0)
[]
sage: parent(m.submatrix(1, 1, 0))
Full MatrixSpace of 0 by 3 dense matrices over Integer Ring

Previous topic

Base class for matrices, part 0

Next topic

Base class for matrices, part 2

This Page