/
helper.py
252 lines (202 loc) · 6.86 KB
/
helper.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import numpy as np
import matplotlib.pyplot as plt
import time
# Credit: "qsppack" (https://github.com/qsppack/QSPPACK)
def cvx_poly_coef(func, deg, opts=None):
import cvxpy as cp
"""
Find a polynomial approximation for a given function, using convex optimization.
Input:
func: The target function for which the polynomial approximation is sought.
deg: The degree of the polynomial approximation.
opts: A dictionary containing optional parameters for the function.
Output:
coef_full: The Chebyshev coefficients of the best polynomial approximation.
"""
# Default options
if opts is None:
opts = {
'npts': 200,
'epsil': 0.01,
'fscale': 1 - 0.01,
'intervals': [0, 1],
'isplot': False,
'objnorm': np.inf
}
# Check variables and assign local variables
assert len(opts['intervals']) % 2 == 0
parity = deg % 2
epsil = opts['epsil']
npts = opts['npts']
xpts = np.union1d(np.polynomial.chebyshev.chebpts1(2 * npts), opts['intervals'])
xpts = xpts[xpts >= 0]
npts = len(xpts)
n_interval = len(opts['intervals']) // 2
ind_union = []
ind_set = {}
for i in range(n_interval):
ind_set[i] = np.where((xpts >= opts['intervals'][2 * i]) & (xpts <= opts['intervals'][2 * i + 1]))[0]
ind_union = np.union1d(ind_union, ind_set[i])
# Evaluate the target function
fx = np.zeros(npts)
ind_union = ind_union.astype(int)
# print(f'ind_union: {ind_union}')
fx[ind_union] = opts['fscale'] * func(xpts[ind_union])
# Prepare the Chebyshev polynomials
if parity == 0:
n_coef = deg // 2 + 1
else:
n_coef = (deg + 1) // 2
Ax = np.zeros((npts, n_coef))
for k in range(1, n_coef + 1):
if parity == 0:
coef = [0] * (2 * (k - 1)) + [1]
else:
coef = [0] * (2 * k - 1) + [1]
Ax[:, k - 1] = np.polynomial.chebyshev.chebval(xpts, coef)
# Use CVXPY to optimize the Chebyshev coefficients
coef = cp.Variable(n_coef)
y = cp.Variable(npts)
objective = cp.Minimize(cp.norm(y[ind_union] - fx[ind_union], opts['objnorm']))
constraints = [
y == Ax @ coef,
y >= -(1 - epsil),
y <= (1 - epsil)
]
problem = cp.Problem(objective, constraints)
problem.solve()
err_inf = np.linalg.norm(y[ind_union].value - fx[ind_union], opts['objnorm'])
print(f'norm error = {err_inf}')
# Use numpy.poly1d to make sure the maximum is less than 1
coef_full = np.zeros(deg + 1)
if parity == 0:
coef_full[::2] = coef.value
else:
coef_full[1::2] = coef.value
sol_cheb = np.poly1d(coef_full)
max_sol = np.max(np.abs(sol_cheb(xpts)))
print(f'max of solution = {max_sol}')
if max_sol > 1.0 - 1e-10:
raise ValueError('Solution is not bounded by 1. Increase npts')
# Plot target polynomial
if opts['isplot']:
plt.figure(1)
plt.clf()
plt.plot(xpts, y.value, 'ro', linewidth=1.5)
for i in range(n_interval):
plt.plot(xpts[ind_set[i]], y.value[ind_set[i]], 'b-', linewidth=2)
plt.xlabel('$x$', fontsize=15)
plt.ylabel('$f(x)$', fontsize=15)
plt.legend({'polynomial', 'target'}, fontsize=15)
plt.figure(2)
plt.clf()
for i in range(n_interval):
plt.plot(xpts[ind_set[i]], np.abs(y.value[ind_set[i]] - fx[ind_set[i]]), 'k-', linewidth=1.5)
plt.xlabel('$x$', fontsize=15)
plt.ylabel('$|f_{poly}(x) - f(x)|$', fontsize=15)
plt.show()
return coef_full
def total_variation(P: np.ndarray, Q: np.ndarray):
return np.sum(np.abs(P - Q)) / 2
############# Warning! The functions covered run extremely slow! ########################
def cheby(n: int) -> list:
"""
Description & Return:
Return the coefficients of the Chebyshev polynomial of the first kind (Tn(x)).
(Descending order: [x_n, x_{n-1}, ..., x_0])
Args:
n: The order of T (Chebyshev polynomial) requested.
"""
if n == 0: return [1]
if n == 1: return [1, 0]
Tn_1 = cheby(n - 1)
Tn_2 = cheby(n - 2)
Tn_1.append(0)
Tn_2.insert(0, 0)
Tn_2.insert(0, 0)
# Tn = []
# for i in range(n + 1):
# Tn.append(2 * Tn_1[i] - Tn_2[i])
Tn = [2 * Tn_1[i] - Tn_2[i] for i in range(n + 1)]
return Tn
def cheby_calc(S: list, n: int) -> list:
"""
Description & Return:
Given a list "S" of (complex) numbers, return a list
that evaluate "S" by "Tn(x)" (Chebyshev polynomial), element-wise.
Args:
S: Input list, consisting of numbers.
n: The order of T (Chebyshev polynomial) requested.
"""
coeff = cheby(n)
# fS = []
# for x in S:
# fS.append(np.polyval(coeff, x))
fS = [np.polyval(coeff, x) for x in S]
# fS = np.array([np.polyval(coeff, x) for x in S])
return fS
def coskx(S, k):
"""
Description & Return:
Given a list "S" of (complex) numbers, return a list
that evaluate "S" by "cos(kx)", element-wise.
Args:
S: Input list, consisting of numbers.
k: The parameter for "cos(kx)".
"""
k = float(k)
# fS = []
# for x in S:
# fS.append(np.cos(k * x))
fS = np.array([np.cos(k * x) for x in S])
return fS
def sinkx(S, k):
"""
Description & Return:
Given a list "S" of (complex) numbers, return a list
that evaluate "S" by "sin(kx)", element-wise.
Args:
S: Input list, consisting of numbers.
k: The parameter for "sin(kx)".
"""
k = float(k)
# fS = []
# for x in S:
# fS.append(np.sin(k * x))
fS = np.array([np.sin(k * x) for x in S])
return fS
##############################################################################
# Generate a random circulant matrix.
def circulant_matrix(N, fixed=True):
if fixed:
np.random.seed(65535)
a, b = np.random.random(2**N), np.random.random(2**N)
for i in range(2 ** N):
if b[i] > 0.5:
a[i] *= -1
A = np.zeros((2**N, 2**N))
for i in range(2 ** N):
A[i] = a
# shift right (if shift left, "A" become symmetric)
a = np.roll(a, 1)
# kappa = np.linalg.cond(A)
return A
def sparse_matrix(A, d):
L = len(A)
zero_num = L - d
for i in range(L):
zero_pos = np.random.choice(range(L), zero_num)
for j in zero_pos:
A[i, j] = 0
return A
def random_matrix(N, fixed=True):
if fixed:
np.random.seed(65535)
A = np.zeros((2 ** N, 2 ** N), dtype=np.double)
for j in range(2 ** N):
a, b = np.random.random(2**N), np.random.random(2**N)
for i in range(2 ** N):
if b[i] > 0.5:
a[i] *= -1
A[j] = a
return A