In [2]:
Copied!
from sage.all import *
v = vector(range(8))
v
from sage.all import *
v = vector(range(8))
v
Out[2]:
(0, 1, 2, 3, 4, 5, 6, 7)
In [4]:
Copied!
# v.norm(self, p=__two__): 范式
v.norm()
# v.norm(self, p=__two__): 范式
v.norm()
Out[4]:
2*sqrt(35)
In [45]:
Copied!
# from one/two dimensional list of
M_ = Matrix(2, range(6))
M = Matrix(Zmod(11), [[1, 2], [3, 4]])
M_, M, ~M, M.nrows(), M.ncols(), M.parent(), M.base_ring(), vector(M), M.det()
# from one/two dimensional list of
M_ = Matrix(2, range(6))
M = Matrix(Zmod(11), [[1, 2], [3, 4]])
M_, M, ~M, M.nrows(), M.ncols(), M.parent(), M.base_ring(), vector(M), M.det()
Out[45]:
([0 1 2] [3 4 5], [1 2] [3 4], [9 1] [7 5], 2, 2, Full MatrixSpace of 2 by 2 dense matrices over Ring of integers modulo 11, Ring of integers modulo 11, (1, 2, 3, 4), 9)
special matrix¶
In [16]:
Copied!
# block matrix
Ms = MatrixSpace(QQ, 2, 2)
A = Ms([1, 2, 3, 4])
B = ~A
I = Ms.one()
O = Ms.zero()
x = polygen(ZZ)
block_matrix([[I, A] , [B, O]]), block_matrix(2, 2, [I, A, B, O]), block_matrix(2, 2, [2, A, B, x])
# block matrix
Ms = MatrixSpace(QQ, 2, 2)
A = Ms([1, 2, 3, 4])
B = ~A
I = Ms.one()
O = Ms.zero()
x = polygen(ZZ)
block_matrix([[I, A] , [B, O]]), block_matrix(2, 2, [I, A, B, O]), block_matrix(2, 2, [2, A, B, x])
Out[16]:
([ 1 0| 1 2] [ 0 1| 3 4] [---------+---------] [ -2 1| 0 0] [ 3/2 -1/2| 0 0], [ 1 0| 1 2] [ 0 1| 3 4] [---------+---------] [ -2 1| 0 0] [ 3/2 -1/2| 0 0], [ 2 0| 1 2] [ 0 2| 3 4] [---------+---------] [ -2 1| x 0] [ 3/2 -1/2| 0 x])
In [17]:
Copied!
# block_diagonal_matrix(A, B) == block_matrix([[A, O], [O, B]])
block_diagonal_matrix(A, B)
# block_diagonal_matrix(A, B) == block_matrix([[A, O], [O, B]])
block_diagonal_matrix(A, B)
Out[17]:
[ 1 2| 0 0] [ 3 4| 0 0] [---------+---------] [ 0 0| -2 1] [ 0 0| 3/2 -1/2]
In [4]:
Copied!
# https://en.wikipedia.org/wiki/Jordan_normal_form
# jordan_form(base_ring=None, sparse=False, subdivide=True, transformation=False, eigenvalues=None, check_input=True)
# A = PJP^{-1} => A^k = PJ^{k}P^{-1}
M = matrix(ZZ, [[ 5 , 4, 2, 1],[ 0, 1, -1, -1],[-1, -1, 3, 0],[ 1, 1, -1, 2]])
J, P = M.jordan_form(transformation=True)
J, P*J*~P, ~P*M*P
# https://en.wikipedia.org/wiki/Jordan_normal_form
# jordan_form(base_ring=None, sparse=False, subdivide=True, transformation=False, eigenvalues=None, check_input=True)
# A = PJP^{-1} => A^k = PJ^{k}P^{-1}
M = matrix(ZZ, [[ 5 , 4, 2, 1],[ 0, 1, -1, -1],[-1, -1, 3, 0],[ 1, 1, -1, 2]])
J, P = M.jordan_form(transformation=True)
J, P*J*~P, ~P*M*P
Out[4]:
( [2|0|0 0] [-+-+---] [0|1|0 0] [ 5 4 2 1] [2 0 0 0] [-+-+---] [ 0 1 -1 -1] [0 1 0 0] [0|0|4 1] [-1 -1 3 0] [0 0 4 1] [0|0|0 4], [ 1 1 -1 2], [0 0 0 4] )
elementary_matrix(R, n, row1=i, row2=j)
The matrix which swaps rows i and j.
elementary_matrix(R, n, row1=i, scale=s)
The matrix which multiplies row i by s.
elementary_matrix(R, n, row1=i, row2=j, scale=s)
The matrix which multiplies row j by s and adds it to row i.
In [18]:
Copied!
A = matrix(ZZ, 4, 10, range(40)); A
A = matrix(ZZ, 4, 10, range(40)); A
Out[18]:
[ 0 1 2 3 4 5 6 7 8 9] [10 11 12 13 14 15 16 17 18 19] [20 21 22 23 24 25 26 27 28 29] [30 31 32 33 34 35 36 37 38 39]
In [29]:
Copied!
E = elementary_matrix(4, row1=1, row2=3, scale=Integer(2))
E, E * A # 左乘行变换
E = elementary_matrix(4, row1=1, row2=3, scale=Integer(2))
E, E * A # 左乘行变换
Out[29]:
([1 0 0 0] [0 1 0 2] [0 0 1 0] [0 0 0 1], [ 0 1 2 3 4 5 6 7 8 9] [70 73 76 79 82 85 88 91 94 97] [20 21 22 23 24 25 26 27 28 29] [30 31 32 33 34 35 36 37 38 39])
In [ ]:
Copied!
# ones() / ones_matrix()
# zero() / zero_matrix()
ones_matrix(2, 3), zero_matrix(2, 3)
# ones() / ones_matrix()
# zero() / zero_matrix()
ones_matrix(2, 3), zero_matrix(2, 3)
Out[ ]:
([1 1 1] [1 1 1], [0 0 0] [0 0 0])
Matrix space¶
https://doc.sagemath.org/html/en/reference/matrices/sage/matrix/matrix_space.html
创建矩阵空间 ¶
In [2]:
Copied!
from sage.all import *
from sage.matrix.matrix_space import is_MatrixSpace
# MatrixSpace(base_ring, nrows, ncols, sparse, implementation)
Ms = MatrixSpace(Zmod(0x10001), 3, 3, sparse=True)
MsT = Ms.transposed
MsC = copy(Ms)
Ms, Ms.is_dense(), Ms.is_finite(), Ms.is_sparse(), MsT, MsC, is_MatrixSpace(MsT)
from sage.all import *
from sage.matrix.matrix_space import is_MatrixSpace
# MatrixSpace(base_ring, nrows, ncols, sparse, implementation)
Ms = MatrixSpace(Zmod(0x10001), 3, 3, sparse=True)
MsT = Ms.transposed
MsC = copy(Ms)
Ms, Ms.is_dense(), Ms.is_finite(), Ms.is_sparse(), MsT, MsC, is_MatrixSpace(MsT)
/tmp/ipykernel_28247/3499655851.py:7: DeprecationWarning: the function is_MatrixSpace is deprecated; use 'isinstance(..., MatrixSpace)' instead See https://github.com/sagemath/sage/issues/37924 for details. Ms, Ms.is_dense(), Ms.is_finite(), Ms.is_sparse(), MsT, MsC, is_MatrixSpace(MsT)
Out[2]:
(Full MatrixSpace of 3 by 3 sparse matrices over Ring of integers modulo 65537, False, True, True, Full MatrixSpace of 3 by 3 sparse matrices over Ring of integers modulo 65537, Full MatrixSpace of 3 by 3 sparse matrices over Ring of integers modulo 65537, True)
创建矩阵 ¶
In [42]:
Copied!
# diagonal_matrix(<vector>) # matrix space must be a square
# one() / identity_matrix() # matrix space must be a square
# zero() / zero_matrix() # matrix space must be a square
Ms.matrix(range(Ms.dimension())), Ms.from_vector(vector(range(Ms.dimension()))), Ms.diagonal_matrix(range(Ms.ncols())), Ms.one(), Ms.zero()
# diagonal_matrix() # matrix space must be a square
# one() / identity_matrix() # matrix space must be a square
# zero() / zero_matrix() # matrix space must be a square
Ms.matrix(range(Ms.dimension())), Ms.from_vector(vector(range(Ms.dimension()))), Ms.diagonal_matrix(range(Ms.ncols())), Ms.one(), Ms.zero()
Out[42]:
([0 1] [2 3], [0 1] [2 3], [0 0] [0 1], [1 0] [0 1], [0 0] [0 0])
In [ ]:
Copied!
# matrix_space(nrows=None, ncols=None, sparse=False)[source]
Ms.matrix_space(4, 3) # 在原矩阵空间基础上小修小改
# matrix_space(nrows=None, ncols=None, sparse=False)[source]
Ms.matrix_space(4, 3) # 在原矩阵空间基础上小修小改
Out[ ]:
Full MatrixSpace of 4 by 3 dense matrices over Ring of integers modulo 65537
属性 ¶
In [49]:
Copied!
# 形状,维度,基环,基,特征
Ms.nrows(), Ms.ncols(), Ms.dims(), Ms.dimension(), Ms.base_ring(), Ms.change_ring(Zmod(0x101)), Ms.characteristic(), Ms.ngens()
# 形状,维度,基环,基,特征
Ms.nrows(), Ms.ncols(), Ms.dims(), Ms.dimension(), Ms.base_ring(), Ms.change_ring(Zmod(0x101)), Ms.characteristic(), Ms.ngens()
Out[49]:
(3, 3, (3, 3), 9, Ring of integers modulo 65537, Full MatrixSpace of 3 by 3 sparse matrices over Ring of integers modulo 257, 65537, 9)
In [53]:
Copied!
A = Ms.basis()
B = list(A) # list of basis of Ms
for i in range(Ms.dimension()):
assert B[i] == Ms.gen(i)
assert Ms.ngens() == Ms.dimension() # 前者是生成元个数,后者是维度,但是二者在数值上相同
type(A), B[0], type(B[0])
A = Ms.basis()
B = list(A) # list of basis of Ms
for i in range(Ms.dimension()):
assert B[i] == Ms.gen(i)
assert Ms.ngens() == Ms.dimension() # 前者是生成元个数,后者是维度,但是二者在数值上相同
type(A), B[0], type(B[0])
Out[53]:
(sage.sets.family.FiniteFamily, [1 0 0] [0 0 0] [0 0 0], sage.matrix.matrix_generic_sparse.Matrix_generic_sparse)
操作 ¶
In [51]:
Copied!
# 变环
Ms.change_ring(QQ), Ms.one(), Ms.zero()
# 变环
Ms.change_ring(QQ), Ms.one(), Ms.zero()
Out[51]:
(Full MatrixSpace of 3 by 3 sparse matrices over Rational Field, [1 0 0] [0 1 0] [0 0 1], [0 0 0] [0 0 0] [0 0 0])
In [46]:
Copied!
A = matrix([[1,2,3],[3,2,1],[1,1,1]])
B = vector([0,-4,-1])
A.solve_right(B)
# 有教程说可以用 A\B 来求解,但是 `the backslash operator has been deprecated See https://github.com/sagemath/sage/issues/36394 for details.`
A = matrix([[1,2,3],[3,2,1],[1,1,1]])
B = vector([0,-4,-1])
A.solve_right(B)
# 有教程说可以用 A\B 来求解,但是 `the backslash operator has been deprecated See https://github.com/sagemath/sage/issues/36394 for details.`
Out[46]:
(-2, 1, 0)