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