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