3 * Interface to GiNaC's indices. */
6 * GiNaC Copyright (C) 1999-2002 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;
53 unsigned nops() const;
55 ex evalf(int level = 0) const;
56 ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const;
59 ex derivative(const symbol & s) const;
60 bool match_same_type(const basic & other) const;
62 // new virtual functions in this class
64 /** Check whether the index forms a dummy index pair with another index
65 * of the same type. */
66 virtual bool is_dummy_pair_same_type(const basic & other) const;
68 // non-virtual functions in this class
70 /** Get value of index. */
71 ex get_value(void) const {return value;}
73 /** Check whether the index is numeric. */
74 bool is_numeric(void) const {return is_exactly_a<numeric>(value);}
76 /** Check whether the index is symbolic. */
77 bool is_symbolic(void) const {return !is_exactly_a<numeric>(value);}
79 /** Get dimension of index space. */
80 ex get_dim(void) const {return dim;}
82 /** Check whether the dimension is numeric. */
83 bool is_dim_numeric(void) const {return is_exactly_a<numeric>(dim);}
85 /** Check whether the dimension is symbolic. */
86 bool is_dim_symbolic(void) const {return !is_exactly_a<numeric>(dim);}
89 ex value; /**< Expression that constitutes the index (numeric or symbolic name) */
90 ex dim; /**< Dimension of space (can be symbolic or numeric) */
94 /** This class holds an index with a variance (co- or contravariant). There
95 * is an associated metric tensor that can be used to raise/lower indices. */
96 class varidx : public idx
98 GINAC_DECLARE_REGISTERED_CLASS(varidx, idx)
100 // other constructors
102 /** Construct index with given value, dimension and variance.
104 * @param v Value of index (numeric or symbolic)
105 * @param dim Dimension of index space (numeric or symbolic)
106 * @param covariant Make covariant index (default is contravariant)
107 * @return newly constructed index */
108 varidx(const ex & v, const ex & dim, bool covariant = false);
110 // functions overriding virtual functions from base classes
112 void print(const print_context & c, unsigned level = 0) const;
113 bool is_dummy_pair_same_type(const basic & other) const;
116 bool match_same_type(const basic & other) const;
118 // non-virtual functions in this class
120 /** Check whether the index is covariant. */
121 bool is_covariant(void) const {return covariant;}
123 /** Check whether the index is contravariant (not covariant). */
124 bool is_contravariant(void) const {return !covariant;}
126 /** Make a new index with the same value but the opposite variance. */
127 ex toggle_variance(void) const;
131 bool covariant; /**< x.mu, default is contravariant: x~mu */
135 /** This class holds a spinor index that can be dotted or undotted and that
136 * also has a variance. This is used in the Weyl-van-der-Waerden formalism
137 * where the dot indicates complex conjugation. There is an associated
138 * (asymmetric) metric tensor that can be used to raise/lower spinor
140 class spinidx : public varidx
142 GINAC_DECLARE_REGISTERED_CLASS(spinidx, varidx)
144 // other constructors
146 /** Construct index with given value, dimension, variance and dot.
148 * @param v Value of index (numeric or symbolic)
149 * @param dim Dimension of index space (numeric or symbolic)
150 * @param covariant Make covariant index (default is contravariant)
151 * @param dotted Make covariant dotted (default is undotted)
152 * @return newly constructed index */
153 spinidx(const ex & v, const ex & dim = 2, bool covariant = false, bool dotted = false);
155 // functions overriding virtual functions from base classes
157 void print(const print_context & c, unsigned level = 0) const;
158 bool is_dummy_pair_same_type(const basic & other) const;
161 bool match_same_type(const basic & other) const;
163 // non-virtual functions in this class
165 /** Check whether the index is dotted. */
166 bool is_dotted(void) const {return dotted;}
168 /** Check whether the index is not dotted. */
169 bool is_undotted(void) const {return !dotted;}
171 /** Make a new index with the same value and variance but the opposite
173 ex toggle_dot(void) const;
175 /** Make a new index with the same value but opposite variance and
177 ex toggle_variance_dot(void) const;
187 /** Specialization of is_exactly_a<idx>(obj) for idx objects. */
188 template<> inline bool is_exactly_a<idx>(const basic & obj)
190 return obj.tinfo()==TINFO_idx;
193 /** Specialization of is_exactly_a<varidx>(obj) for varidx objects. */
194 template<> inline bool is_exactly_a<varidx>(const basic & obj)
196 return obj.tinfo()==TINFO_varidx;
199 /** Specialization of is_exactly_a<spinidx>(obj) for spinidx objects. */
200 template<> inline bool is_exactly_a<spinidx>(const basic & obj)
202 return obj.tinfo()==TINFO_spinidx;
205 /** Check whether two indices form a dummy pair. */
206 bool is_dummy_pair(const idx & i1, const idx & i2);
208 /** Check whether two expressions form a dummy index pair. */
209 bool is_dummy_pair(const ex & e1, const ex & e2);
211 /** Given a vector of indices, split them into two vectors, one containing
212 * the free indices, the other containing the dummy indices (numeric
213 * indices are neither free nor dummy ones).
215 * @param it Pointer to start of index vector
216 * @param itend Pointer to end of index vector
217 * @param out_free Vector of free indices (returned, sorted)
218 * @param out_dummy Vector of dummy indices (returned, sorted) */
219 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy);
221 /** Given a vector of indices, split them into two vectors, one containing
222 * the free indices, the other containing the dummy indices (numeric
223 * indices are neither free nor dummy ones).
225 * @param v Index vector
226 * @param out_free Vector of free indices (returned, sorted)
227 * @param out_dummy Vector of dummy indices (returned, sorted) */
228 inline void find_free_and_dummy(const exvector & v, exvector & out_free, exvector & out_dummy)
230 find_free_and_dummy(v.begin(), v.end(), out_free, out_dummy);
233 /** Given a vector of indices, find the dummy indices.
235 * @param v Index vector
236 * @param out_dummy Vector of dummy indices (returned, sorted) */
237 inline void find_dummy_indices(const exvector & v, exvector & out_dummy)
239 exvector free_indices;
240 find_free_and_dummy(v.begin(), v.end(), free_indices, out_dummy);
243 /** Count the number of dummy index pairs in an index vector. */
244 inline unsigned count_dummy_indices(const exvector & v)
246 exvector free_indices, dummy_indices;
247 find_free_and_dummy(v.begin(), v.end(), free_indices, dummy_indices);
248 return dummy_indices.size();
251 /** Count the number of dummy index pairs in an index vector. */
252 inline unsigned count_free_indices(const exvector & v)
254 exvector free_indices, dummy_indices;
255 find_free_and_dummy(v.begin(), v.end(), free_indices, dummy_indices);
256 return free_indices.size();
261 #endif // ndef __GINAC_IDX_H__