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