-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathivp-mod3.cc
1777 lines (1541 loc) · 45.2 KB
/
ivp-mod3.cc
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
#include "iRRAM.h"
#include <vector>
#include <list>
#include <cassert>
#include "iRRAM/limit_templates.h"
#include <sys/time.h>
#include <cstdarg>
#ifndef METHOD_PICARD
# define METHOD_PICARD 0
#endif
#define dbg(fmt, ...) DEBUG0(2,{fprintf(stderr, (fmt), ##__VA_ARGS__);})
unsigned int max_taylor_coeff=0; // only statistical purpose!
unsigned int last_taylor_coeff=0; // only statistical purpose!
using namespace iRRAM;
using std::vector;
using std::pair;
//********************************************************************************
// Small helper functions
// not yet part of the published version of the iRRAM
//********************************************************************************
//********************************************************************************
// Absolute value of vector in Euclidean space
//********************************************************************************
REAL abs(const std::vector<REAL>& x)
{
unsigned int n=x.size();
REAL sqrsum=0;
for (unsigned i=0;i<n;i++) {
sqrsum += square(x[i]);
}
return sqrt(sqrsum);
}
//********************************************************************************
//********************************************************************************
//********************************************************************************
/****************************************************************************************/
/* summation of Taylor sequences */
/* The constructor needs a sequences of numbers with a radius of convergence */
/* larger(!) than the given value 'radius'. */
/* 'bound' has to be an upper bound for the absolute value of the sum function */
/* on a circle with the given 'radius' value */
/****************************************************************************************/
class FUNCTIONAL_taylor_sum :public FUNCTIONAL_object<REAL,REAL> {
virtual void clear()
{
if (this->release_check())
return;
delete this;
}
/***********************************************************/
/* local data: coefficients (as a function), radius and corresponding bound */
FUNCTION<unsigned int,REAL> _coeff;
REAL _radius;
REAL _bound;
public:
FUNCTIONAL_taylor_sum(
FUNCTION<unsigned int,REAL> coeff,
const REAL& radius,
const REAL& bound
) {
_coeff=coeff;
_radius=radius;
_bound=bound;
}
/****************************************************************************************/
/* Evaluation: */
/* Compute coefficients until the truncation error term is smaller than actual_prec */
/* or until the accumulated error is already of the order of the truncation error */
/* (whichever comes first) */
/****************************************************************************************/
REAL eval(const REAL &x)
{
ITERATION_STACK SAVED_STACK;
ACTUAL_STACK.inlimit+=1;
REAL sum=0;
REAL best=0;
REAL factor=1;
REAL error=_bound*_radius/(_radius-abs(x));
REAL errorfactor=abs(x)/_radius;
DEBUG0(2,{cerr << "FUNCTIONAL_taylor_sum starting with precision "<<ACTUAL_STACK.actual_prec
<< " at ratio "<< errorfactor.vsize.mantissa*pow(2,errorfactor.vsize.exponent)<<"\n";});
sizetype sum_error,trunc_error,best_error;
sizetype_add(best_error,error.vsize,error.error);
int best_index=-1;
for (unsigned int i=0;;i++){
sum= sum + _coeff(i)*factor;
error= error*errorfactor;
sum.geterror(sum_error);
sizetype_add(trunc_error,error.vsize,error.error);
sizetype local_error;
sizetype_add(local_error,trunc_error,sum_error);
if (sizetype_less(local_error,best_error)) {
best=sum;
best_error=local_error;
best.seterror(best_error);
best_index=i;
}
if (trunc_error.exponent<ACTUAL_STACK.actual_prec ||
sizetype_less(trunc_error,sum_error)) {
DEBUG0(2,{cerr << "FUNCTIONAL_taylor_sum: stop at "
<<i<< " with best at "<<best_index<<"\n";});
if (i>max_taylor_coeff) max_taylor_coeff=i; // only statistical purpose!
last_taylor_coeff = i;
break;
}
factor=factor*x;
}
return best;
}
};
/********************************************************/
/* corresponding constructor for FUNCTION */
/********************************************************/
inline FUNCTION<REAL,REAL> taylor_sum (
FUNCTION<unsigned int,REAL> coeff,
const REAL& radius,
const REAL& bound
) {
return new FUNCTIONAL_taylor_sum (coeff,radius,bound);
}
//********************************************************************************
// Class for summation of a vector of Taylor series
// 'bound' must be valid for 'radius' in each dimension
//********************************************************************************
class FUNCTIONAL_vector_taylor_sum :public FUNCTIONAL_object<REAL,std::vector<REAL> > {
virtual void clear()
{
if (this->release_check()) return;
delete this;
}
REAL _radius;
REAL _bound;
FUNCTION<unsigned int,std::vector<REAL> > _coeff;
public:
FUNCTIONAL_vector_taylor_sum(
FUNCTION<unsigned int,std::vector<REAL> > coeff,
const REAL& radius,
const REAL& bound
) {
_coeff=coeff;
_radius=radius;
_bound=bound;
}
std::vector<REAL> eval(const REAL& t)
{
std::vector<REAL> result=_coeff(0); // just to get the dimension
for (unsigned int nu=0;nu<result.size();nu++){
FUNCTION<unsigned int,REAL> coeff_nu=projection(_coeff,nu);
FUNCTION<REAL,REAL> f_nu=taylor_sum(coeff_nu,_radius,_bound);
result[nu]=f_nu(t);
}
return result;
}
};
/********************************************************/
/* corresponding constructor for FUNCTION */
/********************************************************/
inline FUNCTION<REAL,std::vector<REAL> > taylor_sum (
FUNCTION<unsigned int,std::vector<REAL> > coeff,
const REAL& radius,
const REAL& maximum
) {
return new FUNCTIONAL_vector_taylor_sum(coeff,radius,maximum);
}
//********************************************************************************
//********************************************************************************
class FLOW {
public:
//local data:
unsigned int _dimension;
vector< vector<REAL> > _coeff;
REAL _coeff_linear_bound;
REAL _coeff_const_bound;
//information on the state space of the flow
unsigned int dimension(){return _dimension;};
// default constructor, empty system
FLOW()
{
_dimension=0;
_coeff_linear_bound=0;
_coeff_const_bound=0;
}
// trivial constructor, here only for linear systems
FLOW(const vector< vector<REAL> > coeff)
{
cout << "# This is a prototypical implementation of flows!\n";
_dimension=coeff.size();
_coeff=coeff;
for (unsigned int nu=0;nu<_dimension;nu++)
for (unsigned int j=1;j<_dimension+1;j++)
_coeff_linear_bound = maximum(_coeff_linear_bound,abs(coeff[nu][j]));
for (unsigned int nu=0;nu<_dimension;nu++)
_coeff_const_bound = maximum(_coeff_const_bound,abs(coeff[nu][0]));
}
// bound function, simplified algorithm!
REAL bound_of_flow(const std::vector<REAL> x,const REAL& radius){
return abs(x)+_coeff_const_bound+radius*_coeff_linear_bound;
}
REAL bound_of_solution(const std::vector<REAL> x,const REAL& radius){
return radius * (abs(x) + _coeff_const_bound)
+ exp( abs(x)+radius * _coeff_linear_bound);
}
REAL coeff(unsigned int i, unsigned int j){
return _coeff[i][j];
}
};
class FUNCTIONAL_ivp_solver_simple :public FUNCTIONAL_object<unsigned int,std::vector<REAL> >
{
public:
/***********************************************************/
/* local data: flow "function" */
FLOW _flow;
/* The vector taylorpower[nu][i] contains an initial segment of the Taylor coefficients
for (y_nu)^i.
As soon as taylor[nu][i][1] is computed, we will also compute
taylorpower[nu][j][i] for as many values i (<=j) as needed by the flow.
*/
std::vector< std::vector< std::vector<REAL> > >taylorpower;
unsigned int _dimension;
/***********************************************************/
/* trivialer Objekt-Konstruktor:
es werden nur die Daten kopiert
*/
FUNCTIONAL_ivp_solver_simple(
const std::vector<REAL> & x0,
const FLOW & flow
) {
_flow=flow;
_dimension=_flow.dimension();
/* kopiere den Startwert x0 in die Tayloreihen als jeweils 0-te Koeffizienten bei Potenz 1*/
taylorpower.resize(_dimension);
for (unsigned int nu=0;nu<_dimension;nu++){
taylorpower[nu].resize(2);
taylorpower[nu][0].resize(1);
taylorpower[nu][0][0]=REAL(1);
taylorpower[nu][1].resize(1);
taylorpower[nu][1][0]=x0[nu];
}
}
/* (3.13) */
REAL simple_IVP(unsigned int nu,int l)
{
REAL sum=REAL(0);
for (unsigned int j=0;j<_dimension;j++){
sum += _flow.coeff(nu,j+1)*taylorpower[j][1][l];
}
if (l==0) return sum+_flow.coeff(nu,0);
return sum /(l+1);
}
/*****************************************************************/
/* Auswertungsfunktion: Taylorreihe in jeder Dimension bestimmen */
/* Werte werden dabei zwischengespeichert */
/*****************************************************************/
std::vector<REAL> eval(const unsigned int& n)
{
/* determine Taylor coefficients up to index n (inclusive) */
/* for the simple linear systems we do not need higher powers... */
/* in the general case we would have max_power=n*/
unsigned int max_power=1;
unsigned int l_old=taylorpower[0][0].size();
/* increase the space used to store the coefficients and their powers*/
for (unsigned int nu=0;nu<_dimension;nu++){
taylorpower[nu].resize(max_power+1);
for (unsigned int l=0;l<=max_power;l++){
taylorpower[nu][l].resize(n+1);
}
}
/* first determine the coefficients for exponent 1, then later the higher powers */
for (unsigned int l=l_old;l<=n;l++){
for (unsigned int nu=0;nu<_dimension;nu++){
/* for power 0: we only need to append with sufficiently many zeroes */
taylorpower[nu][0][l]=REAL(0);
/* for power 1: we have to compute the values explicitly using a recursive scheme */
/* the case l=0 has already been treated in the constructor */
if (l>0) taylorpower[nu][1][l]=simple_IVP(nu,l-1);
/* for powers >1: the values are determined using folding*/
for (unsigned int m=2;m<=max_power;m++){
unsigned int n0=0;
if (m<l)
n0=l;
for (unsigned int n1=n0;n1<=l;n1++){
REAL sum;
for (unsigned int j=0;j<=n1;j++){
sum = sum + (taylorpower[nu][1][j] *
taylorpower[nu][m-1][n1-j]);
}
taylorpower[nu][m][n1]=sum;
}
}
}
}
std::vector<REAL> result(_dimension);
for (unsigned int nu=0;nu<_dimension;nu++){
result[nu]=taylorpower[nu][1][n];
}
return result;
}
virtual void clear()
{
if (this->release_check()) return;
delete this;
}
};
/*****************************************************/
/* FUNCTION - constructor for "ivp_solver_simple" */
/*****************************************************/
inline FUNCTION<unsigned int,std::vector<REAL> > ivp_solver_simple (
const std::vector<REAL> & x0,
const FLOW & flow
) {
return new FUNCTIONAL_ivp_solver_simple (x0,flow);
}
template <typename K>
class POLYNOMIAL {
std::vector<K> c;
public:
POLYNOMIAL() : c{0} {assert(c.size());}
inline POLYNOMIAL(const K &c0) : c{c0} {assert(c.size());}
inline explicit POLYNOMIAL(const std::vector<K> &c) : c(c)
{
if (c.size() == 0)
this->c.push_back(K(0));
assert(this->c.size());
}
template <typename K2>
POLYNOMIAL(const POLYNOMIAL<K2> &p) : c(p.degree() + 1)
{
for (unsigned i=0; i<c.size(); i++)
c[i] = p[i];
assert(c.size());
}
void set_coeff(unsigned long i, const K &v)
{
c.resize(std::max(i+1, c.size()));
c[i] = v;
}
template <typename K2>
auto operator()(const K2 &x) const -> decltype(c[0]*x)
{
using KR = decltype(c[0]*x);
KR r(0);
K2 xi(1);
for (const K &ci : c) {
r += ci * xi;
xi *= x;
}
return r;
}
template <typename K2>
auto operator()(const POLYNOMIAL<K2> &b) const
-> decltype((*this)*(b))
{
using PR = decltype((*this) * b);
PR r;
POLYNOMIAL<K2> bi = K2(1);
for (const K &ci : c) {
r += POLYNOMIAL(ci) * bi;
bi *= b;
}
return r;
}
/* beware: returns just upper bound, unknown whether c[degree()] == 0 */
inline unsigned degree() const { return c.size()-1; }
inline const K & operator[](unsigned i) const { return c[i]; }
template <typename K2>
POLYNOMIAL & operator+=(const POLYNOMIAL<K2> &b)
{
c.resize(1+std::max(degree(), b.degree()));
for (unsigned i=0; i<=b.degree(); i++)
c[i] += b[i];
return *this;
}
template <typename K2>
inline auto operator+(const POLYNOMIAL<K2> &b) const
-> POLYNOMIAL<decltype(c[0]+b[0])>
{
return POLYNOMIAL<decltype(c[0]+b[0])>(*this) += b;
}
template <typename K2>
POLYNOMIAL & operator-=(const POLYNOMIAL<K2> &b)
{
c.resize(1+std::max(degree(), b.degree()));
for (unsigned i=0; i<=b.degree(); i++)
c[i] = c[i] - b[i];
return *this;
}
template <typename K2>
inline auto operator-(const POLYNOMIAL<K2> &b) const
-> POLYNOMIAL<decltype(c[0]-b[0])>
{
return POLYNOMIAL<decltype(c[0]-b[0])>(*this) -= b;
}
POLYNOMIAL operator-() const
{
POLYNOMIAL r = *this;
for (K &ci : r.c)
ci = -ci;
return r;
}
template <typename K2>
auto operator*(const POLYNOMIAL<K2> &b) const
-> POLYNOMIAL<decltype(c[0]*b[0])>
{
using KR = decltype(c[0]*b[0]);
std::vector<KR> r(degree() + b.degree() + 1);
for (unsigned i=0; i<=degree(); i++)
for (unsigned j=0; j<=b.degree(); j++)
r[i+j] += c[i] * b[j];
return POLYNOMIAL<KR>(r);
}
template <typename K2>
inline POLYNOMIAL & operator*=(const POLYNOMIAL<K2> &b)
{
return *this = *this * b;
}
POLYNOMIAL primitive(const K &C = 0) const
{
POLYNOMIAL b(std::vector<K>(c.size() + 1));
b.c[0] = C;
b.c[1] = c[0];
for (unsigned i=1; i<c.size(); i++)
b.c[i+1] = c[i] / (int)(i+1);
return b;
}
POLYNOMIAL derivative() const
{
POLYNOMIAL b(std::vector<K>(c.size() - 1));
for (unsigned i=1; i<c.size(); i++)
b.c[i-1] = c[i] * (int)i;
return b;
}
friend POLYNOMIAL imod(const POLYNOMIAL &p, unsigned degree)
{
if (degree > p.degree())
return p;
decltype(p.c) c(degree);
for (unsigned i=0; i<degree; i++)
c[i] = p.c[i];
return POLYNOMIAL(c);
}
friend orstream & operator<<(orstream &o, const POLYNOMIAL &p)
{
o << "(" << p[0] << ")";
for (unsigned i=1; i<=p.degree(); i++)
o << "+(" << p[i] << ")*x^" << i;
return o;
}
friend POLYNOMIAL abs(const POLYNOMIAL &p)
{
POLYNOMIAL r = p;
for (K &ci : r.c)
ci = abs(ci);
return r;
}
};
class POLYNOMIAL2 {
public:
struct I {
std::vector<unsigned> i;
REAL ci;
};
explicit POLYNOMIAL2(unsigned d, unsigned n_coeffs = 0) : mu(0), d(d)
{
c.reserve(n_coeffs);
}
unsigned degree() const { return mu; }
POLYNOMIAL2 & operator+=(const POLYNOMIAL2 &p)
{
for (const I &i : p.c) {
for (I &j : c) {
if (i.i == j.i) {
j.ci += i.ci;
goto ok;
}
}
c.push_back(i);
ok:;
}
mu = std::max(mu, p.mu);
return *this;
}
const std::vector<I> & coefficients() const { return c; }
void add_coeff(const I &i)
{
assert(i.i.size() == d);
c.push_back(i);
for (const unsigned &ij : i.i)
mu = std::max(mu, ij);
}
POLYNOMIAL2 & operator*=(const REAL &r)
{
for (I &l : c)
l.ci *= r;
return *this;
}
POLYNOMIAL2 operator*(const REAL &r) const { return POLYNOMIAL2(*this) *= r; }
POLYNOMIAL2 & operator*=(const I &k)
{
for (I &l : c) {
assert(l.i.size() == k.i.size());
for (unsigned j = 0; j < l.i.size(); j++) {
l.i[j] += k.i[j];
if (l.i[j] > mu) mu = l.i[j];
}
l.ci *= k.ci;
}
return *this;
}
POLYNOMIAL2 operator*(const I &k) const { return POLYNOMIAL2(*this) *= k; }
POLYNOMIAL2 & operator*=(const POLYNOMIAL2 &p)
{
for (const I &i : p.c) {
*this *= i;
}
return *this;
}
POLYNOMIAL2 operator*(const POLYNOMIAL2 &p) const { return POLYNOMIAL2(*this) *= p; }
private:
unsigned mu;
unsigned d;
std::vector<I> c;
};
struct VI {
std::vector<unsigned> ik;
std::vector<unsigned> idx_i_gt0;
unsigned ni_gt0;
REAL c;
VI(const REAL &c, const std::vector<unsigned> &ik)
: ik(ik), c(c)
{
for (unsigned j=0; j<ik.size()-1; j++)
if (ik[j] > 0)
idx_i_gt0.push_back(j);
ni_gt0 = idx_i_gt0.size();
}
VI(const POLYNOMIAL2::I &pi) : VI(pi.ci, pi.i) {}
operator POLYNOMIAL2::I() const
{
return { ik, c };
}
};
class POLYNOMIAL_FLOW {
public:
typedef VI I_type;
class Iterator {
const POLYNOMIAL_FLOW &F;
const unsigned nu;
unsigned state;
public:
Iterator(const POLYNOMIAL_FLOW &F, unsigned nu)
: F(F), nu(nu), state(0) {}
void operator++() { state++; }
explicit operator bool() const { return state < F.c[nu].size(); }
unsigned size() const { return F.dimension() + 1; }
const I_type * operator->() const { return &**this; }
const I_type & operator*() const { return F.c[nu][state]; }
int operator[](unsigned j) const { return (*this)->ik[j]; }
};
typedef Iterator iterator_type;
POLYNOMIAL_FLOW(unsigned dimension)
: _d(dimension), _mu(0), _autonomous(true), c(dimension)
{}
POLYNOMIAL_FLOW(const POLYNOMIAL_FLOW &old, const REAL &told)
: _d(old._d), _mu(0), _autonomous(old.is_autonomous()), c(old._d)
{
unsigned d = _d;
for (unsigned nu=0; nu<_d; nu++) {
POLYNOMIAL2 p(d+1);
for (Iterator it = old.iterator(nu, old.mu()); it; ++it) {
unsigned k = it[d];
REAL c = old(nu,it);
std::vector<unsigned> i = it->ik;
POLYNOMIAL2 q(d+1, k+1);
INTEGER bin_coeff = 1;
REAL told_power = 1;
for (unsigned l=0; l<=k; l++) {
i[d] = k-l;
// q.c.push_back({ i, times_bin_coeff(c, k, l) * power(told, k-l) });
q.add_coeff({ i, c * REAL(bin_coeff) * told_power });
bin_coeff *= k - l;
bin_coeff /= l + 1;
told_power *= told;
}
p += q;
}
for (const POLYNOMIAL2::I &ik : p.coefficients())
c[nu].push_back(ik);
_mu = std::max(_mu, p.degree());
}
}
void add_coeff(unsigned nu, const I_type &coeff)
{
assert(nu < _d);
assert(coeff.ik.size() == _d + 1);
c[nu].push_back(coeff);
for (unsigned j=0; j<=_d; j++)
_mu = std::max(_mu, coeff.ik[j]);
if (coeff.ik[_d] != 0)
_autonomous = false;
}
inline unsigned dimension() const { return _d; }
inline int mu() const { return _mu; }
inline bool is_autonomous() const { return _autonomous; }
iterator_type iterator(unsigned nu, unsigned l) const
{
return Iterator(*this, nu);
}
REAL operator()(unsigned nu, const iterator_type &idx) const
{
return idx->c;
}
REAL UF(std::vector<REAL> w, REAL t0, const REAL &delta, const REAL &eps) const
{
for (REAL &wj : w)
wj = abs(wj) + eps;
t0 = abs(t0) + delta;
REAL max_result(0);
for (unsigned nu=0; nu<dimension(); nu++) {
REAL result(0);
for (Iterator it = iterator(nu, _mu); it; ++it) {
REAL c = (*this)(nu, it);
REAL m(1);
if (it->ik[_d] > 0)
m *= power(t0, it->ik[_d]);
for (unsigned j : it->idx_i_gt0)
m *= power(w[j], it->ik[j]);
result += m * abs(c);
}
max_result = nu == 0 ? result : maximum(max_result, result);
}
return max_result;
}
void get_RM(
const std::vector<REAL> &w,
const REAL &t0,
const REAL &delta,
const REAL &eps,
REAL &R,
REAL &M
) const {
REAL _UF = UF(w,t0,delta,eps);
// cout << "U: " << _UF << "\n";
R = minimum(delta, eps/_UF);
REAL abs_w(0);
for (unsigned j = 0; j < w.size(); j++) {
abs_w += square(w[j]);
}
abs_w = sqrt(abs_w);
//M = abs_w + R*_UF;
// besser: _UF neu berechnen, aber das macht nur bei nicht-autonomen
// Systemen einen Unterschied
// NM, 2013-08-31
M = abs_w + R*UF(w,t0,R,eps);
}
#if 0
REAL f_abs(unsigned nu, REAL x) const
{
REAL r(0);
for (Iterator it = iterator(nu, _mu); it; ++it) {
unsigned sum_i = 0;
for (unsigned j=0; j<dimension(); j++)
sum_i += it[j];
r += abs((*this)(nu, it)) * power(x, sum_i);
}
return r;
}
REAL f_max(const REAL &x) const
{
REAL m = f_abs(0, x);
for (unsigned nu=1; nu<dimension(); nu++)
m = maximum(m, f_abs(nu, x));
return m;
}
#endif
REAL f_abs(unsigned nu, const std::vector<REAL> &w_abs, const REAL &s, const REAL &t) const
{
REAL r(0);
for (Iterator it = iterator(nu, _mu); it; ++it) {
REAL m = abs((*this)(nu, it));
if (it[dimension()] != 0)
m *= power(t, it[dimension()]);
for (unsigned j=0; j<it->ni_gt0; j++)
m *= power(w_abs[it->idx_i_gt0[j]] + s, it[it->idx_i_gt0[j]]);
r += m;
}
return r;
}
REAL f_max(const std::vector<REAL> &w, const REAL &s, const REAL &t) const
{
REAL m = f_abs(0, w, s, t);
for (unsigned nu=1; nu<dimension(); nu++)
m = maximum(m, f_abs(nu, w, s, t));
return m;
}
void get_RM2(
std::vector<REAL> w,
const REAL &t0,
const REAL &delta,
REAL &eps,
REAL &R,
REAL &M,
const int step_control_alg
) const {
// stiff area(-ACTUAL_STACK.prec_step/2);
REAL abs_w(0);
for (unsigned j = 0; j < w.size(); j++) {
abs_w += square(w[j]);
w[j] = abs(w[j]);
}
REAL t = abs(t0) + delta;
abs_w = sqrt(abs_w);
REAL lower_sum = 0;
REAL rect_w = eps;
REAL R_simple;
REAL eps_simple=eps;
REAL eps_opt;
int kl=0,kr=0,ks=0;
unsigned k =100;
for (unsigned j=0; j<k; j++) {
rect_w = 9*rect_w/8;
REAL tmp=rect_w / f_max(w, rect_w, t);
if (tmp > R_simple || tmp > 999*R_simple/1000) {R_simple=tmp; eps_simple=rect_w;ks=-j;}
// cout << "# stepcontrol " <<j<< " # "<<rect_w<< " : "<< tmp/9<< " sum: " << lower_sum << " vs. " << tmp<<"\n";;
lower_sum += tmp/9;
if ( tmp < lower_sum/30 || tmp< lower_sum/40) { kl=-j; break;}
}
eps_opt=rect_w;
rect_w = eps;
for (unsigned j=0; j<k; j++) {
REAL tmp=rect_w / f_max(w, rect_w, t);
if (tmp > R_simple || tmp > 999*R_simple/1000) {R_simple=tmp; eps_simple=rect_w;ks=j;}
// cout << "# stepcontrol " <<j<< " # " <<rect_w<< " : "<< tmp/16<< " sum: " << lower_sum << " vs. " << tmp<<"\n";;
lower_sum += tmp/16;
rect_w =15*rect_w/16;
if ( tmp < lower_sum/60 || tmp< lower_sum/70) { kr=j; break;}
}
lower_sum += rect_w / f_max(w,rect_w, t);
REAL R_opt = minimum(delta, lower_sum);
REAL M_opt = abs_w + eps_opt;
eps=eps_simple;
REAL M_simple=abs_w + eps_simple;
cout << "# Radius " << R_opt << " vs. " << R_simple<<"\n";
cout << "# Maximum " << M_opt << " vs. " << M_simple<<"\n";
cout << "# Epsilon " << eps_opt << " vs. " << eps_simple<<"\n";
cout << "# kl/ks/kr "<< kl << " " << ks << " " << kr<<"\n";
eps=eps_simple;
if (step_control_alg) {
R=R_simple; M=M_simple;
} else {
R=R_opt; M=M_opt;
}
}
private:
unsigned _d;
unsigned _mu;
bool _autonomous;
std::vector<std::vector<I_type>> c;
};
static REAL parse_REAL(const char *s)
{
if (!strcmp(s, "pi"))
return pi();
else if (!strcmp(s, "e"))
return euler();
else if (strchr(s, '/'))
return RATIONAL(s);
else
return s;
}
POLYNOMIAL_FLOW read_poly_flow(const char *fname)
{
REAL r;
unsigned dimension, nu;
int n, ret;
FILE *f;
const char *msg = NULL;
if (!(f = fopen(fname, "r"))) {
perror(fname);
exit(1);
}
ret = fscanf(f, "%u", &dimension); /* d */
POLYNOMIAL_FLOW F(dimension);
std::vector<unsigned> ik(dimension+1);
char *s = NULL;
if (ret < 0)
{ msg = "d"; goto err; }
while (fscanf(f, "%u", &nu) > 0) { /* nu */
if (fscanf(f, "%u", &ik[dimension]) < 0) /* k */
{ msg = "k"; goto err; }
for (unsigned j=0; j<dimension; j++)
if (fscanf(f, "%u", &ik[j]) < 0) /* i1,...,id */
{ msg = "i_j"; goto err; }
if (fscanf(f, "%*s%n", &n) < 0 || !n)
{ msg = "real number c_{nu,k,i_1,...,i_d}"; goto err; }
if (!(s = (char *)realloc(s, n+1))) {
perror("realloc");
exit(1);
}
fseek(f, -n, SEEK_CUR);
if (fscanf(f, "%s", s) < 0) /* c_{nu,k,i1,...,id} */
{ msg = "real number c_{nu,k,i_1,...,i_d}"; goto err; }
F.add_coeff(nu, VI(parse_REAL(s), ik));
}
free(s);
fclose(f);
return F;
err:
fprintf(stderr,
"invalid coefficient input file: expected <%s> at offset %lu\n",
msg, ftell(f));
free(s);
fclose(f);
exit(1);
}
struct Timer {
unsigned long t = 0;
void start() { struct timeval tv; gettimeofday(&tv, NULL); t -= tv.tv_sec * 1000000UL + tv.tv_usec; }
void stop() { struct timeval tv; gettimeofday(&tv, NULL); t += tv.tv_sec * 1000000UL + tv.tv_usec; }
} t_auto, t_rest0, t_rest1;
template <typename F>
class FUNCTIONAL_ivp_solver_auto :public FUNCTIONAL_object<unsigned int,std::vector<REAL> >
{
F _flow;
/* The vector a[nu][i] contains an initial segment of the Taylor coefficients
for (y_nu)^i.
As soon as taylor[nu][i][1] is computed, we will also compute
a[nu][j][i] for as many values i (<=j) as needed by the flow.
*/
std::vector< std::vector< std::vector<REAL> > >a;
unsigned int _dimension;
const bool iv_is_zero;
REAL a_vector_power(const std::vector<int> &n, const typename F::iterator_type &i) const {
REAL ergebnis = REAL(1);
for (unsigned int nu = 0; nu < _dimension; nu++) {
ergebnis *= a[nu][i[nu]][n[nu]];
}
return ergebnis;
}
inline void combination_recursive(std::vector<int> &n, unsigned j, REAL &sum, const typename F::iterator_type &idx, unsigned l) const
{
if (j == n.size() -1) {
n[j] = (iv_is_zero ? idx[j] : 0) + l;
sum += a_vector_power(n, idx);
return;
}
if (idx[j] == 0) {
n[j] = 0;
combination_recursive(n, j+1, sum, idx, l);
} else {
for (unsigned k=0; k <= l; k++) {
n[j] = (iv_is_zero ? idx[j] : 0) + k;
combination_recursive(n, j+1, sum, idx, l-k);
}
}
}
inline void combination_recursive2(const REAL &mul, unsigned j, unsigned d, REAL &sum, const typename F::iterator_type &idx, unsigned l) const
{
if (j == d) {
if (l == 0)
sum += mul;
return;
}
if (idx[j] == 0) {
combination_recursive2(mul, j+1, d, sum, idx, l);