]> www.ginac.de Git - ginac.git/blob - ginac/indexed.cpp
Adapted to new version 1.2.5.
[ginac.git] / ginac / indexed.cpp
1 /** @file indexed.cpp
2  *
3  *  Implementation of GiNaC's index carrying objects. */
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 <string>
24
25 #include "indexed.h"
26 #include "ex.h"
27 #include "idx.h"
28 #include "debugmsg.h"
29
30 #ifndef NO_NAMESPACE_GINAC
31 namespace GiNaC {
32 #endif // ndef NO_NAMESPACE_GINAC
33
34 GINAC_IMPLEMENT_REGISTERED_CLASS(indexed, exprseq)
35
36 //////////
37 // default constructor, destructor, copy constructor assignment operator and helpers
38 //////////
39
40 // public
41
42 indexed::indexed()
43 {
44         debugmsg("indexed default constructor",LOGLEVEL_CONSTRUCT);
45         tinfo_key=TINFO_indexed;
46 }
47
48 indexed::~indexed()
49 {
50         debugmsg("indexed destructor",LOGLEVEL_DESTRUCT);
51         destroy(false);
52 }
53
54 indexed::indexed(const indexed & other)
55 {
56         debugmsg("indexed copy constructor",LOGLEVEL_CONSTRUCT);
57         copy (other);
58 }
59
60 const indexed & indexed::operator=(const indexed & other)
61 {
62         debugmsg("indexed operator=",LOGLEVEL_ASSIGNMENT);
63         if (this != &other) {
64                 destroy(true);
65                 copy(other);
66         }
67         return *this;
68 }
69
70 // protected
71
72 void indexed::copy(const indexed & other)
73 {
74         inherited::copy(other);
75 }
76
77 void indexed::destroy(bool call_parent)
78 {
79         if (call_parent) {
80                 inherited::destroy(call_parent);
81         }
82 }
83
84 //////////
85 // other constructors
86 //////////
87
88 // public
89
90 /** Construct indexed object with one index. The index must be of class idx
91  *  or a subclass.
92  *
93  *  @param i1 The index
94  *  @return newly constructed indexed object */
95 indexed::indexed(const ex & i1) : inherited(i1)
96 {
97         debugmsg("indexed constructor from ex",LOGLEVEL_CONSTRUCT);
98         tinfo_key=TINFO_indexed;
99         GINAC_ASSERT(all_of_type_idx());
100 }
101
102 /** Construct indexed object with two indices. The indices must be of class
103  *  idx or a subclass.
104  *
105  *  @param i1 First index
106  *  @param i2 Second index
107  *  @return newly constructed indexed object */
108 indexed::indexed(const ex & i1, const ex & i2) : inherited(i1,i2)
109 {
110         debugmsg("indexed constructor from ex,ex",LOGLEVEL_CONSTRUCT);
111         tinfo_key=TINFO_indexed;
112         GINAC_ASSERT(all_of_type_idx());
113 }
114
115 /** Construct indexed object with three indices. The indices must be of class
116  *  idx or a subclass.
117  *
118  *  @param i1 First index
119  *  @param i2 Second index
120  *  @param i3 Third index
121  *  @return newly constructed indexed object */
122 indexed::indexed(const ex & i1, const ex & i2, const ex & i3)
123   : inherited(i1,i2,i3)
124 {
125         debugmsg("indexed constructor from ex,ex,ex",LOGLEVEL_CONSTRUCT);
126         tinfo_key=TINFO_indexed;
127         GINAC_ASSERT(all_of_type_idx());
128 }
129
130 /** Construct indexed object with four indices. The indices must be of class
131  *  idx or a subclass.
132  *
133  *  @param i1 First index
134  *  @param i2 Second index
135  *  @param i3 Third index
136  *  @param i4 Fourth index
137  *  @return newly constructed indexed object */
138 indexed::indexed(const ex & i1, const ex & i2, const ex & i3, const ex & i4)
139   : inherited(i1,i2,i3,i4)
140 {
141         debugmsg("indexed constructor from ex,ex,ex,ex",LOGLEVEL_CONSTRUCT);
142         tinfo_key=TINFO_indexed;
143         GINAC_ASSERT(all_of_type_idx());
144 }
145
146 /** Construct indexed object with a specified vector of indices. The indices
147  *  must be of class idx or a subclass.
148  *
149  *  @param iv Vector of indices
150  *  @return newly constructed indexed object */
151 indexed::indexed(const exvector & iv) : inherited(iv)
152 {
153         debugmsg("indexed constructor from exvector",LOGLEVEL_CONSTRUCT);
154         tinfo_key=TINFO_indexed;
155         GINAC_ASSERT(all_of_type_idx());
156 }
157
158 indexed::indexed(exvector * ivp) : inherited(ivp)
159 {
160         debugmsg("indexed constructor from exvector *",LOGLEVEL_CONSTRUCT);
161         tinfo_key=TINFO_indexed;
162         GINAC_ASSERT(all_of_type_idx());
163 }
164
165 //////////
166 // archiving
167 //////////
168
169 /** Construct object from archive_node. */
170 indexed::indexed(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
171 {
172         debugmsg("indexed constructor from archive_node", LOGLEVEL_CONSTRUCT);
173         tinfo_key = TINFO_indexed;
174 }
175
176 /** Unarchive the object. */
177 ex indexed::unarchive(const archive_node &n, const lst &sym_lst)
178 {
179         return (new indexed(n, sym_lst))->setflag(status_flags::dynallocated);
180 }
181
182 /** Archive the object. */
183 void indexed::archive(archive_node &n) const
184 {
185         inherited::archive(n);
186 }
187
188 //////////
189 // functions overriding virtual functions from bases classes
190 //////////
191
192 // public
193
194 basic * indexed::duplicate() const
195 {
196         debugmsg("indexed duplicate",LOGLEVEL_DUPLICATE);
197         return new indexed(*this);
198 }
199
200 void indexed::printraw(std::ostream & os) const
201 {
202         debugmsg("indexed printraw",LOGLEVEL_PRINT);
203         os << "indexed(indices=";
204         printrawindices(os);
205         os << ",hash=" << hashvalue << ",flags=" << flags << ")";
206 }
207
208 void indexed::printtree(std::ostream & os, unsigned indent) const
209 {
210         debugmsg("indexed printtree",LOGLEVEL_PRINT);
211         os << std::string(indent,' ') << "indexed: " << seq.size() << " indices";
212         os << ",hash=" << hashvalue << ",flags=" << flags << std::endl;
213         printtreeindices(os,indent);
214 }
215
216 void indexed::print(std::ostream & os, unsigned upper_precedence) const
217 {
218         debugmsg("indexed print",LOGLEVEL_PRINT);
219         os << "UNNAMEDINDEX";
220         printindices(os);
221 }
222
223 void indexed::printcsrc(std::ostream & os, unsigned type,
224                         unsigned upper_precedence) const
225 {
226         debugmsg("indexed print csrc",LOGLEVEL_PRINT);
227         print(os,upper_precedence);
228 }
229
230 bool indexed::info(unsigned inf) const
231 {
232         if (inf==info_flags::indexed) return true;
233         if (inf==info_flags::has_indices) return seq.size()!=0;
234         return inherited::info(inf);
235 }
236
237 // protected
238
239 /** Implementation of ex::diff() for an indexed object. It always returns 0.
240  *  @see ex::diff */
241 ex indexed::derivative(const symbol & s) const
242 {
243         return _ex0();
244 }
245
246 int indexed::compare_same_type(const basic & other) const
247 {
248         GINAC_ASSERT(is_of_type(other,indexed));
249         return inherited::compare_same_type(other);
250 }
251
252 bool indexed::is_equal_same_type(const basic & other) const
253 {
254         GINAC_ASSERT(is_of_type(other,indexed));
255         return inherited::is_equal_same_type(other);
256 }
257
258 unsigned indexed::return_type(void) const
259 {
260         return return_types::noncommutative;
261 }
262    
263 unsigned indexed::return_type_tinfo(void) const
264 {
265         return tinfo_key;
266 }
267
268 ex indexed::thisexprseq(const exvector & v) const
269 {
270         return indexed(v);
271 }
272
273 ex indexed::thisexprseq(exvector * vp) const
274 {
275         return indexed(vp);
276 }
277
278 //////////
279 // virtual functions which can be overridden by derived classes
280 //////////
281
282 // none
283
284 //////////
285 // non-virtual functions in this class
286 //////////
287
288 // protected
289
290 void indexed::printrawindices(std::ostream & os) const
291 {
292         if (seq.size()!=0) {
293                 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
294                         (*cit).printraw(os);
295                         os << ",";
296                 }
297         }
298 }
299
300 void indexed::printtreeindices(std::ostream & os, unsigned indent) const
301 {
302         if (seq.size()!=0) {
303                 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
304                         os << std::string(indent+delta_indent,' ');
305                         (*cit).printraw(os);
306                         os << std::endl;
307                 }
308         }
309 }
310
311 void indexed::printindices(std::ostream & os) const
312 {
313         if (seq.size()!=0) {
314                 if (seq.size()>1) {
315                         os << "{";
316                 }
317                 exvector::const_iterator last=seq.end()-1;
318                 exvector::const_iterator cit=seq.begin();
319                 for (; cit!=last; ++cit) {
320                         (*cit).print(os);
321                         os << ",";
322                 }
323                 (*cit).print(os);
324                 if (seq.size()>1) {
325                         os << "}";
326                 }
327         }
328 }
329
330 /** Check whether all indices are of class idx or a subclass. This function
331  *  is used internally to make sure that all constructed indexed objects
332  *  really carry indices and not some other classes. */
333 bool indexed::all_of_type_idx(void) const
334 {
335         for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
336                 if (!is_ex_of_type(*cit,idx)) return false;
337         }
338         return true;
339 }
340
341 //////////
342 // static member variables
343 //////////
344
345 // none
346
347 //////////
348 // global constants
349 //////////
350
351 const indexed some_indexed;
352 const std::type_info & typeid_indexed = typeid(some_indexed);
353
354 #ifndef NO_NAMESPACE_GINAC
355 } // namespace GiNaC
356 #endif // ndef NO_NAMESPACE_GINAC