]> www.ginac.de Git - ginac.git/blob - ginac/indexed.h
- dummy index recognition in products was flawed: A.i.i*B.j.j would be
[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         /** Return a vector containing the object's indices. */
177         exvector get_indices(void) const;
178
179         /** Return a vector containing the dummy indices of the object, if any. */
180         exvector get_dummy_indices(void) const;
181
182         /** Return a vector containing the dummy indices in the contraction with
183          *  another indexed object. */
184         exvector get_dummy_indices(const indexed & other) const;
185
186 protected:
187         void printrawindices(std::ostream & os) const;
188         void printtreeindices(std::ostream & os, unsigned indent) const;
189         void printindices(std::ostream & os) const;
190         void assert_all_indices_of_type_idx(void) const;
191
192         // member variables
193 protected:
194         symmetry_type symmetry; /**< Index symmetry */
195 };
196
197
198 typedef std::pair<ex, ex> spmapkey;
199
200 struct spmapkey_is_less {
201         bool operator() (const spmapkey &p, const spmapkey &q) const 
202         {
203                 int cmp = p.first.compare(q.first);
204                 return ((cmp<0) || (!(cmp>0) && p.second.compare(q.second)<0));
205         }
206 };
207
208 typedef std::map<spmapkey, ex, spmapkey_is_less> spmap;
209
210 /** Helper class for storing information about known scalar products which
211  *  are to be automatically replaced by simplify_indexed().
212  *
213  *  @see simplify_indexed */
214 class scalar_products {
215 public:
216         /** Register scalar product pair and its value. */
217         void add(const ex & v1, const ex & v2, const ex & sp);
218
219         /** Clear all registered scalar products. */
220         void clear(void);
221
222         bool is_defined(const ex & v1, const ex & v2) const;
223         ex evaluate(const ex & v1, const ex & v2) const;
224         void debugprint(void) const;
225
226 private:
227         static spmapkey make_key(const ex & v1, const ex & v2);
228
229         spmap spm; /*< Map from defined scalar product pairs to their values */
230 };
231
232
233 // utility functions
234 inline const indexed &ex_to_indexed(const ex &e)
235 {
236         return static_cast<const indexed &>(*e.bp);
237 }
238
239
240 /** Simplify/canonicalize expression containing indexed objects. This
241  *  performs contraction of dummy indices where possible and checks whether
242  *  the free indices in sums are consistent.
243  *
244  *  @param e The expression to be simplified
245  *  @return simplified expression */
246 ex simplify_indexed(const ex & e);
247
248 /** Simplify/canonicalize expression containing indexed objects. This
249  *  performs contraction of dummy indices where possible, checks whether
250  *  the free indices in sums are consistent, and automatically replaces
251  *  scalar products by known values if desired.
252  *
253  *  @param e The expression to be simplified
254  *  @param sp Scalar products to be replaced automatically
255  *  @return simplified expression */
256 ex simplify_indexed(const ex & e, const scalar_products & sp);
257
258
259 } // namespace GiNaC
260
261 #endif // ndef __GINAC_INDEXED_H__