]> www.ginac.de Git - ginac.git/blob - ginac/indexed.cpp
GINAC_DECLARE_REGISTERED_CLASS declares duplicate() and compare_same_type(),
[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 void indexed::printraw(std::ostream & os) const
173 {
174         debugmsg("indexed printraw",LOGLEVEL_PRINT);
175         os << "indexed(indices=";
176         printrawindices(os);
177         os << ",hash=" << hashvalue << ",flags=" << flags << ")";
178 }
179
180 void indexed::printtree(std::ostream & os, unsigned indent) const
181 {
182         debugmsg("indexed printtree",LOGLEVEL_PRINT);
183         os << std::string(indent,' ') << "indexed: " << seq.size() << " indices";
184         os << ",hash=" << hashvalue << ",flags=" << flags << std::endl;
185         printtreeindices(os,indent);
186 }
187
188 void indexed::print(std::ostream & os, unsigned upper_precedence) const
189 {
190         debugmsg("indexed print",LOGLEVEL_PRINT);
191         os << "UNNAMEDINDEX";
192         printindices(os);
193 }
194
195 void indexed::printcsrc(std::ostream & os, unsigned type,
196                         unsigned upper_precedence) const
197 {
198         debugmsg("indexed print csrc",LOGLEVEL_PRINT);
199         print(os,upper_precedence);
200 }
201
202 bool indexed::info(unsigned inf) const
203 {
204         if (inf==info_flags::indexed) return true;
205         if (inf==info_flags::has_indices) return seq.size()!=0;
206         return inherited::info(inf);
207 }
208
209 // protected
210
211 /** Implementation of ex::diff() for an indexed object. It always returns 0.
212  *  @see ex::diff */
213 ex indexed::derivative(const symbol & s) const
214 {
215         return _ex0();
216 }
217
218 int indexed::compare_same_type(const basic & other) const
219 {
220         GINAC_ASSERT(is_of_type(other,indexed));
221         return inherited::compare_same_type(other);
222 }
223
224 bool indexed::is_equal_same_type(const basic & other) const
225 {
226         GINAC_ASSERT(is_of_type(other,indexed));
227         return inherited::is_equal_same_type(other);
228 }
229
230 unsigned indexed::return_type(void) const
231 {
232         return return_types::noncommutative;
233 }
234    
235 unsigned indexed::return_type_tinfo(void) const
236 {
237         return tinfo_key;
238 }
239
240 ex indexed::thisexprseq(const exvector & v) const
241 {
242         return indexed(v);
243 }
244
245 ex indexed::thisexprseq(exvector * vp) const
246 {
247         return indexed(vp);
248 }
249
250 //////////
251 // virtual functions which can be overridden by derived classes
252 //////////
253
254 // none
255
256 //////////
257 // non-virtual functions in this class
258 //////////
259
260 // protected
261
262 void indexed::printrawindices(std::ostream & os) const
263 {
264         if (seq.size()!=0) {
265                 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
266                         (*cit).printraw(os);
267                         os << ",";
268                 }
269         }
270 }
271
272 void indexed::printtreeindices(std::ostream & os, unsigned indent) const
273 {
274         if (seq.size()!=0) {
275                 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
276                         os << std::string(indent+delta_indent,' ');
277                         (*cit).printraw(os);
278                         os << std::endl;
279                 }
280         }
281 }
282
283 void indexed::printindices(std::ostream & os) const
284 {
285         if (seq.size()!=0) {
286                 if (seq.size()>1) {
287                         os << "{";
288                 }
289                 exvector::const_iterator last=seq.end()-1;
290                 exvector::const_iterator cit=seq.begin();
291                 for (; cit!=last; ++cit) {
292                         (*cit).print(os);
293                         os << ",";
294                 }
295                 (*cit).print(os);
296                 if (seq.size()>1) {
297                         os << "}";
298                 }
299         }
300 }
301
302 /** Check whether all indices are of class idx or a subclass. This function
303  *  is used internally to make sure that all constructed indexed objects
304  *  really carry indices and not some other classes. */
305 bool indexed::all_of_type_idx(void) const
306 {
307         for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
308                 if (!is_ex_of_type(*cit,idx)) return false;
309         }
310         return true;
311 }
312
313 #ifndef NO_NAMESPACE_GINAC
314 } // namespace GiNaC
315 #endif // ndef NO_NAMESPACE_GINAC