3 * Implementation of GiNaC's symbolic objects. */
6 * GiNaC Copyright (C) 1999-2008 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
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 /** Construct object from archive_node. */
100 symbol::symbol(const archive_node &n, lst &sym_lst)
101 : inherited(n, sym_lst), serial(next_serial++)
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);
110 /** Unarchive the object. */
111 ex symbol::unarchive(const archive_node &n, lst &sym_lst)
113 ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
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))
121 // Otherwise add new symbol to list and return it
126 /** Archive the object. */
127 void symbol::archive(archive_node &n) const
129 inherited::archive(n);
130 // XXX: we should not archive anonymous symbols.
132 n.add_string("name", name);
133 if (!TeX_name.empty())
134 n.add_string("TeX_name", TeX_name);
138 // functions overriding virtual functions from base classes
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);
146 void symbol::do_print(const print_context & c, unsigned level) const
151 c.s << "symbol" << serial;
154 void symbol::do_print_latex(const print_latex & c, unsigned level) const
156 if (!TeX_name.empty())
158 else if (!name.empty())
159 c.s << get_default_TeX_name(name);
161 c.s << "symbol" << serial;
164 void symbol::do_print_tree(const print_tree & c, unsigned level) const
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()
173 void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
175 c.s << class_name() << "('";
179 c.s << "symbol" << serial;
180 if (!TeX_name.empty())
181 c.s << "','" << TeX_name;
185 bool symbol::info(unsigned inf) const
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:
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:
205 return inherited::info(inf);
208 ex symbol::conjugate() const
210 return conjugate_function(*this).hold();
213 ex symbol::real_part() const
215 return real_part_function(*this).hold();
218 ex symbol::imag_part() const
220 return imag_part_function(*this).hold();
223 bool symbol::is_polynomial(const ex & var) const
230 /** Implementation of ex::diff() for single differentiation of a symbol.
234 ex symbol::derivative(const symbol & s) const
236 if (compare_same_type(s))
242 int symbol::compare_same_type(const basic & other) const
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;
250 bool symbol::is_equal_same_type(const basic & other) const
252 GINAC_ASSERT(is_a<symbol>(other));
253 const symbol *o = static_cast<const symbol *>(&other);
254 return serial==o->serial;
257 unsigned symbol::calchash() const
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);
266 // virtual functions which can be overridden by derived classes
272 // non-virtual functions in this class
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)
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;
324 std::map<std::string, std::string>::const_iterator it = standard_names.find(name);
325 if (it != standard_names.end())
332 // static member variables
337 unsigned symbol::next_serial = 0;