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