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;
35 /** This class holds an indexed expression. It consists of a 'base' expression
36 * (the expression being indexed) which can be accessed as op(0), and n (n >= 0)
37 * indices (all of class idx), accessible as op(1)..op(n). */
38 class indexed : public exprseq
40 GINAC_DECLARE_REGISTERED_CLASS(indexed, exprseq)
42 friend ex simplify_indexed(const ex & e, exvector & free_indices, const scalar_products & sp);
43 friend ex simplify_indexed_product(const ex & e, exvector & free_indices, const scalar_products & sp);
47 /** Type of symmetry of the object with respect to commutation of its indices. */
49 unknown, /**< symmetry properties unknown */
50 symmetric, /**< totally symmetric */
51 antisymmetric, /**< totally antisymmetric */
52 mixed /**< mixed symmetry (unimplemented) */
57 /** Construct indexed object with no index.
59 * @param b Base expression
60 * @return newly constructed indexed object */
61 indexed(const ex & b);
63 /** Construct indexed object with one index. The index must be of class idx.
65 * @param b Base expression
67 * @return newly constructed indexed object */
68 indexed(const ex & b, const ex & i1);
70 /** Construct indexed object with two indices. The indices must be of class idx.
72 * @param b Base expression
73 * @param i1 First index
74 * @param i2 Second index
75 * @return newly constructed indexed object */
76 indexed(const ex & b, const ex & i1, const ex & i2);
78 /** Construct indexed object with three 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 * @return newly constructed indexed object */
85 indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3);
87 /** Construct indexed object with four indices. The indices must be of class idx.
89 * @param b Base expression
90 * @param i1 First index
91 * @param i2 Second index
92 * @param i3 Third index
93 * @param i4 Fourth index
94 * @return newly constructed indexed object */
95 indexed(const ex & b, const ex & i1, const ex & i2, const ex & i3, const ex & i4);
97 /** Construct indexed object with two indices and a specified symmetry. The
98 * indices must be of class idx.
100 * @param b Base expression
101 * @param symm Symmetry of indices
102 * @param i1 First index
103 * @param i2 Second index
104 * @return newly constructed indexed object */
105 indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2);
107 /** Construct indexed object with three indices and a specified symmetry.
108 * The indices must be of class idx.
110 * @param b Base expression
111 * @param symm Symmetry of indices
112 * @param i1 First index
113 * @param i2 Second index
114 * @param i3 Third index
115 * @return newly constructed indexed object */
116 indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2, const ex & i3);
118 /** Construct indexed object with four indices and a specified symmetry. The
119 * indices must be of class idx.
121 * @param b Base expression
122 * @param symm Symmetry of indices
123 * @param i1 First index
124 * @param i2 Second index
125 * @param i3 Third index
126 * @param i4 Fourth index
127 * @return newly constructed indexed object */
128 indexed(const ex & b, symmetry_type symm, const ex & i1, const ex & i2, const ex & i3, const ex & i4);
130 /** Construct indexed object with a specified vector of indices. The indices
131 * must be of class idx.
133 * @param b Base expression
134 * @param iv Vector of indices
135 * @return newly constructed indexed object */
136 indexed(const ex & b, const exvector & iv);
138 /** Construct indexed object with a specified vector of indices and
139 * symmetry. The indices must be of class idx.
141 * @param b Base expression
142 * @param symm Symmetry of indices
143 * @param iv Vector of indices
144 * @return newly constructed indexed object */
145 indexed(const ex & b, symmetry_type symm, const exvector & iv);
147 // internal constructors
148 indexed(symmetry_type symm, const exprseq & es);
149 indexed(symmetry_type symm, const exvector & v, bool discardable = false);
150 indexed(symmetry_type symm, exvector * vp); // vp will be deleted
152 // functions overriding virtual functions from base classes
154 void print(const print_context & c, unsigned level = 0) const;
155 bool info(unsigned inf) const;
156 ex eval(int level = 0) const;
157 int degree(const ex & s) const;
158 int ldegree(const ex & s) const;
159 ex coeff(const ex & s, int n = 1) const;
160 exvector get_free_indices(void) const;
163 ex thisexprseq(const exvector & v) const;
164 ex thisexprseq(exvector * vp) const;
165 unsigned return_type(void) const { return return_types::commutative; }
166 ex expand(unsigned options = 0) const;
168 // new virtual functions which can be overridden by derived classes
171 // non-virtual functions in this class
173 /** Check whether all index values have a certain property.
174 * @see class info_flags */
175 bool all_index_values_are(unsigned inf) const;
177 /** Return a vector containing the object's indices. */
178 exvector get_indices(void) const;
180 /** Return a vector containing the dummy indices of the object, if any. */
181 exvector get_dummy_indices(void) const;
183 /** Return a vector containing the dummy indices in the contraction with
184 * another indexed object. */
185 exvector get_dummy_indices(const indexed & other) const;
188 void printindices(const print_context & c, unsigned level) const;
189 void assert_all_indices_of_type_idx(void) const;
193 symmetry_type symmetry; /**< Index symmetry */
197 typedef std::pair<ex, ex> spmapkey;
199 struct spmapkey_is_less {
200 bool operator() (const spmapkey &p, const spmapkey &q) const
202 int cmp = p.first.compare(q.first);
203 return ((cmp<0) || (!(cmp>0) && p.second.compare(q.second)<0));
207 typedef std::map<spmapkey, ex, spmapkey_is_less> spmap;
209 /** Helper class for storing information about known scalar products which
210 * are to be automatically replaced by simplify_indexed().
212 * @see simplify_indexed */
213 class scalar_products {
215 /** Register scalar product pair and its value. */
216 void add(const ex & v1, const ex & v2, const ex & sp);
218 /** Clear all registered scalar products. */
221 bool is_defined(const ex & v1, const ex & v2) const;
222 ex evaluate(const ex & v1, const ex & v2) const;
223 void debugprint(void) const;
226 static spmapkey make_key(const ex & v1, const ex & v2);
228 spmap spm; /*< Map from defined scalar product pairs to their values */
233 inline const indexed &ex_to_indexed(const ex &e)
235 return static_cast<const indexed &>(*e.bp);
239 /** Simplify/canonicalize expression containing indexed objects. This
240 * performs contraction of dummy indices where possible and checks whether
241 * the free indices in sums are consistent.
243 * @param e The expression to be simplified
244 * @return simplified expression */
245 ex simplify_indexed(const ex & e);
247 /** Simplify/canonicalize expression containing indexed objects. This
248 * performs contraction of dummy indices where possible, checks whether
249 * the free indices in sums are consistent, and automatically replaces
250 * scalar products by known values if desired.
252 * @param e The expression to be simplified
253 * @param sp Scalar products to be replaced automatically
254 * @return simplified expression */
255 ex simplify_indexed(const ex & e, const scalar_products & sp);
260 #endif // ndef __GINAC_INDEXED_H__