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