SyFi 0.3
ginac_tools.h
Go to the documentation of this file.
00001 // Copyright (C) 2006-2009 Kent-Andre Mardal and Simula Research Laboratory.
00002 // Licensed under the GNU GPL Version 2, or (at your option) any later version.
00003 
00004 #ifndef GINAC_TOOLS_IS_INCLUDED
00005 #define GINAC_TOOLS_IS_INCLUDED
00006 
00007 #include <string>
00008 #include <iostream>
00009 #include <map>
00010 
00011 #include <ginac/ginac.h>
00012 
00013 #include "utilities.h"
00014 
00015 namespace SyFi
00016 {
00017 
00018         bool compare(const GiNaC::ex & e, const std::string & s);
00019 
00020         void EQUAL_OR_DIE(const GiNaC::ex & e, const std::string & s);
00021 
00022         // Read two archive files and compare the expressions.
00023         // If any expressions are not equal, prints them and
00024         // returns false. Returns true if all expressions are equal.
00025         bool compare_archives(const std::string & first, const std::string & second, std::ostream & os=std::cout);
00026 
00027         // using lst as a vector
00028         // inner product of vectors or lst
00029         GiNaC::ex inner(GiNaC::ex a, GiNaC::ex b, bool transposed = false);
00030         GiNaC::ex inner(GiNaC::exvector& v1, GiNaC::exvector& v2);
00031         GiNaC::ex inner(GiNaC::lst v1, GiNaC::lst v2);
00032         GiNaC::lst cross(GiNaC::lst& v1, GiNaC::lst& v2);
00033         // matrix vector product
00034         GiNaC::lst matvec(GiNaC::matrix& M, GiNaC::lst& x);
00035         GiNaC::ex  matvec(GiNaC::ex A, GiNaC::ex x);
00036 
00037         GiNaC::lst ex2equations(GiNaC::ex rel);
00038         GiNaC::lst collapse(GiNaC::lst l);
00039 
00040         GiNaC::matrix equations2matrix (const GiNaC::ex &eqns, const GiNaC::ex &symbols);
00041         void matrix_from_equations(const GiNaC::ex &eqns, const GiNaC::ex &symbols, GiNaC::matrix &A, GiNaC::matrix& b);
00042 
00043         GiNaC::ex  lst_to_matrix2(const GiNaC::lst & l);
00044         GiNaC::lst matrix_to_lst2(const GiNaC::ex & m );
00045 
00046         GiNaC::lst lst_equals(GiNaC::ex a, GiNaC::ex b);
00047 
00048         // FIXME bad name
00049         int find(GiNaC::ex e, GiNaC::lst list);
00050 
00051         // TODO: remove these two:
00052         void check_visitor(GiNaC::ex e, GiNaC::lst& exlist);
00053         void visitor_subst_pow(GiNaC::ex e, GiNaC::exmap& map, ex_int_map& intmap, std::string a);
00054 
00055         // generates a polynom of arbitrary order on a line, a triangle, or a tetrahedron
00056         GiNaC::ex pol(unsigned int order, unsigned int nsd, const std::string a);
00057         // generates a vector polynom of arbitrary order on a line, a triangle or a tetrahedron
00058         GiNaC::lst polv(unsigned int no_fields, unsigned int order, unsigned int nsd, const std::string a);
00059         // generates a polynom of arbitrary order on a square or a box
00060         GiNaC::ex polb(unsigned int order, unsigned int nsd, const std::string a);
00061         // generates a vector polynom of arbitrary order on a squart or a box
00062         //lst polbv(int order, int nsd, const std::string a);
00063 
00064         // generates a homogenous polynom of arbitrary order on a line, a triangle, or a tetrahedron
00065         GiNaC::ex homogenous_pol(unsigned int order, unsigned int nsd, const std::string a);
00066         // generates a homogenous vector polynom of arbitrary order
00067         GiNaC::lst homogenous_polv(unsigned int no_fields, unsigned int order, unsigned int nsd, const std::string a);
00068 
00069         // generates a Legendre polynom of arbitrary order
00070         GiNaC::ex legendre(unsigned int order, unsigned int nsd, const std::string a);
00071         // generates a Legendre vector polynom of arbitrary order
00072         GiNaC::lst legendrev(unsigned int no_fields, unsigned int order, unsigned int nsd, const std::string a);
00073 
00074         // extracts the coefficents from a polynomial
00075         GiNaC::exvector coeff(GiNaC::ex pol);
00076         GiNaC::lst      coeffs(GiNaC::ex pol);
00077         GiNaC::lst      coeffs(GiNaC::lst pols);
00078 
00079         // extract the basisfunctions and corresponding coefficents from a polynomial
00080         GiNaC::exmap  pol2basisandcoeff(GiNaC::ex e);
00081         GiNaC::exmap  pol2basisandcoeff(GiNaC::ex e, GiNaC::ex s);
00082 
00083         // Collect all symbols of an expression
00084         void collect_symbols(const GiNaC::ex & e, exset & v);
00085 
00086         GiNaC::exvector collect_symbols(const GiNaC::ex & e);
00087 
00088         // Builds a map with the number of occurrences of each symbol.
00089         // Highly dependent on the current form of the expression.
00090         GiNaC::exhashmap<int> count_symbols(const GiNaC::ex & e);
00091 
00092         // Extract all symbols into a lst. Useful for comparing
00093         // expressions to other ex read from an archive.
00094         GiNaC::ex extract_symbols(const GiNaC::ex & e);
00095 
00096         //std::list<GiNaC::ex> get_symbols(const GiNaC::ex & e);
00097         //GiNaC::exvector get_symbols(const GiNaC::ex & e);
00098         //void get_symbols(const GiNaC::ex & e, GiNaC::exmap & em);
00099 
00100         // Utility class to collect statistics about an expression.
00101         class ExStats
00102         {
00103                 public:
00104                         ExStats(): muls(0), adds(0), pows(0), functions(0), flops(0) {}
00105 
00106                         inline const ExStats & operator+=(const ExStats & rhs)
00107                         {
00108                                 muls      += rhs.muls;
00109                                 adds      += rhs.adds;
00110                                 pows      += rhs.pows;
00111                                 functions += rhs.functions;
00112                                 flops     += rhs.flops;
00113                                 return *this;
00114                         }
00115 
00116                         int muls;
00117                         int adds;
00118 
00119                         int pows;
00120                         int functions;
00121 
00122                         // flops = sum of multiplications and additions, with integer powers interpreted as many multiplications
00123                         int flops;
00124         };
00125 
00126         // Count the number of operations in an expression.
00127         ExStats count_ops(const GiNaC::ex & e);
00128 
00129         // ===== expression manipulation
00130 
00131         GiNaC::ex replace_powers(const GiNaC::ex & e, const std::list<GiNaC::symbol> & symbols, std::list<symexpair> & sel, const std::string & tmpsymbolprefix="p_");
00132 
00133 };                                                               // namespace SyFi
00134 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines