]> www.ginac.de Git - ginac.git/blob - ginac/indexed.h
* AUTHORS: Add Chris Dams as contributor of patches.
[ginac.git] / ginac / indexed.h
1 /** @file indexed.h
2  *
3  *  Interface to GiNaC's indexed expressions. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2002 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #ifndef __GINAC_INDEXED_H__
24 #define __GINAC_INDEXED_H__
25
26 #include <map>
27
28 #include "exprseq.h"
29
30 namespace GiNaC {
31
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, exvector * vp); // vp will be deleted
143
144         // functions overriding virtual functions from base classes
145 public:
146         void print(const print_context & c, unsigned level = 0) const;
147         unsigned precedence(void) const {return 55;}
148         bool info(unsigned inf) const;
149         ex eval(int level = 0) const;
150         exvector get_free_indices(void) const;
151
152 protected:
153         ex derivative(const symbol & s) const;
154         ex thisexprseq(const exvector & v) const;
155         ex thisexprseq(exvector * vp) const;
156         unsigned return_type(void) const { return return_types::commutative; }
157         ex expand(unsigned options = 0) const;
158
159         // new virtual functions which can be overridden by derived classes
160         // none
161         
162         // non-virtual functions in this class
163 public:
164         /** Check whether all index values have a certain property.
165          *  @see class info_flags */
166         bool all_index_values_are(unsigned inf) const;
167
168         /** Return a vector containing the object's indices. */
169         exvector get_indices(void) const;
170
171         /** Return a vector containing the dummy indices of the object, if any. */
172         exvector get_dummy_indices(void) const;
173
174         /** Return a vector containing the dummy indices in the contraction with
175          *  another indexed object. */
176         exvector get_dummy_indices(const indexed & other) const;
177
178         /** Check whether the object has an index that forms a dummy index pair
179          *  with a given index. */
180         bool has_dummy_index_for(const ex & i) const;
181
182         /** Return symmetry properties. */
183         ex get_symmetry(void) const {return symtree;}
184
185 protected:
186         void printindices(const print_context & c, unsigned level) const;
187         void validate(void) const;
188
189         // member variables
190 protected:
191         ex symtree; /**< Index symmetry (tree of symmetry objects) */
192 };
193
194
195 typedef std::pair<ex, ex> spmapkey;
196
197 struct spmapkey_is_less {
198         bool operator() (const spmapkey &p, const spmapkey &q) const 
199         {
200                 int cmp = p.first.compare(q.first);
201                 return ((cmp<0) || (!(cmp>0) && p.second.compare(q.second)<0));
202         }
203 };
204
205 typedef std::map<spmapkey, ex, spmapkey_is_less> spmap;
206
207 /** Helper class for storing information about known scalar products which
208  *  are to be automatically replaced by simplify_indexed().
209  *
210  *  @see simplify_indexed */
211 class scalar_products {
212 public:
213         /** Register scalar product pair and its value. */
214         void add(const ex & v1, const ex & v2, const ex & sp);
215
216         /** Register list of vectors. This adds all possible pairs of products
217          *  a.i * b.i with the value a*b (note that this is not a scalar vector
218          *  product but an ordinary product of scalars). */
219         void add_vectors(const lst & l);
220
221         /** Clear all registered scalar products. */
222         void clear(void);
223
224         bool is_defined(const ex & v1, const ex & v2) const;
225         ex evaluate(const ex & v1, const ex & v2) const;
226         void debugprint(void) const;
227
228 private:
229         static spmapkey make_key(const ex & v1, const ex & v2);
230
231         spmap spm; /*< Map from defined scalar product pairs to their values */
232 };
233
234
235 // utility functions
236
237 /** Specialization of is_exactly_a<indexed>(obj) for indexed objects. */
238 template<> inline bool is_exactly_a<indexed>(const basic & obj)
239 {
240         return obj.tinfo()==TINFO_indexed;
241 }
242
243 } // namespace GiNaC
244
245 #endif // ndef __GINAC_INDEXED_H__