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