]> www.ginac.de Git - ginac.git/blob - ginac/symbol.cpp
Reviving return_type_tinfo-system.
[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 GiNaC::conjugate_function(*this).hold();
224         } else {
225                 return *this;
226         }
227 }
228
229 // protected
230
231 /** Implementation of ex::diff() for single differentiation of a symbol.
232  *  It returns 1 or 0.
233  *
234  *  @see ex::diff */
235 ex symbol::derivative(const symbol & s) const
236 {
237         if (compare_same_type(s))
238                 return _ex0;
239         else
240                 return _ex1;
241 }
242
243 int symbol::compare_same_type(const basic & other) const
244 {
245         GINAC_ASSERT(is_a<symbol>(other));
246         const symbol *o = static_cast<const symbol *>(&other);
247         if (serial==o->serial) return 0;
248         return serial < o->serial ? -1 : 1;
249 }
250
251 bool symbol::is_equal_same_type(const basic & other) const
252 {
253         GINAC_ASSERT(is_a<symbol>(other));
254         const symbol *o = static_cast<const symbol *>(&other);
255         return serial==o->serial;
256 }
257
258 unsigned symbol::calchash() const
259 {
260         hashvalue = golden_ratio_hash((p_int)tinfo() ^ serial);
261         setflag(status_flags::hash_calculated);
262         return hashvalue;
263 }
264
265 //////////
266 // virtual functions which can be overridden by derived classes
267 //////////
268
269 // none
270
271 //////////
272 // non-virtual functions in this class
273 //////////
274
275 // public
276
277 void symbol::assign(const ex & value)
278 {
279         asexinfop->is_assigned = true;
280         asexinfop->assigned_expression = value;
281         clearflag(status_flags::evaluated | status_flags::expanded);
282 }
283
284 void symbol::unassign()
285 {
286         if (asexinfop->is_assigned) {
287                 asexinfop->is_assigned = false;
288                 asexinfop->assigned_expression = _ex0;
289         }
290         setflag(status_flags::evaluated | status_flags::expanded);
291 }
292
293 // private
294
295 /** Symbols not constructed with a string get one assigned using this
296  *  prefix and a number. */
297 std::string & symbol::autoname_prefix()
298 {
299         static std::string *s = new std::string("symbol");
300         return *s;
301 }
302
303 /** Return default TeX name for symbol. This recognizes some greek letters. */
304 std::string symbol::default_TeX_name() const
305 {
306         if (name=="alpha"        || name=="beta"         || name=="gamma"
307          || name=="delta"        || name=="epsilon"      || name=="varepsilon"
308          || name=="zeta"         || name=="eta"          || name=="theta"
309          || name=="vartheta"     || name=="iota"         || name=="kappa"
310          || name=="lambda"       || name=="mu"           || name=="nu"
311          || name=="xi"           || name=="omicron"      || name=="pi"
312          || name=="varpi"        || name=="rho"          || name=="varrho"
313          || name=="sigma"        || name=="varsigma"     || name=="tau"
314          || name=="upsilon"      || name=="phi"          || name=="varphi"
315          || name=="chi"          || name=="psi"          || name=="omega"
316          || name=="Gamma"        || name=="Delta"        || name=="Theta"
317          || name=="Lambda"       || name=="Xi"           || name=="Pi"
318          || name=="Sigma"        || name=="Upsilon"      || name=="Phi"
319          || name=="Psi"          || name=="Omega")
320                 return "\\" + name;
321         else
322                 return name;
323 }
324
325 //////////
326 // static member variables
327 //////////
328
329 // private
330
331 unsigned symbol::next_serial = 0;
332
333 //////////
334 // subclass assigned_ex_info
335 //////////
336
337 /** Default ctor.  Defaults to unassigned. */
338 symbol::assigned_ex_info::assigned_ex_info() throw() : is_assigned(false)
339 {
340 }
341
342 } // namespace GiNaC