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