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