synaps/msolve/Bezout.h

00001 /********************************************************************
00002  *   This file is part of the source code of the SYNAPS kernel.
00003  *   Author(s): B. Mourrain, GALAAD, INRIA.
00004  *   $Id: Bezout.h,v 1.1 2005/07/11 10:03:50 jppavone Exp $
00005  ********************************************************************/
00006 #ifndef SYNAPS_SOLVE_HIDDEN_H
00007 #define SYNAPS_SOLVE_HIDDEN_H
00008 /********************************************************************/
00009 #include <stdio.h>
00010 #include <synaps/init.h>
00011 #include <synaps/linalg.h>
00012 #include <synaps/lapack.h>
00013 #include <synaps/mpol/resultant/MBezout.h>
00014 
00015 
00016 
00017 __BEGIN_NAMESPACE_SYNAPS
00018 
00019 //------------------------------------------------------------------
00020 template <class MAT, class MLAP, class SCAL> inline 
00021 void Expand(const MAT & M, MLAP & ML, typename MAT::size_type ind, SCAL ty)
00022 {
00023   typedef typename MAT::size_type size_type;
00024   typedef typename MAT::value_type coeff_t;
00025 
00026   for (size_type i = 0; i < M.nbrow(); i++)
00027     for (size_type j = 0; j < M.nbcol(); j++)
00028       {
00029         if (M(i,j)!=0) ML(i,j) = SCAL(0);
00030         else
00031           {
00032             bool find=false;
00033             typename coeff_t::const_iterator m=M(i,j).begin();
00034             for (int l=0; l < M(i,j).size(); l++) {
00035               if ((m->nvars() == ind) && (m->GetDegree(ind) > 0)) {
00036                 ML(i,j)=m->GetCoeff();
00037                 find=true;
00038               }
00039               m++;
00040             }
00041             if (!find) ML(i,j)=SCAL(0);
00042           }
00043       }
00044 }
00045 //---------------------------------------------------------------------------
00062 template <class POL>
00063 MatrDse<array2d<typename POL::coeff_t> >  MPSolveURes(const list<POL> & f, bool badcond=true )
00064 {
00065   typedef typename POL::coeff_t scal_t;
00066   typedef MatrDse<array2d<POL> > MatRes;
00067   typedef typename MatRes::value_type value_type;
00068   typedef typename MatRes::size_type size_type;
00069   typedef typename value_type::monom_t monom_t;
00070   
00071   size_type nbrow, nbcol, rank;
00072   size_type nvar=f.size();
00073   MatRes ResMat;
00074   POL s;
00075   list<POL> L;
00076   POL f0(1); 
00077 
00078   monom_t m0(1,0,1);
00079 
00080   // We construct the linear form, f0, which will be added to the system
00081   f0*= m0;
00082   for (size_type i=1; i <=f.size(); i++) {
00083     monom_t mi(1,i,1), mj(1,i+nvar,1);
00084     mi*=mj;
00085     f0+=mi;
00086     }
00087 
00088   L.push_back(f0);
00089 
00090   // Here we shift the index of variables of the original system, in order
00091   // to consider the coefficients of the linear form as variables.
00092 
00093   for (list<POL>::const_iterator p=f.begin(); p!=f.end(); p++)
00094     {
00095       POL fi(0);
00096       for (typename POL::const_iterator m=p->begin(); m!= p->end(); m++) {
00097         int n = m->nvars()+1;
00098         if (n!=0) {
00099           typename monom_t::index_t * t= new (typename monom_t::index_t)[n+nvar];
00100           for(int j=0; j<n;j++) {t[j]=0; t[j+nvar]=(*m)[j];}
00101           scal_t coef(m->GetCoeff());
00102           fi +=POL(monom_t(coef,n+nvar,t));
00103           delete [] t;
00104         }
00105         else 
00106           fi+=POL(m->GetCoeff());
00107       }
00108       L.push_back(fi);
00109     }
00110 
00111   // Construction of the resultant matrix of the system of type
00112   // defined by MatRes.
00113   // The first variables, representing the coefficients of the linear form,
00114   // are hidden.
00115  
00116   POL bzt;
00117   bzt=MBezout(L,nvar);
00118   ResMat=DeltaOf(bzt,L.size()-1,L.size()-1,L.size()-1,MatRes());
00119   cout <<ResMat<<endl;
00120   nbrow=ResMat.nbrow();  nbcol=ResMat.nbcol();
00121   
00122   cout <<nbrow<<" "<<nbcol<<endl;
00123   // Evaluation of the entries of the matrix in a random point.
00124   // These evaluations are put in another matrix.
00125 
00126   array1d<scal_t> ui(f.size()+1);
00127   for (size_type i=0; i <= f.size(); i++) ui[i]=rand()%((i+1)*100);
00128 
00129   MatrDse<array2d<scal_t> > Meval(nbrow, nbcol);
00130   for (size_type i=0; i<nbrow; i++)
00131     for (size_type j=0; j<nbcol; j++)
00132       {
00133         Meval(i,j)=ResMat(i,j)(ui);
00134       }
00135   
00136   array1d<size_type> permut_row(Meval.nbrow()), 
00137                      permut_col(Meval.nbcol());
00138 
00139   // Computation of the rank of the evaluation matrix.
00140   // This rank is also the rank of the Bezoutian matrix.
00141   // It is firts computed with QR decomposition.
00142   cout <<Meval<<endl;
00143   
00144   rank = Rank(Meval,permut_row,permut_col);
00145  
00146   // But if we know in advance that the Bezoutian matrix has a
00147   // bad condition number, we verify the previous value found by
00148   // a computation with SVD.
00149   if (badcond) rank=Rank(Meval);
00150 
00151   cout << " Rang de la Bezoutienne : " << rank << endl;
00152 
00153   // If it is not of maximal rank, extract a maximal minor
00154   bool perm=(rank!=Max(nbrow,nbcol));
00155   if (perm) 
00156     ResMat=Extract(ResMat,permut_row, permut_col, rank);
00157 
00158   MatrDse<lapack<scal_t> > D0(rank,rank),Di(rank,rank),VE(rank,rank);
00159   VectStd<array1d<scal_t> > VE_V(rank), D0V(rank), DiV(rank);
00160   array2d<scal_t>* res = new array2d<scal_t>(rank,f.size()-1);
00161 
00162   // Take the coefficient matrices of the variable x0 and x1
00163   Expand(ResMat, D0, 0, scal_t());
00164   Expand(ResMat, Di, 1, scal_t());
00165  
00166   // Compute generalized eigenvector VE such, (Di- v D0)=0
00167   VE=Eigenvct(D0,Di, Real(),VE_V);
00168 
00169   // Apply each eigenvector to each coefficient matrix of all hidden variables.
00170   for (size_type l=0;l<rank;l++) {
00171     VE_V=Eval(Col(VE,l));
00172     DiV = Di*VE_V ;
00173     D0V = D0*VE_V ;
00174     
00175     for (size_type i=0; i<rank; i++)
00176       if (DiV[i]!=0) {
00177         for (size_type j=0;j<rank;j++)
00178           if (D0V[j]!=0) (*res)(i,0)=DiV[i]/D0V[j];
00179       }
00180   }
00181   for (size_type k=2; k<=f.size(); k++) {
00182     Expand(ResMat, Di, k,scal_t());
00183     
00184     for (size_type l=0;l<rank;l++) {
00185       VE_V=Eval(Col(VE,l));
00186       DiV=Di*VE_V;
00187       D0V = D0*VE_V;
00188       for (size_type i=0; i<rank; i++)
00189         if (DiV[i]!=0) {
00190           for (size_type j=0;j<rank;j++)
00191             if (D0V[j]!=0) 
00192               (*res)(i,k-1)=DiV[i]/D0V[j];
00193         }
00194     }
00195   }
00196   return MatrDse<array2d<scal_t> >(*res);
00197 }
00198 
00199 __END_NAMESPACE_SYNAPS
00200 
00201 /********************************************************************/
00202 #endif // SYNAPS_SOLVE_HIDDEN_H
00203 
00204 

SYNAPS DOCUMENTATION
logo