X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fsymbol.cpp;h=169ee631bc5ef0361f855ef3e7267a764e58c345;hp=b32418f1aa50e70ff1029dc52308568f30a3b2e4;hb=4e3a4ac2bcb0837611ea31bc8fc05d84a20c33ac;hpb=4e3986a8583584945681ee2dc1560e0fc08e4978 diff --git a/ginac/symbol.cpp b/ginac/symbol.cpp index b32418f1..169ee631 100644 --- a/ginac/symbol.cpp +++ b/ginac/symbol.cpp @@ -1,8 +1,9 @@ /** @file symbol.cpp * - * Implementation of GiNaC's symbolic objects. - * - * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany + * Implementation of GiNaC's symbolic objects. */ + +/* + * GiNaC Copyright (C) 1999-2001 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 @@ -24,72 +25,99 @@ #include "symbol.h" #include "lst.h" -#include "utils.h" #include "idx.h" +#include "archive.h" +#include "debugmsg.h" +#include "utils.h" + +namespace GiNaC { + +GINAC_IMPLEMENT_REGISTERED_CLASS_NO_CTORS(symbol, basic) ////////// -// default constructor, destructor, copy constructor assignment operator and helpers +// default ctor, dtor, copy ctor assignment operator and helpers ////////// -symbol::symbol() : basic(TINFO_symbol) +symbol::symbol() : inherited(TINFO_symbol), serial(next_serial++) { - debugmsg("symbol default constructor",LOGLEVEL_CONSTRUCT); - serial=next_serial++; - name=autoname_prefix()+ToString(serial); - asexinfop=new assigned_ex_info; - setflag(status_flags::evaluated); + debugmsg("symbol default ctor", LOGLEVEL_CONSTRUCT); + name = autoname_prefix()+ToString(serial); + asexinfop = new assigned_ex_info; + setflag(status_flags::evaluated | status_flags::expanded); } -symbol::~symbol() +/** For use by copy ctor and assignment operator. */ +void symbol::copy(const symbol & other) { - debugmsg("symbol destructor",LOGLEVEL_DESTRUCT); - destroy(0); + inherited::copy(other); + name = other.name; + serial = other.serial; + asexinfop = other.asexinfop; + ++asexinfop->refcount; } -symbol::symbol(symbol const & other) +void symbol::destroy(bool call_parent) { - debugmsg("symbol copy constructor",LOGLEVEL_CONSTRUCT); - copy(other); + if (--asexinfop->refcount == 0) + delete asexinfop; + if (call_parent) + inherited::destroy(call_parent); } -void symbol::copy(symbol const & other) +////////// +// other ctors +////////// + +// public + +symbol::symbol(const symbol & other) { - basic::copy(other); - name=other.name; - serial=other.serial; - asexinfop=other.asexinfop; - ++asexinfop->refcount; + debugmsg("symbol copy ctor", LOGLEVEL_CONSTRUCT); + copy(other); } -void symbol::destroy(bool call_parent) +symbol::symbol(const std::string & initname) : inherited(TINFO_symbol) { - if (--asexinfop->refcount == 0) { - delete asexinfop; - } - if (call_parent) { - basic::destroy(call_parent); - } + debugmsg("symbol ctor from string", LOGLEVEL_CONSTRUCT); + name = initname; + serial = next_serial++; + asexinfop = new assigned_ex_info; + setflag(status_flags::evaluated | status_flags::expanded); } -// how should the following be interpreted? -// symbol x; -// symbol y; -// x=y; -// probably as: x=ex(y); - ////////// -// other constructors +// archiving ////////// -// public +/** 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); + asexinfop = new assigned_ex_info; + setflag(status_flags::evaluated); +} -symbol::symbol(string const & initname) : basic(TINFO_symbol) +/** Unarchive the object. */ +ex symbol::unarchive(const archive_node &n, const 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 (unsigned i=0; ihold(); + return this->hold(); } -bool symbol::has(ex const & other) const +bool symbol::has(const ex & other) const { - if (is_equal(*other.bp)) return true; - return false; + if (this->is_equal(*other.bp)) + return true; + else + return false; } -int symbol::degree(symbol const & s) const +int symbol::degree(const symbol & s) const { - return compare_same_type(s)==0 ? 1 : 0; + return compare_same_type(s)==0 ? 1 : 0; } -int symbol::ldegree(symbol const & s) const +int symbol::ldegree(const symbol & s) const { - return compare_same_type(s)==0 ? 1 : 0; + return compare_same_type(s)==0 ? 1 : 0; } -ex symbol::coeff(symbol const & s, int const n) const +ex symbol::coeff(const symbol & s, int n) const { - if (compare_same_type(s)==0) { - return n==1 ? exONE() : exZERO(); - } else { - return n==0 ? *this : exZERO(); - } + if (compare_same_type(s)==0) + return n==1 ? _ex1() : _ex0(); + else + return n==0 ? *this : _ex0(); } ex symbol::eval(int level) 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 (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(); + } } -ex symbol::subs(lst const & ls, lst const & lr) const +ex symbol::subs(const lst & ls, const lst & lr) const { - ASSERT(ls.nops()==lr.nops()); -#ifdef DOASSERT - for (int i=0; i(&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(&other); - return serial==o->serial; + GINAC_ASSERT(is_of_type(other,symbol)); + const symbol *o = static_cast(&other); + if (serial==o->serial) return 0; + return serial < o->serial ? -1 : 1; +} + +bool symbol::is_equal_same_type(const basic & other) const +{ + GINAC_ASSERT(is_of_type(other,symbol)); + const symbol *o = static_cast(&other); + return serial==o->serial; } unsigned symbol::return_type(void) const { - return return_types::commutative; + return return_types::commutative; } unsigned symbol::return_type_tinfo(void) const { - return tinfo_key; + return tinfo_key; } unsigned symbol::calchash(void) const { - // return golden_ratio_hash(tinfo()) ^ serial; - hashvalue=golden_ratio_hash(golden_ratio_hash(0x55555555U ^ serial)); - setflag(status_flags::hash_calculated); - return hashvalue; + // 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); + setflag(status_flags::hash_calculated); + return hashvalue; } ////////// @@ -227,28 +301,30 @@ unsigned symbol::calchash(void) const // public -void symbol::assign(ex const & value) +void symbol::assign(const ex & value) { - asexinfop->is_assigned=1; - asexinfop->assigned_expression=value; - clearflag(status_flags::evaluated); + asexinfop->is_assigned = 1; + asexinfop->assigned_expression = value; + clearflag(status_flags::evaluated | status_flags::expanded); } void symbol::unassign(void) { - if (asexinfop->is_assigned) { - asexinfop->is_assigned=0; - asexinfop->assigned_expression=exZERO(); - } - setflag(status_flags::evaluated); + if (asexinfop->is_assigned) { + asexinfop->is_assigned = 0; + asexinfop->assigned_expression = _ex0(); + } + setflag(status_flags::evaluated | status_flags::expanded); } // private -string & symbol::autoname_prefix(void) +/** Symbols not constructed with a string get one assigned using this + * prefix and a number. */ +std::string & symbol::autoname_prefix(void) { - static string * s=new string("symbol"); - return *s; + static std::string *s = new std::string("symbol"); + return *s; } ////////// @@ -257,16 +333,7 @@ string & symbol::autoname_prefix(void) // 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 @@ -276,3 +343,5 @@ type_info const & typeid_symbol=typeid(some_symbol); symbol::assigned_ex_info::assigned_ex_info(void) : is_assigned(0), refcount(1) { } + +} // namespace GiNaC