1 将矩阵化为最简形

以下是将矩阵 ( 1 7 2 5 2 3 0 − 1 1 − 1 2 1 0 6 4 0 4 1 2 − 1 ) \begin{pmatrix} 1 & 7 & 2 & 5 & 2 \\ 3 & 0 & -1 & 1 & -1 \\ 2 & 1 & 0 & 6 & 4 \\ 0 & 4 & 1 & 2 & -1 \end{pmatrix} 13207014210151622141 化为最简形的 Python 代码,并以矩阵形式展示结果:

from sympy import Matrix

# Define the matrix A
A = Matrix([
    [1, 7, 2, 5, 2],
    [3, 0, -1, 1, -1],
    [2, 1, 0, 6, 4],
    [0, 4, 1, 2, -1]
])

# Compute the reduced row echelon form (rref)
rref_matrix, pivot_columns = A.rref()

# Display the result
print("The matrix in reduced row echelon form is:")
print(rref_matrix)

运行此代码将输出:

The matrix in reduced row echelon form is:
Matrix([
[1, 0, 0, 0, 3/2],
[0, 1, 0, 0,  -2],
[0, 0, 1, 0,   6],
[0, 0, 0, 1, 1/2]])

化为最简形后的矩阵表示为:

( 1 0 0 0 3 2 0 1 0 0 − 2 0 0 1 0 6 0 0 0 1 1 2 ) \begin{pmatrix} 1 & 0 & 0 & 0 & \frac{3}{2} \\ 0 & 1 & 0 & 0 & -2 \\ 0 & 0 & 1 & 0 & 6 \\ 0 & 0 & 0 & 1 & \frac{1}{2} \end{pmatrix} 1000010000100001232621

2 求矩阵列向量的最大线性最大无关组

对于矩阵 ( 1 7 2 5 2 3 0 − 1 1 − 1 2 1 0 6 4 0 4 1 2 − 1 ) \begin{pmatrix} 1 & 7 & 2 & 5 & 2 \\ 3 & 0 & -1 & 1 & -1 \\ 2 & 1 & 0 & 6 & 4 \\ 0 & 4 & 1 & 2 & -1 \end{pmatrix} 13207014210151622141

最大线性无关组的列索引是 ((0, 1, 2, 3)),即列向量:

( 1 3 2 0 ) , ( 7 0 1 4 ) , ( 2 − 1 0 1 ) , ( 5 1 6 2 ) \begin{pmatrix} 1 \\ 3 \\ 2 \\ 0 \end{pmatrix}, \begin{pmatrix} 7 \\ 0 \\ 1 \\ 4 \end{pmatrix}, \begin{pmatrix} 2 \\ -1 \\ 0 \\ 1 \end{pmatrix}, \begin{pmatrix} 5 \\ 1 \\ 6 \\ 2 \end{pmatrix} 1320 , 7014 , 2101 , 5162

将其余的列向量表示为最大无关组的线性组合:

第5列向量 ( 2 − 1 4 − 1 ) \begin{pmatrix} 2 \\ -1 \\ 4 \\ -1 \end{pmatrix} 2141 可以表示为:

( 2 − 1 4 − 1 ) = 3 2 ( 1 3 2 0 ) − 2 ( 7 0 1 4 ) + 6 ( 2 − 1 0 1 ) + 1 2 ( 5 1 6 2 ) \begin{pmatrix} 2 \\ -1 \\ 4 \\ -1 \end{pmatrix} = \frac{3}{2} \begin{pmatrix} 1 \\ 3 \\ 2 \\ 0 \end{pmatrix} - 2 \begin{pmatrix} 7 \\ 0 \\ 1 \\ 4 \end{pmatrix} + 6 \begin{pmatrix} 2 \\ -1 \\ 0 \\ 1 \end{pmatrix} + \frac{1}{2} \begin{pmatrix} 5 \\ 1 \\ 6 \\ 2 \end{pmatrix} 2141 =23 1320 2 7014 +6 2101 +21 5162

即:

( 2 − 1 4 − 1 ) = 3 2 v 1 − 2 v 2 + 6 v 3 + 1 2 v 4 \begin{pmatrix} 2 \\ -1 \\ 4 \\ -1 \end{pmatrix} = \frac{3}{2} \mathbf{v}_1 - 2 \mathbf{v}_2 + 6 \mathbf{v}_3 + \frac{1}{2} \mathbf{v}_4 2141 =23v12v2+6v3+21v4

其中 v 1 , v 2 , v 3 , v 4 \mathbf{v}_1, \mathbf{v}_2, \mathbf{v}_3, \mathbf{v}_4 v1,v2,v3,v4 分别是最大无关组的列向量。

# Define the matrix A
A = Matrix([
    [1, 7, 2, 5, 2],
    [3, 0, -1, 1, -1],
    [2, 1, 0, 6, 4],
    [0, 4, 1, 2, -1]
])

# Compute the reduced row echelon form
rref_matrix, pivot_columns = A.rref()

# Get the maximum linearly independent set of columns
max_independent_set = A[:, pivot_columns]

# Express the remaining columns as linear combinations of the independent set
expressions = {}
for i in range(A.shape[1]):
    if i not in pivot_columns:
        coeffs = rref_matrix[:, i]
        expression = {}
        for j, coeff in enumerate(coeffs):
            if coeff != 0:
                expression[pivot_columns[j]] = coeff
        expressions[i] = expression

pivot_columns, max_independent_set, expressions

3 给你一个增广矩阵,你帮我求出他的阶梯型矩阵(1,-1,3,-1,1;2,-1,-1,4,2;3,-2,2,3,3)最后一个列是增广列

以下是给定增广矩阵 ( 1 − 1 3 − 1 1 2 − 1 − 1 4 2 3 − 2 2 3 3 ) \begin{pmatrix} 1 & -1 & 3 & -1 & 1 \\ 2 & -1 & -1 & 4 & 2 \\ 3 & -2 & 2 & 3 & 3 \end{pmatrix} 123112312143123 的阶梯型矩阵:

( 1 0 − 4 5 1 0 1 − 7 6 0 0 0 0 0 0 ) \begin{pmatrix} 1 & 0 & -4 & 5 & 1 \\ 0 & 1 & -7 & 6 & 0 \\ 0 & 0 & 0 & 0 & 0 \end{pmatrix} 100010470560100

这个矩阵已经转换为阶梯型形式。

from sympy import Matrix

# Define the augmented matrix
augmented_matrix = Matrix([
    [1, -1, 3, -1, 1],
    [2, -1, -1, 4, 2],
    [3, -2, 2, 3, 3]
])

# Compute the row echelon form
echelon_matrix = augmented_matrix.rref()[0]

echelon_matrix

4 求特征值和特征向量

将该矩阵 ( − 1 1 1 0 1 4 0 4 1 ) \begin{pmatrix} -1 & 1 & 1 \\ 0 & 1 & 4 \\ 0 & 4 & 1 \end{pmatrix} 100114141 的特征值和对应的特征向量写成分数或无理数如下:

特征值:
− 1 , 5 , − 3 -1, 5, -3 1,5,3

对应的特征向量(按列排列):

  • 对应特征值 − 1 -1 1 的特征向量是:
    ( 1 0 0 ) \begin{pmatrix} 1 \\ 0 \\ 0 \end{pmatrix} 100

  • 对应特征值 5 5 5的特征向量是:
    ( 1 3 1 1 ) \begin{pmatrix} \frac{1}{3} \\ 1 \\ 1 \end{pmatrix} 3111

  • 对应特征值 − 3 -3 3的特征向量是:
    ( 0 − 1 1 ) \begin{pmatrix} 0 \\ -1 \\ 1 \end{pmatrix} 011

#写成分数或者无理数的形式
import numpy as np
from sympy import Matrix, Rational
##求矩阵特征值和特征向量
# Define the matrix
matrix = np.array([[2, 0, 0], [0, 3, 2], [0, 2, 3]])

# Compute the eigenvalues and eigenvectors using numpy
eigenvalues, eigenvectors = np.linalg.eig(matrix)

# Convert numpy arrays to sympy for exact computation
matrix_sympy = Matrix(matrix)
eigenvals = matrix_sympy.eigenvals()
eigenvecs = matrix_sympy.eigenvects()

# Print eigenvalues as fractions or irrational numbers
print("Eigenvalues:")
for val in eigenvals:
    print(Rational(val).limit_denominator())

# Print eigenvectors as fractions or irrational numbers
print("\nEigenvectors:")
for val, multiplicity, vecs in eigenvecs:
    for vec in vecs:
        print(f"Eigenvalue {Rational(val).limit_denominator()}:")
        print(vec)

不写成分数或者无理数形式

import numpy as np

# Define the matrix
matrix = np.array([[-1, 1, 1], [0, 1, 4], [0, 4, 1]])

# Compute the eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(matrix)

eigenvalues, eigenvectors

07-11 00:38