线性代数

向量空间

这个 VectorSpace 命令创建向量空间类,从中可以创建子空间。请注意,Sage计算的基数是“行减少”。

sage: V = VectorSpace(GF(2),8)
sage: S = V.subspace([V([1,1,0,0,0,0,0,0]),V([1,0,0,0,0,1,1,0])])
sage: S.basis()
[
(1, 0, 0, 0, 0, 1, 1, 0),
(0, 1, 0, 0, 0, 1, 1, 0)
]
sage: S.dimension()
2

矩阵幂

如何在Sage中计算矩阵幂?其语法如下例所示。

sage: R = IntegerModRing(51)
sage: M = MatrixSpace(R,3,3)
sage: A = M([1,2,3, 4,5,6, 7,8,9])
sage: A^1000*A^1007
<BLANKLINE>
[ 3  3  3]
[18  0 33]
[33 48 12]
sage: A^2007
<BLANKLINE>
[ 3  3  3]
[18  0 33]
[33 48 12]

内核

通过将核方法应用于矩阵对象来计算核。以下示例说明了该语法。

sage: M = MatrixSpace(IntegerRing(),4,2)(range(8))
sage: M.kernel()
Free module of degree 4 and rank 2 over Integer Ring
Echelon basis matrix:
[ 1  0 -3  2]
[ 0  1 -2  1]

一维以上的核 \(\QQ\)

sage: A = MatrixSpace(RationalField(),3)(range(9))
sage: A.kernel()
Vector space of degree 3 and dimension 1 over Rational Field
Basis matrix:
[ 1 -2  1]

一个微不足道的内核:

sage: A = MatrixSpace(RationalField(),2)([1,2,3,4])
sage: A.kernel()
Vector space of degree 2 and dimension 0 over Rational Field
Basis matrix:
[]
sage: M = MatrixSpace(RationalField(),0,2)(0)
sage: M
[]
sage: M.kernel()
Vector space of degree 0 and dimension 0 over Rational Field
Basis matrix:
[]
sage: M = MatrixSpace(RationalField(),2,0)(0)
sage: M.kernel()
Vector space of degree 2 and dimension 2 over Rational Field
Basis matrix:
[1 0]
[0 1]

零矩阵的核:

sage: A = MatrixSpace(RationalField(),2)(0)
sage: A.kernel()
Vector space of degree 2 and dimension 2 over Rational Field
Basis matrix:
[1 0]
[0 1]

非方阵的核:

sage: A = MatrixSpace(RationalField(),3,2)(range(6))
sage: A.kernel()
Vector space of degree 3 and dimension 1 over Rational Field
Basis matrix:
[ 1 -2  1]

分圆域上矩阵的2维核:

sage: K = CyclotomicField(12); a = K.gen()
sage: M = MatrixSpace(K,4,2)([1,-1, 0,-2, 0,-a^2-1, 0,a^2-1])
sage: M
[             1            -1]
[             0            -2]
[             0 -zeta12^2 - 1]
[             0  zeta12^2 - 1]
sage: M.kernel()
Vector space of degree 4 and dimension 2 over Cyclotomic Field of order 12
 and degree 4
Basis matrix:
[               0                1                0     -2*zeta12^2]
[               0                0                1 -2*zeta12^2 + 1]

复杂基域上的非平凡核。

sage: K = FractionField(PolynomialRing(RationalField(),2,'x'))
sage: M = MatrixSpace(K, 2)([[K.gen(1),K.gen(0)], [K.gen(1), K.gen(0)]])
sage: M
[x1 x0]
[x1 x0]
sage: M.kernel()
Vector space of degree 2 and dimension 1 over Fraction Field of Multivariate
Polynomial Ring in x0, x1 over Rational Field
Basis matrix:
 [ 1 -1]

整数矩阵的其他方法有 elementary_divisorssmith_form (对于Smith范式), echelon_form 对于Hermite范式, frobenius 对于Frobenius标准型(有理标准形)。

对于域上的矩阵,有许多方法,例如 \(\QQ\) 或有限域: row_spannullitytransposeswap_rowsmatrix_from_columnsmatrix_from_rows ,还有许多其他的。

请参阅该文件 matrix.py 了解更多细节。

特征向量和特征值

如何使用Sage计算特征值和特征向量?

SAGE具有计算特征值以及左、右特征向量和特征空间的全系列函数。如果我们的矩阵是 \(A\) ,然后是 eigenmatrix_right (请回复。 eightmatrix_left )命令还会给出矩阵 \(D\)\(P\) 以至于 \(AP=PD\) (请回复。 \(PA=DP\) 。)

sage: A = matrix(QQ, [[1,1,0],[0,2,0],[0,0,3]])
sage: A
[1 1 0]
[0 2 0]
[0 0 3]
sage: A.eigenvalues()
[3, 2, 1]
sage: A.eigenvectors_right()
[(3, [
(0, 0, 1)
], 1), (2, [
(1, 1, 0)
], 1), (1, [
(1, 0, 0)
], 1)]

sage: A.eigenspaces_right()
[
(3, Vector space of degree 3 and dimension 1 over Rational Field
User basis matrix:
[0 0 1]),
(2, Vector space of degree 3 and dimension 1 over Rational Field
User basis matrix:
[1 1 0]),
(1, Vector space of degree 3 and dimension 1 over Rational Field
User basis matrix:
[1 0 0])
]

sage: D, P = A.eigenmatrix_right()
sage: D
[3 0 0]
[0 2 0]
[0 0 1]
sage: P
[0 1 1]
[0 1 0]
[1 0 0]
sage: A*P == P*D
True

For eigenvalues outside the fraction field of the base ring of the matrix, you can choose to have all the eigenspaces output when the algebraic closure of the field is implemented, such as the algebraic numbers, QQbar. Or you may request just a single eigenspace for each irreducible factor of the characteristic polynomial, since the others may be formed through Galois conjugation. The eigenvalues of the matrix below are $pmsqrt{3}$ and we exhibit each possible output.

此外,目前Sage不实现多精度数值特征值和特征向量,因此从 CCRR 可能会给出不准确和无意义的结果(还会打印警告)。带浮点数项的矩阵的特征值和特征向量 CDFRDF )可以用“特征矩阵”命令获得。**

sage: MS = MatrixSpace(QQ, 2, 2)
sage: A = MS([1,-4,1, -1])
sage: A.eigenspaces_left(format='all')
[
(-1.732050807568878?*I, Vector space of degree 2 and dimension 1 over Algebraic Field
User basis matrix:
[                        1 -1 - 1.732050807568878?*I]),
(1.732050807568878?*I, Vector space of degree 2 and dimension 1 over Algebraic Field
User basis matrix:
[                        1 -1 + 1.732050807568878?*I])
]
sage: A.eigenspaces_left(format='galois')
[
(a0, Vector space of degree 2 and dimension 1 over Number Field in a0 with defining polynomial x^2 + 3
User basis matrix:
[     1 a0 - 1])
]

另一种方法是使用与Maxima的接口:

sage: A = maxima("matrix ([1, -4], [1, -1])")
sage: eig = A.eigenvectors()
sage: eig.sage()
[[[-I*sqrt(3), I*sqrt(3)], [1, 1]], [[[1, 1/4*I*sqrt(3) + 1/4]], [[1, -1/4*I*sqrt(3) + 1/4]]]]

This tells us that \(\vec{v}_1 = [1,(\sqrt{3}i + 1)/4]\) is an eigenvector of \(\lambda_1 = - \sqrt{3}i\) (which occurs with multiplicity one) and \(\vec{v}_2 = [1,(-\sqrt{3}i + 1)/4]\) is an eigenvector of \(\lambda_2 = \sqrt{3}i\) (which also occurs with multiplicity one).

下面是另外两个例子:

sage: A = maxima("matrix ([11, 0, 0], [1, 11, 0], [1, 3, 2])")
sage: A.eigenvectors()
[[[2,11],[1,2]],[[[0,0,1]],[[0,1,1/3]]]]
sage: A = maxima("matrix ([-1, 0, 0], [1, -1, 0], [1, 3, 2])")
sage: A.eigenvectors()
[[[-1,2],[2,1]],[[[0,1,-1]],[[0,0,1]]]]

警告:注意输出的顺序是如何颠倒的,尽管矩阵几乎是相同的。

最后,您还可以使用Sage的Gap界面来计算“有理的”特征值和特征向量:

sage: print(gap.eval("A := [[1,2,3],[4,5,6],[7,8,9]]"))
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
sage: print(gap.eval("v := Eigenvectors( Rationals,A)"))
[ [ 1, -2, 1 ] ]
sage: print(gap.eval("lambda := Eigenvalues( Rationals,A)"))
[ 0 ]

行减少

矩阵的降行梯形计算如下例所示。

sage: M = MatrixSpace(RationalField(),2,3)
sage: A = M([1,2,3, 4,5,6])
sage: A
[1 2 3]
[4 5 6]
sage: A.parent()
Full MatrixSpace of 2 by 3 dense matrices over Rational Field
sage: A[0,2] = 389
sage: A
[  1   2 389]
[  4   5   6]
sage: A.echelon_form()
[      1       0 -1933/3]
[      0       1  1550/3]

特征多项式

特征多项式是方阵的一种Sage方法。

首先是一个矩阵 \(\ZZ\)

sage: A = MatrixSpace(IntegerRing(),2)( [[1,2], [3,4]] )
sage: f = A.charpoly()
sage: f
x^2 - 5*x - 2
sage: f.parent()
Univariate Polynomial Ring in x over Integer Ring

我们计算了多项式环上矩阵的特征多项式 \(\ZZ[a]\)

sage: R = PolynomialRing(IntegerRing(),'a'); a = R.gen()
sage: M = MatrixSpace(R,2)([[a,1], [a,a+1]])
sage: M
[    a     1]
[    a a + 1]
sage: f = M.charpoly()
sage: f
x^2 + (-2*a - 1)*x + a^2
sage: f.parent()
Univariate Polynomial Ring in x over Univariate Polynomial Ring in a over
Integer Ring

sage: M.trace()
2*a + 1
sage: M.determinant()
a^2

计算了多元多项式环上矩阵的特征多项式 \(\ZZ[u,v]\)

sage: R.<u,v> = PolynomialRing(ZZ,2)
sage: A = MatrixSpace(R,2)([u,v,u^2,v^2])
sage: f = A.charpoly(); f
x^2 + (-v^2 - u)*x - u^2*v + u*v^2

要区分这些变量有点困难。为了解决这个问题,我们可能想要重命名不确定的“Z”,我们可以很容易地这样做:

sage: f = A.charpoly('Z'); f
Z^2 + (-v^2 - u)*Z - u^2*v + u*v^2

解线性方程组

使用Maxima,您可以轻松地解线性方程:

sage: var('a,b,c')
(a, b, c)
sage: eqn = [a+b*c==1, b-a*c==0, a+b==5]
sage: s = solve(eqn, a,b,c); s
[[a == -1/4*I*sqrt(79) + 11/4, b == 1/4*I*sqrt(79) + 9/4, c == 1/10*I*sqrt(79) + 1/10], [a == 1/4*I*sqrt(79) + 11/4, b == -1/4*I*sqrt(79) + 9/4, c == -1/10*I*sqrt(79) + 1/10]]

你甚至可以用LaTeX巧妙地排版解决方案:

sage.: print(latex(s))
...

要使以上内容通过xdvi显示在屏幕上,请键入 view(s)

也可以使用符号解线性方程 solve 命令::

sage: var('x,y,z,a')
(x, y, z, a)
sage: eqns = [x + z == y, 2*a*x - y == 2*a^2, y - 2*z == 2]
sage: solve(eqns, x, y, z)
[[x == a + 1, y == 2*a, z == a - 1]]

以下是一个数字Numpy示例:

sage: from numpy import arange, eye, linalg
sage: A = eye(10)       ##   the 10x10 identity matrix
sage: b = arange(1,11)
sage: x = linalg.solve(A,b)

数值求解系统的另一种方法是使用Sage的八度界面:

sage: M33 = MatrixSpace(QQ,3,3)
sage: A   = M33([1,2,3,4,5,6,7,8,0])
sage: V3  = VectorSpace(QQ,3)
sage: b   = V3([1,2,3])
sage: octave.solve_linear_system(A,b)    # optional - octave
[-0.333333, 0.666667, 0]