realroot_doc 0.1.1
/Users/mourrain/Devel/mmx/realroot/include/realroot/solver_univariate_glue.hpp
Go to the documentation of this file.
00001 
00002 
00003 #include <basix/glue.hpp>
00004 #include <basix/vector.hpp>
00005 #include <basix/vector_sort.hpp>
00006 #include <numerix/kernel.hpp>
00007 #include <realroot/Seq.hpp>
00008 #include <realroot/Interval_glue.hpp>
00009 #include <realroot/polynomial_tensor.hpp>
00010 #include <realroot/solver_uv_sleeve.hpp>
00011 #include <realroot/solver_uv_continued_fraction.hpp>
00012 #include <realroot/solver_uv_interval_newton.hpp>
00013 
00014 
00015 #define TMPL        template<class C> 
00016 #define RING        ring<C,MonomialTensor>
00017 #define POLYNOMIAL  polynomial< C, with<MonomialTensor> >
00018 
00019 namespace mmx {
00020 
00021   template<typename FT, typename RT, typename Poly> 
00022   struct as_helper<interval<FT>,IntervalData<RT,Poly> > {
00023     static inline interval<FT>
00024     cv (const IntervalData<RT,Poly>& I) { 
00025       FT left, right;
00026       if ( I.c == RT(0)) 
00027         left = sign(I.a) * bound_root( I.p, Cauchy<FT>());
00028       else 
00029         left = FT(I.a)/I.c;
00030       
00031       if ( I.d == RT(0)) 
00032         right = sign(I.b) * bound_root( I.p, Cauchy<FT>()); 
00033       else 
00034         right = FT(I.b)/I.d;
00035       //      std::cout << I.a<<"/"<<I.c<<", "<<I.b<<"/"<<I.d<<std::endl;
00036       return interval<FT>(left, right); 
00037     }
00038   };
00039 
00040   TMPL vector<generic> 
00041   solver_univariate_contfrac_prec(const POLYNOMIAL & p, const int& prec) {
00042     typedef Numerix                             kernel;
00043     typedef typename kernel::integer            Integer;
00044     typedef typename kernel::rational           Rational;
00045     typedef typename kernel::interval_rational  interval_rational;
00046     typedef solver< Integer, ContFrac<Approximate> > Solver;
00047 
00048     vector<interval_rational> sol;
00049     Solver::set_precision(prec);
00050     Solver::solve(sol,p);
00051     sort(sol);
00052 
00053     vector<generic> r;
00054     for(unsigned i=0;i<sol.size();i++) 
00055       r <<as<generic>(interval< Rational > (lower(sol[i]),upper(sol[i])));
00056     //r <<as<generic>(sol[i]);
00057     return r;
00058   }
00059 
00060   //--------------------------------------------------------------------
00061   TMPL vector<generic> solver_univariate_contfrac(const POLYNOMIAL & p) {
00062     return solver_univariate_contfrac_prec(p,mmx_bit_precision);
00063   }
00064   
00065   //--------------------------------------------------------------------
00066   TMPL vector<generic> 
00067   solver_univariate_contfrac(const POLYNOMIAL & p, const interval<rational>& D) {
00068     typedef Numerix                        kernel;
00069     typedef typename kernel::integer       Integer;
00070     typedef typename kernel::rational      Rational;
00071     typedef typename kernel::interval_rational interval_rational;
00072     typedef solver<Integer, ContFrac<Approximate> >       Solver;
00073 
00074     vector<interval_rational> sol;
00075     Solver::set_precision(mmx_bit_precision);
00076     Solver::solve(sol,p,lower(D),upper(D));
00077     sort(sol);
00078     vector<generic> r;
00079     for(unsigned i=0;i<N(sol);i++) 
00080       r <<as<generic>(interval< rational > (lower(sol[i]),upper(sol[i])));
00081     return r;
00082   }
00083   //--------------------------------------------------------------------
00084 
00085 
00086   TMPL vector<generic> 
00087   solver_univariate_contfrac_approx_prec(const POLYNOMIAL & p, const int& prec) {
00088     typedef Numerix                            kernel;
00089     typedef typename kernel::integer           Integer;
00090     typedef typename kernel::rational          rational;
00091     typedef typename kernel::interval_rational interval_rational;
00092     typedef solver<Integer,ContFrac<Approximate> >  Solver;
00093 
00094 
00095     Solver::set_precision(prec);
00096 
00097     vector<interval_rational> sol;
00098     Solver::solve(sol,p);
00099     sort(sol);
00100     long int old_prec = mmx_bit_precision;
00101     vector<generic> r;
00102     mmx_bit_precision = prec;
00103     for(unsigned i=0;i<sol.size();i++) {
00104       r << as<generic>(as<floating<> >((lower(sol[i])+upper(sol[i]))/2));
00105     }
00106     mmx_bit_precision = old_prec;
00107     return r;
00108   }
00109   //--------------------------------------------------------------------
00110   TMPL vector<generic> 
00111   solver_univariate_contfrac_approx(const POLYNOMIAL & p) {
00112     return solver_univariate_contfrac_approx_prec(p,mmx_bit_precision);
00113   }
00114   //--------------------------------------------------------------------
00115   TMPL vector<generic> 
00116   solver_univariate_sleeve(const POLYNOMIAL & p) {
00117     typedef Numerix                   kernel;
00118     typedef typename kernel::integer  Integer;
00119     typedef typename kernel::rational rational;
00120     typedef typename kernel::interval_rational interval_rational;
00121     typedef kernel::interval_rational interval_rational;
00122     Seq<interval_rational> sol;
00123     solver<RING,Sleeve<Isolate> >::solve(sol,p);
00124     vector<generic> r;
00125     for(unsigned i=0;i<sol.size();i++) r << as<generic>(sol[i]);
00126     return r;
00127   }
00128   //--------------------------------------------------------------------
00129   TMPL interval<floating<> > 
00130   solver_univariate_newton(const POLYNOMIAL & p, const interval<floating<> >& I) {
00131     typedef interval<floating<> > interval_t;
00132     IntervalNewton<interval_t,C> Nwt(I);
00133     Seq<interval_t> sol =solve(p,Nwt);
00134     
00135     return sol[0];
00136   }
00137 
00138 } // namespace mmx
00139 //--------------------------------------------------------------------
00140 #undef TMPL
00141 #undef RING
00142 #undef POLYNOMIAL
00143 
00144