]> www.ginac.de Git - ginac.git/blobdiff - ginac/print.cpp
- changed mul::print() to behave similar to add::print()
[ginac.git] / ginac / print.cpp
index 899c21b0d02aacb85ce45e83056460a8f9f94606..f90a40ce89fa86aa9acb01e3ca15bd37ff70ad47 100644 (file)
@@ -46,14 +46,14 @@ 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();
 }
 
@@ -84,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
@@ -110,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
 {
@@ -179,42 +132,31 @@ 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.is_equal(numONE()) &&
+            !coeff.is_equal(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;
@@ -222,27 +164,22 @@ 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);
     if (precedence<=upper_precedence) os << "(";
     bool first=true;
-    if (!overall_coeff.is_equal(exONE())) {
-        overall_coeff.print(os,precedence);
-        first=false;
+    // first print the overall numeric coefficient:
+    if (ex_to_numeric(overall_coeff).csgn()==-1) os << '-';
+    if (!overall_coeff.is_equal(exONE()) &&
+        !overall_coeff.is_equal(exMINUSONE())) {
+        if (ex_to_numeric(overall_coeff).csgn()==-1)
+            (numMINUSONE()*overall_coeff).print(os, precedence);
+        else
+            overall_coeff.print(os, precedence);
+        os << '*';
     }
+    // then proceed with the remaining factors:
     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
         if (!first) {
             os << '*';