]> www.ginac.de Git - ginac.git/blob - ginac/indexed.h
* Supplement some (now deprecated) macros by inlined template functions:
[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 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
46         // other constructors
47 public:
48         /** Construct indexed object with no index.
49          *
50          *  @param b Base expression
51          *  @return newly constructed indexed object */
52         indexed(const ex & b);
53
54         /** Construct indexed object with one index. The index must be of class idx.
55          *
56          *  @param b Base expression
57          *  @param i1 The index
58          *  @return newly constructed indexed object */
59         indexed(const ex & b, const ex & i1);
60
61         /** Construct indexed object with two indices. The indices must be of class idx.
62          *
63          *  @param b Base expression
64          *  @param i1 First index
65          *  @param i2 Second index
66          *  @return newly constructed indexed object */
67         indexed(const ex & b, const ex & i1, const ex & i2);
68
69         /** Construct indexed object with three indices. The indices must be of class idx.
70          *
71          *  @param b Base expression
72          *  @param i1 First index
73          *  @param i2 Second index
74          *  @param i3 Third index
75          *  @return newly constructed indexed object */
76         indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3);
77
78         /** Construct indexed object with four 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          *  @param i4 Fourth index
85          *  @return newly constructed indexed object */
86         indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3, const ex & i4);
87
88         /** Construct indexed object with two indices and a specified symmetry. The
89          *  indices must be of class idx.
90          *
91          *  @param b Base expression
92          *  @param symm Symmetry of indices
93          *  @param i1 First index
94          *  @param i2 Second index
95          *  @return newly constructed indexed object */
96         indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2);
97
98         /** Construct indexed object with three indices and a specified symmetry.
99          *  The indices must be of class idx.
100          *
101          *  @param b Base expression
102          *  @param symm Symmetry of indices
103          *  @param i1 First index
104          *  @param i2 Second index
105          *  @param i3 Third index
106          *  @return newly constructed indexed object */
107         indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3);
108
109         /** Construct indexed object with four indices and a specified symmetry. The
110          *  indices must be of class idx.
111          *
112          *  @param b Base expression
113          *  @param symm Symmetry of indices
114          *  @param i1 First index
115          *  @param i2 Second index
116          *  @param i3 Third index
117          *  @param i4 Fourth index
118          *  @return newly constructed indexed object */
119         indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3, const ex & i4);
120
121         /** Construct indexed object with a specified vector of indices. The indices
122          *  must be of class idx.
123          *
124          *  @param b Base expression
125          *  @param iv Vector of indices
126          *  @return newly constructed indexed object */
127         indexed(const ex & b, const exvector & iv);
128
129         /** Construct indexed object with a specified vector of indices and
130          *  symmetry. The indices must be of class idx.
131          *
132          *  @param b Base expression
133          *  @param symm Symmetry of indices
134          *  @param iv Vector of indices
135          *  @return newly constructed indexed object */
136         indexed(const ex & b, const symmetry & symm, const exvector & iv);
137
138         // internal constructors
139         indexed(const symmetry & symm, const exprseq & es);
140         indexed(const symmetry & symm, const exvector & v, bool discardable = false);
141         indexed(const symmetry & symm, exvector * vp); // vp will be deleted
142
143         // functions overriding virtual functions from base classes
144 public:
145         void print(const print_context & c, unsigned level = 0) const;
146         bool info(unsigned inf) const;
147         ex eval(int level = 0) const;
148         int degree(const ex & s) const;
149         int ldegree(const ex & s) const;
150         ex coeff(const ex & s, int n = 1) const;
151         exvector get_free_indices(void) const;
152
153 protected:
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 /** Return the indexed object handled by an ex.
238  *  This is unsafe: you need to check the type first. */
239 inline const indexed &ex_to_indexed(const ex &e)
240 {
241         return static_cast<const indexed &>(*e.bp);
242 }
243
244 /** Specialization of is_exactly_a<indexed>(obj) for indexed objects. */
245 template<> inline bool is_exactly_a<indexed>(const basic & obj)
246 {
247         return obj.tinfo()==TINFO_indexed;
248 }
249
250 } // namespace GiNaC
251
252 #endif // ndef __GINAC_INDEXED_H__