]> www.ginac.de Git - ginac.git/blob - ginac/indexed.cpp
0f6726e543a633ed93bad6dcb3b8c550af6710ec
[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-2000 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(0);
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(1);
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 indexed::indexed(const ex & i1) : inherited(i1)
91 {
92     debugmsg("indexed constructor from ex",LOGLEVEL_CONSTRUCT);
93     tinfo_key=TINFO_indexed;
94     GINAC_ASSERT(all_of_type_idx());
95 }
96
97 indexed::indexed(const ex & i1, const ex & i2) : inherited(i1,i2)
98 {
99     debugmsg("indexed constructor from ex,ex",LOGLEVEL_CONSTRUCT);
100     tinfo_key=TINFO_indexed;
101     GINAC_ASSERT(all_of_type_idx());
102 }
103
104 indexed::indexed(const ex & i1, const ex & i2, const ex & i3)
105     : inherited(i1,i2,i3)
106 {
107     debugmsg("indexed constructor from ex,ex,ex",LOGLEVEL_CONSTRUCT);
108     tinfo_key=TINFO_indexed;
109     GINAC_ASSERT(all_of_type_idx());
110 }
111
112 indexed::indexed(const ex & i1, const ex & i2, const ex & i3, const ex & i4)
113     : inherited(i1,i2,i3,i4)
114 {
115     debugmsg("indexed constructor from ex,ex,ex,ex",LOGLEVEL_CONSTRUCT);
116     tinfo_key=TINFO_indexed;
117     GINAC_ASSERT(all_of_type_idx());
118 }
119
120 indexed::indexed(const exvector & iv) : inherited(iv)
121 {
122     debugmsg("indexed constructor from exvector",LOGLEVEL_CONSTRUCT);
123     tinfo_key=TINFO_indexed;
124     GINAC_ASSERT(all_of_type_idx());
125 }
126
127 indexed::indexed(exvector * ivp) : inherited(ivp)
128 {
129     debugmsg("indexed constructor from exvector *",LOGLEVEL_CONSTRUCT);
130     tinfo_key=TINFO_indexed;
131     GINAC_ASSERT(all_of_type_idx());
132 }
133
134 //////////
135 // archiving
136 //////////
137
138 /** Construct object from archive_node. */
139 indexed::indexed(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
140 {
141     debugmsg("indexed constructor from archive_node", LOGLEVEL_CONSTRUCT);
142     tinfo_key = TINFO_indexed;
143 }
144
145 /** Unarchive the object. */
146 ex indexed::unarchive(const archive_node &n, const lst &sym_lst)
147 {
148     return (new indexed(n, sym_lst))->setflag(status_flags::dynallocated);
149 }
150
151 /** Archive the object. */
152 void indexed::archive(archive_node &n) const
153 {
154     inherited::archive(n);
155 }
156
157 //////////
158 // functions overriding virtual functions from bases classes
159 //////////
160
161 // public
162
163 basic * indexed::duplicate() const
164 {
165     debugmsg("indexed duplicate",LOGLEVEL_DUPLICATE);
166     return new indexed(*this);
167 }
168
169 void indexed::printraw(ostream & os) const
170 {
171     debugmsg("indexed printraw",LOGLEVEL_PRINT);
172     os << "indexed(indices=";
173     printrawindices(os);
174     os << ",hash=" << hashvalue << ",flags=" << flags << ")";
175 }
176
177 void indexed::printtree(ostream & os, unsigned indent) const
178 {
179     debugmsg("indexed printtree",LOGLEVEL_PRINT);
180     os << string(indent,' ') << "indexed: " << seq.size() << " indices";
181     os << ",hash=" << hashvalue << ",flags=" << flags << endl;
182     printtreeindices(os,indent);
183 }
184
185 void indexed::print(ostream & os, unsigned upper_precedence) const
186 {
187     debugmsg("indexed print",LOGLEVEL_PRINT);
188     os << "UNNAMEDINDEX";
189     printindices(os);
190 }
191
192 void indexed::printcsrc(ostream & os, unsigned type,
193                         unsigned upper_precedence) const
194 {
195     debugmsg("indexed print csrc",LOGLEVEL_PRINT);
196     print(os,upper_precedence);
197 }
198
199 bool indexed::info(unsigned inf) const
200 {
201     if (inf==info_flags::indexed) return true;
202     if (inf==info_flags::has_indices) return seq.size()!=0;
203     return inherited::info(inf);
204 }
205
206 exvector indexed::get_indices(void) const
207 {
208     return seq;
209
210     /*
211     idxvector filtered_indices;
212     filtered_indices.reserve(indices.size());
213     for (idxvector::const_iterator cit=indices.begin(); cit!=indices.end(); ++cit) {
214         if ((*cit).get_type()==t) {
215             filtered_indices.push_back(*cit);
216         }
217     }
218     return filtered_indices;
219     */
220 }
221
222 // protected
223
224 /** Implementation of ex::diff() for an indexed object. It always returns 0.
225  *  @see ex::diff */
226 ex indexed::derivative(const symbol & s) const
227 {
228         return _ex0();
229 }
230
231 int indexed::compare_same_type(const basic & other) const
232 {
233     GINAC_ASSERT(is_of_type(other,indexed));
234     return inherited::compare_same_type(other);
235 }
236
237 bool indexed::is_equal_same_type(const basic & other) const
238 {
239     GINAC_ASSERT(is_of_type(other,indexed));
240     return inherited::is_equal_same_type(other);
241 }
242
243 unsigned indexed::return_type(void) const
244 {
245     return return_types::noncommutative;
246 }
247    
248 unsigned indexed::return_type_tinfo(void) const
249 {
250     return tinfo_key;
251 }
252
253 ex indexed::thisexprseq(const exvector & v) const
254 {
255     return indexed(v);
256 }
257
258 ex indexed::thisexprseq(exvector * vp) const
259 {
260     return indexed(vp);
261 }
262
263 //////////
264 // virtual functions which can be overridden by derived classes
265 //////////
266
267 // none
268
269 //////////
270 // non-virtual functions in this class
271 //////////
272
273 // protected
274
275 void indexed::printrawindices(ostream & os) const
276 {
277     if (seq.size()!=0) {
278         for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
279             (*cit).printraw(os);
280             os << ",";
281         }
282     }
283 }
284
285 void indexed::printtreeindices(ostream & os, unsigned indent) const
286 {
287     if (seq.size()!=0) {
288         for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
289             os << string(indent+delta_indent,' ');
290             (*cit).printraw(os);
291             os << endl;
292         }
293     }
294 }
295
296 void indexed::printindices(ostream & os) const
297 {
298     if (seq.size()!=0) {
299         if (seq.size()>1) {
300             os << "{";
301         }
302         exvector::const_iterator last=seq.end()-1;
303         exvector::const_iterator cit=seq.begin();
304         for (; cit!=last; ++cit) {
305             (*cit).print(os);
306             os << ",";
307         }
308         (*cit).print(os);
309         if (seq.size()>1) {
310             os << "}";
311         }
312     }
313 }
314
315 bool indexed::all_of_type_idx(void) const
316 {
317     // used only inside of ASSERTs
318     for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
319         if (!is_ex_of_type(*cit,idx)) return false;
320     }
321     return true;
322 }
323
324 //////////
325 // static member variables
326 //////////
327
328 // none
329
330 //////////
331 // global constants
332 //////////
333
334 const indexed some_indexed;
335 const type_info & typeid_indexed=typeid(some_indexed);
336
337 #ifndef NO_NAMESPACE_GINAC
338 } // namespace GiNaC
339 #endif // ndef NO_NAMESPACE_GINAC