]> www.ginac.de Git - ginac.git/blob - ginac/indexed.h
[BUGFIX] Fix crash in parser.
[ginac.git] / ginac / indexed.h
1 /** @file indexed.h
2  *
3  *  Interface to GiNaC's indexed expressions. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2024 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_INDEXED_H
24 #define GINAC_INDEXED_H
25
26 #include "exprseq.h"
27 #include "wildcard.h"
28
29 #include <map>
30
31 namespace GiNaC {
32
33 class scalar_products;
34 class symmetry;
35
36 /** This class holds an indexed expression. It consists of a 'base' expression
37  *  (the expression being indexed) which can be accessed as op(0), and n (n >= 0)
38  *  indices (all of class idx), accessible as op(1)..op(n). */
39 class indexed : public exprseq
40 {
41         GINAC_DECLARE_REGISTERED_CLASS(indexed, exprseq)
42
43         friend ex simplify_indexed(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp);
44         friend ex simplify_indexed_product(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp);
45         friend bool reposition_dummy_indices(ex & e, exvector & variant_dummy_indices, exvector & moved_indices);
46
47         // other constructors
48 public:
49         /** Construct indexed object with no index.
50          *
51          *  @param b Base expression */
52         indexed(const ex & b);
53
54         /** Construct indexed object with one index. The index must be of class idx.
55          *
56          *  @param b Base expression
57          *  @param i1 The index */
58         indexed(const ex & b, const ex & i1);
59
60         /** Construct indexed object with two indices. The indices must be of class idx.
61          *
62          *  @param b Base expression
63          *  @param i1 First index
64          *  @param i2 Second index */
65         indexed(const ex & b, const ex & i1, const ex & i2);
66
67         /** Construct indexed object with three indices. The indices must be of class idx.
68          *
69          *  @param b Base expression
70          *  @param i1 First index
71          *  @param i2 Second index
72          *  @param i3 Third index */
73         indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3);
74
75         /** Construct indexed object with four indices. The indices must be of class idx.
76          *
77          *  @param b Base expression
78          *  @param i1 First index
79          *  @param i2 Second index
80          *  @param i3 Third index
81          *  @param i4 Fourth index */
82         indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3, const ex & i4);
83
84         /** Construct indexed object with two indices and a specified symmetry. The
85          *  indices must be of class idx.
86          *
87          *  @param b Base expression
88          *  @param symm Symmetry of indices
89          *  @param i1 First index
90          *  @param i2 Second index */
91         indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2);
92
93         /** Construct indexed object with three indices and a specified symmetry.
94          *  The indices must be of class idx.
95          *
96          *  @param b Base expression
97          *  @param symm Symmetry of indices
98          *  @param i1 First index
99          *  @param i2 Second index
100          *  @param i3 Third index */
101         indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3);
102
103         /** Construct indexed object with four indices and a specified symmetry. The
104          *  indices must be of class idx.
105          *
106          *  @param b Base expression
107          *  @param symm Symmetry of indices
108          *  @param i1 First index
109          *  @param i2 Second index
110          *  @param i3 Third index
111          *  @param i4 Fourth index */
112         indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3, const ex & i4);
113
114         /** Construct indexed object with a specified vector of indices. The indices
115          *  must be of class idx.
116          *
117          *  @param b Base expression
118          *  @param iv Vector of indices */
119         indexed(const ex & b, const exvector & iv);
120
121         /** Construct indexed object with a specified vector of indices and
122          *  symmetry. The indices must be of class idx.
123          *
124          *  @param b Base expression
125          *  @param symm Symmetry of indices
126          *  @param iv Vector of indices */
127         indexed(const ex & b, const symmetry & symm, const exvector & iv);
128
129         // internal constructors
130         indexed(const symmetry & symm, const exprseq & es);
131         indexed(const symmetry & symm, const exvector & v);
132         indexed(const symmetry & symm, exvector && v);
133
134         // functions overriding virtual functions from base classes
135 public:
136         unsigned precedence() const override {return 55;}
137         bool info(unsigned inf) const override;
138         ex eval() const override;
139         ex real_part() const override;
140         ex imag_part() const override;
141         exvector get_free_indices() const override;
142
143         /** Save (a.k.a. serialize) indexed object into archive. */
144         void archive(archive_node& n) const override;
145         /** Read (a.k.a. deserialize) indexed object from archive. */
146         void read_archive(const archive_node& n, lst& syms) override;
147 protected:
148         ex derivative(const symbol & s) const override;
149         ex thiscontainer(const exvector & v) const override;
150         ex thiscontainer(exvector && v) const override;
151         unsigned return_type() const override;
152         return_type_t return_type_tinfo() const override { return op(0).return_type_tinfo(); }
153         ex expand(unsigned options = 0) const override;
154
155         // new virtual functions which can be overridden by derived classes
156         // none
157         
158         // non-virtual functions in this class
159 public:
160         /** Check whether all index values have a certain property.
161          *  @see class info_flags */
162         bool all_index_values_are(unsigned inf) const;
163
164         /** Return a vector containing the object's indices. */
165         exvector get_indices() const;
166
167         /** Return a vector containing the dummy indices of the object, if any. */
168         exvector get_dummy_indices() const;
169
170         /** Return a vector containing the dummy indices in the contraction with
171          *  another indexed object. This is symmetric: a.get_dummy_indices(b)
172          *  == b.get_dummy_indices(a) */
173         exvector get_dummy_indices(const indexed & other) const;
174
175         /** Check whether the object has an index that forms a dummy index pair
176          *  with a given index. */
177         bool has_dummy_index_for(const ex & i) const;
178
179         /** Return symmetry properties. */
180         ex get_symmetry() const {return symtree;}
181
182 protected:
183         void printindices(const print_context & c, unsigned level) const;
184         void print_indexed(const print_context & c, const char *openbrace, const char *closebrace, unsigned level) const;
185         void do_print(const print_context & c, unsigned level) const;
186         void do_print_latex(const print_latex & c, unsigned level) const;
187         void do_print_tree(const print_tree & c, unsigned level) const;
188         void validate() const;
189
190         // member variables
191 protected:
192         ex symtree; /**< Index symmetry (tree of symmetry objects) */
193 };
194 GINAC_DECLARE_UNARCHIVER(indexed);
195
196
197 class spmapkey {
198 public:
199         spmapkey() : dim(wild()) {}
200         spmapkey(const ex & v1, const ex & v2, const ex & dim = wild());
201
202         bool operator==(const spmapkey &other) const;
203         bool operator<(const spmapkey &other) const;
204
205         void debugprint() const;
206
207 protected:
208         ex v1, v2, dim;
209 };
210
211 typedef std::map<spmapkey, ex> spmap;
212
213 /** Helper class for storing information about known scalar products which
214  *  are to be automatically replaced by simplify_indexed().
215  *
216  *  @see simplify_indexed */
217 class scalar_products {
218 public:
219         /** Register scalar product pair and its value. */
220         void add(const ex & v1, const ex & v2, const ex & sp);
221
222         /** Register scalar product pair and its value for a specific space dimension. */
223         void add(const ex & v1, const ex & v2, const ex & dim, const ex & sp);
224
225         /** Register list of vectors. This adds all possible pairs of products
226          *  a.i * b.i with the value a*b (note that this is not a scalar vector
227          *  product but an ordinary product of scalars). */
228         void add_vectors(const lst & l, const ex & dim = wild());
229
230         /** Clear all registered scalar products. */
231         void clear();
232
233         bool is_defined(const ex & v1, const ex & v2, const ex & dim) const;
234         ex evaluate(const ex & v1, const ex & v2, const ex & dim) const;
235         void debugprint() const;
236
237 protected:
238         spmap spm; /*< Map from defined scalar product pairs to their values */
239 };
240
241
242 // utility functions
243
244 /** Returns all dummy indices from the expression */
245 exvector get_all_dummy_indices(const ex & e);
246
247 /** More reliable version of the form. The former assumes that e is an
248   * expanded expression. */
249 exvector get_all_dummy_indices_safely(const ex & e);
250
251 /** Returns b with all dummy indices, which are listed in va, renamed 
252  *  if modify_va is set to TRUE all dummy indices of b will be appended to va */
253 ex rename_dummy_indices_uniquely(exvector & va, const ex & b, bool modify_va = false);
254
255 /** Returns b with all dummy indices, which are common with a, renamed */
256 ex rename_dummy_indices_uniquely(const ex & a, const ex & b);
257
258 /** Same as above, where va and vb contain the indices of a and b and are sorted */
259 ex rename_dummy_indices_uniquely(const exvector & va, const exvector & vb, const ex & b);
260
261 /** Similar to above, where va and vb are the same and the return value is a list of two lists 
262  *  for substitution in b */
263 lst rename_dummy_indices_uniquely(const exvector & va, const exvector & vb);
264
265 /** This function returns the given expression with expanded sums
266  *  for all dummy index summations, where the dimensionality of 
267  *  the dummy index is a nonnegative integer.
268  *  Optionally all indices with a variance will be substituted by 
269  *  indices with the corresponding numeric values without variance.
270  *
271  *  @param e the given expression
272  *  @param subs_idx indicates if variance of dummy indices should be neglected
273  */
274 ex expand_dummy_sum(const ex & e, bool subs_idx = false);
275
276 } // namespace GiNaC
277
278 #endif // ndef GINAC_INDEXED_H