]> www.ginac.de Git - ginac.git/blob - ginac/expairseq.h
- Made determinant_algo (in flags.h) really work.
[ginac.git] / ginac / expairseq.h
1 /** @file expairseq.h
2  *
3  *  Interface to sequences of expression pairs. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2000 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
29 // CINT needs <algorithm> to work properly with <vector> and <list>
30 #include <algorithm>
31
32 #include "expair.h"
33
34 #ifndef NO_NAMESPACE_GINAC
35 namespace GiNaC {
36 #endif // ndef NO_NAMESPACE_GINAC
37
38 //#define EXPAIRSEQ_USE_HASHTAB
39
40 typedef std::vector<expair> epvector;
41 typedef epvector::iterator epviter;
42
43 inline void iter_swap(epvector::iterator it1, epvector::iterator it2)
44 {
45     (*it1).rest.swap((*it2).rest);
46     (*it1).coeff.swap((*it2).coeff);
47 }
48
49 typedef epvector::iterator epp;
50 typedef std::list<epp> epplist;
51 typedef std::vector<epplist> epplistvector;
52
53 /** A sequence of class expair.
54  *  This is used for time-critical classes like sums and products of terms
55  *  since handling a list of coeff and rest is much faster than handling a
56  *  list of products or powers, respectively. (Incidentally, Maple does it
57  *  the same way.) */
58 class expairseq : public basic
59 {
60     GINAC_DECLARE_REGISTERED_CLASS(expairseq, basic)
61
62 // member functions
63
64     // default constructor, destructor, copy constructor assignment operator and helpers
65 public:
66     expairseq() : basic(TINFO_expairseq)
67 #ifdef EXPAIRSEQ_USE_HASHTAB
68         , hashtabsize(0)
69 #endif // def EXPAIRSEQ_USE_HASHTAB
70         {
71         }
72     ~expairseq()
73         {
74             destroy(0);
75         }
76     expairseq(const expairseq & other);
77     const expairseq & operator=(const expairseq & other);
78 protected:
79     void copy(const expairseq & other);
80     void destroy(bool call_parent)
81         {
82             if (call_parent) basic::destroy(call_parent);
83         };
84
85     // other constructors
86 public:
87     expairseq(const ex & lh, const ex & rh);
88     expairseq(const exvector & v);
89     expairseq(const epvector & v, const ex & oc);
90     expairseq(epvector * vp, const ex & oc); // vp will be deleted
91
92     // functions overriding virtual functions from bases classes
93 public:
94     basic * duplicate() const;
95     void print(std::ostream & os, unsigned upper_precedence=0) const;
96     void printraw(std::ostream & os) const;
97     void printtree(std::ostream & os, unsigned indent) const;
98     bool info(unsigned inf) const;
99     unsigned nops() const;
100     ex op(int i) const;
101     ex & let_op(int i);
102     ex eval(int level=0) const;
103     ex evalf(int level=0) const;
104     ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
105     ex to_rational(lst &repl_lst) const;
106     ex subs(const lst & ls, const lst & lr) const;
107 protected:
108     ex derivative(const symbol & s) const;
109     int compare_same_type(const basic & other) const;
110     bool is_equal_same_type(const basic & other) const;
111     unsigned return_type(void) const;
112     unsigned calchash(void) const;
113     ex expand(unsigned options=0) const;
114
115     // new virtual functions which can be overridden by derived classes
116 protected:
117     virtual ex thisexpairseq(const epvector & v, const ex & oc) const;
118     virtual ex thisexpairseq(epvector * vp, const ex & oc) const;
119     virtual void printseq(std::ostream & os, char delim,
120                           unsigned this_precedence,
121                           unsigned upper_precedence) const;
122     virtual void printpair(std::ostream & os, const expair & p,
123                            unsigned upper_precedence) const;
124     virtual expair split_ex_to_pair(const ex & e) const;
125     virtual expair combine_ex_with_coeff_to_pair(const ex & e,
126                                                  const ex & c) const;
127     virtual expair combine_pair_with_coeff_to_pair(const expair & p,
128                                                    const ex & c) const;
129     virtual ex recombine_pair_to_ex(const expair & p) const;
130     virtual bool expair_needs_further_processing(epp it);
131     virtual ex default_overall_coeff(void) const;
132     virtual void combine_overall_coeff(const ex & c);
133     virtual void combine_overall_coeff(const ex & c1, const ex & c2);
134     virtual bool can_make_flat(const expair & p) const;
135     
136     // non-virtual functions in this class
137 protected:
138     void construct_from_2_ex_via_exvector(const ex & lh, const ex & rh);
139     void construct_from_2_ex(const ex & lh, const ex & rh);
140     void construct_from_2_expairseq(const expairseq & s1,
141                                     const expairseq & s2);
142     void construct_from_expairseq_ex(const expairseq & s,
143                                      const ex & e);
144     void construct_from_exvector(const exvector & v);
145     void construct_from_epvector(const epvector & v);
146     void make_flat(const exvector & v);
147     void make_flat(const epvector & v);
148     epvector * bubblesort(epvector::iterator itbegin, epvector::iterator itend);
149     epvector * mergesort(epvector::iterator itbegin, epvector::iterator itend);
150     void canonicalize(void);
151     void combine_same_terms_sorted_seq(void);
152 #ifdef EXPAIRSEQ_USE_HASHTAB
153     void combine_same_terms(void);
154     unsigned calc_hashtabsize(unsigned sz) const;
155     unsigned calc_hashindex(const ex & e) const;
156     void shrink_hashtab(void);
157     void remove_hashtab_entry(epvector::const_iterator element);
158     void move_hashtab_entry(epvector::const_iterator oldpos,
159                             epvector::iterator newpos);
160     void sorted_insert(epplist & eppl, epp elem);
161     void build_hashtab_and_combine(epvector::iterator & first_numeric,
162                                    epvector::iterator & last_non_zero,
163                                    vector<bool> & touched,
164                                    unsigned & number_of_zeroes);
165     void drop_coeff_0_terms(epvector::iterator & first_numeric,
166                             epvector::iterator & last_non_zero,
167                             vector<bool> & touched,
168                             unsigned & number_of_zeroes);
169     bool has_coeff_0(void) const;
170     void add_numerics_to_hashtab(epvector::iterator first_numeric,
171                                  epvector::const_iterator last_non_zero);
172 #endif // def EXPAIRSEQ_USE_HASHTAB
173     bool is_canonical() const;
174     epvector * expandchildren(unsigned options) const;
175     epvector * evalchildren(int level) const;
176     epvector evalfchildren(int level) const;
177     epvector normalchildren(int level) const;
178     epvector diffchildren(const symbol & s) const;
179     epvector * subschildren(const lst & ls, const lst & lr) const;
180     
181 // member variables
182     
183 protected:
184     epvector seq;
185     ex overall_coeff;
186     static unsigned precedence;
187 #ifdef EXPAIRSEQ_USE_HASHTAB
188     epplistvector hashtab;
189     unsigned hashtabsize;
190     unsigned hashmask;
191     static unsigned maxhashtabsize;
192     static unsigned minhashtabsize;
193     static unsigned hashtabfactor;
194 #endif // def EXPAIRSEQ_USE_HASHTAB
195 };
196
197 // global constants
198
199 extern const expairseq some_expairseq;
200 extern const type_info & typeid_expairseq;
201
202 // utility functions
203 inline const expairseq &ex_to_expairseq(const ex &e)
204 {
205         return static_cast<const expairseq &>(*e.bp);
206 }
207
208 #ifndef NO_NAMESPACE_GINAC
209 } // namespace GiNaC
210 #endif // ndef NO_NAMESPACE_GINAC
211
212 #endif // ndef __GINAC_EXPAIRSEQ_H__