]> www.ginac.de Git - ginac.git/blob - ginac/lorentzidx.cpp
6a367be65c1deec6068f2e08546de67dbec7efbf
[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 basic * lorentzidx::duplicate() const
175 {
176         debugmsg("lorentzidx duplicate",LOGLEVEL_DUPLICATE);
177         return new lorentzidx(*this);
178 }
179
180 void lorentzidx::printraw(std::ostream & os) const
181 {
182         debugmsg("lorentzidx printraw",LOGLEVEL_PRINT);
183
184         os << "lorentzidx(";
185
186         if (symbolic) {
187                 os << "symbolic,name=" << name;
188         } else {
189                 os << "non symbolic,value=" << value;
190         }
191
192         if (covariant) {
193                 os << ",covariant";
194         } else {
195                 os << ",contravariant";
196         }
197
198         if (orthogonal_only) {
199                 os << ",only orthogonal components at " << dim_parallel_space
200                    << " parallel dimensions";
201         } else {
202                 os << ",parallel and orthogonal components";
203         }
204
205         os << ",serial=" << serial;
206         os << ",hash=" << hashvalue << ",flags=" << flags;
207         os << ")";
208 }
209
210 void lorentzidx::printtree(std::ostream & os, unsigned indent) const
211 {
212         debugmsg("lorentzidx printtree",LOGLEVEL_PRINT);
213
214         os << std::string(indent,' ') << "lorentzidx: ";
215
216         if (symbolic) {
217                 os << "symbolic,name=" << name;
218         } else {
219                 os << "non symbolic,value=" << value;
220         }
221
222         if (covariant) {
223                 os << ",covariant";
224         } else {
225                 os << ",contravariant";
226         }
227
228         if (orthogonal_only) {
229                 os << ",only orthogonal components at " << dim_parallel_space
230                    << " parallel dimensions";
231         } else {
232                 os << ",parallel and orthogonal components";
233         }
234
235         os << ", serial=" << serial
236            << ", hash=" << hashvalue
237            << " (0x" << std::hex << hashvalue << std::dec << ")"
238            << ", flags=" << flags << std::endl;
239 }
240
241 void lorentzidx::print(std::ostream & os, unsigned upper_precedence) const
242 {
243         debugmsg("lorentzidx print",LOGLEVEL_PRINT);
244
245         if (covariant) {
246                 os << "_";
247         } else {
248                 os << "~";
249         }
250         if (symbolic) {
251                 os << name;
252         } else {
253                 os << value;
254         }
255 }
256
257 bool lorentzidx::info(unsigned inf) const
258 {
259         if (inf==info_flags::lorentzidx) return true;
260         return inherited::info(inf);
261 }
262
263 //////////
264 // non-virtual functions in this class
265 //////////
266
267 // public
268
269 /** Create anonymous contravariant copy of a symbolic Lorentz index. */
270 lorentzidx lorentzidx::create_anonymous_representative(void) const
271 {
272         GINAC_ASSERT(is_symbolic());
273         lorentzidx i_copy(*this);
274         i_copy.serial=0;
275         i_copy.name="anonymous_representative";
276         i_copy.covariant=false;
277         i_copy.clearflag(status_flags::dynallocated |
278                          status_flags::hash_calculated);
279         return i_copy;
280 }
281
282 //////////
283 // global functions
284 //////////
285
286 /** Return the global symbol that represents the dimension D of spacetime. */
287 ex Dim(void)
288 {
289         static symbol *d = new symbol("dim");
290         return *d;
291 }
292
293 #ifndef NO_NAMESPACE_GINAC
294 } // namespace GiNaC
295 #endif // ndef NO_NAMESPACE_GINAC