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 : serial(next_serial++), name(autoname_prefix() + ToString(serial)), TeX_name(name), domain(domain::complex), ret_type(return_types::commutative), ret_type_tinfo(make_return_type_t<symbol>())
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 serial(next_serial++), name(initname), TeX_name(default_TeX_name()),
77 domain(domain), ret_type(return_types::commutative),
78 ret_type_tinfo(make_return_type_t<symbol>())
80 setflag(status_flags::evaluated | status_flags::expanded);
83 symbol::symbol(const std::string & initname, unsigned rt, const return_type_t& rtt, unsigned domain) :
84 serial(next_serial++), name(initname), TeX_name(default_TeX_name()),
85 domain(domain), ret_type(rt), ret_type_tinfo(rtt)
87 setflag(status_flags::evaluated | status_flags::expanded);
90 symbol::symbol(const std::string & initname, const std::string & texname, unsigned domain) :
91 serial(next_serial++), name(initname), TeX_name(texname), domain(domain),
92 ret_type(return_types::commutative), ret_type_tinfo(make_return_type_t<symbol>())
94 setflag(status_flags::evaluated | status_flags::expanded);
97 symbol::symbol(const std::string & initname, const std::string & texname,
98 unsigned rt, const return_type_t& rtt, unsigned domain) :
99 serial(next_serial++), name(initname), TeX_name(texname),
100 domain(domain), ret_type(rt), ret_type_tinfo(rtt)
102 setflag(status_flags::evaluated | status_flags::expanded);
107 realsymbol::realsymbol(const std::string & initname, unsigned domain)
108 : symbol(initname, domain) { }
110 realsymbol::realsymbol(const std::string & initname, const std::string & texname, unsigned domain)
111 : symbol(initname, texname, domain) { }
113 realsymbol::realsymbol(const std::string & initname, unsigned rt, const return_type_t& rtt, unsigned domain)
114 : symbol(initname, rt, rtt, domain) { }
116 realsymbol::realsymbol(const std::string & initname, const std::string & texname, unsigned rt, const return_type_t& rtt, unsigned domain)
117 : symbol(initname, texname, rt, rtt, domain) { }
121 possymbol::possymbol(const std::string & initname, unsigned domain)
122 : symbol(initname, domain) { }
124 possymbol::possymbol(const std::string & initname, const std::string & texname, unsigned domain)
125 : symbol(initname, texname, domain) { }
127 possymbol::possymbol(const std::string & initname, unsigned rt, const return_type_t& rtt, unsigned domain)
128 : symbol(initname, rt, rtt, domain) { }
130 possymbol::possymbol(const std::string & initname, const std::string & texname, unsigned rt, const return_type_t& rtt, unsigned domain)
131 : symbol(initname, texname, rt, rtt, domain) { }
137 /** Construct object from archive_node. */
138 symbol::symbol(const archive_node &n, lst &sym_lst)
139 : inherited(n, sym_lst), serial(next_serial++)
141 if (!n.find_string("name", name))
142 name = autoname_prefix() + ToString(serial);
143 if (!n.find_string("TeXname", TeX_name))
144 TeX_name = default_TeX_name();
145 if (!n.find_unsigned("domain", domain))
146 domain = domain::complex;
147 if (!n.find_unsigned("return_type", ret_type))
148 ret_type = return_types::commutative;
149 setflag(status_flags::evaluated | status_flags::expanded);
152 /** Unarchive the object. */
153 ex symbol::unarchive(const archive_node &n, lst &sym_lst)
155 ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
157 // If symbol is in sym_lst, return the existing symbol
158 for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
159 if (is_a<symbol>(*it) && (ex_to<symbol>(*it).name == ex_to<symbol>(s).name))
163 // Otherwise add new symbol to list and return it
168 /** Archive the object. */
169 void symbol::archive(archive_node &n) const
171 inherited::archive(n);
172 n.add_string("name", name);
173 if (TeX_name != default_TeX_name())
174 n.add_string("TeX_name", TeX_name);
175 if (domain != domain::complex)
176 n.add_unsigned("domain", domain);
177 if (ret_type != return_types::commutative)
178 n.add_unsigned("return_type", ret_type);
182 // functions overriding virtual functions from base classes
187 void symbol::do_print(const print_context & c, unsigned level) const
192 void symbol::do_print_latex(const print_latex & c, unsigned level) const
197 void symbol::do_print_tree(const print_tree & c, unsigned level) const
199 c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
200 << ", serial=" << serial
201 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
202 << ", domain=" << domain
206 void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
208 c.s << class_name() << "('" << name;
209 if (TeX_name != default_TeX_name())
210 c.s << "','" << TeX_name;
214 bool symbol::info(unsigned inf) const
217 case info_flags::symbol:
218 case info_flags::polynomial:
219 case info_flags::integer_polynomial:
220 case info_flags::cinteger_polynomial:
221 case info_flags::rational_polynomial:
222 case info_flags::crational_polynomial:
223 case info_flags::rational_function:
224 case info_flags::expanded:
226 case info_flags::real:
227 return domain == domain::real || domain == domain::positive;
228 case info_flags::positive:
229 case info_flags::nonnegative:
230 return domain == domain::positive;
231 case info_flags::has_indices:
234 return inherited::info(inf);
237 ex symbol::conjugate() const
239 if (this->domain == domain::complex) {
240 return conjugate_function(*this).hold();
246 ex symbol::real_part() const
248 if (domain==domain::real || domain==domain::positive)
250 return real_part_function(*this).hold();
253 ex symbol::imag_part() const
255 if (domain==domain::real || domain==domain::positive)
257 return imag_part_function(*this).hold();
260 bool symbol::is_polynomial(const ex & var) const
267 /** Implementation of ex::diff() for single differentiation of a symbol.
271 ex symbol::derivative(const symbol & s) const
273 if (compare_same_type(s))
279 int symbol::compare_same_type(const basic & other) const
281 GINAC_ASSERT(is_a<symbol>(other));
282 const symbol *o = static_cast<const symbol *>(&other);
283 if (serial==o->serial) return 0;
284 return serial < o->serial ? -1 : 1;
287 bool symbol::is_equal_same_type(const basic & other) const
289 GINAC_ASSERT(is_a<symbol>(other));
290 const symbol *o = static_cast<const symbol *>(&other);
291 return serial==o->serial;
294 unsigned symbol::calchash() const
296 const void* this_tinfo = (const void*)typeid(*this).name();
297 hashvalue = golden_ratio_hash((p_int)this_tinfo ^ serial);
298 setflag(status_flags::hash_calculated);
303 // virtual functions which can be overridden by derived classes
309 // non-virtual functions in this class
314 /** Symbols not constructed with a string get one assigned using this
315 * prefix and a number. */
316 std::string& symbol::autoname_prefix()
318 static std::string s("symbol");
322 /** Return default TeX name for symbol. This recognizes some greek letters. */
323 std::string symbol::default_TeX_name() const
325 if (name=="alpha" || name=="beta" || name=="gamma"
326 || name=="delta" || name=="epsilon" || name=="varepsilon"
327 || name=="zeta" || name=="eta" || name=="theta"
328 || name=="vartheta" || name=="iota" || name=="kappa"
329 || name=="lambda" || name=="mu" || name=="nu"
330 || name=="xi" || name=="omicron" || name=="pi"
331 || name=="varpi" || name=="rho" || name=="varrho"
332 || name=="sigma" || name=="varsigma" || name=="tau"
333 || name=="upsilon" || name=="phi" || name=="varphi"
334 || name=="chi" || name=="psi" || name=="omega"
335 || name=="Gamma" || name=="Delta" || name=="Theta"
336 || name=="Lambda" || name=="Xi" || name=="Pi"
337 || name=="Sigma" || name=="Upsilon" || name=="Phi"
338 || name=="Psi" || name=="Omega")
345 // static member variables
350 unsigned symbol::next_serial = 0;