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