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