* Implementation of GiNaC's symbolic objects. */
/*
- * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
+ * GiNaC Copyright (C) 1999-2008 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
*
* 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 <string>
#include "symbol.h"
#include "lst.h"
+#include "archive.h"
+#include "tostring.h"
#include "utils.h"
-#include "idx.h"
+#include "inifcns.h"
+
+namespace GiNaC {
+
+GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(symbol, basic,
+ print_func<print_context>(&symbol::do_print).
+ print_func<print_latex>(&symbol::do_print_latex).
+ print_func<print_tree>(&symbol::do_print_tree).
+ print_func<print_python_repr>(&symbol::do_print_python_repr))
//////////
-// default constructor, destructor, copy constructor assignment operator and helpers
+// default constructor
//////////
-symbol::symbol() : basic(TINFO_symbol)
+// symbol
+
+symbol::symbol()
+ : inherited(&symbol::tinfo_static), serial(next_serial++), name(autoname_prefix() + ToString(serial)), TeX_name(name), domain(domain::complex), ret_type(return_types::commutative), ret_type_tinfo(&symbol::tinfo_static)
+{
+ setflag(status_flags::evaluated | status_flags::expanded);
+}
+
+// realsymbol
+
+realsymbol::realsymbol()
{
- debugmsg("symbol default constructor",LOGLEVEL_CONSTRUCT);
- serial=next_serial++;
- name=autoname_prefix()+ToString(serial);
- asexinfop=new assigned_ex_info;
- setflag(status_flags::evaluated);
+ domain = domain::real;
}
-symbol::~symbol()
+// possymbol
+
+possymbol::possymbol()
+{
+ domain = domain::positive;
+}
+
+//////////
+// other constructors
+//////////
+
+// public
+
+// symbol
+
+symbol::symbol(const std::string & initname, unsigned domain)
+ : inherited(&symbol::tinfo_static), serial(next_serial++), name(initname), TeX_name(default_TeX_name()), domain(domain), ret_type(return_types::commutative), ret_type_tinfo(&symbol::tinfo_static)
{
- debugmsg("symbol destructor",LOGLEVEL_DESTRUCT);
- destroy(0);
+ setflag(status_flags::evaluated | status_flags::expanded);
}
-symbol::symbol(symbol const & other)
+symbol::symbol(const std::string & initname, unsigned rt, tinfo_t rtt, unsigned domain)
+ : inherited(&symbol::tinfo_static), serial(next_serial++), name(initname), TeX_name(default_TeX_name()), domain(domain), ret_type(rt), ret_type_tinfo(rtt)
{
- debugmsg("symbol copy constructor",LOGLEVEL_CONSTRUCT);
- copy(other);
+ setflag(status_flags::evaluated | status_flags::expanded);
}
-void symbol::copy(symbol const & other)
+symbol::symbol(const std::string & initname, const std::string & texname, unsigned domain)
+ : inherited(&symbol::tinfo_static), serial(next_serial++), name(initname), TeX_name(texname), domain(domain), ret_type(return_types::commutative), ret_type_tinfo(&symbol::tinfo_static)
{
- basic::copy(other);
- name=other.name;
- serial=other.serial;
- asexinfop=other.asexinfop;
- ++asexinfop->refcount;
+ setflag(status_flags::evaluated | status_flags::expanded);
}
-void symbol::destroy(bool call_parent)
+symbol::symbol(const std::string & initname, const std::string & texname, unsigned rt, tinfo_t rtt, unsigned domain)
+ : inherited(&symbol::tinfo_static), serial(next_serial++), name(initname), TeX_name(texname), domain(domain), ret_type(rt), ret_type_tinfo(rtt)
{
- if (--asexinfop->refcount == 0) {
- delete asexinfop;
- }
- if (call_parent) {
- basic::destroy(call_parent);
- }
+ setflag(status_flags::evaluated | status_flags::expanded);
}
-// how should the following be interpreted?
-// symbol x;
-// symbol y;
-// x=y;
-// probably as: x=ex(y);
+// realsymbol
+
+realsymbol::realsymbol(const std::string & initname, unsigned domain)
+ : symbol(initname, domain) { }
+
+realsymbol::realsymbol(const std::string & initname, const std::string & texname, unsigned domain)
+ : symbol(initname, texname, domain) { }
+
+realsymbol::realsymbol(const std::string & initname, unsigned rt, tinfo_t rtt, unsigned domain)
+ : symbol(initname, rt, rtt, domain) { }
+
+realsymbol::realsymbol(const std::string & initname, const std::string & texname, unsigned rt, tinfo_t rtt, unsigned domain)
+ : symbol(initname, texname, rt, rtt, domain) { }
+
+// possymbol
+
+possymbol::possymbol(const std::string & initname, unsigned domain)
+ : symbol(initname, domain) { }
+
+possymbol::possymbol(const std::string & initname, const std::string & texname, unsigned domain)
+ : symbol(initname, texname, domain) { }
+
+possymbol::possymbol(const std::string & initname, unsigned rt, tinfo_t rtt, unsigned domain)
+ : symbol(initname, rt, rtt, domain) { }
+
+possymbol::possymbol(const std::string & initname, const std::string & texname, unsigned rt, tinfo_t rtt, unsigned domain)
+ : symbol(initname, texname, rt, rtt, domain) { }
//////////
-// other constructors
+// archiving
//////////
-// public
+/** Construct object from archive_node. */
+symbol::symbol(const archive_node &n, lst &sym_lst)
+ : inherited(n, sym_lst), serial(next_serial++)
+{
+ 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("domain", domain))
+ domain = domain::complex;
+ if (!n.find_unsigned("return_type", ret_type))
+ ret_type = return_types::commutative;
+ setflag(status_flags::evaluated | status_flags::expanded);
+}
-symbol::symbol(string const & initname) : basic(TINFO_symbol)
+/** Unarchive the object. */
+ex symbol::unarchive(const archive_node &n, lst &sym_lst)
{
- debugmsg("symbol constructor from string",LOGLEVEL_CONSTRUCT);
- name=initname;
- serial=next_serial++;
- asexinfop=new assigned_ex_info;
- setflag(status_flags::evaluated);
+ ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
+
+ // 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<symbol>(*it) && (ex_to<symbol>(*it).name == ex_to<symbol>(s).name))
+ return *it;
+ }
+
+ // Otherwise add new symbol to list and return it
+ sym_lst.append(s);
+ return s;
+}
+
+/** Archive the object. */
+void symbol::archive(archive_node &n) const
+{
+ inherited::archive(n);
+ n.add_string("name", name);
+ if (TeX_name != default_TeX_name())
+ n.add_string("TeX_name", TeX_name);
+ if (domain != domain::complex)
+ n.add_unsigned("domain", domain);
+ if (ret_type != return_types::commutative)
+ n.add_unsigned("return_type", ret_type);
}
//////////
-// functions overriding virtual functions from bases classes
+// functions overriding virtual functions from base classes
//////////
// public
-basic * symbol::duplicate() const
+void symbol::do_print(const print_context & c, unsigned level) const
{
- debugmsg("symbol duplicate",LOGLEVEL_DUPLICATE);
- return new symbol(*this);
+ c.s << name;
}
-bool symbol::info(unsigned inf) const
+void symbol::do_print_latex(const print_latex & c, unsigned level) const
{
- if (inf==info_flags::symbol) return true;
- if (inf==info_flags::polynomial || inf==info_flags::integer_polynomial || inf==info_flags::rational_polynomial || inf==info_flags::rational_function) {
- return true;
- } else {
- return basic::info(inf);
- }
+ c.s << TeX_name;
}
-ex symbol::expand(unsigned options) const
+void symbol::do_print_tree(const print_tree & c, unsigned level) const
{
- return this->hold();
+ c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
+ << ", serial=" << serial
+ << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
+ << ", domain=" << domain
+ << std::endl;
}
-bool symbol::has(ex const & other) const
+void symbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
{
- if (is_equal(*other.bp)) return true;
- return false;
+ c.s << class_name() << "('" << name;
+ if (TeX_name != default_TeX_name())
+ c.s << "','" << TeX_name;
+ c.s << "')";
}
-int symbol::degree(symbol const & s) const
+bool symbol::info(unsigned inf) const
{
- return compare_same_type(s)==0 ? 1 : 0;
+ 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 domain == domain::real || domain == domain::positive;
+ case info_flags::positive:
+ case info_flags::nonnegative:
+ return domain == domain::positive;
+ case info_flags::has_indices:
+ return false;
+ }
+ return inherited::info(inf);
}
-int symbol::ldegree(symbol const & s) const
+ex symbol::conjugate() const
{
- return compare_same_type(s)==0 ? 1 : 0;
+ if (this->domain == domain::complex) {
+ return conjugate_function(*this).hold();
+ } else {
+ return *this;
+ }
}
-ex symbol::coeff(symbol const & s, int const n) const
+ex symbol::real_part() const
{
- if (compare_same_type(s)==0) {
- return n==1 ? exONE() : exZERO();
- } else {
- return n==0 ? *this : exZERO();
- }
+ if (domain==domain::real || domain==domain::positive)
+ return *this;
+ return real_part_function(*this).hold();
}
-ex symbol::eval(int level) const
+ex symbol::imag_part() 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();
- }
+ if (domain==domain::real || domain==domain::positive)
+ return 0;
+ return imag_part_function(*this).hold();
}
-ex symbol::subs(lst const & ls, lst const & lr) const
+bool symbol::is_polynomial(const ex & var) const
{
- ASSERT(ls.nops()==lr.nops());
-#ifdef DOASSERT
- for (int i=0; i<ls.nops(); i++) {
- ASSERT(is_ex_exactly_of_type(ls.op(i),symbol)||
- is_ex_of_type(ls.op(i),idx));
- }
-#endif // def DOASSERT
-
- for (int i=0; i<ls.nops(); i++) {
- if (is_ex_exactly_of_type(ls.op(i),symbol)) {
- if (compare_same_type(ex_to_symbol(ls.op(i)))==0) return lr.op(i);
- }
- }
- return *this;
+ return true;
}
// protected
-int symbol::compare_same_type(basic const & other) const
+/** Implementation of ex::diff() for single differentiation of a symbol.
+ * It returns 1 or 0.
+ *
+ * @see ex::diff */
+ex symbol::derivative(const symbol & s) const
{
- ASSERT(is_of_type(other,symbol));
- const symbol *o = static_cast<const symbol *>(&other);
- if (serial==o->serial) return 0;
- return serial < o->serial ? -1 : 1;
+ if (compare_same_type(s))
+ return _ex0;
+ else
+ return _ex1;
}
-bool symbol::is_equal_same_type(basic const & other) const
+int symbol::compare_same_type(const basic & other) const
{
- ASSERT(is_of_type(other,symbol));
- const symbol *o = static_cast<const symbol *>(&other);
- return serial==o->serial;
+ GINAC_ASSERT(is_a<symbol>(other));
+ const symbol *o = static_cast<const symbol *>(&other);
+ if (serial==o->serial) return 0;
+ return serial < o->serial ? -1 : 1;
}
-unsigned symbol::return_type(void) const
-{
- return return_types::commutative;
-}
-
-unsigned symbol::return_type_tinfo(void) const
+bool symbol::is_equal_same_type(const basic & other) const
{
- return tinfo_key;
+ GINAC_ASSERT(is_a<symbol>(other));
+ const symbol *o = static_cast<const symbol *>(&other);
+ return serial==o->serial;
}
-unsigned symbol::calchash(void) const
+unsigned symbol::calchash() const
{
- // return golden_ratio_hash(tinfo()) ^ serial;
- hashvalue=golden_ratio_hash(golden_ratio_hash(0x55555555U ^ serial));
- setflag(status_flags::hash_calculated);
- return hashvalue;
+ hashvalue = golden_ratio_hash((p_int)tinfo() ^ serial);
+ setflag(status_flags::hash_calculated);
+ return hashvalue;
}
//////////
// non-virtual functions in this class
//////////
-// public
-
-void symbol::assign(ex const & value)
-{
- asexinfop->is_assigned=1;
- asexinfop->assigned_expression=value;
- clearflag(status_flags::evaluated);
-}
+// private
-void symbol::unassign(void)
+/** Symbols not constructed with a string get one assigned using this
+ * prefix and a number. */
+std::string & symbol::autoname_prefix()
{
- if (asexinfop->is_assigned) {
- asexinfop->is_assigned=0;
- asexinfop->assigned_expression=exZERO();
- }
- setflag(status_flags::evaluated);
+ static std::string *s = new std::string("symbol");
+ return *s;
}
-// private
-
-string & symbol::autoname_prefix(void)
+/** Return default TeX name for symbol. This recognizes some greek letters. */
+std::string symbol::default_TeX_name() const
{
- static string * s=new string("symbol");
- return *s;
+ 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;
+ else
+ return name;
}
//////////
// private
-unsigned symbol::next_serial=0;
-
-// string const symbol::autoname_prefix="symbol";
-
-//////////
-// global constants
-//////////
-
-const symbol some_symbol;
-type_info const & typeid_symbol=typeid(some_symbol);
+unsigned symbol::next_serial = 0;
-//////////
-// subclass assigned_ex_info
-//////////
-
-/** Default ctor. Defaults to unassigned. */
-symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)
-{
-}
+} // namespace GiNaC