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