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