]> www.ginac.de Git - ginac.git/blob - ginac/expairseq.h
Fix algebraic power::has() for larger integer exponents.
[ginac.git] / ginac / expairseq.h
1 /** @file expairseq.h
2  *
3  *  Interface to sequences of expression pairs. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2015 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 "expair.h"
27 #include "indexed.h"
28
29 // CINT needs <algorithm> to work properly with <vector> and <list>
30 #include <algorithm>
31 #include <list>
32 #include <memory>
33 #include <vector>
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, exmap& repl_lst) const;
88         ex subs(const exmap & m, unsigned options = 0) const;
89         ex conjugate() const;
90
91         void archive(archive_node& n) const;
92         void read_archive(const archive_node& n, lst& syms);
93 protected:
94         bool is_equal_same_type(const basic & other) const;
95         unsigned return_type() const;
96         unsigned calchash() const;
97         ex expand(unsigned options=0) const;
98         
99         // new virtual functions which can be overridden by derived classes
100 protected:
101         virtual ex thisexpairseq(const epvector & v, const ex & oc, bool do_index_renaming = false) const;
102         virtual ex thisexpairseq(std::auto_ptr<epvector> vp, const ex & oc, bool do_index_renaming = false) const;
103         virtual void printseq(const print_context & c, char delim,
104                               unsigned this_precedence,
105                               unsigned upper_precedence) const;
106         virtual void printpair(const print_context & c, const expair & p,
107                                unsigned upper_precedence) const;
108         virtual expair split_ex_to_pair(const ex & e) const;
109         virtual expair combine_ex_with_coeff_to_pair(const ex & e,
110                                                      const ex & c) const;
111         virtual expair combine_pair_with_coeff_to_pair(const expair & p,
112                                                        const ex & c) const;
113         virtual ex recombine_pair_to_ex(const expair & p) const;
114         virtual bool expair_needs_further_processing(epp it);
115         virtual ex default_overall_coeff() const;
116         virtual void combine_overall_coeff(const ex & c);
117         virtual void combine_overall_coeff(const ex & c1, const ex & c2);
118         virtual bool can_make_flat(const expair & p) const;
119         
120         // non-virtual functions in this class
121 protected:
122         void do_print(const print_context & c, unsigned level) const;
123         void do_print_tree(const print_tree & c, unsigned level) const;
124         void construct_from_2_ex_via_exvector(const ex & lh, const ex & rh);
125         void construct_from_2_ex(const ex & lh, const ex & rh);
126         void construct_from_2_expairseq(const expairseq & s1,
127                                         const expairseq & s2);
128         void construct_from_expairseq_ex(const expairseq & s,
129                                          const ex & e);
130         void construct_from_exvector(const exvector & v);
131         void construct_from_epvector(const epvector & v, bool do_index_renaming = false);
132         void make_flat(const exvector & v);
133         void make_flat(const epvector & v, bool do_index_renaming = false);
134         void canonicalize();
135         void combine_same_terms_sorted_seq();
136 #if EXPAIRSEQ_USE_HASHTAB
137         void combine_same_terms();
138         unsigned calc_hashtabsize(unsigned sz) const;
139         unsigned calc_hashindex(const ex & e) const;
140         void shrink_hashtab();
141         void remove_hashtab_entry(epvector::const_iterator element);
142         void move_hashtab_entry(epvector::const_iterator oldpos,
143                                 epvector::iterator newpos);
144         void sorted_insert(epplist & eppl, epvector::const_iterator elem);
145         void build_hashtab_and_combine(epvector::iterator & first_numeric,
146                                        epvector::iterator & last_non_zero,
147                                        vector<bool> & touched,
148                                        unsigned & number_of_zeroes);
149         void drop_coeff_0_terms(epvector::iterator & first_numeric,
150                                 epvector::iterator & last_non_zero,
151                                 vector<bool> & touched,
152                                 unsigned & number_of_zeroes);
153         bool has_coeff_0() const;
154         void add_numerics_to_hashtab(epvector::iterator first_numeric,
155                                      epvector::const_iterator last_non_zero);
156 #endif // EXPAIRSEQ_USE_HASHTAB
157         bool is_canonical() const;
158         std::auto_ptr<epvector> expandchildren(unsigned options) const;
159         std::auto_ptr<epvector> evalchildren(int level) const;
160         std::auto_ptr<epvector> subschildren(const exmap & m, unsigned options = 0) const;
161         
162 // member variables
163         
164 protected:
165         epvector seq;
166         ex overall_coeff;
167 #if EXPAIRSEQ_USE_HASHTAB
168         epplistvector hashtab;
169         unsigned hashtabsize;
170         unsigned hashmask;
171         static unsigned maxhashtabsize;
172         static unsigned minhashtabsize;
173         static unsigned hashtabfactor;
174 #endif // EXPAIRSEQ_USE_HASHTAB
175 };
176
177 /** Class to handle the renaming of dummy indices. It holds a vector of
178  *  indices that are being used in the expression so far. If the same
179  *  index occurs again as a dummy index in a factor, it is to be renamed.
180  *  Unless dummy index renaming was switched off, of course ;-) . */
181 class make_flat_inserter
182 {
183         public:
184                 make_flat_inserter(const epvector &epv, bool b): do_renaming(b)
185                 {
186                         if (!do_renaming)
187                                 return;
188                         for (epvector::const_iterator i=epv.begin(); i!=epv.end(); ++i)
189                                 if(are_ex_trivially_equal(i->coeff, 1))
190                                         combine_indices(i->rest.get_free_indices());
191                 }
192                 make_flat_inserter(const exvector &v, bool b): do_renaming(b)
193                 {
194                         if (!do_renaming)
195                                 return;
196                         for (exvector::const_iterator i=v.begin(); i!=v.end(); ++i)
197                                 combine_indices(i->get_free_indices());
198                 }
199                 ex handle_factor(const ex &x, const ex &coeff)
200                 {
201                         if (!do_renaming)
202                                 return x;
203                         exvector dummies_of_factor;
204                         if (is_a<numeric>(coeff) && coeff.is_equal(GiNaC::numeric(1)))
205                                 dummies_of_factor = get_all_dummy_indices_safely(x);
206                         else if (is_a<numeric>(coeff) && coeff.is_equal(GiNaC::numeric(2)))
207                                 dummies_of_factor = x.get_free_indices();
208                         else
209                                 return x;
210                         if (dummies_of_factor.size() == 0)
211                                 return x;
212                         sort(dummies_of_factor.begin(), dummies_of_factor.end(), ex_is_less());
213                         ex new_factor = rename_dummy_indices_uniquely(used_indices,
214                                 dummies_of_factor, x);
215                         combine_indices(dummies_of_factor);
216                         return new_factor;
217                 }
218         private:
219                 void combine_indices(const exvector &dummies_of_factor)
220                 {
221                         exvector new_dummy_indices;
222                         set_union(used_indices.begin(), used_indices.end(),
223                                 dummies_of_factor.begin(), dummies_of_factor.end(),
224                                 std::back_insert_iterator<exvector>(new_dummy_indices), ex_is_less());
225                         used_indices.swap(new_dummy_indices);
226                 }
227                 bool do_renaming;
228                 exvector used_indices;
229 };
230
231 } // namespace GiNaC
232
233 #endif // ndef GINAC_EXPAIRSEQ_H