From: Richard Kreckel Date: Thu, 18 Nov 1999 00:22:09 +0000 (+0000) Subject: - deleted add::printpair() since this has become obsolete X-Git-Tag: release_0-5-0~158 X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=commitdiff_plain;h=8c5b07aac21d8de1a6cfcf4cac10706ca92a1605 - deleted add::printpair() since this has become obsolete - revamped add::print() in order to fix all those output-bugs like this infamous one: x-2*y --> x(-2)*y --- diff --git a/ginac/add.h b/ginac/add.h index f471741b..37d502a0 100644 --- a/ginac/add.h +++ b/ginac/add.h @@ -81,8 +81,6 @@ protected: unsigned return_type_tinfo(void) const; ex thisexpairseq(epvector const & v, ex const & oc) const; ex thisexpairseq(epvector * vp, ex const & oc) const; - void printpair(ostream & os, expair const & p, - unsigned upper_precedence) const; expair split_ex_to_pair(ex const & e) const; expair combine_ex_with_coeff_to_pair(ex const & e, ex const & c) const; diff --git a/ginac/mul.h b/ginac/mul.h index 4e5a6e63..19dad491 100644 --- a/ginac/mul.h +++ b/ginac/mul.h @@ -82,8 +82,6 @@ protected: unsigned return_type_tinfo(void) const; ex thisexpairseq(epvector const & v, ex const & oc) const; ex thisexpairseq(epvector * vp, ex const & oc) const; - void printpair(ostream & os, expair const & p, - unsigned upper_precedence) const; expair split_ex_to_pair(ex const & e) const; expair combine_ex_with_coeff_to_pair(ex const & e, ex const & c) const; diff --git a/ginac/numeric.cpp b/ginac/numeric.cpp index 8609740e..cc4faf30 100644 --- a/ginac/numeric.cpp +++ b/ginac/numeric.cpp @@ -224,7 +224,7 @@ void numeric::printraw(ostream & os) const void numeric::print(ostream & os, unsigned upper_precedence) const { debugmsg("numeric print", LOGLEVEL_PRINT); - if (is_real()) { + if (is_real()) { // case 1, real: x or -x if ((precedence<=upper_precedence) && (!is_pos_integer())) { os << "(" << *value << ")"; @@ -515,10 +515,35 @@ numeric const & numeric::operator=(char const * s) return operator=(numeric(s)); } +/** Return the complex half-plane (left or right) in which the number lies. + * csgn(x)==0 for x==0, csgn(x)==1 for Re(x)>0 or Re(x)=0 and Im(x)>0, + * csgn(x)==-1 for Re(x)<0 or Re(x)=0 and Im(x)<0. + * + * @see numeric::compare(numeric const @ other) */ +int numeric::csgn(void) const +{ + if (is_zero()) + return 0; + if (!zerop(realpart(*value))) { + if (plusp(realpart(*value))) + return 1; + else + return -1; + } else { + if (plusp(imagpart(*value))) + return 1; + else + return -1; + } +} + /** This method establishes a canonical order on all numbers. For complex * numbers this is not possible in a mathematically consistent way but we need * to establish some order and it ought to be fast. So we simply define it - * similar to Maple's csgn. */ + * to be compatible with our method csgn. + * + * @return csgn(*this-other) + * @see numeric::csgn(void) */ int numeric::compare(numeric const & other) const { // Comparing two real numbers? @@ -1065,6 +1090,10 @@ numeric factorial(numeric const & nn) * @exception range_error (argument must be integer >= -1) */ numeric doublefactorial(numeric const & nn) { + // META-NOTE: The whole shit here will become obsolete and may be moved + // out once CLN learns about double factorial, which should be as soon as + // 1.0.3 rolls out. + // We store the results separately for even and odd arguments. This has // the advantage that we don't have to compute any even result at all if // the function is always called with odd arguments and vice versa. There diff --git a/ginac/numeric.h b/ginac/numeric.h index a460d099..fce1f196 100644 --- a/ginac/numeric.h +++ b/ginac/numeric.h @@ -170,6 +170,7 @@ public: numeric power_dyn(numeric const & other) const { return power(other); } */ numeric inverse(void) const; + int csgn(void) const; int compare(numeric const & other) const; bool is_equal(numeric const & other) const; bool is_zero(void) const; @@ -266,6 +267,9 @@ struct numeric_fail inline numeric inverse(numeric const & x) { return x.inverse(); } +inline bool csgn(numeric const & x) +{ return x.csgn(); } + inline bool is_zero(numeric const & x) { return x.is_zero(); } diff --git a/ginac/print.cpp b/ginac/print.cpp index 899c21b0..8f70910d 100644 --- a/ginac/print.cpp +++ b/ginac/print.cpp @@ -179,42 +179,32 @@ void expairseq::print(ostream & os, unsigned upper_precedence) const os << "]]"; } -void add::printpair(ostream & os, expair const & p, unsigned upper_precedence) const -{ - os << "("; - if (p.coeff == -1) { - os << "-"; - } else { - if (p.coeff != 1) { - os << p.coeff; - os << "*"; - } - } - os << p.rest; - os << ")"; -} - void add::print(ostream & os, unsigned upper_precedence) const { debugmsg("add print",LOGLEVEL_PRINT); if (precedence<=upper_precedence) os << "("; + numeric coeff; bool first=true; for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) { + coeff = ex_to_numeric(cit->coeff); if (!first) { - if (cit->coeff > 0) os << '+'; + if (coeff < 0) os << '-'; else os << '+'; } else { + if (coeff < 0) os << '-'; first=false; } - if (cit->coeff == -1) { - os << "-"; - } else { - if (cit->coeff != 1) { - (cit->coeff).print(os,precedence); - os << "*"; - } + if (coeff.compare(numONE()) && coeff.compare(numMINUSONE())) { + if (!coeff.is_real() && !coeff.real().is_zero()) os << '('; + if (coeff > 0) + os << coeff; + else + os << numeric(-1)*coeff; + if (!coeff.is_real() && !coeff.real().is_zero()) os << ')'; + os << '*'; } os << cit->rest; } + // print the overall numeric coefficient, if present: if (!overall_coeff.is_zero()) { if (overall_coeff > 0) os << '+'; os << overall_coeff; @@ -222,18 +212,6 @@ void add::print(ostream & os, unsigned upper_precedence) const if (precedence<=upper_precedence) os << ")"; } -void mul::printpair(ostream & os, expair const & p, unsigned upper_precedence) const -{ - os << "("; - if (p.coeff.compare(exONE())==0) { - p.rest.print(os,upper_precedence); - } else { - // outer parens around ex needed for broken gcc-2.95 parser: - (ex(power(p.rest,p.coeff))).print(os,upper_precedence); - } - os << ")"; -} - void mul::print(ostream & os, unsigned upper_precedence) const { debugmsg("mul print",LOGLEVEL_PRINT);