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