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