]> www.ginac.de Git - ginac.git/blob - ginac/expairseq.h
- permutation_sign() and shaker_sort() are no longer inlined
[ginac.git] / ginac / expairseq.h
1 /** @file expairseq.h
2  *
3  *  Interface to sequences of expression pairs. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 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 epviter;         ///< expair-vector iterator
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_NO_CTORS(expairseq, basic)
64
65 // member functions
66
67         // default ctor, dtor, copy ctor assignment operator and helpers
68 public:
69         expairseq() : basic(TINFO_expairseq)
70 #if EXPAIRSEQ_USE_HASHTAB
71                                             , hashtabsize(0)
72 #endif // EXPAIRSEQ_USE_HASHTAB
73         { }
74         ~expairseq() { destroy(false); }
75         expairseq(const expairseq & other);
76         const expairseq & operator=(const expairseq & other);
77 protected:
78         void copy(const expairseq & other);
79         void destroy(bool call_parent);
80         // other ctors
81 public:
82         expairseq(const ex & lh, const ex & rh);
83         expairseq(const exvector & v);
84         expairseq(const epvector & v, const ex & oc);
85         expairseq(epvector * vp, const ex & oc); // vp will be deleted
86         
87         // functions overriding virtual functions from bases classes
88 public:
89         basic * duplicate() const;
90         void print(const print_context & c, unsigned level = 0) const;
91         unsigned precedence(void) const {return 10;}
92         bool info(unsigned inf) const;
93         unsigned nops() const;
94         ex op(int i) const;
95         ex & let_op(int i);
96         ex eval(int level=0) const;
97         ex evalf(int level=0) const;
98         ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
99         ex to_rational(lst &repl_lst) const;
100         bool match(const ex & pattern, lst & repl_lst) const;
101         ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const;
102 protected:
103         ex derivative(const symbol & s) const;
104         int compare_same_type(const basic & other) const;
105         bool is_equal_same_type(const basic & other) const;
106         unsigned return_type(void) const;
107         unsigned calchash(void) const;
108         ex expand(unsigned options=0) const;
109         
110         // new virtual functions which can be overridden by derived classes
111 protected:
112         virtual ex thisexpairseq(const epvector & v, const ex & oc) const;
113         virtual ex thisexpairseq(epvector * vp, const ex & oc) const;
114         virtual void printseq(const print_context & c, char delim,
115                               unsigned this_precedence,
116                               unsigned upper_precedence) const;
117         virtual void printpair(const print_context & c, const expair & p,
118                                unsigned upper_precedence) const;
119         virtual expair split_ex_to_pair(const ex & e) const;
120         virtual expair combine_ex_with_coeff_to_pair(const ex & e,
121                                                                                                  const ex & c) const;
122         virtual expair combine_pair_with_coeff_to_pair(const expair & p,
123                                                                                                    const ex & c) const;
124         virtual ex recombine_pair_to_ex(const expair & p) const;
125         virtual bool expair_needs_further_processing(epp it);
126         virtual ex default_overall_coeff(void) const;
127         virtual void combine_overall_coeff(const ex & c);
128         virtual void combine_overall_coeff(const ex & c1, const ex & c2);
129         virtual bool can_make_flat(const expair & p) const;
130         
131         // non-virtual functions in this class
132 protected:
133         void construct_from_2_ex_via_exvector(const ex & lh, const ex & rh);
134         void construct_from_2_ex(const ex & lh, const ex & rh);
135         void construct_from_2_expairseq(const expairseq & s1,
136                                         const expairseq & s2);
137         void construct_from_expairseq_ex(const expairseq & s,
138                                          const ex & e);
139         void construct_from_exvector(const exvector & v);
140         void construct_from_epvector(const epvector & v);
141         void make_flat(const exvector & v);
142         void make_flat(const epvector & v);
143         void canonicalize(void);
144         void combine_same_terms_sorted_seq(void);
145 #if EXPAIRSEQ_USE_HASHTAB
146         void combine_same_terms(void);
147         unsigned calc_hashtabsize(unsigned sz) const;
148         unsigned calc_hashindex(const ex & e) const;
149         void shrink_hashtab(void);
150         void remove_hashtab_entry(epvector::const_iterator element);
151         void move_hashtab_entry(epvector::const_iterator oldpos,
152                                 epvector::iterator newpos);
153         void sorted_insert(epplist & eppl, epp elem);
154         void build_hashtab_and_combine(epvector::iterator & first_numeric,
155                                        epvector::iterator & last_non_zero,
156                                        vector<bool> & touched,
157                                        unsigned & number_of_zeroes);
158         void drop_coeff_0_terms(epvector::iterator & first_numeric,
159                                 epvector::iterator & last_non_zero,
160                                 vector<bool> & touched,
161                                 unsigned & number_of_zeroes);
162         bool has_coeff_0(void) const;
163         void add_numerics_to_hashtab(epvector::iterator first_numeric,
164                                      epvector::const_iterator last_non_zero);
165 #endif // EXPAIRSEQ_USE_HASHTAB
166         bool is_canonical() const;
167         epvector * expandchildren(unsigned options) const;
168         epvector * evalchildren(int level) const;
169         epvector evalfchildren(int level) const;
170         epvector normalchildren(int level) const;
171         epvector diffchildren(const symbol & s) const;
172         epvector * subschildren(const lst & ls, const lst & lr, bool no_pattern = false) const;
173         
174 // member variables
175         
176 protected:
177         epvector seq;
178         ex overall_coeff;
179 #if EXPAIRSEQ_USE_HASHTAB
180         epplistvector hashtab;
181         unsigned hashtabsize;
182         unsigned hashmask;
183         static unsigned maxhashtabsize;
184         static unsigned minhashtabsize;
185         static unsigned hashtabfactor;
186 #endif // EXPAIRSEQ_USE_HASHTAB
187 };
188
189 // utility functions
190 inline const expairseq &ex_to_expairseq(const ex &e)
191 {
192         return static_cast<const expairseq &>(*e.bp);
193 }
194
195 } // namespace GiNaC
196
197 #endif // ndef __GINAC_EXPAIRSEQ_H__