synaps/mpol/matrixof.h

Go to the documentation of this file.
00001 /*********************************************************************
00002 *      This file is part of the source code of SYNAPS kernel.        *
00003 *   Author(s): B. Mourrain, GALAAD, INRIA                            *
00004 **********************************************************************
00005 History: 
00006 $Id: matrix_of.h,v 1.1 2005/07/11 08:15:52 mourrain Exp $
00007 **********************************************************************/
00009 /*********************************************************************/
00010 #ifndef SYNAPS_RESULTANT_MATRIXOF_H
00011 #define SYNAPS_RESULTANT_MATRIXOF_H
00012 //----------------------------------------------------------------------
00013 #include <synaps/init.h>
00014 #include <synaps/linalg/rep2d.h>
00015 #include <synaps/linalg/MATRIX.m>
00016 #include <synaps/mpol/Monom.h>
00017 #include <synaps/mpol/MPol.h>
00018 #include <list>
00019 #include <map>
00020 #include <set>
00021 
00022 
00023 __BEGIN_NAMESPACE_SYNAPS
00024 
00025 //----------------------------------------------------------------------
00026 template<class C, class R> struct MatrSps;
00027 //----------------------------------------------------------------------
00028 namespace matrixof
00029 {
00030   //----------------------------------------------------------------------
00036   template<class R> inline
00037   void reserve(R & A, typename R::size_type nl, typename R::size_type nc, 
00038                typename R::size_type nz) 
00039   {A= * new R(nl,nc);}
00040   
00041   template<class C, class R> inline
00042   void reserve(MatrSps<C,R> & A, 
00043                typename R::size_type nl, typename R::size_type nc, 
00044                typename R::size_type nz) 
00045   {A= * new R(nl,nc,nz);}
00046   //----------------------------------------------------------------------
00047   template<class R,class C> inline
00048   void assigncoeff(R & A, unsigned int m, unsigned int n, const C & c)
00049   {
00050     A(m,n)=c;
00051   }
00052   //----------------------------------------------------------------------
00053   template<class R,class C> 
00054   void assigncoeff(MatrSps<C,R> & A, unsigned int m, unsigned int n, const C & c)
00055   {
00056     (A.rep())(m,n,c);
00057   }
00058   //----------------------------------------------------------------------
00059 } //matrixof
00060 
00061 //======================================================================
00068 template<class R, class L, class LM>
00069 R matrix_of(const L & f, const LM & s, char t) 
00070 {
00071   typedef typename L::value_type POL;
00072   typedef typename POL::monom_t  MON;
00073   typedef typename POL::order_t  O;
00074 
00075   MON mt;
00076 
00077   std::map<MON,int,O> index;
00078   int l=0;
00079   for(typename LM::const_iterator m = s.begin(); m!=s.end(); m++){
00080     //m->SetCoeff(1);
00081     index[*m]=l;
00082     l++;
00083   }
00084   unsigned nz=0;
00085   for(typename L::const_iterator fi = f.begin(); fi != f.end(); ++fi) 
00086     nz+=fi->size();
00087     
00088   R res; 
00089   if(t=='T') {
00090     matrixof::reserve(res.rep(),(unsigned) f.size(),(unsigned) s.size(),nz);
00091     l=0;
00092     for(typename L::const_iterator fi = f.begin(); fi != f.end(); ++fi){
00093       for(typename POL::const_iterator m = fi->begin(); m!=fi->end(); m++)
00094         matrixof::assigncoeff(res,l,index[*m],m->coeff());
00095       l++;
00096       }
00097   }else{
00098     matrixof::reserve(res.rep(),(unsigned) s.size(),(unsigned) f.size(),nz);
00099     l=0;
00100     for(typename L::const_iterator fi = f.begin(); fi != f.end(); ++fi){
00101       for(typename POL::const_iterator m = fi->begin(); m!=fi->end(); m++)
00102         matrixof::assigncoeff(res,index[*m],l,m->coeff());
00103       l++;
00104     }
00105   }
00106   return res;
00107 }
00108 //----------------------------------------------------------------------
00119 template<class R, class L>
00120 R matrix_of(const L & l, char t='N') 
00121 {
00122   typedef typename L::value_type POL;
00123   typedef typename POL::monom_t  MON;
00124   POL s; MON tm;
00125   for(typename L::const_iterator fi = l.begin(); fi != l.end(); ++fi){
00126     for(typename POL::const_iterator m = fi->begin(); m!= fi->end(); m++)
00127       {
00128         tm =*m; tm.setcoeff(1);
00129         s += POL(tm);
00130       }
00131   }
00132   return matrix_of<R>(l,s,t);   
00133 }
00134 //----------------------------------------------------------------------
00135 template<class R, class L, class LM>
00136 R MatUpolyOf(const L & f, const LM & s, unsigned int v) 
00137 {
00138   typedef typename R::value_type UPOL;
00139   typedef typename L::value_type POL;
00140   typedef typename POL::monom_t  MON;
00141   typedef typename POL::order_t  O;
00142 
00143   MON mt;
00144 
00145   std::map<MON,int,O> index;
00146   int l=0;
00147   for(typename LM::const_iterator m = s.begin(); m!=s.end(); m++){
00148     mt=*m;
00149     index[mt]=l;
00150     l++;
00151   }
00152   unsigned nz=0;
00153   for(typename L::const_iterator fi = f.begin(); fi != f.end(); ++fi) 
00154     nz+=fi->size();
00155     
00156   R res; 
00157   matrixof::reserve(res.rep(),(unsigned) s.size(),(unsigned) f.size(),nz);
00158   //Should be a zero matrix;
00159   l=0;
00160   for(typename L::const_iterator fi = f.begin(); fi != f.end(); ++fi){
00161     for(typename POL::const_iterator m = fi->begin(); m!=fi->end(); m++)
00162       {
00163         mt=*m; 
00164         if((int)v<=mt.nvars()) mt.setexp(v,0); 
00165         res(index[mt],l)+= UPOL(m->coeff(),(*m)[v]);
00166       }
00167       l++;
00168   }
00169   return res;
00170 }
00171 //----------------------------------------------------------------------
00172 template<class R, class L>
00173 R MatUpolyOf(const L & l,unsigned int v) 
00174 {
00175   typedef typename L::value_type POL;
00176   typedef typename POL::monom_t  MON;
00177   POL s(0); MON tm;
00178   for(typename L::const_iterator fi = l.begin(); fi != l.end(); ++fi){
00179     for(typename POL::const_iterator m = fi->begin(); m!= fi->end(); m++)
00180       {
00181         tm =*m; 
00182         tm.setcoeff(1);
00183         if((int)v<=tm.nvars()) tm.setexp(v,0); 
00184         s += POL(tm);
00185       }
00186   }
00187   return MatUpolyOf<R>(l,s,v);   
00188 }
00189 //----------------------------------------------------------------------
00190 
00191 
00192 __END_NAMESPACE_SYNAPS
00193 
00194 #endif // SYNAPS_LINALG_MATRIXOF_H
00195 

SYNAPS DOCUMENTATION
logo