synaps/mpol/bezoutian.h

Go to the documentation of this file.
00001 /*********************************************************************
00002 *      This file is part of the source code of SYNAPS.               *
00003 *      B. Mourrain, GALAAD, INRIA                                    *
00004 *********************************************************************/
00005 #ifndef SYNAPS_RESULTANT_MBEZOUT_H
00006 #define SYNAPS_RESULTANT_MBEZOUT_H
00007 //----------------------------------------------------------------------
00008 #include <list>
00009 #include <map>
00010 #include <synaps/init.h>
00011 #include <synaps/linalg/MatrDse.h>
00012 #include <synaps/linalg/Det.h>
00013 #include <synaps/mpol/MPol.h>
00014 #include <synaps/mpol/MPOLDST.m>
00015 //--------------------------------------------------------------------
00016 __BEGIN_NAMESPACE_SYNAPS
00017 //--------------------------------------------------------------------
00022 //--------------------------------------------------------------------
00029 template <class T>
00030 typename T::value_type Theta(const T & l, typename T::value_type::monom_t::index_t c=0)
00031 {
00032   typedef typename T::value_type     POL;
00033   typedef typename POL::monom_t      monom_t;
00034   typedef typename monom_t::coeff_t  C;
00035   typedef typename monom_t::index_t  index_t;
00036 
00037   index_t n= l.size()-1;
00038 
00039   std::list<POL> ldp;
00040   POL* dx = new POL[n];
00041   for(int i=0; i < n; i++)
00042   {
00043         dx[i] = POL(monom_t(C(1),c+i,1))- POL(monom_t(C(1),c+i+n,1));
00044   }
00045 
00046   MatrDse<POL, linalg::rep2d<POL> > M(n+1,n+1);
00047   int k=0;
00048 
00049   for(typename T::const_iterator p = l.begin(); p != l.end(); ++p){
00050         M(k,0)=  (*p);
00051         POL dp = (*p);
00052         for(typename POL::const_iterator m =(*p).begin(); m!=(*p).end(); m++){
00053           index_t s = m->nvars();
00054           // if the monomial is not zero and the coefficient is not in the ground ring
00055           if(degree(*m) > 0 && s >= c){
00056                 // t will be the monomial in the new variables and has the same coefficient
00057                 monom_t t(m->coeff(),s+n);
00058                 for(index_t j=0; j < c; j++){
00059                   // expoments for the variables in the ground ring are the same
00060                   t.setexpt(j,(*m)[j]);
00061                   // the variables in y_i = x_{i+n} corresponding to a variable in the ground field has exponents zero
00062                   t.setexpt(j+n,0);
00063                 }
00064                 for(index_t j=c; j < s;j++) {
00065                   // exponent of y_i = x_{i+n} in t is the exponent of x_i in *m and the exponent of x_i is zero in t
00066                   t.setexpt(j,0);
00067                   t.setexpt(j+n,(*m)[j]);
00068                 }
00069                 //We substract this monomial to the initial polynomial
00070                 dp-=POL(t);
00071           }else{
00072                 //the monmial must be considered as a constant and removed from the initial polynomial
00073                 dp-=(*m);
00074           }
00075         }
00076         // the divided differences are computed by successive division by x_i - y_i = x_i - y_{i+n}, the coefficient of theta are the successives quotients
00077         for (int i = 0; i<n;i++)
00078         {
00079           MPOLDST::div_rem_x(M(k,i+1), dp, dx[i]);
00080         }
00081         k++;
00082   }
00083   delete [] dx;
00084   // The result is the determinant of the matrix
00085   return Det(M);
00086 }
00087 
00088 
00089 //-------------------------------------------------------------------------
00096 template <class UPOL, class MPOL>
00097 MatrDse<UPOL> UBezout(const MPOL & PP,const MPOL QQ)
00098 {
00099   typedef typename MPOL::monom_t     monom_t;
00100   typedef typename MPOL::order_t     order_t;
00101   typedef typename monom_t::coeff_t coeff_t;
00102 
00103   std::list<MPOL> L;
00104   L.push_back(PP);
00105   L.push_back(QQ);
00106   MPOL P=Theta(L,1);
00107 
00108   typename MPOL::const_iterator i = P.begin();
00109 
00110   int taille= std::max(degree(PP,1),degree(QQ,1));
00111   MatrDse<UPOL> res(taille,taille);
00112 
00113   for(typename MPOL::const_iterator i = P.begin(); i != P.end(); ++i)
00114     {
00115       int d0=(*i)[0];int d1=(*i)[1];int d2=(*i)[2];
00116       coeff_t k=i->coeff();
00117       UPOL P1("x0");
00118       P1=P1^d0; P1*=k;
00119       res(d1,d2) += P1;
00120     }
00121   return (res);
00122 }
00123 //----------------------------------------------------------------------
00124 template <class T, class I> inline
00125 void add_from_iterator(T & v, const I & it, unsigned l0)
00126 {
00127   v+=it->coeff();
00128 }
00129 //----------------------------------------------------------------------
00130 template<class C, class R> struct UPolDse;
00131 template<class C,class R,class I> inline
00132 void add_from_iterator(UPolDse<C,R> & v,
00133                        const I & it,
00134                        unsigned l0)
00135 {
00136   v+=UPolDse<C,R>(it->coeff(),(*it)[0]);
00137 }
00138 //----------------------------------------------------------------------
00139 template<class C, class R, class O> struct MPol;
00140 template<class C, class R, class O> inline
00141 void add_from_iterator(MPol<C,R,O> & v,
00142                        const typename MPol<C,R,O>::const_iterator & it,
00143                        int l0)
00144 {
00145   typename MPol<C,R,O>::monom_t cf(it->coeff(),l0,type::AsSize());
00146   for(int k=0;k<l0;k++) cf.setexpt(k,(*it)[k]);
00147   v+=cf;
00148 }
00149 
00150 //======================================================================
00162 template <class MAT, class POL>
00163 MAT DeltaOf(const POL & P, int l0, int l1, int l2)
00164 {
00165   typedef typename POL::monom_t     monom_t;
00166   typedef typename POL::order_t     order_t;
00167   typedef typename monom_t::coeff_t coeff_t;
00168   POL p1(0),p2(0);
00169 
00170   monom_t m1(1,l0+l1,type::AsSize()), m2(1,l2,type::AsSize());
00171 
00172   // We construct to polynomials p1 and p2
00173   // The set of monomials of p1 are the monomials of P on the first set of variables
00174   // The set of monomials of p2 are the monomials of P on the second set of variables
00175   for(typename POL::const_iterator i = P.begin(); i != P.end(); ++i){
00176     for(int k=l0;k<l0+l1;k++){
00177       m1.setexpt(k,(*i)[k]);
00178     }
00179     p1+=POL(m1);
00180     for(int k=0;k<l2;k++){
00181       m2.setexpt(k,(*i)[l0+l1+k]);
00182     }
00183     p2+=POL(m2);
00184   }
00185 
00186 
00187   unsigned int c1=0, c2=0;
00188   std::map<monom_t,int,order_t> index1,index2;
00189 
00190   for(typename POL::iterator i = p1.begin(); i != p1.end(); ++i){
00191     i->setcoeff(coeff_t(1));
00192     index1[*i]=c1;++c1;
00193   }
00194   for(typename POL::iterator i = p2.begin(); i != p2.end(); ++i){
00195     i->setcoeff(coeff_t(1));
00196     index2[*i]=c2;++c2;
00197   }
00198 
00199   MAT res(c1,c2);
00200   //  int j=-1;
00201   for(typename POL::const_iterator i = P.begin(); i != P.end(); ++i){
00202     for(int k=l0;k<l0+l1;k++){
00203       m1.setexpt(k,(*i)[k]);
00204     }
00205     for(int k=0;k<l2;k++){
00206       m2.setexpt(k,(*i)[l0+l1+k]);
00207     }
00208     //    if (index2[m2] !=j) j=index2[m2];
00209 
00210     add_from_iterator(res(index1[m1],index2[m2]),i,l0);
00211 
00212 //      monom_t cf(i->coeff(),l0+1,AsSize());
00213 //     for(int k=l0  ;k>=0;--k) cf.setexpt(k,(*i)[k]);
00214 //     (res)(index1[m1],index2[m2])+=POL(cf);
00215   }
00216   return (res);
00217 }
00218 //----------------------------------------------------------------------
00228 template <class MAT, class T> inline
00229 MAT mbezout(const T & l, typename T::value_type::monom_t::index_t c=0)
00230 {
00231   typename T::value_type::monom_t::index_t n= l.size()-1;
00232   return DeltaOf<MAT>(Theta(l,c),c,n,n);
00233 }
00234 
00235 
00236 __END_NAMESPACE_SYNAPS
00237 
00238 //----------------------------------------------------------------------
00239 #endif // SYNAPS_RESULTANT_MBEZOUT_H
00240 

SYNAPS DOCUMENTATION
logo