3 * Interface to GiNaC's indices. */
6 * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
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.
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.
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
23 #ifndef __GINAC_IDX_H__
24 #define __GINAC_IDX_H__
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
38 GINAC_DECLARE_REGISTERED_CLASS(idx, basic)
42 /** Construct index with given value and dimension.
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);
49 // functions overriding virtual functions from base classes
51 void print(const print_context & c, unsigned level = 0) const;
52 bool info(unsigned inf) 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 exmap & m, unsigned options = 0) const;
60 ex derivative(const symbol & s) const;
61 bool match_same_type(const basic & other) const;
62 unsigned calchash() const;
64 // new virtual functions in this class
66 /** Check whether the index forms a dummy index pair with another index
67 * of the same type. */
68 virtual bool is_dummy_pair_same_type(const basic & other) const;
70 // non-virtual functions in this class
72 /** Get value of index. */
73 ex get_value() const {return value;}
75 /** Check whether the index is numeric. */
76 bool is_numeric() const {return is_exactly_a<numeric>(value);}
78 /** Check whether the index is symbolic. */
79 bool is_symbolic() const {return !is_exactly_a<numeric>(value);}
81 /** Get dimension of index space. */
82 ex get_dim() const {return dim;}
84 /** Check whether the dimension is numeric. */
85 bool is_dim_numeric() const {return is_exactly_a<numeric>(dim);}
87 /** Check whether the dimension is symbolic. */
88 bool is_dim_symbolic() const {return !is_exactly_a<numeric>(dim);}
90 /** Make a new index with the same value but a different dimension. */
91 ex replace_dim(const ex & new_dim) const;
93 /** Return the minimum of the dimensions of this and another index.
94 * If this is undecidable, throw an exception. */
95 ex minimal_dim(const idx & other) const;
98 ex value; /**< Expression that constitutes the index (numeric or symbolic name) */
99 ex dim; /**< Dimension of space (can be symbolic or numeric) */
103 /** This class holds an index with a variance (co- or contravariant). There
104 * is an associated metric tensor that can be used to raise/lower indices. */
105 class varidx : public idx
107 GINAC_DECLARE_REGISTERED_CLASS(varidx, idx)
109 // other constructors
111 /** Construct index with given value, dimension and variance.
113 * @param v Value of index (numeric or symbolic)
114 * @param dim Dimension of index space (numeric or symbolic)
115 * @param covariant Make covariant index (default is contravariant)
116 * @return newly constructed index */
117 varidx(const ex & v, const ex & dim, bool covariant = false);
119 // functions overriding virtual functions from base classes
121 void print(const print_context & c, unsigned level = 0) const;
122 bool is_dummy_pair_same_type(const basic & other) const;
125 bool match_same_type(const basic & other) const;
127 // non-virtual functions in this class
129 /** Check whether the index is covariant. */
130 bool is_covariant() const {return covariant;}
132 /** Check whether the index is contravariant (not covariant). */
133 bool is_contravariant() const {return !covariant;}
135 /** Make a new index with the same value but the opposite variance. */
136 ex toggle_variance() const;
140 bool covariant; /**< x.mu, default is contravariant: x~mu */
144 /** This class holds a spinor index that can be dotted or undotted and that
145 * also has a variance. This is used in the Weyl-van-der-Waerden formalism
146 * where the dot indicates complex conjugation. There is an associated
147 * (asymmetric) metric tensor that can be used to raise/lower spinor
149 class spinidx : public varidx
151 GINAC_DECLARE_REGISTERED_CLASS(spinidx, varidx)
153 // other constructors
155 /** Construct index with given value, dimension, variance and dot.
157 * @param v Value of index (numeric or symbolic)
158 * @param dim Dimension of index space (numeric or symbolic)
159 * @param covariant Make covariant index (default is contravariant)
160 * @param dotted Make covariant dotted (default is undotted)
161 * @return newly constructed index */
162 spinidx(const ex & v, const ex & dim = 2, bool covariant = false, bool dotted = false);
164 // functions overriding virtual functions from base classes
166 void print(const print_context & c, unsigned level = 0) const;
167 bool is_dummy_pair_same_type(const basic & other) const;
170 bool match_same_type(const basic & other) const;
172 // non-virtual functions in this class
174 /** Check whether the index is dotted. */
175 bool is_dotted() const {return dotted;}
177 /** Check whether the index is not dotted. */
178 bool is_undotted() const {return !dotted;}
180 /** Make a new index with the same value and variance but the opposite
182 ex toggle_dot() const;
184 /** Make a new index with the same value but opposite variance and
186 ex toggle_variance_dot() const;
196 /** Specialization of is_exactly_a<idx>(obj) for idx objects. */
197 template<> inline bool is_exactly_a<idx>(const basic & obj)
199 return obj.tinfo()==TINFO_idx;
202 /** Specialization of is_exactly_a<varidx>(obj) for varidx objects. */
203 template<> inline bool is_exactly_a<varidx>(const basic & obj)
205 return obj.tinfo()==TINFO_varidx;
208 /** Specialization of is_exactly_a<spinidx>(obj) for spinidx objects. */
209 template<> inline bool is_exactly_a<spinidx>(const basic & obj)
211 return obj.tinfo()==TINFO_spinidx;
214 /** Check whether two indices form a dummy pair. */
215 bool is_dummy_pair(const idx & i1, const idx & i2);
217 /** Check whether two expressions form a dummy index pair. */
218 bool is_dummy_pair(const ex & e1, const ex & e2);
220 /** Given a vector of indices, split them into two vectors, one containing
221 * the free indices, the other containing the dummy indices (numeric
222 * indices are neither free nor dummy ones).
224 * @param it Pointer to start of index vector
225 * @param itend Pointer to end of index vector
226 * @param out_free Vector of free indices (returned, sorted)
227 * @param out_dummy Vector of dummy indices (returned, sorted) */
228 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy);
230 /** Given a vector of indices, split them into two vectors, one containing
231 * the free indices, the other containing the dummy indices (numeric
232 * indices are neither free nor dummy ones).
234 * @param v Index vector
235 * @param out_free Vector of free indices (returned, sorted)
236 * @param out_dummy Vector of dummy indices (returned, sorted) */
237 inline void find_free_and_dummy(const exvector & v, exvector & out_free, exvector & out_dummy)
239 find_free_and_dummy(v.begin(), v.end(), out_free, out_dummy);
242 /** Given a vector of indices, find the dummy indices.
244 * @param v Index vector
245 * @param out_dummy Vector of dummy indices (returned, sorted) */
246 inline void find_dummy_indices(const exvector & v, exvector & out_dummy)
248 exvector free_indices;
249 find_free_and_dummy(v.begin(), v.end(), free_indices, out_dummy);
252 /** Count the number of dummy index pairs in an index vector. */
253 inline size_t count_dummy_indices(const exvector & v)
255 exvector free_indices, dummy_indices;
256 find_free_and_dummy(v.begin(), v.end(), free_indices, dummy_indices);
257 return dummy_indices.size();
260 /** Count the number of dummy index pairs in an index vector. */
261 inline size_t count_free_indices(const exvector & v)
263 exvector free_indices, dummy_indices;
264 find_free_and_dummy(v.begin(), v.end(), free_indices, dummy_indices);
265 return free_indices.size();
268 /** Return the minimum of two index dimensions. If this is undecidable,
269 * throw an exception. Numeric dimensions are always considered "smaller"
270 * than symbolic dimensions. */
271 ex minimal_dim(const ex & dim1, const ex & dim2);
275 #endif // ndef __GINAC_IDX_H__