]> www.ginac.de Git - ginac.git/blob - ginac/symbol.cpp
symbol: make get_domain() a virtual method, remove symbol::domain.
[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 /** Construct object from archive_node. */
100 symbol::symbol(const archive_node &n, lst &sym_lst)
101  : inherited(n, sym_lst), serial(next_serial++)
102 {
103         if (!n.find_string("name", name))
104                 name = std::string("");
105         if (!n.find_string("TeXname", TeX_name))
106                 TeX_name = std::string("");
107         setflag(status_flags::evaluated | status_flags::expanded);
108 }
109
110 /** Unarchive the object. */
111 ex symbol::unarchive(const archive_node &n, lst &sym_lst)
112 {
113         ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
114
115         // If symbol is in sym_lst, return the existing symbol
116         for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
117                 if (is_a<symbol>(*it) && (ex_to<symbol>(*it).name == ex_to<symbol>(s).name))
118                         return *it;
119         }
120
121         // Otherwise add new symbol to list and return it
122         sym_lst.append(s);
123         return s;
124 }
125
126 /** Archive the object. */
127 void symbol::archive(archive_node &n) const
128 {
129         inherited::archive(n);
130         // XXX: we should not archive anonymous symbols.
131         if (!name.empty())
132                 n.add_string("name", name);
133         if (!TeX_name.empty())
134                 n.add_string("TeX_name", TeX_name);
135 }
136
137 //////////
138 // functions overriding virtual functions from base classes
139 //////////
140
141 /** Return default TeX name for symbol. This recognizes some greek letters. */
142 static const std::string& get_default_TeX_name(const std::string& name);
143
144 // public
145
146 void symbol::do_print(const print_context & c, unsigned level) const
147 {
148         if (!name.empty())
149                 c.s << name;
150         else
151                 c.s << "symbol" << serial;
152 }
153
154 void symbol::do_print_latex(const print_latex & c, unsigned level) const
155 {
156         if (!TeX_name.empty())
157                 c.s << TeX_name;
158         else if (!name.empty())
159                 c.s << get_default_TeX_name(name);
160         else
161                 c.s << "symbol" << serial;
162 }
163
164 void symbol::do_print_tree(const print_tree & c, unsigned level) const
165 {
166         c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
167             << ", serial=" << serial
168             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
169             << ", domain=" << get_domain()
170             << std::endl;
171 }
172
173 void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
174 {
175         c.s << class_name() << "('";
176         if (!name.empty())
177                 c.s << name;
178         else
179                 c.s << "symbol" << serial;
180         if (!TeX_name.empty())
181                 c.s << "','" << TeX_name;
182         c.s << "')";
183 }
184
185 bool symbol::info(unsigned inf) const
186 {
187         switch (inf) {
188                 case info_flags::symbol:
189                 case info_flags::polynomial:
190                 case info_flags::integer_polynomial: 
191                 case info_flags::cinteger_polynomial: 
192                 case info_flags::rational_polynomial: 
193                 case info_flags::crational_polynomial: 
194                 case info_flags::rational_function: 
195                 case info_flags::expanded:
196                         return true;
197                 case info_flags::real:
198                         return get_domain() == domain::real || get_domain() == domain::positive;
199                 case info_flags::positive:
200                 case info_flags::nonnegative:
201                         return get_domain() == domain::positive;
202                 case info_flags::has_indices:
203                         return false;
204         }
205         return inherited::info(inf);
206 }
207
208 ex symbol::conjugate() const
209 {
210         return conjugate_function(*this).hold();
211 }
212
213 ex symbol::real_part() const
214 {
215         return real_part_function(*this).hold();
216 }
217
218 ex symbol::imag_part() const
219 {
220         return imag_part_function(*this).hold();
221 }
222
223 bool symbol::is_polynomial(const ex & var) const
224 {
225         return true;
226 }
227
228 // protected
229
230 /** Implementation of ex::diff() for single differentiation of a symbol.
231  *  It returns 1 or 0.
232  *
233  *  @see ex::diff */
234 ex symbol::derivative(const symbol & s) const
235 {
236         if (compare_same_type(s))
237                 return _ex0;
238         else
239                 return _ex1;
240 }
241
242 int symbol::compare_same_type(const basic & other) const
243 {
244         GINAC_ASSERT(is_a<symbol>(other));
245         const symbol *o = static_cast<const symbol *>(&other);
246         if (serial==o->serial) return 0;
247         return serial < o->serial ? -1 : 1;
248 }
249
250 bool symbol::is_equal_same_type(const basic & other) const
251 {
252         GINAC_ASSERT(is_a<symbol>(other));
253         const symbol *o = static_cast<const symbol *>(&other);
254         return serial==o->serial;
255 }
256
257 unsigned symbol::calchash() const
258 {
259         const void* this_tinfo = (const void*)typeid(*this).name();
260         hashvalue = golden_ratio_hash((p_int)this_tinfo ^ serial);
261         setflag(status_flags::hash_calculated);
262         return hashvalue;
263 }
264
265 //////////
266 // virtual functions which can be overridden by derived classes
267 //////////
268
269 // none
270
271 //////////
272 // non-virtual functions in this class
273 //////////
274
275 /** Return default TeX name for symbol. This recognizes some greek letters. */
276 static const std::string& get_default_TeX_name(const std::string& name)
277 {
278         static std::map<std::string, std::string> standard_names;
279         static bool names_initialized = false;
280         if (!names_initialized) {
281                 standard_names["alpha"] = std::string("\\alpha");
282                 standard_names["beta"] = std::string("\\beta");;
283                 standard_names["gamma"] = std::string("\\gamma");;
284                 standard_names["delta"] = std::string("\\delta");;
285                 standard_names["epsilon"] = std::string("\\epsilon");
286                 standard_names["varepsilon"] = std::string("\\varepsilon");
287                 standard_names["zeta"] = std::string("\\zeta");
288                 standard_names["eta" ] = std::string("\\eta" );
289                 standard_names["theta"] = std::string("\\theta");
290                 standard_names["vartheta"] = std::string("\\vartheta");
291                 standard_names["iota"] = std::string("\\iota");
292                 standard_names["kappa"] = std::string("\\kappa");
293                 standard_names["lambda"] = std::string("\\lambda");
294                 standard_names["mu"] = std::string("\\mu");
295                 standard_names["nu"] = std::string("\\nu");
296                 standard_names["xi"] = std::string("\\xi");
297                 standard_names["omicron"] = std::string("\\omicron");
298                 standard_names["pi"] = std::string("\\pi");
299                 standard_names["varpi"] = std::string("\\varpi");
300                 standard_names["rho"] = std::string("\\rho");
301                 standard_names["varrho"] = std::string("\\varrho");
302                 standard_names["sigma"] = std::string("\\sigma");
303                 standard_names["varsigma"] = std::string("\\varsigma");
304                 standard_names["tau"] = std::string("\\tau");
305                 standard_names["upsilon"] = std::string("\\upsilon");
306                 standard_names["phi"] = std::string("\\phi");
307                 standard_names["varphi"] = std::string("\\varphi");
308                 standard_names["chi"] = std::string("\\chi");
309                 standard_names["psi"] = std::string("\\psi");
310                 standard_names["omega"] = std::string("\\omega");
311                 standard_names["Gamma"] = std::string("\\Gamma");
312                 standard_names["Delta"] = std::string("\\Delta");
313                 standard_names["Theta"] = std::string("\\Theta");
314                 standard_names["Lambda"] = std::string("\\Lambda");
315                 standard_names["Xi"] = std::string("\\Xi");
316                 standard_names["Pi"] = std::string("\\Pi");
317                 standard_names["Sigma"] = std::string("\\Sigma");
318                 standard_names["Upsilon"] = std::string("\\Upsilon");
319                 standard_names["Phi"] = std::string("\\Phi");
320                 standard_names["Psi"] = std::string("\\Psi");
321                 standard_names["Omega"] = std::string("\\Omega");
322                 names_initialized = true;
323         }
324         std::map<std::string, std::string>::const_iterator it = standard_names.find(name);
325         if (it != standard_names.end())
326                 return it->second;
327         else
328                 return name;
329 }
330
331 //////////
332 // static member variables
333 //////////
334
335 // private
336
337 unsigned symbol::next_serial = 0;
338
339 } // namespace GiNaC