3 * Implementation of GiNaC's symbolic objects. */
6 * GiNaC Copyright (C) 1999-2003 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
34 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(symbol, basic,
35 print_func<print_context>(&symbol::do_print).
36 print_func<print_latex>(&symbol::do_print_latex).
37 print_func<print_tree>(&symbol::do_print_tree).
38 print_func<print_python_repr>(&symbol::do_print_python_repr))
41 // default constructor
45 : 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)
47 setflag(status_flags::evaluated | status_flags::expanded);
56 symbol::symbol(const std::string & initname)
57 : 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)
59 setflag(status_flags::evaluated | status_flags::expanded);
62 symbol::symbol(const std::string & initname, const std::string & texname)
63 : 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)
65 setflag(status_flags::evaluated | status_flags::expanded);
68 symbol::symbol(const std::string & initname, unsigned rt, unsigned rtt)
69 : 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)
71 setflag(status_flags::evaluated | status_flags::expanded);
74 symbol::symbol(const std::string & initname, const std::string & texname, unsigned rt, unsigned rtt)
75 : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(texname), ret_type(rt), ret_type_tinfo(rtt)
77 setflag(status_flags::evaluated | status_flags::expanded);
84 /** Construct object from archive_node. */
85 symbol::symbol(const archive_node &n, lst &sym_lst)
86 : inherited(n, sym_lst), asexinfop(new assigned_ex_info), serial(next_serial++), ret_type(return_types::commutative), ret_type_tinfo(TINFO_symbol)
88 if (!(n.find_string("name", name)))
89 name = autoname_prefix() + ToString(serial);
90 if (!(n.find_string("TeXname", TeX_name)))
91 TeX_name = default_TeX_name();
92 if (!n.find_unsigned("return_type", ret_type))
93 ret_type = return_types::commutative;
94 if (!n.find_unsigned("return_type_tinfo", ret_type_tinfo))
95 ret_type_tinfo = TINFO_symbol;
96 setflag(status_flags::evaluated | status_flags::expanded);
99 /** Unarchive the object. */
100 ex symbol::unarchive(const archive_node &n, lst &sym_lst)
102 ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
104 // If symbol is in sym_lst, return the existing symbol
105 for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
106 if (is_a<symbol>(*it) && (ex_to<symbol>(*it).name == ex_to<symbol>(s).name))
110 // Otherwise add new symbol to list and return it
115 /** Archive the object. */
116 void symbol::archive(archive_node &n) const
118 inherited::archive(n);
119 n.add_string("name", name);
120 if (TeX_name != default_TeX_name())
121 n.add_string("TeX_name", TeX_name);
122 if (ret_type != return_types::commutative)
123 n.add_unsigned("return_type", ret_type);
124 if (ret_type_tinfo != TINFO_symbol)
125 n.add_unsigned("return_type_tinfo", ret_type_tinfo);
129 // functions overriding virtual functions from base classes
134 void symbol::do_print(const print_context & c, unsigned level) const
139 void symbol::do_print_latex(const print_latex & c, unsigned level) const
144 void symbol::do_print_tree(const print_tree & c, unsigned level) const
146 c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
147 << ", serial=" << serial
148 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
152 void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
154 c.s << class_name() << "('" << name;
155 if (TeX_name != default_TeX_name())
156 c.s << "','" << TeX_name;
160 bool symbol::info(unsigned inf) const
162 if (inf==info_flags::symbol) return true;
163 if (inf==info_flags::polynomial ||
164 inf==info_flags::integer_polynomial ||
165 inf==info_flags::cinteger_polynomial ||
166 inf==info_flags::rational_polynomial ||
167 inf==info_flags::crational_polynomial ||
168 inf==info_flags::rational_function)
171 return inherited::info(inf);
174 ex symbol::eval(int level) const
176 if (level == -max_recursion_level)
177 throw(std::runtime_error("max recursion level reached"));
179 if (asexinfop->is_assigned) {
180 setflag(status_flags::evaluated);
182 return (asexinfop->assigned_expression);
184 return (asexinfop->assigned_expression).eval(level);
192 /** Implementation of ex::diff() for single differentiation of a symbol.
196 ex symbol::derivative(const symbol & s) const
198 if (compare_same_type(s))
204 int symbol::compare_same_type(const basic & other) const
206 GINAC_ASSERT(is_a<symbol>(other));
207 const symbol *o = static_cast<const symbol *>(&other);
208 if (serial==o->serial) return 0;
209 return serial < o->serial ? -1 : 1;
212 bool symbol::is_equal_same_type(const basic & other) const
214 GINAC_ASSERT(is_a<symbol>(other));
215 const symbol *o = static_cast<const symbol *>(&other);
216 return serial==o->serial;
219 unsigned symbol::calchash() const
221 hashvalue = golden_ratio_hash(tinfo() ^ serial);
222 setflag(status_flags::hash_calculated);
227 // virtual functions which can be overridden by derived classes
233 // non-virtual functions in this class
238 void symbol::assign(const ex & value)
240 asexinfop->is_assigned = true;
241 asexinfop->assigned_expression = value;
242 clearflag(status_flags::evaluated | status_flags::expanded);
245 void symbol::unassign()
247 if (asexinfop->is_assigned) {
248 asexinfop->is_assigned = false;
249 asexinfop->assigned_expression = _ex0;
251 setflag(status_flags::evaluated | status_flags::expanded);
256 /** Symbols not constructed with a string get one assigned using this
257 * prefix and a number. */
258 std::string & symbol::autoname_prefix()
260 static std::string *s = new std::string("symbol");
264 /** Return default TeX name for symbol. This recognizes some greek letters. */
265 std::string symbol::default_TeX_name() const
267 if (name=="alpha" || name=="beta" || name=="gamma"
268 || name=="delta" || name=="epsilon" || name=="varepsilon"
269 || name=="zeta" || name=="eta" || name=="theta"
270 || name=="vartheta" || name=="iota" || name=="kappa"
271 || name=="lambda" || name=="mu" || name=="nu"
272 || name=="xi" || name=="omicron" || name=="pi"
273 || name=="varpi" || name=="rho" || name=="varrho"
274 || name=="sigma" || name=="varsigma" || name=="tau"
275 || name=="upsilon" || name=="phi" || name=="varphi"
276 || name=="chi" || name=="psi" || name=="omega"
277 || name=="Gamma" || name=="Delta" || name=="Theta"
278 || name=="Lambda" || name=="Xi" || name=="Pi"
279 || name=="Sigma" || name=="Upsilon" || name=="Phi"
280 || name=="Psi" || name=="Omega")
287 // static member variables
292 unsigned symbol::next_serial = 0;
295 // subclass assigned_ex_info
298 /** Default ctor. Defaults to unassigned. */
299 symbol::assigned_ex_info::assigned_ex_info() throw() : is_assigned(false)