]> www.ginac.de Git - ginac.git/blob - ginac/symbol.cpp
Allow for more general expressions for clifford metrics. Patch by Vladimir.
[ginac.git] / ginac / symbol.cpp
1 /** @file symbol.cpp
2  *
3  *  Implementation of GiNaC's symbolic objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #include <string>
24 #include <stdexcept>
25
26 #include "symbol.h"
27 #include "lst.h"
28 #include "archive.h"
29 #include "tostring.h"
30 #include "utils.h"
31 #include "inifcns.h"
32
33 namespace GiNaC {
34
35 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(symbol, basic,
36   print_func<print_context>(&symbol::do_print).
37   print_func<print_latex>(&symbol::do_print_latex).
38   print_func<print_tree>(&symbol::do_print_tree).
39   print_func<print_python_repr>(&symbol::do_print_python_repr))
40
41 //////////
42 // default constructor
43 //////////
44
45 // symbol
46
47 symbol::symbol()
48  : inherited(&symbol::tinfo_static), asexinfop(new assigned_ex_info), serial(next_serial++), name(autoname_prefix() + ToString(serial)), TeX_name(name), ret_type(return_types::commutative), ret_type_tinfo(&symbol::tinfo_static), domain(domain::complex)
49 {
50         setflag(status_flags::evaluated | status_flags::expanded);
51 }
52
53 // realsymbol
54
55 realsymbol::realsymbol()
56 {
57         domain = domain::real;
58 }
59
60 //////////
61 // other constructors
62 //////////
63
64 // public
65
66 // symbol
67
68 symbol::symbol(const std::string & initname, unsigned domain)
69  : inherited(&symbol::tinfo_static), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(default_TeX_name()), ret_type(return_types::commutative), ret_type_tinfo(&symbol::tinfo_static), domain(domain)
70 {
71         setflag(status_flags::evaluated | status_flags::expanded);
72 }
73
74 symbol::symbol(const std::string & initname, unsigned rt, tinfo_t rtt, unsigned domain)
75  : inherited(&symbol::tinfo_static), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(default_TeX_name()), ret_type(rt), ret_type_tinfo(rtt), domain(domain)
76 {
77         setflag(status_flags::evaluated | status_flags::expanded);
78 }
79
80 symbol::symbol(const std::string & initname, const std::string & texname, unsigned domain)
81  : inherited(&symbol::tinfo_static), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(texname), ret_type(return_types::commutative), ret_type_tinfo(&symbol::tinfo_static), domain(domain)
82 {
83         setflag(status_flags::evaluated | status_flags::expanded);
84 }
85
86 symbol::symbol(const std::string & initname, const std::string & texname, unsigned rt, tinfo_t rtt, unsigned domain)
87  : inherited(&symbol::tinfo_static), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(texname), ret_type(rt), ret_type_tinfo(rtt), domain(domain)
88 {
89         setflag(status_flags::evaluated | status_flags::expanded);
90 }
91
92 // realsymbol
93         
94 realsymbol::realsymbol(const std::string & initname, unsigned domain)
95  : symbol(initname, domain) { }
96
97 realsymbol::realsymbol(const std::string & initname, const std::string & texname, unsigned domain)
98  : symbol(initname, texname, domain) { }
99
100 realsymbol::realsymbol(const std::string & initname, unsigned rt, tinfo_t rtt, unsigned domain)
101  : symbol(initname, rt, rtt, domain) { }
102
103 realsymbol::realsymbol(const std::string & initname, const std::string & texname, unsigned rt, tinfo_t rtt, unsigned domain)
104  : symbol(initname, texname, rt, rtt, domain) { }
105
106 //////////
107 // archiving
108 //////////
109
110 /** Construct object from archive_node. */
111 symbol::symbol(const archive_node &n, lst &sym_lst)
112  : inherited(n, sym_lst), asexinfop(new assigned_ex_info), serial(next_serial++)
113 {
114         if (!n.find_string("name", name))
115                 name = autoname_prefix() + ToString(serial);
116         if (!n.find_string("TeXname", TeX_name))
117                 TeX_name = default_TeX_name();
118         if (!n.find_unsigned("domain", domain))
119                 domain = domain::complex;
120         if (!n.find_unsigned("return_type", ret_type))
121                 ret_type = return_types::commutative;
122         setflag(status_flags::evaluated | status_flags::expanded);
123 }
124
125 /** Unarchive the object. */
126 ex symbol::unarchive(const archive_node &n, lst &sym_lst)
127 {
128         ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
129
130         // If symbol is in sym_lst, return the existing symbol
131         for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
132                 if (is_a<symbol>(*it) && (ex_to<symbol>(*it).name == ex_to<symbol>(s).name))
133                         return *it;
134         }
135
136         // Otherwise add new symbol to list and return it
137         sym_lst.append(s);
138         return s;
139 }
140
141 /** Archive the object. */
142 void symbol::archive(archive_node &n) const
143 {
144         inherited::archive(n);
145         n.add_string("name", name);
146         if (TeX_name != default_TeX_name())
147                 n.add_string("TeX_name", TeX_name);
148         if (domain != domain::complex)
149                 n.add_unsigned("domain", domain);
150         if (ret_type != return_types::commutative)
151                 n.add_unsigned("return_type", ret_type);
152 }
153
154 //////////
155 // functions overriding virtual functions from base classes
156 //////////
157
158 // public
159
160 void symbol::do_print(const print_context & c, unsigned level) const
161 {
162         c.s << name;
163 }
164
165 void symbol::do_print_latex(const print_latex & c, unsigned level) const
166 {
167         c.s << TeX_name;
168 }
169
170 void symbol::do_print_tree(const print_tree & c, unsigned level) const
171 {
172         c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
173             << ", serial=" << serial
174             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
175             << ", domain=" << domain
176             << std::endl;
177 }
178
179 void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
180 {
181         c.s << class_name() << "('" << name;
182         if (TeX_name != default_TeX_name())
183                 c.s << "','" << TeX_name;
184         c.s << "')";
185 }
186
187 bool symbol::info(unsigned inf) const
188 {
189         if (inf == info_flags::symbol)
190                 return true;
191         if (inf == info_flags::polynomial ||
192             inf == info_flags::integer_polynomial ||
193             inf == info_flags::cinteger_polynomial ||
194             inf == info_flags::rational_polynomial ||
195             inf == info_flags::crational_polynomial ||
196             inf == info_flags::rational_function)
197                 return true;
198         if (inf == info_flags::real)
199                 return domain == domain::real;
200         else
201                 return inherited::info(inf);
202 }
203
204 ex symbol::eval(int level) const
205 {
206         if (level == -max_recursion_level)
207                 throw(std::runtime_error("max recursion level reached"));
208         
209         if (asexinfop->is_assigned) {
210                 setflag(status_flags::evaluated);
211                 if (level==1)
212                         return (asexinfop->assigned_expression);
213                 else
214                         return (asexinfop->assigned_expression).eval(level);
215         } else {
216                 return this->hold();
217         }
218 }
219
220 ex symbol::conjugate() const
221 {
222         if (this->domain == domain::complex) {
223                 return conjugate_function(*this).hold();
224         } else {
225                 return *this;
226         }
227 }
228
229 ex symbol::real_part() const
230 {
231         if (domain == domain::real)
232                 return *this;
233         return real_part_function(*this).hold();
234 }
235
236 ex symbol::imag_part() const
237 {
238         if (domain == domain::real)
239                 return 0;
240         return imag_part_function(*this).hold();
241 }
242
243 bool symbol::is_polynomial(const ex & var) const
244 {
245         return true;
246 }
247
248 // protected
249
250 /** Implementation of ex::diff() for single differentiation of a symbol.
251  *  It returns 1 or 0.
252  *
253  *  @see ex::diff */
254 ex symbol::derivative(const symbol & s) const
255 {
256         if (compare_same_type(s))
257                 return _ex0;
258         else
259                 return _ex1;
260 }
261
262 int symbol::compare_same_type(const basic & other) const
263 {
264         GINAC_ASSERT(is_a<symbol>(other));
265         const symbol *o = static_cast<const symbol *>(&other);
266         if (serial==o->serial) return 0;
267         return serial < o->serial ? -1 : 1;
268 }
269
270 bool symbol::is_equal_same_type(const basic & other) const
271 {
272         GINAC_ASSERT(is_a<symbol>(other));
273         const symbol *o = static_cast<const symbol *>(&other);
274         return serial==o->serial;
275 }
276
277 unsigned symbol::calchash() const
278 {
279         hashvalue = golden_ratio_hash((p_int)tinfo() ^ serial);
280         setflag(status_flags::hash_calculated);
281         return hashvalue;
282 }
283
284 //////////
285 // virtual functions which can be overridden by derived classes
286 //////////
287
288 // none
289
290 //////////
291 // non-virtual functions in this class
292 //////////
293
294 // public
295
296 void symbol::assign(const ex & value)
297 {
298         asexinfop->is_assigned = true;
299         asexinfop->assigned_expression = value;
300         clearflag(status_flags::evaluated | status_flags::expanded);
301 }
302
303 void symbol::unassign()
304 {
305         if (asexinfop->is_assigned) {
306                 asexinfop->is_assigned = false;
307                 asexinfop->assigned_expression = _ex0;
308         }
309         setflag(status_flags::evaluated | status_flags::expanded);
310 }
311
312 // private
313
314 /** Symbols not constructed with a string get one assigned using this
315  *  prefix and a number. */
316 std::string & symbol::autoname_prefix()
317 {
318         static std::string *s = new std::string("symbol");
319         return *s;
320 }
321
322 /** Return default TeX name for symbol. This recognizes some greek letters. */
323 std::string symbol::default_TeX_name() const
324 {
325         if (name=="alpha"        || name=="beta"         || name=="gamma"
326          || name=="delta"        || name=="epsilon"      || name=="varepsilon"
327          || name=="zeta"         || name=="eta"          || name=="theta"
328          || name=="vartheta"     || name=="iota"         || name=="kappa"
329          || name=="lambda"       || name=="mu"           || name=="nu"
330          || name=="xi"           || name=="omicron"      || name=="pi"
331          || name=="varpi"        || name=="rho"          || name=="varrho"
332          || name=="sigma"        || name=="varsigma"     || name=="tau"
333          || name=="upsilon"      || name=="phi"          || name=="varphi"
334          || name=="chi"          || name=="psi"          || name=="omega"
335          || name=="Gamma"        || name=="Delta"        || name=="Theta"
336          || name=="Lambda"       || name=="Xi"           || name=="Pi"
337          || name=="Sigma"        || name=="Upsilon"      || name=="Phi"
338          || name=="Psi"          || name=="Omega")
339                 return "\\" + name;
340         else
341                 return name;
342 }
343
344 //////////
345 // static member variables
346 //////////
347
348 // private
349
350 unsigned symbol::next_serial = 0;
351
352 //////////
353 // subclass assigned_ex_info
354 //////////
355
356 /** Default ctor.  Defaults to unassigned. */
357 symbol::assigned_ex_info::assigned_ex_info() throw() : is_assigned(false)
358 {
359 }
360
361 } // namespace GiNaC