|
GiNaC
1.6.2
|
00001 00005 /* 00006 * GiNaC Copyright (C) 1999-2011 Johannes Gutenberg University Mainz, Germany 00007 * 00008 * This program is free software; you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation; either version 2 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #ifndef GINAC_EXPAIRSEQ_H 00024 #define GINAC_EXPAIRSEQ_H 00025 00026 #include "expair.h" 00027 #include "indexed.h" 00028 00029 // CINT needs <algorithm> to work properly with <vector> and <list> 00030 #include <algorithm> 00031 #include <list> 00032 #include <memory> 00033 #include <vector> 00034 00035 namespace GiNaC { 00036 00046 #define EXPAIRSEQ_USE_HASHTAB 0 00047 00048 typedef std::vector<expair> epvector; 00049 typedef epvector::iterator epp; 00050 typedef std::list<epp> epplist; 00051 typedef std::vector<epplist> epplistvector; 00052 00055 epvector* conjugateepvector(const epvector&); 00056 00066 class expairseq : public basic 00067 { 00068 GINAC_DECLARE_REGISTERED_CLASS(expairseq, basic) 00069 00070 // other constructors 00071 public: 00072 expairseq(const ex & lh, const ex & rh); 00073 expairseq(const exvector & v); 00074 expairseq(const epvector & v, const ex & oc, bool do_index_renaming = false); 00075 expairseq(std::auto_ptr<epvector>, const ex & oc, bool do_index_renaming = false); 00076 00077 // functions overriding virtual functions from base classes 00078 public: 00079 unsigned precedence() const {return 10;} 00080 bool info(unsigned inf) const; 00081 size_t nops() const; 00082 ex op(size_t i) const; 00083 ex map(map_function & f) const; 00084 ex eval(int level=0) const; 00085 ex to_rational(exmap & repl) const; 00086 ex to_polynomial(exmap & repl) const; 00087 bool match(const ex & pattern, exmap& repl_lst) const; 00088 ex subs(const exmap & m, unsigned options = 0) const; 00089 ex conjugate() const; 00090 00091 void archive(archive_node& n) const; 00092 void read_archive(const archive_node& n, lst& syms); 00093 protected: 00094 bool is_equal_same_type(const basic & other) const; 00095 unsigned return_type() const; 00096 unsigned calchash() const; 00097 ex expand(unsigned options=0) const; 00098 00099 // new virtual functions which can be overridden by derived classes 00100 protected: 00101 virtual ex thisexpairseq(const epvector & v, const ex & oc, bool do_index_renaming = false) const; 00102 virtual ex thisexpairseq(std::auto_ptr<epvector> vp, const ex & oc, bool do_index_renaming = false) const; 00103 virtual void printseq(const print_context & c, char delim, 00104 unsigned this_precedence, 00105 unsigned upper_precedence) const; 00106 virtual void printpair(const print_context & c, const expair & p, 00107 unsigned upper_precedence) const; 00108 virtual expair split_ex_to_pair(const ex & e) const; 00109 virtual expair combine_ex_with_coeff_to_pair(const ex & e, 00110 const ex & c) const; 00111 virtual expair combine_pair_with_coeff_to_pair(const expair & p, 00112 const ex & c) const; 00113 virtual ex recombine_pair_to_ex(const expair & p) const; 00114 virtual bool expair_needs_further_processing(epp it); 00115 virtual ex default_overall_coeff() const; 00116 virtual void combine_overall_coeff(const ex & c); 00117 virtual void combine_overall_coeff(const ex & c1, const ex & c2); 00118 virtual bool can_make_flat(const expair & p) const; 00119 00120 // non-virtual functions in this class 00121 protected: 00122 void do_print(const print_context & c, unsigned level) const; 00123 void do_print_tree(const print_tree & c, unsigned level) const; 00124 void construct_from_2_ex_via_exvector(const ex & lh, const ex & rh); 00125 void construct_from_2_ex(const ex & lh, const ex & rh); 00126 void construct_from_2_expairseq(const expairseq & s1, 00127 const expairseq & s2); 00128 void construct_from_expairseq_ex(const expairseq & s, 00129 const ex & e); 00130 void construct_from_exvector(const exvector & v); 00131 void construct_from_epvector(const epvector & v, bool do_index_renaming = false); 00132 void make_flat(const exvector & v); 00133 void make_flat(const epvector & v, bool do_index_renaming = false); 00134 void canonicalize(); 00135 void combine_same_terms_sorted_seq(); 00136 #if EXPAIRSEQ_USE_HASHTAB 00137 void combine_same_terms(); 00138 unsigned calc_hashtabsize(unsigned sz) const; 00139 unsigned calc_hashindex(const ex & e) const; 00140 void shrink_hashtab(); 00141 void remove_hashtab_entry(epvector::const_iterator element); 00142 void move_hashtab_entry(epvector::const_iterator oldpos, 00143 epvector::iterator newpos); 00144 void sorted_insert(epplist & eppl, epvector::const_iterator elem); 00145 void build_hashtab_and_combine(epvector::iterator & first_numeric, 00146 epvector::iterator & last_non_zero, 00147 vector<bool> & touched, 00148 unsigned & number_of_zeroes); 00149 void drop_coeff_0_terms(epvector::iterator & first_numeric, 00150 epvector::iterator & last_non_zero, 00151 vector<bool> & touched, 00152 unsigned & number_of_zeroes); 00153 bool has_coeff_0() const; 00154 void add_numerics_to_hashtab(epvector::iterator first_numeric, 00155 epvector::const_iterator last_non_zero); 00156 #endif // EXPAIRSEQ_USE_HASHTAB 00157 bool is_canonical() const; 00158 std::auto_ptr<epvector> expandchildren(unsigned options) const; 00159 std::auto_ptr<epvector> evalchildren(int level) const; 00160 std::auto_ptr<epvector> subschildren(const exmap & m, unsigned options = 0) const; 00161 00162 // member variables 00163 00164 protected: 00165 epvector seq; 00166 ex overall_coeff; 00167 #if EXPAIRSEQ_USE_HASHTAB 00168 epplistvector hashtab; 00169 unsigned hashtabsize; 00170 unsigned hashmask; 00171 static unsigned maxhashtabsize; 00172 static unsigned minhashtabsize; 00173 static unsigned hashtabfactor; 00174 #endif // EXPAIRSEQ_USE_HASHTAB 00175 }; 00176 00181 class make_flat_inserter 00182 { 00183 public: 00184 make_flat_inserter(const epvector &epv, bool b): do_renaming(b) 00185 { 00186 if (!do_renaming) 00187 return; 00188 for (epvector::const_iterator i=epv.begin(); i!=epv.end(); ++i) 00189 if(are_ex_trivially_equal(i->coeff, 1)) 00190 combine_indices(i->rest.get_free_indices()); 00191 } 00192 make_flat_inserter(const exvector &v, bool b): do_renaming(b) 00193 { 00194 if (!do_renaming) 00195 return; 00196 for (exvector::const_iterator i=v.begin(); i!=v.end(); ++i) 00197 combine_indices(i->get_free_indices()); 00198 } 00199 ex handle_factor(const ex &x, const ex &coeff) 00200 { 00201 if (!do_renaming) 00202 return x; 00203 exvector dummies_of_factor; 00204 if (is_a<numeric>(coeff) && coeff.is_equal(GiNaC::numeric(1))) 00205 dummies_of_factor = get_all_dummy_indices_safely(x); 00206 else if (is_a<numeric>(coeff) && coeff.is_equal(GiNaC::numeric(2))) 00207 dummies_of_factor = x.get_free_indices(); 00208 else 00209 return x; 00210 if (dummies_of_factor.size() == 0) 00211 return x; 00212 sort(dummies_of_factor.begin(), dummies_of_factor.end(), ex_is_less()); 00213 ex new_factor = rename_dummy_indices_uniquely(used_indices, 00214 dummies_of_factor, x); 00215 combine_indices(dummies_of_factor); 00216 return new_factor; 00217 } 00218 private: 00219 void combine_indices(const exvector &dummies_of_factor) 00220 { 00221 exvector new_dummy_indices; 00222 set_union(used_indices.begin(), used_indices.end(), 00223 dummies_of_factor.begin(), dummies_of_factor.end(), 00224 std::back_insert_iterator<exvector>(new_dummy_indices), ex_is_less()); 00225 used_indices.swap(new_dummy_indices); 00226 } 00227 bool do_renaming; 00228 exvector used_indices; 00229 }; 00230 00231 } // namespace GiNaC 00232 00233 #endif // ndef GINAC_EXPAIRSEQ_H