3 * Implementation of GiNaC's symbolic objects. */
6 * GiNaC Copyright (C) 1999-2022 Johannes Gutenberg University Mainz, Germany
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.
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.
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
27 #include "hash_seed.h"
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))
43 // default constructor
48 symbol::symbol() : serial(next_serial++), name(""), TeX_name("")
50 setflag(status_flags::evaluated | status_flags::expanded);
55 realsymbol::realsymbol() : symbol() { }
59 possymbol::possymbol() : realsymbol() { }
69 symbol::symbol(const std::string & initname) : serial(next_serial++),
70 name(initname), TeX_name("")
72 setflag(status_flags::evaluated | status_flags::expanded);
75 symbol::symbol(const std::string & initname, const std::string & texname) :
76 serial(next_serial++), name(initname), TeX_name(texname)
78 setflag(status_flags::evaluated | status_flags::expanded);
83 realsymbol::realsymbol(const std::string & initname) : symbol(initname) { }
85 realsymbol::realsymbol(const std::string & initname, const std::string & texname)
86 : symbol(initname, texname) { }
90 possymbol::possymbol(const std::string & initname) : realsymbol(initname) { }
92 possymbol::possymbol(const std::string & initname, const std::string & texname)
93 : realsymbol(initname, texname) { }
99 /** Read object from archive_node. */
100 void symbol::read_archive(const archive_node &n, lst &sym_lst)
102 inherited::read_archive(n, sym_lst);
103 serial = next_serial++;
104 std::string tmp_name;
105 n.find_string("name", tmp_name);
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);
124 if (!n.find_string("TeXname", TeX_name))
125 TeX_name = std::string("");
126 setflag(status_flags::evaluated | status_flags::expanded);
128 setflag(status_flags::dynallocated);
129 sym_lst.append(*this);
132 /** Archive the object. */
133 void symbol::archive(archive_node &n) const
135 inherited::archive(n);
136 // XXX: we should not archive anonymous symbols.
138 n.add_string("name", name);
139 if (!TeX_name.empty())
140 n.add_string("TeX_name", TeX_name);
144 // functions overriding virtual functions from base classes
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);
152 std::string symbol::get_name() const
155 name = "symbol" + std::to_string(serial);
160 std::string symbol::get_TeX_name() const
162 if (TeX_name.empty()) {
163 return get_default_TeX_name(get_name());
170 void symbol::do_print(const print_context & c, unsigned level) const
175 void symbol::do_print_latex(const print_latex & c, unsigned level) const
177 if (!TeX_name.empty())
179 else if (!name.empty())
180 c.s << get_default_TeX_name(name);
182 c.s << "symbol" << serial;
185 void symbol::do_print_tree(const print_tree & c, unsigned level) const
187 c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
188 << ", serial=" << serial
189 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
190 << ", domain=" << get_domain()
194 void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
196 c.s << class_name() << "('";
200 c.s << "symbol" << serial;
201 if (!TeX_name.empty())
202 c.s << "','" << TeX_name;
206 bool symbol::info(unsigned inf) const
209 case info_flags::symbol:
210 case info_flags::polynomial:
211 case info_flags::integer_polynomial:
212 case info_flags::cinteger_polynomial:
213 case info_flags::rational_polynomial:
214 case info_flags::crational_polynomial:
215 case info_flags::rational_function:
216 case info_flags::expanded:
218 case info_flags::real:
219 return get_domain() == domain::real || get_domain() == domain::positive;
220 case info_flags::positive:
221 case info_flags::nonnegative:
222 return get_domain() == domain::positive;
223 case info_flags::has_indices:
226 return inherited::info(inf);
229 ex symbol::conjugate() const
231 return conjugate_function(*this).hold();
234 ex symbol::real_part() const
236 return real_part_function(*this).hold();
239 ex symbol::imag_part() const
241 return imag_part_function(*this).hold();
244 bool symbol::is_polynomial(const ex & var) const
251 /** Implementation of ex::diff() for single differentiation of a symbol.
255 ex symbol::derivative(const symbol & s) const
257 if (compare_same_type(s))
263 int symbol::compare_same_type(const basic & other) const
265 GINAC_ASSERT(is_a<symbol>(other));
266 const symbol *o = static_cast<const symbol *>(&other);
267 if (serial==o->serial) return 0;
268 return serial < o->serial ? -1 : 1;
271 bool symbol::is_equal_same_type(const basic & other) const
273 GINAC_ASSERT(is_a<symbol>(other));
274 const symbol *o = static_cast<const symbol *>(&other);
275 return serial==o->serial;
278 unsigned symbol::calchash() const
280 unsigned seed = make_hash_seed(typeid(*this));
281 hashvalue = golden_ratio_hash(seed ^ serial);
282 setflag(status_flags::hash_calculated);
287 // virtual functions which can be overridden by derived classes
293 // non-virtual functions in this class
296 /** Return default TeX name for symbol. This recognizes some greek letters. */
297 static const std::string& get_default_TeX_name(const std::string& name)
299 static std::map<std::string, std::string> standard_names;
300 static bool names_initialized = false;
301 if (!names_initialized) {
302 standard_names["alpha"] = std::string("\\alpha");
303 standard_names["beta"] = std::string("\\beta");;
304 standard_names["gamma"] = std::string("\\gamma");;
305 standard_names["delta"] = std::string("\\delta");;
306 standard_names["epsilon"] = std::string("\\epsilon");
307 standard_names["varepsilon"] = std::string("\\varepsilon");
308 standard_names["zeta"] = std::string("\\zeta");
309 standard_names["eta" ] = std::string("\\eta" );
310 standard_names["theta"] = std::string("\\theta");
311 standard_names["vartheta"] = std::string("\\vartheta");
312 standard_names["iota"] = std::string("\\iota");
313 standard_names["kappa"] = std::string("\\kappa");
314 standard_names["lambda"] = std::string("\\lambda");
315 standard_names["mu"] = std::string("\\mu");
316 standard_names["nu"] = std::string("\\nu");
317 standard_names["xi"] = std::string("\\xi");
318 standard_names["omicron"] = std::string("\\omicron");
319 standard_names["pi"] = std::string("\\pi");
320 standard_names["varpi"] = std::string("\\varpi");
321 standard_names["rho"] = std::string("\\rho");
322 standard_names["varrho"] = std::string("\\varrho");
323 standard_names["sigma"] = std::string("\\sigma");
324 standard_names["varsigma"] = std::string("\\varsigma");
325 standard_names["tau"] = std::string("\\tau");
326 standard_names["upsilon"] = std::string("\\upsilon");
327 standard_names["phi"] = std::string("\\phi");
328 standard_names["varphi"] = std::string("\\varphi");
329 standard_names["chi"] = std::string("\\chi");
330 standard_names["psi"] = std::string("\\psi");
331 standard_names["omega"] = std::string("\\omega");
332 standard_names["Gamma"] = std::string("\\Gamma");
333 standard_names["Delta"] = std::string("\\Delta");
334 standard_names["Theta"] = std::string("\\Theta");
335 standard_names["Lambda"] = std::string("\\Lambda");
336 standard_names["Xi"] = std::string("\\Xi");
337 standard_names["Pi"] = std::string("\\Pi");
338 standard_names["Sigma"] = std::string("\\Sigma");
339 standard_names["Upsilon"] = std::string("\\Upsilon");
340 standard_names["Phi"] = std::string("\\Phi");
341 standard_names["Psi"] = std::string("\\Psi");
342 standard_names["Omega"] = std::string("\\Omega");
343 names_initialized = true;
345 std::map<std::string, std::string>::const_iterator it = standard_names.find(name);
346 if (it != standard_names.end())
352 GINAC_BIND_UNARCHIVER(symbol);
353 GINAC_BIND_UNARCHIVER(realsymbol);
354 GINAC_BIND_UNARCHIVER(possymbol);
357 // static member variables
362 unsigned symbol::next_serial = 0;