realroot_doc 0.1.1
|
00001 # ifndef realroot_polynomial_fcts_hpp 00002 # define realroot_polynomial_fcts_hpp 00003 # include <sstream> 00004 # include <realroot/binomials.hpp> 00005 # include <realroot/Seq.hpp> 00006 //==================================================================== 00007 # define TMPL template <class C, class Rep, class Ord> 00008 # define VARIANT with<Rep,Ord> 00009 # define Polynomial polynomial<C,VARIANT> 00010 //==================================================================== 00011 namespace mmx { 00012 #ifndef WITH_POW 00013 #define WITH_POW 00014 template<class T> 00015 T pow(const T& a, int i) { 00016 assert(i>=0); 00017 if(i == 1) return a; 00018 if(i > 0) 00019 { 00020 if (a == T(1)) return a; 00021 if (a == T(-1)) return (i % 2 == 0) ? T(1) : T(-1); 00022 T y(a); 00023 T z(1); 00024 unsigned int n = i; 00025 unsigned int m; 00026 while (n > 0) { 00027 m = n / 2; 00028 if (n %2 ) 00029 { 00030 z *= y; 00031 } 00032 y = y * y; 00033 n = m; 00034 } 00035 return z; 00036 } 00037 return T(1); 00038 } 00039 #endif //WITH_POW 00040 //====================================================================== 00041 TMPL inline int 00042 nbvar(const Polynomial& mp ) { return mp.nbvar(); } 00043 00044 TMPL inline int 00045 degree(const Polynomial& mp ) { return degree(mp.rep()); } 00046 00047 TMPL inline int 00048 degree(const Polynomial& mp, int v ) { 00049 return degree(mp.rep(),v); 00050 } 00051 TMPL inline unsigned 00052 size(const Polynomial& p) { 00053 return p.size(); 00054 } 00055 00056 TMPL std::ostream& 00057 operator<<(std::ostream& os, const Polynomial& mp ) { 00058 print(os, mp.rep(), Polynomial::Ring::vars()); return os; 00059 } 00060 00061 template<class OSTREAM, class C, class Rep, class Ord> inline OSTREAM& 00062 print(OSTREAM& os, const Polynomial& mp) { 00063 print(os,mp.rep()); return os; 00064 } 00065 00066 template<class OSTREAM, class C, class Rep, class Ord> inline OSTREAM& 00067 print(OSTREAM& os, const Polynomial& mp, const variables& Var) { 00068 print(os,mp.rep(),Var); return os; 00069 } 00070 00071 00072 TMPL std::string 00073 as_string(const Polynomial & p){ 00074 std::ostringstream s; 00075 print(s,p); 00076 return s.str(); 00077 } 00078 00079 TMPL std::string 00080 as_string(const Polynomial & p,const variables& var){ 00081 std::ostringstream s; 00082 print(s,p,var); 00083 return s.str(); 00084 } 00085 00086 00087 //-------------------------------------------------------------------- 00093 TMPL inline Polynomial 00094 diff(const Polynomial& pol, int v ) { 00095 Polynomial der; diff(der.rep(),pol.rep(),v); return der; 00096 } 00097 00098 //-------------------------------------------------------------------- 00104 TMPL inline Polynomial 00105 diff(const Polynomial& pol) { 00106 return diff(pol,0);//leading_variable(p.rep())); 00107 } 00108 00109 TMPL inline Seq<C> 00110 coefficients(const Polynomial& pol) { 00111 return coefficients(pol.rep()); 00112 } 00113 00114 TMPL inline Seq<Polynomial> 00115 coefficients(const Polynomial& pol, int v) { 00116 typedef typename Polynomial::Ring Ring; 00117 Seq<Polynomial> res; 00118 coefficients(res,pol.rep(),v); 00119 return res; 00120 } 00121 00128 template<class Result, class C, class Rep, class Ord, class Parameters> inline 00129 Result eval(const Polynomial& polynomial, const Parameters& parameters ) { 00130 Result result; 00131 eval(result, polynomial.rep(), parameters ); 00132 return result; 00133 } 00134 00135 template<class Result, class C, class Rep, class Ord, class Parameters> inline 00136 Result eval(const Polynomial& polynomial, const Parameters& parameters, int n ) { 00137 Result result; 00138 eval(result, polynomial.rep(), parameters, n ); 00139 return result; 00140 } 00141 00142 template<class Result, class C, class Rep, class Ord, class Parameters> inline void 00143 eval(Result& result, const Polynomial& polynomial, const Parameters& parameters ) { 00144 eval(result, polynomial.rep(), parameters ); 00145 } 00146 //-------------------------------------------------------------------- 00147 TMPL inline Polynomial 00148 homogenize(const Polynomial& p, const Polynomial& v) { 00149 Polynomial r; homogenize(r.rep(),p.rep(),v.rep()); return r; 00150 } 00151 00152 TMPL inline Polynomial 00153 homogenize(const Polynomial& p, int i, const Polynomial& v) { 00154 Polynomial r; homogenize(r.rep(),p.rep(),i,v.rep()); return r; 00155 } 00156 00157 00158 //====================================================================== 00159 namespace let { 00160 template<class C1, class R1, class C2, class R2> inline void 00161 assign(polynomial<C1, R1>& p, const polynomial<C2, R2>& q) { 00162 assign(p.rep(),q.rep()); 00163 } 00164 00165 template<class C1, class R1, class DOM, class C2, class R2> inline void 00166 assign(polynomial<C1, R1>& p, const polynomial<C2, R2>& q, const DOM& dmn) { 00167 assign(p.rep(),q.rep(), dmn); 00168 } 00169 } 00170 00171 template<class POL1, class C2, class R2, class V2> 00172 inline POL1 00173 as(const polynomial<C2, with<R2,V2> >& p) { 00174 POL1 res; 00175 let::assign(res,p); 00176 return res; 00177 } 00178 //====================================================================== 00179 00180 namespace tensor { 00181 TMPL void 00182 face(Polynomial& r, const Polynomial& p,int v, int f) { 00183 face(r.rep(),p.rep(),v,f); 00184 } 00185 00186 TMPL inline void 00187 split(Polynomial& r, Polynomial& p, int v) { 00188 split(r.rep(),p.rep(),v); 00189 } 00190 00191 } 00192 //====================================================================== 00193 #ifndef WITH_BINOMIAL_POWER 00194 #define WITH_BINOMIAL_POWER 00195 00200 template < class MP > MP 00201 binomial( typename MP::coeff_t coeff, int i, int d, typename MP::coeff_t a) { 00202 MP polynomial; 00203 typename MP::coeff_t tmp; 00204 for ( int k = 0 ; k <= d ; k++ ) 00205 { 00206 tmp = coeff * binomials<typename MP::coeff_t>()(d, k) * pow(a,d-k); 00207 polynomial += MP( typename MP::monom_t( tmp , i, k ) ); 00208 } 00209 return polynomial; 00210 } 00211 #endif //WITH_BINOMIAL_POWER 00212 00213 //====================================================================== 00215 TMPL inline void 00216 shift( Polynomial & f, const C & t, const int & v ) { 00217 shift( f.rep(), t, v ); 00218 } 00219 00221 TMPL inline void 00222 shift( Polynomial & r, const Polynomial & p, int d, int v=0) { 00223 shift( r.rep(), p.rep(), d, v ); 00224 } 00225 //====================================================================== 00227 TMPL inline void 00228 reciprocal( Polynomial & f, const int & v ) { 00229 reciprocal( f.rep(), v ); 00230 } 00231 00232 00233 TMPL typename Polynomial::Scalar 00234 content(const Polynomial & p) { 00235 return content(p.rep()); 00236 } 00237 00238 //====================================================================== 00239 } //namespace mmx 00240 #undef Polynomial 00241 #undef TMPL 00242 #endif //realroot_polynomial_fcts_hpp