线性代数

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

构造矩阵和矩阵的乘法都是很容易的,也是很自然的:

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

注意,在Sage中,矩阵$A$的核(kernel)是”左核”(left kernel),即在向量空间中,$w$满足$wA=0$.

解矩阵方程也很容易,使用方法solve_right. 执行A.solve_right(Y)返回一个矩阵(或向量)$X$满足$AX=Y$:

  1. sage: Y = vector([0, -4, -1])
  2. sage: X = A.solve_right(Y)
  3. sage: X
  4. (-2, 1, 0)
  5. sage: A * X # 检查回答
  6. (0, -4, -1)

反斜杠\可以代替solve_right; 用A \ Y代替A.solve_right(Y).

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

如果无解,Sage返回一个错误:

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

类似的,使用A.solve_left(Y)求解满足$XA=Y$的$X$.

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

  1. sage: A = matrix([[0, 4],[-1, 0]])
  2. sage: A.eigenvalues ()
  3. [-2*I, 2*I]
  4. sage: B = matrix([[1, 3],[3, 1]])
  5. sage: B.eigenvectors_left()
  6. [(4,[
  7. (1, 1)
  8. ], 1), (-2,[
  9. (1, -1)
  10. ], 1)]

(eigenvectors_left的输出是三元组的列表:(特征值,特征向量, 重数)。)在QQRR上的特征值和特征向量也可以用 Maxima计算(参见Maxima)。

基本的环中提到,矩阵所在的环影响它的性质。下面matrix命令中的第一个参数告诉Sage这个矩阵是整数环(ZZ)上的,有理数环(QQ)上的,还是实数环(RR)上的:

  1. sage: AZ = matrix(ZZ,[[2,0],[0,1]])
  2. sage: AQ = matrix(QQ,[[2,0],[0,1]])
  3. sage: AR = matrix(RR,[[2,0],[0,1]])
  4. sage: AZ.echelon_form()
  5. [2 0]
  6. [0 1]
  7. sage: AQ.echelon_form()
  8. [1 0]
  9. [0 1]
  10. sage: AR.echelon_form()
  11. [ 1.00000000000000 0.000000000000000]
  12. [0.000000000000000 1.00000000000000]

为了计算实数阵与复数阵的特征值与特征向量,它应该被定义在RDF(实双场)或CDF(复双场)上。如果没有指定一个环,这个矩阵将被默认定义在RRCC上,但这将使得它不支持下面的运算。

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

矩阵空间

我们建立由$3\times 3$的有理数矩阵构成的空间 $\text{Mat}_{3\times 3}(Q)$:

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

(如果要指定$3\times 4$矩阵组成的空间,使用MatrixSpace(QQ,3,4). 如果省略列数,则默认的等于行数,MatrixSpace(QQ,3)等价于MatrixSpace(QQ,3,3).) Sage将矩阵空间的基保存为一个列表。

  1. sage: B = M.basis()
  2. sage: len(B)
  3. 9
  4. sage: B[1]
  5. [0 1 0]
  6. [0 0 0]
  7. [0 0 0]

新建一个矩阵作为M的元素。

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

然后我们计算它约简后的行阶梯矩阵形式以及核。

  1. sage: A.echelon_form()
  2. [ 1 0 -1]
  3. [ 0 1 2]
  4. [ 0 0 0]
  5. sage: A.kernel()
  6. Vector space of degree 3 and dimension 1 over Rational Field
  7. Basis matrix:
  8. [ 1 -2 1]

下面我们展示定义在有限域上的矩阵的运算:

  1. sage: M = MatrixSpace(GF(2),4,8)
  2. sage: A = M([1,1,0,0, 1,1,1,1, 0,1,0,0, 1,0,1,1,
  3. ....: 0,0,1,0, 1,1,0,1, 0,0,1,1, 1,1,1,0])
  4. sage: A
  5. [1 1 0 0 1 1 1 1]
  6. [0 1 0 0 1 0 1 1]
  7. [0 0 1 0 1 1 0 1]
  8. [0 0 1 1 1 1 1 0]
  9. sage: rows = A.rows()
  10. sage: A.columns()
  11. [(1, 0, 0, 0), (1, 1, 0, 0), (0, 0, 1, 1), (0, 0, 0, 1),
  12. (1, 1, 1, 1), (1, 0, 1, 1), (1, 1, 0, 1), (1, 1, 1, 0)]
  13. sage: rows
  14. [(1, 1, 0, 0, 1, 1, 1, 1), (0, 1, 0, 0, 1, 0, 1, 1),
  15. (0, 0, 1, 0, 1, 1, 0, 1), (0, 0, 1, 1, 1, 1, 1, 0)]

我们构造一个由上面的行张开的$F_{2}$的子空间。

  1. sage: V = VectorSpace(GF(2),8)
  2. sage: S = V.subspace(rows)
  3. sage: S
  4. Vector space of degree 8 and dimension 4 over Finite Field of size 2
  5. Basis matrix:
  6. [1 0 0 0 0 1 0 0]
  7. [0 1 0 0 1 0 1 1]
  8. [0 0 1 0 1 1 0 1]
  9. [0 0 0 1 0 0 1 1]
  10. sage: A.echelon_form()
  11. [1 0 0 0 0 1 0 0]
  12. [0 1 0 0 1 0 1 1]
  13. [0 0 1 0 1 1 0 1]
  14. [0 0 0 1 0 0 1 1]

$S$的基是由$S$的行梯形矩阵形式中的非零元的行得到的。

稀疏线性代数

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

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

Sage中的多模算法对于方阵效果比较好(但是对于非方阵效果不怎么好):

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

注意,Python是区分大小写的:

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