]> www.ginac.de Git - ginac.git/blob - ginac/expairseq.h
Copyright goes 2010.
[ginac.git] / ginac / expairseq.h
1 /** @file expairseq.h
2  *
3  *  Interface to sequences of expression pairs. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2010 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         bool is_polynomial(const ex & var) const;
91
92         void archive(archive_node& n) const;
93         void read_archive(const archive_node& n, lst& syms);
94 protected:
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;
99         
100         // new virtual functions which can be overridden by derived classes
101 protected:
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,
111                                                                                                  const ex & c) const;
112         virtual expair combine_pair_with_coeff_to_pair(const expair & p,
113                                                                                                    const ex & c) const;
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;
120         
121         // non-virtual functions in this class
122 protected:
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,
130                                          const ex & e);
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);
135         void canonicalize();
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;
162         
163 // member variables
164         
165 protected:
166         epvector seq;
167         ex overall_coeff;
168 #if EXPAIRSEQ_USE_HASHTAB
169         epplistvector hashtab;
170         unsigned hashtabsize;
171         unsigned hashmask;
172         static unsigned maxhashtabsize;
173         static unsigned minhashtabsize;
174         static unsigned hashtabfactor;
175 #endif // EXPAIRSEQ_USE_HASHTAB
176 };
177
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
183 {
184         public:
185                 make_flat_inserter(const epvector &epv, bool b): do_renaming(b)
186                 {
187                         if (!do_renaming)
188                                 return;
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());
192                 }
193                 make_flat_inserter(const exvector &v, bool b): do_renaming(b)
194                 {
195                         if (!do_renaming)
196                                 return;
197                         for (exvector::const_iterator i=v.begin(); i!=v.end(); ++i)
198                                 combine_indices(i->get_free_indices());
199                 }
200                 ex handle_factor(const ex &x, const ex &coeff)
201                 {
202                         if (!do_renaming)
203                                 return x;
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();
209                         else
210                                 return x;
211                         if (dummies_of_factor.size() == 0)
212                                 return x;
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);
217                         return new_factor;
218                 }
219         private:
220                 void combine_indices(const exvector &dummies_of_factor)
221                 {
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);
227                 }
228                 bool do_renaming;
229                 exvector used_indices;
230 };
231
232 } // namespace GiNaC
233
234 #endif // ndef GINAC_EXPAIRSEQ_H