]> www.ginac.de Git - ginac.git/blob - ginac/idx.h
- added structure template
[ginac.git] / ginac / idx.h
1 /** @file idx.h
2  *
3  *  Interface to GiNaC's indices. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2003 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 #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         void print(const print_context & c, unsigned level = 0) const;
52         bool info(unsigned inf) const;
53         size_t nops() const;
54         ex op(size_t i) const;
55         ex map(map_function & f) const;
56         ex evalf(int level = 0) const;
57         ex subs(const lst & ls, const lst & lr, unsigned options = 0) const;
58
59 protected:
60         ex derivative(const symbol & s) const;
61         bool match_same_type(const basic & other) 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         ex value; /**< Expression that constitutes the index (numeric or symbolic name) */
98         ex dim;   /**< Dimension of space (can be symbolic or numeric) */
99 };
100
101
102 /** This class holds an index with a variance (co- or contravariant). There
103  *  is an associated metric tensor that can be used to raise/lower indices. */
104 class varidx : public idx
105 {
106         GINAC_DECLARE_REGISTERED_CLASS(varidx, idx)
107
108         // other constructors
109 public:
110         /** Construct index with given value, dimension and variance.
111          *
112          *  @param v Value of index (numeric or symbolic)
113          *  @param dim Dimension of index space (numeric or symbolic)
114          *  @param covariant Make covariant index (default is contravariant)
115          *  @return newly constructed index */
116         varidx(const ex & v, const ex & dim, bool covariant = false);
117
118         // functions overriding virtual functions from base classes
119 public:
120         void print(const print_context & c, unsigned level = 0) const;
121         bool is_dummy_pair_same_type(const basic & other) const;
122
123 protected:
124         bool match_same_type(const basic & other) const;
125
126         // non-virtual functions in this class
127 public:
128         /** Check whether the index is covariant. */
129         bool is_covariant() const {return covariant;}
130
131         /** Check whether the index is contravariant (not covariant). */
132         bool is_contravariant() const {return !covariant;}
133
134         /** Make a new index with the same value but the opposite variance. */
135         ex toggle_variance() const;
136
137         // member variables
138 protected:
139         bool covariant; /**< x.mu, default is contravariant: x~mu */
140 };
141
142
143 /** This class holds a spinor index that can be dotted or undotted and that
144  *  also has a variance. This is used in the Weyl-van-der-Waerden formalism
145  *  where the dot indicates complex conjugation. There is an associated
146  *  (asymmetric) metric tensor that can be used to raise/lower spinor
147  *  indices. */
148 class spinidx : public varidx
149 {
150         GINAC_DECLARE_REGISTERED_CLASS(spinidx, varidx)
151
152         // other constructors
153 public:
154         /** Construct index with given value, dimension, variance and dot.
155          *
156          *  @param v Value of index (numeric or symbolic)
157          *  @param dim Dimension of index space (numeric or symbolic)
158          *  @param covariant Make covariant index (default is contravariant)
159          *  @param dotted Make covariant dotted (default is undotted)
160          *  @return newly constructed index */
161         spinidx(const ex & v, const ex & dim = 2, bool covariant = false, bool dotted = false);
162
163         // functions overriding virtual functions from base classes
164 public:
165         void print(const print_context & c, unsigned level = 0) const;
166         bool is_dummy_pair_same_type(const basic & other) const;
167
168 protected:
169         bool match_same_type(const basic & other) const;
170
171         // non-virtual functions in this class
172 public:
173         /** Check whether the index is dotted. */
174         bool is_dotted() const {return dotted;}
175
176         /** Check whether the index is not dotted. */
177         bool is_undotted() const {return !dotted;}
178
179         /** Make a new index with the same value and variance but the opposite
180          *  dottedness. */
181         ex toggle_dot() const;
182
183         /** Make a new index with the same value but opposite variance and
184          *  dottedness. */
185         ex toggle_variance_dot() const;
186
187         // member variables
188 protected:
189         bool dotted;
190 };
191
192
193 // utility functions
194
195 /** Specialization of is_exactly_a<idx>(obj) for idx objects. */
196 template<> inline bool is_exactly_a<idx>(const basic & obj)
197 {
198         return obj.tinfo()==TINFO_idx;
199 }
200
201 /** Specialization of is_exactly_a<varidx>(obj) for varidx objects. */
202 template<> inline bool is_exactly_a<varidx>(const basic & obj)
203 {
204         return obj.tinfo()==TINFO_varidx;
205 }
206
207 /** Specialization of is_exactly_a<spinidx>(obj) for spinidx objects. */
208 template<> inline bool is_exactly_a<spinidx>(const basic & obj)
209 {
210         return obj.tinfo()==TINFO_spinidx;
211 }
212
213 /** Check whether two indices form a dummy pair. */
214 bool is_dummy_pair(const idx & i1, const idx & i2);
215
216 /** Check whether two expressions form a dummy index pair. */
217 bool is_dummy_pair(const ex & e1, const ex & e2);
218
219 /** Given a vector of indices, split them into two vectors, one containing
220  *  the free indices, the other containing the dummy indices (numeric
221  *  indices are neither free nor dummy ones).
222  *
223  *  @param it Pointer to start of index vector
224  *  @param itend Pointer to end of index vector
225  *  @param out_free Vector of free indices (returned, sorted)
226  *  @param out_dummy Vector of dummy indices (returned, sorted) */
227 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy);
228
229 /** Given a vector of indices, split them into two vectors, one containing
230  *  the free indices, the other containing the dummy indices (numeric
231  *  indices are neither free nor dummy ones).
232  *
233  *  @param v Index vector
234  *  @param out_free Vector of free indices (returned, sorted)
235  *  @param out_dummy Vector of dummy indices (returned, sorted) */
236 inline void find_free_and_dummy(const exvector & v, exvector & out_free, exvector & out_dummy)
237 {
238         find_free_and_dummy(v.begin(), v.end(), out_free, out_dummy);
239 }
240
241 /** Given a vector of indices, find the dummy indices.
242  *
243  *  @param v Index vector
244  *  @param out_dummy Vector of dummy indices (returned, sorted) */
245 inline void find_dummy_indices(const exvector & v, exvector & out_dummy)
246 {
247         exvector free_indices;
248         find_free_and_dummy(v.begin(), v.end(), free_indices, out_dummy);
249 }
250
251 /** Count the number of dummy index pairs in an index vector. */
252 inline size_t count_dummy_indices(const exvector & v)
253 {
254         exvector free_indices, dummy_indices;
255         find_free_and_dummy(v.begin(), v.end(), free_indices, dummy_indices);
256         return dummy_indices.size();
257 }
258
259 /** Count the number of dummy index pairs in an index vector. */
260 inline size_t count_free_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 free_indices.size();
265 }
266
267 /** Return the minimum of two index dimensions. If this is undecidable,
268  *  throw an exception. Numeric dimensions are always considered "smaller"
269  *  than symbolic dimensions. */
270 ex minimal_dim(const ex & dim1, const ex & dim2);
271
272 } // namespace GiNaC
273
274 #endif // ndef __GINAC_IDX_H__