线性代数

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)

如果没有解决方案,则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 也可以使用最大值进行计算(请参见 最大值 (见下文)。

如中所述 基本环 ,在其上定义矩阵的环影响其某些性质。在下面的代码中, 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)]

矩阵空间

We create the space \(\text{Mat}_{3\times 3}(\QQ)\) of 3 times 3 matrices with rational entries:

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

(要指定3x4矩阵的空间,您可以使用 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 的生成矩阵的非零行的约简行梯形形式获得 S

稀疏线性代数

SAGE支持ID上的稀疏线性代数。

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'