-
Notifications
You must be signed in to change notification settings - Fork 17
/
chebfun2_suite.py
765 lines (661 loc) · 29.8 KB
/
chebfun2_suite.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
import numpy as np
from yroots.Combined_Solver import solve
from time import time
from matplotlib import pyplot as plt
# TODO Description of where these tests come from, links to relevant papers,
# acknowledgements, etc.
def sortRoots(roots, seed = 12399):
"""Sorts roots so they can be compared against other roots that were sorted the same way.
Sorts by distance from a random hyperplane to avoid roots being too close according to the sort.
"""
if len(roots) == 0:
return roots
np.random.seed(seed)
dim = roots.shape[1]
r = np.array(np.random.rand(dim))
order = np.argsort(roots@r)
return roots[order]
def pass_or_fail(funcs, yroots, roots, test_num, test_type="norm", tol=2.220446049250313e-13):
"""Determines whether a test passes or fails bsed on the given criteria.
Parameters
----------
funcs : list of functions
The functions to find the roots of.
yroots : numpy array
Roots found by yroots.
roots : numpy array
The list of "actual" or Marching Squares roots.
test_num : float or string
The number of the test. For example, test 9.2 one could pass in
"9.2" or 9.2.
test_type : string, optional
What type of test to use to determine wheter it passes or fails.
- "norm" -- runs norm_pass_or_fail, default
- "residual" -- runs residual_pass_or_fail
tol : float, optional
The tolerance with which we want to run our tests. Defualts to
1000*eps where eps is machine epsilon.
Raises
------
AssertionError
If len(yroots) != len(roots) or if it fails the residual
or norm tests.
ValueError
If test_type is not "norm" or "residual"
"""
if (test_type not in ['norm','residual']):
raise ValueError("test_type must be 'norm' or 'residual'.")
if len(yroots) != len(roots):
if len(yroots) > len(roots):
raise AssertionError("Test " + str(test_num) + ": YRoots found"
" too many roots: " + str(len(yroots)) +
" where " + str(len(roots)) + " were expected.")
else:
raise AssertionError("Test " + str(test_num) + ": YRoots didn't"
" find enough roots: " + str(len(yroots)) +
" where " + str(len(roots)) + " were expected.")
if test_type == 'norm':
assert norm_pass_or_fail(yroots, roots, tol=tol), "Test " + str(test_num) + " failed."
else:
assert residuals_pass_or_fail(funcs, yroots, tol=tol), "Test " + str(test_num) + " failed."
def norm_pass_or_fail(yroots, roots, tol=2.220446049250313e-13):
""" Determines whether the roots given pass or fail the test according
to whether or not their norms are within tol of the norms of the
"actual" roots, which are determined either by previously known
roots or Marching Squares roots.
Parameters
----------
yroots : numpy array
The roots that yroots found.
roots : numpy array
"Actual" roots either obtained analytically or through Marching
Squares.
tol : float, optional
Tolerance that determines how close the roots need to be in order
to be considered close. Defaults to 1000*eps where eps is machine
epsilon.
Returns
-------
bool
Whether or not all the roots were close enough.
"""
roots_sorted = sortRoots(roots)
yroots_sorted = sortRoots(yroots)
root_diff = roots_sorted - yroots_sorted
norm0 = np.linalg.norm(root_diff[:,0])
norm1 = np.linalg.norm(root_diff[:,1])
return norm0 < tol and norm1 < tol, norm0, norm1
def residuals(func, roots):
""" Finds the residuals of the given function at the roots.
Paramters
---------
func : function
The function to find the residuals of.
roots : numpy array
The coordinates of the roots.
Returns
-------
numpy array
The residuals of the function.
"""
return np.abs(func(roots[:,0],roots[:,1]))
def residuals_pass_or_fail(funcs, roots, tol=2.220446049250313e-13):
""" Determines whether the roots given pass or fail the test according
to whether or not the maximal residuals are within a certain tolerance.
Parameters
----------
funcs : list of functions
The functions to find the residuals of.
roots : numpy array
The roots to plug into the functions to get the residuals.
tol : float, optional
How close to 0 the maximal residual must be in order to pass.
Defaults to 1000* eps where eps is machine epsilon.
Returns
-------
bool
True if the roots pass the test (are close enough to 0), False
otherwise.
"""
for func in funcs:
if np.max(residuals(func, roots)) > tol:
return False
return True
def verbose_pass_or_fail(funcs, yroots, polished_roots, test_num, cheb_roots=None, tol=2.220446049250313e-13):
""" Determines which tests pass and which fail.
Parameters
----------
funcs : list of functions
The functions to find the roots of.
yroots : numpy array
Roots found by yroots.
MSroots : numpy array
The list of "actual" or Marching Squares roots.
test_num : float or string
The number of the test. For example, test 9.2 one could pass in
"9.2" or 9.2.
cheb_roots : numpy array
Chebfun roots for extra comparison when MS are available.
tol : float, optional
The tolerance with which we want to run our tests. Defualts to
1000*eps where eps is machine epsilon.
Raises
------
AssertionError
If len(yroots) != len(roots) or if it fails the residual
or norm tests.
"""
print ("=========================================================")
print("Test " + str(test_num))
#Make sure dimensions are right
if polished_roots.ndim == 1:
polished_roots = polished_roots[..., np.newaxis].T
#Fail if the number of roots is wrong
if len(yroots) != len(polished_roots) and test_num != 6.1:
print(f"\t Num Roots Wrong! Found {len(yroots)}, Has {len(polished_roots)}!")
return False, False
alt_resid_tols = {4.2: 3.35e-07, 10.1 : 5e-12}
if test_num in alt_resid_tols.keys():
residuals_pass = residuals_pass_or_fail(funcs, yroots, alt_resid_tols[test_num])
else:
residuals_pass = residuals_pass_or_fail(funcs, yroots, tol)
if residuals_pass:
print("\t Residual test: pass")
else:
print("\t Residual test: fail")
if cheb_roots is not None:
if residuals_pass_or_fail(funcs, cheb_roots, tol):
print("\t Chebfun passes residual test")
else:
print("\t Chebfun fails residual test")
try:
norm_pass, x_norm, y_norm = norm_pass_or_fail(polished_roots, cheb_roots, tol)
if norm_pass:
print("\t Chebfun norm test: pass")
else:
print("\t Chebfun norm test: fail")
print("The norm of the difference in x values:", x_norm)
print("The norm of the difference in y values:", y_norm)
except ValueError as e:
print("A different number of roots were found.")
print ("Yroots: " + str(len(yroots)))
print("Chebfun Roots: " + str(len(cheb_roots)))
alt_norm_tols = {1.2 : 1e-7, 3.1 : 5e-11, 4.2 : 7e-13, 7.2 : 1e-8}
if polished_roots is not None:
try:
if test_num == 6.1:
norm_pass = True
elif test_num in alt_norm_tols.keys():
norm_pass, x_norm, y_norm = norm_pass_or_fail(yroots, polished_roots, alt_norm_tols[test_num])
else:
norm_pass, x_norm, y_norm = norm_pass_or_fail(yroots, polished_roots, tol)
if norm_pass:
print("\t YRoots norm test: pass")
else:
print("\t YRoots norm test: fail")
print("The norm of the difference in x values:", x_norm)
print("The norm of the difference in y values:", y_norm)
except ValueError as e:
print("A different number of roots were found.")
print ("Yroots: " + str(len(yroots)))
print("Polished: " + str(len(polished_roots)))
print("YRoots max residuals:")
YR_resid = list()
for i, func in enumerate(funcs):
YR_resid.append(residuals(func, yroots))
print("\tf" + str(i) + ": " + str(np.max(residuals(func, yroots))))
cheb_resid = None
if cheb_roots is not None:
cheb_resid = list()
print("Chebfun max residuals:")
for i, func in enumerate(funcs):
cheb_resid.append(residuals(func, cheb_roots))
print("\tf" + str(i) + ": " + str(np.max(residuals(func, cheb_roots))))
if polished_roots is not None:
print("Polished max residuals:")
Other_resid = list()
for i, func in enumerate(funcs):
Other_resid.append(residuals(func, polished_roots))
print("\tf" + str(i) + ": " + str(np.max(residuals(func, polished_roots))))
if len(yroots) > len(polished_roots):
print("YRoots found more roots.")
print("=========================================================")
return residuals_pass,norm_pass
# print("Comparison of Residuals (YRoots <= Other)")
num_smaller = 0
if polished_roots is not None:
for i in range(len(YR_resid)):
comparison_array = (YR_resid[i] <= Other_resid[i])
# print(comparison_array)
num_smaller += np.sum(comparison_array)
print("Number of YRoots residual values <= Polished residual values are: " + str(num_smaller))
if cheb_resid is not None:
if len(yroots) > len(cheb_roots):
print("=========================================================")
return residuals_pass,norm_pass
for i in range(len(YR_resid)):
comparison_array2 = (YR_resid[i] <= cheb_resid[i])
num_smaller += np.sum(comparison_array2)
print("Number of YRoots residual values <= to Chebfun residual values are: " + str(num_smaller))
print("=========================================================")
return residuals_pass,norm_pass
#TODO: include test cases for for returnbounding boxes, exact, rescale
def test_roots_1_1():
# Test 1.1
f = lambda x,y: 144*(x**4+y**4)-225*(x**2+y**2) + 350*x**2*y**2+81
g = lambda x,y: y-x**6
funcs = [f,g]
a, b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_1.1.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_1.1.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 1.1, cheb_roots=chebfun_roots)
def test_roots_1_2():
# Test 1.2
f = lambda x,y: (y**2-x**3)*((y-0.7)**2-(x-0.3)**3)*((y+0.2)**2-(x+0.8)**3)*((y+0.2)**2-(x-0.8)**3)
g = lambda x,y: ((y+.4)**3-(x-.4)**2)*((y+.3)**3-(x-.3)**2)*((y-.5)**3-(x+.6)**2)*((y+0.3)**3-(2*x-0.8)**3)
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_1.2.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_1.2.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 1.2, cheb_roots=chebfun_roots, tol=2.220446049250313e-10)
def test_roots_1_3():
# Test 1.3
f = lambda x,y: y**2-x**3
g = lambda x,y: (y+.1)**3-(x-.1)**2
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_1.3.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_1.3.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 1.3, cheb_roots=chebfun_roots)
def test_roots_1_4():
# Test 1.4
f = lambda x,y: x - y + .5
g = lambda x,y: x + y
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
# Single root has to be in matrix form because yroots
# returns the roots in matrix form.
actual_roots = np.load('Polished_results/polished_1.4.npy')
chebfun_roots = np.array([np.loadtxt('Chebfun_results/test_roots_1.4.csv', delimiter=',')])
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 1.4, cheb_roots=chebfun_roots)
def test_roots_1_5():
# Test 1.5
f = lambda x,y: y + x/2 + 1/10
g = lambda x,y: y - 2.1*x + 2
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
# Single root has to be in matrix form because yroots
# returns the roots in matrix form.
actual_roots = np.load('Polished_results/polished_1.5.npy')
chebfun_roots = np.array([np.loadtxt('Chebfun_results/test_roots_1.5.csv', delimiter=',')])
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 1.5, cheb_roots=chebfun_roots)
def test_roots_2_1():
# Test 2.1
f = lambda x,y: np.cos(10*x*y)
g = lambda x,y: x + y**2
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_2.1.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_2.1.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 2.1, cheb_roots=chebfun_roots)
def test_roots_2_2():
# Test 2.2
f = lambda x,y: x
g = lambda x,y: (x-.9999)**2 + y**2-1
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_2.2.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_2.2.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 2.2, cheb_roots=chebfun_roots)
def test_roots_2_3():
# Test 2.3
f = lambda x,y: np.sin(4*(x + y/10 + np.pi/10))
g = lambda x,y: np.cos(2*(x-2*y+ np.pi/7))
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_2.3.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_2.3.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 2.3, cheb_roots=chebfun_roots)
def test_roots_2_4():
# Test 2.4
f = lambda x,y: np.exp(x-2*x**2-y**2)*np.sin(10*(x+y+x*y**2))
g = lambda x,y: np.exp(-x+2*y**2+x*y**2)*np.sin(10*(x-y-2*x*y**2))
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_2.4.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_2.4.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 2.4, cheb_roots=chebfun_roots)
def test_roots_2_5():
# Test 2.5
f = lambda x,y: 2*y*np.cos(y**2)*np.cos(2*x)-np.cos(y)
g = lambda x,y: 2*np.sin(y**2)*np.sin(2*x)-np.sin(x)
funcs = [f,g]
a,b = np.array([-4,-4]), np.array([4,4])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_2.5.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_2.5.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 2.5, cheb_roots=chebfun_roots, tol=2.220446049250313e-12)
def test_roots_3_1():
# Test 3.1
f = lambda x,y: ((x-.3)**2+2*(y+0.3)**2-1)
g = lambda x,y: ((x-.49)**2+(y+.5)**2-1)*((x+0.5)**2+(y+0.5)**2-1)*((x-1)**2+(y-0.5)**2-1)
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_3.1.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_3.1.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 3.1, cheb_roots=chebfun_roots, tol=2.220446049250313e-11)
def test_roots_3_2():
# Test 3.2
f = lambda x,y: ((x-0.1)**2+2*(y-0.1)**2-1)*((x+0.3)**2+2*(y-0.2)**2-1)*((x-0.3)**2+2*(y+0.15)**2-1)*((x-0.13)**2+2*(y+0.15)**2-1)
g = lambda x,y: (2*(x+0.1)**2+(y+0.1)**2-1)*(2*(x+0.1)**2+(y-0.1)**2-1)*(2*(x-0.3)**2+(y-0.15)**2-1)*((x-0.21)**2+2*(y-0.15)**2-1)
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_3.2.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_3.2.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 3.2, cheb_roots=chebfun_roots, tol=2.220446049250313e-11)
def test_roots_4_1():
# Test 4.1
# This system hs 4 true roots, but ms fails (finds 5).
f = lambda x,y: np.sin(3*(x+y))
g = lambda x,y: np.sin(3*(x-y))
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_4.1.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_4.1.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 4.1, cheb_roots=chebfun_roots)
def test_roots_4_2():
# Test 4.2
f = lambda x,y: ((90000*y**10 + (-1440000)*y**9 + (360000*x**4 + 720000*x**3 + 504400*x**2 + 144400*x + 9971200)*(y**8) +
((-4680000)*x**4 + (-9360000)*x**3 + (-6412800)*x**2 + (-1732800)*x + (-39554400))*(y**7) + (540000*x**8 +
2160000*x**7 + 3817600*x**6 + 3892800*x**5 + 27577600*x**4 + 51187200*x**3 + 34257600*x**2 + 8952800*x + 100084400)*(y**6) +
((-5400000)*x**8 + (-21600000)*x**7 + (-37598400)*x**6 + (-37195200)*x**5 + (-95198400)*x**4 +
(-153604800)*x**3 + (-100484000)*x**2 + (-26280800)*x + (-169378400))*(y**5) + (360000*x**12 + 2160000*x**11 +
6266400*x**10 + 11532000*x**9 + 34831200*x**8 + 93892800*x**7 + 148644800*x**6 + 141984000*x**5 + 206976800*x**4 +
275671200*x**3 + 176534800*x**2 + 48374000*x + 194042000)*(y**4) + ((-2520000)*x**12 + (-15120000)*x**11 + (-42998400)*x**10 +
(-76392000)*x**9 + (-128887200)*x**8 + (-223516800)*x**7 + (-300675200)*x**6 + (-274243200)*x**5 + (-284547200)*x**4 +
(-303168000)*x**3 + (-190283200)*x**2 + (-57471200)*x + (-147677600))*(y**3) + (90000*x**16 + 720000*x**15 + 3097600*x**14 +
9083200*x**13 + 23934400*x**12 + 58284800*x**11 + 117148800*x**10 + 182149600*x**9 + 241101600*x**8 + 295968000*x**7 +
320782400*x**6 + 276224000*x**5 + 236601600*x**4 + 200510400*x**3 + 123359200*x**2 + 43175600*x + 70248800)*(y**2) +
((-360000)*x**16 + (-2880000)*x**15 + (-11812800)*x**14 + (-32289600)*x**13 + (-66043200)*x**12 + (-107534400)*x**11 +
(-148807200)*x**10 + (-184672800)*x**9 + (-205771200)*x**8 + (-196425600)*x**7 + (-166587200)*x**6 + (-135043200)*x**5 +
(-107568800)*x**4 + (-73394400)*x**3 + (-44061600)*x**2 + (-18772000)*x + (-17896000))*y + (144400*x**18 + 1299600*x**17 +
5269600*x**16 + 12699200*x**15 + 21632000*x**14 + 32289600*x**13 + 48149600*x**12 + 63997600*x**11 + 67834400*x**10 +
61884000*x**9 + 55708800*x**8 + 45478400*x**7 + 32775200*x**6 + 26766400*x**5 + 21309200*x**4 + 11185200*x**3 + 6242400*x**2 +
3465600*x + 1708800)))
g = lambda x,y: 1e-4*(y**7 + (-3)*y**6 + (2*x**2 + (-1)*x + 2)*y**5 + (x**3 + (-6)*x**2 + x + 2)*y**4 + (x**4 + (-2)*x**3 + 2*x**2 +
x + (-3))*y**3 + (2*x**5 + (-3)*x**4 + x**3 + 10*x**2 + (-1)*x + 1)*y**2 + ((-1)*x**5 + 3*x**4 + 4*x**3 + (-12)*x**2)*y +
(x**7 + (-3)*x**5 + (-1)*x**4 + (-4)*x**3 + 4*x**2))
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_4.2.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_4.2.csv', delimiter=',')
print(yroots)
print(actual_roots)
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 4.2, cheb_roots=chebfun_roots)
def test_roots_5():
# Test 5.1
f = lambda x,y: 2*x*y*np.cos(y**2)*np.cos(2*x)-np.cos(x*y)
g = lambda x,y: 2*np.sin(x*y**2)*np.sin(3*x*y)-np.sin(x*y)
funcs = [f,g]
a,b = np.array([-2,-2]), np.array([2,2])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_5.1.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_5.1.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 5.1, cheb_roots=chebfun_roots)
def test_roots_6_1():
# Test 6.1
f = lambda x,y: (y - 2*x)*(y+0.5*x)
g = lambda x,y: x*(x**2+y**2-1)
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_6.1.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_6.1.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 6.1, cheb_roots=chebfun_roots)
def test_roots_6_2():
# Test 6.2
f = lambda x,y: (y - 2*x)*(y+.5*x)
g = lambda x,y: (x-.0001)*(x**2+y**2-1)
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_6.2.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_6.2.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 6.2, cheb_roots=chebfun_roots, tol=2.220446049250313e-11)
def test_roots_6_3():
# Test 6.3
f = lambda x,y: 25*x*y - 12
g = lambda x,y: x**2+y**2-1
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_6.3.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_6.3.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 6.3, cheb_roots=chebfun_roots)
def test_roots_7_1():
# Test 7.1
f = lambda x,y: (x**2+y**2-1)*(x-1.1)
g = lambda x,y: (25*x*y-12)*(x-1.1)
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_7.1.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_7.1.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 7.1, cheb_roots=chebfun_roots)
def test_roots_7_2():
# Test 7.2
f = lambda x,y: y**4 + (-1)*y**3 + (2*x**2)*(y**2) + (3*x**2)*y + (x**4)
h = lambda x,y: y**10-2*(x**8)*(y**2)+4*(x**4)*y-2
g = lambda x,y: h(2*x,2*(y+.5))
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_7.2.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_7.2.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 7.2, cheb_roots=chebfun_roots, tol=2.220446049250313e-10)
def test_roots_7_3():
# Test 7.3
c = 1.e-09
f = lambda x,y: np.cos(x*y/(c**2))+np.sin(3*x*y/(c**2))
g = lambda x,y: np.cos(y/c)-np.cos(2*x*y/(c**2))
funcs = [f,g]
a,b = np.array([-1e-9, -1e-9]), np.array([1e-9, 1e-9])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_7.3.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_7.3.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 7.3, cheb_roots=chebfun_roots,tol=2.220446049250313e-10)
def test_roots_7_4():
# Test 7.4
f = lambda x,y: np.sin(3*np.pi*x)*np.cos(x*y)
g = lambda x,y: np.sin(3*np.pi*y)*np.cos(np.sin(x*y))
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_7.4.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_7.4.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 7.4, cheb_roots=chebfun_roots)
def test_roots_8_1():
# Test 8.1
f = lambda x,y: np.sin(10*x-y/10)
g = lambda x,y: np.cos(3*x*y)
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_8.1.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_8.1.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 8.1, cheb_roots=chebfun_roots)
def test_roots_8_2():
# Test 8.2
f = lambda x,y: np.sin(10*x-y/10) + y
g = lambda x,y: np.cos(10*y-x/10) - x
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_8.2.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_8.2.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 8.2, cheb_roots=chebfun_roots)
def test_roots_9_1():
# Test 9.1
f = lambda x,y: x**2+y**2-.9**2
g = lambda x,y: np.sin(x*y)
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_9.1.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_9.1.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 9.1, cheb_roots=chebfun_roots)
def test_roots_9_2():
# Test 9.2
f = lambda x,y: x**2+y**2-.49**2
g = lambda x,y: (x-.1)*(x*y - .2)
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.load('Polished_results/polished_9.2.npy')
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_9.2.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 9.2, cheb_roots=chebfun_roots)
def test_roots_10():
# Test 10.1
f = lambda x,y: (x-1)*(np.cos(x*y**2)+2)
g = lambda x,y: np.sin(8*np.pi*y)*(np.cos(x*y)+2)
funcs = [f,g]
a,b = np.array([-1,-1]), np.array([1,1])
start = time()
yroots = solve(funcs,a,b)
t = time() - start
actual_roots = np.array([[1, -1.0], [1, -0.875], [1, -0.75], [1, -0.625], [1, -0.5], [1, -0.375],
[1, -0.25], [1, -0.125], [1, 0.0], [1, 0.125], [1, 0.25], [1, 0.375],
[1, 0.5], [1, 0.625], [1, 0.75], [1, 0.875], [1, 1.0]])
chebfun_roots = np.loadtxt('Chebfun_results/test_roots_10.1.csv', delimiter=',')
return t, verbose_pass_or_fail([f,g], yroots, actual_roots, 10.1, cheb_roots=chebfun_roots)
def plot_timings(tests,timings):
labels = [test.__name__[11:].replace('_','.') for test in tests]
plt.figure(figsize=(8,5))
plt.subplot(211)
plt.bar(labels,timings)
plt.xticks(rotation=45)
plt.ylim(0,40)
plt.subplot(212)
plt.bar(labels,timings)
plt.xticks(rotation=45)
plt.yscale('log')
plt.ylim((10**-3,10**2))
plt.tight_layout()
plt.show()
if __name__ == "__main__":
# # Run all the tests!
test_roots_2_5()
tests = np.array([test_roots_1_1,
test_roots_1_2,
test_roots_1_3,
test_roots_1_4,
test_roots_1_5,
test_roots_2_1,
test_roots_2_2,
test_roots_2_3,
test_roots_2_4,
test_roots_2_5,
test_roots_3_1,
test_roots_3_2,
test_roots_4_1,
test_roots_4_2,
test_roots_5,
test_roots_6_1,
test_roots_6_2,
test_roots_6_3,
test_roots_7_1,
test_roots_7_2,
test_roots_7_3,
test_roots_7_4,
test_roots_8_1,
test_roots_8_2,
test_roots_9_1,
test_roots_9_2,
test_roots_10])
res_passes = np.zeros_like(tests,dtype=bool)
norm_passes = np.zeros_like(tests,dtype=bool)
times = np.zeros_like(tests)
start = time()
for i,test in enumerate(tests):
t, passes = test()
res_pass,norm_pass = passes
res_passes[i] = res_pass
norm_passes[i] = norm_pass
times[i] = t
finish = time()
print('\n\nSummary')
print(f'Residual Test: Passed {np.sum(res_passes)} of 27, {100*np.mean(res_passes)}%')
where_failed_res = np.where(~res_passes)[0]
failed_res_tests = tests[where_failed_res]
print(f'Failed Residual Test on \n{[t.__name__ for t in failed_res_tests]}')
print(f'Norm Test : Passed {np.sum(norm_passes)} of 27, {100*np.mean(norm_passes)}%')
where_failed_norm = np.where(~norm_passes)[0]
failed_norm_tests = tests[where_failed_norm]
print(f'Failed Norm Test on \n{[t.__name__ for t in failed_norm_tests]}')
plot_timings(tests,times)
print(finish-start)