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