realroot_doc 0.1.1
/Users/mourrain/Devel/mmx/realroot/include/realroot/polynomial_operators.hpp
Go to the documentation of this file.
00001 #ifndef realroot_polynom_operators_hpp
00002 #define realroot_polynom_operators_hpp
00003 //====================================================================
00004 #include <realroot/texp_sup.hpp>
00005 #include <realroot/texp_expression.hpp>
00006 #include <realroot/texp_operators.hpp>
00007 
00008 # define TMPL       template <class C, class Rep, class Ord>
00009 # define TMPLX      template <class C, class Rep, class Ord, class X>
00010 # define VARIANT    with<Rep,Ord>
00011 # define POLYNOMIAL polynomial<C,VARIANT>
00012 # define Scalar     C
00013 //====================================================================
00014 namespace mmx {
00015 
00016 TMPL inline void 
00017 add (POLYNOMIAL &r, const POLYNOMIAL &a ) { 
00018   add (r.rep (), a.rep () ); 
00019 }
00020 TMPL inline void 
00021 add (POLYNOMIAL &r, const POLYNOMIAL &a, const POLYNOMIAL &b) {
00022   add (r.rep (), a.rep (), b.rep ()); 
00023 }
00024 TMPL inline void
00025 add (POLYNOMIAL &r, const POLYNOMIAL &a, const C & b) {
00026   add (r.rep (), a.rep (), b); 
00027 }
00028 TMPL inline void
00029 add (POLYNOMIAL &r, const C & a, const POLYNOMIAL &b) {
00030   add (r.rep(), b.rep(), a); 
00031 }
00032 TMPL inline void
00033 sub (POLYNOMIAL &r, const POLYNOMIAL &a ) {
00034   sub (r.rep (), a.rep () ); 
00035 }
00036 TMPL inline void
00037 sub (POLYNOMIAL &r, const POLYNOMIAL &a, const POLYNOMIAL &b) {
00038   sub (r.rep (), a.rep (), b.rep ()); 
00039 }
00040 TMPL inline void
00041 sub (POLYNOMIAL &r, const C & a, const POLYNOMIAL &b) {
00042   sub (r.rep (), a, b.rep ()); 
00043 }
00044 TMPL inline void
00045 sub (POLYNOMIAL &r, const POLYNOMIAL &a, const C & b) {
00046   sub (r.rep (), a.rep (), b); 
00047 }
00048 TMPL inline void
00049 mul (POLYNOMIAL &r, const POLYNOMIAL &a ) {
00050   mul (r.rep (), a.rep () ); 
00051 }
00052 TMPL inline void
00053 mul (POLYNOMIAL &r, const POLYNOMIAL &a, const POLYNOMIAL &b) {
00054   mul (r.rep (), a.rep (), b.rep ()); 
00055 }
00056 TMPL inline void
00057 mul (POLYNOMIAL &r, const POLYNOMIAL &a, const C & b) {
00058   mul (r.rep (), a.rep (), b); }
00059 TMPL inline void
00060 mul (POLYNOMIAL &r, const C & a, const POLYNOMIAL &b) {
00061   mul (r.rep (), b.rep (), a); 
00062 }
00063 TMPL inline void
00064 div (POLYNOMIAL &r, const POLYNOMIAL &a, const POLYNOMIAL &b) {
00065   div (r.rep (), a.rep (), b.rep ()); 
00066 }
00067 TMPL inline void
00068 div (POLYNOMIAL &r, const POLYNOMIAL &a, const C & b) {
00069   div (r.rep (), a.rep (), b); 
00070 }
00071 TMPL inline void
00072 rem (POLYNOMIAL &r, const POLYNOMIAL &a, const POLYNOMIAL &b) {
00073   rem (r.rep (), b.rep (), a.rep); 
00074 }
00075 //====================================================================
00076 struct operators_of {};
00077 TMPL struct use<operators_of,POLYNOMIAL> {
00078     static inline void 
00079     add (POLYNOMIAL &r, const POLYNOMIAL &a ) { 
00080       add (r.rep (), a.rep () ); 
00081     }
00082     static inline void 
00083     add (POLYNOMIAL &r, const POLYNOMIAL &a, const POLYNOMIAL &b) {
00084       add (r.rep (), a.rep (), b.rep ()); 
00085     }
00086     static inline void
00087     add (POLYNOMIAL &r, const POLYNOMIAL &a, const C & b) {
00088       add (r.rep (), a.rep (), b); 
00089     }
00090     static inline void
00091     add (POLYNOMIAL &r, const C & a, const POLYNOMIAL &b) {
00092       add (r.rep(), b.rep(), a); 
00093     }
00094     static inline void
00095     sub (POLYNOMIAL &r, const POLYNOMIAL &a ) {
00096       sub (r.rep (), a.rep () ); 
00097     }
00098     static inline void
00099     sub (POLYNOMIAL &r, const POLYNOMIAL &a, const POLYNOMIAL &b) {
00100       sub (r.rep (), a.rep (), b.rep ()); 
00101     }
00102     static inline void
00103     sub (POLYNOMIAL &r, const C & a, const POLYNOMIAL &b) {
00104       sub (r.rep (), a, b.rep ()); 
00105     }
00106     static inline void
00107     sub (POLYNOMIAL &r, const POLYNOMIAL &a, const C & b) {
00108       sub (r.rep (), a.rep (), b); 
00109     }
00110     static inline void
00111     mul (POLYNOMIAL &r, const POLYNOMIAL &a ) {
00112       mul (r.rep (), a.rep () ); 
00113     }
00114     static inline void
00115     mul (POLYNOMIAL &r, const POLYNOMIAL &a, const POLYNOMIAL &b) {
00116       mul (r.rep (), a.rep (), b.rep ()); 
00117 }
00118     static inline void
00119     mul (POLYNOMIAL &r, const POLYNOMIAL &a, const C & b) {
00120       mul (r.rep (), a.rep (), b); }
00121     static inline void
00122     mul (POLYNOMIAL &r, const C & a, const POLYNOMIAL &b) {
00123       mul (r.rep (), b.rep (), a); 
00124     }
00125     static inline void
00126     div (POLYNOMIAL &r, const POLYNOMIAL &a, const POLYNOMIAL &b) {
00127       div (r.rep (), a.rep (), b.rep ()); 
00128     }
00129     static inline void
00130     div (POLYNOMIAL &r, const POLYNOMIAL &a, const C & b) {
00131       div (r.rep (), a.rep (), b); 
00132     }
00133     static inline void
00134     rem (POLYNOMIAL &r, const POLYNOMIAL &a, const POLYNOMIAL &b) {
00135       rem (r.rep (), b.rep (), a.rep); 
00136     }
00137 };
00138 //====================================================================
00139 TMPL inline POLYNOMIAL
00140 operator-(const POLYNOMIAL& p) { 
00141   POLYNOMIAL r(p); mul(r.rep(),(typename POLYNOMIAL::value_type)(-1)); return r;
00142 }
00143 
00144 //----------------------------------------------------------------------
00145 # define OP use<operators_of,typename POLYNOMIAL::rep_t>
00146 TMPL  define_operator_rrr(POLYNOMIAL,POLYNOMIAL,POLYNOMIAL,operator+,OP::add)
00147 TMPLX define_operator_r_r(POLYNOMIAL,X         ,POLYNOMIAL,operator+,OP::add)
00148 TMPLX define_operator_rr_(POLYNOMIAL,POLYNOMIAL,X         ,operator+,OP::add)
00149 
00150 TMPL  define_operator_rrr(POLYNOMIAL,POLYNOMIAL,POLYNOMIAL,operator-,OP::sub)
00151 TMPLX define_operator_rr_(POLYNOMIAL,POLYNOMIAL,X         ,operator-,OP::sub)
00152 TMPLX define_operator_r_r(POLYNOMIAL,X         ,POLYNOMIAL,operator-,OP::sub)
00153 
00154 TMPL  define_operator_rrr(POLYNOMIAL,POLYNOMIAL,POLYNOMIAL,operator*,OP::mul)
00155 TMPLX define_operator_rr_(POLYNOMIAL,POLYNOMIAL,X         ,operator*,OP::mul)
00156 TMPLX define_operator_r_r(POLYNOMIAL,X         ,POLYNOMIAL,operator*,OP::mul)
00157 
00158 TMPL  define_operator_rrr(POLYNOMIAL,POLYNOMIAL,POLYNOMIAL,operator/,OP::div)
00159 TMPLX define_operator_rr_(POLYNOMIAL,POLYNOMIAL,X,operator/,OP::div)
00160 
00161 TMPL  define_operator_rrr(POLYNOMIAL,POLYNOMIAL,POLYNOMIAL,operator%,OP::rem)
00162 # undef OP
00163 //----------------------------------------------------------------------
00164 TMPL POLYNOMIAL 
00165 operator^(const POLYNOMIAL &a, unsigned n) {
00166   return pow (a, n); }
00167 
00168 
00169 TMPL bool 
00170 operator== (const POLYNOMIAL &mp, const C & c) {
00171   return mp.rep() == c; 
00172 }
00173 
00174 TMPL bool 
00175 operator== (const POLYNOMIAL &mp, unsigned n) {
00176   return mp.rep() == (C)n; 
00177 }
00178 
00179 
00180   template<class C, class V, class X> 
00181 bool operator !=( const polynomial<C,V>& p, const X& q ) {return !(p==q);}
00182 
00183 //====================================================================== 
00184 } //namespace mmx
00185 # undef TMPL
00186 # undef TMPLX
00187 # undef VARIANT
00188 # undef POLYNOMIAL
00189 # undef Scalar
00190 //====================================================================
00191 #endif