X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fprint.cpp;h=1df9cd2fbe1387e3c02ba65b392018a207780e08;hp=2d3a936341b292e036f66f8f6cb7165ff34e6e61;hb=844253ded5f41b0c7326ac2471de16567bd34f37;hpb=66c0f31c678e6c1938d637636b230ea376c157c1 diff --git a/ginac/print.cpp b/ginac/print.cpp index 2d3a9363..1df9cd2f 100644 --- a/ginac/print.cpp +++ b/ginac/print.cpp @@ -1,8 +1,9 @@ /** @file print.cpp * * The methods .print() are responsible for the nice default-output of - * objects. All related helper-functions go in here as well. - * + * objects. All related helper-functions go in here as well. */ + +/* * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany * * This program is free software; you can redistribute it and/or modify @@ -38,18 +39,21 @@ #include "relational.h" #include "series.h" #include "symbol.h" +#include "debugmsg.h" + +namespace GiNaC { void ex::print(ostream & os, unsigned upper_precedence) const { debugmsg("ex print",LOGLEVEL_PRINT); - ASSERT(bp!=0); + GINAC_ASSERT(bp!=0); bp->print(os,upper_precedence); } void ex::dbgprint(void) const { debugmsg("ex dbgprint",LOGLEVEL_PRINT); - ASSERT(bp!=0); + GINAC_ASSERT(bp!=0); bp->dbgprint(); } @@ -80,15 +84,11 @@ void constant::print(ostream & os, unsigned upper_precedence) const void power::print(ostream & os, unsigned upper_precedence) const { debugmsg("power print",LOGLEVEL_PRINT); -#if 1 if (precedence<=upper_precedence) os << "("; basis.print(os,precedence); os << "^"; exponent.print(os,precedence); if (precedence<=upper_precedence) os << ")"; -#else - os << "pow(" << basis << "," << exponent << ")"; -#endif } void fail::print(ostream & os, unsigned upper_precedence) const @@ -106,49 +106,6 @@ void expairseq::printpair(ostream & os, expair const & p, unsigned upper_precede os << "]]"; } -/* -void expairseq::printseq(ostream & os, char delim, unsigned this_precedence, - unsigned upper_precedence) const -{ - if (this_precedence<=upper_precedence) os << "("; - epvector::const_iterator itt,it,it_last,it_start; - it_last=seq.end(); - --it_last; - it_start=seq.begin(); - - switch (delim) { - case '+': - for (it=seq.begin(); it!=it_last; ++it) { - itt = it +1; - expair const & k = *itt; - printpair(os,*it, this_precedence); - if (((is_ex_of_type(k.rest, numeric)) && (k.coeff*k.rest > 0) ) || ((!is_ex_of_type(k.rest, numeric)) && (k.coeff >0))){ - os << "+"; - } - } - printpair(os,*it,this_precedence); - break; - - case '*': - for (it = it_last ; it!=it_start; --it) { - if ((*it).rest.is_equal(exMINUSONE()) && - (*it).coeff.is_equal(exONE())) { - os << "-"; - } else { - printpair(os, *it,this_precedence); - os << delim; - } - } - printpair(os,*it,this_precedence); - break; - default: - clog << "Nobody expects the Spanish Inquisition: " - << "deliminator unknown!" << endl; - } - if (this_precedence<=upper_precedence) os << ")"; -} -*/ - void expairseq::printseq(ostream & os, char delim, unsigned this_precedence, unsigned upper_precedence) const { @@ -175,42 +132,30 @@ 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.csgn()==-1) os << '-'; else os << '+'; } else { + if (coeff.csgn()==-1) 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.csgn()==-1) + (numMINUSONE()*coeff).print(os, precedence); + else + coeff.print(os, precedence); + os << '*'; } os << cit->rest; } + // print the overall numeric coefficient, if present: if (!overall_coeff.is_zero()) { if (overall_coeff > 0) os << '+'; os << overall_coeff; @@ -218,18 +163,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); @@ -317,3 +250,5 @@ void matrix::print(ostream & os, unsigned upper_precedence) const } os << m[row*col-1] << "]] ]]"; } + +} // namespace GiNaC