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)
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()),
79 setflag(status_flags::evaluated | status_flags::expanded);
82 symbol::symbol(const std::string & initname, const std::string & texname, unsigned domain) :
83 serial(next_serial++), name(initname), TeX_name(texname), domain(domain)
85 setflag(status_flags::evaluated | status_flags::expanded);
90 realsymbol::realsymbol(const std::string & initname, unsigned domain)
91 : symbol(initname, domain) { }
93 realsymbol::realsymbol(const std::string & initname, const std::string & texname, unsigned domain)
94 : symbol(initname, texname, domain) { }
98 possymbol::possymbol(const std::string & initname, unsigned domain)
99 : symbol(initname, domain) { }
101 possymbol::possymbol(const std::string & initname, const std::string & texname, unsigned domain)
102 : symbol(initname, texname, domain) { }
108 /** Construct object from archive_node. */
109 symbol::symbol(const archive_node &n, lst &sym_lst)
110 : inherited(n, sym_lst), serial(next_serial++)
112 if (!n.find_string("name", name))
113 name = autoname_prefix() + ToString(serial);
114 if (!n.find_string("TeXname", TeX_name))
115 TeX_name = default_TeX_name();
116 if (!n.find_unsigned("domain", domain))
117 domain = domain::complex;
118 setflag(status_flags::evaluated | status_flags::expanded);
121 /** Unarchive the object. */
122 ex symbol::unarchive(const archive_node &n, lst &sym_lst)
124 ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
126 // If symbol is in sym_lst, return the existing symbol
127 for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
128 if (is_a<symbol>(*it) && (ex_to<symbol>(*it).name == ex_to<symbol>(s).name))
132 // Otherwise add new symbol to list and return it
137 /** Archive the object. */
138 void symbol::archive(archive_node &n) const
140 inherited::archive(n);
141 n.add_string("name", name);
142 if (TeX_name != default_TeX_name())
143 n.add_string("TeX_name", TeX_name);
144 if (domain != domain::complex)
145 n.add_unsigned("domain", domain);
149 // functions overriding virtual functions from base classes
154 void symbol::do_print(const print_context & c, unsigned level) const
159 void symbol::do_print_latex(const print_latex & c, unsigned level) const
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=" << domain
173 void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
175 c.s << class_name() << "('" << name;
176 if (TeX_name != default_TeX_name())
177 c.s << "','" << TeX_name;
181 bool symbol::info(unsigned inf) const
184 case info_flags::symbol:
185 case info_flags::polynomial:
186 case info_flags::integer_polynomial:
187 case info_flags::cinteger_polynomial:
188 case info_flags::rational_polynomial:
189 case info_flags::crational_polynomial:
190 case info_flags::rational_function:
191 case info_flags::expanded:
193 case info_flags::real:
194 return domain == domain::real || domain == domain::positive;
195 case info_flags::positive:
196 case info_flags::nonnegative:
197 return domain == domain::positive;
198 case info_flags::has_indices:
201 return inherited::info(inf);
204 ex symbol::conjugate() const
206 if (this->domain == domain::complex) {
207 return conjugate_function(*this).hold();
213 ex symbol::real_part() const
215 if (domain==domain::real || domain==domain::positive)
217 return real_part_function(*this).hold();
220 ex symbol::imag_part() const
222 if (domain==domain::real || domain==domain::positive)
224 return imag_part_function(*this).hold();
227 bool symbol::is_polynomial(const ex & var) const
234 /** Implementation of ex::diff() for single differentiation of a symbol.
238 ex symbol::derivative(const symbol & s) const
240 if (compare_same_type(s))
246 int symbol::compare_same_type(const basic & other) const
248 GINAC_ASSERT(is_a<symbol>(other));
249 const symbol *o = static_cast<const symbol *>(&other);
250 if (serial==o->serial) return 0;
251 return serial < o->serial ? -1 : 1;
254 bool symbol::is_equal_same_type(const basic & other) const
256 GINAC_ASSERT(is_a<symbol>(other));
257 const symbol *o = static_cast<const symbol *>(&other);
258 return serial==o->serial;
261 unsigned symbol::calchash() const
263 const void* this_tinfo = (const void*)typeid(*this).name();
264 hashvalue = golden_ratio_hash((p_int)this_tinfo ^ serial);
265 setflag(status_flags::hash_calculated);
270 // virtual functions which can be overridden by derived classes
276 // non-virtual functions in this class
281 /** Symbols not constructed with a string get one assigned using this
282 * prefix and a number. */
283 std::string& symbol::autoname_prefix()
285 static std::string s("symbol");
289 /** Return default TeX name for symbol. This recognizes some greek letters. */
290 std::string symbol::default_TeX_name() const
292 if (name=="alpha" || name=="beta" || name=="gamma"
293 || name=="delta" || name=="epsilon" || name=="varepsilon"
294 || name=="zeta" || name=="eta" || name=="theta"
295 || name=="vartheta" || name=="iota" || name=="kappa"
296 || name=="lambda" || name=="mu" || name=="nu"
297 || name=="xi" || name=="omicron" || name=="pi"
298 || name=="varpi" || name=="rho" || name=="varrho"
299 || name=="sigma" || name=="varsigma" || name=="tau"
300 || name=="upsilon" || name=="phi" || name=="varphi"
301 || name=="chi" || name=="psi" || name=="omega"
302 || name=="Gamma" || name=="Delta" || name=="Theta"
303 || name=="Lambda" || name=="Xi" || name=="Pi"
304 || name=="Sigma" || name=="Upsilon" || name=="Phi"
305 || name=="Psi" || name=="Omega")
312 // static member variables
317 unsigned symbol::next_serial = 0;