3 * Interface to sequences of expression pairs. */
6 * GiNaC Copyright (C) 1999-2010 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, exmap& repl_lst) const;
88 ex subs(const exmap & m, unsigned options = 0) const;
90 bool is_polynomial(const ex & var) const;
92 void archive(archive_node& n) const;
93 void read_archive(const archive_node& n, lst& syms);
95 bool is_equal_same_type(const basic & other) const;
96 unsigned return_type() const;
97 unsigned calchash() const;
98 ex expand(unsigned options=0) const;
100 // new virtual functions which can be overridden by derived classes
102 virtual ex thisexpairseq(const epvector & v, const ex & oc, bool do_index_renaming = false) const;
103 virtual ex thisexpairseq(std::auto_ptr<epvector> vp, const ex & oc, bool do_index_renaming = false) const;
104 virtual void printseq(const print_context & c, char delim,
105 unsigned this_precedence,
106 unsigned upper_precedence) const;
107 virtual void printpair(const print_context & c, const expair & p,
108 unsigned upper_precedence) const;
109 virtual expair split_ex_to_pair(const ex & e) const;
110 virtual expair combine_ex_with_coeff_to_pair(const ex & e,
112 virtual expair combine_pair_with_coeff_to_pair(const expair & p,
114 virtual ex recombine_pair_to_ex(const expair & p) const;
115 virtual bool expair_needs_further_processing(epp it);
116 virtual ex default_overall_coeff() const;
117 virtual void combine_overall_coeff(const ex & c);
118 virtual void combine_overall_coeff(const ex & c1, const ex & c2);
119 virtual bool can_make_flat(const expair & p) const;
121 // non-virtual functions in this class
123 void do_print(const print_context & c, unsigned level) const;
124 void do_print_tree(const print_tree & c, unsigned level) const;
125 void construct_from_2_ex_via_exvector(const ex & lh, const ex & rh);
126 void construct_from_2_ex(const ex & lh, const ex & rh);
127 void construct_from_2_expairseq(const expairseq & s1,
128 const expairseq & s2);
129 void construct_from_expairseq_ex(const expairseq & s,
131 void construct_from_exvector(const exvector & v);
132 void construct_from_epvector(const epvector & v, bool do_index_renaming = false);
133 void make_flat(const exvector & v);
134 void make_flat(const epvector & v, bool do_index_renaming = false);
136 void combine_same_terms_sorted_seq();
137 #if EXPAIRSEQ_USE_HASHTAB
138 void combine_same_terms();
139 unsigned calc_hashtabsize(unsigned sz) const;
140 unsigned calc_hashindex(const ex & e) const;
141 void shrink_hashtab();
142 void remove_hashtab_entry(epvector::const_iterator element);
143 void move_hashtab_entry(epvector::const_iterator oldpos,
144 epvector::iterator newpos);
145 void sorted_insert(epplist & eppl, epvector::const_iterator elem);
146 void build_hashtab_and_combine(epvector::iterator & first_numeric,
147 epvector::iterator & last_non_zero,
148 vector<bool> & touched,
149 unsigned & number_of_zeroes);
150 void drop_coeff_0_terms(epvector::iterator & first_numeric,
151 epvector::iterator & last_non_zero,
152 vector<bool> & touched,
153 unsigned & number_of_zeroes);
154 bool has_coeff_0() const;
155 void add_numerics_to_hashtab(epvector::iterator first_numeric,
156 epvector::const_iterator last_non_zero);
157 #endif // EXPAIRSEQ_USE_HASHTAB
158 bool is_canonical() const;
159 std::auto_ptr<epvector> expandchildren(unsigned options) const;
160 std::auto_ptr<epvector> evalchildren(int level) const;
161 std::auto_ptr<epvector> subschildren(const exmap & m, unsigned options = 0) const;
168 #if EXPAIRSEQ_USE_HASHTAB
169 epplistvector hashtab;
170 unsigned hashtabsize;
172 static unsigned maxhashtabsize;
173 static unsigned minhashtabsize;
174 static unsigned hashtabfactor;
175 #endif // EXPAIRSEQ_USE_HASHTAB
178 /** Class to handle the renaming of dummy indices. It holds a vector of
179 * indices that are being used in the expression so-far. If the same
180 * index occurs again as a dummy index in a factor, it is to be renamed.
181 * Unless dummy index renaming was swichted of, of course ;-) . */
182 class make_flat_inserter
185 make_flat_inserter(const epvector &epv, bool b): do_renaming(b)
189 for (epvector::const_iterator i=epv.begin(); i!=epv.end(); ++i)
190 if(are_ex_trivially_equal(i->coeff, 1))
191 combine_indices(i->rest.get_free_indices());
193 make_flat_inserter(const exvector &v, bool b): do_renaming(b)
197 for (exvector::const_iterator i=v.begin(); i!=v.end(); ++i)
198 combine_indices(i->get_free_indices());
200 ex handle_factor(const ex &x, const ex &coeff)
204 exvector dummies_of_factor;
205 if (is_a<numeric>(coeff) && coeff.is_equal(GiNaC::numeric(1)))
206 dummies_of_factor = get_all_dummy_indices_safely(x);
207 else if (is_a<numeric>(coeff) && coeff.is_equal(GiNaC::numeric(2)))
208 dummies_of_factor = x.get_free_indices();
211 if (dummies_of_factor.size() == 0)
213 sort(dummies_of_factor.begin(), dummies_of_factor.end(), ex_is_less());
214 ex new_factor = rename_dummy_indices_uniquely(used_indices,
215 dummies_of_factor, x);
216 combine_indices(dummies_of_factor);
220 void combine_indices(const exvector &dummies_of_factor)
222 exvector new_dummy_indices;
223 set_union(used_indices.begin(), used_indices.end(),
224 dummies_of_factor.begin(), dummies_of_factor.end(),
225 std::back_insert_iterator<exvector>(new_dummy_indices), ex_is_less());
226 used_indices.swap(new_dummy_indices);
229 exvector used_indices;
234 #endif // ndef GINAC_EXPAIRSEQ_H