]> www.ginac.de Git - ginac.git/blob - ginac/idx.cpp
added tutorial section about indexed objects
[ginac.git] / ginac / idx.cpp
1 /** @file idx.cpp
2  *
3  *  Implementation of GiNaC's indices. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <stdexcept>
24
25 #include "idx.h"
26 #include "symbol.h"
27 #include "lst.h"
28 #include "archive.h"
29 #include "utils.h"
30 #include "debugmsg.h"
31
32 namespace GiNaC {
33
34 GINAC_IMPLEMENT_REGISTERED_CLASS(idx, basic)
35 GINAC_IMPLEMENT_REGISTERED_CLASS(varidx, idx)
36
37 //////////
38 // default constructor, destructor, copy constructor assignment operator and helpers
39 //////////
40
41 idx::idx() : inherited(TINFO_idx)
42 {
43         debugmsg("idx default constructor", LOGLEVEL_CONSTRUCT);
44 }
45
46 varidx::varidx() : covariant(false)
47 {
48         debugmsg("varidx default constructor", LOGLEVEL_CONSTRUCT);
49         tinfo_key = TINFO_varidx;
50 }
51
52 void idx::copy(const idx & other)
53 {
54         inherited::copy(other);
55         value = other.value;
56         dim = other.dim;
57 }
58
59 void varidx::copy(const varidx & other)
60 {
61         inherited::copy(other);
62         covariant = other.covariant;
63 }
64
65 void idx::destroy(bool call_parent)
66 {
67         if (call_parent)
68                 inherited::destroy(call_parent);
69 }
70
71 void varidx::destroy(bool call_parent)
72 {
73         if (call_parent)
74                 inherited::destroy(call_parent);
75 }
76
77 //////////
78 // other constructors
79 //////////
80
81 idx::idx(const ex & v, const ex & d) : inherited(TINFO_idx), value(v), dim(d)
82 {
83         debugmsg("idx constructor from ex,ex", LOGLEVEL_CONSTRUCT);
84         if (is_dim_numeric())
85                 if (!dim.info(info_flags::posint))
86                         throw(std::invalid_argument("dimension of space must be a positive integer"));
87 }
88
89 varidx::varidx(const ex & v, const ex & d, bool cov) : inherited(v, d), covariant(cov)
90 {
91         debugmsg("varidx constructor from ex,ex,bool", LOGLEVEL_CONSTRUCT);
92         tinfo_key = TINFO_varidx;
93 }
94
95 //////////
96 // archiving
97 //////////
98
99 idx::idx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
100 {
101         debugmsg("idx constructor from archive_node", LOGLEVEL_CONSTRUCT);
102         n.find_ex("value", value, sym_lst);
103         n.find_ex("dim", dim, sym_lst);
104 }
105
106 varidx::varidx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
107 {
108         debugmsg("varidx constructor from archive_node", LOGLEVEL_CONSTRUCT);
109         n.find_bool("covariant", covariant);
110 }
111
112 ex idx::unarchive(const archive_node &n, const lst &sym_lst)
113 {
114         return (new idx(n, sym_lst))->setflag(status_flags::dynallocated);
115 }
116
117 ex varidx::unarchive(const archive_node &n, const lst &sym_lst)
118 {
119         return (new varidx(n, sym_lst))->setflag(status_flags::dynallocated);
120 }
121
122 void idx::archive(archive_node &n) const
123 {
124         inherited::archive(n);
125         n.add_ex("value", value);
126         n.add_ex("dim", dim);
127 }
128
129 void varidx::archive(archive_node &n) const
130 {
131         inherited::archive(n);
132         n.add_bool("covariant", covariant);
133 }
134
135 //////////
136 // functions overriding virtual functions from bases classes
137 //////////
138
139 void idx::printraw(std::ostream & os) const
140 {
141         debugmsg("idx printraw", LOGLEVEL_PRINT);
142
143         os << class_name() << "(";
144         value.printraw(os);
145         os << ",dim=";
146         dim.printraw(os);
147         os << ",hash=" << hashvalue << ",flags=" << flags;
148         os << ")";
149 }
150
151 void idx::printtree(std::ostream & os, unsigned indent) const
152 {
153         debugmsg("idx printtree",LOGLEVEL_PRINT);
154
155         os << std::string(indent, ' ') << "type=" << class_name();
156         value.printtree(os, indent + delta_indent);
157         os << std::string(indent, ' ');
158         os << ", hash=" << hashvalue
159            << " (0x" << std::hex << hashvalue << std::dec << ")"
160            << ", flags=" << flags << std::endl;
161 }
162
163 void idx::print(std::ostream & os, unsigned upper_precedence) const
164 {
165         debugmsg("idx print", LOGLEVEL_PRINT);
166
167         os << ".";
168
169         bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol));
170         if (need_parens)
171                 os << "(";
172         os << value;
173         if (need_parens)
174                 os << ")";
175 }
176
177 void varidx::print(std::ostream & os, unsigned upper_precedence) const
178 {
179         debugmsg("varidx print", LOGLEVEL_PRINT);
180
181         if (covariant)
182                 os << ".";
183         else
184                 os << "~";
185
186         bool need_parens = !(is_ex_exactly_of_type(value, numeric) || is_ex_of_type(value, symbol));
187         if (need_parens)
188                 os << "(";
189         os << value;
190         if (need_parens)
191                 os << ")";
192 }
193
194 bool idx::info(unsigned inf) const
195 {
196         if (inf == info_flags::idx)
197                 return true;
198         return inherited::info(inf);
199 }
200
201 /** Returns order relation between two indices of the same type. The order
202  *  must be such that dummy indices lie next to each other. */
203 int idx::compare_same_type(const basic & other) const
204 {
205         GINAC_ASSERT(is_of_type(other, idx));
206         const idx &o = static_cast<const idx &>(other);
207
208         int cmpval = value.compare(o.value);
209         if (cmpval)
210                 return cmpval;
211         return dim.compare(o.dim);
212 }
213
214 int varidx::compare_same_type(const basic & other) const
215 {
216         GINAC_ASSERT(is_of_type(other, varidx));
217         const varidx &o = static_cast<const varidx &>(other);
218
219         int cmpval = inherited::compare_same_type(other);
220         if (cmpval)
221                 return cmpval;
222
223         // Check variance last so dummy indices will end up next to each other
224         if (covariant != o.covariant)
225                 return covariant ? -1 : 1;
226         return 0;
227 }
228
229 ex idx::subs(const lst & ls, const lst & lr) const
230 {
231         GINAC_ASSERT(ls.nops() == lr.nops());
232
233         // First look for index substitutions
234         for (unsigned i=0; i<ls.nops(); i++) {
235                 if (is_equal(*(ls.op(i)).bp)) {
236
237                         // Substitution index->index
238                         if (is_ex_of_type(lr.op(i), idx))
239                                 return lr.op(i);
240
241                         // Otherwise substitute value
242                         idx *i_copy = static_cast<idx *>(duplicate());
243                         i_copy->value = lr.op(i);
244                         return i_copy->setflag(status_flags::dynallocated);
245                 }
246         }
247
248         // None, substitute objects in value (not in dimension)
249         const ex &subsed_value = value.subs(ls, lr);
250         if (are_ex_trivially_equal(value, subsed_value))
251                 return *this;
252
253         idx *i_copy = static_cast<idx *>(duplicate());
254         i_copy->value = subsed_value;
255         return i_copy->setflag(status_flags::dynallocated);
256 }
257
258 //////////
259 // new virtual functions
260 //////////
261
262 bool idx::is_dummy_pair_same_type(const basic & other) const
263 {
264         const idx &o = static_cast<const idx &>(other);
265
266         // Only pure symbols form dummy pairs, "2n+1" doesn't
267         if (!is_ex_of_type(value, symbol))
268                 return false;
269
270         // Value must be equal, of course
271         if (!value.is_equal(o.value))
272                 return false;
273
274         // Also the dimension
275         return dim.is_equal(o.dim);
276 }
277
278 bool varidx::is_dummy_pair_same_type(const basic & other) const
279 {
280         const varidx &o = static_cast<const varidx &>(other);
281
282         // Variance must be opposite
283         if (covariant == o.covariant)
284                 return false;
285
286         return inherited::is_dummy_pair_same_type(other);
287 }
288
289 //////////
290 // non-virtual functions
291 //////////
292
293 ex varidx::toggle_variance(void) const
294 {
295         varidx *i_copy = static_cast<varidx *>(duplicate());
296         i_copy->covariant = !i_copy->covariant;
297         i_copy->clearflag(status_flags::hash_calculated);
298         return i_copy->setflag(status_flags::dynallocated);
299 }
300
301 //////////
302 // global functions
303 //////////
304
305 bool is_dummy_pair(const idx & i1, const idx & i2)
306 {
307         // The indices must be of exactly the same type
308         if (i1.tinfo() != i2.tinfo())
309                 return false;
310
311         // Same type, let the indices decide whether they are paired
312         return i1.is_dummy_pair_same_type(i2);
313 }
314
315 bool is_dummy_pair(const ex & e1, const ex & e2)
316 {
317         // The expressions must be indices
318         if (!is_ex_of_type(e1, idx) || !is_ex_of_type(e2, idx))
319                 return false;
320
321         return is_dummy_pair(ex_to_idx(e1), ex_to_idx(e2));
322 }
323
324 } // namespace GiNaC