线性代数

Sage提供线性代数的标准构造,例如矩阵的特征多项式、梯队形式、迹、分解等。

矩阵和矩阵乘法的创建简单而自然:

sage: A = Matrix([[1,2,3],[3,2,1],[1,1,1]])
sage: w = vector([1,1,-4])
sage: w*A
(0, 0, 0)
sage: A*w
(-9, 1, -2)
sage: kernel(A)
Free module of degree 3 and rank 1 over Integer Ring
Echelon basis matrix:
[ 1  1 -4]

注意在Sage中,矩阵的核心 \(A\) 是“左核”,即向量空间 \(w\) 这样的话 \(wA=0\) .

用这种方法求解矩阵方程很容易 solve_right . 评价 A.solve_right(Y) 返回矩阵(或向量) \(X\) 以便 \(AX=Y\)

sage: Y = vector([0, -4, -1])
sage: X = A.solve_right(Y)
sage: X
(-2, 1, 0)
sage: A * X   # checking our answer...
(0, -4, -1)

反斜杠 \ 可以代替 solve_right 使用 A \ Y 而不是 A.solve_right(Y) .

sage: A \ Y
(-2, 1, 0)

如果没有解决方案,Sage将返回错误:

sage: A.solve_right(w)
Traceback (most recent call last):
...
ValueError: matrix equation has no solutions

同样,使用 A.solve_left(Y) 解决 \(X\) 在里面 \(XA=Y\) .

Sage还可以计算特征值和特征向量:

sage: A = matrix([[0, 4], [-1, 0]])
sage: A.eigenvalues ()
[-2*I, 2*I]
sage: B = matrix([[1, 3], [3, 1]])
sage: B.eigenvectors_left()
[(4, [
(1, 1)
], 1), (-2, [
(1, -1)
], 1)]

(输出的语法 eigenvectors_left 是一个三元组的列表:(特征值,特征向量,多重性)。特征值和特征向量 QQRR 也可以使用Maxima计算(参见 马克西玛 下面)。

如中所述 基本环 ,定义矩阵的环会影响它的一些属性。在下面的第一个参数中 matrix 命令告诉Sage将矩阵视为整数矩阵( ZZ case),有理数矩阵 (QQ ),或实数矩阵 (RR ):

sage: AZ = matrix(ZZ, [[2,0], [0,1]])
sage: AQ = matrix(QQ, [[2,0], [0,1]])
sage: AR = matrix(RR, [[2,0], [0,1]])
sage: AZ.echelon_form()
[2 0]
[0 1]
sage: AQ.echelon_form()
[1 0]
[0 1]
sage: AR.echelon_form()
[ 1.00000000000000 0.000000000000000]
[0.000000000000000  1.00000000000000]

在浮点实数或复数上计算矩阵的特征值和特征向量时,应定义 RDF (实双字段)或 CDF (复数双场)。如果没有指定环,并且使用浮点实数或复数,则默认情况下,在 RRCC 不支持所有情况下的这些计算的字段:

sage: ARDF = matrix(RDF, [[1.2, 2], [2, 3]])
sage: ARDF.eigenvalues()  # rel tol 8e-16
[-0.09317121994613098, 4.293171219946131]
sage: ACDF = matrix(CDF, [[1.2, I], [2, 3]])
sage: ACDF.eigenvectors_right()  # rel tol 3e-15
[(0.8818456983293743 - 0.8209140653434135*I, [(0.7505608183809549, -0.616145932704589 + 0.2387941530333261*I)], 1),
(3.3181543016706256 + 0.8209140653434133*I, [(0.14559469829270957 + 0.3756690858502104*I, 0.9152458258662108)], 1)]

矩阵空间

我们创造空间 \(\text{{Mat}}_{{3\times 3}}(\QQ)\) 属于 3 times 3 带有理项的矩阵:

sage: M = MatrixSpace(QQ,3)
sage: M
Full MatrixSpace of 3 by 3 dense matrices over Rational Field

(要指定3×4矩阵的空间,可以使用 MatrixSpace(QQ,3,4) . 如果省略列数,则默认为行数,因此 MatrixSpace(QQ,3) 是的同义词 MatrixSpace(QQ,3,3) )矩阵空间有其标准基:

sage: B = M.basis()
sage: len(B)
9
sage: B[0,1]
[0 1 0]
[0 0 0]
[0 0 0]

我们创建一个矩阵作为 M .

sage: A = M(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.kernel()
Vector space of degree 3 and dimension 1 over Rational Field
Basis matrix:
[ 1 -2  1]

接下来,我们将说明在有限域上定义的矩阵的计算:

sage: M = MatrixSpace(GF(2),4,8)
sage: A = M([1,1,0,0, 1,1,1,1, 0,1,0,0, 1,0,1,1,
....:        0,0,1,0, 1,1,0,1, 0,0,1,1, 1,1,1,0])
sage: A
[1 1 0 0 1 1 1 1]
[0 1 0 0 1 0 1 1]
[0 0 1 0 1 1 0 1]
[0 0 1 1 1 1 1 0]
sage: rows = A.rows()
sage: A.columns()
[(1, 0, 0, 0), (1, 1, 0, 0), (0, 0, 1, 1), (0, 0, 0, 1),
 (1, 1, 1, 1), (1, 0, 1, 1), (1, 1, 0, 1), (1, 1, 1, 0)]
sage: rows
[(1, 1, 0, 0, 1, 1, 1, 1), (0, 1, 0, 0, 1, 0, 1, 1),
 (0, 0, 1, 0, 1, 1, 0, 1), (0, 0, 1, 1, 1, 1, 1, 0)]

我们把子空间恢复过来 GF{{2}} 由以上几行组成。

sage: V = VectorSpace(GF(2),8)
sage: S = V.subspace(rows)
sage: S
Vector space of degree 8 and dimension 4 over Finite Field of size 2
Basis matrix:
[1 0 0 0 0 1 0 0]
[0 1 0 0 1 0 1 1]
[0 0 1 0 1 1 0 1]
[0 0 0 1 0 0 1 1]
sage: A.echelon_form()
[1 0 0 0 0 1 0 0]
[0 1 0 0 1 0 1 1]
[0 0 1 0 1 1 0 1]
[0 0 0 1 0 0 1 1]

基础 S 所用的Sage是从非零行的归约列梯队形式的矩阵的生成元 S .

稀疏线性代数

Sage支持PIDs上的稀疏线性代数。

sage: M = MatrixSpace(QQ, 100, sparse=True)
sage: A = M.random_element(density = 0.05)
sage: E = A.echelon_form()

Sage中的多模块算法适用于方阵(但不适用于非方阵):

sage: M = MatrixSpace(QQ, 50, 100, sparse=True)
sage: A = M.random_element(density = 0.05)
sage: E = A.echelon_form()
sage: M = MatrixSpace(GF(2), 20, 40, sparse=True)
sage: A = M.random_element()
sage: E = A.echelon_form()

请注意,Python区分大小写:

sage: M = MatrixSpace(QQ, 10,10, Sparse=True)
Traceback (most recent call last):
...
TypeError: __init__() got an unexpected keyword argument 'Sparse'