3 * Interface to GiNaC's indexed expressions. */
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_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 * @return newly constructed indexed object */
53 indexed(const ex & b);
55 /** Construct indexed object with one index. The index must be of class idx.
57 * @param b Base expression
59 * @return newly constructed indexed object */
60 indexed(const ex & b, const ex & i1);
62 /** Construct indexed object with two indices. The indices must be of class idx.
64 * @param b Base expression
65 * @param i1 First index
66 * @param i2 Second index
67 * @return newly constructed indexed object */
68 indexed(const ex & b, const ex & i1, const ex & i2);
70 /** Construct indexed object with three indices. The indices must be of class idx.
72 * @param b Base expression
73 * @param i1 First index
74 * @param i2 Second index
75 * @param i3 Third index
76 * @return newly constructed indexed object */
77 indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3);
79 /** Construct indexed object with four indices. The indices must be of class idx.
81 * @param b Base expression
82 * @param i1 First index
83 * @param i2 Second index
84 * @param i3 Third index
85 * @param i4 Fourth index
86 * @return newly constructed indexed object */
87 indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3, const ex & i4);
89 /** Construct indexed object with two indices and a specified symmetry. The
90 * indices must be of class idx.
92 * @param b Base expression
93 * @param symm Symmetry of indices
94 * @param i1 First index
95 * @param i2 Second index
96 * @return newly constructed indexed object */
97 indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2);
99 /** Construct indexed object with three indices and a specified symmetry.
100 * The indices must be of class idx.
102 * @param b Base expression
103 * @param symm Symmetry of indices
104 * @param i1 First index
105 * @param i2 Second index
106 * @param i3 Third index
107 * @return newly constructed indexed object */
108 indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3);
110 /** Construct indexed object with four indices and a specified symmetry. The
111 * indices must be of class idx.
113 * @param b Base expression
114 * @param symm Symmetry of indices
115 * @param i1 First index
116 * @param i2 Second index
117 * @param i3 Third index
118 * @param i4 Fourth index
119 * @return newly constructed indexed object */
120 indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3, const ex & i4);
122 /** Construct indexed object with a specified vector of indices. The indices
123 * must be of class idx.
125 * @param b Base expression
126 * @param iv Vector of indices
127 * @return newly constructed indexed object */
128 indexed(const ex & b, const exvector & iv);
130 /** Construct indexed object with a specified vector of indices and
131 * symmetry. The indices must be of class idx.
133 * @param b Base expression
134 * @param symm Symmetry of indices
135 * @param iv Vector of indices
136 * @return newly constructed indexed object */
137 indexed(const ex & b, const symmetry & symm, const exvector & iv);
139 // internal constructors
140 indexed(const symmetry & symm, const exprseq & es);
141 indexed(const symmetry & symm, const exvector & v, bool discardable = false);
142 indexed(const symmetry & symm, exvector * vp); // vp will be deleted
144 // functions overriding virtual functions from base classes
146 void print(const print_context & c, unsigned level = 0) const;
147 bool info(unsigned inf) const;
148 ex eval(int level = 0) const;
149 exvector get_free_indices(void) const;
152 ex derivative(const symbol & s) const;
153 ex thisexprseq(const exvector & v) const;
154 ex thisexprseq(exvector * vp) const;
155 unsigned return_type(void) const { return return_types::commutative; }
156 ex expand(unsigned options = 0) const;
158 // new virtual functions which can be overridden by derived classes
161 // non-virtual functions in this class
163 /** Check whether all index values have a certain property.
164 * @see class info_flags */
165 bool all_index_values_are(unsigned inf) const;
167 /** Return a vector containing the object's indices. */
168 exvector get_indices(void) const;
170 /** Return a vector containing the dummy indices of the object, if any. */
171 exvector get_dummy_indices(void) const;
173 /** Return a vector containing the dummy indices in the contraction with
174 * another indexed object. */
175 exvector get_dummy_indices(const indexed & other) const;
177 /** Check whether the object has an index that forms a dummy index pair
178 * with a given index. */
179 bool has_dummy_index_for(const ex & i) const;
181 /** Return symmetry properties. */
182 ex get_symmetry(void) const {return symtree;}
185 void printindices(const print_context & c, unsigned level) const;
186 void validate(void) const;
190 ex symtree; /**< Index symmetry (tree of symmetry objects) */
194 typedef std::pair<ex, ex> spmapkey;
196 struct spmapkey_is_less {
197 bool operator() (const spmapkey &p, const spmapkey &q) const
199 int cmp = p.first.compare(q.first);
200 return ((cmp<0) || (!(cmp>0) && p.second.compare(q.second)<0));
204 typedef std::map<spmapkey, ex, spmapkey_is_less> spmap;
206 /** Helper class for storing information about known scalar products which
207 * are to be automatically replaced by simplify_indexed().
209 * @see simplify_indexed */
210 class scalar_products {
212 /** Register scalar product pair and its value. */
213 void add(const ex & v1, const ex & v2, const ex & sp);
215 /** Register list of vectors. This adds all possible pairs of products
216 * a.i * b.i with the value a*b (note that this is not a scalar vector
217 * product but an ordinary product of scalars). */
218 void add_vectors(const lst & l);
220 /** Clear all registered scalar products. */
223 bool is_defined(const ex & v1, const ex & v2) const;
224 ex evaluate(const ex & v1, const ex & v2) const;
225 void debugprint(void) const;
228 static spmapkey make_key(const ex & v1, const ex & v2);
230 spmap spm; /*< Map from defined scalar product pairs to their values */
236 /** Specialization of is_exactly_a<indexed>(obj) for indexed objects. */
237 template<> inline bool is_exactly_a<indexed>(const basic & obj)
239 return obj.tinfo()==TINFO_indexed;
244 #endif // ndef __GINAC_INDEXED_H__