]> www.ginac.de Git - ginac.git/blob - ginac/expairseq.h
Synced to HEAD.
[ginac.git] / ginac / expairseq.h
1 /** @file expairseq.h
2  *
3  *  Interface to sequences of expression pairs. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2003 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
34 namespace GiNaC {
35
36 /** Using hash tables can potentially enhance the asymptotic behaviour of
37  *  combining n terms into one large sum (or n terms into one large product)
38  *  from O(n*log(n)) to about O(n).  There are, however, several drawbacks.
39  *  The constant in front of O(n) is quite large, when copying such an object
40  *  one also has to copy the has table, comparison is quite expensive because
41  *  there is no ordering any more, it doesn't help at all when combining two
42  *  expairseqs because due to the presorted nature the behaviour would be
43  *  O(n) anyways, the code is quite messy, etc, etc.  The code is here as
44  *  an example for following generations to tinker with. */
45 #define EXPAIRSEQ_USE_HASHTAB 0
46
47 typedef std::vector<expair> epvector;       ///< expair-vector
48 typedef epvector::iterator epp;             ///< expair-vector pointer
49 typedef std::list<epp> epplist;             ///< list of expair-vector pointers
50 typedef std::vector<epplist> epplistvector; ///< vector of epplist
51
52 /** A sequence of class expair.
53  *  This is used for time-critical classes like sums and products of terms
54  *  since handling a list of coeff and rest is much faster than handling a
55  *  list of products or powers, respectively. (Not incidentally, Maple does it
56  *  the same way, maybe others too.)  The semantics is (at least) twofold:
57  *  one for addition and one for multiplication and several methods have to
58  *  be overridden by derived classes to reflect the change in semantics.
59  *  However, most functionality turns out to be shared between addition and
60  *  multiplication, which is the reason why there is this base class. */
61 class expairseq : public basic
62 {
63         GINAC_DECLARE_REGISTERED_CLASS(expairseq, basic)
64
65         // other constructors
66 public:
67         expairseq(const ex & lh, const ex & rh);
68         expairseq(const exvector & v);
69         expairseq(const epvector & v, const ex & oc);
70         expairseq(std::auto_ptr<epvector>, const ex & oc);
71         
72         // functions overriding virtual functions from base classes
73 public:
74         unsigned precedence() const {return 10;}
75         bool info(unsigned inf) const;
76         size_t nops() const;
77         ex op(size_t i) const;
78         ex map(map_function & f) const;
79         ex eval(int level=0) const;
80         ex to_rational(exmap & repl) const;
81         ex to_polynomial(exmap & repl) const;
82         bool match(const ex & pattern, lst & repl_lst) const;
83         ex subs(const exmap & m, unsigned options = 0) const;
84 protected:
85         bool is_equal_same_type(const basic & other) const;
86         unsigned return_type() const;
87         unsigned calchash() const;
88         ex expand(unsigned options=0) const;
89         
90         // new virtual functions which can be overridden by derived classes
91 protected:
92         virtual ex thisexpairseq(const epvector & v, const ex & oc) const;
93         virtual ex thisexpairseq(std::auto_ptr<epvector> vp, const ex & oc) const;
94         virtual void printseq(const print_context & c, char delim,
95                               unsigned this_precedence,
96                               unsigned upper_precedence) const;
97         virtual void printpair(const print_context & c, const expair & p,
98                                unsigned upper_precedence) const;
99         virtual expair split_ex_to_pair(const ex & e) const;
100         virtual expair combine_ex_with_coeff_to_pair(const ex & e,
101                                                                                                  const ex & c) const;
102         virtual expair combine_pair_with_coeff_to_pair(const expair & p,
103                                                                                                    const ex & c) const;
104         virtual ex recombine_pair_to_ex(const expair & p) const;
105         virtual bool expair_needs_further_processing(epp it);
106         virtual ex default_overall_coeff() const;
107         virtual void combine_overall_coeff(const ex & c);
108         virtual void combine_overall_coeff(const ex & c1, const ex & c2);
109         virtual bool can_make_flat(const expair & p) const;
110         
111         // non-virtual functions in this class
112 protected:
113         void do_print(const print_context & c, unsigned level) const;
114         void do_print_tree(const print_tree & c, unsigned level) const;
115         void construct_from_2_ex_via_exvector(const ex & lh, const ex & rh);
116         void construct_from_2_ex(const ex & lh, const ex & rh);
117         void construct_from_2_expairseq(const expairseq & s1,
118                                         const expairseq & s2);
119         void construct_from_expairseq_ex(const expairseq & s,
120                                          const ex & e);
121         void construct_from_exvector(const exvector & v);
122         void construct_from_epvector(const epvector & v);
123         void make_flat(const exvector & v);
124         void make_flat(const epvector & v);
125         void canonicalize();
126         void combine_same_terms_sorted_seq();
127 #if EXPAIRSEQ_USE_HASHTAB
128         void combine_same_terms();
129         unsigned calc_hashtabsize(unsigned sz) const;
130         unsigned calc_hashindex(const ex & e) const;
131         void shrink_hashtab();
132         void remove_hashtab_entry(epvector::const_iterator element);
133         void move_hashtab_entry(epvector::const_iterator oldpos,
134                                 epvector::iterator newpos);
135         void sorted_insert(epplist & eppl, epvector::const_iterator elem);
136         void build_hashtab_and_combine(epvector::iterator & first_numeric,
137                                        epvector::iterator & last_non_zero,
138                                        vector<bool> & touched,
139                                        unsigned & number_of_zeroes);
140         void drop_coeff_0_terms(epvector::iterator & first_numeric,
141                                 epvector::iterator & last_non_zero,
142                                 vector<bool> & touched,
143                                 unsigned & number_of_zeroes);
144         bool has_coeff_0() const;
145         void add_numerics_to_hashtab(epvector::iterator first_numeric,
146                                      epvector::const_iterator last_non_zero);
147 #endif // EXPAIRSEQ_USE_HASHTAB
148         bool is_canonical() const;
149         std::auto_ptr<epvector> expandchildren(unsigned options) const;
150         std::auto_ptr<epvector> evalchildren(int level) const;
151         std::auto_ptr<epvector> subschildren(const exmap & m, unsigned options = 0) const;
152         
153 // member variables
154         
155 protected:
156         epvector seq;
157         ex overall_coeff;
158 #if EXPAIRSEQ_USE_HASHTAB
159         epplistvector hashtab;
160         unsigned hashtabsize;
161         unsigned hashmask;
162         static unsigned maxhashtabsize;
163         static unsigned minhashtabsize;
164         static unsigned hashtabfactor;
165 #endif // EXPAIRSEQ_USE_HASHTAB
166 };
167
168 // utility functions
169
170 /** Specialization of is_exactly_a<expairseq>(obj) for expairseq objects. */
171 template<> inline bool is_exactly_a<expairseq>(const basic & obj)
172 {
173         return obj.tinfo()==TINFO_expairseq;
174 }
175
176 } // namespace GiNaC
177
178 #endif // ndef __GINAC_EXPAIRSEQ_H__