-
Notifications
You must be signed in to change notification settings - Fork 0
/
distlink.cpp
1812 lines (1626 loc) · 59.2 KB
/
distlink.cpp
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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2018-2020 R.V. Baluev and D.V. Mikryukov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "distlink.h"
#include <cstdlib>
#include <cmath>
#include <limits>
#include <complex>
#include <ctime>
#include <algorithm>
#include <utility>
#define DIM 21 // must be odd greater than DEG
#define DEG 16
using namespace std;
// square of x
template<typename T> inline T sqr(T x) {return x*x;}
template<typename T> inline short sign(T x) {return (x>0) - (x<0);}
// asin with range check
template<typename realfp> inline realfp safe_asin(realfp x) {return asin(max<realfp>(min<realfp>(x,1),-1));}
// sqrt with range check
template<typename realfp> inline realfp safe_sqrt(realfp x) {return sqrt(max<realfp>(x,0));}
// acosh with range check
template<typename realfp> inline realfp safe_acosh(realfp x) {return acosh(max<realfp>(x,1));}
template<typename realfp> inline realfp atan2h(realfp y, realfp x) {return (log(fabs(x+y))-log(fabs(x-y)))/2;} // real part
template<typename realfp> inline realfp atan2_smart(realfp y, realfp x, bool trig) {return trig ? atan2(y,x) : atan2h(y,x);}
template<typename realfp>
inline void cross_product(const realfp a[3], const realfp b[3], realfp res[3])
{
res[0] = a[1]*b[2]-a[2]*b[1];
res[1] = a[2]*b[0]-a[0]*b[2];
res[2] = a[0]*b[1]-a[1]*b[0];
}
template<typename realfp>
inline realfp dot_product(const realfp a[3], const realfp b[3]) {return a[0]*b[0]+a[1]*b[1]+a[2]*b[2];}
template<typename realfp>
inline realfp norm(const realfp a[3]) {return dot_product(a,a);}
//Some extra definitions for the template type "complex" from <complex>
template<typename T> inline complex<T> operator+(const complex<T>& x, int y) {return x+static_cast<T>(y);}
template<typename T> inline complex<T> operator+(int x, const complex<T>& y) {return static_cast<T>(x)+y;}
template<typename T> inline complex<T> operator-(const complex<T>& x, int y) {return x-static_cast<T>(y);}
template<typename T> inline complex<T> operator-(int x, const complex<T>& y) {return static_cast<T>(x)-y;}
template<typename T> inline complex<T> operator*(const complex<T>& x, int y) {return x*static_cast<T>(y);}
template<typename T> inline complex<T> operator*(int x, const complex<T>& y) {return static_cast<T>(x)*y;}
template<typename T> inline complex<T> operator/(const complex<T>& x, int y) {return x/static_cast<T>(y);}
template<typename T> inline complex<T> operator/(int x, const complex<T>& y) {return static_cast<T>(x)/y;}
template<typename T> inline complex<T> inverse(const complex<T>& x) {return conj(x)/norm(x);}
template<typename T>
inline T intpow(const T& x, int n)
{
if(n==0) return 1;
if(n==1) return x;
if(n<0) return intpow(T(1)/x,-n);
if(n>1) return n%2==0 ? sqr(intpow(x,n/2)) : x*intpow(x,n-1);
}
template<typename realfp>
inline realfp pi()
{
static const realfp _pi = acos(static_cast<realfp>(-1));
return _pi;
}
template<typename realfp>
inline realfp angle_wrap(realfp x)
{
static const realfp circ = 2*pi<realfp>();
x = fmod(x+pi<realfp>(),circ);
return x<0 ? x+pi<realfp>() : x-pi<realfp>();
}
//This class is for initial setup.
template<typename realfp, unsigned short dimension>
class CExps
{
public:
complex<realfp> expvals[dimension];
CExps(){
for(int i=0; i<dimension; i++)
expvals[i] = polar<realfp>(1,2*i*pi<realfp>()/dimension);}
};
template<int dimension>
class CInitializer
{
public:
CExps<float,dimension> expsf;
CExps<double,dimension> expsd;
CExps<long double,dimension> expsld;
CInitializer() {srand(time(0));}
};
const CInitializer<DIM> Init;
template<typename realfp> inline const complex<realfp>* exps();
template<> inline const complex<float>* exps<float>() {return Init.expsf.expvals;}
template<> inline const complex<double>* exps<double>() {return Init.expsd.expvals;}
template<> inline const complex<long double>* exps<long double>() {return Init.expsld.expvals;}
template<typename realfp> inline realfp ierr() {return numeric_limits<realfp>::epsilon();}
template<typename realfp> inline realfp random() {return static_cast<realfp>(rand())/RAND_MAX;}
template<typename realfp>
void detect_suitable_options(realfp& max_root_error,
realfp& min_root_error,
realfp& max_anom_error)
{
max_root_error = sqrt(ierr<realfp>());
min_root_error = ierr<realfp>()*2;
max_anom_error = ierr<realfp>()*1000;
}
// Implementation of COrbitData.
template<typename realfp>
COrbitData<realfp>::COrbitData(): a(1.), e(0.), i(0.), w(0.), Om(0.) {
P[0]=1.; P[1]=0.; P[2]=0.;
Q[0]=0.; Q[1]=1.; Q[2]=0.;}
template<typename realfp>
COrbitData<realfp>::COrbitData(realfp a_, realfp e_, realfp i_, realfp w_, realfp Om_):
a(a_), e(e_), i(i_), w(w_), Om(Om_) {set_vectors();}
template<typename realfp>
template<typename T>
COrbitData<realfp>::COrbitData(const COrbitData<T>& other):
a(other.a), e(other.e), i(other.i), w(other.w), Om(other.Om) {set_vectors();}
//Components of P and Q vectors.
template<typename realfp>
void COrbitData<realfp>::set_vectors()
{
P[0]= cos(w)*cos(Om)-cos(i)*sin(w)*sin(Om);
P[1]= cos(w)*sin(Om)+cos(i)*sin(w)*cos(Om);
P[2]= sin(i)*sin(w);
Q[0]=-sin(w)*cos(Om)-cos(i)*cos(w)*sin(Om);
Q[1]= cos(i)*cos(w)*cos(Om)-sin(w)*sin(Om);
Q[2]= sin(i)*cos(w);
}
//This function evaluates the value of the algebraic polynomial.
template<typename realfp>
complex<realfp> polynomial_val(int n, const complex<realfp> c[], const complex<realfp>& z, bool forward=true)
{
complex<realfp> R;
if(forward)
{
R=c[n];
for(int i=n-1; i>=0; i--)
R=R*z+c[i];
}
else
{
R=c[0];
for(int i=1; i<=n; i++)
R=R*z+c[i];
}
return R;
}
//This function evaluates simultaneously the value of the algebraic polynomial and of its derivative.
template<typename realfp>
void polynomial_valder(int n, const complex<realfp> c[], const complex<realfp>& z,
complex<realfp>& val, complex<realfp>& der, bool forward)
{
if(forward)
{
val=c[n];
der=c[n]*n;
for(int i=n-1;i>1;i--)
{
val=val*z+c[i];
der=der*z+c[i]*i;
}
if(n>1)
{
val=val*z+c[1];
der=der*z+c[1];
}
if(n>0)
val=val*z+c[0];
}
else
{
val=c[0];
der=c[0]*n;
for(int i=1;i<n-1;i++)
{
val=val*z+c[i];
der=der*z+c[i]*(n-i);
}
if(n>1)
{
val=val*z+c[n-1];
der=der*z+c[n-1];
}
if(n>0)
val=val*z+c[n];
}
}
//This function evaluates simultaneously the value of the algebraic polynomial and of its first and second derivatives.
template<typename realfp>
void polynomial_valder(int n, const complex<realfp> c[], const complex<realfp>& z,
complex<realfp>& val, complex<realfp>& der, complex<realfp>& der2, bool forward)
{
if(forward)
{
val =c[n];
der =c[n]*n;
der2=c[n]*(n*(n-1));
for(int i=n-1; i>2; i--)
{
val =val*z+c[i];
der =der*z+c[i]*i;
der2=der2*z+c[i]*(i*(i-1));
}
if(n>2)
{
val=val*z+c[2];
der=der*z+c[2]*2;
der2=der2*z+c[2]*2;
}
if(n>1)
{
val=val*z+c[1];
der=der*z+c[1];
}
if(n>0)
val=val*z+c[0];
}
else
{
val =c[0];
der =c[0]*n;
der2=c[0]*(n*(n-1));
for(int i=1; i<n-2; i++)
{
val =val*z+c[i];
der =der*z+c[i]*(n-i);
der2=der2*z+c[i]*((n-i)*(n-i-1));
}
if(n>2)
{
val=val*z+c[n-2];
der=der*z+c[n-2]*2;
der2=der2*z+c[n-2]*2;
}
if(n>1)
{
val=val*z+c[n-1];
der=der*z+c[n-1];
}
if(n>0)
val=val*z+c[n];
}
}
template<typename realfp>
inline complex<realfp> ratio1(int n, const complex<realfp> c[], const complex<realfp>& z, bool forward)
{
complex<realfp> f, fd;
polynomial_valder(n, c, z, f, fd, forward);
return f/fd;
}
template<typename realfp>
inline complex<realfp> ratio2(const complex<realfp> c[4], const complex<realfp>& z, bool forward)
{
return forward ? ((c[2]*z+c[1])*z+c[0])/(2*c[2]*z+c[1]) :
((c[0]*z+c[1])*z+c[2])/(2*c[0]*z+c[1]);
}
template<typename realfp>
inline complex<realfp> ratio4(const complex<realfp> c[4], const complex<realfp>& z, bool forward)
{
return forward ? ((((c[4]*z+c[3])*z+c[2])*z+c[1])*z+c[0])/(((4*c[4]*z+3*c[3])*z+2*c[2])*z+c[1]) :
((((c[0]*z+c[1])*z+c[2])*z+c[3])*z+c[4])/(((4*c[0]*z+3*c[1])*z+2*c[2])*z+c[3]);
}
/*
This function evaluates one complex root of the algebraic polynomial with complex coefficients.
Input:
n - degree of the polynomial;
c - array of complex coefficients;
z - initial approximation of the root;
maxeps - maximum relative error allowed for determination of the root;
mineps - desirable relative error of the root; it may be set to zero;
maxcount- maximum number of iterations in a single run;
restartcount - maximum number of runs.
Output:
z - final approximation of the root.
Return value: the full number of iterations.
*/
template<typename realfp>
unsigned long Newton(int n, complex<realfp> c[], complex<realfp> &z, realfp maxeps, realfp mineps,
unsigned long maxcount, unsigned long restartcount)
{
complex<realfp> dz;
maxeps=fabs(maxeps);
mineps=fabs(mineps);
complex<realfp> z1=z,z2=z1;
//correcting inadequate values of the least precision required
if(!(maxeps>=2*mineps)) maxeps=2*mineps;
if(!(maxeps>=2*ierr<realfp>())) maxeps=2*ierr<realfp>();
unsigned long iter_cnt=0;
realfp err2=0,nrm;
bool inner=true;
do
{
iter_cnt++;
//if the Newtonian algorithm hanged in maxcount steps,
//change the starting approximation and restart it:
const bool cycled = (iter_cnt>2 && norm(z-z2)<norm(z-z1)*1e-6) || iter_cnt%maxcount == 0;
complex<realfp> rand_mlt;
if(cycled) rand_mlt = polar<realfp>((2*random<realfp>()+1)/3,random<realfp>()*2*pi<realfp>());
nrm=norm(z);
if(inner) {if(nrm>5) inner=false;}
else {if(2*nrm<1) inner=true;}
z2=z1;z1=z;
if(inner)
{
dz=ratio1(n,c,z,true);
z-=cycled?dz*rand_mlt:dz;
}
else
{
nrm=1/nrm;
dz=ratio1(n,c,conj(z)*nrm,false); // actually, dw
z/=(1-z*(cycled?dz*rand_mlt:dz));
}
err2=norm(dz);
}
//require relative precision of at least maxeps...
while(err2>sqr(maxeps)*nrm && iter_cnt<maxcount*restartcount);
if(err2>sqr(mineps)*nrm)
{
unsigned long _iter_cnt=0;
realfp derr2;
do
{
nrm=norm(z);
if(inner)
{
dz=ratio1(n,c,z,true);
z-=dz;
}
else
{
nrm=1/nrm;
dz=ratio1(n,c,conj(z)*nrm,false);
z/=(1-z*dz);
}
const realfp err2_=norm(dz);
derr2=err2-err2_;
err2=err2_;
_iter_cnt++;
}
// ...and even more, until the desirable or the maximum possible precision is reached
while(derr2>ierr<realfp>()*err2 && err2>sqr(mineps)*nrm && _iter_cnt<=maxcount);
iter_cnt += _iter_cnt;
}
return iter_cnt;
}
/*
This function uses Ruffini-Horner algorithm for decrementing the degree of the algebraic
polynomial (if one complex root was obtained before calling this function).
For numerical stability, it extracts:
factor (z-root) out of P(z) = sum_{k=0..n}{c_k z^k} if |root|<1
factor (1/z-1/root) out of Q(1/z) = sum_{k=0..n}{c_{n-k}*(1/z)^k} otherwise.
In the first case, it does not spend time for shifting the array of coefficients
according to c[k]:=c[k+1]. The new set of coefficients starts from c[1] and ends by
c[n-1]. The element c[0] should be disregarded.
In the second case, the new set of coefficients starts from c[0] and ends by c[n-2]. The
element c[n-1] should be disregarded.
Input:
n - degree of the polynomial;
c - array of complex coefficients of the polynomial;
root - the root of the polynomial.
Output:
c - array of the complex coefficients of the new polynomial.
Return value: pointer to the first coefficient of the new polynomial.
*/
template<typename realfp>
inline complex<realfp>* extract_linear_factor(int n, complex<realfp> c[], const complex<realfp> &root)
{
if(norm(root)<=1)
{
for(int i=n-1;i>0;i--) c[i]+=c[i+1]*root;
return c+1;
}
else
{
const complex<realfp> iroot=inverse(root);
for(int i=1; i<n; i++) c[i]+=c[i-1]*iroot;
return c;
}
}
/*
This function solves the quadratic polynomial equation
*/
template<typename realfp>
unsigned int solve_quadratic(const complex<realfp> c[3], complex<realfp> x[2])
{
const complex<realfp> D=sqrt(c[1]*c[1]-4*c[2]*c[0]);
const complex<realfp> tmp=(real(c[1])*real(D)+imag(c[1])*imag(D)>=0 ? -c[1]-D : -c[1]+D);
x[0] = tmp/(c[2]*2);
x[1] = (c[0]*2)/tmp;
if(norm(x[0])<=1) x[0]-=ratio2(c,x[0],true);
else x[0]/=(1-x[0]*ratio2(c,inverse(x[0]),false));
if(norm(x[1])<=1) x[1]-=ratio2(c,x[1],true);
else x[1]/=(1-x[1]*ratio2(c,inverse(x[1]),false));
return 2;
}
/*
This function solves the quartic polynomial equation
*/
template<typename realfp>
unsigned int solve_quartic(const complex<realfp> c[5], complex<realfp> x[4])
{
const complex<realfp> r = 4*c[4];
const complex<realfp> p = 2*r*c[2]-3*sqr(c[3]);
const complex<realfp> q = 2*c[3]*(sqr(c[3])-r*c[2])+sqr(r)*c[1];
const complex<realfp> D0 = sqr(c[2])-3*c[3]*c[1]+3*c[0]*r;
const complex<realfp> D1 = 2*c[2]*sqr(c[2])-9*c[3]*c[2]*c[1]+27*sqr(c[3])*c[0]+27*sqr(c[1])*c[4]-18*r*c[2]*c[0];
complex<realfp> tmp= sqrt(sqr(D1)-4*D0*sqr(D0));
const complex<realfp> Qc = (real(D1)*real(tmp)+imag(D1)*imag(tmp)>=0 ? D1+tmp : D1-tmp)/2;
const realfp Qabs=cbrt(abs(Qc));
const realfp Qarg=arg(Qc)/3;
const complex<realfp> Q1=polar<realfp>(Qabs,Qarg);
static const realfp twopi3 = 2*pi<realfp>()/3;
const complex<realfp> Q2=polar<realfp>(Qabs,Qarg+twopi3);
const complex<realfp> Q3=polar<realfp>(Qabs,Qarg-twopi3);
const complex<realfp> vals[3] = {(Q1+D0/Q1)*r-p, (Q2+D0/Q2)*r-p, (Q3+D0/Q3)*r-p};
const realfp nrms[3] = {norm(vals[0]), norm(vals[1]), norm(vals[2])};
tmp=sqrt(vals[nrms[0]>max(nrms[1],nrms[2])?0:(nrms[1]>nrms[2]?1:2)]/3);
const complex<realfp> S = (real(c[4])>=0 ? tmp : -tmp);
tmp = sqrt( q/S-sqr(S)-p);
x[0] = (-c[3]-S+tmp)/r;
x[1] = (-c[3]-S-tmp)/r;
tmp = sqrt(-q/S-sqr(S)-p);
x[2] = (-c[3]+S+tmp)/r;
x[3] = (-c[3]+S-tmp)/r;
if(norm(x[0])<=1) x[0]-=ratio4(c,x[0],true);
else x[0]/=(1-x[0]*ratio4(c,inverse(x[0]),false));
if(norm(x[1])<=1) x[1]-=ratio4(c,x[1],true);
else x[1]/=(1-x[1]*ratio4(c,inverse(x[1]),false));
if(norm(x[2])<=1) x[2]-=ratio4(c,x[2],true);
else x[2]/=(1-x[2]*ratio4(c,inverse(x[2]),false));
if(norm(x[3])<=1) x[3]-=ratio4(c,x[3],true);
else x[3]/=(1-x[3]*ratio4(c,inverse(x[3]),false));
return 4;
}
/*
This function evaluates all roots of algebraic polinomials of degree n.
P(z) = sum_{k=0..n}{c_k*z^k} = 0 <===> Q(1/z) = sum_{k=0..n}{c_{n-k}*(1/z)^k} = 0.
The algebraic polynomial is assumed to be originated from a trigonometric polynomial
with real coefficients of degree n/2.
Input:
n - degree of the algebraic polynomial;
c - array of complex coefficients of the polynomial;
minerr - desirable precision of the roots;
maxerr - maximum error of the roots allowed.
trigonometric - boolean indicator
Output:
roots - array of the roots.
*/
template<typename realfp>
unsigned long polynomial_roots(int n, complex<realfp> c[], complex<realfp> roots[],
realfp minerr, realfp maxerr, bool trigonometric)
{
complex<realfp>* c_ = c; // pointer to the first coefficient
int cnt = 0;
unsigned long iter_cnt = 0;
//extracting roots until a square polynomial is obtained
for(int i=0, degree=n; degree>4; i++)
{
iter_cnt += Newton(degree, c_, roots[i], maxerr, minerr, 300, 10); //Newtonian root search
c_ = extract_linear_factor(degree, c_, roots[i]); //extracting the root
degree--;
if(degree>4 && roots[i+1]==complex<realfp>(0,0))
if(trigonometric)
//if z is a root of an algebraic polynomial
//produced by a trigonometric polynomial with real coefficients
//then 1/z^* is also its root;
//therefore, the starting approximation for the next root is 1/z^*
roots[i+1]=roots[i]/norm(roots[i]);
else
//if z is a root of an algebraic polynomial with real coefficients
//then z^* is also its root;
//therefore, the starting approximation for the next root is z^*
if(fabs(imag(roots[i]))>fabs(real(roots[i]))*1e-3) // additionally check that it is not a real-valued root
roots[i+1]=conj(roots[i]);
else roots[i+1]=complex<realfp>(imag(roots[i]),real(roots[i]));
}
//solving the final square equation
iter_cnt += solve_quartic(c_,roots+n-4);
return iter_cnt;
}
/*
This function evaluates the uncertainty of the root of the algebraic polynomial.
Input:
n - degree of the polynomial;
c - array of the complex coefficients of the polynomial;
cerr - estimated error of the coefficients of the polynomial;
z - value of the root.
Return value:
relative uncertainty of the root.
*/
template<typename realfp>
realfp root_error(int n, const complex<realfp> c[], realfp cerr, const complex<realfp>& z)
{
realfp m=norm(z);
const bool forward = m<=1;
if(!forward) m=1/m;
//evaluating expected error of calculating the value of the polynomial:
realfp ferr2=1;
for(int i=1; i<=n; i++)
{ferr2*=m;ferr2++;}
ferr2*=sqr(cerr);
//using quadratic approximation to estimate the uncertainty of the root:
complex<realfp> f, fd, fd2;
polynomial_valder(n, c, forward ? z : inverse(z), f, fd, fd2, forward);
const complex<realfp> D=sqrt(fd*fd-2*f*fd2);
const realfp d2=4*norm(f)/max(norm(fd+D),norm(fd-D));
return sqrt((d2+ferr2/norm(D))/m);
}
/*
This function evaluates the maximum root bound.
Input:
n - degree of the polynomial;
c - array of the complex coefficients of the polynomial;
Return value:
maximum bound on |z_k|.
*/
template<typename realfp>
realfp max_root_bound(int n, const complex<realfp> c[], bool forward)
{
const realfp nch=norm(c[forward?n:0]);
if(n<1) return numeric_limits<realfp>::infinity();
realfp res=norm(c[forward?n-1:1])/nch;
for(int i=2; i<=n; i++)
{
const realfp tmp=pow(norm(c[forward?n-i:i])/nch,static_cast<realfp>(1)/i);
if(res<tmp) res=tmp;
}
return sqrt(res);
}
//This structure contains necessary pre-calculated data for a pair of orbits.
template<typename realfp>
struct SAuxData
{
//these data are for calculating MOID
realfp e1, e2, a1, a2;
realfp alpha1, alpha2;
realfp K;
realfp Pp, Ps, Sp, Ss;
//these data are for calculating linking coefficients
realfp p1, p2, w1, w2, I, abs_w;
realfp P1w, P2w, Q1w, Q2w;
const realfp* P1;
const realfp* P2;
const realfp* Q1;
const realfp* Q2;
SAuxData(const COrbitData<realfp>& O1, const COrbitData<realfp>& O2)
{
e1 = O1.get_e(); e2 = O2.get_e();
a1 = O1.get_a(); a2 = O2.get_a();
w1 = O1.get_w(); w2 = O2.get_w();
if(e1<0) {e1=-e1;w1=w1+pi<realfp>();}
if(e2<0) {e2=-e2;w2=w2+pi<realfp>();}
a1 = fabs(a1); a2 = fabs(a2);
if(!(e1<=1)) a1=-a1;
if(!(e2<=1)) a2=-a2;
p1 = a1*(1-e1*e1); p2 = a2*(1-e2*e2);
alpha1 = a1/a2; alpha2 = a2/a1;
K = alpha2*e2*e2;
const realfp eta1 = sqrt(fabs(1-e1*e1)); const realfp eta2 = sqrt(fabs(1-e2*e2));
const realfp i1 = O1.get_i(); const realfp i2 = O2.get_i();
const realfp Om1 = O1.get_Om(); const realfp Om2 = O2.get_Om();
const realfp c1=cos(i1); const realfp s1=sin(i1);
const realfp c2=cos(i2); const realfp s2=sin(i2);
const realfp w[3] = {c1*s2*cos(Om2)-s1*c2*cos(Om1),
c1*s2*sin(Om2)-s1*c2*sin(Om1),
s1*s2*sin(Om2-Om1)};
abs_w = sqrt(norm(w));
const realfp cosI = c1*c2+s1*s2*cos(Om2-Om1);
I = cosI>0 ? safe_asin(abs_w) : pi<realfp>()-safe_asin(abs_w); //radians
//second small letter in Pp, Ps, Sp, Ss refers to the orbit O2
P1=O1.vectorP(); P2=O2.vectorP();
Q1=O1.vectorQ(); Q2=O2.vectorQ();
Pp = dot_product(P1,P2);
Ps = dot_product(P1,Q2)*eta2;
Sp = dot_product(Q1,P2)*eta1;
Ss = dot_product(Q1,Q2)*eta1*eta2;
P1w = dot_product(P1,w); P2w = dot_product(P2,w);
Q1w = dot_product(Q1,w); Q2w = dot_product(Q2,w);
// P1w = cos(i1)*sin(i2)*cos(w1)*cos(Om1-Om2)-sin(i1)*cos(i2)*cos(w1)+sin(i2)*sin(w1)*sin(Om2-Om1);
// P2w =-cos(i2)*sin(i1)*cos(w2)*cos(Om1-Om2)+sin(i2)*cos(i1)*cos(w2)+sin(i1)*sin(w2)*sin(Om2-Om1);
// Q1w =-cos(i1)*sin(i2)*sin(w1)*cos(Om1-Om2)+sin(i1)*cos(i2)*sin(w1)+sin(i2)*cos(w1)*sin(Om2-Om1);
// Q2w = cos(i2)*sin(i1)*sin(w2)*cos(Om1-Om2)-sin(i2)*cos(i1)*sin(w2)+sin(i1)*cos(w2)*sin(Om2-Om1);
}
};
//This is the implementation of the function g(u). It is not used anywhere.
template<typename realfp>
realfp func_g_ee(realfp u, const SAuxData<realfp>& data)
{
const realfp x = cos(u);
const realfp y = sin(u);
const realfp A = data.Ps*y-data.Ss*x;
const realfp B = data.Pp*y-data.Sp*x;
const realfp C = data.e2*B-data.alpha1*data.e1*y*(1-data.e1*x);
const realfp M = data.Sp*y+data.Pp*(x-data.e1)+data.alpha2*data.e2;
const realfp N =-data.Ss*y-data.Ps*(x-data.e1);
const realfp C2 = C*C;
const realfp A2 = A*A;
const realfp B2 = B*B;
const realfp Ac = A2-C2;
const realfp Bc = B2-C2;
return data.K*data.K*Ac*Bc+2*data.K*C*(N*A*Ac+M*B*Bc)-(A2+B2)*(N*N*Ac+M*M*Bc-2*N*M*A*B);
}
template<typename realfp>
realfp func_g_he(realfp u, const SAuxData<realfp>& data)
{
const realfp eu= exp(u);
const realfp x = (eu+1/eu)/2;
const realfp y = (eu-1/eu)/2;
const realfp A = data.Ps*y-data.Ss*x;
const realfp B = data.Pp*y-data.Sp*x;
const realfp C = data.e2*B-data.alpha1*data.e1*y*(1-data.e1*x);
const realfp M =-data.Sp*y+data.Pp*(x-data.e1)+data.alpha2*data.e2;
const realfp N = data.Ss*y-data.Ps*(x-data.e1);
const realfp C2 = C*C;
const realfp A2 = A*A;
const realfp B2 = B*B;
const realfp Ac = A2-C2;
const realfp Bc = B2-C2;
return data.K*data.K*Ac*Bc+2*data.K*C*(N*A*Ac+M*B*Bc)-(A2+B2)*(N*N*Ac+M*M*Bc-2*N*M*A*B);
}
template<typename realfp>
realfp func_g_eh(realfp u, const SAuxData<realfp>& data)
{
const realfp x = cos(u);
const realfp y = sin(u);
const realfp A = data.Ps*y-data.Ss*x;
const realfp B = data.Pp*y-data.Sp*x;
const realfp C = data.e2*B-data.alpha1*data.e1*y*(1-data.e1*x);
const realfp M = data.Sp*y+data.Pp*(x-data.e1)+data.alpha2*data.e2;
const realfp N =-data.Ss*y-data.Ps*(x-data.e1);
const realfp C2 = C*C;
const realfp A2 =-A*A;
const realfp B2 = B*B;
const realfp Ac = A2-C2;
const realfp Bc = B2-C2;
return data.K*data.K*Ac*Bc+2*data.K*C*(-N*A*Ac+M*B*Bc)-(A2+B2)*(-N*N*Ac+M*M*Bc+2*N*M*A*B);
}
template<typename realfp>
realfp func_g_hh(realfp u, const SAuxData<realfp>& data)
{
const realfp eu= exp(u);
const realfp x = (eu+1/eu)/2;
const realfp y = (eu-1/eu)/2;
const realfp A = data.Ps*y-data.Ss*x;
const realfp B = data.Pp*y-data.Sp*x;
const realfp C = data.e2*B-data.alpha1*data.e1*y*(1-data.e1*x);
const realfp M =-data.Sp*y+data.Pp*(x-data.e1)+data.alpha2*data.e2;
const realfp N = data.Ss*y-data.Ps*(x-data.e1);
const realfp C2 = C*C;
const realfp A2 =-A*A;
const realfp B2 = B*B;
const realfp Ac = A2-C2;
const realfp Bc = B2-C2;
return data.K*data.K*Ac*Bc+2*data.K*C*(-N*A*Ac+M*B*Bc)-(A2+B2)*(-N*N*Ac+M*M*Bc+2*N*M*A*B);
}
//This is a fast algorithm for evaluating g(u) at u = 2*M_PI*j/N. This algorithm does not
//use calls of trigonometric functions, because these data were pre-calculated.
template<typename realfp>
realfp func_g_ee_fast(int j, const SAuxData<realfp>& data)
{
const realfp x = real(exps<realfp>()[j]); //cos(u)
const realfp y = imag(exps<realfp>()[j]); //sin(u)
const realfp A = data.Ps*y - data.Ss*x;
const realfp B = data.Pp*y - data.Sp*x;
const realfp C = data.e2*B - data.alpha1*data.e1*y*(1-data.e1*x);
const realfp x_= x - data.e1;
const realfp M = data.Sp*y + data.Pp*x_+data.alpha2*data.e2;
const realfp N =-data.Ss*y - data.Ps*x_;
const realfp C2 = C*C;
const realfp A2 = A*A;
const realfp B2 = B*B;
const realfp Ac = A2-C2;
const realfp Bc = B2-C2;
return data.K*data.K*(Ac*Bc) + 2*data.K*C*(N*A*Ac+M*B*Bc) - (A2+B2)*(N*N*Ac+M*M*Bc-2*N*M*A*B);
}
// computes complex-valued g for imaginary hyperbolic argument
template<typename realfp>
complex<realfp> func_g_he_fast(int j, const SAuxData<realfp>& data)
{
const realfp x = real(exps<realfp>()[j]); //cos(u)
const realfp y = imag(exps<realfp>()[j]); //sin(u)
const complex<realfp> A(data.Ps*y, -data.Ss*x);
const complex<realfp> B(data.Pp*y, -data.Sp*x);
const complex<realfp> C = data.e2*B - data.alpha1*data.e1*y*(1-data.e1*x);
const realfp x_= x - data.e1;
const complex<realfp> M( data.Pp*x_+data.alpha2*data.e2, data.Sp*y);
const complex<realfp> N(-data.Ps*x_, -data.Ss*y);
const complex<realfp> C2 = C*C;
const complex<realfp> A2 = A*A;
const complex<realfp> B2 = B*B;
const complex<realfp> Ac = A2-C2;
const complex<realfp> Bc = B2-C2;
return data.K*data.K*(Ac*Bc) + 2*data.K*C*(N*A*Ac+M*B*Bc) - (A2+B2)*(N*N*Ac+M*M*Bc-2*N*M*A*B);
}
template<typename realfp>
realfp func_g_eh_fast(int j, const SAuxData<realfp>& data)
{
const realfp x = real(exps<realfp>()[j]); //cos(u)
const realfp y = imag(exps<realfp>()[j]); //sin(u)
const realfp A = data.Ps*y - data.Ss*x;
const realfp B = data.Pp*y - data.Sp*x;
const realfp C = data.e2*B - data.alpha1*data.e1*y*(1-data.e1*x);
const realfp x_= x - data.e1;
const realfp M = data.Sp*y + data.Pp*x_+data.alpha2*data.e2;
const realfp N =-data.Ss*y - data.Ps*x_;
const realfp C2 = C*C;
const realfp A2 =-A*A;
const realfp B2 = B*B;
const realfp Ac = A2-C2;
const realfp Bc = B2-C2;
return data.K*data.K*(Ac*Bc) + 2*data.K*C*(-N*A*Ac+M*B*Bc) - (A2+B2)*(-N*N*Ac+M*M*Bc+2*N*M*A*B);
}
// computes complex-valued g for imaginary hyperbolic argument
template<typename realfp>
complex<realfp> func_g_hh_fast(int j, const SAuxData<realfp>& data)
{
const realfp x = real(exps<realfp>()[j]); //cos(u)
const realfp y = imag(exps<realfp>()[j]); //sin(u)
const complex<realfp> A(data.Ps*y, -data.Ss*x);
const complex<realfp> B(data.Pp*y, -data.Sp*x);
const complex<realfp> C = data.e2*B - data.alpha1*data.e1*y*(1-data.e1*x);
const realfp x_= x - data.e1;
const complex<realfp> M( data.Pp*x_+data.alpha2*data.e2, data.Sp*y);
const complex<realfp> N(-data.Ps*x_, -data.Ss*y);
const complex<realfp> C2 = C*C;
const complex<realfp> A2 =-A*A;
const complex<realfp> B2 = B*B;
const complex<realfp> Ac = A2-C2;
const complex<realfp> Bc = B2-C2;
return data.K*data.K*(Ac*Bc) + 2*data.K*C*(-N*A*Ac+M*B*Bc) - (A2+B2)*(-N*N*Ac+M*M*Bc+2*N*M*A*B);
}
/*
This function evaluates the complex coefficients of the algebraic polynomial
using Fourier transform of the function g(u).
Input:
data - structure containing pre-calculated data.
Output:
c - array of complex coefficients of the polynomial.
Return value: uncertainty of the coefficients.
*/
template<typename realfp>
realfp create_polynomial(const SAuxData<realfp>& data, complex<realfp> c[DEG+1], realfp (*func_g_fast)(int, const SAuxData<realfp>&))
{
if(!(data.e1<=1)) return -1;
//direct evaluating the highest coefficients
const complex<realfp> hc = sqr(data.alpha1*sqr(data.e1)/16)*( data.e2<=1 ?
complex<realfp>(data.Pp-data.Ss-data.e1*data.e2, data.Sp+data.Ps)*
complex<realfp>(data.Pp-data.Ss+data.e1*data.e2, data.Sp+data.Ps)*
complex<realfp>(data.Pp+data.Ss-data.e1*data.e2, data.Sp-data.Ps)*
complex<realfp>(data.Pp+data.Ss+data.e1*data.e2, data.Sp-data.Ps) :
complex<realfp>(data.Pp-data.Ps-data.e1*data.e2, data.Sp-data.Ss)*
complex<realfp>(data.Pp-data.Ps+data.e1*data.e2, data.Sp-data.Ss)*
complex<realfp>(data.Pp+data.Ps-data.e1*data.e2, data.Sp+data.Ss)*
complex<realfp>(data.Pp+data.Ps+data.e1*data.e2, data.Sp+data.Ss) );
int i, j;
realfp vals[DIM];
for(j=0; j<DIM; j++)
vals[j] = func_g_fast(j, data);
for(j=1; j<=DIM/2; j++)
{
const realfp sum = vals[j]+vals[DIM-j];
const realfp dif = vals[j]-vals[DIM-j];
vals[j]=sum; vals[DIM-j]=dif;
}
realfp a=vals[0], b;
for(j=1; j<=DIM/2; j++) a+=vals[j];
c[DEG/2]=a/DIM;
for(i=1; i<DEG/2; i++)
{
a=vals[0]; b=0;
for(j=1; j<=DIM/2; j++)
{
a+=vals[j]*real(exps<realfp>()[(j*i)%DIM]);
b+=vals[DIM-j]*imag(exps<realfp>()[(j*i)%DIM]);
}
a/=DIM; b/=DIM;
c[DEG/2-i]=complex<realfp>(a, b);
c[DEG/2+i]=complex<realfp>(a,-b);
}
c[0]=hc; c[DEG]=conj(hc);
realfp cerr=0;
a=vals[0]; b=0;
for(j=1; j<=DIM/2; j++)
{
a+=vals[j]*real(exps<realfp>()[(j*i)%DIM]);
b+=vals[DIM-j]*imag(exps<realfp>()[(j*i)%DIM]);
}
a/=DIM; b/=DIM;
cerr+=norm(complex<realfp>(a, b)-hc);
for(i++; i<=DIM/2; i++)
{
a=vals[0]; b=0;
for(j=1; j<=DIM/2; j++)
{
a+=vals[j]*real(exps<realfp>()[(j*i)%DIM]);
b+=vals[DIM-j]*imag(exps<realfp>()[(j*i)%DIM]);
}
a/=DIM; b/=DIM;
cerr+=(a*a+b*b);
}
cerr/=((DIM-DEG+1)/2);
return max(sqrt(cerr),ierr<realfp>()*abs(hc));
}
// this is hyperbolic version for real-valued coefficients
template<typename realfp>
realfp create_polynomial(const SAuxData<realfp>& data, complex<realfp> c[DEG+1], complex<realfp> (*func_g_fast)(int, const SAuxData<realfp>&))
{
if(!(data.e1>1)) return -1;
//direct evaluating the highest coefficients
const realfp d=sqr(data.e2); // from 0 to 1
const realfp d1p=sqr((data.Pp+data.Sp)/data.e1);
const realfp d2p=sqr((data.Ps+data.Ss)/data.e1);
const realfp dpp=d1p+d2p;
const realfp dpm=d1p-d2p;
const realfp h = sqr(data.alpha1*sqr(sqr(data.e1))/16)*( data.e2<=1 ?
(dpp*dpp+d*d-2*d*dpm) :
(dpm*dpm+d*d-2*d*dpp) );
const realfp d1m=sqr((data.Pp-data.Sp)/data.e1);
const realfp d2m=sqr((data.Ps-data.Ss)/data.e1);
const realfp dmp=d1m+d2m;
const realfp dmm=d1m-d2m;
const realfp hc = sqr(data.alpha1*sqr(sqr(data.e1))/16)*( data.e2<=1 ?
(dmp*dmp+d*d-2*d*dmm) :
(dmm*dmm+d*d-2*d*dmp) );
int i, j;
complex<realfp> vals[DIM/2+1];
const realfp vals0=real(func_g_fast(0, data)); // the only real value is g(0)
// second half of the array is complex-conjugate of the first one taken in reverse order
for(j=1; j<=DIM/2; j++) vals[j] = func_g_fast(j, data);
realfp a=0,b;
for(j=1; j<=DIM/2; j++) a+=real(vals[j]);
c[DEG/2]=(2*a+vals0)/DIM;
for(i=1; i<DEG/2; i++)
{
a=0; b=0;
for(j=1; j<=DIM/2; j++)
{
a+=real(vals[j])*real(exps<realfp>()[(j*i)%DIM]);
b+=imag(vals[j])*imag(exps<realfp>()[(j*i)%DIM]);
}
c[DEG/2-i]=(2*(a-b)+vals0)/DIM;
c[DEG/2+i]=(2*(a+b)+vals0)/DIM;
}
c[0]=hc; c[DEG]=h;
a=0; b=0;
for(j=1; j<=DIM/2; j++)
{
a+=real(vals[j])*real(exps<realfp>()[(j*i)%DIM]);
b+=imag(vals[j])*imag(exps<realfp>()[(j*i)%DIM]);
}
realfp cerr=sqr((2*(a-b)+vals0)/DIM-hc)+sqr((2*(a+b)+vals0)/DIM-h);
for(i++; i<=DIM/2; i++)
{
a=0; b=0;
for(j=1; j<=DIM/2; j++)
{
a+=real(vals[j])*real(exps<realfp>()[(j*i)%DIM]);
b+=imag(vals[j])*imag(exps<realfp>()[(j*i)%DIM]);
}
cerr+=sqr((2*a+vals0)/DIM)+sqr(2*b/DIM);
}
cerr/=(DIM-DEG+1);
return max(sqrt(cerr),ierr<realfp>()*max(fabs(hc),fabs(h)));
}
/*
This function evaluates the distance between two fixed points of the two orbits.
Input:
data - array of the pre-calculated data;
u1,u2 - eccentric anomalies of the points.
Return value: distance.
*/
/* template<typename realfp>
realfp distance_between(const SAuxData<realfp>& data, realfp u1, realfp u2)
{
const realfp x1 = cos(u1);
const realfp _x1 = x1-data.e1;
const realfp y1 = sin(u1);
const realfp x2 = cos(u2);
const realfp _x2 = x2-data.e2;
const realfp y2 = sin(u2);
const realfp R1 = data.a1*(1-data.e1*x1);
const realfp R2 = data.a2*(1-data.e2*x2);
return safe_sqrt(R1*R1+R2*R2-2*data.a1*data.a2*(data.Pp*_x1*_x2+data.Sp*y1*_x2+data.Ps*y2*_x1+data.Ss*y1*y2));
}*/
/*
Computes the radius-vector in an orbit