From ece407276fc0f844b2cfcd51838e1cbe83a2d70c Mon Sep 17 00:00:00 2001 From: Chris Dams Date: Mon, 19 Jun 2006 15:29:43 +0000 Subject: [PATCH] Made class possymbol for positive symbols and documented it. --- doc/tutorial/ginac.texi | 10 ++++++++-- ginac/symbol.cpp | 21 +++++++++++++++++++++ ginac/symbol.h | 23 +++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/doc/tutorial/ginac.texi b/doc/tutorial/ginac.texi index 26f004ed..bdea81b8 100644 --- a/doc/tutorial/ginac.texi +++ b/doc/tutorial/ginac.texi @@ -1155,11 +1155,17 @@ in the complex domain. As a consequence, operations like complex conjugation, for example (@pxref{Complex expressions}), do @emph{not} evaluate if applied to such symbols. Likewise @code{log(exp(x))} does not evaluate to @code{x}, because of the unknown imaginary part of @code{x}. -On the other hand, if you are sure that your symbols will hold only real values, you -would like to have such functions evaluated. Therefore GiNaC allows you to specify +On the other hand, if you are sure that your symbols will hold only real +values, you would like to have such functions evaluated. Therefore GiNaC +allows you to specify the domain of the symbol. Instead of @code{symbol x("x");} you can write @code{realsymbol x("x");} to tell GiNaC that @code{x} stands in for real values. +@cindex @code{possymbol()} +Furthermore, it is also possible to declare a symbol as positive. This will, +for instance, enable the automatic simplification of @code{abs(x)} into +@code{x}. This is done by declaying the symbol as @code{possymbol x("x");}. + @node Numbers, Constants, Symbols, Basic concepts @c node-name, next, previous, up diff --git a/ginac/symbol.cpp b/ginac/symbol.cpp index 7e3b49d8..bf58fd4b 100644 --- a/ginac/symbol.cpp +++ b/ginac/symbol.cpp @@ -57,6 +57,13 @@ realsymbol::realsymbol() domain = domain::real; } +// possymbol + +possymbol::possymbol() +{ + domain = domain::positive; +} + ////////// // other constructors ////////// @@ -103,6 +110,20 @@ realsymbol::realsymbol(const std::string & initname, unsigned rt, tinfo_t rtt, u 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) { } + ////////// // archiving ////////// diff --git a/ginac/symbol.h b/ginac/symbol.h index 90013144..2a5ae5cc 100644 --- a/ginac/symbol.h +++ b/ginac/symbol.h @@ -41,6 +41,7 @@ class symbol : public basic GINAC_DECLARE_REGISTERED_CLASS(symbol, basic) friend class realsymbol; + friend class possymbol; // types @@ -127,6 +128,19 @@ public: }; +/** Specialization of symbol to real domain */ +class possymbol : public symbol +{ + // constructors +public: + possymbol(); + explicit possymbol(const std::string & initname, unsigned domain = domain::positive); + possymbol(const std::string & initname, const std::string & texname, unsigned domain = domain::positive); + possymbol(const std::string & initname, unsigned rt, tinfo_t rtt, unsigned domain = domain::positive); + possymbol(const std::string & initname, const std::string & texname, unsigned rt, tinfo_t rtt, unsigned domain = domain::positive); +}; + + // utility functions /** Specialization of is_exactly_a(obj) for realsymbol objects. */ @@ -138,6 +152,15 @@ template<> inline bool is_exactly_a(const basic & obj) return domain==domain::real || domain==domain::positive; } +/** Specialization of is_exactly_a(obj) for possymbol objects. */ +template<> inline bool is_exactly_a(const basic & obj) +{ + if (obj.tinfo() != &symbol::tinfo_static) + return false; + unsigned domain = static_cast(obj).get_domain(); + return domain == domain::positive; +} + // wrapper functions around member functions inline void unassign(symbol & symarg) { symarg.unassign(); } -- 2.44.0