3 * Interface to GiNaC's indices. */
6 * GiNaC Copyright (C) 1999-2005 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 bool info(unsigned inf) const;
53 ex op(size_t i) const;
54 ex map(map_function & f) const;
55 ex evalf(int level = 0) const;
56 ex subs(const exmap & m, unsigned options = 0) const;
59 ex derivative(const symbol & s) const;
60 bool match_same_type(const basic & other) const;
61 unsigned calchash() const;
63 // new virtual functions in this class
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;
69 // non-virtual functions in this class
71 /** Get value of index. */
72 ex get_value() const {return value;}
74 /** Check whether the index is numeric. */
75 bool is_numeric() const {return is_exactly_a<numeric>(value);}
77 /** Check whether the index is symbolic. */
78 bool is_symbolic() const {return !is_exactly_a<numeric>(value);}
80 /** Get dimension of index space. */
81 ex get_dim() const {return dim;}
83 /** Check whether the dimension is numeric. */
84 bool is_dim_numeric() const {return is_exactly_a<numeric>(dim);}
86 /** Check whether the dimension is symbolic. */
87 bool is_dim_symbolic() const {return !is_exactly_a<numeric>(dim);}
89 /** Make a new index with the same value but a different dimension. */
90 ex replace_dim(const ex & new_dim) const;
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;
97 void print_index(const print_context & c, unsigned level) const;
98 void do_print(const print_context & c, unsigned level) const;
99 void do_print_latex(const print_latex & c, unsigned level) const;
100 void do_print_tree(const print_tree & c, unsigned level) const;
103 ex value; /**< Expression that constitutes the index (numeric or symbolic name) */
104 ex dim; /**< Dimension of space (can be symbolic or numeric) */
108 /** This class holds an index with a variance (co- or contravariant). There
109 * is an associated metric tensor that can be used to raise/lower indices. */
110 class varidx : public idx
112 GINAC_DECLARE_REGISTERED_CLASS(varidx, idx)
114 // other constructors
116 /** Construct index with given value, dimension and variance.
118 * @param v Value of index (numeric or symbolic)
119 * @param dim Dimension of index space (numeric or symbolic)
120 * @param covariant Make covariant index (default is contravariant)
121 * @return newly constructed index */
122 varidx(const ex & v, const ex & dim, bool covariant = false);
124 // functions overriding virtual functions from base classes
126 bool is_dummy_pair_same_type(const basic & other) const;
129 bool match_same_type(const basic & other) const;
131 // non-virtual functions in this class
133 /** Check whether the index is covariant. */
134 bool is_covariant() const {return covariant;}
136 /** Check whether the index is contravariant (not covariant). */
137 bool is_contravariant() const {return !covariant;}
139 /** Make a new index with the same value but the opposite variance. */
140 ex toggle_variance() const;
143 void do_print(const print_context & c, unsigned level) const;
144 void do_print_tree(const print_tree & c, unsigned level) const;
148 bool covariant; /**< x.mu, default is contravariant: x~mu */
152 /** This class holds a spinor index that can be dotted or undotted and that
153 * also has a variance. This is used in the Weyl-van-der-Waerden formalism
154 * where the dot indicates complex conjugation. There is an associated
155 * (asymmetric) metric tensor that can be used to raise/lower spinor
157 class spinidx : public varidx
159 GINAC_DECLARE_REGISTERED_CLASS(spinidx, varidx)
161 // other constructors
163 /** Construct index with given value, dimension, variance and dot.
165 * @param v Value of index (numeric or symbolic)
166 * @param dim Dimension of index space (numeric or symbolic)
167 * @param covariant Make covariant index (default is contravariant)
168 * @param dotted Make covariant dotted (default is undotted)
169 * @return newly constructed index */
170 spinidx(const ex & v, const ex & dim = 2, bool covariant = false, bool dotted = false);
172 // functions overriding virtual functions from base classes
174 bool is_dummy_pair_same_type(const basic & other) const;
175 // complex conjugation
176 ex conjugate() const { return toggle_dot(); }
179 bool match_same_type(const basic & other) const;
181 // non-virtual functions in this class
183 /** Check whether the index is dotted. */
184 bool is_dotted() const {return dotted;}
186 /** Check whether the index is not dotted. */
187 bool is_undotted() const {return !dotted;}
189 /** Make a new index with the same value and variance but the opposite
191 ex toggle_dot() const;
193 /** Make a new index with the same value but opposite variance and
195 ex toggle_variance_dot() const;
198 void do_print(const print_context & c, unsigned level) const;
199 void do_print_latex(const print_latex & c, unsigned level) const;
200 void do_print_tree(const print_tree & c, unsigned level) const;
210 /** Specialization of is_exactly_a<idx>(obj) for idx objects. */
211 template<> inline bool is_exactly_a<idx>(const basic & obj)
213 return obj.tinfo()==TINFO_idx;
216 /** Specialization of is_exactly_a<varidx>(obj) for varidx objects. */
217 template<> inline bool is_exactly_a<varidx>(const basic & obj)
219 return obj.tinfo()==TINFO_varidx;
222 /** Specialization of is_exactly_a<spinidx>(obj) for spinidx objects. */
223 template<> inline bool is_exactly_a<spinidx>(const basic & obj)
225 return obj.tinfo()==TINFO_spinidx;
228 /** Check whether two indices form a dummy pair. */
229 bool is_dummy_pair(const idx & i1, const idx & i2);
231 /** Check whether two expressions form a dummy index pair. */
232 bool is_dummy_pair(const ex & e1, const ex & e2);
234 /** Given a vector of indices, split them into two vectors, one containing
235 * the free indices, the other containing the dummy indices (numeric
236 * indices are neither free nor dummy ones).
238 * @param it Pointer to start of index vector
239 * @param itend Pointer to end of index vector
240 * @param out_free Vector of free indices (returned, sorted)
241 * @param out_dummy Vector of dummy indices (returned, sorted) */
242 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy);
244 /** Given a vector of indices, split them into two vectors, one containing
245 * the free indices, the other containing the dummy indices (numeric
246 * indices are neither free nor dummy ones).
248 * @param v Index vector
249 * @param out_free Vector of free indices (returned, sorted)
250 * @param out_dummy Vector of dummy indices (returned, sorted) */
251 inline void find_free_and_dummy(const exvector & v, exvector & out_free, exvector & out_dummy)
253 find_free_and_dummy(v.begin(), v.end(), out_free, out_dummy);
256 /** Given a vector of indices, find the dummy indices.
258 * @param v Index vector
259 * @param out_dummy Vector of dummy indices (returned, sorted) */
260 inline void find_dummy_indices(const exvector & v, exvector & out_dummy)
262 exvector free_indices;
263 find_free_and_dummy(v.begin(), v.end(), free_indices, out_dummy);
266 /** Count the number of dummy index pairs in an index vector. */
267 inline size_t count_dummy_indices(const exvector & v)
269 exvector free_indices, dummy_indices;
270 find_free_and_dummy(v.begin(), v.end(), free_indices, dummy_indices);
271 return dummy_indices.size();
274 /** Count the number of dummy index pairs in an index vector. */
275 inline size_t count_free_indices(const exvector & v)
277 exvector free_indices, dummy_indices;
278 find_free_and_dummy(v.begin(), v.end(), free_indices, dummy_indices);
279 return free_indices.size();
282 /** Return the minimum of two index dimensions. If this is undecidable,
283 * throw an exception. Numeric dimensions are always considered "smaller"
284 * than symbolic dimensions. */
285 ex minimal_dim(const ex & dim1, const ex & dim2);
289 #endif // ndef __GINAC_IDX_H__