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
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
46 : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(autoname_prefix() + ToString(serial)), TeX_name(name)
48 setflag(status_flags::evaluated | status_flags::expanded);
57 symbol::symbol(const std::string & initname)
58 : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(default_TeX_name())
60 setflag(status_flags::evaluated | status_flags::expanded);
63 symbol::symbol(const std::string & initname, const std::string & texname)
64 : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(texname)
66 setflag(status_flags::evaluated | status_flags::expanded);
73 /** Construct object from archive_node. */
74 symbol::symbol(const archive_node &n, lst &sym_lst)
75 : inherited(n, sym_lst), asexinfop(new assigned_ex_info), serial(next_serial++)
77 if (!(n.find_string("name", name)))
78 name = autoname_prefix() + ToString(serial);
79 if (!(n.find_string("TeXname", TeX_name)))
80 TeX_name = default_TeX_name();
81 setflag(status_flags::evaluated);
84 /** Unarchive the object. */
85 ex symbol::unarchive(const archive_node &n, lst &sym_lst)
87 ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
89 // If symbol is in sym_lst, return the existing symbol
90 for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
91 if (is_a<symbol>(*it) && (ex_to<symbol>(*it).name == ex_to<symbol>(s).name))
95 // Otherwise add new symbol to list and return it
100 /** Archive the object. */
101 void symbol::archive(archive_node &n) const
103 inherited::archive(n);
104 n.add_string("name", name);
105 if (TeX_name != default_TeX_name())
106 n.add_string("TeX_name", TeX_name);
110 // functions overriding virtual functions from base classes
115 void symbol::do_print(const print_context & c, unsigned level) const
120 void symbol::do_print_latex(const print_latex & c, unsigned level) const
125 void symbol::do_print_tree(const print_tree & c, unsigned level) const
127 c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
128 << ", serial=" << serial
129 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
133 void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
135 c.s << class_name() << "('" << name;
136 if (TeX_name != default_TeX_name())
137 c.s << "','" << TeX_name;
141 bool symbol::info(unsigned inf) const
143 if (inf==info_flags::symbol) return true;
144 if (inf==info_flags::polynomial ||
145 inf==info_flags::integer_polynomial ||
146 inf==info_flags::cinteger_polynomial ||
147 inf==info_flags::rational_polynomial ||
148 inf==info_flags::crational_polynomial ||
149 inf==info_flags::rational_function)
152 return inherited::info(inf);
155 ex symbol::eval(int level) const
157 if (level == -max_recursion_level)
158 throw(std::runtime_error("max recursion level reached"));
160 if (asexinfop->is_assigned) {
161 setflag(status_flags::evaluated);
163 return (asexinfop->assigned_expression);
165 return (asexinfop->assigned_expression).eval(level);
173 /** Implementation of ex::diff() for single differentiation of a symbol.
177 ex symbol::derivative(const symbol & s) const
179 if (compare_same_type(s))
185 int symbol::compare_same_type(const basic & other) const
187 GINAC_ASSERT(is_a<symbol>(other));
188 const symbol *o = static_cast<const symbol *>(&other);
189 if (serial==o->serial) return 0;
190 return serial < o->serial ? -1 : 1;
193 bool symbol::is_equal_same_type(const basic & other) const
195 GINAC_ASSERT(is_a<symbol>(other));
196 const symbol *o = static_cast<const symbol *>(&other);
197 return serial==o->serial;
200 unsigned symbol::calchash() const
202 hashvalue = golden_ratio_hash(tinfo() ^ serial);
203 setflag(status_flags::hash_calculated);
208 // virtual functions which can be overridden by derived classes
214 // non-virtual functions in this class
219 void symbol::assign(const ex & value)
221 asexinfop->is_assigned = true;
222 asexinfop->assigned_expression = value;
223 clearflag(status_flags::evaluated | status_flags::expanded);
226 void symbol::unassign()
228 if (asexinfop->is_assigned) {
229 asexinfop->is_assigned = false;
230 asexinfop->assigned_expression = _ex0;
232 setflag(status_flags::evaluated | status_flags::expanded);
237 /** Symbols not constructed with a string get one assigned using this
238 * prefix and a number. */
239 std::string & symbol::autoname_prefix()
241 static std::string *s = new std::string("symbol");
245 /** Return default TeX name for symbol. This recognizes some greek letters. */
246 std::string symbol::default_TeX_name() const
248 if (name=="alpha" || name=="beta" || name=="gamma"
249 || name=="delta" || name=="epsilon" || name=="varepsilon"
250 || name=="zeta" || name=="eta" || name=="theta"
251 || name=="vartheta" || name=="iota" || name=="kappa"
252 || name=="lambda" || name=="mu" || name=="nu"
253 || name=="xi" || name=="omicron" || name=="pi"
254 || name=="varpi" || name=="rho" || name=="varrho"
255 || name=="sigma" || name=="varsigma" || name=="tau"
256 || name=="upsilon" || name=="phi" || name=="varphi"
257 || name=="chi" || name=="psi" || name=="omega"
258 || name=="Gamma" || name=="Delta" || name=="Theta"
259 || name=="Lambda" || name=="Xi" || name=="Pi"
260 || name=="Sigma" || name=="Upsilon" || name=="Phi"
261 || name=="Psi" || name=="Omega")
268 // static member variables
273 unsigned symbol::next_serial = 0;
276 // subclass assigned_ex_info
279 /** Default ctor. Defaults to unassigned. */
280 symbol::assigned_ex_info::assigned_ex_info() : is_assigned(false)