3 * Interface to sequences of expression pairs. */
6 * GiNaC Copyright (C) 1999-2008 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #ifndef __GINAC_EXPAIRSEQ_H__
24 #define __GINAC_EXPAIRSEQ_H__
29 // CINT needs <algorithm> to work properly with <vector> and <list>
37 /** Using hash tables can potentially enhance the asymptotic behaviour of
38 * combining n terms into one large sum (or n terms into one large product)
39 * from O(n*log(n)) to about O(n). There are, however, several drawbacks.
40 * The constant in front of O(n) is quite large, when copying such an object
41 * one also has to copy the has table, comparison is quite expensive because
42 * there is no ordering any more, it doesn't help at all when combining two
43 * expairseqs because due to the presorted nature the behaviour would be
44 * O(n) anyways, the code is quite messy, etc, etc. The code is here as
45 * an example for following generations to tinker with. */
46 #define EXPAIRSEQ_USE_HASHTAB 0
48 typedef std::vector<expair> epvector; ///< expair-vector
49 typedef epvector::iterator epp; ///< expair-vector pointer
50 typedef std::list<epp> epplist; ///< list of expair-vector pointers
51 typedef std::vector<epplist> epplistvector; ///< vector of epplist
53 /** Complex conjugate every element of an epvector. Returns zero if this
54 * does not change anything. */
55 epvector* conjugateepvector(const epvector&);
57 /** A sequence of class expair.
58 * This is used for time-critical classes like sums and products of terms
59 * since handling a list of coeff and rest is much faster than handling a
60 * list of products or powers, respectively. (Not incidentally, Maple does it
61 * the same way, maybe others too.) The semantics is (at least) twofold:
62 * one for addition and one for multiplication and several methods have to
63 * be overridden by derived classes to reflect the change in semantics.
64 * However, most functionality turns out to be shared between addition and
65 * multiplication, which is the reason why there is this base class. */
66 class expairseq : public basic
68 GINAC_DECLARE_REGISTERED_CLASS(expairseq, basic)
72 expairseq(const ex & lh, const ex & rh);
73 expairseq(const exvector & v);
74 expairseq(const epvector & v, const ex & oc, bool do_index_renaming = false);
75 expairseq(std::auto_ptr<epvector>, const ex & oc, bool do_index_renaming = false);
77 // functions overriding virtual functions from base classes
79 unsigned precedence() const {return 10;}
80 bool info(unsigned inf) const;
82 ex op(size_t i) const;
83 ex map(map_function & f) const;
84 ex eval(int level=0) const;
85 ex to_rational(exmap & repl) const;
86 ex to_polynomial(exmap & repl) const;
87 bool match(const ex & pattern, lst & repl_lst) const;
88 ex subs(const exmap & m, unsigned options = 0) const;
90 bool is_polynomial(const ex & var) const;
92 bool is_equal_same_type(const basic & other) const;
93 unsigned return_type() const;
94 unsigned calchash() const;
95 ex expand(unsigned options=0) const;
97 // new virtual functions which can be overridden by derived classes
99 virtual ex thisexpairseq(const epvector & v, const ex & oc, bool do_index_renaming = false) const;
100 virtual ex thisexpairseq(std::auto_ptr<epvector> vp, const ex & oc, bool do_index_renaming = false) const;
101 virtual void printseq(const print_context & c, char delim,
102 unsigned this_precedence,
103 unsigned upper_precedence) const;
104 virtual void printpair(const print_context & c, const expair & p,
105 unsigned upper_precedence) const;
106 virtual expair split_ex_to_pair(const ex & e) const;
107 virtual expair combine_ex_with_coeff_to_pair(const ex & e,
109 virtual expair combine_pair_with_coeff_to_pair(const expair & p,
111 virtual ex recombine_pair_to_ex(const expair & p) const;
112 virtual bool expair_needs_further_processing(epp it);
113 virtual ex default_overall_coeff() const;
114 virtual void combine_overall_coeff(const ex & c);
115 virtual void combine_overall_coeff(const ex & c1, const ex & c2);
116 virtual bool can_make_flat(const expair & p) const;
118 // non-virtual functions in this class
120 void do_print(const print_context & c, unsigned level) const;
121 void do_print_tree(const print_tree & c, unsigned level) const;
122 void construct_from_2_ex_via_exvector(const ex & lh, const ex & rh);
123 void construct_from_2_ex(const ex & lh, const ex & rh);
124 void construct_from_2_expairseq(const expairseq & s1,
125 const expairseq & s2);
126 void construct_from_expairseq_ex(const expairseq & s,
128 void construct_from_exvector(const exvector & v);
129 void construct_from_epvector(const epvector & v, bool do_index_renaming = false);
130 void make_flat(const exvector & v);
131 void make_flat(const epvector & v, bool do_index_renaming = false);
133 void combine_same_terms_sorted_seq();
134 #if EXPAIRSEQ_USE_HASHTAB
135 void combine_same_terms();
136 unsigned calc_hashtabsize(unsigned sz) const;
137 unsigned calc_hashindex(const ex & e) const;
138 void shrink_hashtab();
139 void remove_hashtab_entry(epvector::const_iterator element);
140 void move_hashtab_entry(epvector::const_iterator oldpos,
141 epvector::iterator newpos);
142 void sorted_insert(epplist & eppl, epvector::const_iterator elem);
143 void build_hashtab_and_combine(epvector::iterator & first_numeric,
144 epvector::iterator & last_non_zero,
145 vector<bool> & touched,
146 unsigned & number_of_zeroes);
147 void drop_coeff_0_terms(epvector::iterator & first_numeric,
148 epvector::iterator & last_non_zero,
149 vector<bool> & touched,
150 unsigned & number_of_zeroes);
151 bool has_coeff_0() const;
152 void add_numerics_to_hashtab(epvector::iterator first_numeric,
153 epvector::const_iterator last_non_zero);
154 #endif // EXPAIRSEQ_USE_HASHTAB
155 bool is_canonical() const;
156 std::auto_ptr<epvector> expandchildren(unsigned options) const;
157 std::auto_ptr<epvector> evalchildren(int level) const;
158 std::auto_ptr<epvector> subschildren(const exmap & m, unsigned options = 0) const;
165 #if EXPAIRSEQ_USE_HASHTAB
166 epplistvector hashtab;
167 unsigned hashtabsize;
169 static unsigned maxhashtabsize;
170 static unsigned minhashtabsize;
171 static unsigned hashtabfactor;
172 #endif // EXPAIRSEQ_USE_HASHTAB
175 /** Class to handle the renaming of dummy indices. It holds a vector of
176 * indices that are being used in the expression so-far. If the same
177 * index occurs again as a dummy index in a factor, it is to be renamed.
178 * Unless dummy index renaming was swichted of, of course ;-) . */
179 class make_flat_inserter
182 make_flat_inserter(const epvector &epv, bool b): do_renaming(b)
186 for (epvector::const_iterator i=epv.begin(); i!=epv.end(); ++i)
187 if(are_ex_trivially_equal(i->coeff, 1))
188 combine_indices(i->rest.get_free_indices());
190 make_flat_inserter(const exvector &v, bool b): do_renaming(b)
194 for (exvector::const_iterator i=v.begin(); i!=v.end(); ++i)
195 combine_indices(i->get_free_indices());
197 ex handle_factor(const ex &x, const ex &coeff)
201 exvector dummies_of_factor;
202 if (is_a<numeric>(coeff) && coeff.is_equal(GiNaC::numeric(1)))
203 dummies_of_factor = get_all_dummy_indices_safely(x);
204 else if (is_a<numeric>(coeff) && coeff.is_equal(GiNaC::numeric(2)))
205 dummies_of_factor = x.get_free_indices();
208 if (dummies_of_factor.size() == 0)
210 sort(dummies_of_factor.begin(), dummies_of_factor.end(), ex_is_less());
211 ex new_factor = rename_dummy_indices_uniquely(used_indices,
212 dummies_of_factor, x);
213 combine_indices(dummies_of_factor);
217 void combine_indices(const exvector &dummies_of_factor)
219 exvector new_dummy_indices;
220 set_union(used_indices.begin(), used_indices.end(),
221 dummies_of_factor.begin(), dummies_of_factor.end(),
222 std::back_insert_iterator<exvector>(new_dummy_indices), ex_is_less());
223 used_indices.swap(new_dummy_indices);
226 exvector used_indices;
231 #endif // ndef __GINAC_EXPAIRSEQ_H__