-
Notifications
You must be signed in to change notification settings - Fork 0
/
mv_poly.hpp
809 lines (720 loc) · 24.8 KB
/
mv_poly.hpp
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
/** @file mv_poly.hpp
*
* Implementation of multivariate polynomials and accompanying routines.
* The main idea of current implementation of multivariate polynomials
* is in the fact that polynomial from say n variables is just the
* polynomial from one variable with coefficients being (from the ring of)
* polynomials from n-1 variables. This idea is implemented on the basis of
* template recursive instantiation.
*
* @author Artem Pelenitsyn
* @date 2010-06-28
*/
#ifndef MV_POLY_HPP_
#define MV_POLY_HPP_
#include <algorithm>
#include <functional>
#include <deque>
#include <iostream>
#include <iterator>
#include <numeric>
#include <map>
#include <sstream>
#include <string>
#include <typeinfo>
#include <cassert>
#include <tr1/functional>
#include "Utilities.hpp"
#include "Point.hpp"
#include "CoefficientTraits.hpp"
namespace TestMVPoly {
void outputTest();
}
namespace mv_poly {
/// \cond
template<typename T>
class Polynomial;
/// \endcond
/** Neat template type for actually getting multivariate polynomials.
* Delivers user from writing \c Polynomial<Polynomial<… Polynomial<int>… >.
@param VarCnt — polynomial variables count
@param Coef — polynomial coefficient type */
template<int VarCnt, typename Coef>
struct MVPolyType {
typedef Polynomial<typename MVPolyType<VarCnt - 1, Coef>::ResultT> ResultT;
typedef ResultT type; // result as in Boost.MPL
};
/// \cond
/* Specialization of MVPolyType
for stopping recursive instantiation */
template<typename Coef>
struct MVPolyType<1, Coef> {
// multivariate polynomial from 1 variable is just Polynomial
typedef Polynomial<Coef> ResultT;
typedef ResultT type; // result as in Boost.MPL - for compatability reasons
};
/// \endcond
/**
* Variables counter for Polynomial class template.
* Uses simple temp
* late recursion. The actual formula for the number of
* variables in Polynomial<T> is:
* <tt>VarCnt< Polynomial<T> > = 1 + VarCnt<T> ></tt>
* with VarCnt<T> is equal to 0 for all T not in form Polynomial<S>.
*
* For the purpose of counting the recurison depth in recursive template types
* like Polynomial Vandevoorde&Josuttis suggest making enum constant
* in recursive type itself, but we notice
* some code bloating when defining template specialization for
* recursion stopping. So we decided to make separate class (VarCnt) for this.
*
* \note The class isn't meant to be used in application (client) code.
*/
template<typename T>
class VarCnt {
public:
static const int result = 0;
};
/**
* Generic polynomial class template.
*/
template<typename T>
class Polynomial {
public:
template<typename S>
friend
std::istream& operator>>(std::istream& is, Polynomial<S> & p);
friend
void TestMVPoly::outputTest();
Polynomial() : data(1, CoefficientTraits<ElemT>::addId()) {}
explicit Polynomial(std::string const & s) {
loadPolyFromString(*this, s );
}
/**
* Template for computing the type of multivariate polynomial coefficients.
*/
template<typename S>
class PolyCoeff {
public:
typedef S Type;
};
template<typename S>
class PolyCoeff< Polynomial<S> > {
public:
typedef typename PolyCoeff<S>::Type Type;
};
/**
* Multivariate polynomial coefficients type.
* For <tt>Polynomial<Polynomial<… Polynomial<T>… ></tt> it's \c T.
*/
typedef typename PolyCoeff<T>::Type CoefT;
/**
* STL-compliant typedef for map-like types.
*/
typedef CoefT mapped_type;
/**
* Number of polynomial variables. Actually it's nestedness depth of type
* <tt>Polynomial<Polynomial<… Polynomial<T>… ></tt>.
*/
static const int VAR_CNT = 1 + VarCnt<T>::result;
/**
* Subscript operator aimed for use with “plain” (one-variable) polynomials
* (i.e.\ Polynomial<T> where T is not of the form Polynomial<S>).
* @param idx index of (plain) polynomial coefficient to be returned.
* @return (plain) polynomial coefficient with index \c idx.
*/
CoefT operator[](int idx) const;
/**
* Subscript operator aimed for use with multivariate polynomials
* (i.e.\ Polynomial<T> where T is not of the form Polynomial<S>, S is possibly
* of the form Polynomial<U> and so on). Coefficients of mv-polynomial is
* numbered with points (Point<Dim>), and point dimension (Dim) should be
* equal to the number of polynomial variables.
* @param pt point-index (sometimes called multi-indexd) of polynomial
* coefficient to be returned.
* @return polynomial coefficient with point-index \c pt.
*/
template<template <typename PointImpl> class OrderPolicy>
CoefT operator[](Point<VAR_CNT, OrderPolicy> const & pt) const;
/**
* Multiply polynomial on a scalar (assignment version).
* @param c Scalar to multiply on.
* @return This polynomial multiplyed on \c c.
*/
Polynomial operator*=(CoefT const & c) {
//using std::tr1::bind;
//using std::tr1::placeholders::_1;
//std::for_each(data.begin(), data.end(), // binding overloaded functions
//bind(operator*=, _1, c)); // from different scopes is really messy
for (typename StorageT::iterator it = data.begin(); it != data.end(); ++it) {
(*it) *= c;
}
return *this;
}
/**
* Multiply polynomial on a monomial represented by its degree (assignment version)
* aimed for use with multivariate polynomials. If the polynomial has
* VAR_CNT variables <tt>x = x_1 x_2 … x_{VAR_CNT}</tt>, then
* <tt>Point<VAR_CNT> m</tt> represents monomial
* <tt>x^m = x_1^{m_1} x_2^{m_2} … x_{VAR_CNT}^{m_{VAR_CNT}}</tt>
* @param c Monomial x^{\c m} to multiply on.
* @return This polynomial multiplyed on \c x^{\c m}.
*/
template<template <typename PointImpl> class OrderPolicy>
Polynomial operator<<=(Point<VAR_CNT, OrderPolicy> const & m);
/**
* Multiply polynomial on a monomial represented by its degree (assignment version)
* aimed for use with “plain” (one-variable) polynomials.
* @param m x^{\c m} to multiply on.
* @return This polynomial multiplyed on \c x^{\c m}.
*/
Polynomial operator<<=(int m);
/**
* Polynomial addition (assignment version).
* @param p[in] Polynomial to be added to this.
* @return This polynomial after addition \c p.
*/
Polynomial operator+=(Polynomial const & p);
/**
* Polynomial subtraction (assignment version).
* @param p[in] Polynomial to be subtracted from this.
* @return This polynomial after addition \c p.
*/
Polynomial operator-=(Polynomial const & p);
/**
* Polynomial comparison for equality after normalization.
* @param lhs Left-hand side operand for comparison
* @param rhs Right-hand side operand for comparison
* @return True iff all coefficients of both polynomials are equal, false
* otherwise. If high coefficients of one polynomial are zero, they considered
* equal to absent high coefficients of other polynomial.
*/
template<typename S>
friend
inline
bool operator==(Polynomial<S> const & lhs, Polynomial<S> const & rhs);
CoefT operator()(CoefT const & c) const;
template<typename CurvePoint>
CoefT operator()(CurvePoint const & cp) const;
template<int Dim, typename Body>
CoefT operator()(ConstSlice<Body, Dim, Dim - 1> const & cp) const;
/**
* As polynomial is actually a special container type, it has distinguished
* type for it's elements.
*/
typedef T ElemT;
typedef std::deque<ElemT> StorageT;
StorageT const & getCoefs() const { return data; }
size_t getPlainDegree() const { return data.size(); }
// Point<VAR_CNT, OrderPolicy> getDegree()
void setCoefs(StorageT const & data) { this->data = data; }
/**
* Returns polynomial 1.
* @return polynomial 1
*/
static Polynomial getId() {
std::string strRep;
std::fill_n(std::back_inserter(strRep), VAR_CNT, '[');
std::ostringstream elemId;
elemId << CoefficientTraits<CoefT>::multId();
strRep += elemId.str();
std::fill_n(std::back_inserter(strRep), VAR_CNT, ']');
return Polynomial(strRep);
}
private:
/**
* Implementation detail: used in operator[](Point<VAR_CNT>) for going
* deeper in the polynomial nest. If client calls
* <tt>MVPolyType<n, T>::operator[Point<n> pt]</tt>,
* then for all k we will use
* <tt>MVPolyType<n - k, T>::operator[ConstSlice<n, k>]</tt>,
* where <tt>ConstSlice<n, k></tt> offers access to the part of pt, namely
* <tt>pt[k, k+1, ..., n-1]</tt>
* @param sl
* @return
*/
template<typename Body, int Dim, int Offset>
CoefT operator[](ConstSlice<Body, Dim, Offset> const & sl) const;
template<typename T1, typename S, typename Pt>
friend
T1 applySubscript(S const & el, Pt const & pt);
/**
* Implementation detail: used in operator<<=(Point<VAR_CNT>) for going
* deeper in the polynomial nest.
* @param m ConstSlice of the monomial got in initial client call for
* <tt>operator<<=(Point<VAR_CNT>)</tt>.
* @return This polynomial with one more dimension multiplied on the next
* initial point component pointed to by the current slice.
*/
template<typename Body, int Dim, int Offset>
Polynomial operator<<=(ConstSlice<Body, Dim, Offset> const & m);
template<typename S, typename Pt>
friend
void applyMonomialMultiplication(S & elem, Pt const & monomial);
/**
* Delete trailing zeros in coefficient collection \c data.
*/
void normalization() const {
StorageT & data = const_cast<StorageT &>(this->data);
if (data.size() < 2)
return; // we will not delete single zero
ElemT tempDefElem = ElemT();
typename StorageT::iterator it = --data.end(); // it is still valid
do {
if (*it == tempDefElem)
--it;
else
break;
} while (it != data.begin());
if (it == data.begin()) // we'll not delete all the elements
++it;
data.erase(it, data.end());
}
StorageT data;
};
template<typename T>
inline
bool operator==(Polynomial<T> const & lhs, Polynomial<T> const & rhs) {
const_cast<Polynomial<T> &>(lhs).normalization();
const_cast<Polynomial<T> &>(rhs).normalization();
// yep, I know, that every time I use const_cast God kills a kitten
return lhs.data == rhs.data;
}
template<typename T>
inline
bool operator!=(Polynomial<T> const & lhs, Polynomial<T> const & rhs) {
return !(lhs == rhs);
}
/// \cond
/*
* Template recursion for defining polynomial variables counter continued.
*/
template<typename T>
class VarCnt< Polynomial<T> > {
public:
static const int result = Polynomial<T>::VAR_CNT;
};
/// \endcond
/** We need generic subscript utility as with Polynomial::operator[] we
* can't use template specialization (the rule for template members of
* template class: specialize member only for enclosing class specialization
* cf. [VJ, 12.3.3]).
*
* The point type here (Pt) is actually either Point or ConstSlice, as opposed to
* Polynomial::operator[] we don't handle the variants separately because it's
* (some kind) hidden implementation detail — with Polynomial::operator[] we
* wanna be as clear as we can, so parameteres types are as strict as they
* could be.
*
* @param el the object to be subscripted
* @param pt generic subscription index. The function could be tuned for speical
* types of \c pt.
* @return result of suscript operator applied to \c el at \c pt.
*/
template<typename T, typename S, typename Pt>
T applySubscript(S const & el, Pt const & pt) {
return el[pt];
}
/// When slice is maximal ConstSlice<Dim, Dim - 1> stop slicing a point.
template<typename T, typename S, int Dim, typename Body>
T applySubscript(S const & el, ConstSlice<Body, Dim, Dim - 1> const & pt) {
return el[pt[0]];
}
/**
* Handling ConstSlice<Dim, Dim>. Actually this type is illegal
* (in ConstSlice<Dim, Offset> we should have Dim > Offset). If we get here, it means
* client used MVPolyType<1, T>::operator[](Point<1>) instead of operator[](int).
* But client is always right so we cope with this correctly.
*/
template<typename T, typename S, int Dim, typename Body>
T applySubscript(S const & el, ConstSlice<Body, Dim, Dim> const & pt) {
return el;
}
template<typename T>
template<template <typename PointImpl> class OrderPolicy>
typename Polynomial<T>::CoefT
Polynomial<T>::operator[](Point<VAR_CNT, OrderPolicy> const & pt) const {
if (pt[0] < (int)0 || (int)data.size() <= pt[0])
return CoefficientTraits<CoefT>::addId();
else
return applySubscript<Polynomial<T>::CoefT>(
data[pt[0]], make_slice<VAR_CNT>(pt));
}
template<typename T>
template<typename Body, int Dim, int Offset>
typename Polynomial<T>::CoefT
Polynomial<T>::operator[](ConstSlice<Body, Dim, Offset> const & sl) const {
if (sl[0] < int(0) || int(data.size()) <= sl[0])
return CoefficientTraits<CoefT>::addId();
else
return applySubscript<Polynomial<T>::CoefT>(data[sl[0]], make_slice(sl));
}
template<typename T>
typename Polynomial<T>::CoefT
Polynomial<T>::operator[](int pt) const {
if (pt < (int)0 || (int)data.size() <= pt)
return CoefficientTraits<CoefT>::addId();
else
return data[pt];
}
/**
* Convolution-like operation for polynomials with immediate return of \c m\ -th
* compoment (in general the result of convolution of two sequences is a
* sequence also). We need a hint where to stop convoluting our two polynomial/
* sequences — \c degf plays this role.
* @param f One polynomial to convolute.
* @param u Second polynomial to convolute.
* @param degf The hint where to stop convolute.
* @param m The index of resulting polynomial/sequence that we — only
* virtually — get with our convolution operation.
* @return
*/
template<typename ResT, typename SeqT1, typename SeqT2, typename PointT>
inline
ResT
conv(
SeqT1 /*const*/ & f,
SeqT2 /*const*/ & u,
PointT const & degf,
PointT const & m) {
assert( byCoordinateLess(degf, m) );
ResT res;
PointT i;
while(i <= degf) {
res += f[i] * u[i + m - degf];
++i;
}
return res;
}
/**
* conv overloading using nested typedef of SeqT1 (mapped_type) instead of
* extra template parameter as in general version of conv (see above).
*/
template<typename SeqT1, typename SeqT2, typename PointT>
inline
typename SeqT1::mapped_type
conv(
SeqT1 /*const*/ & f,
SeqT2 /*const*/ & u,
PointT const & degf,
PointT const & m) {
return conv<typename SeqT1::mapped_type>(f, u, degf, m);
}
/**
* Input polynomial from stream, format: [[a b c][e f]].
* @param[in,out] is Stream which contains data for creating polynomial.
* @param[out] p Polynomial to store.
* @return \c is (conventionally).
*/
template <typename T>
std::istream& operator>>(std::istream& is, Polynomial<T> & p) {
char c = 0;
is >> c;
if ('[' != c) {
is.setstate(std::ios::failbit);
return is;
}
typename Polynomial<T>::StorageT tempStorage;
typename Polynomial<T>::ElemT el;
while ( is.peek() != ']' && is ) {
is >> el;
tempStorage.push_back(el);
}
if (is) {
is.ignore(); // ignore trailing ']'
if (!tempStorage.empty())
p.setCoefs(tempStorage);
else
p = Polynomial<T>();
}
return is;
}
/**
* Loading polynomial from the string.
* @param[out] p Polynomial instance to get in loaded data.
* @param[in] s String that defines contents of polynomial to de loaded.
*/
template<typename T>
void loadPolyFromString( Polynomial<T> & p, std::string const & s ) {
std::istringstream is(s);
is >> p;
}
/**
* Generic output for mv-polynomials, format: [[a b c][e f]].
* @param[out] os Target output stream.
* @param[in] p Polynomial to be outputed in \c os.
* @return Output stream \c os after polynomial \c p have been
* outputed to \c os (conventionally).
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, Polynomial<T> const & p) {
if (p.getCoefs().empty()) {
os << "[]";
return os;
}
os << '[';
std::copy(p.getCoefs().begin(), --(p.getCoefs().end()),
std::ostream_iterator<T>(os, " "));
os << *(--(p.getCoefs().end()));
os << ']';
return os;
}
template<typename T>
std::string toString(Polynomial<T> const & p) {
std::ostringstream os;
os << p;
return os.str();
}
template<typename T>
std::string mapToStr(Polynomial<T> const & p) {
return toString(p);
}
/**
* Multiply polynomial on a scalar.
* @param c Scalar to multiply on.
* @param p Polynomial to be multiplied.
* @return This polynomial multiplyed on \c c.
*/
template<typename T>
inline
Polynomial<T> operator*(typename Polynomial<T>::CoefT const & c, Polynomial<T> p) {
// p — possible pass-by-copy optimization
return p *= c;
}
/**
* Multiply polynomial on a scalar.
* @param p Polynomial to be multiplied.
* @param c Scalar to multiply on.
* @return This polynomial multiplyed on \c c.
*/
template<typename T>
inline
Polynomial<T> operator*(Polynomial<T> p, typename Polynomial<T>::CoefT const & c) {
return p *= c;
}
/**
* The purpose of the function is essentially the same as with applySubscript:
* to pull structure recursion on <tt>Polynomial<… Polynomial<T>… ></tt> type
* deeper, in this case — applying operator<<= on the way.
* @param elem
* @param monomial
*/
template<typename T, typename Pt>
inline
void applyMonomialMultiplication(T & elem, Pt const & monomial) {
elem <<= monomial;
}
template<typename T, typename Body, int Dim>
inline
void applyMonomialMultiplication(
T & elem,
ConstSlice<Body, Dim, Dim - 1> const & monomial) {
elem <<= monomial[0];
}
template<typename T, typename Body, int Dim>
inline
void applyMonomialMultiplication(
T & elem,
ConstSlice<Body, Dim, Dim> const & monomial) {
return;
}
template<typename T>
template<template <typename PointImpl> class OrderPolicy>
inline
Polynomial<T> Polynomial<T>::operator<<=(Point<VAR_CNT, OrderPolicy> const & monomial) {
for (typename StorageT::iterator it = data.begin(); it != data.end(); ++it)
applyMonomialMultiplication(*it, make_slice<VAR_CNT>(monomial));
std::fill_n(std::front_inserter(data), monomial[0], ElemT());
return *this;
}
template<typename T>
template<typename Body, int Dim, int Offset>
inline
Polynomial<T>
Polynomial<T>::operator<<=(ConstSlice<Body, Dim, Offset> const & monomial) {
using std::tr1::bind;
using std::tr1::placeholders::_1;
for (typename StorageT::iterator it = data.begin(); it != data.end(); ++it)
applyMonomialMultiplication(*it, make_slice(monomial));
std::fill_n(std::front_inserter(data), monomial[0], ElemT());
return *this;
}
template<typename T>
inline
Polynomial<T> Polynomial<T>::operator<<=(int monomial) {
std::fill_n(std::front_inserter(data), monomial, ElemT());
return *this;
}
/**
* Multiply polynomial on a monomial represented by its degree
* aimed for use with multivariate polynomials. If the polynomial has
* VAR_CNT variables <tt>x = x_1 x_2 … x_{VAR_CNT}</tt>, then
* <tt>Point<VAR_CNT> m</tt> represents monomial
* <tt>x^m = x_1^{m_1} x_2^{m_2} … x_{VAR_CNT}^{m_{VAR_CNT}}</tt>
* @param p Polynomial to be multiplied.
* @param c Monomial x^{\c m} to multiply on.
* @return This polynomial multiplyed on \c x^{\c m}.
*/
template<typename T, template <typename PointImpl> class OrderPolicy>
inline
Polynomial<T> operator<<(
Polynomial<T> p,
Point<Polynomial<T>::VAR_CNT, OrderPolicy> const & m) {
return p <<= m;
}
/**
* Multiply polynomial on a monomial represented by its degree
* aimed for use with “plain” (univariate) polynomials.
* @param p Polynomial to be multiplied.
* @param m x^{\c m} to multiply on.
* @return This polynomial multiplyed on \c x^{\c m}.
*/
template<typename T>
inline
Polynomial<T> operator<<(Polynomial<T> p, int m) {
return p <<= m;
}
/*template<typename T>
Polynomial<T> Polynomial<T>::operator+=(Polynomial<T> const & p) {
int degDiff = (this->data).size() - p.data.size();
if (degDiff < 0) { // if p has greater (“one-dimensional”) degree then
// first: copy tail of p to this
typename StorageT::const_iterator commonPartDelimeter(p.data.begin());
std::advance(commonPartDelimeter, data.size());
std::copy(commonPartDelimeter, p.data.end(), std::back_inserter(data));
// second: add the rest of p to this
typename StorageT::iterator itThis = data.begin();
for (
typename StorageT::const_iterator itP = p.data.begin();
itP != commonPartDelimeter;
++itThis, ++itP) {
*itThis += *itP;
}
} else { // if this polynomial has greater degree then
typename StorageT::iterator itThis = data.begin();
for (
typename StorageT::const_iterator itP = p.data.begin();
itP != p.data.end();
++itThis, ++itP) {
*itThis += *itP;
}
}
return *this;
}*/
template<typename T>
Polynomial<T> Polynomial<T>::operator+=(Polynomial<T> const & p) {
int degDiff = (this->data).size() - p.data.size();
typename StorageT::const_iterator commonPartDelimeter;
if (degDiff < 0) {
commonPartDelimeter = p.data.begin();
std::advance(commonPartDelimeter, data.size());
std::copy(commonPartDelimeter, p.data.end(), std::back_inserter(data));
} else {
commonPartDelimeter = p.data.end();
}
typename StorageT::iterator itThis = data.begin();
for (
typename StorageT::const_iterator itP = p.data.begin();
itP != commonPartDelimeter;
++itThis, ++itP) {
*itThis += *itP;
}
return *this;
}
template<typename T>
inline
Polynomial<T> operator+(Polynomial<T> lhs, Polynomial<T> const & rhs) {
return lhs += rhs;
}
template<typename T>
Polynomial<T> Polynomial<T>::operator-=(Polynomial<T> const & p) {
return (*this +=
CoefficientTraits<CoefT>::addInverse(
CoefficientTraits<CoefT>::multId())
* p);
}
template<typename T>
inline
Polynomial<T> operator-(Polynomial<T> lhs, Polynomial<T> const & rhs) {
return lhs -= rhs;
}
template<typename Pt, typename T, typename ResultT>
ResultT
polyToDegCoefMapImpl(
Polynomial<T> const & poly,
Pt deg,
ResultT & result) {
BOOST_FOREACH(typename Polynomial<T>::CoefT const & cf, poly.getCoefs()) {
result[deg] = cf;
++deg[0];
}
deg[0] = 0;
return result;
}
template<typename Pt, typename T, typename ResultT>
ResultT
polyToDegCoefMapImpl(
Polynomial< Polynomial <T> > const & poly,
Pt deg,
ResultT & result) {
BOOST_FOREACH(typename Polynomial< Polynomial <T> >::ElemT const & cf, poly.getCoefs()) {
polyToDegCoefMapImpl(cf, make_slice(deg), result);
++deg[0];
}
deg[0] = 0;
return result;
}
template<template <typename> class OrderPolicy, typename T>
std::map<
Point<Polynomial<T>::VAR_CNT, OrderPolicy>,
typename Polynomial<T>::CoefT>
polyToDegCoefMap(Polynomial<T> const & poly) {
typedef Point<Polynomial<T>::VAR_CNT, OrderPolicy> Pt;
Pt deg;
std::map<Pt, typename Polynomial<T>::CoefT> result;
return polyToDegCoefMapImpl(poly, deg, result);
}
template<typename PolyT>
PolyT load_coefs(typename PolyT::CoefT const * coefs[], size_t len, size_t * coefsLens) {
typename PolyT::StorageT stor;
typedef typename PolyT::ElemT ElemT;
for (size_t i = 0; i < len; ++i) {
ElemT elem;
elem.setCoefs(typename ElemT::StorageT(
coefs[i], coefs[i] + coefsLens[i]));
stor.push_back(elem);
}
PolyT res;
res.setCoefs(stor);
return res;
}
template<typename T>
typename Polynomial<T>::CoefT
inline
Polynomial<T>::operator()(typename Polynomial<T>::CoefT const & c) const {
auto f = [&c](CoefT const & val, CoefT const & a) {
return val*c + a;
};
return std::accumulate(data.rbegin(), data.rend(),
CoefficientTraits<CoefT>::addId(), f);
}
template<typename T>
template<typename CurvePoint>
inline
typename Polynomial<T>::CoefT
Polynomial<T>::operator()(CurvePoint const & cp) const {
auto f = [&cp](CoefT const & val, T const & a) {
return val*cp[0] + a(make_slice<Polynomial::VAR_CNT>(cp));
};
return std::accumulate(data.rbegin(), data.rend(),
CoefficientTraits<CoefT>::addId(), f);
}
template<typename T>
template<int Dim, typename Body>
inline
typename Polynomial<T>::CoefT
Polynomial<T>::operator()(ConstSlice<Body, Dim, Dim - 1> const & sl) const {
return (*this)(sl[0]);
}
} // namespace mv_poly
#endif /* MV_POLY_HPP_ */