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