3 * Interface to GiNaC's indices. */
6 * GiNaC Copyright (C) 1999-2001 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__
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
37 GINAC_DECLARE_REGISTERED_CLASS(idx, basic)
41 /** Construct index with given value and dimension.
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);
48 // functions overriding virtual functions from base classes
50 void print(const print_context & c, unsigned level = 0) const;
51 bool info(unsigned inf) const;
52 unsigned nops() const;
54 ex evalf(int level = 0) const;
55 ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const;
58 ex derivative(const symbol & s) const;
59 bool match_same_type(const basic & other) const;
61 // new virtual functions in this class
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;
67 // non-virtual functions in this class
69 /** Get value of index. */
70 ex get_value(void) const {return value;}
72 /** Check whether the index is numeric. */
73 bool is_numeric(void) const {return is_ex_exactly_of_type(value, numeric);}
75 /** Check whether the index is symbolic. */
76 bool is_symbolic(void) const {return !is_ex_exactly_of_type(value, numeric);}
78 /** Get dimension of index space. */
79 ex get_dim(void) const {return dim;}
81 /** Check whether the dimension is numeric. */
82 bool is_dim_numeric(void) const {return is_ex_exactly_of_type(dim, numeric);}
84 /** Check whether the dimension is symbolic. */
85 bool is_dim_symbolic(void) const {return !is_ex_exactly_of_type(dim, numeric);}
88 ex value; /**< Expression that constitutes the index (numeric or symbolic name) */
89 ex dim; /**< Dimension of space (can be symbolic or numeric) */
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
97 GINAC_DECLARE_REGISTERED_CLASS(varidx, idx)
101 /** Construct index with given value, dimension and variance.
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);
109 // functions overriding virtual functions from base classes
111 void print(const print_context & c, unsigned level = 0) const;
112 bool is_dummy_pair_same_type(const basic & other) const;
115 bool match_same_type(const basic & other) const;
117 // non-virtual functions in this class
119 /** Check whether the index is covariant. */
120 bool is_covariant(void) const {return covariant;}
122 /** Check whether the index is contravariant (not covariant). */
123 bool is_contravariant(void) const {return !covariant;}
125 /** Make a new index with the same value but the opposite variance. */
126 ex toggle_variance(void) const;
130 bool covariant; /**< x.mu, default is contravariant: x~mu */
134 /** This class holds a spinor index that can be dotted or undotted and that
135 * also has a variance. This is used in the Weyl-van-der-Waerden formalism
136 * where the dot indicates complex conjugation. There is an associated
137 * (asymmetric) metric tensor that can be used to raise/lower spinor
139 class spinidx : public varidx
141 GINAC_DECLARE_REGISTERED_CLASS(spinidx, varidx)
143 // other constructors
145 /** Construct index with given value, dimension, variance and dot.
147 * @param v Value of index (numeric or symbolic)
148 * @param dim Dimension of index space (numeric or symbolic)
149 * @param covariant Make covariant index (default is contravariant)
150 * @param dotted Make covariant dotted (default is undotted)
151 * @return newly constructed index */
152 spinidx(const ex & v, const ex & dim = 2, bool covariant = false, bool dotted = false);
154 // functions overriding virtual functions from base classes
156 void print(const print_context & c, unsigned level = 0) const;
157 bool is_dummy_pair_same_type(const basic & other) const;
160 bool match_same_type(const basic & other) const;
162 // non-virtual functions in this class
164 /** Check whether the index is dotted. */
165 bool is_dotted(void) const {return dotted;}
167 /** Check whether the index is not dotted. */
168 bool is_undotted(void) const {return !dotted;}
170 /** Make a new index with the same value and variance but the opposite
172 ex toggle_dot(void) const;
174 /** Make a new index with the same value but opposite variance and
176 ex toggle_variance_dot(void) const;
186 /** Return the idx object handled by an ex. Deprecated: use ex_to<idx>().
187 * This is unsafe: you need to check the type first. */
188 inline const idx &ex_to_idx(const ex & e)
190 return static_cast<const idx &>(*e.bp);
193 /** Return the varidx object handled by an ex. Deprecated: use ex_to<varidx>().
194 * This is unsafe: you need to check the type first. */
195 inline const varidx &ex_to_varidx(const ex & e)
197 return static_cast<const varidx &>(*e.bp);
200 /** Return the spinidx object handled by an ex. Deprecated: use ex_to<spinidx>().
201 * This is unsafe: you need to check the type first. */
202 inline const spinidx &ex_to_spinidx(const ex & e)
204 return static_cast<const spinidx &>(*e.bp);
207 /** Specialization of is_exactly_a<idx>(obj) for idx objects. */
208 template<> inline bool is_exactly_a<idx>(const basic & obj)
210 return obj.tinfo()==TINFO_idx;
213 /** Specialization of is_exactly_a<varidx>(obj) for varidx objects. */
214 template<> inline bool is_exactly_a<varidx>(const basic & obj)
216 return obj.tinfo()==TINFO_varidx;
219 /** Specialization of is_exactly_a<spinidx>(obj) for spinidx objects. */
220 template<> inline bool is_exactly_a<spinidx>(const basic & obj)
222 return obj.tinfo()==TINFO_spinidx;
225 /** Check whether two indices form a dummy pair. */
226 bool is_dummy_pair(const idx & i1, const idx & i2);
228 /** Check whether two expressions form a dummy index pair. */
229 bool is_dummy_pair(const ex & e1, const ex & e2);
231 /** Given a vector of indices, split them into two vectors, one containing
232 * the free indices, the other containing the dummy indices (numeric
233 * indices are neither free nor dummy ones).
235 * @param it Pointer to start of index vector
236 * @param itend Pointer to end of index vector
237 * @param out_free Vector of free indices (returned, sorted)
238 * @param out_dummy Vector of dummy indices (returned, sorted) */
239 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy);
241 /** Given a vector of indices, split them into two vectors, one containing
242 * the free indices, the other containing the dummy indices (numeric
243 * indices are neither free nor dummy ones).
245 * @param v Index vector
246 * @param out_free Vector of free indices (returned, sorted)
247 * @param out_dummy Vector of dummy indices (returned, sorted) */
248 inline void find_free_and_dummy(const exvector & v, exvector & out_free, exvector & out_dummy)
250 find_free_and_dummy(v.begin(), v.end(), out_free, out_dummy);
253 /** Given a vector of indices, find the dummy indices.
255 * @param v Index vector
256 * @param out_dummy Vector of dummy indices (returned, sorted) */
257 inline void find_dummy_indices(const exvector & v, exvector & out_dummy)
259 exvector free_indices;
260 find_free_and_dummy(v.begin(), v.end(), free_indices, out_dummy);
263 /** Count the number of dummy index pairs in an index vector. */
264 inline unsigned count_dummy_indices(const exvector & v)
266 exvector free_indices, dummy_indices;
267 find_free_and_dummy(v.begin(), v.end(), free_indices, dummy_indices);
268 return dummy_indices.size();
271 /** Count the number of dummy index pairs in an index vector. */
272 inline unsigned count_free_indices(const exvector & v)
274 exvector free_indices, dummy_indices;
275 find_free_and_dummy(v.begin(), v.end(), free_indices, dummy_indices);
276 return free_indices.size();
281 #endif // ndef __GINAC_IDX_H__