3 * Interface to GiNaC's indexed expressions. */
6 * GiNaC Copyright (C) 1999-2024 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #ifndef GINAC_INDEXED_H
24 #define GINAC_INDEXED_H
33 class scalar_products;
36 /** This class holds an indexed expression. It consists of a 'base' expression
37 * (the expression being indexed) which can be accessed as op(0), and n (n >= 0)
38 * indices (all of class idx), accessible as op(1)..op(n). */
39 class indexed : public exprseq
41 GINAC_DECLARE_REGISTERED_CLASS(indexed, exprseq)
43 friend ex simplify_indexed(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp);
44 friend ex simplify_indexed_product(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp);
45 friend bool reposition_dummy_indices(ex & e, exvector & variant_dummy_indices, exvector & moved_indices);
49 /** Construct indexed object with no index.
51 * @param b Base expression */
52 indexed(const ex & b);
54 /** Construct indexed object with one index. The index must be of class idx.
56 * @param b Base expression
57 * @param i1 The index */
58 indexed(const ex & b, const ex & i1);
60 /** Construct indexed object with two indices. The indices must be of class idx.
62 * @param b Base expression
63 * @param i1 First index
64 * @param i2 Second index */
65 indexed(const ex & b, const ex & i1, const ex & i2);
67 /** Construct indexed object with three indices. The indices must be of class idx.
69 * @param b Base expression
70 * @param i1 First index
71 * @param i2 Second index
72 * @param i3 Third index */
73 indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3);
75 /** Construct indexed object with four indices. The indices must be of class idx.
77 * @param b Base expression
78 * @param i1 First index
79 * @param i2 Second index
80 * @param i3 Third index
81 * @param i4 Fourth index */
82 indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3, const ex & i4);
84 /** Construct indexed object with two indices and a specified symmetry. The
85 * indices must be of class idx.
87 * @param b Base expression
88 * @param symm Symmetry of indices
89 * @param i1 First index
90 * @param i2 Second index */
91 indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2);
93 /** Construct indexed object with three indices and a specified symmetry.
94 * The indices must be of class idx.
96 * @param b Base expression
97 * @param symm Symmetry of indices
98 * @param i1 First index
99 * @param i2 Second index
100 * @param i3 Third index */
101 indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3);
103 /** Construct indexed object with four indices and a specified symmetry. The
104 * indices must be of class idx.
106 * @param b Base expression
107 * @param symm Symmetry of indices
108 * @param i1 First index
109 * @param i2 Second index
110 * @param i3 Third index
111 * @param i4 Fourth index */
112 indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3, const ex & i4);
114 /** Construct indexed object with a specified vector of indices. The indices
115 * must be of class idx.
117 * @param b Base expression
118 * @param iv Vector of indices */
119 indexed(const ex & b, const exvector & iv);
121 /** Construct indexed object with a specified vector of indices and
122 * symmetry. The indices must be of class idx.
124 * @param b Base expression
125 * @param symm Symmetry of indices
126 * @param iv Vector of indices */
127 indexed(const ex & b, const symmetry & symm, const exvector & iv);
129 // internal constructors
130 indexed(const symmetry & symm, const exprseq & es);
131 indexed(const symmetry & symm, const exvector & v);
132 indexed(const symmetry & symm, exvector && v);
134 // functions overriding virtual functions from base classes
136 unsigned precedence() const override {return 55;}
137 bool info(unsigned inf) const override;
138 ex eval() const override;
139 ex real_part() const override;
140 ex imag_part() const override;
141 exvector get_free_indices() const override;
143 /** Save (a.k.a. serialize) indexed object into archive. */
144 void archive(archive_node& n) const override;
145 /** Read (a.k.a. deserialize) indexed object from archive. */
146 void read_archive(const archive_node& n, lst& syms) override;
148 ex derivative(const symbol & s) const override;
149 ex thiscontainer(const exvector & v) const override;
150 ex thiscontainer(exvector && v) const override;
151 unsigned return_type() const override;
152 return_type_t return_type_tinfo() const override { return op(0).return_type_tinfo(); }
153 ex expand(unsigned options = 0) const override;
155 // new virtual functions which can be overridden by derived classes
158 // non-virtual functions in this class
160 /** Check whether all index values have a certain property.
161 * @see class info_flags */
162 bool all_index_values_are(unsigned inf) const;
164 /** Return a vector containing the object's indices. */
165 exvector get_indices() const;
167 /** Return a vector containing the dummy indices of the object, if any. */
168 exvector get_dummy_indices() const;
170 /** Return a vector containing the dummy indices in the contraction with
171 * another indexed object. This is symmetric: a.get_dummy_indices(b)
172 * == b.get_dummy_indices(a) */
173 exvector get_dummy_indices(const indexed & other) const;
175 /** Check whether the object has an index that forms a dummy index pair
176 * with a given index. */
177 bool has_dummy_index_for(const ex & i) const;
179 /** Return symmetry properties. */
180 ex get_symmetry() const {return symtree;}
183 void printindices(const print_context & c, unsigned level) const;
184 void print_indexed(const print_context & c, const char *openbrace, const char *closebrace, unsigned level) const;
185 void do_print(const print_context & c, unsigned level) const;
186 void do_print_latex(const print_latex & c, unsigned level) const;
187 void do_print_tree(const print_tree & c, unsigned level) const;
188 void validate() const;
192 ex symtree; /**< Index symmetry (tree of symmetry objects) */
194 GINAC_DECLARE_UNARCHIVER(indexed);
199 spmapkey() : dim(wild()) {}
200 spmapkey(const ex & v1, const ex & v2, const ex & dim = wild());
202 bool operator==(const spmapkey &other) const;
203 bool operator<(const spmapkey &other) const;
205 void debugprint() const;
211 typedef std::map<spmapkey, ex> spmap;
213 /** Helper class for storing information about known scalar products which
214 * are to be automatically replaced by simplify_indexed().
216 * @see simplify_indexed */
217 class scalar_products {
219 /** Register scalar product pair and its value. */
220 void add(const ex & v1, const ex & v2, const ex & sp);
222 /** Register scalar product pair and its value for a specific space dimension. */
223 void add(const ex & v1, const ex & v2, const ex & dim, const ex & sp);
225 /** Register list of vectors. This adds all possible pairs of products
226 * a.i * b.i with the value a*b (note that this is not a scalar vector
227 * product but an ordinary product of scalars). */
228 void add_vectors(const lst & l, const ex & dim = wild());
230 /** Clear all registered scalar products. */
233 bool is_defined(const ex & v1, const ex & v2, const ex & dim) const;
234 ex evaluate(const ex & v1, const ex & v2, const ex & dim) const;
235 void debugprint() const;
238 spmap spm; /*< Map from defined scalar product pairs to their values */
244 /** Returns all dummy indices from the expression */
245 exvector get_all_dummy_indices(const ex & e);
247 /** More reliable version of the form. The former assumes that e is an
248 * expanded expression. */
249 exvector get_all_dummy_indices_safely(const ex & e);
251 /** Returns b with all dummy indices, which are listed in va, renamed
252 * if modify_va is set to TRUE all dummy indices of b will be appended to va */
253 ex rename_dummy_indices_uniquely(exvector & va, const ex & b, bool modify_va = false);
255 /** Returns b with all dummy indices, which are common with a, renamed */
256 ex rename_dummy_indices_uniquely(const ex & a, const ex & b);
258 /** Same as above, where va and vb contain the indices of a and b and are sorted */
259 ex rename_dummy_indices_uniquely(const exvector & va, const exvector & vb, const ex & b);
261 /** Similar to above, where va and vb are the same and the return value is a list of two lists
262 * for substitution in b */
263 lst rename_dummy_indices_uniquely(const exvector & va, const exvector & vb);
265 /** This function returns the given expression with expanded sums
266 * for all dummy index summations, where the dimensionality of
267 * the dummy index is a nonnegative integer.
268 * Optionally all indices with a variance will be substituted by
269 * indices with the corresponding numeric values without variance.
271 * @param e the given expression
272 * @param subs_idx indicates if variance of dummy indices should be neglected
274 ex expand_dummy_sum(const ex & e, bool subs_idx = false);
278 #endif // ndef GINAC_INDEXED_H