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(symbol, basic)
38 // default constructor
42 : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(autoname_prefix() + ToString(serial)), TeX_name(name)
44 setflag(status_flags::evaluated | status_flags::expanded);
53 symbol::symbol(const std::string & initname)
54 : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(default_TeX_name())
56 setflag(status_flags::evaluated | status_flags::expanded);
59 symbol::symbol(const std::string & initname, const std::string & texname)
60 : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(texname)
62 setflag(status_flags::evaluated | status_flags::expanded);
69 /** Construct object from archive_node. */
70 symbol::symbol(const archive_node &n, lst &sym_lst)
71 : inherited(n, sym_lst), asexinfop(new assigned_ex_info), serial(next_serial++)
73 if (!(n.find_string("name", name)))
74 name = autoname_prefix() + ToString(serial);
75 if (!(n.find_string("TeXname", TeX_name)))
76 TeX_name = default_TeX_name();
77 setflag(status_flags::evaluated);
80 /** Unarchive the object. */
81 ex symbol::unarchive(const archive_node &n, lst &sym_lst)
83 ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
85 // If symbol is in sym_lst, return the existing symbol
86 for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
87 if (is_a<symbol>(*it) && (ex_to<symbol>(*it).name == ex_to<symbol>(s).name))
91 // Otherwise add new symbol to list and return it
96 /** Archive the object. */
97 void symbol::archive(archive_node &n) const
99 inherited::archive(n);
100 n.add_string("name", name);
101 if (TeX_name != default_TeX_name())
102 n.add_string("TeX_name", TeX_name);
106 // functions overriding virtual functions from base classes
111 void symbol::print(const print_context & c, unsigned level) const
113 if (is_a<print_tree>(c)) {
115 c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
116 << ", serial=" << serial
117 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
120 } else if (is_a<print_latex>(c)) {
122 } else if (is_a<print_python_repr>(c)) {
123 c.s << class_name() << "('" << name;
124 if (TeX_name != default_TeX_name())
125 c.s << "','" << TeX_name;
131 bool symbol::info(unsigned inf) const
133 if (inf==info_flags::symbol) return true;
134 if (inf==info_flags::polynomial ||
135 inf==info_flags::integer_polynomial ||
136 inf==info_flags::cinteger_polynomial ||
137 inf==info_flags::rational_polynomial ||
138 inf==info_flags::crational_polynomial ||
139 inf==info_flags::rational_function)
142 return inherited::info(inf);
145 ex symbol::eval(int level) const
147 if (level == -max_recursion_level)
148 throw(std::runtime_error("max recursion level reached"));
150 if (asexinfop->is_assigned) {
151 setflag(status_flags::evaluated);
153 return (asexinfop->assigned_expression);
155 return (asexinfop->assigned_expression).eval(level);
163 /** Implementation of ex::diff() for single differentiation of a symbol.
167 ex symbol::derivative(const symbol & s) const
169 if (compare_same_type(s))
175 int symbol::compare_same_type(const basic & other) const
177 GINAC_ASSERT(is_a<symbol>(other));
178 const symbol *o = static_cast<const symbol *>(&other);
179 if (serial==o->serial) return 0;
180 return serial < o->serial ? -1 : 1;
183 bool symbol::is_equal_same_type(const basic & other) const
185 GINAC_ASSERT(is_a<symbol>(other));
186 const symbol *o = static_cast<const symbol *>(&other);
187 return serial==o->serial;
190 unsigned symbol::calchash() const
192 hashvalue = golden_ratio_hash(tinfo() ^ serial);
193 setflag(status_flags::hash_calculated);
198 // virtual functions which can be overridden by derived classes
204 // non-virtual functions in this class
209 void symbol::assign(const ex & value)
211 asexinfop->is_assigned = true;
212 asexinfop->assigned_expression = value;
213 clearflag(status_flags::evaluated | status_flags::expanded);
216 void symbol::unassign()
218 if (asexinfop->is_assigned) {
219 asexinfop->is_assigned = false;
220 asexinfop->assigned_expression = _ex0;
222 setflag(status_flags::evaluated | status_flags::expanded);
227 /** Symbols not constructed with a string get one assigned using this
228 * prefix and a number. */
229 std::string & symbol::autoname_prefix()
231 static std::string *s = new std::string("symbol");
235 /** Return default TeX name for symbol. This recognizes some greek letters. */
236 std::string symbol::default_TeX_name() const
238 if (name=="alpha" || name=="beta" || name=="gamma"
239 || name=="delta" || name=="epsilon" || name=="varepsilon"
240 || name=="zeta" || name=="eta" || name=="theta"
241 || name=="vartheta" || name=="iota" || name=="kappa"
242 || name=="lambda" || name=="mu" || name=="nu"
243 || name=="xi" || name=="omicron" || name=="pi"
244 || name=="varpi" || name=="rho" || name=="varrho"
245 || name=="sigma" || name=="varsigma" || name=="tau"
246 || name=="upsilon" || name=="phi" || name=="varphi"
247 || name=="chi" || name=="psi" || name=="omega"
248 || name=="Gamma" || name=="Delta" || name=="Theta"
249 || name=="Lambda" || name=="Xi" || name=="Pi"
250 || name=="Sigma" || name=="Upsilon" || name=="Phi"
251 || name=="Psi" || name=="Omega")
258 // static member variables
263 unsigned symbol::next_serial = 0;
266 // subclass assigned_ex_info
269 /** Default ctor. Defaults to unassigned. */
270 symbol::assigned_ex_info::assigned_ex_info() : is_assigned(false)