]> www.ginac.de Git - ginac.git/blob - ginac/idx.h
beb137d12d967f4c940825ec8ace48d392b210ef
[ginac.git] / ginac / idx.h
1 /** @file idx.h
2  *
3  *  Interface to GiNaC's indices. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2024 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  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 /** 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
36 {
37         GINAC_DECLARE_REGISTERED_CLASS(idx, basic)
38
39         // other constructors
40 public:
41         /** Construct index with given value and dimension.
42          *
43          *  @param v Value of index (numeric or symbolic)
44          *  @param dim Dimension of index space (numeric or symbolic) */
45         explicit idx(const ex & v, const ex & dim);
46
47         // functions overriding virtual functions from base classes
48 public:
49         bool info(unsigned inf) const override;
50         size_t nops() const override;
51         ex op(size_t i) const override;
52         ex map(map_function & f) const override;
53         ex evalf() const override;
54         ex subs(const exmap & m, unsigned options = 0) const override;
55         void archive(archive_node& n) const override;
56         void read_archive(const archive_node& n, lst& syms) override;
57 protected:
58         ex derivative(const symbol & s) const override;
59         bool match_same_type(const basic & other) const override;
60         unsigned calchash() const override;
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() const {return value;}
72
73         /** Check whether the index is numeric. */
74         bool is_numeric() const {return is_exactly_a<numeric>(value);}
75
76         /** Check whether the index is symbolic. */
77         bool is_symbolic() const {return !is_exactly_a<numeric>(value);}
78
79         /** Get dimension of index space. */
80         ex get_dim() const {return dim;}
81
82         /** Check whether the dimension is numeric. */
83         bool is_dim_numeric() const {return is_exactly_a<numeric>(dim);}
84
85         /** Check whether the dimension is symbolic. */
86         bool is_dim_symbolic() 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         void print_index(const print_context & c, unsigned level) const;
97         void do_print(const print_context & c, unsigned level) const;
98         void do_print_csrc(const print_csrc & 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;
101
102 protected:
103         ex value; /**< Expression that constitutes the index (numeric or symbolic name) */
104         ex dim;   /**< Dimension of space (can be symbolic or numeric) */
105 };
106 GINAC_DECLARE_UNARCHIVER(idx); 
107
108
109 /** This class holds an index with a variance (co- or contravariant). There
110  *  is an associated metric tensor that can be used to raise/lower indices. */
111 class varidx : public idx
112 {
113         GINAC_DECLARE_REGISTERED_CLASS(varidx, idx)
114
115         // other constructors
116 public:
117         /** Construct index with given value, dimension and variance.
118          *
119          *  @param v Value of index (numeric or symbolic)
120          *  @param dim Dimension of index space (numeric or symbolic)
121          *  @param covariant Make covariant index (default is contravariant) */
122         varidx(const ex & v, const ex & dim, bool covariant = false);
123
124         // functions overriding virtual functions from base classes
125 public:
126         bool is_dummy_pair_same_type(const basic & other) const override;
127         void archive(archive_node& n) const override;
128         void read_archive(const archive_node& n, lst& syms) override;
129 protected:
130         bool match_same_type(const basic & other) const override;
131
132         // non-virtual functions in this class
133 public:
134         /** Check whether the index is covariant. */
135         bool is_covariant() const {return covariant;}
136
137         /** Check whether the index is contravariant (not covariant). */
138         bool is_contravariant() const {return !covariant;}
139
140         /** Make a new index with the same value but the opposite variance. */
141         ex toggle_variance() const;
142
143 protected:
144         void do_print(const print_context & c, unsigned level) const;
145         void do_print_tree(const print_tree & c, unsigned level) const;
146
147         // member variables
148 protected:
149         bool covariant; /**< x.mu, default is contravariant: x~mu */
150 };
151 GINAC_DECLARE_UNARCHIVER(varidx);
152
153
154 /** This class holds a spinor index that can be dotted or undotted and that
155  *  also has a variance. This is used in the Weyl-van-der-Waerden formalism
156  *  where the dot indicates complex conjugation. There is an associated
157  *  (asymmetric) metric tensor that can be used to raise/lower spinor
158  *  indices. */
159 class spinidx : public varidx
160 {
161         GINAC_DECLARE_REGISTERED_CLASS(spinidx, varidx)
162
163         // other constructors
164 public:
165         /** Construct index with given value, dimension, variance and dot.
166          *
167          *  @param v Value of index (numeric or symbolic)
168          *  @param dim Dimension of index space (numeric or symbolic)
169          *  @param covariant Make covariant index (default is contravariant)
170          *  @param dotted Make covariant dotted (default is undotted) */
171         spinidx(const ex & v, const ex & dim = 2, bool covariant = false, bool dotted = false);
172
173         // functions overriding virtual functions from base classes
174 public:
175         bool is_dummy_pair_same_type(const basic & other) const override;
176         // complex conjugation
177         ex conjugate() const override { return toggle_dot(); }
178         void archive(archive_node& n) const override;
179         void read_archive(const archive_node& n, lst& syms) override;
180 protected:
181         bool match_same_type(const basic & other) const override;
182
183         // non-virtual functions in this class
184 public:
185         /** Check whether the index is dotted. */
186         bool is_dotted() const {return dotted;}
187
188         /** Check whether the index is not dotted. */
189         bool is_undotted() const {return !dotted;}
190
191         /** Make a new index with the same value and variance but the opposite
192          *  dottedness. */
193         ex toggle_dot() const;
194
195         /** Make a new index with the same value but opposite variance and
196          *  dottedness. */
197         ex toggle_variance_dot() const;
198
199 protected:
200         void do_print(const print_context & c, unsigned level) const;
201         void do_print_latex(const print_latex & c, unsigned level) const;
202         void do_print_tree(const print_tree & c, unsigned level) const;
203
204         // member variables
205 protected:
206         bool dotted;
207 };
208 GINAC_DECLARE_UNARCHIVER(spinidx);
209
210
211 // utility functions
212
213 /** Check whether two indices form a dummy pair. */
214 bool is_dummy_pair(const idx & i1, const idx & i2);
215
216 /** Check whether two expressions form a dummy index pair. */
217 bool is_dummy_pair(const ex & e1, const ex & e2);
218
219 /** Given a vector of indices, split them into two vectors, one containing
220  *  the free indices, the other containing the dummy indices (numeric
221  *  indices are neither free nor dummy ones).
222  *
223  *  @param it Pointer to start of index vector
224  *  @param itend Pointer to end of index vector
225  *  @param out_free Vector of free indices (returned, sorted)
226  *  @param out_dummy Vector of dummy indices (returned, sorted) */
227 void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy);
228
229 /** Given a vector of indices, split them into two vectors, one containing
230  *  the free indices, the other containing the dummy indices (numeric
231  *  indices are neither free nor dummy ones).
232  *
233  *  @param v Index vector
234  *  @param out_free Vector of free indices (returned, sorted)
235  *  @param out_dummy Vector of dummy indices (returned, sorted) */
236 inline void find_free_and_dummy(const exvector & v, exvector & out_free, exvector & out_dummy)
237 {
238         find_free_and_dummy(v.begin(), v.end(), out_free, out_dummy);
239 }
240
241 /** Given a vector of indices, find the dummy indices.
242  *
243  *  @param v Index vector
244  *  @param out_dummy Vector of dummy indices (returned, sorted) */
245 inline void find_dummy_indices(const exvector & v, exvector & out_dummy)
246 {
247         exvector free_indices;
248         find_free_and_dummy(v.begin(), v.end(), free_indices, out_dummy);
249 }
250
251 /** Count the number of dummy index pairs in an index vector. */
252 inline size_t count_dummy_indices(const exvector & v)
253 {
254         exvector free_indices, dummy_indices;
255         find_free_and_dummy(v.begin(), v.end(), free_indices, dummy_indices);
256         return dummy_indices.size();
257 }
258
259 /** Count the number of dummy index pairs in an index vector. */
260 inline size_t count_free_indices(const exvector & v)
261 {
262         exvector free_indices, dummy_indices;
263         find_free_and_dummy(v.begin(), v.end(), free_indices, dummy_indices);
264         return free_indices.size();
265 }
266
267 /** Return the minimum of two index dimensions. If this is undecidable,
268  *  throw an exception. Numeric dimensions are always considered "smaller"
269  *  than symbolic dimensions. */
270 ex minimal_dim(const ex & dim1, const ex & dim2);
271
272 } // namespace GiNaC
273
274 #endif // ndef GINAC_IDX_H