-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRemezPoly.py
87 lines (67 loc) · 2.28 KB
/
RemezPoly.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
import numpy as np
class RemezPoly:
def __init__(self, func, interval):
self.func=func;
self.interval=interval;
deg=10
maxIter=100
def solve_coeff(x, n, f):
"""
Solve the linear system of equations:
a0+a1*xi+a2*xi^2+...+an*x^n+(-1)^i E=f(xi)
for the unknowns a0, a1, ... an, E
Input:
x: list of (n+2) points
n: degree of polynomial
f: given function
Return:
p: list of coefficients
"""
A=np.ones(n+2)
for i in range(0, n):
A=np.c_[A, np.power(x, i+1)]
A=np.c_[A, np.power((-1),range(0,n+2))]
b=np.transpose(f(x))
p=np.linalg.solve(A,b)
return p
thresh=1e-32
f=func
n=deg
y=np.linspace(interval[0], interval[1],n+2, endpoint=True)
xf=np.linspace(interval[0], interval[1], n*10+2, endpoint=True)
fval=f(xf)
for i in range(0, maxIter):
p0=solve_coeff(y, n, f)
p=p0[0:-1]
c=range(0,len(p))
data=np.zeros(len(xf))
for j in range(0,len(xf)):
pf=np.dot(p,np.power(xf[j],c))
data[j]=fval[j]-pf
if max(data)<thresh:
break
y1=np.zeros(n+2)
y1[0]=interval[0]
y1[n+1]=interval[1]
extrema_ind=np.diff(np.sign(np.diff(data))).nonzero()[0] + 1
for j in range(0, n):
y1[j+1]=xf[extrema_ind[j]]
v=[data[0]]
for j in range(0, n):
v.append(abs(data[extrema_ind[j]]))
v.append(data[len(data)-1])
m=max(v)
ind=np.argmin(v)
if (abs(y[ind]-y1[ind])<thresh):
break
if (ind<len(y) and abs(y[ind+1]-y1[ind])<thresh):
break
y=y1
self.p=p
pfun=lambda x: np.dot(p,np.power(x,c))
self.pfun=pfun
def eval(self,x):
y=[]
for j in x:
y.append(self.pfun(j))
return y