]> www.ginac.de Git - ginac.git/blob - ginac/symbol.cpp
G_do_hoelder: fix the transformation of the imaginary part.
[ginac.git] / ginac / symbol.cpp
1 /** @file symbol.cpp
2  *
3  *  Implementation of GiNaC's symbolic objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2011 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 "symbol.h"
24 #include "lst.h"
25 #include "archive.h"
26 #include "tostring.h"
27 #include "utils.h"
28 #include "hash_seed.h"
29 #include "inifcns.h"
30
31 #include <map>
32 #include <stdexcept>
33 #include <string>
34
35 namespace GiNaC {
36
37 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(symbol, basic,
38   print_func<print_context>(&symbol::do_print).
39   print_func<print_latex>(&symbol::do_print_latex).
40   print_func<print_tree>(&symbol::do_print_tree).
41   print_func<print_python_repr>(&symbol::do_print_python_repr))
42
43 //////////
44 // default constructor
45 //////////
46
47 // symbol
48
49 symbol::symbol() : serial(next_serial++), name(""), TeX_name("")
50 {
51         setflag(status_flags::evaluated | status_flags::expanded);
52 }
53
54 // realsymbol
55
56 realsymbol::realsymbol() : symbol() { }
57
58 // possymbol
59
60 possymbol::possymbol() : realsymbol() { }
61
62 //////////
63 // other constructors
64 //////////
65
66 // public
67
68 // symbol
69
70 symbol::symbol(const std::string & initname) : serial(next_serial++),
71         name(initname), TeX_name("")
72 {
73         setflag(status_flags::evaluated | status_flags::expanded);
74 }
75
76 symbol::symbol(const std::string & initname, const std::string & texname) :
77         serial(next_serial++), name(initname), TeX_name(texname)
78 {
79         setflag(status_flags::evaluated | status_flags::expanded);
80 }
81
82 // realsymbol
83         
84 realsymbol::realsymbol(const std::string & initname) : symbol(initname) { }
85
86 realsymbol::realsymbol(const std::string & initname, const std::string & texname)
87         : symbol(initname, texname) { }
88
89 // possymbol
90         
91 possymbol::possymbol(const std::string & initname) : realsymbol(initname) { }
92
93 possymbol::possymbol(const std::string & initname, const std::string & texname) 
94         : realsymbol(initname, texname) { }
95
96 //////////
97 // archiving
98 //////////
99
100 /** Read object from archive_node. */
101 void symbol::read_archive(const archive_node &n, lst &sym_lst)
102 {
103         inherited::read_archive(n, sym_lst);
104         serial = next_serial++;
105         std::string tmp_name;
106         n.find_string("name", tmp_name);
107
108         // If symbol is in sym_lst, return the existing symbol
109         for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
110                 if (is_a<symbol>(*it) && (ex_to<symbol>(*it).name == tmp_name)) {
111                         *this = ex_to<symbol>(*it);
112                         // XXX: This method is responsible for reading realsymbol
113                         // and possymbol objects too. But
114                         // basic::operator=(const basic& other)
115                         // resets status_flags::evaluated if other and *this are
116                         // of different types. Usually this is a good idea, but
117                         // doing this for symbols is wrong (for one, nothing is
118                         // going to set status_flags::evaluated, evaluation will
119                         // loop forever). Therefore we need to restore flags.
120                         setflag(status_flags::evaluated | status_flags::expanded);
121                         return;
122                 }
123         }
124         name = tmp_name;
125         if (!n.find_string("TeXname", TeX_name))
126                 TeX_name = std::string("");
127         setflag(status_flags::evaluated | status_flags::expanded);
128
129         setflag(status_flags::dynallocated);
130         sym_lst.append(*this);
131 }
132
133 /** Archive the object. */
134 void symbol::archive(archive_node &n) const
135 {
136         inherited::archive(n);
137         // XXX: we should not archive anonymous symbols.
138         if (!name.empty())
139                 n.add_string("name", name);
140         if (!TeX_name.empty())
141                 n.add_string("TeX_name", TeX_name);
142 }
143
144 //////////
145 // functions overriding virtual functions from base classes
146 //////////
147
148 /** Return default TeX name for symbol. This recognizes some greek letters. */
149 static const std::string& get_default_TeX_name(const std::string& name);
150
151 // public
152
153 std::string symbol::get_name() const
154 {
155         if (name.empty()) {
156                 std::ostringstream s;
157                 s << "symbol" << serial;
158                 name = s.str();
159         }
160         return name;
161 }
162
163 // protected
164
165 void symbol::do_print(const print_context & c, unsigned level) const
166 {
167         c.s << get_name();
168 }
169
170 void symbol::do_print_latex(const print_latex & c, unsigned level) const
171 {
172         if (!TeX_name.empty())
173                 c.s << TeX_name;
174         else if (!name.empty())
175                 c.s << get_default_TeX_name(name);
176         else
177                 c.s << "symbol" << serial;
178 }
179
180 void symbol::do_print_tree(const print_tree & c, unsigned level) const
181 {
182         c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
183             << ", serial=" << serial
184             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
185             << ", domain=" << get_domain()
186             << std::endl;
187 }
188
189 void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
190 {
191         c.s << class_name() << "('";
192         if (!name.empty())
193                 c.s << name;
194         else
195                 c.s << "symbol" << serial;
196         if (!TeX_name.empty())
197                 c.s << "','" << TeX_name;
198         c.s << "')";
199 }
200
201 bool symbol::info(unsigned inf) const
202 {
203         switch (inf) {
204                 case info_flags::symbol:
205                 case info_flags::polynomial:
206                 case info_flags::integer_polynomial: 
207                 case info_flags::cinteger_polynomial: 
208                 case info_flags::rational_polynomial: 
209                 case info_flags::crational_polynomial: 
210                 case info_flags::rational_function: 
211                 case info_flags::expanded:
212                         return true;
213                 case info_flags::real:
214                         return get_domain() == domain::real || get_domain() == domain::positive;
215                 case info_flags::positive:
216                 case info_flags::nonnegative:
217                         return get_domain() == domain::positive;
218                 case info_flags::has_indices:
219                         return false;
220         }
221         return inherited::info(inf);
222 }
223
224 ex symbol::conjugate() const
225 {
226         return conjugate_function(*this).hold();
227 }
228
229 ex symbol::real_part() const
230 {
231         return real_part_function(*this).hold();
232 }
233
234 ex symbol::imag_part() const
235 {
236         return imag_part_function(*this).hold();
237 }
238
239 bool symbol::is_polynomial(const ex & var) const
240 {
241         return true;
242 }
243
244 // protected
245
246 /** Implementation of ex::diff() for single differentiation of a symbol.
247  *  It returns 1 or 0.
248  *
249  *  @see ex::diff */
250 ex symbol::derivative(const symbol & s) const
251 {
252         if (compare_same_type(s))
253                 return _ex0;
254         else
255                 return _ex1;
256 }
257
258 int symbol::compare_same_type(const basic & other) const
259 {
260         GINAC_ASSERT(is_a<symbol>(other));
261         const symbol *o = static_cast<const symbol *>(&other);
262         if (serial==o->serial) return 0;
263         return serial < o->serial ? -1 : 1;
264 }
265
266 bool symbol::is_equal_same_type(const basic & other) const
267 {
268         GINAC_ASSERT(is_a<symbol>(other));
269         const symbol *o = static_cast<const symbol *>(&other);
270         return serial==o->serial;
271 }
272
273 unsigned symbol::calchash() const
274 {
275         unsigned seed = make_hash_seed(typeid(*this));
276         hashvalue = golden_ratio_hash(seed ^ serial);
277         setflag(status_flags::hash_calculated);
278         return hashvalue;
279 }
280
281 //////////
282 // virtual functions which can be overridden by derived classes
283 //////////
284
285 // none
286
287 //////////
288 // non-virtual functions in this class
289 //////////
290
291 /** Return default TeX name for symbol. This recognizes some greek letters. */
292 static const std::string& get_default_TeX_name(const std::string& name)
293 {
294         static std::map<std::string, std::string> standard_names;
295         static bool names_initialized = false;
296         if (!names_initialized) {
297                 standard_names["alpha"] = std::string("\\alpha");
298                 standard_names["beta"] = std::string("\\beta");;
299                 standard_names["gamma"] = std::string("\\gamma");;
300                 standard_names["delta"] = std::string("\\delta");;
301                 standard_names["epsilon"] = std::string("\\epsilon");
302                 standard_names["varepsilon"] = std::string("\\varepsilon");
303                 standard_names["zeta"] = std::string("\\zeta");
304                 standard_names["eta" ] = std::string("\\eta" );
305                 standard_names["theta"] = std::string("\\theta");
306                 standard_names["vartheta"] = std::string("\\vartheta");
307                 standard_names["iota"] = std::string("\\iota");
308                 standard_names["kappa"] = std::string("\\kappa");
309                 standard_names["lambda"] = std::string("\\lambda");
310                 standard_names["mu"] = std::string("\\mu");
311                 standard_names["nu"] = std::string("\\nu");
312                 standard_names["xi"] = std::string("\\xi");
313                 standard_names["omicron"] = std::string("\\omicron");
314                 standard_names["pi"] = std::string("\\pi");
315                 standard_names["varpi"] = std::string("\\varpi");
316                 standard_names["rho"] = std::string("\\rho");
317                 standard_names["varrho"] = std::string("\\varrho");
318                 standard_names["sigma"] = std::string("\\sigma");
319                 standard_names["varsigma"] = std::string("\\varsigma");
320                 standard_names["tau"] = std::string("\\tau");
321                 standard_names["upsilon"] = std::string("\\upsilon");
322                 standard_names["phi"] = std::string("\\phi");
323                 standard_names["varphi"] = std::string("\\varphi");
324                 standard_names["chi"] = std::string("\\chi");
325                 standard_names["psi"] = std::string("\\psi");
326                 standard_names["omega"] = std::string("\\omega");
327                 standard_names["Gamma"] = std::string("\\Gamma");
328                 standard_names["Delta"] = std::string("\\Delta");
329                 standard_names["Theta"] = std::string("\\Theta");
330                 standard_names["Lambda"] = std::string("\\Lambda");
331                 standard_names["Xi"] = std::string("\\Xi");
332                 standard_names["Pi"] = std::string("\\Pi");
333                 standard_names["Sigma"] = std::string("\\Sigma");
334                 standard_names["Upsilon"] = std::string("\\Upsilon");
335                 standard_names["Phi"] = std::string("\\Phi");
336                 standard_names["Psi"] = std::string("\\Psi");
337                 standard_names["Omega"] = std::string("\\Omega");
338                 names_initialized = true;
339         }
340         std::map<std::string, std::string>::const_iterator it = standard_names.find(name);
341         if (it != standard_names.end())
342                 return it->second;
343         else
344                 return name;
345 }
346
347 GINAC_BIND_UNARCHIVER(symbol);
348 GINAC_BIND_UNARCHIVER(realsymbol);
349 GINAC_BIND_UNARCHIVER(possymbol);
350
351 //////////
352 // static member variables
353 //////////
354
355 // private
356
357 unsigned symbol::next_serial = 0;
358
359 } // namespace GiNaC