]> www.ginac.de Git - ginac.git/blob - ginac/expairseq.h
- Partially solves performance regression in expand(), gcd(), etc [Sheplyakov].
[ginac.git] / ginac / expairseq.h
1 /** @file expairseq.h
2  *
3  *  Interface to sequences of expression pairs. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2007 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #ifndef __GINAC_EXPAIRSEQ_H__
24 #define __GINAC_EXPAIRSEQ_H__
25
26 #include <vector>
27 #include <list>
28 #include <memory>
29 // CINT needs <algorithm> to work properly with <vector> and <list>
30 #include <algorithm>
31
32 #include "expair.h"
33 #include "indexed.h"
34
35 namespace GiNaC {
36
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
47
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
52
53 /** Complex conjugate every element of an epvector. Returns zero if this
54  *  does not change anything. */
55 epvector* conjugateepvector(const epvector&);
56
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
67 {
68         GINAC_DECLARE_REGISTERED_CLASS(expairseq, basic)
69
70         // other constructors
71 public:
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);
76         
77         // functions overriding virtual functions from base classes
78 public:
79         unsigned precedence() const {return 10;}
80         bool info(unsigned inf) const;
81         size_t nops() 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;
89         ex conjugate() const;
90         bool is_polynomial(const ex & var) const;
91 protected:
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;
96         
97         // new virtual functions which can be overridden by derived classes
98 protected:
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,
108                                                                                                  const ex & c) const;
109         virtual expair combine_pair_with_coeff_to_pair(const expair & p,
110                                                                                                    const ex & c) const;
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;
117         
118         // non-virtual functions in this class
119 protected:
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,
127                                          const ex & e);
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);
132         void canonicalize();
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;
159         
160 // member variables
161         
162 protected:
163         epvector seq;
164         ex overall_coeff;
165 #if EXPAIRSEQ_USE_HASHTAB
166         epplistvector hashtab;
167         unsigned hashtabsize;
168         unsigned hashmask;
169         static unsigned maxhashtabsize;
170         static unsigned minhashtabsize;
171         static unsigned hashtabfactor;
172 #endif // EXPAIRSEQ_USE_HASHTAB
173 };
174
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
180 {
181         public:
182                 make_flat_inserter(const epvector &epv, bool b): do_renaming(b)
183                 {
184                         if (!do_renaming)
185                                 return;
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());
189                 }
190                 make_flat_inserter(const exvector &v, bool b): do_renaming(b)
191                 {
192                         if (!do_renaming)
193                                 return;
194                         for (exvector::const_iterator i=v.begin(); i!=v.end(); ++i)
195                                 combine_indices(i->get_free_indices());
196                 }
197                 ex handle_factor(const ex &x, const ex &coeff)
198                 {
199                         if (!do_renaming)
200                                 return x;
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();
206                         else
207                                 return x;
208                         if (dummies_of_factor.size() == 0)
209                                 return x;
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);
214                         return new_factor;
215                 }
216         private:
217                 void combine_indices(const exvector &dummies_of_factor)
218                 {
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);
224                 }
225                 bool do_renaming;
226                 exvector used_indices;
227 };
228
229 } // namespace GiNaC
230
231 #endif // ndef __GINAC_EXPAIRSEQ_H__