-
Notifications
You must be signed in to change notification settings - Fork 0
/
5-1.py
58 lines (52 loc) · 1.29 KB
/
5-1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# @Time : 2022/05/18 15:54:34
# @Author : dch0319
# @File : 5-1.py
# @Software: Visual Studio Code
import numpy as np
def TiDuXiaJiang(A, b):
A = np.array(A, dtype=np.float64)
dim = A.shape[0]
b = np.array(b, dtype=np.float64)
x = np.zeros((dim, 1))
v = b - A @ x
x_list = [x]
while True:
w = A @ v
t = (v.T @ v) / (v.T @ w)
x = x + t * v
v = v - t * w
x_list.append(x)
if abs(x_list[-1] - x_list[-2]).max() < 0.5 * 10**(-8):
break
print("梯度下降: ")
print(x_list[-1])
return x_list[-1]
def GongETiDu(A, b):
A = np.array(A, dtype=np.float64)
dim = A.shape[0]
b = np.array(b, dtype=np.float64)
x = np.zeros((dim, 1))
v = r = b - A @ x
rr0 = r.T @ r
x_list = [x]
while True:
w = A @ v
t = rr0 / (v.T @ w)
x = x + t * v
r = r - t * w
rr1 = r.T @ r
s = rr1 / rr0
rr0 = rr1
v = r + s * v
x_list.append(x)
if abs(x_list[-1] - x_list[-2]).max() < 0.5 * 10**(-8):
break
print("共轭梯度: ")
print(x_list[-1])
return x_list[-1]
A = [[4, -2, 0], [-2, 2, -1], [0, -1, 5]]
b = [[0], [3], [-7]]
TiDuXiaJiang(A, b)
GongETiDu(A, b)