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