]> www.ginac.de Git - ginac.git/blob - ginac/indexed.h
b15ed8b30ec892975e322178fb6f32da7f0b222a
[ginac.git] / ginac / indexed.h
1 /** @file indexed.h
2  *
3  *  Interface to GiNaC's indexed expressions. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 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
35 /** This class holds an indexed expression. It consists of a 'base' expression
36  *  (the expression being indexed) which can be accessed as op(0), and n (n >= 0)
37  *  indices (all of class idx), accessible as op(1)..op(n). */
38 class indexed : public exprseq
39 {
40         GINAC_DECLARE_REGISTERED_CLASS(indexed, exprseq)
41
42         friend ex simplify_indexed(const ex & e, exvector & free_indices, const scalar_products & sp);
43         friend ex simplify_indexed_product(const ex & e, exvector & free_indices, const scalar_products & sp);
44
45         // types
46 public:
47         /** Type of symmetry of the object with respect to commutation of its indices. */
48         typedef enum {
49                 unknown,       /**< symmetry properties unknown */
50                 symmetric,     /**< totally symmetric */
51                 antisymmetric, /**< totally antisymmetric */
52                 mixed          /**< mixed symmetry (unimplemented) */
53         } symmetry_type;
54
55         // other constructors
56 public:
57         /** Construct indexed object with no index.
58          *
59          *  @param b Base expression
60          *  @return newly constructed indexed object */
61         indexed(const ex & b);
62
63         /** Construct indexed object with one index. The index must be of class idx.
64          *
65          *  @param b Base expression
66          *  @param i1 The index
67          *  @return newly constructed indexed object */
68         indexed(const ex & b, const ex & i1);
69
70         /** Construct indexed object with two 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          *  @return newly constructed indexed object */
76         indexed(const ex & b, const ex & i1, const ex & i2);
77
78         /** Construct indexed object with three indices. The indices must be of class idx.
79          *
80          *  @param b Base expression
81          *  @param i1 First index
82          *  @param i2 Second index
83          *  @param i3 Third index
84          *  @return newly constructed indexed object */
85         indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3);
86
87         /** Construct indexed object with four indices. The indices must be of class idx.
88          *
89          *  @param b Base expression
90          *  @param i1 First index
91          *  @param i2 Second index
92          *  @param i3 Third index
93          *  @param i4 Fourth index
94          *  @return newly constructed indexed object */
95         indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3, const ex & i4);
96
97         /** Construct indexed object with two indices and a specified symmetry. The
98          *  indices must be of class idx.
99          *
100          *  @param b Base expression
101          *  @param symm Symmetry of indices
102          *  @param i1 First index
103          *  @param i2 Second index
104          *  @return newly constructed indexed object */
105         indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2);
106
107         /** Construct indexed object with three indices and a specified symmetry.
108          *  The indices must be of class idx.
109          *
110          *  @param b Base expression
111          *  @param symm Symmetry of indices
112          *  @param i1 First index
113          *  @param i2 Second index
114          *  @param i3 Third index
115          *  @return newly constructed indexed object */
116         indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2, const ex & i3);
117
118         /** Construct indexed object with four indices and a specified symmetry. The
119          *  indices must be of class idx.
120          *
121          *  @param b Base expression
122          *  @param symm Symmetry of indices
123          *  @param i1 First index
124          *  @param i2 Second index
125          *  @param i3 Third index
126          *  @param i4 Fourth index
127          *  @return newly constructed indexed object */
128         indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2, const ex & i3, const ex & i4);
129
130         /** Construct indexed object with a specified vector of indices. The indices
131          *  must be of class idx.
132          *
133          *  @param b Base expression
134          *  @param iv Vector of indices
135          *  @return newly constructed indexed object */
136         indexed(const ex & b, const exvector & iv);
137
138         /** Construct indexed object with a specified vector of indices and
139          *  symmetry. The indices must be of class idx.
140          *
141          *  @param b Base expression
142          *  @param symm Symmetry of indices
143          *  @param iv Vector of indices
144          *  @return newly constructed indexed object */
145         indexed(const ex & b, symmetry_type symm, const exvector & iv);
146
147         // internal constructors
148         indexed(symmetry_type symm, const exprseq & es);
149         indexed(symmetry_type symm, const exvector & v, bool discardable = false);
150         indexed(symmetry_type symm, exvector * vp); // vp will be deleted
151
152         // functions overriding virtual functions from base classes
153 public:
154         void printraw(std::ostream & os) const;
155         void printtree(std::ostream & os, unsigned indent) const;
156         void print(std::ostream & os, unsigned upper_precedence=0) const;
157         bool info(unsigned inf) const;
158         ex eval(int level = 0) const;
159         exvector get_free_indices(void) const;
160
161 protected:
162         ex thisexprseq(const exvector & v) const;
163         ex thisexprseq(exvector * vp) const;
164         unsigned return_type(void) const { return return_types::commutative; }
165         ex expand(unsigned options = 0) const;
166
167         // new virtual functions which can be overridden by derived classes
168         // none
169         
170         // non-virtual functions in this class
171 public:
172         /** Check whether all index values have a certain property.
173          *  @see class info_flags */
174         bool all_index_values_are(unsigned inf) const;
175
176 protected:
177         void printrawindices(std::ostream & os) const;
178         void printtreeindices(std::ostream & os, unsigned indent) const;
179         void printindices(std::ostream & os) const;
180         bool all_indices_of_type_idx(void) const;
181
182         // member variables
183 protected:
184         symmetry_type symmetry; /**< Index symmetry */
185 };
186
187
188 typedef std::pair<ex, ex> spmapkey;
189
190 struct spmapkey_is_less {
191         bool operator() (const spmapkey &p, const spmapkey &q) const 
192         {
193                 int cmp = p.first.compare(q.first);
194                 return ((cmp<0) || (!(cmp>0) && p.second.compare(q.second)<0));
195         }
196 };
197
198 typedef std::map<spmapkey, ex, spmapkey_is_less> spmap;
199
200 /** Helper class for storing information about known scalar products which
201  *  are to be automatically replaced by simplify_indexed().
202  *
203  *  @see simplify_indexed */
204 class scalar_products {
205 public:
206         /** Register scalar product pair and its value. */
207         void add(const ex & v1, const ex & v2, const ex & sp);
208
209         /** Clear all registered scalar products. */
210         void clear(void);
211
212         bool is_defined(const ex & v1, const ex & v2) const;
213         ex evaluate(const ex & v1, const ex & v2) const;
214         void debugprint(void) const;
215
216 private:
217         static spmapkey make_key(const ex & v1, const ex & v2);
218
219         spmap spm; /*< Map from defined scalar product pairs to their values */
220 };
221
222
223 // utility functions
224 inline const indexed &ex_to_indexed(const ex &e)
225 {
226         return static_cast<const indexed &>(*e.bp);
227 }
228
229
230 /** Simplify/canonicalize expression containing indexed objects. This
231  *  performs contraction of dummy indices where possible and checks whether
232  *  the free indices in sums are consistent.
233  *
234  *  @param e The expression to be simplified
235  *  @return simplified expression */
236 ex simplify_indexed(const ex & e);
237
238 /** Simplify/canonicalize expression containing indexed objects. This
239  *  performs contraction of dummy indices where possible, checks whether
240  *  the free indices in sums are consistent, and automatically replaces
241  *  scalar products by known values if desired.
242  *
243  *  @param e The expression to be simplified
244  *  @param sp Scalar products to be replaced automatically
245  *  @return simplified expression */
246 ex simplify_indexed(const ex & e, const scalar_products & sp);
247
248
249 } // namespace GiNaC
250
251 #endif // ndef __GINAC_INDEXED_H__