Developer documentation

polynomial_fcts.hpp
Go to the documentation of this file.
1 # ifndef realroot_polynomial_fcts_hpp
2 # define realroot_polynomial_fcts_hpp
3 # include <sstream>
4 # include <realroot/binomials.hpp>
5 # include <realroot/variables.hpp>
6 # include <realroot/Seq.hpp>
7 //====================================================================
8 # define TMPL template <class C, class Rep, class Ord>
9 # define VARIANT with<Rep,Ord>
10 # define Polynomial polynomial<C,VARIANT>
11 //====================================================================
12 namespace mmx {
13 #ifndef WITH_POW
14 #define WITH_POW
15  template<class T>
16  T pow(const T& a, int i) {
17  assert(i>=0);
18  if(i == 1) return a;
19  if(i > 0)
20  {
21  if (a == T(1)) return a;
22  if (a == T(-1)) return (i % 2 == 0) ? T(1) : T(-1);
23  T y(a);
24  T z(1);
25  unsigned int n = i;
26  unsigned int m;
27  while (n > 0) {
28  m = n / 2;
29  if (n %2 )
30  {
31  z *= y;
32  }
33  y = y * y;
34  n = m;
35  }
36  return z;
37  }
38  return T(1);
39  }
40 #endif //WITH_POW
41 //======================================================================
42 TMPL inline int
43 nbvar(const Polynomial& mp ) { return mp.nbvar(); }
44 
45 TMPL inline int
46 degree(const Polynomial& mp ) { return degree(mp.rep()); }
47 
48 TMPL inline int
49 degree(const Polynomial& mp, int v ) {
50  return degree(mp.rep(),v);
51 }
52 TMPL inline unsigned
53 size(const Polynomial& p) {
54  return p.size();
55 }
56 
57 TMPL std::string
58 to_string(const Polynomial& mp, const variables& var) {
59  return to_string(mp.rep(),var);
60 }
61 
62 TMPL std::ostream&
63 operator<<(std::ostream& os, const Polynomial& mp ) {
64  print(os, mp.rep(), Polynomial::Ring::vars()); return os;
65 }
66 
67  template<class OSTREAM, class C, class Rep, class Ord> inline OSTREAM&
68 print(OSTREAM& os, const Polynomial& mp) {
69  print(os,mp.rep()); return os;
70 }
71 
72 template<class OSTREAM, class C, class Rep, class Ord> inline OSTREAM&
73 print(OSTREAM& os, const Polynomial& mp, const variables& Var) {
74  print(os,mp.rep(),Var); return os;
75 }
76 
77 
78 TMPL std::string
79 as_string(const Polynomial & p){
80  std::ostringstream s;
81  print(s,p);
82  return s.str();
83 }
84 
85 TMPL std::string
86 as_string(const Polynomial & p,const variables& var){
87  std::ostringstream s;
88  print(s,p,var);
89  return s.str();
90 }
91 
92 
93 //--------------------------------------------------------------------
98 TMPL inline Polynomial
99 diff(const Polynomial& pol, int v ) {
100  Polynomial der; diff(der.rep(),pol.rep(),v); return der;
101 }
102 
103 //--------------------------------------------------------------------
108 TMPL inline Polynomial
109 diff(const Polynomial& pol) {
110  return diff(pol,0);//leading_variable(p.rep()));
111 }
112 
113 TMPL inline Seq<C>
115  return coefficients(pol.rep());
116 }
117 
118 TMPL inline Seq<Polynomial>
119 coefficients(const Polynomial& pol, int v) {
120  //typedef typename Polynomial::Ring Ring;
121  Seq<Polynomial> res;
122  coefficients(res,pol.rep(),v);
123  return res;
124 }
125 
134 template<class Result, class C, class Rep, class Ord, class Parameters> inline
135 Result eval(const Polynomial& polynomial, const Parameters& parameters ) {
136  Result result;
137  eval(result, polynomial.rep(), parameters );
138  return result;
139 }
140 
141 template<class Result, class C, class Rep, class Ord, class Parameters> inline
142 Result eval(const Polynomial& polynomial, const Parameters& parameters, int n ) {
143  Result result;
144  eval(result, polynomial.rep(), parameters, n );
145  return result;
146 }
147 
148 template<class Result, class C, class Rep, class Ord, class Parameters> inline void
149 eval(Result& result, const Polynomial& polynomial, const Parameters& parameters ) {
150  eval(result, polynomial.rep(), parameters );
151 }
152 //--------------------------------------------------------------------
153 TMPL inline Polynomial
154 homogenize(const Polynomial& p, const Polynomial& v) {
155  Polynomial r; homogenize(r.rep(),p.rep(),v.rep()); return r;
156 }
157 
158 TMPL inline Polynomial
159 homogenize(const Polynomial& p, int i, const Polynomial& v) {
160  Polynomial r; homogenize(r.rep(),p.rep(),i,v.rep()); return r;
161 }
162 
163 
164 //======================================================================
165 namespace let {
166  template<class C1, class R1, class C2, class R2> inline void
168  assign(p.rep(),q.rep());
169  }
170 
171  template<class C1, class R1, class DOM, class C2, class R2> inline void
172  assign(polynomial<C1, R1>& p, const polynomial<C2, R2>& q, const DOM& dmn) {
173  assign(p.rep(),q.rep(), dmn);
174  }
175 }
176 
177 template<class POL1, class C2, class R2, class V2>
178 inline POL1
179 as(const polynomial<C2, with<R2,V2> >& p) {
180  POL1 res;
181  let::assign(res,p);
182  return res;
183 }
184 //======================================================================
185 
186 namespace tensor {
187  TMPL void
188  face(Polynomial& r, const Polynomial& p,int v, int f) {
189  face(r.rep(),p.rep(),v,f);
190  }
191 
192  TMPL inline void
193  split(Polynomial& r, Polynomial& p, int v) {
194  split(r.rep(),p.rep(),v);
195  }
196 
197 }
198 //======================================================================
199 #ifndef WITH_BINOMIAL_POWER
200 #define WITH_BINOMIAL_POWER
201 
206 template < class MP > MP
207 binomial( typename MP::coeff_t coeff, int i, int d, typename MP::coeff_t a) {
208  MP polynomial;
209  typename MP::coeff_t tmp;
210  for ( int k = 0 ; k <= d ; k++ )
211  {
212  tmp = coeff * binomials<typename MP::coeff_t>()(d, k) * pow(a,d-k);
213  polynomial += MP( typename MP::monom_t( tmp , i, k ) );
214  }
215  return polynomial;
216 }
217 #endif //WITH_BINOMIAL_POWER
218 
219 //======================================================================
221 TMPL inline void
222 shift( Polynomial & f, const C & t, const int & v ) {
223  shift( f.rep(), t, v );
224 }
225 
227 TMPL inline void
228 shift( Polynomial & r, const Polynomial & p, int d, int v=0) {
229  shift( r.rep(), p.rep(), d, v );
230 }
231 //======================================================================
233 TMPL inline void
234 reciprocal( Polynomial & f, const int & v ) {
235  reciprocal( f.rep(), v );
236 }
237 
238 
239 TMPL typename Polynomial::Scalar
240 content(const Polynomial & p) {
241  return content(p.rep());
242 }
243 
244 //======================================================================
245 } //namespace mmx
246 #undef Polynomial
247 #undef TMPL
248 #endif //realroot_polynomial_fcts_hpp
Sequence of terms with reference counter.
Definition: Seq.hpp:28
TMPL void reciprocal(Polynomial &f, const int &v)
Definition: polynomial_fcts.hpp:234
T pow(const T &a, int i)
Definition: binomials.hpp:12
TMPL Polynomial diff(const Polynomial &pol, int v)
Multivariate Polynomial Differentiation.
Definition: polynomial_fcts.hpp:99
dynamic_exp< E >::degree_t degree(const dynamic_exp< E > &t)
Definition: dynamicexp.hpp:191
void shift(IntervalData< RT, Poly > &ID, const RT &a)
Definition: contfrac_intervaldata.hpp:257
std::ostream & operator<<(std::ostream &os, const dynamic_exp< E > &t)
Definition: dynamicexp.hpp:199
Result eval(const Polynomial &polynomial, const Parameters &parameters)
Multivariate Polynomial Evaluation.
Definition: polynomial_fcts.hpp:135
std::string to_string(int x)
to_string convert int to std::string In C++11, it should use std::to_string
Definition: assign.hpp:24
polynomial< COEFF, with< MonomialTensor > > Polynomial
Definition: solver_mv_cf.cpp:23
T as(const F &x)
Definition: assign.hpp:51
#define TMPL
Definition: polynomial_fcts.hpp:8
TMPL void face(Polynomial &r, const Polynomial &p, int v, int f)
Definition: polynomial_fcts.hpp:188
Definition: polynomial.hpp:37
ZZ size(const ZZ &z)
Definition: GMPXX.hpp:67
TMPL void split(Polynomial &r, Polynomial &p, int v)
Definition: polynomial_fcts.hpp:193
void print(OSTREAM &os, const Interval< T, r > &a)
Definition: Interval.hpp:135
Definition: binomials.hpp:68
#define Scalar
Definition: polynomial_operators.hpp:12
R & rep()
Definition: Seq.hpp:62
TMPL std::string as_string(const Polynomial &p)
Definition: polynomial_fcts.hpp:79
double C
Definition: solver_mv_fatarcs.cpp:16
TMPL Polynomial::Scalar content(const Polynomial &p)
Definition: polynomial_fcts.hpp:240
int Var(const T &v)
Definition: sign_variation.hpp:161
TMPL int nbvar(const Polynomial &mp)
Definition: polynomial_fcts.hpp:43
void assign(A &a, const B &b)
Generic definition of the assignement function.
Definition: assign.hpp:97
TMPL Polynomial homogenize(const Polynomial &p, const Polynomial &v)
Definition: polynomial_fcts.hpp:154
Definition: array.hpp:12
Definition: variables.hpp:65
Definition: polynomial.hpp:40
TMPL Seq< typename ring< C, Bernstein >::Polynomial > coefficients(const typename ring< C, Bernstein >::Polynomial &pol, int v)
Definition: polynomial_bernstein.hpp:74
T binomial(int n, int p)
Definition: binomials.hpp:52
#define assert(expr, msg)
Definition: shared_object.hpp:57
Home