tools.cpp

Go to the documentation of this file.
00001 #include <tools.h>
00002 
00003 GiNaC::symbol x("x"); 
00004 GiNaC::symbol y("y"); 
00005 GiNaC::symbol z("z"); 
00006 GiNaC::symbol infinity("infinity"); 
00007 GiNaC::symbol DUMMY("DUMMY"); 
00008 
00009 int nsd = 2; 
00010 
00011 
00012 /*
00013 void print(GiNaC::exvector& v) {
00014   for (int i=0; i< v.size(); i++) {
00015     cout <<"v["<<i<<"]="<<v[i]<<endl; 
00016   }
00017 }
00018 */
00019 
00020 
00021         
00022 
00023 void print(GiNaC::lst& l) {
00024 //  for (GiNaC::lst::const_iterator i = l.begin(); i != l.end(); ++i)
00025 //    cout << *i << endl;
00026 //
00027   GiNaC::lst::const_iterator i = l.begin(); 
00028   cout <<"GiNaC::lst("<<*i; 
00029   ++i; 
00030 
00031   for (; i != l.end() ; ++i) {
00032     cout << ","<< *i;
00033   }
00034   cout <<");"<<endl; 
00035 }
00036 
00037 void print(GiNaC::exvector& v) {
00038   cout <<"v=["; 
00039   for (int i=0; i< v.size()-1; i++) {
00040     cout <<v[i]<<"," <<endl; 
00041   }
00042   cout <<v[v.size()-1]<< "]"<<endl; 
00043 
00044 }
00045 
00046 GiNaC::lst cross(GiNaC::lst& v1, GiNaC::lst& v2) {
00047   GiNaC::lst ret; 
00048   if ( v1.nops() != v2.nops() ) {
00049     cout <<"incompatible vectors "<<endl; 
00050     cout <<"v1.nops() "<<v1.nops(); 
00051     cout <<"  v2.nops() "<<v2.nops()<<endl; ; 
00052     return GiNaC::lst(); 
00053   }
00054   ret.append(  v1.op(1)*v2.op(2) - v1.op(2)*v2.op(1)); 
00055   ret.append(- v1.op(0)*v2.op(2) + v1.op(2)*v2.op(0)); 
00056   ret.append(  v1.op(0)*v2.op(1) - v1.op(1)*v2.op(0)); 
00057   return ret; 
00058 }
00059 
00060 GiNaC::ex inner(GiNaC::ex a, GiNaC::ex b, bool transposed){
00061   if (GiNaC::is_a<GiNaC::matrix>(a) && GiNaC::is_a<GiNaC::matrix>(b)) {
00062     GiNaC::matrix ma = GiNaC::ex_to<GiNaC::matrix>(a); 
00063     GiNaC::matrix mb = GiNaC::ex_to<GiNaC::matrix>(b); 
00064     if ( !transposed ) {
00065       if (ma.cols() != mb.cols() || ma.rows() != mb.rows() ) { 
00066          cout <<"Incompatible matrices "<<endl; 
00067          cout <<"a.cols() "<<ma.cols()<<endl; 
00068          cout <<"a.rows() "<<ma.rows()<<endl; 
00069          cout <<"b.cols() "<<mb.cols()<<endl; 
00070          cout <<"b.rows() "<<mb.rows()<<endl; 
00071          cout <<"a="<<a<<endl; 
00072          cout <<"b="<<b<<endl; 
00073          return DUMMY; 
00074       }
00075   
00076       GiNaC::ex ret; 
00077       for (int i=0; i<ma.rows(); i++) { 
00078         for (int j=0; j<ma.cols(); j++) { 
00079           ret += ma(i,j)*mb(i,j);  
00080         }
00081       }
00082       return ret; 
00083     } 
00084     else { 
00085       if (ma.cols() != mb.rows() || ma.rows() != mb.cols() ) { 
00086         cout <<"Incompatible matrices "<<endl; 
00087         cout <<"a.cols() "<<ma.cols()<<endl; 
00088         cout <<"a.rows() "<<ma.rows()<<endl; 
00089         cout <<"b.cols() "<<mb.cols()<<endl; 
00090         cout <<"b.rows() "<<mb.rows()<<endl; 
00091         cout <<"a="<<a<<endl; 
00092         cout <<"b="<<b<<endl; 
00093          return DUMMY; 
00094        }
00095 
00096       GiNaC::ex ret; 
00097       for (int i=0; i<ma.rows(); i++) { 
00098         for (int j=0; j<ma.cols(); j++) { 
00099           ret += ma(i,j)*mb(j,i);  
00100         }
00101       }
00102       return ret; 
00103     }
00104   } else if (GiNaC::is_a<GiNaC::lst>(a) 
00105           && GiNaC::is_a<GiNaC::lst>(b)) {
00106     return inner(GiNaC::ex_to<GiNaC::lst>(a), GiNaC::ex_to<GiNaC::lst>(b)); 
00107   } else {
00108     return a*b; 
00109   }
00110 }
00111 
00112 
00113 GiNaC::ex inner(GiNaC::lst v1, GiNaC::lst v2) {
00114   GiNaC::ex ret; 
00115   if ( v1.nops() != v2.nops() ) {
00116     cout <<"incompatible vectors "<<endl; 
00117     cout <<"v1.nops() "<<v1.nops(); 
00118     cout <<"  v2.nops() "<<v2.nops()<<endl; ; 
00119     return 0; 
00120   }
00121   int i; 
00122   for (i = 0; i <= v1.nops()-1 ; ++i) {
00123     if ( GiNaC::is_a<GiNaC::lst>(v1.op(i)) && 
00124          GiNaC::is_a<GiNaC::lst>(v2.op(i)) ) {   
00125       ret += inner(GiNaC::ex_to<GiNaC::lst>(v1.op(i)), 
00126                    GiNaC::ex_to<GiNaC::lst>(v2.op(i))); 
00127     } else {
00128       ret += v1.op(i)*v2.op(i);  
00129     }
00130   }
00131   return ret; 
00132 }
00133 
00134 GiNaC::lst matvec(GiNaC::matrix& M, GiNaC::lst& x) {
00135   GiNaC::lst ret; 
00136   int nr = M.rows(); 
00137   int nc = M.cols(); 
00138   for (int i = 0; i < nr; i++) {
00139     GiNaC::ex tmp; 
00140     for (int j = 0; j < nc; j++) {
00141        tmp = tmp +  M(i,j)*(x.op(j)); 
00142     }
00143     ret.append(tmp); 
00144   }
00145   return ret; 
00146 }
00147 
00148 
00149 GiNaC::ex inner(GiNaC::exvector& v1, GiNaC::exvector& v2){
00150   GiNaC::ex ret; 
00151   for (int i=0; i< v1.size(); i++) {
00152     ret += v1[i]*v2[i]; 
00153   }
00154   return ret; 
00155 }
00156 
00157 
00158 
00159 
00160 GiNaC::ex pol(int order, int nsd, const string a) {
00161   GiNaC::ex ret; // ex to return   
00162   int dof; // degrees of freedom  
00163   GiNaC::ex A;    // ex holding the coefficients a_0 .. a_dof  
00164   GiNaC::lst basis; 
00165 
00166 
00167   if (nsd == 1) {
00168     /* 1D: 
00169      * P^n = a_0 + a_1*x + .... + a_n*x^n 
00170      * dof : n+1
00171      */ 
00172     dof = order+1; 
00173     A = GiNaC::symbolic_matrix(1,dof, a); 
00174     int o=0; 
00175     for (GiNaC::const_iterator i = A.begin(); i != A.end(); ++i)  {  
00176       ret += (*i)*pow(x,o); 
00177       basis.append(pow(x,o)); 
00178       o++; 
00179     }
00180   }
00181   else if ( nsd == 2) {
00182 
00183     /* 2D: structure of coefficients (a_i)  
00184      * [ a_0      a_1 x     a_3 x^2     a_6 x^3  
00185      * [ a_2 y    a_4 xy    a_7 x^2y  
00186      * [ a_5 y^2  a_8 xy^2  
00187      * [ a_9 y^3 
00188      */
00189     dof = (order+1)*(order+2)/2; 
00190     A = GiNaC::symbolic_matrix(1, dof , a); 
00191 
00192     size_t i=0; 
00193     for (int o = 0; o <= order; o++) {
00194       for (int d = 0; d <= o; d++) { 
00195         ret += A.op(i)*pow(y,d)*pow(x,o-d); 
00196         basis.append(pow(y,d)*pow(x,o-d));
00197         i++; 
00198       }
00199     }
00200   }
00201   else if (nsd = 3) {
00202 
00203   /* Similar structure as in 2D, but 
00204    * structured as a tetraheder, i.e., 
00205    *   a_o + a_1 x + a_2 y + a_3 z 
00206    * + a_4 x^2 + a_5 xy +  
00207    */
00208     dof = 0; 
00209     for (int j=0; j<= order; j++) { 
00210       dof += (j+1)*(j+2)/2; 
00211     }
00212     A = GiNaC::symbolic_matrix(1, dof , a); 
00213 
00214 
00215     size_t i=0; 
00216     for (int o = 0; o <= order; o++) {
00217       for (int d = 0; d <= o; d++) { 
00218         for (int f = 0; f <= o; f++) { 
00219           if ( o-d-f >= 0) {
00220             ret += A.op(i)*pow(y,f)*pow(z,d)*pow(x,o-d-f);  
00221             basis.append(pow(y,f)*pow(z,d)*pow(x,o-d-f));
00222             i++; 
00223           }
00224         }
00225       }
00226     }
00227   }
00228   return GiNaC::lst(ret,matrix_to_lst2(A), basis); 
00229 }
00230 
00231 GiNaC::lst polv(int order, int nsd, const string a){
00232   GiNaC::lst ret1;  // contains the polynom  
00233   GiNaC::lst ret2;  // contains the coefficients   
00234   GiNaC::lst ret3;  // constains the basis functions  
00235   GiNaC::lst basis_tmp; 
00236   for (int i=1; i<= nsd; i++) { 
00237     GiNaC::lst basis; 
00238     std::ostringstream s; 
00239     s <<a<<""<<i<<"_"; 
00240     GiNaC::ex polspace = pol(order, nsd, s.str()); 
00241     ret1.append(polspace.op(0)); 
00242     ret2.append(polspace.op(1)); 
00243     basis_tmp = GiNaC::ex_to<GiNaC::lst>(polspace.op(2)); 
00244     for (GiNaC::lst::const_iterator basis_iterator = basis_tmp.begin(); 
00245          basis_iterator != basis_tmp.end(); ++basis_iterator) {
00246       GiNaC::lst tmp_lst; 
00247       for (int d=1; d<=nsd; d++) tmp_lst.append(0);   
00248       tmp_lst.let_op(i-1) = (*basis_iterator);  
00249       ret3.append(tmp_lst); 
00250     }
00251   }
00252   return GiNaC::lst(ret1,ret2,ret3); 
00253 
00254 
00255 
00256 /* Old Code: 
00257   GiNaC::lst ret; 
00258   for (int i=1; i<= nsd; i++) { 
00259     std::ostringstream s; 
00260     s <<a<<"^"<<i<<"_"; 
00261     GiNaC::ex p = pol(order, nsd, s.str()); 
00262     ret.append(p); 
00263   }
00264   return ret; 
00265   */
00266 }
00267 
00268 
00269 GiNaC::ex polb(int order, int nsd, const string a) {
00270 
00271   GiNaC::ex ret; // ex to return   
00272   int dof; // degrees of freedom  
00273   GiNaC::ex A;    // ex holding the coefficients a_0 .. a_dof  
00274   GiNaC::lst basis; 
00275 
00276   if (nsd == 1) {
00277     /* 1D: 
00278      * P^n = a_0 + a_1*x + .... + a_n*x^n 
00279      * dof : n+1
00280      */ 
00281     dof = order+1; 
00282     A = GiNaC::symbolic_matrix(1,dof, a); 
00283     int o=0; 
00284     for (GiNaC::const_iterator i = A.begin(); i != A.end(); ++i)  {  
00285       ret += (*i)*pow(x,o); 
00286       basis.append(pow(x,o)); 
00287       o++; 
00288     }
00289   }
00290   else if ( nsd == 2) {
00291 
00292     /* 2D: structure of coefficients (a_i)  
00293      * [ a_0      a_1 x     a_3 x^2     a_6 x^3  
00294      * [ a_2 y    a_4 xy    a_7 x^2y  
00295      * [ a_5 y^2  a_8 xy^2  
00296      * [ a_9 y^3 
00297      */
00298 
00299 
00300     dof = (order+1)*(order+1); 
00301     A = GiNaC::symbolic_matrix(1, dof , a); 
00302 
00303 
00304     size_t i=0; 
00305     for (int o = 0; o <= order; o++) {
00306       for (int d = 0; d <= order; d++) { 
00307         ret += A.op(i)*pow(y,d)*pow(x,o); 
00308         basis.append(pow(y,d)*pow(x,o));
00309         i++; 
00310       }
00311     }
00312   }
00313   else if (nsd = 3) {
00314 
00315   /* Similar structure as in 2D, but 
00316    * structured as a tetraheder, i.e., 
00317    *   a_o + a_1 x + a_2 y + a_3 z 
00318    * + a_4 x^2 + a_5 xy +  
00319    */
00320     dof = (order+1)*(order+1)*(order+1); 
00321     A = GiNaC::symbolic_matrix(1, dof , a); 
00322 
00323 
00324     size_t i=0; 
00325     for (int o = 0; o <= order; o++) {
00326       for (int d = 0; d <= order; d++) { 
00327         for (int f = 0; f <= order; f++) { 
00328             ret += A.op(i)*pow(y,f)*pow(z,d)*pow(x,o);  
00329             basis.append(pow(y,f)*pow(z,d)*pow(x,o));
00330             i++; 
00331         }
00332       }
00333     }
00334   }
00335 
00336   return GiNaC::lst(ret,matrix_to_lst2(A), basis); 
00337 }
00338 
00339 GiNaC::ex div(GiNaC::ex v){
00340   GiNaC::ex ret = DUMMY;
00341   if (GiNaC::is_a<GiNaC::matrix>(v)) {
00342     GiNaC::matrix m = GiNaC::ex_to<GiNaC::matrix>(v); 
00343     if (nsd == 1) {
00344       ret = diff(m,x);
00345     } else if (nsd == 2) {
00346       ret = diff(m.op(0),x) + diff(m.op(1),y) ;
00347     } else if (nsd == 3) {
00348       ret = diff(m.op(0),x) + diff(m.op(1),y) + diff(m.op(2),z) ;
00349     }
00350   }
00351   return ret;
00352 }
00353 
00354 GiNaC::ex div(GiNaC::lst& v) {
00355   nsd = v.nops();  
00356   GiNaC::ex ret; 
00357   if (nsd == 1) {
00358     ret = v.op(0).diff(x);
00359   }
00360   else if (nsd == 2) {
00361     ret = v.op(0).diff(x) + v.op(1).diff(y); 
00362   }
00363   else if (nsd == 3) {
00364     ret = v.op(0).diff(x) + v.op(1).diff(y) + v.op(2).diff(z); 
00365   }
00366   return ret; 
00367 }
00368 
00369 GiNaC::ex div(GiNaC::exvector& v) {
00370   GiNaC::ex ret; 
00371   if (nsd == 2) {
00372     ret = v[0].diff(x) + v[1].diff(y); 
00373   }
00374   else if (nsd == 3) {
00375     ret = v[0].diff(x) + v[1].diff(y) + v[2].diff(z); 
00376   }
00377   return ret; 
00378 }
00379 
00380 GiNaC::lst coeffs(GiNaC::lst pols) {
00381   GiNaC::lst cc; 
00382   GiNaC::lst tmp; 
00383   for (int i=0; i<= pols.nops()-1; i++) {
00384     tmp = coeffs(pols.op(i)); 
00385     cc = collapse(GiNaC::lst(cc, tmp)); 
00386   }
00387   return cc; 
00388 }
00389 
00390 GiNaC::lst coeffs(GiNaC::ex pol) {
00391   GiNaC::lst cc; 
00392   GiNaC::ex c, b; 
00393   for (int i=pol.ldegree(x); i<=pol.degree(x); ++i) {
00394     for (int j=pol.ldegree(y); j<=pol.degree(y); ++j) {
00395       for (int k=pol.ldegree(z); k<=pol.degree(z); ++k) {
00396         c = pol.coeff(x,i).coeff(y, j).coeff(z,k); 
00397         if ( c != 0 ) cc.append(c); 
00398       }
00399     }
00400   }
00401   return cc; 
00402 }
00403 
00404 
00405 
00406 
00407 
00408 GiNaC::exvector coeff(GiNaC::ex pol) {
00409   GiNaC::exvector cc; 
00410   GiNaC::ex c, b; 
00411   for (int i=pol.ldegree(x); i<=pol.degree(x); ++i) {
00412     for (int j=pol.ldegree(y); j<=pol.degree(y); ++j) {
00413       for (int k=pol.ldegree(z); k<=pol.degree(z); ++k) {
00414         c = pol.coeff(x,i).coeff(y, j).coeff(z,k); 
00415         if ( c != 0 ) cc.insert(cc.begin(),c); 
00416       }
00417     }
00418   }
00419   return cc; 
00420 }
00421 
00422 int dirac(int i, int j) {
00423   if (i==j) return 1; 
00424   else return 0; 
00425 }
00426 
00427 ex_ex_map pol2basisandcoeff(GiNaC::ex e) { 
00428   GiNaC::ex c; 
00429   GiNaC::ex b; 
00430   ex_ex_map map; 
00431   for (int i=e.ldegree(x); i<=e.degree(x); ++i) {
00432     for (int j=e.ldegree(y); j<=e.degree(y); ++j) {
00433       for (int k=e.ldegree(z); k<=e.degree(z); ++k) {
00434         c = e.coeff(x,i).coeff(y, j).coeff(z,k); 
00435         b = pow(x,i)*pow(y,j)*pow(z,k); 
00436         map[b] = c;  
00437       }
00438     }
00439   }
00440   return map; 
00441 }
00442 
00443 void print(ex_int_map map) {
00444   GiNaC::ex b; 
00445   int c; 
00446   ex_int_it iter; 
00447   iter = map.begin(); 
00448   cout <<"{" <<b<<":"<<c; 
00449   for (iter = map.begin(); iter != map.end(); iter++) {  
00450     b = (*iter).first; c = map[b]; 
00451     cout <<", "<<b<<":"<<c; 
00452   }
00453   cout <<"}"<<endl; 
00454 
00455 }
00456 
00457 
00458 
00459 void print(ex_ex_map map) {
00460   GiNaC::ex b; 
00461   GiNaC::ex c; 
00462   ex_ex_it iter; 
00463   iter = map.begin(); 
00464   cout <<"{" <<b<<":"<<c; 
00465   for (iter = map.begin(); iter != map.end(); iter++) {  
00466     b = (*iter).first; c = map[b]; 
00467     cout <<", "<<b<<":"<<c; 
00468   }
00469   cout <<"}"<<endl; 
00470 
00471 }
00472 
00473 
00474 GiNaC::lst ex2equations(GiNaC::ex rel) {
00475   GiNaC::ex lhs = rel.lhs();  
00476   GiNaC::ex rhs = rel.rhs(); 
00477 
00478   GiNaC::ex l; 
00479   GiNaC::ex r; 
00480 
00481   GiNaC::lst eqs; 
00482 
00483   for (int i=lhs.ldegree(x); i<=lhs.degree(x); ++i) {
00484     for (int j=lhs.ldegree(y); j<=lhs.degree(y); ++j) {
00485       for (int k=lhs.ldegree(z); k<=lhs.degree(z); ++k) {
00486         l = lhs.coeff(x,i).coeff(y, j).coeff(z,k); 
00487         r = rhs.coeff(x,i).coeff(y, j).coeff(z,k); 
00488 //      if (! (l == 0 && r == 0 ) )  eqs.append(l == r); OLD VERSION 
00489         if ( (l != 0 && (r == 0 || r == 1) ) )  eqs.append(l == r); 
00490       }
00491     }
00492   }
00493   eqs.sort(); 
00494   return eqs; 
00495 }
00496 
00497 GiNaC::lst collapse(GiNaC::lst l) {
00498   GiNaC::lst lc;  
00499   GiNaC::lst::const_iterator iter1, iter2; 
00500 
00501   for (iter1 = l.begin(); iter1 != l.end(); ++iter1) {
00502      if (GiNaC::is_a<GiNaC::lst>(*iter1)) {
00503        for (iter2 = GiNaC::ex_to<GiNaC::lst>(*iter1).begin(); iter2 != GiNaC::ex_to<GiNaC::lst>(*iter1).end(); ++iter2) {
00504           lc.append(*iter2); 
00505        }
00506      } else {
00507        lc.append(*iter1); 
00508      }
00509   }
00510   lc.sort(); 
00511   lc.unique(); 
00512 return lc; 
00513 }
00514 
00515 
00516 GiNaC::matrix equations2matrix(const GiNaC::ex &eqns, const GiNaC::ex &symbols) {
00517 
00518   cout <<"no equations "<<eqns.nops()<<endl; 
00519   cout <<"no variables "<<symbols.nops()<<endl; 
00520         
00521   GiNaC::matrix sys(eqns.nops(),symbols.nops());
00522   GiNaC::matrix rhs(eqns.nops(),1);
00523   GiNaC::matrix vars(symbols.nops(),1);
00524         
00525   for (size_t r=0; r<eqns.nops(); r++) {
00526     const GiNaC::ex eq = eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
00527     GiNaC::ex linpart = eq;
00528     for (size_t c=0; c<symbols.nops(); c++) {
00529       const GiNaC::ex co = eq.coeff(GiNaC::ex_to<GiNaC::symbol>(symbols.op(c)),1);
00530       linpart -= co*symbols.op(c);
00531       sys(r,c) = co;
00532     }
00533     linpart = linpart.expand();
00534     rhs(r,0) = -linpart;
00535   }
00536   cout <<"matrix "<<sys<<endl; 
00537   cout <<"rhs "<<rhs<<endl; 
00538   cout <<"vars"<<vars<<endl; 
00539   return sys; 
00540 }
00541 
00542 GiNaC::ex grad(GiNaC::ex f) {
00543   if (GiNaC::is_a<GiNaC::matrix>(f)) {
00544      GiNaC::matrix m = GiNaC::ex_to<GiNaC::matrix>(f); 
00545      GiNaC::matrix ret_m(nsd,m.rows()); 
00546      for (int r=0; r< m.rows(); r++) { 
00547        if (nsd == 1) {
00548          ret_m(0,r) = diff(m.op(r),x); 
00549        } else if ( nsd == 2) {
00550          ret_m(0,r) = diff(m.op(r),x); 
00551          ret_m(1,r) = diff(m.op(r),y);  
00552        } else if ( nsd == 3) {
00553          ret_m(0,r) = diff(m.op(r),x); 
00554          ret_m(1,r) = diff(m.op(r),y);  
00555          ret_m(2,r) = diff(m.op(r),z);  
00556        }
00557      }
00558      return ret_m; 
00559   } else {
00560   
00561     if (nsd == 1) {
00562       return GiNaC::matrix(nsd,1,GiNaC::lst(diff(f,x))); 
00563     } else if ( nsd == 2) {
00564       return GiNaC::matrix(nsd,1,GiNaC::lst(diff(f,x), diff(f,y)));  
00565     } else if ( nsd == 3) {
00566       return GiNaC::matrix(nsd,1,GiNaC::lst(diff(f,x), diff(f,y), diff(f,z)));  
00567     } else {
00568       //FIXME: proper error handling
00569       cout <<"THIS SHOULD NEVER HAPPEN"; 
00570       return GiNaC::lst(); 
00571     }
00572   }
00573 }
00574 
00575 GiNaC::lst lst_equals(GiNaC::ex a, GiNaC::ex b) { 
00576   GiNaC::lst ret; 
00577   if ( (GiNaC::is_a<GiNaC::lst>(a)) && (GiNaC::is_a<GiNaC::lst>(b)) /*&& (a.nops() == b.nops())*/ ) { 
00578     for (int i=0; i<= a.nops()-1; i++) {
00579       ret.append(b.op(i) == a.op(i)); 
00580     }
00581   } else if ( !(GiNaC::is_a<GiNaC::lst>(a)) && !(GiNaC::is_a<GiNaC::lst>(b))) { 
00582       ret.append(b == a); 
00583   } else if ( !(GiNaC::is_a<GiNaC::lst>(a)) && (GiNaC::is_a<GiNaC::lst>(b))) { 
00584       ret.append(b.op(0) == a); 
00585   } else {
00586       //FIXME: proper error handling
00587       cout <<"THIS SHOULD NEVER HAPPEN"<<endl;    
00588   }
00589   return ret; 
00590 }
00591 
00592 GiNaC::ex lst_to_matrix2(const GiNaC::lst& l)
00593 {
00594      GiNaC::lst::const_iterator itr, itc;
00595  
00596      // Find number of rows and columns
00597      size_t rows = l.nops(), cols = 0;
00598      for (itr = l.begin(); itr != l.end(); ++itr) {
00599          if (!GiNaC::is_a<GiNaC::lst>(*itr))
00600 //              throw (std::invalid_argument("lst_to_matrix: argument must be a list of lists"));
00601              cols = 1; 
00602          if (itr->nops() > cols)
00603              cols = itr->nops();
00604      }
00605      // Allocate and fill matrix
00606      GiNaC::matrix &M = *new GiNaC::matrix(rows, cols);
00607      M.setflag(GiNaC::status_flags::dynallocated);
00608  
00609      unsigned i;
00610      for (itr = l.begin(), i = 0; itr != l.end(); ++itr, ++i) {
00611          unsigned j;
00612          if (cols == 1) {
00613              M(i, 0) = *itr;
00614          } else {
00615            for (itc = GiNaC::ex_to<GiNaC::lst>(*itr).begin(), j = 0; itc != GiNaC::ex_to<GiNaC::lst>(*itr).end(); ++itc, ++j)
00616                M(i, j) = *itc;
00617          }
00618      }
00619      return M;
00620 }
00621 
00622 
00623 GiNaC::lst matrix_to_lst2(const GiNaC::ex& m) {
00624    if (GiNaC::is_a<GiNaC::matrix>(m)) {
00625      GiNaC::matrix A = GiNaC::ex_to<GiNaC::matrix>(m);  
00626      int cols = A.cols(); 
00627      int rows = A.rows(); 
00628   
00629  
00630      GiNaC::lst ret; 
00631      if ( cols == 1 ) {
00632        for (int i=0; i<=A.rows()-1; i++) { 
00633          ret.append(A(i,0)); 
00634        }
00635      } else if ( rows == 1 ) {
00636        for (int i=0; i<=A.cols()-1; i++) { 
00637          ret.append(A(0,i)); 
00638        }
00639      } else {
00640        for (int i=0; i<=A.rows()-1; i++) { 
00641          GiNaC::lst rl; 
00642          for (int j=0; j<=A.cols()-1; j++) { 
00643            rl.append(A(i,j)); 
00644          }
00645          ret.append(rl); 
00646        }
00647      }
00648      return ret; 
00649    } else { 
00650      return GiNaC::lst(); 
00651    }
00652 
00653 }
00654 
00655 
00656 
00657 int find(GiNaC::ex e, GiNaC::lst list){
00658   for (int i=0; i< list.nops(); i++) {
00659     if ( e == list.op(i) ) return i; 
00660   }
00661   return -1; 
00662 }
00663 
00664 void visitor_subst_pow(GiNaC::ex e, ex_ex_map& map, ex_int_map& intmap, string a) {  
00665   static int i=0;  
00666   if (map.find(e) != map.end()) { 
00667     intmap[e] = intmap[e]+1;
00668     return;   
00669   }
00670   if (GiNaC::is_exactly_a<GiNaC::power>(e)) { 
00671     std::ostringstream s; 
00672     s <<a<<i++; 
00673     map[e] = GiNaC::symbol(s.str()); 
00674     intmap[e] = 0;  
00675     for (int i=0; i< e.nops(); i++) { 
00676        GiNaC::ex e2 = e.op(i);  
00677 //       cout <<"power e "<<e2<<endl; 
00678        visitor_subst_pow(e2,map,intmap, a); 
00679     }
00680   }
00681   else if (GiNaC::is_a<GiNaC::function>(e)) { 
00682     std::ostringstream s; 
00683     s <<a<<i++; 
00684     map[e] = GiNaC::symbol(s.str()); 
00685     intmap[e] = 0;  
00686     for (int i=0; i< e.nops(); i++) { 
00687        GiNaC::ex e2 = e.op(i);  
00688 //       cout <<"function e "<<e2<<endl; 
00689        visitor_subst_pow(e2,map,intmap, a); 
00690     }
00691   }
00692   else if (GiNaC::is_a<GiNaC::mul>(e)) { 
00693     if (e.nops() > 4 && e.nops() < 10 ) { 
00694       std::ostringstream s; 
00695       s <<a<<i++; 
00696       map[e] = GiNaC::symbol(s.str()); 
00697       intmap[e] = 0;  
00698     }
00699 
00700     for (int i=0; i< e.nops(); i++) { 
00701        GiNaC::ex e2 = e.op(i);  
00702        visitor_subst_pow(e2,map,intmap, a); 
00703     }
00704   }
00705   else if (GiNaC::is_a<GiNaC::add>(e)) { 
00706     for (int i=0; i< e.nops(); i++) { 
00707        GiNaC::ex e2 = e.op(i);  
00708        visitor_subst_pow(e2,map,intmap,a); 
00709     }
00710   }
00711 
00712 
00713 }
00714 
00715 
00716 
00717 
00718 void check_visitor(GiNaC::ex e, GiNaC::lst& exlist) {
00719   if (find(e, exlist) >= 0) return;   
00720 
00721 //  cout <<"ex e "<<e<<endl; 
00722   if (GiNaC::is_a<GiNaC::numeric>(e)) { 
00723   }
00724   else if (GiNaC::is_a<GiNaC::add>(e) ) {
00725 //    cout <<"e "<<e <<endl; 
00726 //    cout <<"e.nops() "<<e.nops() <<endl; 
00727     if (e.nops() > 4 && e.nops() < 10 ) exlist.append(e); 
00728     for (int i=0; i< e.nops(); i++) { 
00729        GiNaC::ex e2 = e.op(i);  
00730 //       cout <<"add e "<<e2<<endl; 
00731 //       exlist.append(e2); 
00732        check_visitor(e2,exlist); 
00733     }
00734   } 
00735   else if (GiNaC::is_a<GiNaC::mul>(e)) { 
00736     for (int i=0; i< e.nops(); i++) { 
00737        GiNaC::ex e2 = e.op(i);  
00738 //       cout <<"mul e "<<e2<<endl; 
00739        exlist.append(e2); 
00740        check_visitor(e2,exlist); 
00741     }
00742   }
00743   else if (GiNaC::is_a<GiNaC::lst>(e)) { 
00744     for (int i=0; i< e.nops(); i++) { 
00745        GiNaC::ex e2 = e.op(i);  
00746 //       cout <<"GiNaC::lst e "<<e2<<endl; 
00747 //       exlist.append(e2); 
00748        check_visitor(e2,exlist); 
00749     }
00750   }
00751   else if (GiNaC::is_exactly_a<GiNaC::power>(e)) { 
00752     exlist.append(e); 
00753     for (int i=0; i< e.nops(); i++) { 
00754        GiNaC::ex e2 = e.op(i);  
00755 //       cout <<"power e "<<e2<<endl; 
00756        check_visitor(e2,exlist); 
00757     }
00758   }
00759   else if (GiNaC::is_a<GiNaC::function>(e)) { 
00760     exlist.append(e); 
00761     for (int i=0; i< e.nops(); i++) { 
00762        GiNaC::ex e2 = e.op(i);  
00763 //       cout <<"function e "<<e2<<endl; 
00764        check_visitor(e2,exlist); 
00765     }
00766   }
00767 
00768 
00769 
00770   else {
00771 //       exlist.append(e); 
00772 //    cout <<"atom e "<<e<<endl; 
00773   }
00774 
00775   exlist.sort(); 
00776   exlist.unique(); 
00777 }
00778 
00779 
00780 string istr(string a, int b) {
00781   std::ostringstream s; 
00782   s <<a<<b; 
00783   return s.str(); 
00784 }
00785 
00786 void EQUAL_OR_DIE(GiNaC::ex e, char* s) {
00787   if (!compare(e, string(s))) { 
00788     cout <<"ERROR: expression e: " <<e<<" is not equal to "<<s<<endl; 
00789     exit(-1); 
00790   }
00791 }
00792 
00793 
00794 bool compare(GiNaC::ex e, string s) { 
00795   std::ostringstream ss; 
00796   ss<<e; 
00797   string es = ss.str(); 
00798   if ( es == s) return true;  
00799   else return false; 
00800 }
00801 
00802 void matrix_from_equations(const GiNaC::ex &eqns, const GiNaC::ex &symbols, GiNaC::matrix& A, GiNaC::matrix& b) {
00803         // build matrix from equation system
00804   GiNaC::matrix sys(eqns.nops(),symbols.nops());
00805   GiNaC::matrix rhs(eqns.nops(),1);
00806   GiNaC::matrix vars(symbols.nops(),1);
00807         
00808   for (size_t r=0; r<eqns.nops(); r++) {
00809     const GiNaC::ex eq = eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
00810     GiNaC::ex linpart = eq;
00811     for (size_t c=0; c<symbols.nops(); c++) {
00812       const GiNaC::ex co = eq.coeff(GiNaC::ex_to<GiNaC::symbol>(symbols.op(c)),1);
00813       linpart -= co*symbols.op(c);
00814       sys(r,c) = co;
00815     }
00816     linpart = linpart.expand();
00817     rhs(r,0) = -linpart;
00818   }
00819   A = sys; 
00820   b = rhs; 
00821 }
00822 
00823 
00824 void print(std::map<std::pair<int,int>, GiNaC::ex>& A) {
00825   map<std::pair<int,int>,GiNaC::ex>::iterator iter; 
00826   for (iter = A.begin(); iter != A.end() ; iter++) {
00827       cout <<"A["<<(*iter).first.first<<","<<(*iter).first.second<<"]="<<(*iter).second<<endl; 
00828   }
00829 }
00830 
00831 
00832 

Generated on Mon Jan 9 18:08:08 2006 for SyFi by  doxygen 1.4.4