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