]> www.ginac.de Git - ginac.git/blob - ginac/idx.h
* Fix optimization opportunities missed by Alexei's patch.
[ginac.git] / ginac / idx.h
1 /** @file idx.h
2  *
3  *  Interface to GiNaC's indices. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #ifndef __GINAC_IDX_H__
24 #define __GINAC_IDX_H__
25
26 #include "ex.h"
27 #include "numeric.h"
28
29 namespace GiNaC {
30
31
32 /** This class holds one index of an indexed object. Indices can
33  *  theoretically consist of any symbolic expression but they are usually
34  *  only just a symbol (e.g. "mu", "i") or numeric (integer). Indices belong
35  *  to a space with a certain numeric or symbolic dimension. */
36 class idx : public basic
37 {
38         GINAC_DECLARE_REGISTERED_CLASS(idx, basic)
39
40         // other constructors
41 public:
42         /** Construct index with given value and dimension.
43          *
44          *  @param v Value of index (numeric or symbolic)
45          *  @param dim Dimension of index space (numeric or symbolic)
46          *  @return newly constructed index */
47         explicit idx(const ex & v, const ex & dim);
48
49         // functions overriding virtual functions from base classes
50 public:
51         bool info(unsigned inf) const;
52         size_t nops() const;
53         ex op(size_t i) const;
54         ex map(map_function & f) const;
55         ex evalf(int level = 0) const;
56         ex subs(const exmap & m, unsigned options = 0) const;
57
58 protected:
59         ex derivative(const symbol & s) const;
60         bool match_same_type(const basic & other) const;
61         unsigned calchash() const;
62
63         // new virtual functions in this class
64 public:
65         /** Check whether the index forms a dummy index pair with another index
66          *  of the same type. */
67         virtual bool is_dummy_pair_same_type(const basic & other) const;
68
69         // non-virtual functions in this class
70 public:
71         /** Get value of index. */
72         ex get_value() const {return value;}
73
74         /** Check whether the index is numeric. */
75         bool is_numeric() const {return is_exactly_a<numeric>(value);}
76
77         /** Check whether the index is symbolic. */
78         bool is_symbolic() const {return !is_exactly_a<numeric>(value);}
79
80         /** Get dimension of index space. */
81         ex get_dim() const {return dim;}
82
83         /** Check whether the dimension is numeric. */
84         bool is_dim_numeric() const {return is_exactly_a<numeric>(dim);}
85
86         /** Check whether the dimension is symbolic. */
87         bool is_dim_symbolic() const {return !is_exactly_a<numeric>(dim);}
88
89         /** Make a new index with the same value but a different dimension. */
90         ex replace_dim(const ex & new_dim) const;
91
92         /** Return the minimum of the dimensions of this and another index.
93          *  If this is undecidable, throw an exception. */
94         ex minimal_dim(const idx & other) const;
95
96 protected:
97         void print_index(const print_context & c, unsigned level) const;
98         void do_print(const print_context & c, unsigned level) const;
99         void do_print_csrc(const print_csrc & c, unsigned level) const;
100         void do_print_latex(const print_latex & c, unsigned level) const;
101         void do_print_tree(const print_tree & c, unsigned level) const;
102
103 protected:
104         ex value; /**< Expression that constitutes the index (numeric or symbolic name) */
105         ex dim;   /**< Dimension of space (can be symbolic or numeric) */
106 };
107
108
109 /** This class holds an index with a variance (co- or contravariant). There
110  *  is an associated metric tensor that can be used to raise/lower indices. */
111 class varidx : public idx
112 {
113         GINAC_DECLARE_REGISTERED_CLASS(varidx, idx)
114
115         // other constructors
116 public:
117         /** Construct index with given value, dimension and variance.
118          *
119          *  @param v Value of index (numeric or symbolic)
120          *  @param dim Dimension of index space (numeric or symbolic)
121          *  @param covariant Make covariant index (default is contravariant)
122          *  @return newly constructed index */
123         varidx(const ex & v, const ex & dim, bool covariant = false);
124
125         // functions overriding virtual functions from base classes
126 public:
127         bool is_dummy_pair_same_type(const basic & other) const;
128
129 protected:
130         bool match_same_type(const basic & other) const;
131
132         // non-virtual functions in this class
133 public:
134         /** Check whether the index is covariant. */
135         bool is_covariant() const {return covariant;}
136
137         /** Check whether the index is contravariant (not covariant). */
138         bool is_contravariant() const {return !covariant;}
139
140         /** Make a new index with the same value but the opposite variance. */
141         ex toggle_variance() const;
142
143 protected:
144         void do_print(const print_context & c, unsigned level) const;
145         void do_print_tree(const print_tree & c, unsigned level) const;
146
147         // member variables
148 protected:
149         bool covariant; /**< x.mu, default is contravariant: x~mu */
150 };
151
152
153 /** This class holds a spinor index that can be dotted or undotted and that
154  *  also has a variance. This is used in the Weyl-van-der-Waerden formalism
155  *  where the dot indicates complex conjugation. There is an associated
156  *  (asymmetric) metric tensor that can be used to raise/lower spinor
157  *  indices. */
158 class spinidx : public varidx
159 {
160         GINAC_DECLARE_REGISTERED_CLASS(spinidx, varidx)
161
162         // other constructors
163 public:
164         /** Construct index with given value, dimension, variance and dot.
165          *
166          *  @param v Value of index (numeric or symbolic)
167          *  @param dim Dimension of index space (numeric or symbolic)
168          *  @param covariant Make covariant index (default is contravariant)
169          *  @param dotted Make covariant dotted (default is undotted)
170          *  @return newly constructed index */
171         spinidx(const ex & v, const ex & dim = 2, bool covariant = false, bool dotted = false);
172
173         // functions overriding virtual functions from base classes
174 public:
175         bool is_dummy_pair_same_type(const basic & other) const;
176         // complex conjugation
177         ex conjugate() const { return toggle_dot(); }
178
179 protected:
180         bool match_same_type(const basic & other) const;
181
182         // non-virtual functions in this class
183 public:
184         /** Check whether the index is dotted. */
185         bool is_dotted() const {return dotted;}
186
187         /** Check whether the index is not dotted. */
188         bool is_undotted() const {return !dotted;}
189
190         /** Make a new index with the same value and variance but the opposite
191          *  dottedness. */
192         ex toggle_dot() const;
193
194         /** Make a new index with the same value but opposite variance and
195          *  dottedness. */
196         ex toggle_variance_dot() const;
197
198 protected:
199         void do_print(const print_context & c, unsigned level) const;
200         void do_print_latex(const print_latex & c, unsigned level) const;
201         void do_print_tree(const print_tree & c, unsigned level) const;
202
203         // member variables
204 protected:
205         bool dotted;
206 };
207
208
209 // utility functions
210
211 /** Check whether two indices form a dummy pair. */
212 bool is_dummy_pair(const idx & i1, const idx & i2);
213
214 /** Check whether two expressions form a dummy index pair. */
215 bool is_dummy_pair(const ex & e1, const ex & e2);
216
217 /** Given a vector of indices, split them into two vectors, one containing
218  *  the free indices, the other containing the dummy indices (numeric
219  *  indices are neither free nor dummy ones).
220  *
221  *  @param it Pointer to start of index vector
222  *  @param itend Pointer to end of index vector
223  *  @param out_free Vector of free indices (returned, sorted)
224  *  @param out_dummy Vector of dummy indices (returned, sorted) */
225 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy);
226
227 /** Given a vector of indices, split them into two vectors, one containing
228  *  the free indices, the other containing the dummy indices (numeric
229  *  indices are neither free nor dummy ones).
230  *
231  *  @param v Index vector
232  *  @param out_free Vector of free indices (returned, sorted)
233  *  @param out_dummy Vector of dummy indices (returned, sorted) */
234 inline void find_free_and_dummy(const exvector & v, exvector & out_free, exvector & out_dummy)
235 {
236         find_free_and_dummy(v.begin(), v.end(), out_free, out_dummy);
237 }
238
239 /** Given a vector of indices, find the dummy indices.
240  *
241  *  @param v Index vector
242  *  @param out_dummy Vector of dummy indices (returned, sorted) */
243 inline void find_dummy_indices(const exvector & v, exvector & out_dummy)
244 {
245         exvector free_indices;
246         find_free_and_dummy(v.begin(), v.end(), free_indices, out_dummy);
247 }
248
249 /** Count the number of dummy index pairs in an index vector. */
250 inline size_t count_dummy_indices(const exvector & v)
251 {
252         exvector free_indices, dummy_indices;
253         find_free_and_dummy(v.begin(), v.end(), free_indices, dummy_indices);
254         return dummy_indices.size();
255 }
256
257 /** Count the number of dummy index pairs in an index vector. */
258 inline size_t count_free_indices(const exvector & v)
259 {
260         exvector free_indices, dummy_indices;
261         find_free_and_dummy(v.begin(), v.end(), free_indices, dummy_indices);
262         return free_indices.size();
263 }
264
265 /** Return the minimum of two index dimensions. If this is undecidable,
266  *  throw an exception. Numeric dimensions are always considered "smaller"
267  *  than symbolic dimensions. */
268 ex minimal_dim(const ex & dim1, const ex & dim2);
269
270 } // namespace GiNaC
271
272 #endif // ndef __GINAC_IDX_H__