X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fadd.cpp;h=d376b5d1983937df7a2236142909673ec225164d;hp=ad0ac1fc7f8bc4cf3c2e710a87e92a0f2ebf4a26;hb=4afb5bbe2c0b0a60928120a042997ba7d89e8f5c;hpb=db732c795526690b0a8e1f9c6ac5b50500f19fc6 diff --git a/ginac/add.cpp b/ginac/add.cpp index ad0ac1fc..d376b5d1 100644 --- a/ginac/add.cpp +++ b/ginac/add.cpp @@ -160,7 +160,7 @@ basic * add::duplicate() const return new add(*this); } -void add::print(ostream & os, unsigned upper_precedence) const +/*void add::print(ostream & os, unsigned upper_precedence) const { debugmsg("add print",LOGLEVEL_PRINT); if (precedence<=upper_precedence) os << "("; @@ -176,10 +176,17 @@ void add::print(ostream & os, unsigned upper_precedence) const } if (!coeff.is_equal(_num1()) && !coeff.is_equal(_num_1())) { - if (coeff.csgn()==-1) - (_num_1()*coeff).print(os, precedence); - else - coeff.print(os, precedence); + if (coeff.is_rational()) { + if (coeff.is_negative()) + os << -coeff; + else + os << coeff; + } else { + if (coeff.csgn()==-1) + (-coeff).print(os, precedence); + else + coeff.print(os, precedence); + } os << '*'; } os << cit->rest; @@ -190,6 +197,46 @@ void add::print(ostream & os, unsigned upper_precedence) const os << overall_coeff; } if (precedence<=upper_precedence) 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; + // first print the overall numeric coefficient, if present: + if (!overall_coeff.is_zero()) { + os << overall_coeff; + first = false; + } + // then proceed with the remaining factors: + for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) { + coeff = ex_to_numeric(cit->coeff); + if (!first) { + if (coeff.csgn()==-1) os << '-'; else os << '+'; + } else { + if (coeff.csgn()==-1) os << '-'; + first = false; + } + if (!coeff.is_equal(_num1()) && + !coeff.is_equal(_num_1())) { + if (coeff.is_rational()) { + if (coeff.is_negative()) + os << -coeff; + else + os << coeff; + } else { + if (coeff.csgn()==-1) + (-coeff).print(os, precedence); + else + coeff.print(os, precedence); + } + os << '*'; + } + os << cit->rest; + } + if (precedence<=upper_precedence) os << ")"; } void add::printraw(ostream & os) const