]> www.ginac.de Git - ginac.git/blobdiff - ginac/symbol.cpp
Added complex conjugation methods and GiNaC function "conjugate".
[ginac.git] / ginac / symbol.cpp
index f5fd4a090b0df1f1ab66440b730d31b520104ada..57227fd1d7da9ba561579946056c0a1f04c96226 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of GiNaC's symbolic objects. */
 
 /*
- *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2004 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
 
 #include "symbol.h"
 #include "lst.h"
-#include "print.h"
 #include "archive.h"
-#include "debugmsg.h"
 #include "tostring.h"
 #include "utils.h"
+#include "inifcns.h"
 
 namespace GiNaC {
 
-GINAC_IMPLEMENT_REGISTERED_CLASS_NO_CTORS(symbol, basic)
+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 ctor, dtor, copy ctor assignment operator and helpers
+// default constructor
 //////////
 
-symbol::symbol() : inherited(TINFO_symbol), serial(next_serial++)
+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), domain(symbol_options::complex)
 {
-       debugmsg("symbol default ctor", LOGLEVEL_CONSTRUCT);
-       name = TeX_name = autoname_prefix()+ToString(serial);
-       asexinfop = new assigned_ex_info;
        setflag(status_flags::evaluated | status_flags::expanded);
 }
 
-/** For use by copy ctor and assignment operator. */
-void symbol::copy(const symbol & other)
-{
-       inherited::copy(other);
-       name = other.name;
-       TeX_name = other.TeX_name;
-       serial = other.serial;
-       asexinfop = other.asexinfop;
-       ++asexinfop->refcount;
-}
-
-void symbol::destroy(bool call_parent)
-{
-       if (--asexinfop->refcount == 0)
-               delete asexinfop;
-       if (call_parent)
-               inherited::destroy(call_parent);
-}
-
 //////////
-// other ctors
+// other constructors
 //////////
 
 // public
 
-symbol::symbol(const symbol & other)
+symbol::symbol(const std::string & initname, unsigned domain)
+ : 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), domain(domain)
 {
-       debugmsg("symbol copy ctor", LOGLEVEL_CONSTRUCT);
-       copy(other);
+       setflag(status_flags::evaluated | status_flags::expanded);
 }
 
-symbol::symbol(const std::string & initname) : inherited(TINFO_symbol)
+symbol::symbol(const std::string & initname, const std::string & texname, unsigned domain)
+ : 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), domain(domain)
 {
-       debugmsg("symbol ctor from string", LOGLEVEL_CONSTRUCT);
-       name = initname;
-       TeX_name = default_TeX_name();
-       serial = next_serial++;
-       asexinfop = new assigned_ex_info;
        setflag(status_flags::evaluated | status_flags::expanded);
 }
 
-symbol::symbol(const std::string & initname, const std::string & texname) : inherited(TINFO_symbol)
+symbol::symbol(const std::string & initname, unsigned rt, unsigned rtt, unsigned domain)
+ : 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), domain(domain)
+{
+       setflag(status_flags::evaluated | status_flags::expanded);
+}
+
+symbol::symbol(const std::string & initname, const std::string & texname, unsigned rt, unsigned rtt, unsigned domain)
+ : inherited(TINFO_symbol), asexinfop(new assigned_ex_info), serial(next_serial++), name(initname), TeX_name(texname), ret_type(rt), ret_type_tinfo(rtt), domain(domain)
 {
-       debugmsg("symbol ctor from string", LOGLEVEL_CONSTRUCT);
-       name = initname;
-       TeX_name = texname;
-       serial = next_serial++;
-       asexinfop = new assigned_ex_info;
        setflag(status_flags::evaluated | status_flags::expanded);
 }
 
@@ -103,28 +83,35 @@ symbol::symbol(const std::string & initname, const std::string & texname) : inhe
 //////////
 
 /** Construct object from archive_node. */
-symbol::symbol(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
+symbol::symbol(const archive_node &n, lst &sym_lst)
+ : inherited(n, sym_lst), asexinfop(new assigned_ex_info), serial(next_serial++)
 {
-       debugmsg("symbol ctor from archive_node", LOGLEVEL_CONSTRUCT);
-       serial = next_serial++;
-       if (!(n.find_string("name", name)))
+       if (!n.find_string("name", name))
                name = autoname_prefix() + ToString(serial);
-       if (!(n.find_string("TeXname", TeX_name)))
+       if (!n.find_string("TeXname", TeX_name))
                TeX_name = default_TeX_name();
-       asexinfop = new assigned_ex_info;
-       setflag(status_flags::evaluated);
+       if (!n.find_unsigned("domain", domain))
+               domain = symbol_options::complex;
+       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, const lst &sym_lst)
+ex symbol::unarchive(const archive_node &n, lst &sym_lst)
 {
        ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
-       
+
        // If symbol is in sym_lst, return the existing symbol
-       for (unsigned i=0; i<sym_lst.nops(); i++) {
-               if (is_ex_of_type(sym_lst.op(i), symbol) && (ex_to<symbol>(sym_lst.op(i)).name == ex_to<symbol>(s).name))
-                       return sym_lst.op(i);
+       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;
 }
 
@@ -135,6 +122,12 @@ void symbol::archive(archive_node &n) const
        n.add_string("name", name);
        if (TeX_name != default_TeX_name())
                n.add_string("TeX_name", TeX_name);
+       if (domain != symbol_options::complex)
+               n.add_unsigned("domain", domain);
+       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);
 }
 
 //////////
@@ -143,27 +136,31 @@ void symbol::archive(archive_node &n) const
 
 // 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;
 }
 
-void symbol::print(const print_context & c, unsigned level) const
+void symbol::do_print_latex(const print_latex & c, unsigned level) const
 {
-       debugmsg("symbol print", LOGLEVEL_PRINT);
-
-       if (is_of_type(c, print_tree)) {
+       c.s << TeX_name;
+}
 
-               c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
-                   << ", serial=" << serial
-                   << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
-                   << std::endl;
+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=" << domain
+           << std::endl;
+}
 
-       } else if (is_of_type(c, print_latex))
-               c.s << TeX_name;
-       else
-               c.s << name;
+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 << "','" << TeX_name;
+       c.s << "')";
 }
 
 bool symbol::info(unsigned inf) const
@@ -180,24 +177,6 @@ bool symbol::info(unsigned inf) const
                return inherited::info(inf);
 }
 
-int symbol::degree(const ex & s) const
-{
-       return is_equal(*s.bp) ? 1 : 0;
-}
-
-int symbol::ldegree(const ex & s) const
-{
-       return is_equal(*s.bp) ? 1 : 0;
-}
-
-ex symbol::coeff(const ex & s, int n) const
-{
-       if (is_equal(*s.bp))
-               return n==1 ? _ex1() : _ex0();
-       else
-               return n==0 ? *this : _ex0();
-}
-
 ex symbol::eval(int level) const
 {
        if (level == -max_recursion_level)
@@ -214,6 +193,15 @@ ex symbol::eval(int level) const
        }
 }
 
+ex symbol::conjugate() const
+{
+       if (this->domain == symbol_options::complex) {
+               return GiNaC::conjugate(*this).hold();
+       } else {
+               return *this;
+       }
+}
+
 // protected
 
 /** Implementation of ex::diff() for single differentiation of a symbol.
@@ -223,14 +211,14 @@ ex symbol::eval(int level) const
 ex symbol::derivative(const symbol & s) const
 {
        if (compare_same_type(s))
-               return _ex0();
+               return _ex0;
        else
-               return _ex1();
+               return _ex1;
 }
 
 int symbol::compare_same_type(const basic & other) const
 {
-       GINAC_ASSERT(is_of_type(other,symbol));
+       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;
@@ -238,17 +226,14 @@ int symbol::compare_same_type(const basic & other) const
 
 bool symbol::is_equal_same_type(const basic & other) const
 {
-       GINAC_ASSERT(is_of_type(other,symbol));
+       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
 {
-       // this is where the schoolbook method
-       // (golden_ratio_hash(tinfo()) ^ serial)
-       // is not good enough yet...
-       hashvalue = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ serial);
+       hashvalue = golden_ratio_hash(tinfo() ^ serial);
        setflag(status_flags::hash_calculated);
        return hashvalue;
 }
@@ -267,16 +252,16 @@ unsigned symbol::calchash(void) const
 
 void symbol::assign(const ex & value)
 {
-       asexinfop->is_assigned = 1;
+       asexinfop->is_assigned = true;
        asexinfop->assigned_expression = value;
        clearflag(status_flags::evaluated | status_flags::expanded);
 }
 
-void symbol::unassign(void)
+void symbol::unassign()
 {
        if (asexinfop->is_assigned) {
-               asexinfop->is_assigned = 0;
-               asexinfop->assigned_expression = _ex0();
+               asexinfop->is_assigned = false;
+               asexinfop->assigned_expression = _ex0;
        }
        setflag(status_flags::evaluated | status_flags::expanded);
 }
@@ -285,14 +270,14 @@ void symbol::unassign(void)
 
 /** Symbols not constructed with a string get one assigned using this
  *  prefix and a number. */
-std::string & symbol::autoname_prefix(void)
+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(void) const
+std::string symbol::default_TeX_name() const
 {
        if (name=="alpha"        || name=="beta"         || name=="gamma"
         || name=="delta"        || name=="epsilon"      || name=="varepsilon"
@@ -326,7 +311,7 @@ unsigned symbol::next_serial = 0;
 //////////
 
 /** Default ctor.  Defaults to unassigned. */
-symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1)
+symbol::assigned_ex_info::assigned_ex_info() throw() : is_assigned(false)
 {
 }