]> www.ginac.de Git - ginac.git/blobdiff - ginac/symbol.cpp
- Updated from FSF, reflecting libtool version 1.4.2.
[ginac.git] / ginac / symbol.cpp
index bebb8776d1551ba2c879d65499fccbacceb1ffef..9d291686e59f6b20963b9eb51835ebb1888ac30b 100644 (file)
@@ -28,6 +28,7 @@
 #include "print.h"
 #include "archive.h"
 #include "debugmsg.h"
+#include "tostring.h"
 #include "utils.h"
 
 namespace GiNaC {
@@ -41,7 +42,7 @@ GINAC_IMPLEMENT_REGISTERED_CLASS_NO_CTORS(symbol, basic)
 symbol::symbol() : inherited(TINFO_symbol), serial(next_serial++)
 {
        debugmsg("symbol default ctor", LOGLEVEL_CONSTRUCT);
-       name = autoname_prefix()+ToString(serial);
+       name = TeX_name = autoname_prefix()+ToString(serial);
        asexinfop = new assigned_ex_info;
        setflag(status_flags::evaluated | status_flags::expanded);
 }
@@ -51,6 +52,7 @@ 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;
@@ -80,6 +82,17 @@ symbol::symbol(const std::string & initname) : inherited(TINFO_symbol)
 {
        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)
+{
+       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);
@@ -96,6 +109,8 @@ symbol::symbol(const archive_node &n, const 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();
        asexinfop = new assigned_ex_info;
        setflag(status_flags::evaluated);
 }
@@ -107,7 +122,7 @@ ex symbol::unarchive(const archive_node &n, const lst &sym_lst)
        
        // 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))
+               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);
        }
        return s;
@@ -118,10 +133,12 @@ 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);
 }
 
 //////////
-// functions overriding virtual functions from bases classes
+// functions overriding virtual functions from base classes
 //////////
 
 // public
@@ -136,32 +153,16 @@ void symbol::print(const print_context & c, unsigned level) const
 {
        debugmsg("symbol print", LOGLEVEL_PRINT);
 
-       if (is_of_type(c, print_tree)) {
+       if (is_a<print_tree>(c)) {
 
                c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
                    << ", serial=" << serial
                    << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
                    << std::endl;
 
-       } else if (is_of_type(c, print_latex)) {
-               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=="varphix"
-                || 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")
-                       c.s << "\\" << name;
-               else
-                       c.s << name;
-       } else
+       } else if (is_a<print_latex>(c))
+               c.s << TeX_name;
+       else
                c.s << name;
 }
 
@@ -179,32 +180,19 @@ bool symbol::info(unsigned inf) const
                return inherited::info(inf);
 }
 
-ex symbol::expand(unsigned options) const
-{
-       return this->hold();
-}
-
-bool symbol::has(const ex & other) const
-{
-       if (this->is_equal(*other.bp))
-               return true;
-       else
-               return false;
-}
-
 int symbol::degree(const ex & s) const
 {
-       return is_equal(*s.bp) ? 1 : 0;
+       return is_equal(ex_to<basic>(s)) ? 1 : 0;
 }
 
 int symbol::ldegree(const ex & s) const
 {
-       return is_equal(*s.bp) ? 1 : 0;
+       return is_equal(ex_to<basic>(s)) ? 1 : 0;
 }
 
 ex symbol::coeff(const ex & s, int n) const
 {
-       if (is_equal(*s.bp))
+       if (is_equal(ex_to<basic>(s)))
                return n==1 ? _ex1() : _ex0();
        else
                return n==0 ? *this : _ex0();
@@ -242,7 +230,7 @@ ex symbol::derivative(const symbol & s) const
 
 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;
@@ -250,21 +238,11 @@ 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::return_type(void) const
-{
-       return return_types::commutative;
-}
-   
-unsigned symbol::return_type_tinfo(void) const
-{
-       return tinfo_key;
-}
-
 unsigned symbol::calchash(void) const
 {
        // this is where the schoolbook method
@@ -313,6 +291,28 @@ std::string & symbol::autoname_prefix(void)
        return *s;
 }
 
+/** Return default TeX name for symbol. This recognizes some greek letters. */
+std::string symbol::default_TeX_name(void) const
+{
+       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;
+}
+
 //////////
 // static member variables
 //////////