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