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