-
Notifications
You must be signed in to change notification settings - Fork 88
/
least_square.py
109 lines (91 loc) · 2.82 KB
/
least_square.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
''' mbinary
#########################################################################
# File : least_square.py
# Author: mbinary
# Mail: zhuheqin1@gmail.com
# Blog: https://mbinary.xyz
# Github: https://github.com/mbinary
# Created Time: 2018-10-02 21:14
# Description:
#########################################################################
'''
'''*************************************************************************
> File Name: least-square.py
> Author: mbinary
> Mail: zhuheqin1@gmail.com
> Created Time: Sat 07 Apr 2018 09:55:25 PM DST
> Blog: https://mbinary.xyz
> Description: 最小二乘法解线性方程组, 解矛盾方程组
************************************************************************'''
import re
import numpy as np
def solveConflitEqualtion(A, y):
'''solve equation like this: Av = y,
A:m*n v:n*1 y:m*1
return vector v
'''
A = np.matrix(A)
y = np.array(y)
ata = A.T*A
print('AtA')
print(ata)
return np.linalg.solve(ata, A.T*y) # note that is numpy.linalg.solve
def solveLinear(point, index):
y = [[i[1]] for i in point]
x = [[i[0]] for i in point]
A = []
for i in x:
A.append([i[0]**j for j in index])
res = solveConflitEqualtion(A, y)
print('the solution is : \n', res)
print('namely: ')
items = ['{:.4f}x^{}'.format(res[i, 0], j) for i, j in enumerate(index)]
print('phi(x) = ', ' + '.join(items))
def handleInput(s=None, y=None):
# numPt = re.compile (r'\d*\.{0,1}\d+')
if not s:
s = input('input matrix A:m*n //m>=n\n')
s = s.replace(' ', '')
li = re.findall(r'(\[(\d+)(,(\d+))+\])', s)
li = [parseLst(i[0]) for i in li]
if not y:
y = input('input a vector y:n*1\n')
y = parseLst(y)
print('Equation: Av = y:')
print('y is as follows: ')
print(y)
print('A is as follows: ')
for i in li:
for j in i:
print('{}'.format(j).rjust(5), end='')
print('')
print('result v is as follows: ')
res = solveConflitEqualtion(li, y)
print(res)
def parseLst(s):
s = s.strip('[]')
li = s.split(',')
li = [float(j) for j in li]
return li
if __name__ == '__main__':
'''
li = '[[23,2],[2,5],[2,6]]'
y = '[1,3]'
while True:
handleInput(li,y)
s = input('input y to continue, n for exit')
if s!='y':break
'''
point = [(-3, 14.3), (-2, 8.3), (-1, 4.7), (2, -8.3), (4, -22.7)]
lst = [0, 3]
solveLinear(point, lst)
point = [(-3, 14.3), (-2, 8.3), (-1, 4.7), (2, 8.3), (4, 22.7)]
lst = [0, 2]
solveLinear(point, lst)
A = [[1, 2], [2, 1], [1, 1]]
y = [[5], [6], [4]]
res = solveConflitEqualtion(A, y)
print(res)
A = [[1, -2], [1, 5], [2, 1], [1, 1]]
y = [[1], [13.1], [7.9], [5.1]]
print(solveConflitEqualtion(A, y))