]> www.ginac.de Git - ginac.git/blob - ginac/indexed.h
- document: collect's argument must be expanded.
[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, exvector & dummy_indices, const scalar_products & sp);
43         friend ex simplify_indexed_product(const ex & e, exvector & free_indices, exvector & dummy_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 print(const print_context & c, unsigned level = 0) const;
155         bool info(unsigned inf) const;
156         ex eval(int level = 0) const;
157         int degree(const ex & s) const;
158         int ldegree(const ex & s) const;
159         ex coeff(const ex & s, int n = 1) const;
160         exvector get_free_indices(void) const;
161
162 protected:
163         ex thisexprseq(const exvector & v) const;
164         ex thisexprseq(exvector * vp) const;
165         unsigned return_type(void) const { return return_types::commutative; }
166         ex expand(unsigned options = 0) const;
167
168         // new virtual functions which can be overridden by derived classes
169         // none
170         
171         // non-virtual functions in this class
172 public:
173         /** Check whether all index values have a certain property.
174          *  @see class info_flags */
175         bool all_index_values_are(unsigned inf) const;
176
177         /** Return a vector containing the object's indices. */
178         exvector get_indices(void) const;
179
180         /** Return a vector containing the dummy indices of the object, if any. */
181         exvector get_dummy_indices(void) const;
182
183         /** Return a vector containing the dummy indices in the contraction with
184          *  another indexed object. */
185         exvector get_dummy_indices(const indexed & other) const;
186
187         /** Check whether the object has an index that forms a dummy index pair
188          *  with a given index. */
189         bool has_dummy_index_for(const ex & i) const;
190
191 protected:
192         void printindices(const print_context & c, unsigned level) const;
193         void assert_all_indices_of_type_idx(void) const;
194
195         // member variables
196 protected:
197         symmetry_type symmetry; /**< Index symmetry */
198 };
199
200
201 typedef std::pair<ex, ex> spmapkey;
202
203 struct spmapkey_is_less {
204         bool operator() (const spmapkey &p, const spmapkey &q) const 
205         {
206                 int cmp = p.first.compare(q.first);
207                 return ((cmp<0) || (!(cmp>0) && p.second.compare(q.second)<0));
208         }
209 };
210
211 typedef std::map<spmapkey, ex, spmapkey_is_less> spmap;
212
213 /** Helper class for storing information about known scalar products which
214  *  are to be automatically replaced by simplify_indexed().
215  *
216  *  @see simplify_indexed */
217 class scalar_products {
218 public:
219         /** Register scalar product pair and its value. */
220         void add(const ex & v1, const ex & v2, const ex & sp);
221
222         /** Register list of vectors. This adds all possible pairs of products
223          *  a.i * b.i with the value a*b (note that this is not a scalar vector
224          *  product but an ordinary product of scalars). */
225         void add_vectors(const lst & l);
226
227         /** Clear all registered scalar products. */
228         void clear(void);
229
230         bool is_defined(const ex & v1, const ex & v2) const;
231         ex evaluate(const ex & v1, const ex & v2) const;
232         void debugprint(void) const;
233
234 private:
235         static spmapkey make_key(const ex & v1, const ex & v2);
236
237         spmap spm; /*< Map from defined scalar product pairs to their values */
238 };
239
240
241 // utility functions
242 inline const indexed &ex_to_indexed(const ex &e)
243 {
244         return static_cast<const indexed &>(*e.bp);
245 }
246
247 } // namespace GiNaC
248
249 #endif // ndef __GINAC_INDEXED_H__