]> www.ginac.de Git - ginac.git/blob - ginac/symbol.cpp
Fixed initialization order bug (references to flyweights removed!) [C.Dams].
[ginac.git] / ginac / symbol.cpp
1 /** @file symbol.cpp
2  *
3  *  Implementation of GiNaC's symbolic objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2005 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(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(autoname_prefix() + ToString(serial)), TeX_name(name), ret_type(return_types::commutative), ret_type_tinfo(TINFO_symbol), 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(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(default_TeX_name()), ret_type(return_types::commutative), ret_type_tinfo(TINFO_symbol), domain(domain)
70 {
71         setflag(status_flags::evaluated | status_flags::expanded);
72 }
73
74 symbol::symbol(const std::string & initname, const std::string & texname, unsigned domain)
75  : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(texname), ret_type(return_types::commutative), ret_type_tinfo(TINFO_symbol), domain(domain)
76 {
77         setflag(status_flags::evaluated | status_flags::expanded);
78 }
79
80 symbol::symbol(const std::string & initname, unsigned rt, unsigned rtt, unsigned domain)
81  : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(default_TeX_name()), ret_type(rt), ret_type_tinfo(rtt), 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, unsigned rtt, unsigned domain)
87  : inherited(TINFO_symbol), 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, unsigned rtt, unsigned domain)
101  : symbol(initname, rt, rtt, domain) { }
102
103 realsymbol::realsymbol(const std::string & initname, const std::string & texname, unsigned rt, unsigned 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         if (!n.find_unsigned("return_type_tinfo", ret_type_tinfo))
123                 ret_type_tinfo = TINFO_symbol;
124         setflag(status_flags::evaluated | status_flags::expanded);
125 }
126
127 /** Unarchive the object. */
128 ex symbol::unarchive(const archive_node &n, lst &sym_lst)
129 {
130         ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
131
132         // If symbol is in sym_lst, return the existing symbol
133         for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
134                 if (is_a<symbol>(*it) && (ex_to<symbol>(*it).name == ex_to<symbol>(s).name))
135                         return *it;
136         }
137
138         // Otherwise add new symbol to list and return it
139         sym_lst.append(s);
140         return s;
141 }
142
143 /** Archive the object. */
144 void symbol::archive(archive_node &n) const
145 {
146         inherited::archive(n);
147         n.add_string("name", name);
148         if (TeX_name != default_TeX_name())
149                 n.add_string("TeX_name", TeX_name);
150         if (domain != domain::complex)
151                 n.add_unsigned("domain", domain);
152         if (ret_type != return_types::commutative)
153                 n.add_unsigned("return_type", ret_type);
154         if (ret_type_tinfo != TINFO_symbol)
155                 n.add_unsigned("return_type_tinfo", ret_type_tinfo);
156 }
157
158 //////////
159 // functions overriding virtual functions from base classes
160 //////////
161
162 // public
163
164 void symbol::do_print(const print_context & c, unsigned level) const
165 {
166         c.s << name;
167 }
168
169 void symbol::do_print_latex(const print_latex & c, unsigned level) const
170 {
171         c.s << TeX_name;
172 }
173
174 void symbol::do_print_tree(const print_tree & c, unsigned level) const
175 {
176         c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
177             << ", serial=" << serial
178             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
179             << ", domain=" << domain
180             << std::endl;
181 }
182
183 void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
184 {
185         c.s << class_name() << "('" << name;
186         if (TeX_name != default_TeX_name())
187                 c.s << "','" << TeX_name;
188         c.s << "')";
189 }
190
191 bool symbol::info(unsigned inf) const
192 {
193         if (inf == info_flags::symbol)
194                 return true;
195         if (inf == info_flags::polynomial ||
196             inf == info_flags::integer_polynomial ||
197             inf == info_flags::cinteger_polynomial ||
198             inf == info_flags::rational_polynomial ||
199             inf == info_flags::crational_polynomial ||
200             inf == info_flags::rational_function)
201                 return true;
202         if (inf == info_flags::real)
203                 return domain == domain::real;
204         else
205                 return inherited::info(inf);
206 }
207
208 ex symbol::eval(int level) const
209 {
210         if (level == -max_recursion_level)
211                 throw(std::runtime_error("max recursion level reached"));
212         
213         if (asexinfop->is_assigned) {
214                 setflag(status_flags::evaluated);
215                 if (level==1)
216                         return (asexinfop->assigned_expression);
217                 else
218                         return (asexinfop->assigned_expression).eval(level);
219         } else {
220                 return this->hold();
221         }
222 }
223
224 ex symbol::conjugate() const
225 {
226         if (this->domain == domain::complex) {
227                 return GiNaC::conjugate_function(*this).hold();
228         } else {
229                 return *this;
230         }
231 }
232
233 // protected
234
235 /** Implementation of ex::diff() for single differentiation of a symbol.
236  *  It returns 1 or 0.
237  *
238  *  @see ex::diff */
239 ex symbol::derivative(const symbol & s) const
240 {
241         if (compare_same_type(s))
242                 return _ex0;
243         else
244                 return _ex1;
245 }
246
247 int symbol::compare_same_type(const basic & other) const
248 {
249         GINAC_ASSERT(is_a<symbol>(other));
250         const symbol *o = static_cast<const symbol *>(&other);
251         if (serial==o->serial) return 0;
252         return serial < o->serial ? -1 : 1;
253 }
254
255 bool symbol::is_equal_same_type(const basic & other) const
256 {
257         GINAC_ASSERT(is_a<symbol>(other));
258         const symbol *o = static_cast<const symbol *>(&other);
259         return serial==o->serial;
260 }
261
262 unsigned symbol::calchash() const
263 {
264         hashvalue = golden_ratio_hash(tinfo() ^ serial);
265         setflag(status_flags::hash_calculated);
266         return hashvalue;
267 }
268
269 //////////
270 // virtual functions which can be overridden by derived classes
271 //////////
272
273 // none
274
275 //////////
276 // non-virtual functions in this class
277 //////////
278
279 // public
280
281 void symbol::assign(const ex & value)
282 {
283         asexinfop->is_assigned = true;
284         asexinfop->assigned_expression = value;
285         clearflag(status_flags::evaluated | status_flags::expanded);
286 }
287
288 void symbol::unassign()
289 {
290         if (asexinfop->is_assigned) {
291                 asexinfop->is_assigned = false;
292                 asexinfop->assigned_expression = _ex0;
293         }
294         setflag(status_flags::evaluated | status_flags::expanded);
295 }
296
297 // private
298
299 /** Symbols not constructed with a string get one assigned using this
300  *  prefix and a number. */
301 std::string & symbol::autoname_prefix()
302 {
303         static std::string *s = new std::string("symbol");
304         return *s;
305 }
306
307 /** Return default TeX name for symbol. This recognizes some greek letters. */
308 std::string symbol::default_TeX_name() const
309 {
310         if (name=="alpha"        || name=="beta"         || name=="gamma"
311          || name=="delta"        || name=="epsilon"      || name=="varepsilon"
312          || name=="zeta"         || name=="eta"          || name=="theta"
313          || name=="vartheta"     || name=="iota"         || name=="kappa"
314          || name=="lambda"       || name=="mu"           || name=="nu"
315          || name=="xi"           || name=="omicron"      || name=="pi"
316          || name=="varpi"        || name=="rho"          || name=="varrho"
317          || name=="sigma"        || name=="varsigma"     || name=="tau"
318          || name=="upsilon"      || name=="phi"          || name=="varphi"
319          || name=="chi"          || name=="psi"          || name=="omega"
320          || name=="Gamma"        || name=="Delta"        || name=="Theta"
321          || name=="Lambda"       || name=="Xi"           || name=="Pi"
322          || name=="Sigma"        || name=="Upsilon"      || name=="Phi"
323          || name=="Psi"          || name=="Omega")
324                 return "\\" + name;
325         else
326                 return name;
327 }
328
329 //////////
330 // static member variables
331 //////////
332
333 // private
334
335 unsigned symbol::next_serial = 0;
336
337 //////////
338 // subclass assigned_ex_info
339 //////////
340
341 /** Default ctor.  Defaults to unassigned. */
342 symbol::assigned_ex_info::assigned_ex_info() throw() : is_assigned(false)
343 {
344 }
345
346 } // namespace GiNaC