]> www.ginac.de Git - ginac.git/blob - ginac/expairseq.h
Fix most remaining GCC compiler warnings.
[ginac.git] / ginac / expairseq.h
1 /** @file expairseq.h
2  *
3  *  Interface to sequences of expression pairs. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2015 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #ifndef GINAC_EXPAIRSEQ_H
24 #define GINAC_EXPAIRSEQ_H
25
26 #include "expair.h"
27 #include "indexed.h"
28
29 // CINT needs <algorithm> to work properly with <vector> and <list>
30 #include <algorithm>
31 #include <list>
32 #include <memory>
33 #include <vector>
34
35 namespace GiNaC {
36
37 typedef std::vector<expair> epvector;       ///< expair-vector
38 typedef epvector::iterator epp;             ///< expair-vector pointer
39 typedef std::list<epp> epplist;             ///< list of expair-vector pointers
40 typedef std::vector<epplist> epplistvector; ///< vector of epplist
41
42 /** Complex conjugate every element of an epvector. Returns zero if this
43  *  does not change anything. */
44 epvector* conjugateepvector(const epvector&);
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. (Not incidentally, Maple does it
50  *  the same way, maybe others too.)  The semantics is (at least) twofold:
51  *  one for addition and one for multiplication and several methods have to
52  *  be overridden by derived classes to reflect the change in semantics.
53  *  However, most functionality turns out to be shared between addition and
54  *  multiplication, which is the reason why there is this base class. */
55 class expairseq : public basic
56 {
57         GINAC_DECLARE_REGISTERED_CLASS(expairseq, basic)
58
59         // other constructors
60 public:
61         expairseq(const ex & lh, const ex & rh);
62         expairseq(const exvector & v);
63         expairseq(const epvector & v, const ex & oc, bool do_index_renaming = false);
64         expairseq(epvector && vp, const ex & oc, bool do_index_renaming = false);
65         
66         // functions overriding virtual functions from base classes
67 public:
68         unsigned precedence() const {return 10;}
69         bool info(unsigned inf) const;
70         size_t nops() const;
71         ex op(size_t i) const;
72         ex map(map_function & f) const;
73         ex eval(int level=0) const;
74         ex to_rational(exmap & repl) const;
75         ex to_polynomial(exmap & repl) const;
76         bool match(const ex & pattern, exmap& repl_lst) const;
77         ex subs(const exmap & m, unsigned options = 0) const;
78         ex conjugate() const;
79
80         void archive(archive_node& n) const;
81         void read_archive(const archive_node& n, lst& syms);
82 protected:
83         bool is_equal_same_type(const basic & other) const;
84         unsigned return_type() const;
85         unsigned calchash() const;
86         ex expand(unsigned options=0) const;
87         
88         // new virtual functions which can be overridden by derived classes
89 protected:
90         virtual ex thisexpairseq(const epvector & v, const ex & oc, bool do_index_renaming = false) const;
91         virtual ex thisexpairseq(epvector && vp, const ex & oc, bool do_index_renaming = false) const;
92         virtual void printseq(const print_context & c, char delim,
93                               unsigned this_precedence,
94                               unsigned upper_precedence) const;
95         virtual void printpair(const print_context & c, const expair & p,
96                                unsigned upper_precedence) const;
97         virtual expair split_ex_to_pair(const ex & e) const;
98         virtual expair combine_ex_with_coeff_to_pair(const ex & e,
99                                                      const ex & c) const;
100         virtual expair combine_pair_with_coeff_to_pair(const expair & p,
101                                                        const ex & c) const;
102         virtual ex recombine_pair_to_ex(const expair & p) const;
103         virtual bool expair_needs_further_processing(epp it);
104         virtual ex default_overall_coeff() const;
105         virtual void combine_overall_coeff(const ex & c);
106         virtual void combine_overall_coeff(const ex & c1, const ex & c2);
107         virtual bool can_make_flat(const expair & p) const;
108         
109         // non-virtual functions in this class
110 protected:
111         void do_print(const print_context & c, unsigned level) const;
112         void do_print_tree(const print_tree & c, unsigned level) const;
113         void construct_from_2_ex_via_exvector(const ex & lh, const ex & rh);
114         void construct_from_2_ex(const ex & lh, const ex & rh);
115         void construct_from_2_expairseq(const expairseq & s1,
116                                         const expairseq & s2);
117         void construct_from_expairseq_ex(const expairseq & s,
118                                          const ex & e);
119         void construct_from_exvector(const exvector & v);
120         void construct_from_epvector(const epvector & v, bool do_index_renaming = false);
121         void construct_from_epvector(epvector && v, bool do_index_renaming = false);
122         void make_flat(const exvector & v);
123         void make_flat(const epvector & v, bool do_index_renaming = false);
124         void canonicalize();
125         void combine_same_terms_sorted_seq();
126         bool is_canonical() const;
127         epvector expandchildren(unsigned options) const;
128         epvector evalchildren(int level) const;
129         epvector subschildren(const exmap & m, unsigned options = 0) const;
130         
131 // member variables
132         
133 protected:
134         epvector seq;
135         ex overall_coeff;
136 };
137
138 /** Class to handle the renaming of dummy indices. It holds a vector of
139  *  indices that are being used in the expression so far. If the same
140  *  index occurs again as a dummy index in a factor, it is to be renamed.
141  *  Unless dummy index renaming was switched off, of course ;-) . */
142 class make_flat_inserter
143 {
144         public:
145                 make_flat_inserter(const epvector &epv, bool b): do_renaming(b)
146                 {
147                         if (!do_renaming)
148                                 return;
149                         for (epvector::const_iterator i=epv.begin(); i!=epv.end(); ++i)
150                                 if(are_ex_trivially_equal(i->coeff, 1))
151                                         combine_indices(i->rest.get_free_indices());
152                 }
153                 make_flat_inserter(const exvector &v, bool b): do_renaming(b)
154                 {
155                         if (!do_renaming)
156                                 return;
157                         for (exvector::const_iterator i=v.begin(); i!=v.end(); ++i)
158                                 combine_indices(i->get_free_indices());
159                 }
160                 ex handle_factor(const ex &x, const ex &coeff)
161                 {
162                         if (!do_renaming)
163                                 return x;
164                         exvector dummies_of_factor;
165                         if (is_a<numeric>(coeff) && coeff.is_equal(GiNaC::numeric(1)))
166                                 dummies_of_factor = get_all_dummy_indices_safely(x);
167                         else if (is_a<numeric>(coeff) && coeff.is_equal(GiNaC::numeric(2)))
168                                 dummies_of_factor = x.get_free_indices();
169                         else
170                                 return x;
171                         if (dummies_of_factor.size() == 0)
172                                 return x;
173                         sort(dummies_of_factor.begin(), dummies_of_factor.end(), ex_is_less());
174                         ex new_factor = rename_dummy_indices_uniquely(used_indices,
175                                 dummies_of_factor, x);
176                         combine_indices(dummies_of_factor);
177                         return new_factor;
178                 }
179         private:
180                 void combine_indices(const exvector &dummies_of_factor)
181                 {
182                         exvector new_dummy_indices;
183                         set_union(used_indices.begin(), used_indices.end(),
184                                 dummies_of_factor.begin(), dummies_of_factor.end(),
185                                 std::back_insert_iterator<exvector>(new_dummy_indices), ex_is_less());
186                         used_indices.swap(new_dummy_indices);
187                 }
188                 bool do_renaming;
189                 exvector used_indices;
190 };
191
192 } // namespace GiNaC
193
194 #endif // ndef GINAC_EXPAIRSEQ_H