Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Interpolation basis

Project: Interpolation
Views: 887
1
from sympy import Function,S,symbols,And,Piecewise,lambdify,Matrix,pi,latex
2
from sympy.parsing.sympy_parser import parse_expr
3
from sympy.functions.special.polynomials import jacobi
4
5
from numpy import array,float32,linspace
6
import src.lagrange as lg
7
import matplotlib.pyplot as plt
8
from src.dbg import dbg
9
10
11
'''
12
p=Piecewise((f,x<=0),(g,And(x>0,x<1)),(0,True))
13
p.subs(x,0)
14
p.integrate()
15
p.diff()
16
h=f*p
17
h.integrate()
18
s=lambdify(x,h,"numpy")
19
t=p.integrate((x,0,1))
20
t.evalf()
21
'''
22
#points=lg.choosingPoints(x0=0,n=10)
23
#pointsTest1=[0,pi/6.,pi/4.,pi/3.,pi/2.,2*pi/3.,3*pi/4.,5*pi/6.,pi]
24
25
class LeastSquares():
26
'''
27
find aproximation use least squares method
28
29
func must be set while initialisation
30
basis and points added directly or
31
use pointsSet for point
32
and createBasisH or createBasisJ for Head or Jacoby basis
33
see tests for examples
34
used as
35
l=LeastSquares(func='cos(x)')
36
l.points=list of points or
37
l.points=lg.choosingPoints(x0=0,n=10)
38
l.createBasisH()
39
l.leastSquares(weightFunction=1)
40
l.plot()
41
42
43
'''
44
def __init__(self,func='sin(x)'):
45
self.x=symbols('x')
46
self.points=lg.choosingPoints(x0=0,n=10)
47
self.basis=[]
48
self.texBasis=[]
49
self.texF=0
50
self.f=func
51
self.fLam=0
52
self.iF=0
53
54
def forEps(self,n):
55
x=self.x
56
W=(1-x)*(1+x)
57
self.points=[-1,1]
58
self.createBasisJ(n=n,alpha=1,beta=1)
59
self.leastSquares(weightFunction=W)
60
61
def eps(self):
62
a=linspace(self.points[0],self.points[-1],30)
63
ff=array([self.fLam(p) for p in a])
64
iFF=array([self.iF(p) for p in a])
65
return(max(ff-iFF))
66
67
def showInNotebook(self):
68
self.texBasis=[latex(b) for b in self.basis]
69
self.plot()
70
71
def test1H(self):
72
'''
73
find interplated function iF for function sin(x) (if you create LeastSquares object
74
as default)
75
in Chebyshev points
76
used Head function basis
77
'''
78
self.points=lg.choosingPoints(x0=0,n=10)
79
self.createBasisH()
80
#self.func = function
81
self.leastSquares(weightFunction=1)
82
self.plot()
83
84
def test2H(self):
85
'''
86
find interplated function iF for function sin(x) (if you create LeastSquares object
87
as default)
88
in points from interval 0,\pi
89
used Head function basis
90
'''
91
self.points=[0,float(pi/6.),float(pi/4.),float(pi/3.),float(pi/2.),float(2*pi/3.),float(3*pi/4.),float(5*pi/6.),float(pi)]
92
self.createBasisH()
93
#self.func=function
94
self.leastSquares(weightFunction=1)
95
self.plot()
96
#basisTest1=createBasisH(points=pointsTest1)
97
98
def test1J(self):
99
'''
100
find interplated function iF for function sin(x) (if you create LeastSquares object
101
as default)
102
in Chebyshev points
103
used Jacoby basis
104
'''
105
106
x=self.x
107
W=(1-x)*(1+x)
108
self.points=[-1,1]
109
self.createBasisJ(n=5,alpha=1,beta=1)
110
#self.func=function
111
self.leastSquares(weightFunction=W)
112
self.plot()
113
114
def plot(self):
115
x=self.x
116
#points=array([float(p) for p in self.points])
117
points=array(self.points)
118
f=parse_expr(self.f)
119
##print(f)
120
f1=lambdify(x,f)
121
f=lambdify(x,f,"numpy")
122
123
##print(points)
124
125
#print(self.points)
126
#print(points)
127
#print(f(points))
128
#print(self.iF(points))
129
a=linspace(points[0],points[-1],30)
130
#print(self.iF(a))
131
#print("from plot f(a)=%s"%f(a))
132
#print("from plot iF(a)=%s"%self.iF(a))
133
#print(f)
134
ff=array([f1(p) for p in a])
135
##print(ff)
136
iFF=array([self.iF(p) for p in a])
137
plt.plot(a,ff,a,iFF,'o')
138
#plt.plot(points,f(points),points,self.iF(points),'--')
139
plt.legend(('f(x)','interp f(x)'),loc='upper left')
140
plt.show()
141
142
143
def pointsSet(self,points):
144
self.points=points
145
146
def createBasisJ(self,n=5,alpha=0,beta=0):
147
'''
148
DESCRIPTION:
149
create basis from Jacobi polynomials
150
'''
151
x=self.x
152
basis=[jacobi(i,alpha,beta,x) for i in range(n)]
153
self.basis=basis
154
return(basis)
155
156
def createBasisH(self):#,points=points
157
'''
158
DESCRIPTION:
159
create Head functions for points:
160
(x-p_{i-1})/(p_{i}-p_{i-1}) ,x \in [p_{i-1},p_{i}]
161
H_{i}(x)=(p_{i+1}-x)/(p_{i+1}-p_{i}) ,x \in [p_{i},p_{i+1}]
162
0,otherwise
163
164
it use sympy.Piecewise for conditions
165
every conditions must be rigorus (<,>), otherwise uninfinite integrals
166
will be wrong
167
168
'''
169
points=array(self.points)
170
H=[]
171
x=self.x
172
deltaPoints=(points[1:]-points[:-1])[:-1]
173
ddeltaPoints=points[2:]-points[1:-1]
174
for i in range(len(points))[1:-1]:
175
cond=[]
176
cond.append(((x-points[i-1])/(points[i]-points[i-1]),And(x>points[i-1],x<points[i])))
177
cond.append(((points[i+1]-x)/(points[i+1]-points[i]),And(x>points[i],x<points[i+1])))
178
cond.append((0,True))
179
H.append(Piecewise(*cond))
180
#print(cond)
181
#print('********')
182
#print(H)
183
self.basis=H
184
return(H)
185
186
def leastSquares(self,weightFunction=1):#basis=basis,points=points
187
'''
188
DECRIPTIONS:
189
find a_{i} that satisfy
190
\integral(g(x)-\sum a_{j}*H_{j}(x))^2)->min
191
i.e:
192
find best aproximation of function func in form
193
L_{2} g= \sum a_{j}*H_{j}(x)
194
195
INPUT:
196
weight function:
197
for Head functions =1
198
for Jacobi functions=(1-x)**alpha*(1+x)**beta
199
self.points:
200
for Head functions same as for createBasisH
201
for Jacobi functions points=[-1,1]
202
self.func:
203
is function (as string) whom will be aproximated
204
when you useing Jacoby func mast be from -1 to 1
205
(for that use subset: f(x)=F((a+b)/2+(b-a)*x/2))
206
EXAMPLES:
207
forHead:
208
see self.test1H and test2H methods
209
l=LeastSquares(func='cos(x)')
210
l.points=lg.choocingPoints(x0=0,n=10)
211
l.createBasisH()
212
l.leastSquares()
213
l.plot()
214
for Jacoby:
215
see self.test1J methods
216
x=symbols('x')
217
W=(1-x)*(1+x)
218
l.points=[-1,1]
219
l.createBasisJ(n=5,alpha=1,beta=1)
220
l.leastSquares(weightFunction=W)
221
'''
222
x=self.x
223
points=self.points
224
basis=self.basis
225
func=self.f
226
f=parse_expr(func)
227
self.fLam=lambdify(x,f)
228
229
#h01=(basis[0]*basis[1]).integrate(x)#(x,points[0],points[-1])
230
#h02=(basis[0]*basis[2]).integrate(x)#(x,points[0],points[-1])
231
#h01=(basis[0]*basis[1]).integrate(x)
232
233
#print("H%s=%s"%(0,basis[0]))
234
#print("H%s=%s"%(1,basis[1]))
235
#print("H%s=%s"%(2,basis[2]))
236
#print("intg(H%s*H%s)=%s"%(0,1,h01))
237
#print("intg(H%s*H%s)=%s"%(0,2,h02))
238
#print("intg(H%s*H%s)=%s"%(0,3,h))
239
dbg(**{"leastSquares|part1":(points[0],points[-1])})
240
241
#return(h)
242
W=weightFunction
243
HH=Matrix([[(W*Hi*Hj).integrate((x,points[0],points[-1])) for Hj in basis] for Hi in basis])
244
#print(HH)
245
#print(basis)
246
#print(W)
247
#print(f)
248
#print((W*f*basis[0]).integrate((x,points[0],points[-1])))
249
b=Matrix([(W*f*Hi).integrate((x,points[0],points[-1])) for Hi in basis])
250
##print(b)
251
a=(HH**-1)*b
252
##print(a)
253
expr=sum([a[i]*basis[i] for i in range(len(basis))])
254
#print(expr)
255
dbg(**{"leastSquares|part2":(HH,b,a,expr)})
256
self.texF=latex(expr)
257
##print("expr=%s"%expr)
258
expr1=lambdify(x,expr)
259
##print("expr1(2)=%s"%expr1(2.0))
260
#expr=lambdify(x,expr,"numpy")
261
#print(expr(2.0))
262
#print(expr(linspace(0,1,10)))
263
self.iF=expr1
264
return(expr)
265
266
class my_funct(Function):
267
nargs = 1
268
@classmethod
269
def eval(cls, x):
270
print(cls)
271
print(type(cls))
272
if x.is_Number:
273
if x is S.Zero:
274
return S.One
275
elif x is S.Infinity:
276
return S.Zero
277
278
279