3 * Implementation of GiNaC's symbolic objects. */
6 * GiNaC Copyright (C) 1999-2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(autoname_prefix() + ToString(serial)), TeX_name(name), ret_type(return_types::commutative), ret_type_tinfo(TINFO_symbol), domain(domain::complex)
50 setflag(status_flags::evaluated | status_flags::expanded);
55 realsymbol::realsymbol()
57 domain = domain::real;
68 symbol::symbol(const std::string & initname, unsigned domain)
69 : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(default_TeX_name()), ret_type(return_types::commutative), ret_type_tinfo(TINFO_symbol), domain(domain)
71 setflag(status_flags::evaluated | status_flags::expanded);
74 symbol::symbol(const std::string & initname, const std::string & texname, unsigned domain)
75 : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(texname), ret_type(return_types::commutative), ret_type_tinfo(TINFO_symbol), domain(domain)
77 setflag(status_flags::evaluated | status_flags::expanded);
80 symbol::symbol(const std::string & initname, unsigned rt, unsigned rtt, unsigned domain)
81 : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(default_TeX_name()), ret_type(rt), ret_type_tinfo(rtt), domain(domain)
83 setflag(status_flags::evaluated | status_flags::expanded);
86 symbol::symbol(const std::string & initname, const std::string & texname, unsigned rt, unsigned rtt, unsigned domain)
87 : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(texname), ret_type(rt), ret_type_tinfo(rtt), domain(domain)
89 setflag(status_flags::evaluated | status_flags::expanded);
94 realsymbol::realsymbol(const std::string & initname, unsigned domain)
95 : symbol(initname, domain) { }
97 realsymbol::realsymbol(const std::string & initname, const std::string & texname, unsigned domain)
98 : symbol(initname, texname, domain) { }
100 realsymbol::realsymbol(const std::string & initname, unsigned rt, unsigned rtt, unsigned domain)
101 : symbol(initname, rt, rtt, domain) { }
103 realsymbol::realsymbol(const std::string & initname, const std::string & texname, unsigned rt, unsigned rtt, unsigned domain)
104 : symbol(initname, texname, rt, rtt, domain) { }
110 /** Construct object from archive_node. */
111 symbol::symbol(const archive_node &n, lst &sym_lst)
112 : inherited(n, sym_lst), asexinfop(new assigned_ex_info), serial(next_serial++)
114 if (!n.find_string("name", name))
115 name = autoname_prefix() + ToString(serial);
116 if (!n.find_string("TeXname", TeX_name))
117 TeX_name = default_TeX_name();
118 if (!n.find_unsigned("domain", domain))
119 domain = domain::complex;
120 if (!n.find_unsigned("return_type", ret_type))
121 ret_type = return_types::commutative;
122 if (!n.find_unsigned("return_type_tinfo", ret_type_tinfo))
123 ret_type_tinfo = TINFO_symbol;
124 setflag(status_flags::evaluated | status_flags::expanded);
127 /** Unarchive the object. */
128 ex symbol::unarchive(const archive_node &n, lst &sym_lst)
130 ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
132 // If symbol is in sym_lst, return the existing symbol
133 for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
134 if (is_a<symbol>(*it) && (ex_to<symbol>(*it).name == ex_to<symbol>(s).name))
138 // Otherwise add new symbol to list and return it
143 /** Archive the object. */
144 void symbol::archive(archive_node &n) const
146 inherited::archive(n);
147 n.add_string("name", name);
148 if (TeX_name != default_TeX_name())
149 n.add_string("TeX_name", TeX_name);
150 if (domain != domain::complex)
151 n.add_unsigned("domain", domain);
152 if (ret_type != return_types::commutative)
153 n.add_unsigned("return_type", ret_type);
154 if (ret_type_tinfo != TINFO_symbol)
155 n.add_unsigned("return_type_tinfo", ret_type_tinfo);
159 // functions overriding virtual functions from base classes
164 void symbol::do_print(const print_context & c, unsigned level) const
169 void symbol::do_print_latex(const print_latex & c, unsigned level) const
174 void symbol::do_print_tree(const print_tree & c, unsigned level) const
176 c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
177 << ", serial=" << serial
178 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
179 << ", domain=" << domain
183 void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
185 c.s << class_name() << "('" << name;
186 if (TeX_name != default_TeX_name())
187 c.s << "','" << TeX_name;
191 bool symbol::info(unsigned inf) const
193 if (inf == info_flags::symbol)
195 if (inf == info_flags::polynomial ||
196 inf == info_flags::integer_polynomial ||
197 inf == info_flags::cinteger_polynomial ||
198 inf == info_flags::rational_polynomial ||
199 inf == info_flags::crational_polynomial ||
200 inf == info_flags::rational_function)
202 if (inf == info_flags::real)
203 return domain == domain::real;
205 return inherited::info(inf);
208 ex symbol::eval(int level) const
210 if (level == -max_recursion_level)
211 throw(std::runtime_error("max recursion level reached"));
213 if (asexinfop->is_assigned) {
214 setflag(status_flags::evaluated);
216 return (asexinfop->assigned_expression);
218 return (asexinfop->assigned_expression).eval(level);
224 ex symbol::conjugate() const
226 if (this->domain == domain::complex) {
227 return GiNaC::conjugate_function(*this).hold();
235 /** Implementation of ex::diff() for single differentiation of a symbol.
239 ex symbol::derivative(const symbol & s) const
241 if (compare_same_type(s))
247 int symbol::compare_same_type(const basic & other) const
249 GINAC_ASSERT(is_a<symbol>(other));
250 const symbol *o = static_cast<const symbol *>(&other);
251 if (serial==o->serial) return 0;
252 return serial < o->serial ? -1 : 1;
255 bool symbol::is_equal_same_type(const basic & other) const
257 GINAC_ASSERT(is_a<symbol>(other));
258 const symbol *o = static_cast<const symbol *>(&other);
259 return serial==o->serial;
262 unsigned symbol::calchash() const
264 hashvalue = golden_ratio_hash(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 void symbol::assign(const ex & value)
283 asexinfop->is_assigned = true;
284 asexinfop->assigned_expression = value;
285 clearflag(status_flags::evaluated | status_flags::expanded);
288 void symbol::unassign()
290 if (asexinfop->is_assigned) {
291 asexinfop->is_assigned = false;
292 asexinfop->assigned_expression = _ex0;
294 setflag(status_flags::evaluated | status_flags::expanded);
299 /** Symbols not constructed with a string get one assigned using this
300 * prefix and a number. */
301 std::string & symbol::autoname_prefix()
303 static std::string *s = new std::string("symbol");
307 /** Return default TeX name for symbol. This recognizes some greek letters. */
308 std::string symbol::default_TeX_name() const
310 if (name=="alpha" || name=="beta" || name=="gamma"
311 || name=="delta" || name=="epsilon" || name=="varepsilon"
312 || name=="zeta" || name=="eta" || name=="theta"
313 || name=="vartheta" || name=="iota" || name=="kappa"
314 || name=="lambda" || name=="mu" || name=="nu"
315 || name=="xi" || name=="omicron" || name=="pi"
316 || name=="varpi" || name=="rho" || name=="varrho"
317 || name=="sigma" || name=="varsigma" || name=="tau"
318 || name=="upsilon" || name=="phi" || name=="varphi"
319 || name=="chi" || name=="psi" || name=="omega"
320 || name=="Gamma" || name=="Delta" || name=="Theta"
321 || name=="Lambda" || name=="Xi" || name=="Pi"
322 || name=="Sigma" || name=="Upsilon" || name=="Phi"
323 || name=="Psi" || name=="Omega")
330 // static member variables
335 unsigned symbol::next_serial = 0;
338 // subclass assigned_ex_info
341 /** Default ctor. Defaults to unassigned. */
342 symbol::assigned_ex_info::assigned_ex_info() throw() : is_assigned(false)