X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fsymbol.cpp;h=f566e233ae529efec70fae37bf68073cfb167088;hp=169ee631bc5ef0361f855ef3e7267a764e58c345;hb=e7cc6a764ff67b5885d6633385fac23ccc1dc9a7;hpb=4e3a4ac2bcb0837611ea31bc8fc05d84a20c33ac diff --git a/ginac/symbol.cpp b/ginac/symbol.cpp index 169ee631..f566e233 100644 --- a/ginac/symbol.cpp +++ b/ginac/symbol.cpp @@ -3,7 +3,7 @@ * Implementation of GiNaC's symbolic objects. */ /* - * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany + * GiNaC Copyright (C) 1999-2002 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 @@ -25,9 +25,9 @@ #include "symbol.h" #include "lst.h" -#include "idx.h" +#include "print.h" #include "archive.h" -#include "debugmsg.h" +#include "tostring.h" #include "utils.h" namespace GiNaC { @@ -35,13 +35,12 @@ namespace GiNaC { GINAC_IMPLEMENT_REGISTERED_CLASS_NO_CTORS(symbol, basic) ////////// -// default ctor, dtor, copy ctor assignment operator and helpers +// default ctor, dtor, copy ctor, assignment operator and helpers ////////// 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 +50,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; @@ -72,14 +72,22 @@ void symbol::destroy(bool call_parent) symbol::symbol(const symbol & other) { - debugmsg("symbol copy ctor", LOGLEVEL_CONSTRUCT); copy(other); } 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) +{ + name = initname; + TeX_name = texname; serial = next_serial++; asexinfop = new assigned_ex_info; setflag(status_flags::evaluated | status_flags::expanded); @@ -92,10 +100,11 @@ symbol::symbol(const std::string & initname) : inherited(TINFO_symbol) /** Construct object from archive_node. */ symbol::symbol(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) { - debugmsg("symbol ctor from archive_node", LOGLEVEL_CONSTRUCT); 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 +116,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.op(i)).name == ex_to(s).name)) return sym_lst.op(i); } return s; @@ -118,47 +127,39 @@ 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 basic *symbol::duplicate() const { - debugmsg("symbol duplicate", LOGLEVEL_DUPLICATE); return new symbol(*this); } -void symbol::print(std::ostream & os, unsigned upper_precedence) const -{ - debugmsg("symbol print",LOGLEVEL_PRINT); - os << name; -} - -void symbol::printraw(std::ostream & os) const +void symbol::print(const print_context & c, unsigned level) const { - debugmsg("symbol printraw",LOGLEVEL_PRINT); - os << "symbol(" << "name=" << name << ",serial=" << serial - << ",hash=" << hashvalue << ",flags=" << flags << ")"; -} - -void symbol::printtree(std::ostream & os, unsigned indent) const -{ - debugmsg("symbol printtree",LOGLEVEL_PRINT); - os << std::string(indent,' ') << name << " (symbol): " - << "serial=" << serial - << ", hash=" << hashvalue - << " (0x" << std::hex << hashvalue << std::dec << ")" - << ", flags=" << flags << std::endl; -} - -void symbol::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const -{ - debugmsg("symbol print csrc", LOGLEVEL_PRINT); - os << name; + if (is_a(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_a(c)) { + c.s << TeX_name; + } else if (is_a(c)) { + c.s << class_name() << "('" << name; + if (TeX_name != default_TeX_name()) + c.s << "','" << TeX_name; + c.s << "')"; + } else + c.s << name; } bool symbol::info(unsigned inf) const @@ -175,35 +176,22 @@ bool symbol::info(unsigned inf) const return inherited::info(inf); } -ex symbol::expand(unsigned options) const +int symbol::degree(const ex & s) const { - return this->hold(); + return is_equal(ex_to(s)) ? 1 : 0; } -bool symbol::has(const ex & other) const +int symbol::ldegree(const ex & s) const { - if (this->is_equal(*other.bp)) - return true; - else - return false; + return is_equal(ex_to(s)) ? 1 : 0; } -int symbol::degree(const symbol & s) const +ex symbol::coeff(const ex & s, int n) const { - return compare_same_type(s)==0 ? 1 : 0; -} - -int symbol::ldegree(const symbol & s) const -{ - return compare_same_type(s)==0 ? 1 : 0; -} - -ex symbol::coeff(const symbol & s, int n) const -{ - if (compare_same_type(s)==0) - return n==1 ? _ex1() : _ex0(); + if (is_equal(ex_to(s))) + return n==1 ? _ex1 : _ex0; else - return n==0 ? *this : _ex0(); + return n==0 ? *this : _ex0; } ex symbol::eval(int level) const @@ -222,24 +210,6 @@ ex symbol::eval(int level) const } } -ex symbol::subs(const lst & ls, const lst & lr) const -{ - GINAC_ASSERT(ls.nops()==lr.nops()); -#ifdef DO_GINAC_ASSERT - for (unsigned i=0; i(other)); const symbol *o = static_cast(&other); if (serial==o->serial) return 0; return serial < o->serial ? -1 : 1; @@ -264,21 +234,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(other)); const symbol *o = static_cast(&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 @@ -312,7 +272,7 @@ void symbol::unassign(void) { if (asexinfop->is_assigned) { asexinfop->is_assigned = 0; - asexinfop->assigned_expression = _ex0(); + asexinfop->assigned_expression = _ex0; } setflag(status_flags::evaluated | status_flags::expanded); } @@ -327,6 +287,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 //////////