3 * Interface to GiNaC's indexed expressions. */
6 * GiNaC Copyright (C) 1999-2001 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);
48 /** Construct indexed object with no index.
50 * @param b Base expression
51 * @return newly constructed indexed object */
52 indexed(const ex & b);
54 /** Construct indexed object with one index. The index must be of class idx.
56 * @param b Base expression
58 * @return newly constructed indexed object */
59 indexed(const ex & b, const ex & i1);
61 /** Construct indexed object with two indices. The indices must be of class idx.
63 * @param b Base expression
64 * @param i1 First index
65 * @param i2 Second index
66 * @return newly constructed indexed object */
67 indexed(const ex & b, const ex & i1, const ex & i2);
69 /** Construct indexed object with three indices. The indices must be of class idx.
71 * @param b Base expression
72 * @param i1 First index
73 * @param i2 Second index
74 * @param i3 Third index
75 * @return newly constructed indexed object */
76 indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3);
78 /** Construct indexed object with four indices. The indices must be of class idx.
80 * @param b Base expression
81 * @param i1 First index
82 * @param i2 Second index
83 * @param i3 Third index
84 * @param i4 Fourth index
85 * @return newly constructed indexed object */
86 indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3, const ex & i4);
88 /** Construct indexed object with two indices and a specified symmetry. The
89 * indices must be of class idx.
91 * @param b Base expression
92 * @param symm Symmetry of indices
93 * @param i1 First index
94 * @param i2 Second index
95 * @return newly constructed indexed object */
96 indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2);
98 /** Construct indexed object with three indices and a specified symmetry.
99 * The indices must be of class idx.
101 * @param b Base expression
102 * @param symm Symmetry of indices
103 * @param i1 First index
104 * @param i2 Second index
105 * @param i3 Third index
106 * @return newly constructed indexed object */
107 indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3);
109 /** Construct indexed object with four indices and a specified symmetry. The
110 * indices must be of class idx.
112 * @param b Base expression
113 * @param symm Symmetry of indices
114 * @param i1 First index
115 * @param i2 Second index
116 * @param i3 Third index
117 * @param i4 Fourth index
118 * @return newly constructed indexed object */
119 indexed(const ex & b, const symmetry & symm, const ex & i1, const ex & i2, const ex & i3, const ex & i4);
121 /** Construct indexed object with a specified vector of indices. The indices
122 * must be of class idx.
124 * @param b Base expression
125 * @param iv Vector of indices
126 * @return newly constructed indexed object */
127 indexed(const ex & b, const exvector & iv);
129 /** Construct indexed object with a specified vector of indices and
130 * symmetry. The indices must be of class idx.
132 * @param b Base expression
133 * @param symm Symmetry of indices
134 * @param iv Vector of indices
135 * @return newly constructed indexed object */
136 indexed(const ex & b, const symmetry & symm, const exvector & iv);
138 // internal constructors
139 indexed(const symmetry & symm, const exprseq & es);
140 indexed(const symmetry & symm, const exvector & v, bool discardable = false);
141 indexed(const symmetry & symm, exvector * vp); // vp will be deleted
143 // functions overriding virtual functions from base classes
145 void print(const print_context & c, unsigned level = 0) const;
146 bool info(unsigned inf) const;
147 ex eval(int level = 0) const;
148 int degree(const ex & s) const;
149 int ldegree(const ex & s) const;
150 ex coeff(const ex & s, int n = 1) const;
151 exvector get_free_indices(void) const;
154 ex derivative(const symbol & s) const;
155 ex thisexprseq(const exvector & v) const;
156 ex thisexprseq(exvector * vp) const;
157 unsigned return_type(void) const { return return_types::commutative; }
158 ex expand(unsigned options = 0) const;
160 // new virtual functions which can be overridden by derived classes
163 // non-virtual functions in this class
165 /** Check whether all index values have a certain property.
166 * @see class info_flags */
167 bool all_index_values_are(unsigned inf) const;
169 /** Return a vector containing the object's indices. */
170 exvector get_indices(void) const;
172 /** Return a vector containing the dummy indices of the object, if any. */
173 exvector get_dummy_indices(void) const;
175 /** Return a vector containing the dummy indices in the contraction with
176 * another indexed object. */
177 exvector get_dummy_indices(const indexed & other) const;
179 /** Check whether the object has an index that forms a dummy index pair
180 * with a given index. */
181 bool has_dummy_index_for(const ex & i) const;
183 /** Return symmetry properties. */
184 ex get_symmetry(void) const {return symtree;}
187 void printindices(const print_context & c, unsigned level) const;
188 void validate(void) const;
192 ex symtree; /**< Index symmetry (tree of symmetry objects) */
196 typedef std::pair<ex, ex> spmapkey;
198 struct spmapkey_is_less {
199 bool operator() (const spmapkey &p, const spmapkey &q) const
201 int cmp = p.first.compare(q.first);
202 return ((cmp<0) || (!(cmp>0) && p.second.compare(q.second)<0));
206 typedef std::map<spmapkey, ex, spmapkey_is_less> spmap;
208 /** Helper class for storing information about known scalar products which
209 * are to be automatically replaced by simplify_indexed().
211 * @see simplify_indexed */
212 class scalar_products {
214 /** Register scalar product pair and its value. */
215 void add(const ex & v1, const ex & v2, const ex & sp);
217 /** Register list of vectors. This adds all possible pairs of products
218 * a.i * b.i with the value a*b (note that this is not a scalar vector
219 * product but an ordinary product of scalars). */
220 void add_vectors(const lst & l);
222 /** Clear all registered scalar products. */
225 bool is_defined(const ex & v1, const ex & v2) const;
226 ex evaluate(const ex & v1, const ex & v2) const;
227 void debugprint(void) const;
230 static spmapkey make_key(const ex & v1, const ex & v2);
232 spmap spm; /*< Map from defined scalar product pairs to their values */
238 /** Specialization of is_exactly_a<indexed>(obj) for indexed objects. */
239 template<> inline bool is_exactly_a<indexed>(const basic & obj)
241 return obj.tinfo()==TINFO_indexed;
246 #endif // ndef __GINAC_INDEXED_H__