3 * Interface to GiNaC's indexed expressions. */
6 * GiNaC Copyright (C) 1999-2003 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_INDEXED_H__
24 #define __GINAC_INDEXED_H__
34 class scalar_products;
37 /** This class holds an indexed expression. It consists of a 'base' expression
38 * (the expression being indexed) which can be accessed as op(0), and n (n >= 0)
39 * indices (all of class idx), accessible as op(1)..op(n). */
40 class indexed : public exprseq
42 GINAC_DECLARE_REGISTERED_CLASS(indexed, exprseq)
44 friend ex simplify_indexed(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp);
45 friend ex simplify_indexed_product(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp);
46 friend bool reposition_dummy_indices(ex & e, exvector & variant_dummy_indices, exvector & moved_indices);
50 /** Construct indexed object with no index.
52 * @param b Base expression
53 * @return newly constructed indexed object */
54 indexed(const ex & b);
56 /** Construct indexed object with one index. The index must be of class idx.
58 * @param b Base expression
60 * @return newly constructed indexed object */
61 indexed(const ex & b, const ex & i1);
63 /** Construct indexed object with two indices. The indices must be of class idx.
65 * @param b Base expression
66 * @param i1 First index
67 * @param i2 Second index
68 * @return newly constructed indexed object */
69 indexed(const ex & b, const ex & i1, const ex & i2);
71 /** Construct indexed object with three indices. The indices must be of class idx.
73 * @param b Base expression
74 * @param i1 First index
75 * @param i2 Second index
76 * @param i3 Third index
77 * @return newly constructed indexed object */
78 indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3);
80 /** Construct indexed object with four indices. The indices must be of class idx.
82 * @param b Base expression
83 * @param i1 First index
84 * @param i2 Second index
85 * @param i3 Third index
86 * @param i4 Fourth index
87 * @return newly constructed indexed object */
88 indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3, const ex & i4);
90 /** Construct indexed object with two indices and a specified symmetry. The
91 * indices must be of class idx.
93 * @param b Base expression
94 * @param symm Symmetry of indices
95 * @param i1 First index
96 * @param i2 Second index
97 * @return newly constructed indexed object */
98 indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2);
100 /** Construct indexed object with three indices and a specified symmetry.
101 * The indices must be of class idx.
103 * @param b Base expression
104 * @param symm Symmetry of indices
105 * @param i1 First index
106 * @param i2 Second index
107 * @param i3 Third index
108 * @return newly constructed indexed object */
109 indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3);
111 /** Construct indexed object with four indices and a specified symmetry. The
112 * indices must be of class idx.
114 * @param b Base expression
115 * @param symm Symmetry of indices
116 * @param i1 First index
117 * @param i2 Second index
118 * @param i3 Third index
119 * @param i4 Fourth index
120 * @return newly constructed indexed object */
121 indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3, const ex & i4);
123 /** Construct indexed object with a specified vector of indices. The indices
124 * must be of class idx.
126 * @param b Base expression
127 * @param iv Vector of indices
128 * @return newly constructed indexed object */
129 indexed(const ex & b, const exvector & iv);
131 /** Construct indexed object with a specified vector of indices and
132 * symmetry. The indices must be of class idx.
134 * @param b Base expression
135 * @param symm Symmetry of indices
136 * @param iv Vector of indices
137 * @return newly constructed indexed object */
138 indexed(const ex & b, const symmetry & symm, const exvector & iv);
140 // internal constructors
141 indexed(const symmetry & symm, const exprseq & es);
142 indexed(const symmetry & symm, const exvector & v, bool discardable = false);
143 indexed(const symmetry & symm, exvector * vp); // vp will be deleted
145 // functions overriding virtual functions from base classes
147 unsigned precedence() const {return 55;}
148 bool info(unsigned inf) const;
149 ex eval(int level = 0) const;
150 exvector get_free_indices() const;
153 ex derivative(const symbol & s) const;
154 ex thiscontainer(const exvector & v) const;
155 ex thiscontainer(exvector * vp) const;
156 unsigned return_type() const { return return_types::commutative; }
157 ex expand(unsigned options = 0) const;
159 // new virtual functions which can be overridden by derived classes
162 // non-virtual functions in this class
164 /** Check whether all index values have a certain property.
165 * @see class info_flags */
166 bool all_index_values_are(unsigned inf) const;
168 /** Return a vector containing the object's indices. */
169 exvector get_indices() const;
171 /** Return a vector containing the dummy indices of the object, if any. */
172 exvector get_dummy_indices() const;
174 /** Return a vector containing the dummy indices in the contraction with
175 * another indexed object. */
176 exvector get_dummy_indices(const indexed & other) const;
178 /** Check whether the object has an index that forms a dummy index pair
179 * with a given index. */
180 bool has_dummy_index_for(const ex & i) const;
182 /** Return symmetry properties. */
183 ex get_symmetry() const {return symtree;}
186 void printindices(const print_context & c, unsigned level) const;
187 void print_indexed(const print_context & c, const char *openbrace, const char *closebrace, unsigned level) const;
188 void do_print(const print_context & c, unsigned level) const;
189 void do_print_latex(const print_latex & c, unsigned level) const;
190 void do_print_tree(const print_tree & c, unsigned level) const;
191 void validate() const;
195 ex symtree; /**< Index symmetry (tree of symmetry objects) */
201 spmapkey() : dim(wild()) {}
202 spmapkey(const ex & v1, const ex & v2, const ex & dim = wild());
204 bool operator==(const spmapkey &other) const;
205 bool operator<(const spmapkey &other) const;
207 void debugprint() const;
213 typedef std::map<spmapkey, ex> spmap;
215 /** Helper class for storing information about known scalar products which
216 * are to be automatically replaced by simplify_indexed().
218 * @see simplify_indexed */
219 class scalar_products {
221 /** Register scalar product pair and its value. */
222 void add(const ex & v1, const ex & v2, const ex & sp);
224 /** Register scalar product pair and its value for a specific space dimension. */
225 void add(const ex & v1, const ex & v2, const ex & dim, const ex & sp);
227 /** Register list of vectors. This adds all possible pairs of products
228 * a.i * b.i with the value a*b (note that this is not a scalar vector
229 * product but an ordinary product of scalars). */
230 void add_vectors(const lst & l, const ex & dim = wild());
232 /** Clear all registered scalar products. */
235 bool is_defined(const ex & v1, const ex & v2, const ex & dim) const;
236 ex evaluate(const ex & v1, const ex & v2, const ex & dim) const;
237 void debugprint() const;
240 spmap spm; /*< Map from defined scalar product pairs to their values */
246 /** Specialization of is_exactly_a<indexed>(obj) for indexed objects. */
247 template<> inline bool is_exactly_a<indexed>(const basic & obj)
249 return obj.tinfo()==TINFO_indexed;
254 #endif // ndef __GINAC_INDEXED_H__