3 * Interface to sequences of expression pairs. */
6 * GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
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.
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.
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
23 #ifndef __GINAC_EXPAIRSEQ_H__
24 #define __GINAC_EXPAIRSEQ_H__
29 // CINT needs <algorithm> to work properly with <vector> and <list>
36 /** Using hash tables can potentially enhance the asymptotic behaviour of
37 * combining n terms into one large sum (or n terms into one large product)
38 * from O(n*log(n)) to about O(n). There are, however, several drawbacks.
39 * The constant in front of O(n) is quite large, when copying such an object
40 * one also has to copy the has table, comparison is quite expensive because
41 * there is no ordering any more, it doesn't help at all when combining two
42 * expairseqs because due to the presorted nature the behaviour would be
43 * O(n) anyways, the code is quite messy, etc, etc. The code is here as
44 * an example for following generations to tinker with. */
45 #define EXPAIRSEQ_USE_HASHTAB 0
47 typedef std::vector<expair> epvector; ///< expair-vector
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
52 /** Complex conjugate every element of an epvector. Returns zero if this
53 * does not change anything. */
54 epvector* conjugateepvector(const epvector&);
56 /** A sequence of class expair.
57 * This is used for time-critical classes like sums and products of terms
58 * since handling a list of coeff and rest is much faster than handling a
59 * list of products or powers, respectively. (Not incidentally, Maple does it
60 * the same way, maybe others too.) The semantics is (at least) twofold:
61 * one for addition and one for multiplication and several methods have to
62 * be overridden by derived classes to reflect the change in semantics.
63 * However, most functionality turns out to be shared between addition and
64 * multiplication, which is the reason why there is this base class. */
65 class expairseq : public basic
67 GINAC_DECLARE_REGISTERED_CLASS(expairseq, basic)
71 expairseq(const ex & lh, const ex & rh);
72 expairseq(const exvector & v);
73 expairseq(const epvector & v, const ex & oc);
74 expairseq(std::auto_ptr<epvector>, const ex & oc);
76 // functions overriding virtual functions from base classes
78 unsigned precedence() const {return 10;}
79 bool info(unsigned inf) const;
81 ex op(size_t i) const;
82 ex map(map_function & f) const;
83 ex eval(int level=0) const;
84 ex to_rational(exmap & repl) const;
85 ex to_polynomial(exmap & repl) const;
86 bool match(const ex & pattern, lst & repl_lst) const;
87 ex subs(const exmap & m, unsigned options = 0) const;
90 bool is_equal_same_type(const basic & other) const;
91 unsigned return_type() const;
92 unsigned calchash() const;
93 ex expand(unsigned options=0) const;
95 // new virtual functions which can be overridden by derived classes
97 virtual ex thisexpairseq(const epvector & v, const ex & oc) const;
98 virtual ex thisexpairseq(std::auto_ptr<epvector> vp, const ex & oc) const;
99 virtual void printseq(const print_context & c, char delim,
100 unsigned this_precedence,
101 unsigned upper_precedence) const;
102 virtual void printpair(const print_context & c, const expair & p,
103 unsigned upper_precedence) const;
104 virtual expair split_ex_to_pair(const ex & e) const;
105 virtual expair combine_ex_with_coeff_to_pair(const ex & e,
107 virtual expair combine_pair_with_coeff_to_pair(const expair & p,
109 virtual ex recombine_pair_to_ex(const expair & p) const;
110 virtual bool expair_needs_further_processing(epp it);
111 virtual ex default_overall_coeff() const;
112 virtual void combine_overall_coeff(const ex & c);
113 virtual void combine_overall_coeff(const ex & c1, const ex & c2);
114 virtual bool can_make_flat(const expair & p) const;
116 // non-virtual functions in this class
118 void do_print(const print_context & c, unsigned level) const;
119 void do_print_tree(const print_tree & c, unsigned level) const;
120 void construct_from_2_ex_via_exvector(const ex & lh, const ex & rh);
121 void construct_from_2_ex(const ex & lh, const ex & rh);
122 void construct_from_2_expairseq(const expairseq & s1,
123 const expairseq & s2);
124 void construct_from_expairseq_ex(const expairseq & s,
126 void construct_from_exvector(const exvector & v);
127 void construct_from_epvector(const epvector & v);
128 void make_flat(const exvector & v);
129 void make_flat(const epvector & v);
131 void combine_same_terms_sorted_seq();
132 #if EXPAIRSEQ_USE_HASHTAB
133 void combine_same_terms();
134 unsigned calc_hashtabsize(unsigned sz) const;
135 unsigned calc_hashindex(const ex & e) const;
136 void shrink_hashtab();
137 void remove_hashtab_entry(epvector::const_iterator element);
138 void move_hashtab_entry(epvector::const_iterator oldpos,
139 epvector::iterator newpos);
140 void sorted_insert(epplist & eppl, epvector::const_iterator elem);
141 void build_hashtab_and_combine(epvector::iterator & first_numeric,
142 epvector::iterator & last_non_zero,
143 vector<bool> & touched,
144 unsigned & number_of_zeroes);
145 void drop_coeff_0_terms(epvector::iterator & first_numeric,
146 epvector::iterator & last_non_zero,
147 vector<bool> & touched,
148 unsigned & number_of_zeroes);
149 bool has_coeff_0() const;
150 void add_numerics_to_hashtab(epvector::iterator first_numeric,
151 epvector::const_iterator last_non_zero);
152 #endif // EXPAIRSEQ_USE_HASHTAB
153 bool is_canonical() const;
154 std::auto_ptr<epvector> expandchildren(unsigned options) const;
155 std::auto_ptr<epvector> evalchildren(int level) const;
156 std::auto_ptr<epvector> subschildren(const exmap & m, unsigned options = 0) const;
163 #if EXPAIRSEQ_USE_HASHTAB
164 epplistvector hashtab;
165 unsigned hashtabsize;
167 static unsigned maxhashtabsize;
168 static unsigned minhashtabsize;
169 static unsigned hashtabfactor;
170 #endif // EXPAIRSEQ_USE_HASHTAB
175 /** Specialization of is_exactly_a<expairseq>(obj) for expairseq objects. */
176 template<> inline bool is_exactly_a<expairseq>(const basic & obj)
178 return obj.tinfo()==TINFO_expairseq;
183 #endif // ndef __GINAC_EXPAIRSEQ_H__