]> www.ginac.de Git - ginac.git/blob - ginac/symbol.cpp
Add remark about complex conjugation.
[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) return true;
169         if (inf==info_flags::polynomial ||
170             inf==info_flags::integer_polynomial ||
171             inf==info_flags::cinteger_polynomial ||
172             inf==info_flags::rational_polynomial ||
173             inf==info_flags::crational_polynomial ||
174             inf==info_flags::rational_function)
175                 return true;
176         else
177                 return inherited::info(inf);
178 }
179
180 ex symbol::eval(int level) const
181 {
182         if (level == -max_recursion_level)
183                 throw(std::runtime_error("max recursion level reached"));
184         
185         if (asexinfop->is_assigned) {
186                 setflag(status_flags::evaluated);
187                 if (level==1)
188                         return (asexinfop->assigned_expression);
189                 else
190                         return (asexinfop->assigned_expression).eval(level);
191         } else {
192                 return this->hold();
193         }
194 }
195
196 ex symbol::conjugate() const
197 {
198         if (this->domain == symbol_options::complex) {
199                 return GiNaC::conjugate(*this).hold();
200         } else {
201                 return *this;
202         }
203 }
204
205 // protected
206
207 /** Implementation of ex::diff() for single differentiation of a symbol.
208  *  It returns 1 or 0.
209  *
210  *  @see ex::diff */
211 ex symbol::derivative(const symbol & s) const
212 {
213         if (compare_same_type(s))
214                 return _ex0;
215         else
216                 return _ex1;
217 }
218
219 int symbol::compare_same_type(const basic & other) const
220 {
221         GINAC_ASSERT(is_a<symbol>(other));
222         const symbol *o = static_cast<const symbol *>(&other);
223         if (serial==o->serial) return 0;
224         return serial < o->serial ? -1 : 1;
225 }
226
227 bool symbol::is_equal_same_type(const basic & other) const
228 {
229         GINAC_ASSERT(is_a<symbol>(other));
230         const symbol *o = static_cast<const symbol *>(&other);
231         return serial==o->serial;
232 }
233
234 unsigned symbol::calchash() const
235 {
236         hashvalue = golden_ratio_hash(tinfo() ^ serial);
237         setflag(status_flags::hash_calculated);
238         return hashvalue;
239 }
240
241 //////////
242 // virtual functions which can be overridden by derived classes
243 //////////
244
245 // none
246
247 //////////
248 // non-virtual functions in this class
249 //////////
250
251 // public
252
253 void symbol::assign(const ex & value)
254 {
255         asexinfop->is_assigned = true;
256         asexinfop->assigned_expression = value;
257         clearflag(status_flags::evaluated | status_flags::expanded);
258 }
259
260 void symbol::unassign()
261 {
262         if (asexinfop->is_assigned) {
263                 asexinfop->is_assigned = false;
264                 asexinfop->assigned_expression = _ex0;
265         }
266         setflag(status_flags::evaluated | status_flags::expanded);
267 }
268
269 // private
270
271 /** Symbols not constructed with a string get one assigned using this
272  *  prefix and a number. */
273 std::string & symbol::autoname_prefix()
274 {
275         static std::string *s = new std::string("symbol");
276         return *s;
277 }
278
279 /** Return default TeX name for symbol. This recognizes some greek letters. */
280 std::string symbol::default_TeX_name() const
281 {
282         if (name=="alpha"        || name=="beta"         || name=="gamma"
283          || name=="delta"        || name=="epsilon"      || name=="varepsilon"
284          || name=="zeta"         || name=="eta"          || name=="theta"
285          || name=="vartheta"     || name=="iota"         || name=="kappa"
286          || name=="lambda"       || name=="mu"           || name=="nu"
287          || name=="xi"           || name=="omicron"      || name=="pi"
288          || name=="varpi"        || name=="rho"          || name=="varrho"
289          || name=="sigma"        || name=="varsigma"     || name=="tau"
290          || name=="upsilon"      || name=="phi"          || name=="varphi"
291          || name=="chi"          || name=="psi"          || name=="omega"
292          || name=="Gamma"        || name=="Delta"        || name=="Theta"
293          || name=="Lambda"       || name=="Xi"           || name=="Pi"
294          || name=="Sigma"        || name=="Upsilon"      || name=="Phi"
295          || name=="Psi"          || name=="Omega")
296                 return "\\" + name;
297         else
298                 return name;
299 }
300
301 //////////
302 // static member variables
303 //////////
304
305 // private
306
307 unsigned symbol::next_serial = 0;
308
309 //////////
310 // subclass assigned_ex_info
311 //////////
312
313 /** Default ctor.  Defaults to unassigned. */
314 symbol::assigned_ex_info::assigned_ex_info() throw() : is_assigned(false)
315 {
316 }
317
318 } // namespace GiNaC