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