-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpolation.py
107 lines (87 loc) · 2.38 KB
/
interpolation.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
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 3 00:40:48 2021
@author: angel
"""
from numpy import ones, size, linalg, copy, shape, nan, transpose, dot
from linear_algebra import gauss_elimination, rank
def vandermonde_matrix(x):
"""
Computes a Vandermonde matrix given an array x of values.
Parameters
----------
x : array
Array of values.
Returns
-------
V : bidimensional array
Vandermonde matrix.
"""
m = size(x)
n = m+1
V = ones((m, n))
for j in range(1, n):
for i in range(0, m):
V[i,j] = pow(x[i],j)
return V
def vandermonde_cond(x):
V = vandermonde_matrix(x)
print(linalg.cond(V))
def polynomial(x, y):
"""
Computes coefficients of the interpolation polynomial.
Parameters
----------
x : array
Array of variables, n-by-1.
y : array
Array of knwon terms, n-by-1.
Returns
-------
a : array
Array of coefficients.
"""
var = copy(x)
known = copy(y)
V = vandermonde_matrix(var)
a = gauss_elimination(V, known)
return a
def linear_least_squares(M, v):
"""
Solves the linear least squares problem.
If rank(A) is its maximum then the linear least squares problem has one unique solution, obtained
as solution of a n-equations, n-variables system AtAx = Atb, known as normal system
Parameters
----------
A : bidimensional array
Matrix of coefficients.
b : array
Columns vector m-by-1 of known terms.
Returns
-------
x : array
Column vector n-by-1 of solutions of the linear system.
"""
B = copy(M)
[m,n] = shape(B)
if rank(B) != min(m,n):
print('Warning: can not be solved since the rank of the matrix is not its maximum value')
return nan
else:
A = copy(M)
At = transpose(M)
b = copy(v)
b = transpose(b)
AtA = dot(At, A)
Atb = transpose(dot(At, b))
print(AtA, Atb)
x = gauss_elimination(AtA, Atb)
print('x*:')
return x
#SQUARE_LINEAR_SYSTEM
#A = array([[1, 1, 0], [2, 1, 1], [3, 0, 1]], dtype=float)
#b = array([[1,1,1]], dtype=float)
#x* = [0.5, 0.5, -0.5]
#A = array([[-1, 0, -2], [2,1,2], [-1,0,-2], [2,1,1], [3,1,3]], dtype=float)
#b = array([[1,1,1,1,1]], dtype=float)
#x* = [0, 9/5, -2/5]