X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fsymbol.cpp;h=4dd0691c648ac435b4ef60cbaf1074f6fed28ff5;hp=480c25c453fc8f648548cbded62cabeece80703a;hb=1b8bcb068171ce9d5c8202ae3c76647b65c9a06d;hpb=6eedac996204ebc3355807d7f887b0c366cb3c07 diff --git a/ginac/symbol.cpp b/ginac/symbol.cpp index 480c25c4..4dd0691c 100644 --- a/ginac/symbol.cpp +++ b/ginac/symbol.cpp @@ -3,7 +3,7 @@ * Implementation of GiNaC's symbolic objects. */ /* - * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany + * GiNaC Copyright (C) 1999-2015 Johannes Gutenberg University Mainz, Germany * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,17 +17,20 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include -#include - #include "symbol.h" #include "lst.h" #include "archive.h" #include "tostring.h" #include "utils.h" +#include "hash_seed.h" +#include "inifcns.h" + +#include +#include +#include namespace GiNaC { @@ -41,104 +44,137 @@ GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(symbol, basic, // default constructor ////////// -symbol::symbol() - : 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) +// symbol + +symbol::symbol() : serial(next_serial++), name(""), TeX_name("") { setflag(status_flags::evaluated | status_flags::expanded); } +// realsymbol + +realsymbol::realsymbol() : symbol() { } + +// possymbol + +possymbol::possymbol() : realsymbol() { } + ////////// // other constructors ////////// // public -symbol::symbol(const std::string & initname) - : 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) -{ - setflag(status_flags::evaluated | status_flags::expanded); -} +// symbol -symbol::symbol(const std::string & initname, const std::string & texname) - : 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) +symbol::symbol(const std::string & initname) : serial(next_serial++), + name(initname), TeX_name("") { setflag(status_flags::evaluated | status_flags::expanded); } -symbol::symbol(const std::string & initname, unsigned rt, unsigned rtt) - : 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) +symbol::symbol(const std::string & initname, const std::string & texname) : + serial(next_serial++), name(initname), TeX_name(texname) { setflag(status_flags::evaluated | status_flags::expanded); } -symbol::symbol(const std::string & initname, const std::string & texname, unsigned rt, unsigned rtt) - : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(texname), ret_type(rt), ret_type_tinfo(rtt) -{ - setflag(status_flags::evaluated | status_flags::expanded); -} +// realsymbol + +realsymbol::realsymbol(const std::string & initname) : symbol(initname) { } + +realsymbol::realsymbol(const std::string & initname, const std::string & texname) + : symbol(initname, texname) { } + +// possymbol + +possymbol::possymbol(const std::string & initname) : realsymbol(initname) { } + +possymbol::possymbol(const std::string & initname, const std::string & texname) + : realsymbol(initname, texname) { } ////////// // archiving ////////// -/** Construct object from archive_node. */ -symbol::symbol(const archive_node &n, lst &sym_lst) - : inherited(n, sym_lst), asexinfop(new assigned_ex_info), serial(next_serial++), ret_type(return_types::commutative), ret_type_tinfo(TINFO_symbol) +/** Read object from archive_node. */ +void symbol::read_archive(const archive_node &n, lst &sym_lst) { - if (!(n.find_string("name", name))) - name = autoname_prefix() + ToString(serial); - if (!(n.find_string("TeXname", TeX_name))) - TeX_name = default_TeX_name(); - if (!n.find_unsigned("return_type", ret_type)) - ret_type = return_types::commutative; - if (!n.find_unsigned("return_type_tinfo", ret_type_tinfo)) - ret_type_tinfo = TINFO_symbol; - setflag(status_flags::evaluated | status_flags::expanded); -} - -/** Unarchive the object. */ -ex symbol::unarchive(const archive_node &n, lst &sym_lst) -{ - ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated); + inherited::read_archive(n, sym_lst); + serial = next_serial++; + std::string tmp_name; + n.find_string("name", tmp_name); // If symbol is in sym_lst, return the existing symbol - for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) { - if (is_a(*it) && (ex_to(*it).name == ex_to(s).name)) - return *it; + for (auto & s : sym_lst) { + if (is_a(s) && (ex_to(s).name == tmp_name)) { + *this = ex_to(s); + // XXX: This method is responsible for reading realsymbol + // and possymbol objects too. But + // basic::operator=(const basic& other) + // resets status_flags::evaluated if other and *this are + // of different types. Usually this is a good idea, but + // doing this for symbols is wrong (for one, nothing is + // going to set status_flags::evaluated, evaluation will + // loop forever). Therefore we need to restore flags. + setflag(status_flags::evaluated | status_flags::expanded); + return; + } } + name = tmp_name; + if (!n.find_string("TeXname", TeX_name)) + TeX_name = std::string(""); + setflag(status_flags::evaluated | status_flags::expanded); - // Otherwise add new symbol to list and return it - sym_lst.append(s); - return s; + setflag(status_flags::dynallocated); + sym_lst.append(*this); } /** Archive the object. */ void symbol::archive(archive_node &n) const { inherited::archive(n); - n.add_string("name", name); - if (TeX_name != default_TeX_name()) + // XXX: we should not archive anonymous symbols. + if (!name.empty()) + n.add_string("name", name); + if (!TeX_name.empty()) n.add_string("TeX_name", TeX_name); - if (ret_type != return_types::commutative) - n.add_unsigned("return_type", ret_type); - if (ret_type_tinfo != TINFO_symbol) - n.add_unsigned("return_type_tinfo", ret_type_tinfo); } ////////// // functions overriding virtual functions from base classes ////////// +/** Return default TeX name for symbol. This recognizes some greek letters. */ +static const std::string& get_default_TeX_name(const std::string& name); + // public +std::string symbol::get_name() const +{ + if (name.empty()) { + std::ostringstream s; + s << "symbol" << serial; + name = s.str(); + } + return name; +} + +// protected + void symbol::do_print(const print_context & c, unsigned level) const { - c.s << name; + c.s << get_name(); } void symbol::do_print_latex(const print_latex & c, unsigned level) const { - c.s << TeX_name; + if (!TeX_name.empty()) + c.s << TeX_name; + else if (!name.empty()) + c.s << get_default_TeX_name(name); + else + c.s << "symbol" << serial; } void symbol::do_print_tree(const print_tree & c, unsigned level) const @@ -146,45 +182,63 @@ void symbol::do_print_tree(const print_tree & c, unsigned level) const c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this << ", serial=" << serial << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec + << ", domain=" << get_domain() << std::endl; } void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const { - c.s << class_name() << "('" << name; - if (TeX_name != default_TeX_name()) + c.s << class_name() << "('"; + if (!name.empty()) + c.s << name; + else + c.s << "symbol" << serial; + if (!TeX_name.empty()) c.s << "','" << TeX_name; c.s << "')"; } bool symbol::info(unsigned inf) const { - if (inf==info_flags::symbol) return true; - if (inf==info_flags::polynomial || - inf==info_flags::integer_polynomial || - inf==info_flags::cinteger_polynomial || - inf==info_flags::rational_polynomial || - inf==info_flags::crational_polynomial || - inf==info_flags::rational_function) - return true; - else - return inherited::info(inf); + switch (inf) { + case info_flags::symbol: + case info_flags::polynomial: + case info_flags::integer_polynomial: + case info_flags::cinteger_polynomial: + case info_flags::rational_polynomial: + case info_flags::crational_polynomial: + case info_flags::rational_function: + case info_flags::expanded: + return true; + case info_flags::real: + return get_domain() == domain::real || get_domain() == domain::positive; + case info_flags::positive: + case info_flags::nonnegative: + return get_domain() == domain::positive; + case info_flags::has_indices: + return false; + } + return inherited::info(inf); } -ex symbol::eval(int level) const +ex symbol::conjugate() const { - if (level == -max_recursion_level) - throw(std::runtime_error("max recursion level reached")); - - if (asexinfop->is_assigned) { - setflag(status_flags::evaluated); - if (level==1) - return (asexinfop->assigned_expression); - else - return (asexinfop->assigned_expression).eval(level); - } else { - return this->hold(); - } + return conjugate_function(*this).hold(); +} + +ex symbol::real_part() const +{ + return real_part_function(*this).hold(); +} + +ex symbol::imag_part() const +{ + return imag_part_function(*this).hold(); +} + +bool symbol::is_polynomial(const ex & var) const +{ + return true; } // protected @@ -218,7 +272,8 @@ bool symbol::is_equal_same_type(const basic & other) const unsigned symbol::calchash() const { - hashvalue = golden_ratio_hash(tinfo() ^ serial); + unsigned seed = make_hash_seed(typeid(*this)); + hashvalue = golden_ratio_hash(seed ^ serial); setflag(status_flags::hash_calculated); return hashvalue; } @@ -233,56 +288,66 @@ unsigned symbol::calchash() const // non-virtual functions in this class ////////// -// public - -void symbol::assign(const ex & value) -{ - asexinfop->is_assigned = true; - asexinfop->assigned_expression = value; - clearflag(status_flags::evaluated | status_flags::expanded); -} - -void symbol::unassign() -{ - if (asexinfop->is_assigned) { - asexinfop->is_assigned = false; - asexinfop->assigned_expression = _ex0; - } - setflag(status_flags::evaluated | status_flags::expanded); -} - -// private - -/** Symbols not constructed with a string get one assigned using this - * prefix and a number. */ -std::string & symbol::autoname_prefix() -{ - static std::string *s = new std::string("symbol"); - return *s; -} - /** Return default TeX name for symbol. This recognizes some greek letters. */ -std::string symbol::default_TeX_name() const +static const std::string& get_default_TeX_name(const std::string& name) { - if (name=="alpha" || name=="beta" || name=="gamma" - || name=="delta" || name=="epsilon" || name=="varepsilon" - || name=="zeta" || name=="eta" || name=="theta" - || name=="vartheta" || name=="iota" || name=="kappa" - || name=="lambda" || name=="mu" || name=="nu" - || name=="xi" || name=="omicron" || name=="pi" - || name=="varpi" || name=="rho" || name=="varrho" - || name=="sigma" || name=="varsigma" || name=="tau" - || name=="upsilon" || name=="phi" || name=="varphi" - || name=="chi" || name=="psi" || name=="omega" - || name=="Gamma" || name=="Delta" || name=="Theta" - || name=="Lambda" || name=="Xi" || name=="Pi" - || name=="Sigma" || name=="Upsilon" || name=="Phi" - || name=="Psi" || name=="Omega") - return "\\" + name; + static std::map standard_names; + static bool names_initialized = false; + if (!names_initialized) { + standard_names["alpha"] = std::string("\\alpha"); + standard_names["beta"] = std::string("\\beta");; + standard_names["gamma"] = std::string("\\gamma");; + standard_names["delta"] = std::string("\\delta");; + standard_names["epsilon"] = std::string("\\epsilon"); + standard_names["varepsilon"] = std::string("\\varepsilon"); + standard_names["zeta"] = std::string("\\zeta"); + standard_names["eta" ] = std::string("\\eta" ); + standard_names["theta"] = std::string("\\theta"); + standard_names["vartheta"] = std::string("\\vartheta"); + standard_names["iota"] = std::string("\\iota"); + standard_names["kappa"] = std::string("\\kappa"); + standard_names["lambda"] = std::string("\\lambda"); + standard_names["mu"] = std::string("\\mu"); + standard_names["nu"] = std::string("\\nu"); + standard_names["xi"] = std::string("\\xi"); + standard_names["omicron"] = std::string("\\omicron"); + standard_names["pi"] = std::string("\\pi"); + standard_names["varpi"] = std::string("\\varpi"); + standard_names["rho"] = std::string("\\rho"); + standard_names["varrho"] = std::string("\\varrho"); + standard_names["sigma"] = std::string("\\sigma"); + standard_names["varsigma"] = std::string("\\varsigma"); + standard_names["tau"] = std::string("\\tau"); + standard_names["upsilon"] = std::string("\\upsilon"); + standard_names["phi"] = std::string("\\phi"); + standard_names["varphi"] = std::string("\\varphi"); + standard_names["chi"] = std::string("\\chi"); + standard_names["psi"] = std::string("\\psi"); + standard_names["omega"] = std::string("\\omega"); + standard_names["Gamma"] = std::string("\\Gamma"); + standard_names["Delta"] = std::string("\\Delta"); + standard_names["Theta"] = std::string("\\Theta"); + standard_names["Lambda"] = std::string("\\Lambda"); + standard_names["Xi"] = std::string("\\Xi"); + standard_names["Pi"] = std::string("\\Pi"); + standard_names["Sigma"] = std::string("\\Sigma"); + standard_names["Upsilon"] = std::string("\\Upsilon"); + standard_names["Phi"] = std::string("\\Phi"); + standard_names["Psi"] = std::string("\\Psi"); + standard_names["Omega"] = std::string("\\Omega"); + names_initialized = true; + } + std::map::const_iterator it = standard_names.find(name); + if (it != standard_names.end()) + return it->second; else return name; } +GINAC_BIND_UNARCHIVER(symbol); +GINAC_BIND_UNARCHIVER(realsymbol); +GINAC_BIND_UNARCHIVER(possymbol); + ////////// // static member variables ////////// @@ -291,13 +356,4 @@ std::string symbol::default_TeX_name() const unsigned symbol::next_serial = 0; -////////// -// subclass assigned_ex_info -////////// - -/** Default ctor. Defaults to unassigned. */ -symbol::assigned_ex_info::assigned_ex_info() throw() : is_assigned(false) -{ -} - } // namespace GiNaC