-
Notifications
You must be signed in to change notification settings - Fork 1
/
dual_certificates _v2.py
774 lines (620 loc) · 24.8 KB
/
dual_certificates _v2.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
"""Tools for creating interpolation-based dual certificates."""
from trig_poly import TrigPoly, MultiTrigPoly
import mpmath
import numpy as np
from scipy import linalg as sp_linalg
def _interpolator_norm_quadratic_form(support, kernel):
"""Quadratic form calculating L2 norm of interpolator from coefficients."""
n = support.shape[0]
kernel_1 = kernel.derivative()
kernel_inners = kernel.inners_of_shifts(support)
kernel_1_inners = kernel_1.inners_of_shifts(support)
cross_inners = kernel.inners_of_shifts_and_derivative_shifts(support)
S = np.zeros((4*n, 4*n)).astype(np.complex128)
S[:n, :n] = kernel_inners
S[n:2*n, n:2*n] = kernel_1_inners
S[n:2*n, :n] = cross_inners.T
S[:n, n:2*n] = cross_inners
S[2*n:3*n, 2*n:3*n] = kernel_inners
S[3*n:, 3*n:] = kernel_1_inners
S[3*n:, 2*n:3*n] = cross_inners.T
S[2*n:3*n, 3*n:] = cross_inners
# TODO: Make sure it's ok to cast to real here
S = (S + S.T).real * 0.5
return S
def _interpolator_linear_constraints(support, sign_pattern, kernel):
"""Build linear constraint data for tangent plane derivative problem."""
n = support.shape[0]
m = sign_pattern.shape[1]
time_deltas = np.outer(support, np.ones(n)) - np.outer(np.ones(n), support)
kernel_1 = kernel.derivative()
kernel_2 = kernel_1.derivative()
kernel_values = kernel(time_deltas)
kernel_1_values = kernel_1(time_deltas)
kernel_2_values = kernel_2(time_deltas)
sign_pattern_real = np.real(sign_pattern)
sign_pattern_imag = np.imag(sign_pattern)
zeros = np.zeros((n, n))
problem_mx_rows = []
problem_obj_cols = []
for k in range(m):
# Row of real part constraint
row1 = []
for _ in range(4 * k):
row1.append(zeros)
row1.append(kernel_values)
row1.append(kernel_1_values)
row1.append(zeros)
row1.append(zeros)
for _ in range(4 * (m - 1 - k)):
row1.append(zeros)
problem_mx_rows.append(row1)
# Row of imaginary part constraint
row2 = []
for _ in range(4 * k):
row2.append(zeros)
row2.append(zeros)
row2.append(zeros)
row2.append(kernel_values)
row2.append(kernel_1_values)
for _ in range(4 * (m - 1 - k)):
row2.append(zeros)
problem_mx_rows.append(row2)
gradient_row = []
for k in range(m):
# Row of gradient constraint
single_sign_pattern_real = sign_pattern_real[:, k]
single_sign_pattern_imag = sign_pattern_imag[:, k]
gradient_row.append(
single_sign_pattern_real.reshape((n, 1)) * kernel_1_values)
gradient_row.append(
single_sign_pattern_real.reshape((n, 1)) * kernel_2_values)
gradient_row.append(
single_sign_pattern_imag.reshape((n, 1)) * kernel_1_values)
gradient_row.append(
single_sign_pattern_imag.reshape((n, 1)) * kernel_2_values)
# Objective
problem_obj_cols.append(single_sign_pattern_real)
problem_obj_cols.append(single_sign_pattern_imag)
problem_mx_rows.append(gradient_row)
problem_mx = np.bmat(problem_mx_rows)
problem_obj_cols.append(np.zeros(n))
problem_obj = np.hstack(problem_obj_cols)
return problem_mx, problem_obj
def _interpolator_linear_constraints_kernel_only(
support, sign_pattern, kernel):
"""Build linear constraint data for tangent plane derivative problem."""
n = support.shape[0]
m = sign_pattern.shape[1]
time_deltas = np.outer(support, np.ones(n)) - np.outer(np.ones(n), support)
kernel_values = kernel(time_deltas)
sign_pattern_real = np.real(sign_pattern)
sign_pattern_imag = np.imag(sign_pattern)
zeros = np.zeros((n, n))
problem_mx_rows = []
problem_obj_cols = []
for k in range(m):
# Row of real part constraint
row1 = []
for _ in range(2 * k):
row1.append(zeros)
row1.append(zeros)
row1.append(kernel_values)
for _ in range(2 * (m - 1 - k)):
row1.append(zeros)
problem_mx_rows.append(row1)
# Row of imaginary part constraint
row2 = []
for _ in range(2 * k):
row2.append(zeros)
row2.append(zeros)
row2.append(kernel_values)
for _ in range(2 * (m - 1 - k)):
row2.append(zeros)
problem_mx_rows.append(row2)
for k in range(m):
single_sign_pattern_real = sign_pattern_real[:, k]
single_sign_pattern_imag = sign_pattern_imag[:, k]
# Objective
problem_obj_cols.append(single_sign_pattern_real)
problem_obj_cols.append(single_sign_pattern_imag)
problem_mx = np.bmat(problem_mx_rows)
problem_obj_cols.append(np.zeros(n))
problem_obj = np.hstack(problem_obj_cols)
return problem_mx, problem_obj
def _interpolator_linear_constraints_with_derivatives(
support, sign_pattern, kernel, support_derivatives):
"""Build linear constraint data for fixed derivative problem.
kernel (fn)
support (np.array(s))
sign_pattern (np.array(s, m))
support_derivatives (np.array(s, m))
"""
n = support.shape[0]
m = sign_pattern.shape[1]
time_deltas = np.outer(support, np.ones(n)) - np.outer(np.ones(n), support)
kernel_1 = kernel.derivative()
kernel_2 = kernel_1.derivative()
kernel_values = kernel(time_deltas)
kernel_1_values = kernel_1(time_deltas)
kernel_2_values = kernel_2(time_deltas)
sign_pattern_real = np.real(sign_pattern)
sign_pattern_imag = np.imag(sign_pattern)
problem_mx = np.bmat([
[kernel_values, kernel1_values],
[kernel1_values, kernel2_values]])
coeffss = []
for k in range(m):
single_sign_pattern = sign_pattern[:, k]
problem_obj = np.hstack(
[single_sign_pattern, np.zeros(single_sign_pattern.shape[0])])
coeffss.append(np.linalg.solve(problem_mx, problem_obj))
zeros = np.zeros((n, n))
problem_mx_rows = []
problem_obj_cols = []
for k in range(m):
# Row of real part constraint
row1 = []
for _ in range(4 * k):
row1.append(zeros)
row1.append(kernel_values)
row1.append(kernel_1_values)
row1.append(zeros)
row1.append(zeros)
for _ in range(4 * (m - 1 - k)):
row1.append(zeros)
problem_mx_rows.append(row1)
problem_obj_cols.append(sign_pattern_real[:, k])
# Row of imaginary part constraint
row2 = []
for _ in range(4 * k):
row2.append(zeros)
row2.append(zeros)
row2.append(zeros)
row2.append(kernel_values)
row2.append(kernel_1_values)
for _ in range(4 * (m - 1 - k)):
row2.append(zeros)
problem_mx_rows.append(row2)
problem_obj_cols.append(sign_pattern_imag[:, k])
multiplier = 500
for k in range(m):
# Row of derivative real part constraint
row1 = []
for _ in range(4 * k):
row1.append(zeros)
row1.append(kernel_1_values / multiplier)
row1.append(kernel_2_values / multiplier)
row1.append(zeros)
row1.append(zeros)
for _ in range(4 * (m - 1 - k)):
row1.append(zeros)
problem_mx_rows.append(row1)
problem_obj_cols.append(np.real(support_derivatives[:, k]) / multiplier)
# Row of derivative imaginary part constraint
row2 = []
for _ in range(4 * k):
row2.append(zeros)
row2.append(zeros)
row2.append(zeros)
row2.append(kernel_1_values / multiplier)
row2.append(kernel_2_values / multiplier)
for _ in range(4 * (m - 1 - k)):
row2.append(zeros)
problem_mx_rows.append(row2)
problem_obj_cols.append(np.imag(support_derivatives[:, k]) / multiplier)
problem_mx = np.bmat(problem_mx_rows)
problem_obj = np.hstack(problem_obj_cols)
return problem_mx, problem_obj
def _optimize_quadratic_form(S, A, y, multiplier=1.0):
"""Maximize x^T S x subject to Ax = y.
The multiplier is a factor multiplied into S in formulating the linear
problem, which can help mitigate ill-conditioned systems (resulting from
magnitude discrepancies between S and A).
"""
m = A.shape[0]
n = S.shape[0]
# This expression for the solution is derived with Lagrange multipliers,
# the multiplier vector of the constraint Ax = y being in the last m
# coordinates of the result, which we discard.
return np.linalg.solve(
np.bmat([[multiplier * S, A.T], [A, np.zeros((m, m))]]),
np.hstack([np.zeros(n), y]))[:n]
#
# Interpolation functions
#
def interpolate(support, sign_pattern, kernel):
assert support.shape == sign_pattern.shape
assert np.all(np.absolute(np.absolute(sign_pattern) - 1.0) < 1e-10)
n = support.shape[0]
# time_deltas[i, j] = t_i - t_j
time_deltas = np.outer(support, np.ones(n)) - np.outer(np.ones(n), support)
kernel_values = kernel(time_deltas)
coeffs = np.linalg.solve(kernel_values, sign_pattern)
return kernel.sum_shifts(-support, coeffs)
def interpolate_with_derivative(support, sign_pattern, kernel):
assert support.shape == sign_pattern.shape
assert np.all(np.absolute(np.absolute(sign_pattern) - 1.0) < 1e-10)
n = support.shape[0]
# time_deltas[i, j] = t_i - t_j
time_deltas = np.outer(support, np.ones(n)) - np.outer(np.ones(n), support)
# NOTE: This is assuming that the kernel is real-valued.
kernel_1 = kernel.derivative()
kernel_2 = kernel_1.derivative()
kernel_values = kernel(time_deltas)
kernel_1_values = kernel_1(time_deltas)
kernel_2_values = kernel_2(time_deltas)
sign_pattern_real = np.real(sign_pattern)
sign_pattern_imag = np.imag(sign_pattern)
zeros = np.zeros((n, n))
# Build linear constraint objects
A = np.bmat([
[kernel_values, kernel_1_values, zeros, zeros],
[zeros, zeros, kernel_values, kernel_1_values],
[sign_pattern_real.reshape((n, 1)) * kernel_1_values,
sign_pattern_real.reshape((n, 1)) * kernel_2_values,
sign_pattern_imag.reshape((n, 1)) * kernel_1_values,
sign_pattern_imag.reshape((n, 1)) * kernel_2_values]]).astype(
np.float64)
y = np.hstack(
[sign_pattern_real,
sign_pattern_imag,
np.zeros(sign_pattern.shape[0])])
# Build objective quadratic form corresponding to interpolator L2 norm:
S = _interpolator_norm_quadratic_form(support, kernel)
coeffs = _optimize_quadratic_form(S, A, y)
return (
kernel.sum_shifts(-support, coeffs[:n]) +
kernel_1.sum_shifts(-support, coeffs[n:2*n]) +
kernel.sum_shifts(-support, coeffs[2*n:3*n] * 1j) +
kernel_1.sum_shifts(-support, coeffs[3*n:] * 1j))
def interpolate_direct(support, sign_pattern, kernel):
"""Toy interpolation model multiplying kernel copies by sign pattern."""
m = sign_pattern.shape[1]
return MultiTrigPoly([
sum(
(kernel.shift(-t) * sign
for t, sign in zip(support, sign_pattern[:, i])),
TrigPoly.zero())
for i in range(m)
])
def interpolate_multidim_fixed_derivatives(
support, sign_pattern, kernel, support_derivatives,
return_coeffs=False):
assert support.shape[0] == sign_pattern.shape[0]
assert np.all(
np.absolute(
np.sum(np.absolute(sign_pattern) ** 2, axis=1) - 1.0) < 1e-10)
assert support_derivatives.shape == sign_pattern.shape
n = support.shape[0]
m = sign_pattern.shape[1]
time_deltas = np.outer(support, np.ones(n)) - np.outer(np.ones(n), support)
kernel_1 = kernel.derivative()
kernel_2 = kernel_1.derivative()
kernel_values = kernel(time_deltas)
kernel_1_values = kernel_1(time_deltas)
kernel_2_values = kernel_2(time_deltas)
multiplier = 100.0
problem_mx = np.bmat([
[kernel_values, kernel_1_values / multiplier],
[kernel_1_values / multiplier, kernel_2_values / multiplier / multiplier]]).real
coeffss = []
for k in range(m):
problem_obj = np.hstack(
[sign_pattern[:, k], support_derivatives[:, k] / multiplier])
coeffss.append(np.linalg.lstsq(problem_mx, problem_obj)[0])
return MultiTrigPoly([
kernel.sum_shifts(-support, coeffs[:n]) +
kernel_1.sum_shifts(-support, coeffs[n:] / multiplier)
for coeffs in coeffss])
problem_mx, problem_obj = (
_interpolator_linear_constraints_with_derivatives(
support, sign_pattern, kernel, support_derivatives))
coeffs = np.linalg.solve(problem_mx, problem_obj)
kernel_coeffs = [
coeffs[4*k*n:(4*k+1)*n] + coeffs[(4*k+2)*n:(4*k+3)*n] * 1j
for k in range(m)]
kernel_derivative_coeffs = [
coeffs[(4*k+1)*n:(4*k+2)*n] + coeffs[(4*k+3)*n:(4*k+4)*n] * 1j
for k in range(m)]
if return_coeffs:
return kernel_coeffs, kernel_derivative_coeffs
else:
return MultiTrigPoly([
kernel.sum_shifts(-support, kernel_coeffs[k]) +
kernel.derivative().sum_shifts(
-support, kernel_derivative_coeffs[k])
for k in range(m)])
def interpolate_multidim_only_kernel(support, sign_pattern, kernel):
"""Interpolate only using kernels, no kernel derivatives or derivative
constraints.
"""
assert support.shape[0] == sign_pattern.shape[0]
assert np.all(
np.absolute(
np.sum(np.absolute(sign_pattern) ** 2, axis=1) - 1.0) < 1e-10)
n = support.shape[0]
m = sign_pattern.shape[1]
time_deltas = np.outer(support, np.ones(n)) - np.outer(np.ones(n), support)
kernel_values = kernel(time_deltas)
coeffss = []
for k in range(m):
single_sign_pattern = sign_pattern[:, k]
coeffss.append(
np.linalg.solve(kernel_values, single_sign_pattern))
return MultiTrigPoly([
kernel.sum_shifts(-support, coeffs)
for coeffs in coeffss])
def interpolate_multidim_l2_min(
support, sign_pattern, kernel, return_coeffs=False):
"""Interpolation fixing derivatives at interpolated points in tangent
plane of sphere, and minimizing L2 norm of the polynomial subject to this
constraint.
"""
assert support.shape[0] == sign_pattern.shape[0]
assert np.all(
np.absolute(
np.sum(np.absolute(sign_pattern) ** 2, axis=1) - 1.0) < 1e-10)
n = support.shape[0]
m = sign_pattern.shape[1]
problem_mx, problem_obj = _interpolator_linear_constraints(
support, sign_pattern, kernel)
#
# Build objective quadratic form
#
# S is block-diagonal, with k blocks of size 4n x 4n each of which is the
# same as the objective quadratic form from the one-sample case.
S_diag_block = _interpolator_norm_quadratic_form(support, kernel)
S = np.kron(np.identity(m), S_diag_block)
# This multiplier value is heuristically chosen.
multiplier = (
kernel.derivative().squared_norm() / kernel.squared_norm() * 1e6)
coeffs = _optimize_quadratic_form(
S,
problem_mx,
problem_obj,
multiplier=multiplier)
kernel_coeffs = [
coeffs[4*k*n:(4*k+1)*n] + coeffs[(4*k+2)*n:(4*k+3)*n] * 1j
for k in range(m)]
kernel_derivative_coeffs = [
coeffs[(4*k+1)*n:(4*k+2)*n] + coeffs[(4*k+3)*n:(4*k+4)*n] * 1j
for k in range(m)]
if return_coeffs:
return kernel_coeffs, kernel_derivative_coeffs
else:
return MultiTrigPoly([
kernel.sum_shifts(-support, kernel_coeffs[k]) +
kernel.derivative().sum_shifts(
-support, kernel_derivative_coeffs[k])
for k in range(m)])
def interpolate_multidim_only_kernel_l2_min(
support, sign_pattern, kernel, return_coeffs=False):
"""Interpolation fixing derivatives at interpolated points in tangent
plane of sphere, and minimizing L2 norm of the polynomial subject to this
constraint.
"""
assert support.shape[0] == sign_pattern.shape[0]
assert np.all(
np.absolute(
np.sum(np.absolute(sign_pattern) ** 2, axis=1) - 1.0) < 1e-10)
n = support.shape[0]
m = sign_pattern.shape[1]
problem_mx, problem_obj = _interpolator_linear_constraints_kernel_only(
support, sign_pattern, kernel)
print problem_mx.shape
#
# Build objective quadratic form
#
# S is block-diagonal, with k blocks of size 4n x 4n each of which is the
# same as the objective quadratic form from the one-sample case.
S_diag_block = kernel.inners_of_shifts(support)
S = np.kron(np.identity(2 * m), S_diag_block)
# This multiplier value is heuristically chosen.
multiplier = (
kernel.derivative().squared_norm() / kernel.squared_norm() * 1e6)
coeffs = _optimize_quadratic_form(
S,
problem_mx,
problem_obj,
multiplier=multiplier)
print coeffs
kernel_coeffs = [
coeffs[2*k*n:2*(k+1)*n] + coeffs[(k+2)*n:(2*k+3)*n] * 1j
for k in range(m)]
kernel_derivative_coeffs = [
coeffs[(4*k+1)*n:(4*k+2)*n] + coeffs[(4*k+3)*n:(4*k+4)*n] * 1j
for k in range(m)]
if return_coeffs:
return kernel_coeffs, kernel_derivative_coeffs
else:
return MultiTrigPoly([
kernel.sum_shifts(-support, kernel_coeffs[k]) +
kernel.derivative().sum_shifts(
-support, kernel_derivative_coeffs[k])
for k in range(m)])
#Fix the derivative to the difference of next and previous samples projected on the tangent plane
def interpolate_multidim_adjacent_samples(
support, sign_pattern, kernel):
assert support.shape[0] == sign_pattern.shape[0]
assert np.all(
np.absolute(
np.sum(np.absolute(sign_pattern) ** 2, axis=1) - 1.0) < 1e-10)
n = support.shape[0]
m = sign_pattern.shape[1]
time_deltas = np.outer(support, np.ones(n)) - np.outer(np.ones(n), support)
kernel_1 = kernel.derivative()
kernel_2 = kernel_1.derivative()
kernel_values = kernel(time_deltas)
kernel_1_values = kernel_1(time_deltas)
kernel_2_values = kernel_2(time_deltas)
sign_pattern_real = np.real(sign_pattern)
sign_pattern_imag = np.imag(sign_pattern)
#
# Build linear constraint data
#
zeros = np.zeros((n, n))
problem_mx_rows = []
problem_obj_cols = []
rand_scale = 1.0 / np.sqrt(n)
vj_p1_real = np.append(np.asarray([sign_pattern_real[i + 1,:] for i in range(n-1)]), np.zeros((1,m))).reshape((n,m))
vj_m1_real = np.append(np.zeros((1,m)), np.asarray([sign_pattern_real[i ,:] for i in range(n-1)])).reshape((n,m))
# vj_p1_real = np.random.normal(loc=0.0, scale=rand_scale, size=(n, m))
# vj_m1_real = np.random.normal(loc=0.0, scale=rand_scale, size=(n, m))
vj_coeffs_real = np.diagonal((vj_p1_real - vj_m1_real).dot(sign_pattern_real.T) )
vj_p1_imag = np.append(np.asarray([sign_pattern_imag[i + 1,:] for i in range(n-1)]), np.zeros((1,m))).reshape((n,m))
vj_m1_imag = np.append(np.zeros((1,m)), np.asarray([sign_pattern_imag[i ,:] for i in range(n-1)])).reshape((n,m))
# vj_p1_imag = np.random.normal(loc=0.0, scale=rand_scale, size=(n, m))
# vj_m1_imag = np.random.normal(loc=0.0, scale=rand_scale, size=(n, m))
vj_coeffs_imag = np.diagonal((vj_p1_imag - vj_m1_imag).dot(sign_pattern_imag.T) )
for k in range(m):
# Row of real part constraint
row1 = []
for _ in range(4 * k):
row1.append(zeros)
row1.append(kernel_values)
row1.append(kernel_1_values)
row1.append(zeros)
row1.append(zeros)
for _ in range(4 * (m - 1 - k)):
row1.append(zeros)
problem_mx_rows.append(row1)
# Row of imaginary part constraint
row2 = []
for _ in range(4 * k):
row2.append(zeros)
row2.append(zeros)
row2.append(zeros)
row2.append(kernel_values)
row2.append(kernel_1_values)
for _ in range(4 * (m - 1 - k)):
row2.append(zeros)
problem_mx_rows.append(row2)
# Objective
problem_obj_cols.append(sign_pattern_real[:, k])
problem_obj_cols.append(sign_pattern_imag[:, k])
for k in range(m):
row1 = []
for _ in range(4 * k):
row1.append(zeros)
row1.append(kernel_1_values)
row1.append(kernel_2_values)
row1.append(zeros)
row1.append(zeros)
for _ in range(4 * (m - 1 - k)):
row1.append(zeros)
problem_mx_rows.append(row1)
# Row of imaginary part constraint
row2 = []
for _ in range(4 * k):
row2.append(zeros)
row2.append(zeros)
row2.append(zeros)
row2.append(kernel_1_values)
row2.append(kernel_2_values)
for _ in range(4 * (m - 1 - k)):
row2.append(zeros)
problem_mx_rows.append(row2)
# Objective
problem_obj_cols.append(
(vj_p1_real[:, k] - vj_m1_real[:, k] -
np.multiply(vj_coeffs_real, sign_pattern_real[:, k])) / 1000.0)
problem_obj_cols.append(
(vj_p1_imag[:, k] - vj_m1_imag[:, k] -
np.multiply(vj_coeffs_imag, sign_pattern_imag[:, k])) / 1000.0)
problem_mx = np.bmat(problem_mx_rows)
problem_obj = np.hstack(problem_obj_cols)
#
# Solve
#
coeffs = np.linalg.solve(problem_mx, problem_obj)
#Or L2 min
# S_diag_block = _interpolator_norm_quadratic_form(kernel, support)
# S = np.kron(np.identity(m), S_diag_block)
# # This multiplier value is heuristically chosen.
# multiplier = kernel_1.squared_norm() / kernel.squared_norm() * 1e3
# coeffs = _optimize_quadratic_form(
# S,
# problem_mx,
# problem_obj,
# multiplier=multiplier)
return MultiTrigPoly([
(kernel.sum_shifts(-support, coeffs[4*k*n:(4*k+1)*n]) +
kernel_1.sum_shifts(-support, coeffs[(4*k+1)*n:(4*k+2)*n]) +
kernel.sum_shifts(-support, coeffs[(4*k+2)*n:(4*k+3)*n] * 1j) +
kernel_1.sum_shifts(-support, coeffs[(4*k+3)*n:(4*k+4)*n] * 1j))
for k in range(m)])
def interpolate_multidim_0Grad(support, sign_pattern, kernel):
assert support.shape[0] == sign_pattern.shape[0]
assert np.all(
np.absolute(
np.sum(np.absolute(sign_pattern) ** 2, axis=1) - 1.0) < 1e-10)
n = support.shape[0]
m = sign_pattern.shape[1]
kernel1 = kernel.derivative()
kernel2 = kernel1.derivative()
time_deltas = np.outer(support, np.ones(n)) - np.outer(np.ones(n), support)
kernel_values = kernel(time_deltas)
kernel1_values = kernel1(time_deltas)
kernel2_values = kernel2(time_deltas)
problem_mx = np.bmat([
[kernel_values, kernel1_values],
[kernel1_values, kernel2_values]])
coeffss = []
for k in range(m):
single_sign_pattern = sign_pattern[:, k]
problem_obj = np.hstack(
[single_sign_pattern, np.zeros(single_sign_pattern.shape[0])])
coeffss.append(np.linalg.solve(problem_mx, problem_obj))
return MultiTrigPoly([
(TrigPoly(
kernel.freqs,
sum(kernel.coeffs * np.exp(2.0 * np.pi * 1j * kernel.freqs * -t) * c
for c, t in zip(coeffs[:n], support))) +
TrigPoly(
kernel1.freqs,
sum(kernel1.coeffs * np.exp(2.0 * np.pi * 1j * kernel1.freqs * -t) * c
for c, t in zip(coeffs[n:], support))))
for coeffs in coeffss])
return MultiTrigPoly([
sum([kernel.shift(-t) * c for c, t in zip(coeffs[:n], support)],
TrigPoly.zero()) +
sum([kernel1.shift(-t) * c for c, t in zip(coeffs[n:], support)],
TrigPoly.zero())
for coeffs in coeffss])
#
# Validation functions
#
_EPSILON = 1e-7
def validate(support, sign_pattern, interpolator, grid_pts=1e3):
max_deviation = float('-inf')
for i in range(support.shape[0]):
if len(sign_pattern.shape) == 1:
sign_pattern_slice = sign_pattern[i]
else:
sign_pattern_slice = sign_pattern[i, :]
max_deviation = max(
max_deviation,
np.max(
np.absolute(
interpolator(support[i]).T - sign_pattern_slice)))
values_achieved = max_deviation <= _EPSILON
grid = np.linspace(0.0, 1.0, grid_pts)
grid_values = interpolator(grid)
if len(grid_values.shape) == 1:
grid_magnitudes = np.absolute(grid_values)
else:
grid_magnitudes = np.linalg.norm(grid_values, axis=0)
grid_magnitudes = np.ma.array(grid_magnitudes)
for t in support:
left_ix = np.searchsorted(grid, t)
grid_magnitudes[left_ix % grid_magnitudes.shape[0]] = np.ma.masked
grid_magnitudes[(left_ix + 1) % grid_magnitudes.shape[0]] = (
np.ma.masked)
bound_achieved = np.all(grid_magnitudes < 1.0)
status = values_achieved and bound_achieved
return {
'status': status,
'values_achieved': values_achieved,
'max_deviation': max_deviation,
'bound_achieved': bound_achieved}