]> www.ginac.de Git - ginac.git/blob - ginac/lorentzidx.cpp
* ginac/registrar.h: dtor is inlined now.
[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 namespace GiNaC {
33
34 GINAC_IMPLEMENT_REGISTERED_CLASS(lorentzidx, idx)
35
36 //////////
37 // default constructor, destructor, copy constructor assignment operator and helpers
38 //////////
39
40 // public
41
42 lorentzidx::lorentzidx() : orthogonal_only(false), dim_parallel_space(0)
43 {
44         debugmsg("lorentzidx default constructor",LOGLEVEL_CONSTRUCT);
45         // serial is incremented in idx::idx()
46         name = "mu" + ToString(serial);
47         tinfo_key = TINFO_lorentzidx;
48 }
49
50 // protected
51
52 void lorentzidx::copy(const lorentzidx & other)
53 {
54         inherited::copy(other);
55         orthogonal_only=other.orthogonal_only;
56         dim_parallel_space=other.dim_parallel_space;
57 }
58
59 void lorentzidx::destroy(bool call_parent)
60 {
61         if (call_parent) inherited::destroy(call_parent);
62 }
63
64 //////////
65 // other constructors
66 //////////
67
68 // public
69
70 /** Construct symbolic Lorentz index, using an automatically generated unique name.
71  *
72  *  @param cov Index is covariant (contravariant otherwise)
73  *  @param oonly Index only lives in orthogonal space
74  *  @param dimp Dimension of parallel space
75  *  @return newly constructed index */
76 lorentzidx::lorentzidx(bool cov, bool oonly, unsigned dimp)
77   : idx(cov), orthogonal_only(oonly), dim_parallel_space(dimp)
78 {
79         debugmsg("lorentzidx constructor from bool,bool,unsigned",LOGLEVEL_CONSTRUCT);
80         // serial is incremented in idx::idx(bool)
81         if (oonly) {
82                 name="muorth"+ToString(serial);
83         } else {
84                 name="mu"+ToString(serial);
85         }
86         tinfo_key=TINFO_lorentzidx;
87 }
88
89 /** Construct symbolic Lorentz index with specified name.
90  *
91  *  @param n Symbolic index name
92  *  @param cov Index is covariant (contravariant otherwise)
93  *  @param oonly Index only lives in orthogonal space
94  *  @param dimp Dimension of parallel space
95  *  @return newly constructed index */
96 lorentzidx::lorentzidx(const std::string & n, bool cov, bool oonly, unsigned dimp)
97   : idx(n,cov), orthogonal_only(oonly), dim_parallel_space(dimp)
98 {
99         debugmsg("lorentzidx constructor from string,bool,bool,unsigned",
100                          LOGLEVEL_CONSTRUCT);
101         tinfo_key=TINFO_lorentzidx;
102 }
103
104 /** Construct symbolic Lorentz index with specified name.
105  *
106  *  @param n Symbolic index name
107  *  @param cov Index is covariant (contravariant otherwise)
108  *  @param oonly Index only lives in orthogonal space
109  *  @param dimp Dimension of parallel space
110  *  @return newly constructed index */
111 lorentzidx::lorentzidx(const char * n, bool cov, bool oonly, unsigned dimp)
112   : idx(n,cov), orthogonal_only(oonly), dim_parallel_space(dimp)
113 {
114         debugmsg("lorentzidx constructor from char*,bool,bool,unsigned",
115                          LOGLEVEL_CONSTRUCT);
116         tinfo_key=TINFO_lorentzidx;
117 }
118
119 /** Construct numeric Lorentz index with specified value.
120  *
121  *  @param v Numeric index value
122  *  @param cov Index is covariant (contravariant otherwise)
123  *  @return newly constructed index */
124 lorentzidx::lorentzidx(unsigned v, bool cov)
125   : idx(v,cov), orthogonal_only(false), dim_parallel_space(0)
126 {
127         debugmsg("lorentzidx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
128         tinfo_key=TINFO_lorentzidx;
129 }
130
131 //////////
132 // archiving
133 //////////
134
135 /** Construct object from archive_node. */
136 lorentzidx::lorentzidx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
137 {
138         debugmsg("lorentzidx constructor from archive_node", LOGLEVEL_CONSTRUCT);
139         n.find_bool("orthogonal_only", orthogonal_only);
140         n.find_unsigned("pdim", dim_parallel_space);
141 }
142
143 /** Unarchive the object. */
144 ex lorentzidx::unarchive(const archive_node &n, const lst &sym_lst)
145 {
146         ex s = (new lorentzidx(n, sym_lst))->setflag(status_flags::dynallocated);
147
148         if (ex_to_lorentzidx(s).symbolic) {
149                 // If lorentzidx is in sym_lst, return the existing lorentzidx
150                 for (unsigned i=0; i<sym_lst.nops(); i++) {
151                         if (is_ex_of_type(sym_lst.op(i), lorentzidx) && (ex_to_lorentzidx(sym_lst.op(i)).name == ex_to_lorentzidx(s).name))
152                                 return sym_lst.op(i);
153                 }
154         }
155         return s;
156 }
157
158 /** Archive the object. */
159 void lorentzidx::archive(archive_node &n) const
160 {
161         inherited::archive(n);
162         n.add_bool("orthogonal_only", 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 void lorentzidx::printraw(std::ostream & os) const
173 {
174         debugmsg("lorentzidx printraw",LOGLEVEL_PRINT);
175
176         os << "lorentzidx(";
177
178         if (symbolic) {
179                 os << "symbolic,name=" << name;
180         } else {
181                 os << "non symbolic,value=" << value;
182         }
183
184         if (covariant) {
185                 os << ",covariant";
186         } else {
187                 os << ",contravariant";
188         }
189
190         if (orthogonal_only) {
191                 os << ",only orthogonal components at " << dim_parallel_space
192                    << " parallel dimensions";
193         } else {
194                 os << ",parallel and orthogonal components";
195         }
196
197         os << ",serial=" << serial;
198         os << ",hash=" << hashvalue << ",flags=" << flags;
199         os << ")";
200 }
201
202 void lorentzidx::printtree(std::ostream & os, unsigned indent) const
203 {
204         debugmsg("lorentzidx printtree",LOGLEVEL_PRINT);
205
206         os << std::string(indent,' ') << "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            << ", hash=" << hashvalue
229            << " (0x" << std::hex << hashvalue << std::dec << ")"
230            << ", flags=" << flags << std::endl;
231 }
232
233 void lorentzidx::print(std::ostream & os, unsigned upper_precedence) const
234 {
235         debugmsg("lorentzidx print",LOGLEVEL_PRINT);
236
237         if (covariant) {
238                 os << "_";
239         } else {
240                 os << "~";
241         }
242         if (symbolic) {
243                 os << name;
244         } else {
245                 os << value;
246         }
247 }
248
249 bool lorentzidx::info(unsigned inf) const
250 {
251         if (inf==info_flags::lorentzidx) return true;
252         return inherited::info(inf);
253 }
254
255 int lorentzidx::compare_same_type(const basic & other) const
256 {
257         GINAC_ASSERT(is_of_type(other, lorentzidx));
258         const lorentzidx &o = static_cast<const lorentzidx &>(other);
259
260         if (orthogonal_only != o.orthogonal_only)
261                 return orthogonal_only ? -1 : 1;
262         if (dim_parallel_space != o.dim_parallel_space)
263                 return dim_parallel_space < o.dim_parallel_space ? -1 : 1;
264         return inherited::compare_same_type(other);
265 }
266
267 //////////
268 // non-virtual functions in this class
269 //////////
270
271 // public
272
273 /** Create anonymous contravariant copy of a symbolic Lorentz index. */
274 lorentzidx lorentzidx::create_anonymous_representative(void) const
275 {
276         GINAC_ASSERT(is_symbolic());
277         lorentzidx i_copy(*this);
278         i_copy.serial=0;
279         i_copy.name="anonymous_representative";
280         i_copy.covariant=false;
281         i_copy.clearflag(status_flags::dynallocated |
282                          status_flags::hash_calculated);
283         return i_copy;
284 }
285
286 //////////
287 // global functions
288 //////////
289
290 /** Return the global symbol that represents the dimension D of spacetime. */
291 ex Dim(void)
292 {
293         static symbol *d = new symbol("dim");
294         return *d;
295 }
296
297 } // namespace GiNaC