]> www.ginac.de Git - ginac.git/blobdiff - ginac/add.cpp
* add::coeff(): don't epvector::reserve() more terms than we'll actually
[ginac.git] / ginac / add.cpp
index 150b4f8222d0df346dd357b104656f9fd70aaf15..412bd5a548e65cffab65f1fd3379c7aed137bc03 100644 (file)
@@ -111,107 +111,119 @@ DEFAULT_ARCHIVING(add)
 
 // public
 
-void add::print(std::ostream & os, unsigned upper_precedence) const
+void add::print(const print_context & c, unsigned level) 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 << '-';
+       debugmsg("add print", LOGLEVEL_PRINT);
+
+       if (is_of_type(c, print_tree)) {
+
+               inherited::print(c, level);
+
+       } else if (is_of_type(c, print_csrc)) {
+
+               if (precedence <= level)
+                       c.s << "(";
+       
+               // Print arguments, separated by "+"
+               epvector::const_iterator it = seq.begin(), itend = seq.end();
+               while (it != itend) {
+               
+                       // If the coefficient is -1, it is replaced by a single minus sign
+                       if (it->coeff.compare(_num1()) == 0) {
+                               it->rest.bp->print(c, precedence);
+                       } else if (it->coeff.compare(_num_1()) == 0) {
+                               c.s << "-";
+                               it->rest.bp->print(c, precedence);
+                       } else if (ex_to_numeric(it->coeff).numer().compare(_num1()) == 0) {
+                               it->rest.bp->print(c, precedence);
+                               c.s << "/";
+                               ex_to_numeric(it->coeff).denom().print(c, precedence);
+                       } else if (ex_to_numeric(it->coeff).numer().compare(_num_1()) == 0) {
+                               c.s << "-";
+                               it->rest.bp->print(c, precedence);
+                               c.s << "/";
+                               ex_to_numeric(it->coeff).denom().print(c, precedence);
+                       } else {
+                               it->coeff.bp->print(c, precedence);
+                               c.s << "*";
+                               it->rest.bp->print(c, precedence);
+                       }
+               
+                       // Separator is "+", except if the following expression would have a leading minus sign
+                       it++;
+                       if (it != itend && !(it->coeff.compare(_num0()) < 0 || (it->coeff.compare(_num1()) == 0 && is_ex_exactly_of_type(it->rest, numeric) && it->rest.compare(_num0()) < 0)))
+                               c.s << "+";
+               }
+       
+               if (!overall_coeff.is_zero()) {
+                       if (overall_coeff.info(info_flags::positive))
+                               c.s << '+';
+                       overall_coeff.bp->print(c, precedence);
+               }
+       
+               if (precedence <= level)
+                       c.s << ")";
+
+       } else {
+
+               if (precedence <= level) {
+                       if (is_of_type(c, print_latex))
+                               c.s << "{(";
+                       else
+                               c.s << "(";
+               }
+
+               numeric coeff;
+               bool first = true;
+
+               // First print the overall numeric coefficient, if present
+               if (!overall_coeff.is_zero()) {
+                       if (!is_of_type(c, print_tree))
+                               overall_coeff.print(c, 0);
+                       else
+                               overall_coeff.print(c, precedence);
                        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;
+
+               // Then proceed with the remaining factors
+               epvector::const_iterator it = seq.begin(), itend = seq.end();
+               while (it != itend) {
+                       coeff = ex_to_numeric(it->coeff);
+                       if (!first) {
+                               if (coeff.csgn() == -1) c.s << '-'; else c.s << '+';
                        } else {
-                               if (coeff.csgn()==-1)
-                                       (-coeff).print(os, precedence);
+                               if (coeff.csgn() == -1) c.s << '-';
+                               first = false;
+                       }
+                       if (!coeff.is_equal(_num1()) &&
+                           !coeff.is_equal(_num_1())) {
+                               if (coeff.is_rational()) {
+                                       if (coeff.is_negative())
+                                               (-coeff).print(c);
+                                       else
+                                               coeff.print(c);
+                               } else {
+                                       if (coeff.csgn() == -1)
+                                               (-coeff).print(c, precedence);
+                                       else
+                                               coeff.print(c, precedence);
+                               }
+                               if (is_of_type(c, print_latex))
+                                       c.s << ' ';
                                else
-                                       coeff.print(os, precedence);
+                                       c.s << '*';
                        }
-                       os << '*';
+                       it->rest.print(c, precedence);
+                       it++;
                }
-               cit->rest.print(os, precedence);
-       }
-       if (precedence<=upper_precedence) os << ")";
-}
 
-void add::printraw(std::ostream & os) const
-{
-       debugmsg("add printraw",LOGLEVEL_PRINT);
-
-       os << "+(";
-       for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
-               os << "(";
-               (*it).rest.bp->printraw(os);
-               os << ",";
-               (*it).coeff.bp->printraw(os);        
-               os << "),";
-       }
-       os << ",hash=" << hashvalue << ",flags=" << flags;
-       os << ")";
-}
-
-void add::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
-{
-       debugmsg("add print csrc", LOGLEVEL_PRINT);
-       if (precedence <= upper_precedence)
-               os << "(";
-       
-       // Print arguments, separated by "+"
-       epvector::const_iterator it = seq.begin();
-       epvector::const_iterator itend = seq.end();
-       while (it != itend) {
-               
-               // If the coefficient is -1, it is replaced by a single minus sign
-               if (it->coeff.compare(_num1()) == 0) {
-                       it->rest.bp->printcsrc(os, type, precedence);
-               } else if (it->coeff.compare(_num_1()) == 0) {
-                       os << "-";
-                       it->rest.bp->printcsrc(os, type, precedence);
-               } else if (ex_to_numeric(it->coeff).numer().compare(_num1()) == 0) {
-                       it->rest.bp->printcsrc(os, type, precedence);
-                       os << "/";
-                       ex_to_numeric(it->coeff).denom().printcsrc(os, type, precedence);
-               } else if (ex_to_numeric(it->coeff).numer().compare(_num_1()) == 0) {
-                       os << "-";
-                       it->rest.bp->printcsrc(os, type, precedence);
-                       os << "/";
-                       ex_to_numeric(it->coeff).denom().printcsrc(os, type, precedence);
-               } else {
-                       it->coeff.bp->printcsrc(os, type, precedence);
-                       os << "*";
-                       it->rest.bp->printcsrc(os, type, precedence);
+               if (precedence <= level) {
+                       if (is_of_type(c, print_latex))
+                               c.s << ")}";
+                       else
+                               c.s << ")";
                }
-               
-               // Separator is "+", except if the following expression would have a leading minus sign
-               it++;
-               if (it != itend && !(it->coeff.compare(_num0()) < 0 || (it->coeff.compare(_num1()) == 0 && is_ex_exactly_of_type(it->rest, numeric) && it->rest.compare(_num0()) < 0)))
-                       os << "+";
-       }
-       
-       if (!overall_coeff.is_zero()) {
-               if (overall_coeff.info(info_flags::positive)) os << '+';
-               overall_coeff.bp->printcsrc(os,type,precedence);
        }
-       
-       if (precedence <= upper_precedence)
-               os << ")";
 }
 
 bool add::info(unsigned inf) const
@@ -240,7 +252,7 @@ bool add::info(unsigned inf) const
        return inherited::info(inf);
 }
 
-int add::degree(const symbol & s) const
+int add::degree(const ex & s) const
 {
        int deg = INT_MIN;
        if (!overall_coeff.is_equal(_ex0()))
@@ -255,7 +267,7 @@ int add::degree(const symbol & s) const
        return deg;
 }
 
-int add::ldegree(const symbol & s) const
+int add::ldegree(const ex & s) const
 {
        int deg = INT_MAX;
        if (!overall_coeff.is_equal(_ex0()))
@@ -269,21 +281,19 @@ int add::ldegree(const symbol & s) const
        return deg;
 }
 
-ex add::coeff(const symbol & s, int n) const
+ex add::coeff(const ex & s, int n) const
 {
        epvector coeffseq;
-       coeffseq.reserve(seq.size());
-
+       
        epvector::const_iterator it=seq.begin();
        while (it!=seq.end()) {
-               coeffseq.push_back(combine_ex_with_coeff_to_pair((*it).rest.coeff(s,n),
-                                                                (*it).coeff));
+               ex restcoeff = it->rest.coeff(s,n);
+               if (!restcoeff.is_zero())
+                       coeffseq.push_back(combine_ex_with_coeff_to_pair(restcoeff,it->coeff));
                ++it;
        }
-       if (n==0) {
-               return (new add(coeffseq,overall_coeff))->setflag(status_flags::dynallocated);
-       }
-       return (new add(coeffseq))->setflag(status_flags::dynallocated);
+       
+       return (new add(coeffseq, n==0 ? overall_coeff : default_overall_coeff()))->setflag(status_flags::dynallocated);
 }
 
 ex add::eval(int level) const