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
35 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(symbol, basic,
36 print_func<print_context>(&symbol::do_print).
37 print_func<print_latex>(&symbol::do_print_latex).
38 print_func<print_tree>(&symbol::do_print_tree).
39 print_func<print_python_repr>(&symbol::do_print_python_repr))
42 // default constructor
48 : inherited(&symbol::tinfo_static), serial(next_serial++), name(autoname_prefix() + ToString(serial)), TeX_name(name), domain(domain::complex), ret_type(return_types::commutative), ret_type_tinfo(&symbol::tinfo_static)
50 setflag(status_flags::evaluated | status_flags::expanded);
55 realsymbol::realsymbol()
57 domain = domain::real;
62 possymbol::possymbol()
64 domain = domain::positive;
75 symbol::symbol(const std::string & initname, unsigned domain)
76 : inherited(&symbol::tinfo_static), serial(next_serial++), name(initname), TeX_name(default_TeX_name()), domain(domain), ret_type(return_types::commutative), ret_type_tinfo(&symbol::tinfo_static)
78 setflag(status_flags::evaluated | status_flags::expanded);
81 symbol::symbol(const std::string & initname, unsigned rt, tinfo_t rtt, unsigned domain)
82 : inherited(&symbol::tinfo_static), serial(next_serial++), name(initname), TeX_name(default_TeX_name()), domain(domain), ret_type(rt), ret_type_tinfo(rtt)
84 setflag(status_flags::evaluated | status_flags::expanded);
87 symbol::symbol(const std::string & initname, const std::string & texname, unsigned domain)
88 : inherited(&symbol::tinfo_static), serial(next_serial++), name(initname), TeX_name(texname), domain(domain), ret_type(return_types::commutative), ret_type_tinfo(&symbol::tinfo_static)
90 setflag(status_flags::evaluated | status_flags::expanded);
93 symbol::symbol(const std::string & initname, const std::string & texname, unsigned rt, tinfo_t rtt, unsigned domain)
94 : inherited(&symbol::tinfo_static), serial(next_serial++), name(initname), TeX_name(texname), domain(domain), ret_type(rt), ret_type_tinfo(rtt)
96 setflag(status_flags::evaluated | status_flags::expanded);
101 realsymbol::realsymbol(const std::string & initname, unsigned domain)
102 : symbol(initname, domain) { }
104 realsymbol::realsymbol(const std::string & initname, const std::string & texname, unsigned domain)
105 : symbol(initname, texname, domain) { }
107 realsymbol::realsymbol(const std::string & initname, unsigned rt, tinfo_t rtt, unsigned domain)
108 : symbol(initname, rt, rtt, domain) { }
110 realsymbol::realsymbol(const std::string & initname, const std::string & texname, unsigned rt, tinfo_t rtt, unsigned domain)
111 : symbol(initname, texname, rt, rtt, domain) { }
115 possymbol::possymbol(const std::string & initname, unsigned domain)
116 : symbol(initname, domain) { }
118 possymbol::possymbol(const std::string & initname, const std::string & texname, unsigned domain)
119 : symbol(initname, texname, domain) { }
121 possymbol::possymbol(const std::string & initname, unsigned rt, tinfo_t rtt, unsigned domain)
122 : symbol(initname, rt, rtt, domain) { }
124 possymbol::possymbol(const std::string & initname, const std::string & texname, unsigned rt, tinfo_t rtt, unsigned domain)
125 : symbol(initname, texname, rt, rtt, domain) { }
131 /** Construct object from archive_node. */
132 symbol::symbol(const archive_node &n, lst &sym_lst)
133 : inherited(n, sym_lst), serial(next_serial++)
135 if (!n.find_string("name", name))
136 name = autoname_prefix() + ToString(serial);
137 if (!n.find_string("TeXname", TeX_name))
138 TeX_name = default_TeX_name();
139 if (!n.find_unsigned("domain", domain))
140 domain = domain::complex;
141 if (!n.find_unsigned("return_type", ret_type))
142 ret_type = return_types::commutative;
143 setflag(status_flags::evaluated | status_flags::expanded);
146 /** Unarchive the object. */
147 ex symbol::unarchive(const archive_node &n, lst &sym_lst)
149 ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
151 // If symbol is in sym_lst, return the existing symbol
152 for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
153 if (is_a<symbol>(*it) && (ex_to<symbol>(*it).name == ex_to<symbol>(s).name))
157 // Otherwise add new symbol to list and return it
162 /** Archive the object. */
163 void symbol::archive(archive_node &n) const
165 inherited::archive(n);
166 n.add_string("name", name);
167 if (TeX_name != default_TeX_name())
168 n.add_string("TeX_name", TeX_name);
169 if (domain != domain::complex)
170 n.add_unsigned("domain", domain);
171 if (ret_type != return_types::commutative)
172 n.add_unsigned("return_type", ret_type);
176 // functions overriding virtual functions from base classes
181 void symbol::do_print(const print_context & c, unsigned level) const
186 void symbol::do_print_latex(const print_latex & c, unsigned level) const
191 void symbol::do_print_tree(const print_tree & c, unsigned level) const
193 c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
194 << ", serial=" << serial
195 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
196 << ", domain=" << domain
200 void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
202 c.s << class_name() << "('" << name;
203 if (TeX_name != default_TeX_name())
204 c.s << "','" << TeX_name;
208 bool symbol::info(unsigned inf) const
211 case info_flags::symbol:
212 case info_flags::polynomial:
213 case info_flags::integer_polynomial:
214 case info_flags::cinteger_polynomial:
215 case info_flags::rational_polynomial:
216 case info_flags::crational_polynomial:
217 case info_flags::rational_function:
218 case info_flags::expanded:
220 case info_flags::real:
221 return domain == domain::real || domain == domain::positive;
222 case info_flags::positive:
223 case info_flags::nonnegative:
224 return domain == domain::positive;
225 case info_flags::has_indices:
228 return inherited::info(inf);
231 ex symbol::conjugate() const
233 if (this->domain == domain::complex) {
234 return conjugate_function(*this).hold();
240 ex symbol::real_part() const
242 if (domain==domain::real || domain==domain::positive)
244 return real_part_function(*this).hold();
247 ex symbol::imag_part() const
249 if (domain==domain::real || domain==domain::positive)
251 return imag_part_function(*this).hold();
254 bool symbol::is_polynomial(const ex & var) const
261 /** Implementation of ex::diff() for single differentiation of a symbol.
265 ex symbol::derivative(const symbol & s) const
267 if (compare_same_type(s))
273 int symbol::compare_same_type(const basic & other) const
275 GINAC_ASSERT(is_a<symbol>(other));
276 const symbol *o = static_cast<const symbol *>(&other);
277 if (serial==o->serial) return 0;
278 return serial < o->serial ? -1 : 1;
281 bool symbol::is_equal_same_type(const basic & other) const
283 GINAC_ASSERT(is_a<symbol>(other));
284 const symbol *o = static_cast<const symbol *>(&other);
285 return serial==o->serial;
288 unsigned symbol::calchash() const
290 hashvalue = golden_ratio_hash((p_int)tinfo() ^ serial);
291 setflag(status_flags::hash_calculated);
296 // virtual functions which can be overridden by derived classes
302 // non-virtual functions in this class
307 /** Symbols not constructed with a string get one assigned using this
308 * prefix and a number. */
309 std::string& symbol::autoname_prefix()
311 static std::string s("symbol");
315 /** Return default TeX name for symbol. This recognizes some greek letters. */
316 std::string symbol::default_TeX_name() const
318 if (name=="alpha" || name=="beta" || name=="gamma"
319 || name=="delta" || name=="epsilon" || name=="varepsilon"
320 || name=="zeta" || name=="eta" || name=="theta"
321 || name=="vartheta" || name=="iota" || name=="kappa"
322 || name=="lambda" || name=="mu" || name=="nu"
323 || name=="xi" || name=="omicron" || name=="pi"
324 || name=="varpi" || name=="rho" || name=="varrho"
325 || name=="sigma" || name=="varsigma" || name=="tau"
326 || name=="upsilon" || name=="phi" || name=="varphi"
327 || name=="chi" || name=="psi" || name=="omega"
328 || name=="Gamma" || name=="Delta" || name=="Theta"
329 || name=="Lambda" || name=="Xi" || name=="Pi"
330 || name=="Sigma" || name=="Upsilon" || name=="Phi"
331 || name=="Psi" || name=="Omega")
338 // static member variables
343 unsigned symbol::next_serial = 0;