forked from anubhav-gres/VAL-4.2.08
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomial.cpp
More file actions
1998 lines (1541 loc) · 48.3 KB
/
Copy pathPolynomial.cpp
File metadata and controls
1998 lines (1541 loc) · 48.3 KB
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 2008, Strathclyde Planning Group,
* Department of Computer and Information Sciences,
* University of Strathclyde, Glasgow, UK
* http://planning.cis.strath.ac.uk/
*
* Maria Fox, Richard Howey and Derek Long - VAL
* Stephen Cresswell - PDDL Parser
*
* This file is part of VAL, the PDDL validator.
*
* VAL is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* VAL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VAL. If not, see <http://www.gnu.org/licenses/>.
*
************************************************************************/
/*-----------------------------------------------------------------------------
VAL - The Automatic Plan Validator for PDDL+
$Date: 2009-02-05 10:50:21 $
$Revision: 1.2 $
Maria Fox, Richard Howey and Derek Long - PDDL+ and VAL
Stephen Cresswell - PDDL Parser
maria.fox@cis.strath.ac.uk
derek.long@cis.strath.ac.uk
stephen.cresswell@cis.strath.ac.uk
richard.howey@cis.strath.ac.uk
By releasing this code we imply no warranty as to its reliability
and its use is entirely at your own risk.
Strathclyde Planning Group
http://planning.cis.strath.ac.uk
----------------------------------------------------------------------------*/
//#undef vector
#include <math.h>
#include <vector>
#include <algorithm>
#include "Polynomial.h"
#include "Exceptions.h"
#include "Proposition.h"
#include "main.h"
using std::cout;
//#define vector std::vector
//#define map std::map
namespace VAL {
ostream & operator <<(ostream & o,const Intervals & i)
{
i.write(o);
return o;
};
bool Intervals::operator==(const Intervals & ints) const
{
return intervals == ints.intervals;
};
Intervals::Intervals(vector<pair<intervalEnd,intervalEnd> > ints) : intervals(ints)
{
Intervals int0;
for(vector< pair<intervalEnd,intervalEnd> >::iterator i = intervals.begin(); i != intervals.end();++i)
{
if(((i->first.first > i->second.first) || ( (i->first.first == i->second.first) && (!(i->first.second) || !(i->second.second)) ) )
)
{
*report << "The collection of intervals "<< *this << " is invalid\n";
InvalidIntervalsError iie;
throw iie;
};
};
//Note: no check that intervals do not overlap
};
//dummy intervals for use in derived predicate method getIntervals
Intervals::Intervals(bool b)
{
intervals.push_back(make_pair(make_pair(-2,false),make_pair(-1,false)));
};
Intervals::~Intervals()
{
intervals.clear();
};
void Intervals::write(ostream & o) const
{
if(intervals.size()==0)
{
o << "the empty set";
}
else
{ if(LaTeX) o << "$";
for(vector< pair<intervalEnd,intervalEnd> >::const_iterator i = intervals.begin(); i != intervals.end();)
{
if(i->first.second) o << "[ "; else o << "( ";
o << i->first.first << " , " << i->second.first;
if(i->second.second) o << " ]"; else o << " )";
if(++i != intervals.end()) {if(LaTeX) o << "\\cup"; else o << " U ";};
};
if(LaTeX) o << "$";
};
//o << "\n";
};
void Intervals::writeOffset(double t) const
{
if(intervals.size()==0)
{
*report << "the empty set";
}
else
{
if(LaTeX) *report << "$";
for(vector< pair<intervalEnd,intervalEnd> >::const_iterator i = intervals.begin(); i != intervals.end();)
{
if(i->first.second) *report << "[ "; else *report << "( ";
*report << t + i->first.first << " , " << t + i->second.first;
if(i->second.second) *report << " ]"; else cout << " )";
if(++i != intervals.end()) {if(LaTeX) *report << "\\cup"; else cout << " U ";};
};
if(LaTeX) *report << "$";
};
};
ostream & operator << (ostream & o,const CtsFunction & cf)
{
cf.write(o);
return o;
};
//returns 1 / num!
CoScalar recipfact(unsigned int num)
{
CoScalar ans = 1;
for(CoScalar i = 1; i <= num; ++i)
{
ans *= 1 / i;
};
return ans;
};
unsigned int Exponential::getNoTerms(CoScalar endInt) const
{
unsigned int N = 0;
unsigned int N1 = 0;
CoScalar aConstant;
CoScalar a = 0;
CoScalar rem;
//find the maximum value of the polynomial
vector<CoScalar> roots = poly->getRoots(endInt);
a = poly->evaluate(0);
CoScalar aValue = poly->evaluate(endInt);
if(aValue > a) a = aValue;
for(vector<CoScalar>::const_iterator i = roots.begin(); i != roots.end(); ++i)
{
aValue = *i;
if(aValue > a) a = aValue;
};
//cout << " \\\\\nMax ="<<a<<"\\\\\n";
if( a >= 0)
{
aConstant = fabs(K*exp(a));
rem = aConstant;
while(rem >= Polynomial::tooSmall)
{
N++;
rem = aConstant * fabs(pow(a,N+1)) * recipfact(N);
};
};
//cout << " \\\\\nN = "<<N<<"\\\\\n";
//find the minimum value of the polynomial
a = poly->evaluate(0);
aValue = poly->evaluate(endInt);
if(aValue < a) a = aValue;
for(vector<CoScalar>::const_iterator i = roots.begin(); i != roots.end(); ++i)
{
aValue = *i;
if(aValue < a) a = aValue;
};
//cout << " \\\\\nMin = "<<a<<"\\\\\n";
if( a < 0)
{
aConstant = fabs(K*exp(a));
rem = aConstant;
while(rem >= Polynomial::tooSmall)
{
N1++;
rem = aConstant * fabs(pow(a,N1+1)) * recipfact(N1);
};
// cout << " \\\\\nN1 = "<<N1<<"\\\\\n";
if(N1 > N) N =N1;
};
//cout << "\\\\\\> No of terms for $"<<*this<<"$ = "<<N<<" aha!\\\\\n";
return N;
};
Polynomial Exponential::getApproxPoly(CoScalar endInt) const
{
Polynomial ans;
if(poly->getDegree() == 1 && poly->getCoeff(0) == 0)
{
ans.setCoeff(0,K);
CoScalar a = poly->getCoeff(1);
unsigned int noTerms = getNoTerms(endInt);
for(unsigned int i = 1; i < noTerms ; ++i)
{
ans.setCoeff(i,K * recipfact(i) * pow(a,i));
};
if(c != 0)
ans.addToCoeff(0,c);
}
else if( poly->getDegree() == 0)
{
ans.setCoeff(0,K*exp(poly->getCoeff(0)));
if(c != 0)
ans.addToCoeff(0,c);
}
else
{
ans = *poly;
ans.addToCoeff(0,1);
Polynomial powPoly = *poly;
unsigned int noTerms = getNoTerms(endInt);
if(noTerms > 150) //we are not prepared to wait to compute a lot of terms
{
cout <<"Blown on terms...\n";
ApproxPolyError ape;
throw ape;
};
for(unsigned int i = 2; i <= noTerms; ++i)
{
powPoly = (powPoly*(*poly))/i; //powPoly = poly^i
if(!powPoly.checkPolynomialCoeffs())
{
ApproxPolyError ape;
throw ape;
};
ans += powPoly;
// cout << " \\\\\nApprox poly = "<<i<<":::$"<< ans <<"$\\\\\n";
};
ans = K*ans;
if(c != 0)
ans.addToCoeff(0,c);
};
// cout << " \\\\\nApprox poly = $"<< ans <<"$\\\\\n";
return ans;
};
vector<CoScalar> Exponential::getRoots(CoScalar t) const
{
return getApproxPoly(t).getRoots(t);
};
//check for infinity or NaN
void checkNum(CoScalar num)
{
if( (2 * num == num && num != 0) || (!( num == num)) )
{
NumError ne;
throw ne;
};
};
bool Exponential::isLinear() const
{
if(K == 0 || poly->getDegree() == 0) return true;
return false;
};
bool Polynomial::isLinear() const
{
if(getDegree() < 2) return true;
return false;
};
pair<vector<pair<intervalEnd,intervalEnd> >,vector<CoScalar> > Exponential::isolateRoots(CoScalar t,CoScalar accuracy = 0) const
{
return getApproxPoly(t).isolateRoots(t,accuracy);
};
CoScalar Exponential::evaluate(CoScalar t) const
{
CoScalar ans = K * exp(poly->evaluate(t)) + c;
checkNum(ans);
return ans;
};
void Exponential::write(ostream & o) const
{
if(K == 0)
o << "0";
o << K << "e^{"<<*poly <<"}";
if(c-offSet != 0)
{
if(c-offSet > 0)
o << " + "<<c-offSet;
else
o << " - "<<-c-offSet;
};
};
Polynomial::~Polynomial()
{
coeffs.clear();
};
CoScalar Polynomial::accuracy;
const CoScalar Polynomial::tooSmall = 1e-12;
CoScalar Polynomial::getCoeff(unsigned int pow) const
{
map<unsigned int,CoScalar>::const_iterator i = coeffs.find(pow);
if(i != coeffs.end())
return i->second;
else
return 0;
};
void Polynomial::setCoeff(unsigned int pow,CoScalar value)
{
CoScalar value0 = value;
map<unsigned int,CoScalar>::iterator i = coeffs.find(pow);
if(i != coeffs.end())
{
if(value0 != 0)
i->second = value0;
else
coeffs.erase(i);
}
else
{
if(value0 != 0) coeffs[pow] = value0;
};
};
void Polynomial::addToCoeff(unsigned int pow,CoScalar value)
{
map<unsigned int,CoScalar>::iterator i = coeffs.find(pow);
if(i != coeffs.end())
{
i->second += value;
if(i->second == 0) coeffs.erase(i);
}
else
{
if(value != 0) coeffs[pow] = value;
};
};
unsigned int Polynomial::getDegree() const
{
if(coeffs.size()==0) return 0;
return (--coeffs.end())->first;
};
Polynomial& Polynomial::operator+=(const Polynomial & p)
{
for(map<unsigned int,CoScalar>::const_iterator i = p.coeffs.begin(); i != p.coeffs.end();++i)
{
addToCoeff(i->first,i->second);
};
return *this;
};
Polynomial& Polynomial::operator+=(CoScalar num)
{
addToCoeff(0,num);
return *this;
};
Polynomial& Polynomial::operator-=(const Polynomial & p)
{
for(map<unsigned int,CoScalar>::const_iterator i = p.coeffs.begin(); i != p.coeffs.end();++i)
{
addToCoeff(i->first,-(i->second));
};
return *this;
};
Polynomial& Polynomial::operator-=(CoScalar num)
{
addToCoeff(0,-num);
return *this;
};
Polynomial& Polynomial::operator*=(const Polynomial & p)
{
Polynomial ans;
for(map<unsigned int,CoScalar>::const_iterator i = p.coeffs.begin(); i != p.coeffs.end();++i)
{
for(map<unsigned int,CoScalar>::const_iterator j = coeffs.begin(); j != coeffs.end();++j)
{
ans.addToCoeff((i->first)+(j->first),(i->second)*(j->second));
};
};
*this = ans;
return *this;
};
Polynomial& Polynomial::operator*=(CoScalar num)
{
for(map<unsigned int,CoScalar>::const_iterator i = coeffs.begin(); i != coeffs.end();++i)
{
setCoeff(i->first,(i->second)*num);
};
return *this;
};
Polynomial operator+(const Polynomial & p,const Polynomial & q)
{
Polynomial ans = p;
return ans += q;
};
Polynomial operator+(CoScalar num,const Polynomial & p)
{
Polynomial ans = p;
return ans += num;
};
Polynomial operator+(const Polynomial & p,CoScalar num)
{
Polynomial ans = p;
return ans += num;
};
Polynomial operator-(const Polynomial & p,const Polynomial & q)
{
Polynomial ans = p;
return ans -= q;
};
Polynomial operator-(CoScalar num,const Polynomial & p)
{
Polynomial ans = p;
return ans -= num;
};
Polynomial operator-(const Polynomial & p,CoScalar num)
{
Polynomial ans = p;
return ans -= num;
};
Polynomial operator*(const Polynomial & p,const Polynomial & q)
{
Polynomial ans = p;
return ans *= q;
};
Polynomial operator*(CoScalar num,const Polynomial & p)
{
Polynomial ans = p;
return ans *= num;
};
Polynomial operator*(const Polynomial & p,CoScalar num)
{
Polynomial ans = p;
return ans *= num;
};
Polynomial operator-(const Polynomial & p)
{
Polynomial ans = p;
return ans *= -1;
};
Polynomial operator/(const Polynomial & p,CoScalar num)
{
Polynomial ans = p;
return ans *= (1/num);
};
bool Polynomial::operator==(const Polynomial & p) const
{
if(getDegree() != p.getDegree()) return false;
for(unsigned int i = getDegree(); i >= 0; --i)
{
if(getCoeff(i) != p.getCoeff(i)) return false;
};
return true;
};
CoScalar Polynomial::evaluate(CoScalar t) const
{
CoScalar ans = 0;
for(map<unsigned int,CoScalar>::const_iterator i = coeffs.begin(); i != coeffs.end();++i)
{
ans += CoScalar((i->second)*pow(t,i->first));
};
return ans;
};
Polynomial Polynomial::diff() const
{
Polynomial ans;
for(map<unsigned int,CoScalar>::const_iterator i = coeffs.begin(); i != coeffs.end();++i)
{
if( i->first != 0) ans.setCoeff((i->first) - 1,(i->second)*(i->first));
};
return ans;
};
Polynomial Polynomial::integrate() const
{
Polynomial ans;
for(map<unsigned int,CoScalar>::const_iterator i = coeffs.begin(); i != coeffs.end();++i)
{
ans.setCoeff((i->first) + 1,(i->second)/( (i->first) + 1 ));
};
return ans;
};
CoScalar newtonsMethod(const Polynomial & p,CoScalar startPt,CoScalar accuracy)
{
//return -1 to show the method has failed on this ocassion, we will always be finding roots on (0,1)
Polynomial pDiff = p.diff();
CoScalar approxRoot,diffVal;
CoScalar lastApproxRoot = startPt;
int limit = 50 + int(1/accuracy); //should normally be within accurrcy < 10 steps, so limit is more than enough
int i = 0;
for(; i < limit; ++i)
{
diffVal = pDiff.evaluate(lastApproxRoot);
if(diffVal == 0)
{
return -1;
};
approxRoot = lastApproxRoot - ((p.evaluate(lastApproxRoot) / diffVal));
//cout << "Approx root = "<<approxRoot<<"\n";
if((approxRoot - lastApproxRoot < accuracy) && (approxRoot - lastApproxRoot > -accuracy) ) break;
lastApproxRoot = approxRoot;
};
if(i == limit)
{
return -1;
};
//cout << "Root of "<<p<<" is "<<approxRoot<<"\n";
return approxRoot;
};
//on interval(0,t) within accuracy, repeated roots not returned
vector<CoScalar> Polynomial::getRoots(CoScalar t) const
{
vector<CoScalar> roots;
//remove repeated roots
Polynomial testPoly = removeRepeatedRoots();
if(testPoly.getDegree() == 0)
{
if(testPoly.getCoeff(0) == 0)
{
InfiniteRootsError ire;
throw ire;
};
}
else if(testPoly.getDegree() == 1)
{
CoScalar root = - (testPoly.getCoeff(0) / testPoly.getCoeff(1));
if( root > 0 && root < t) roots.push_back( root );
}
else if(testPoly.getDegree() == 2)
{
CoScalar a = testPoly.getCoeff(2);
CoScalar b = testPoly.getCoeff(1);
CoScalar c = testPoly.getCoeff(0);
CoScalar root1,root2;
if( b*b - 4*a*c >= 0)
{
root1 = (-b - sqrt(b*b - 4*a*c) ) / (2*a);
root2 = ( -b + sqrt(b*b - 4*a*c) ) / (2*a);
if( root1 > 0 && root1 < t) roots.push_back( root1 );
if( root2 > 0 && root2 < t && root1 != root2) roots.push_back( root2 );
};
}
else
{
pair<vector<pair<intervalEnd,intervalEnd> >,vector<CoScalar> > isolExact = testPoly.isolateRoots(t,Polynomial::accuracy);
for(vector<pair<intervalEnd,intervalEnd> >::iterator i = isolExact.first.begin(); i != isolExact.first.end(); ++i)
{
roots.push_back((i->first.first + i->second.first)/2);
};
for(vector<CoScalar>::iterator j = isolExact.second.begin(); j != isolExact.second.end(); ++j)
{
roots.push_back(*j);
};
};
vector<CoScalar> copyRoots = roots;
roots.clear();
//remove small roots
for(vector<CoScalar>::iterator i = copyRoots.begin(); i != copyRoots.end(); ++i)
{
if(*i > 0.01) roots.push_back(*i); //tolerance value
};
copyRoots.clear();
return roots;
};
void Polynomial::write(ostream & o) const
{
if(coeffs.size()==0)
{
o << "0";
}
else
{
map<unsigned int,CoScalar>::const_iterator i = coeffs.end();
for(unsigned int count=0; count != coeffs.size(); ++count)
{
--i;
if( count != 0)
{
if(i->second >= 0)
o << " + ";
else
o << " - ";
}
else if(i->second < 0)
{
o << " - ";
};
if(i->second >= 0)
{
if( (i->second != 1) || (i->first == 0) ) o << i->second;
}
else
{
if( (i->second != -1) || (i->first == 0) ) o << -(i->second);
};
if(i->first == 1)
o << "t";
else if(i->first != 0)
o << "t^" << i->first;
if( i == coeffs.begin()) break;
};
};
//o << "\n";
};
ostream & operator << (ostream & o,const Polynomial & p)
{
p.write(o);
return o;
};
void Polynomial::removeSmallCoeffs()
{
if(coeffs.size()==0) return;
unsigned int deg = getDegree();
for(unsigned int pow = 0; pow <= deg; ++pow)
{
map<unsigned int,CoScalar>::iterator i = coeffs.find(pow);
if(i != coeffs.end() && i->second < tooSmall && i->second > -tooSmall) coeffs.erase(i);
};
};
//divides this by d and returns the quotient and remainder
pair<Polynomial,Polynomial> Polynomial::divide(const Polynomial & d) const
{
Polynomial quot;
unsigned int numDegree = getDegree();
unsigned int denomDegree = d.getDegree();
if(numDegree < denomDegree) return make_pair(quot,*this);
CoScalar quotCo;
CoScalar leadingDenomCo = d.getCoeff(denomDegree);
Polynomial aPoly = *this;
Polynomial bPoly;
unsigned int i = numDegree + 1;
unsigned int stepBound = 2*numDegree;
for(unsigned int c = 0; c < stepBound ; ++c)
{
--i;
quotCo = aPoly.getCoeff(i) / leadingDenomCo;
bPoly.setCoeff(i - denomDegree, quotCo);
quot += bPoly;
aPoly = aPoly - d * bPoly;
aPoly.removeSmallCoeffs();
if(i - denomDegree == 0) break;
bPoly.setCoeff(i - denomDegree,0);
};
return make_pair(quot,aPoly);
};
Polynomial Polynomial::getGCD(const Polynomial & p) const
{
Polynomial gcd,num,denom;
if(getDegree() >= p.getDegree())
{
num = *this;
denom = p;
}
else
{
num = p;
denom = *this;
};
Polynomial rem = denom;
unsigned int stepBound = 3*num.getDegree();
for(unsigned int c = 0; c < stepBound ; ++c)
{
gcd = rem;
rem = num.divide(denom).second;
if(!((rem.getDegree() == 0) && (rem.getCoeff(0) == 0)))
{
num = denom;
denom = rem;
}
else
break;
};
if((gcd.getDegree() == 0) && (gcd.getCoeff(0) == 0)) gcd.setCoeff(0,1);
return gcd;
};
Polynomial Polynomial::removeRepeatedRoots() const
{
Polynomial d = diff();
Polynomial gcd = getGCD(d);
Polynomial ans;
if(gcd.getDegree() > 0)
{
ans = divide(gcd).first;
}
else
{
ans = *this;
};
return ans;
};
//R(p(t)) = t^(deg(p))*p(1/t)
Polynomial mapR(const Polynomial & p)
{
Polynomial ans;
unsigned int deg = p.getDegree();
for(unsigned int i = 0; i < deg + 1; ++i)
{
ans.setCoeff(i,p.getCoeff(deg - i));
};
//cout << "R( "<<p<<" ) = "<<ans<<"\n";
return ans;
};
//H_c(p(t)) = p(ct) for c in Reals
Polynomial mapHc(const Polynomial & p,CoScalar c)
{
Polynomial ans;
unsigned int deg = p.getDegree();
CoScalar cPowi = 1; //c to the power of i
for(unsigned int i = 0; i < deg + 1; ++i)
{
//ans.setCoeff(i,p.getCoeff(i)*pow(c,i));
ans.setCoeff(i,p.getCoeff(i)*cPowi);
cPowi = cPowi*c;
};
return ans;
};
//no. of ways to choose r things from n things, i.e. n!/[(n-r)!r!]
CoScalar choose(unsigned int n,unsigned int r)
{
CoScalar ans = 1;
for(unsigned int i = n; i > n - r; --i)
{
ans = (ans*i)/(n-i+1);
};
return ans;
};
//T_c(p(t)) = p(t+c) for c in Reals
Polynomial mapTc(const Polynomial & p,CoScalar c)
{
Polynomial ans;
unsigned int deg = p.getDegree();
for(unsigned int i = 0; i < deg + 1; ++i)
{
Polynomial aPoly;
for(unsigned int j = 0; j < i + 1; ++j)
{
aPoly.setCoeff(j,p.getCoeff(i)*pow(c,i-j)*choose(i,j));
};
ans += aPoly;
};
//cout << "T( "<<p<<","<<c<<" ) = "<<ans<<"\n";
return ans;
};
//P_k_c (p(t)) = 2^{kn} p( (t+c)/(2^k) ) for c in Reals, k integer, n= degree(p)
Polynomial mapPkc(const Polynomial & p,CoScalar k,CoScalar c)
{
Polynomial ans;
unsigned int deg = p.getDegree();
for(unsigned int i = 0; i < deg + 1; ++i)
{
Polynomial aPoly;
CoScalar cPowij = 1;
/*
for(unsigned int j = 0; j < i + 1; ++j)
{
aPoly.setCoeff(j, pow(2,(deg-i)*k) * p.getCoeff(i) * pow(c,i-j)*choose(i,j));
};*/
for(unsigned int j = i; ; --j)
{
aPoly.setCoeff(j, pow(2,(deg-i)*k) * p.getCoeff(i) * cPowij*choose(i,j));
cPowij = cPowij * c;
if(j==0) break;
};
ans += aPoly;
};
//cout << "P( "<<p<<","<<k<<" , "<<c<<" ) = "<<ans<<"\n";
return ans;
};
int sign(CoScalar a)