]> www.ginac.de Git - ginac.git/blob - ginac/lorentzidx.cpp
- implemented #b, #o, #x and #nR prefixes for binary, octal, hexadecimal, and
[ginac.git] / ginac / lorentzidx.cpp
1 /** @file lorentzidx.cpp
2  *
3  *  Implementation of GiNaC's lorentz indices. */
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 <stdexcept>
24
25 #include "lorentzidx.h"
26 #include "lst.h"
27 #include "archive.h"
28 #include "utils.h"
29 #include "debugmsg.h"
30
31 #ifndef NO_NAMESPACE_GINAC
32 namespace GiNaC {
33 #endif // ndef NO_NAMESPACE_GINAC
34
35 GINAC_IMPLEMENT_REGISTERED_CLASS(lorentzidx, idx)
36
37 //////////
38 // default constructor, destructor, copy constructor assignment operator and helpers
39 //////////
40
41 // public
42
43 lorentzidx::lorentzidx() : orthogonal_only(false), dim_parallel_space(0)
44 {
45     debugmsg("lorentzidx default constructor",LOGLEVEL_CONSTRUCT);
46     // serial is incremented in idx::idx()
47     name="mu"+ToString(serial);
48     tinfo_key=TINFO_lorentzidx;
49 }
50
51 lorentzidx::~lorentzidx() 
52 {
53     debugmsg("lorentzidx destructor",LOGLEVEL_DESTRUCT);
54     destroy(0);
55 }
56
57 lorentzidx::lorentzidx(const lorentzidx & other)
58 {
59     debugmsg("lorentzidx copy constructor",LOGLEVEL_CONSTRUCT);
60     copy(other);
61 }
62
63 const lorentzidx & lorentzidx::operator=(const lorentzidx & other)
64 {
65     debugmsg("lorentzidx operator=",LOGLEVEL_ASSIGNMENT);
66     if (this != &other) {
67         destroy(1);
68         copy(other);
69     }
70     return *this;
71 }
72
73 // protected
74
75 void lorentzidx::copy(const lorentzidx & other)
76 {
77     inherited::copy(other);
78     orthogonal_only=other.orthogonal_only;
79     dim_parallel_space=other.dim_parallel_space;
80 }
81
82 void lorentzidx::destroy(bool call_parent)
83 {
84     if (call_parent) inherited::destroy(call_parent);
85 }
86
87 //////////
88 // other constructors
89 //////////
90
91 // public
92
93 lorentzidx::lorentzidx(bool cov, bool oonly, unsigned dimp) :
94     idx(cov), orthogonal_only(oonly), dim_parallel_space(dimp)
95 {
96     debugmsg("lorentzidx constructor from bool",LOGLEVEL_CONSTRUCT);
97     // serial is incremented in idx::idx(bool)
98     if (oonly) {
99         name="muorth"+ToString(serial);
100     } else {
101         name="mu"+ToString(serial);
102     }
103     tinfo_key=TINFO_lorentzidx;
104 }
105
106 lorentzidx::lorentzidx(const string & n, bool cov, bool oonly, unsigned dimp)
107     : idx(n,cov), orthogonal_only(oonly), dim_parallel_space(dimp)
108 {
109     debugmsg("lorentzidx constructor from string,bool,bool,unsigned",
110              LOGLEVEL_CONSTRUCT);
111     tinfo_key=TINFO_lorentzidx;
112 }
113
114 lorentzidx::lorentzidx(const char * n, bool cov, bool oonly, unsigned dimp)
115     : idx(n,cov), orthogonal_only(oonly), dim_parallel_space(dimp)
116 {
117     debugmsg("lorentzidx constructor from char*,bool,bool,unsigned",
118              LOGLEVEL_CONSTRUCT);
119     tinfo_key=TINFO_lorentzidx;
120 }
121
122 lorentzidx::lorentzidx(unsigned v, bool cov) : idx(v,cov),
123     orthogonal_only(false), dim_parallel_space(0)
124 {
125     debugmsg("lorentzidx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
126     tinfo_key=TINFO_lorentzidx;
127 }
128
129 //////////
130 // archiving
131 //////////
132
133 /** Construct object from archive_node. */
134 lorentzidx::lorentzidx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
135 {
136     debugmsg("lorentzidx constructor from archive_node", LOGLEVEL_CONSTRUCT);
137     n.find_bool("orthogonal_only", orthogonal_only);
138         if (orthogonal_only)
139                 n.find_unsigned("pdim", dim_parallel_space);
140 }
141
142 /** Unarchive the object. */
143 ex lorentzidx::unarchive(const archive_node &n, const lst &sym_lst)
144 {
145     ex s = (new lorentzidx(n, sym_lst))->setflag(status_flags::dynallocated);
146
147     if (ex_to_lorentzidx(s).symbolic) {
148         // If lorentzidx is in sym_lst, return the existing lorentzidx
149         for (unsigned i=0; i<sym_lst.nops(); i++) {
150             if (is_ex_of_type(sym_lst.op(i), lorentzidx) && (ex_to_lorentzidx(sym_lst.op(i)).name == ex_to_lorentzidx(s).name))
151                 return sym_lst.op(i);
152         }
153     }
154     return s;
155 }
156
157 /** Archive the object. */
158 void lorentzidx::archive(archive_node &n) const
159 {
160     inherited::archive(n);
161         n.add_bool("orthogonal_only", orthogonal_only);
162         if (orthogonal_only)
163                 n.add_unsigned("pdim", dim_parallel_space);
164 }
165
166 //////////
167 // functions overriding virtual functions from bases classes
168 //////////
169
170 // public
171
172 basic * lorentzidx::duplicate() const
173 {
174     debugmsg("lorentzidx duplicate",LOGLEVEL_DUPLICATE);
175     return new lorentzidx(*this);
176 }
177
178 void lorentzidx::printraw(ostream & os) const
179 {
180     debugmsg("lorentzidx printraw",LOGLEVEL_PRINT);
181
182     os << "lorentzidx(";
183
184     if (symbolic) {
185         os << "symbolic,name=" << name;
186     } else {
187         os << "non symbolic,value=" << value;
188     }
189
190     if (covariant) {
191         os << ",covariant";
192     } else {
193         os << ",contravariant";
194     }
195
196     if (orthogonal_only) {
197         os << ",only orthogonal components at " << dim_parallel_space
198            << " parallel dimensions";
199     } else {
200         os << ",parallel and orthogonal components";
201     }
202
203     os << ",serial=" << serial;
204     os << ",hash=" << hashvalue << ",flags=" << flags;
205     os << ")";
206 }
207
208 void lorentzidx::printtree(ostream & os, unsigned indent) const
209 {
210     debugmsg("lorentzidx printtree",LOGLEVEL_PRINT);
211
212     os << string(indent,' ') << "lorentzidx: ";
213
214     if (symbolic) {
215         os << "symbolic,name=" << name;
216     } else {
217         os << "non symbolic,value=" << value;
218     }
219
220     if (covariant) {
221         os << ",covariant";
222     } else {
223         os << ",contravariant";
224     }
225
226     if (orthogonal_only) {
227         os << ",only orthogonal components at " << dim_parallel_space
228            << " parallel dimensions";
229     } else {
230         os << ",parallel and orthogonal components";
231     }
232
233     os << ", serial=" << serial
234        << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
235        << ", flags=" << flags << endl;
236 }
237
238 void lorentzidx::print(ostream & os, unsigned upper_precedence) const
239 {
240     debugmsg("lorentzidx print",LOGLEVEL_PRINT);
241
242     if (covariant) {
243         os << "_";
244     } else {
245         os << "~";
246     }
247     if (symbolic) {
248         os << name;
249     } else {
250         os << value;
251     }
252 }
253
254 bool lorentzidx::info(unsigned inf) const
255 {
256     if (inf==info_flags::lorentzidx) return true;
257     return inherited::info(inf);
258 }
259
260 //////////
261 // new virtual functions which can be overridden by derived classes
262 //////////
263
264 // none
265
266 //////////
267 // non-virtual functions in this class
268 //////////
269
270 // public
271
272 lorentzidx lorentzidx::create_anonymous_representative(void) const
273 {
274     GINAC_ASSERT(is_symbolic());
275     lorentzidx i_copy(*this);
276     i_copy.serial=0;
277     i_copy.name="anonymous_representative";
278     i_copy.covariant=false;
279     i_copy.clearflag(status_flags::dynallocated|
280                      status_flags::hash_calculated);
281     return i_copy;
282 }
283
284 //////////
285 // static member variables
286 //////////
287
288 // none
289
290 //////////
291 // global constants
292 //////////
293
294 const lorentzidx some_lorentzidx;
295 const type_info & typeid_lorentzidx=typeid(some_lorentzidx);
296
297 #ifndef NO_NAMESPACE_GINAC
298 } // namespace GiNaC
299 #endif // ndef NO_NAMESPACE_GINAC