]> www.ginac.de Git - ginac.git/blob - ginac/indexed.cpp
Modification in output of last returned expression
[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_GINAC_NAMESPACE
31 namespace GiNaC {
32 #endif // ndef NO_GINAC_NAMESPACE
33
34 //////////
35 // default constructor, destructor, copy constructor assignment operator and helpers
36 //////////
37
38 // public
39
40 indexed::indexed()
41 {
42     debugmsg("indexed default constructor",LOGLEVEL_CONSTRUCT);
43     tinfo_key=TINFO_indexed;
44 }
45
46 indexed::~indexed()
47 {
48     debugmsg("indexed destructor",LOGLEVEL_DESTRUCT);
49     destroy(0);
50 }
51
52 indexed::indexed(indexed const & other)
53 {
54     debugmsg("indexed copy constructor",LOGLEVEL_CONSTRUCT);
55     copy (other);
56 }
57
58 indexed const & indexed::operator=(indexed const & other)
59 {
60     debugmsg("indexed operator=",LOGLEVEL_ASSIGNMENT);
61     if (this != &other) {
62         destroy(1);
63         copy(other);
64     }
65     return *this;
66 }
67
68 // protected
69
70 void indexed::copy(indexed const & other)
71 {
72     exprseq::copy(other);
73 }
74
75 void indexed::destroy(bool call_parent)
76 {
77     if (call_parent) {
78         exprseq::destroy(call_parent);
79     }
80 }
81
82 //////////
83 // other constructors
84 //////////
85
86 // public
87
88 indexed::indexed(ex const & i1) : exprseq(i1)
89 {
90     debugmsg("indexed constructor from ex",LOGLEVEL_CONSTRUCT);
91     tinfo_key=TINFO_indexed;
92     GINAC_ASSERT(all_of_type_idx());
93 }
94
95 indexed::indexed(ex const & i1, ex const & i2) : exprseq(i1,i2)
96 {
97     debugmsg("indexed constructor from ex,ex",LOGLEVEL_CONSTRUCT);
98     tinfo_key=TINFO_indexed;
99     GINAC_ASSERT(all_of_type_idx());
100 }
101
102 indexed::indexed(ex const & i1, ex const & i2, ex const & i3)
103     : exprseq(i1,i2,i3)
104 {
105     debugmsg("indexed constructor from ex,ex,ex",LOGLEVEL_CONSTRUCT);
106     tinfo_key=TINFO_indexed;
107     GINAC_ASSERT(all_of_type_idx());
108 }
109
110 indexed::indexed(ex const & i1, ex const & i2, ex const & i3, ex const & i4)
111     : exprseq(i1,i2,i3,i4)
112 {
113     debugmsg("indexed constructor from ex,ex,ex,ex",LOGLEVEL_CONSTRUCT);
114     tinfo_key=TINFO_indexed;
115     GINAC_ASSERT(all_of_type_idx());
116 }
117
118 indexed::indexed(exvector const & iv) : exprseq(iv)
119 {
120     debugmsg("indexed constructor from exvector",LOGLEVEL_CONSTRUCT);
121     tinfo_key=TINFO_indexed;
122     GINAC_ASSERT(all_of_type_idx());
123 }
124
125 indexed::indexed(exvector * ivp) : exprseq(ivp)
126 {
127     debugmsg("indexed constructor from exvector *",LOGLEVEL_CONSTRUCT);
128     tinfo_key=TINFO_indexed;
129     GINAC_ASSERT(all_of_type_idx());
130 }
131
132 //////////
133 // functions overriding virtual functions from bases classes
134 //////////
135
136 // public
137
138 basic * indexed::duplicate() const
139 {
140     debugmsg("indexed duplicate",LOGLEVEL_DUPLICATE);
141     return new indexed(*this);
142 }
143
144 void indexed::printraw(ostream & os) const
145 {
146     debugmsg("indexed printraw",LOGLEVEL_PRINT);
147     os << "indexed(indices=";
148     printrawindices(os);
149     os << ",hash=" << hashvalue << ",flags=" << flags << ")";
150 }
151
152 void indexed::printtree(ostream & os, unsigned indent) const
153 {
154     debugmsg("indexed printtree",LOGLEVEL_PRINT);
155     os << string(indent,' ') << "indexed: " << seq.size() << " indices";
156     os << ",hash=" << hashvalue << ",flags=" << flags << endl;
157     printtreeindices(os,indent);
158 }
159
160 void indexed::print(ostream & os, unsigned upper_precedence) const
161 {
162     debugmsg("indexed print",LOGLEVEL_PRINT);
163     os << "UNNAMEDINDEX";
164     printindices(os);
165 }
166
167 void indexed::printcsrc(ostream & os, unsigned type,
168                         unsigned upper_precedence) const
169 {
170     debugmsg("indexed print csrc",LOGLEVEL_PRINT);
171     print(os,upper_precedence);
172 }
173
174 bool indexed::info(unsigned inf) const
175 {
176     if (inf==info_flags::indexed) return true;
177     if (inf==info_flags::has_indices) return seq.size()!=0;
178     return exprseq::info(inf);
179 }
180
181 exvector indexed::get_indices(void) const
182 {
183     return seq;
184
185     /*
186     idxvector filtered_indices;
187     filtered_indices.reserve(indices.size());
188     for (idxvector::const_iterator cit=indices.begin(); cit!=indices.end(); ++cit) {
189         if ((*cit).get_type()==t) {
190             filtered_indices.push_back(*cit);
191         }
192     }
193     return filtered_indices;
194     */
195 }
196
197 // protected
198
199 int indexed::compare_same_type(basic const & other) const
200 {
201     GINAC_ASSERT(is_of_type(other,indexed));
202     return exprseq::compare_same_type(other);
203 }
204
205 bool indexed::is_equal_same_type(basic const & other) const
206 {
207     GINAC_ASSERT(is_of_type(other,indexed));
208     return exprseq::is_equal_same_type(other);
209 }
210
211 unsigned indexed::return_type(void) const
212 {
213     return return_types::noncommutative;
214 }
215    
216 unsigned indexed::return_type_tinfo(void) const
217 {
218     return tinfo_key;
219 }
220
221 ex indexed::thisexprseq(exvector const & v) const
222 {
223     return indexed(v);
224 }
225
226 ex indexed::thisexprseq(exvector * vp) const
227 {
228     return indexed(vp);
229 }
230
231 //////////
232 // virtual functions which can be overridden by derived classes
233 //////////
234
235 // none
236
237 //////////
238 // non-virtual functions in this class
239 //////////
240
241 // protected
242
243 void indexed::printrawindices(ostream & os) const
244 {
245     if (seq.size()!=0) {
246         for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
247             (*cit).printraw(os);
248             os << ",";
249         }
250     }
251 }
252
253 void indexed::printtreeindices(ostream & os, unsigned indent) const
254 {
255     if (seq.size()!=0) {
256         for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
257             os << string(indent+delta_indent,' ');
258             (*cit).printraw(os);
259             os << endl;
260         }
261     }
262 }
263
264 void indexed::printindices(ostream & os) const
265 {
266     if (seq.size()!=0) {
267         if (seq.size()>1) {
268             os << "{";
269         }
270         exvector::const_iterator last=seq.end()-1;
271         exvector::const_iterator cit=seq.begin();
272         for (; cit!=last; ++cit) {
273             (*cit).print(os);
274             os << ",";
275         }
276         (*cit).print(os);
277         if (seq.size()>1) {
278             os << "}";
279         }
280     }
281 }
282
283 bool indexed::all_of_type_idx(void) const
284 {
285     // used only inside of ASSERTs
286     for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
287         if (!is_ex_of_type(*cit,idx)) return false;
288     }
289     return true;
290 }
291
292 //////////
293 // static member variables
294 //////////
295
296 // none
297
298 //////////
299 // global constants
300 //////////
301
302 const indexed some_indexed;
303 type_info const & typeid_indexed=typeid(some_indexed);
304
305 #ifndef NO_GINAC_NAMESPACE
306 } // namespace GiNaC
307 #endif // ndef NO_GINAC_NAMESPACE