Chapter 2 Solving Linear Equations (Introduction to Linear Algebar written by Dr. Gilber Strang)

2.1 Vectors and Linear Equations

The central problem of linear algebra is to solve a system of linear equations which means that the unknown are only multiplied by numbers–we never see x x x times y y y.

  1. The column picture of A x = b : Ax = b: Ax=b: a combination of n n n columns of A produces the vector b b b.
  2. The row picture of A x = b : Ax=b: Ax=b: m m m equations from m m m rows give m m m planes meeting at x x x.

[ 1 − 2 3 2 ] [ x y ] = [ 1 11 ] \left[ \begin{matrix} 1 & -2\\ 3 & 2 \end{matrix} \right]\left[\begin{matrix}x\\y\end{matrix}\right]=\left[\begin{matrix}1\\11\end{matrix}\right] [1322][xy]=[111]
The column picture:
x [ 1 3 ] + y [ − 2 2 ] = [ 1 11 ] x\left[\begin{matrix}1\\3\end{matrix}\right] + y\left[\begin{matrix}-2\\2\end{matrix}\right]=\left[\begin{matrix}1\\11\end{matrix}\right] x[13]+y[22]=[111]
The row picture:
x − 2 y = 1 3 x + 2 y = 11 x-2y = 1\\ 3x+2y=11 x2y=13x+2y=11

The column picture is done in numpy, when you create new matrix.
np.linalg.inv(A) calculate the inverse of matrix A A A.

>>> import numpy as np
>>> A = np.array([[1,3],[-2,2]])# The column picture
>>> b = np.array([1,11])
>>> b.dot(np.linalg.inv(A))
array([3., 1.])
>>> np.dot(b,np.linalg.inv(A))
array([3., 1.])

Three Equations in Three Unkonwns

A x = b Ax = b Ax=b
x + 2 y + 3 z = 6 2 x + 5 y + 2 z = 4 6 x − 3 y + z = 2 x+2y+3z=6\\ 2x+5y+2z=4\\ 6x-3y+z=2 x+2y+3z=62x+5y+2z=46x3y+z=2

The Row Picture: Two intersected planes make one line which is intersected with the third plane. This process can do the result.
The Column Picture:
x [ 1 2 6 ] + y [ 2 5 − 3 ] + z [ 3 2 1 ] = [ 6 4 2 ] x\left[\begin{matrix} 1\\2\\6 \end{matrix}\right] + y\left[\begin{matrix} 2\\5\\-3\end{matrix}\right] + z\left[\begin{matrix} 3\\2\\1 \end{matrix}\right]=\left[\begin{matrix} 6\\4\\2 \end{matrix}\right] x126+y253+z321=642

The result should be:
[ x y z ] = [ 0 0 2 ] \left[\begin{matrix} x\\y\\z \end{matrix}\right] = \left[\begin{matrix} 0\\0\\2 \end{matrix}\right] xyz=002

>>> import numpy as np
>>> A = np.array([[1,2,6],[2,5,-3],[3,2,1]])
>>> b = np.array([6,4,2])
>>> b.dot(np.linalg.inv(A))
array([0.00000000e+00, 2.77555756e-17, 2.00000000e+00])

The matrix form merge the row picture and column picture.
[ 1 2 3 2 5 2 6 − 3 1 ] [ x y z ] = [ 6 4 2 ] \left[\begin{matrix}1&2&3\\ 2&5&2\\ 6&-3&1 \end{matrix}\right]\left[\begin{matrix} x\\y\\z\end{matrix}\right]=\left[\begin{matrix} 6\\4\\2 \end{matrix}\right] 126253321xyz=642

identity matrix I \bm{I} I: I x = x \bm{I}\bm{x} = \bm{x} Ix=x
Whatever vector identity matrix multiplies, that vector is not changed.
I = [ 1 0 0 0 1 0 0 0 1 ] \bm{I} = \left[\begin{matrix}1&0&0\\0&1&0\\0&0&1\end{matrix}\right] I=100010001

Except for the Identity Matrix, the following matrix are all interesting.

  1. Exchange matrix
    [ 0 1 1 0 ] [ x y ] = x [ 0 1 ] + y [ 1 0 ] = [ x ∗ 0 + y ∗ 1 x ∗ 1 + y ∗ 0 ] = [ y x ] \left[\begin{matrix} 0&1\\1&0\end{matrix}\right]\left[\begin{matrix} x\\y\end{matrix}\right]=x\left[\begin{matrix} 0\\1\end{matrix}\right] + y\left[\begin{matrix}1\\0 \end{matrix}\right]=\left[\begin{matrix}x*0 + y*1\\x*1+y*0 \end{matrix}\right]=\left[\begin{matrix}y\\x \end{matrix}\right] [0110][xy]=x[01]+y[10]=[x0+y1x1+y0]=[yx]
  2. Rotate every vector by 90°
    [ 0 1 − 1 0 ] [ x y ] = x [ 0 − 1 ] + y [ 1 0 ] = [ x ∗ 0 + y ∗ 1 x ∗ ( − 1 ) + y ∗ 0 ] = [ y − x ] \left[\begin{matrix} 0&1\\-1&0 \end{matrix}\right]\left[\begin{matrix} x\\y \end{matrix}\right]=x\left[\begin{matrix}0\\-1\end{matrix}\right] + y\left[\begin{matrix} 1\\0 \end{matrix}\right]=\left[\begin{matrix} x*0+y*1\\x*(-1) + y*0 \end{matrix}\right]=\left[\begin{matrix}y\\-x \end{matrix}\right] [0110][xy]=x[01]+y[10]=[x0+y1x(1)+y0]=[yx]
  3. Rotate every vector by 180°
    [ 0 − 1 − 1 0 ] [ x y ] = x [ 0 − 1 ] + y [ − 1 0 ] = [ x ∗ 0 + y ∗ ( − 1 ) x ∗ ( − 1 ) + y ∗ 0 ] = [ − y − x ] \left[\begin{matrix} 0&-1\\-1&0 \end{matrix}\right]\left[\begin{matrix} x\\y\end{matrix}\right]=x\left[\begin{matrix} 0\\-1 \end{matrix}\right] + y\left[\begin{matrix} -1\\0 \end{matrix}\right]=\left[\begin{matrix} x*0 + y * (-1)\\x*(-1) + y*0\end{matrix}\right]=\left[\begin{matrix} -y\\-x \end{matrix}\right] [0110][xy]=x[01]+y[10]=[x0+y(1)x(1)+y0]=[yx]
  4. Rotate every vector through 45°
    [ ( 2 ) 2 − ( 2 ) 2 ( 2 ) 2 ( 2 ) 2 ] [ x y ] = x [ ( 2 ) 2 ( 2 ) 2 ] + y [ − ( 2 ) 2 ( 2 ) 2 ] = [ x ( 2 ) 2 − y ( 2 ) 2 x ( 2 ) 2 + y ( 2 ) 2 ] \left[\begin{matrix} \frac{\sqrt(2)}{2} & -\frac{\sqrt(2)}{2} \\\frac{\sqrt(2)}{2}&\frac{\sqrt(2)}{2} \end{matrix}\right]\left[\begin{matrix} x\\y \end{matrix}\right]=x\left[\begin{matrix} \frac{\sqrt(2)}{2}\\\frac{\sqrt(2)}{2} \end{matrix}\right] + y\left[\begin{matrix} -\frac{\sqrt(2)}{2}\\\frac{\sqrt(2)}{2} \end{matrix}\right]=\left[\begin{matrix} x\frac{\sqrt(2)}{2} -y\frac{\sqrt(2)}{2} \\ x\frac{\sqrt(2)}{2}+y\frac{\sqrt(2)}{2} \end{matrix}\right] 2( 2)2( 2)2( 2)2( 2)[xy]=x2( 2)2( 2)+y2( 2)2( 2)=x2( 2)y2( 2)x2( 2)+y2( 2)
    All these problems are solved by the column picture.

Row Picture: Each equation in A x = b Ax=b Ax=b gives a line(n = 2) or a plane(n = 3) or a “hyperplane”(n > 3). They intersect at the solution or solutions, if any.

2.2 The Idea of Elimination

The corner entry a 11 a_{11} a11 is the first “pivot” and the ratio a 21 / a 11 a_{21}/a_{11} a21/a11 is the first “multiplier.”

Elimination breaks down if zero appears in the pivot. Exchanging two equations may save it.

2.4 Rules for Matrix Operations

A B = [ a b c d ] [ E F G H ] = [ a c ] [ E F ] + [ b d ] [ G H ] = [ a E a F c E c F ] + [ b G b H d G d H ] = [ a E + b G a F + b H c E + d G c F + d H ] AB = \left[\begin{matrix} a&b\\c&d \end{matrix}\right]\left[\begin{matrix} E&F\\G&H \end{matrix}\right]=\left[\begin{matrix} a\\c \end{matrix}\right]\left[\begin{matrix} E&F \end{matrix}\right]+\left[\begin{matrix} b\\d \end{matrix}\right]\left[\begin{matrix} G&H \end{matrix}\right]=\left[\begin{matrix} aE&aF\\cE&cF \end{matrix}\right] + \left[\begin{matrix} bG&bH\\dG&dH \end{matrix}\right] = \left[\begin{matrix} aE+bG&aF+bH\\cE+dG&cF+dH \end{matrix}\right] AB=[acbd][EGFH]=[ac][EF]+[bd][GH]=[aEcEaFcF]+[bGdGbHdH]=[aE+bGcE+dGaF+bHcF+dH]

2.5 Inverse Matrices

Calculating A − 1 A^{-1} A1 by Gauss-Jordan Eliminations

A = [ 2 − 1 0 − 1 2 − 1 0 − 1 2 ] A = \left[\begin{matrix} 2&-1&0\\-1&2&-1\\0&-1&2 \end{matrix}\right] A=210121012

A I = [ 2 − 1 0 1 0 0 − 1 2 − 1 0 1 0 0 − 1 2 0 0 1 ] → e l i m i n a t i o n AI = \left[\begin{matrix} 2&-1&0&1&0&0\\-1&2&-1&0&1&0\\0&-1&2&0&0&1 \end{matrix}\right]\rightarrow elimination AI=210121012100010001elimination
→ [ 1 0 0 3 4 1 2 1 4 0 1 1 1 2 1 1 1 0 0 1 1 4 1 2 3 4 ] \rightarrow \left[\begin{matrix} 1&0&0& \frac{3}{4}&\frac{1}{2}&\frac{1}{4} \\0&1&1& \frac{1}{2}&1&\frac{1}{1} \\0&0&1& \frac{1}{4}&\frac{1}{2}&\frac{3}{4} \end{matrix}\right] 10001001143214121121411143

>>> import numpy as np
>>> import sympy as sm
>>> I = np.identity(3, dtype = int)
>>> A = [[2,-1,0], [-1,2,-1],[0,-1,2]]
>>> aug_matrix = sm.Matrix(np.concatenate((A, I), axis=1))
>>> aug_matrix
Matrix([
[ 2, -1,  0, 1, 0, 0],
[-1,  2, -1, 0, 1, 0],
[ 0, -1,  2, 0, 0, 1]])
>>> # Elimination on the augmented matrix [A, I] to the
>>> # reduced echelon form阶梯形矩阵
>>> R, rref_pivots = aug_matrix.rref()
>>> # Pick X = inverse(A) fromthe last n columns of R
>>> R[:,3:]
Matrix([
[3/4, 1/2, 1/4],
[1/2,   1, 1/2],
[1/4, 1/2, 3/4]])

Diagonally dominant matrices are invertible. The diagonally dominant matrices are needed to be following the formula.
∣ a i j ∣ > ∑ j ≠ i ∣ a i j ∣ |a_{ij}| > \sum_{j\neq i}|a_{ij}| aij>j=iaij
like the following matrix:
[ 3 1 1 1 3 1 1 1 3 ] \left[\begin{matrix} 3&1&1\\1&3&1\\1&1&3 \end{matrix}\right] 311131113
Some matrices which are not diagonally dominant matrices are also invertable . like the following matrix:
[ 2 1 1 1 2 1 1 1 3 ] \left[\begin{matrix} 2&1&1\\1&2&1\\1&1&3 \end{matrix}\right] 211121113

The inverse of a triangular difference matrix A A A is a triangular sum matrix S S S.
A I = [ 1 0 0 1 0 0 − 1 1 0 0 1 0 0 − 1 1 0 0 1 ] → [ 1 0 0 1 0 0 0 1 0 1 1 0 0 0 1 1 1 1 ] = [ I A − 1 ] AI=\left[\begin{matrix} 1&0&0&1&0&0\\-1&1&0&0&1&0\\0&-1&1&0&0&1 \end{matrix}\right] \rightarrow\left[\begin{matrix} 1&0&0&1&0&0\\0&1&0&1&1&0\\0&0&1&1&1&1 \end{matrix}\right]=[I A^{-1}] AI=110011001100010001100010001111011001=[IA1]
A A A is difference matrix. A − 1 A^{-1} A1 is sum matrix.

2.6 Elimination = = = Factorization

>>> from scipy.linalg import pascal, cholesky
>>> # create 4*4 pascal matrix
>>> A = pascal(4)
>>> # Cholesky factorization
>>> L = cholesky(A, lower=True)
>>> U = cholesky(A, lower = False)
>>> # check the the factorization of A, A = LU
>>> L.dot(U) == A
array([[ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True]])
>>> A
array([[ 1,  1,  1,  1],
       [ 1,  2,  3,  4],
       [ 1,  3,  6, 10],
       [ 1,  4, 10, 20]], dtype=uint64)
>>> L
array([[1., 0., 0., 0.],
       [1., 1., 0., 0.],
       [1., 2., 1., 0.],
       [1., 3., 3., 1.]])
>>> U
array([[1., 1., 1., 1.],
       [0., 1., 2., 3.],
       [0., 0., 1., 3.],
       [0., 0., 0., 1.]])

2.7 Transpose and Permutations

( A B ) T = B T A T (AB)^T = B^TA^T (AB)T=BTAT

( A − 1 ) T = ( A T ) − 1 (A^{-1})^T = (A^T)^{-1} (A1)T=(AT)1

A symmetric matrix has S T = S S^T = S ST=S. This means that S j i = S i j S_{ji} =S_{ij} Sji=Sij

For permutation matrix P P P, P − 1 = P T P^{-1} = P^T P1=PT

How to find the permutation matrix P P P in the formula P A = L U PA= LU PA=LU?
Look down the column for the largest pivot.

>>> from scipy.linalg import lu
>>> import numpy as np
>>> A = np.array([[0,1,1], [1,2,1], [2,7,9]])
>>> P, L, U = lu(A)
>>> A
array([[0, 1, 1],
       [1, 2, 1],
       [2, 7, 9]])
>>> P.dot(L.dot(U))
array([[0., 1., 1.],
       [1., 2., 1.],
       [2., 7., 9.]])

至此本书内容最多的一章啃完了!!!

### 回答1: 我可以给你一个解决线性方程的代码例子:#include <iostream>using namespace std;int main(){ int a, b, c; // a, b, c are coefficients of the equation int x, y; // x and y are the variables of the equation cout << "Enter the coefficients a, b, c of the equation: "; cin >> a >> b >> c; x = (c - b) / a; y = (a * x) + b; cout << "The solution of the equation is x = " << x << " and y = " << y << endl; return 0; } ### 回答2: 当然可以!下面是一个用Python编写的解线性方程组的代码示例: ```python import numpy as np def solve_linear_equations(coefficients, constants): try: solution = np.linalg.solve(coefficients, constants) return solution except np.linalg.LinAlgError: return "无解或有无限多解" # 示例方程组为: # 2x + 3y = 8 # 4x - 5y = -7 # 系数矩阵 coefficients = np.array([[2, 3], [4, -5]]) # 常数矩阵 constants = np.array([8, -7]) # 求解方程组 result = solve_linear_equations(coefficients, constants) # 输出解 print("解为:") print("x =", result[0]) print("y =", result[1]) ``` 以上代码使用了NumPy库中的函数`np.linalg.solve()`来求解线性方程组。在示例方程组中,使用了一个2x2的系数矩阵和一个常数矩阵。函数`solve_linear_equations()`返回方程组的解(如果有解),如果方程组无解或有无限多解,则返回相应的提示。 运行代码后,输出的解为: ``` 解为: x = 3.0714285714285716 y = 0.8571428571428568 ``` 这说明方程组的解为x ≈ 3.071和y ≈ 0.857。 ### 回答3: 当然可以,以下是一个用Python编写的解线性方程组的代码示例: ``` import numpy as np # 创建系数矩阵A和常数向量b A = np.array([[2, 3], [4, -1]]) b = np.array([9, 5]) # 求解线性方程组 Ax = b x = np.linalg.solve(A, b) # 输出解 print("解为:x =", x) ``` 这个代码示例使用了NumPy库中的`linalg.solve()`函数来求解线性方程组。首先,我们创建了一个2x2的系数矩阵A和一个长度为2的常数向量b。然后,使用`solve()`函数解方程组Ax = b,并将解存储在变量x中。最后,输出解x的值。 此代码示例可以用于解任意大小的线性方程组。只需将系数矩阵A和常数向量b替换为相应的值即可。这样,您可以使用该示例来解决其他线性方程组。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值