]> www.ginac.de Git - ginac.git/blobdiff - ginac/add.cpp
- carried on with felonious plot about making ex::bp private.
[ginac.git] / ginac / add.cpp
index 5a4c805d4373d952dd3aab0712734e1ec2f635d9..c14c1587481ae6c0806ceecd078138ab4f3a928d 100644 (file)
@@ -107,7 +107,7 @@ add::add(epvector * vp, const ex & oc)
 DEFAULT_ARCHIVING(add)
 
 //////////
-// functions overriding virtual functions from bases classes
+// functions overriding virtual functions from base classes
 //////////
 
 // public
@@ -131,23 +131,23 @@ void add::print(const print_context & c, unsigned level) const
                
                        // 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());
+                               it->rest.print(c, precedence());
                        } else if (it->coeff.compare(_num_1()) == 0) {
                                c.s << "-";
-                               it->rest.bp->print(c, precedence());
+                               it->rest.print(c, precedence());
                        } else if (ex_to<numeric>(it->coeff).numer().compare(_num1()) == 0) {
-                               it->rest.bp->print(c, precedence());
+                               it->rest.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());
+                               it->rest.print(c, precedence());
                                c.s << "/";
                                ex_to<numeric>(it->coeff).denom().print(c, precedence());
                        } else {
-                               it->coeff.bp->print(c, precedence());
+                               it->coeff.print(c, precedence());
                                c.s << "*";
-                               it->rest.bp->print(c, precedence());
+                               it->rest.print(c, precedence());
                        }
                
                        // Separator is "+", except if the following expression would have a leading minus sign
@@ -159,7 +159,7 @@ void add::print(const print_context & c, unsigned level) const
                if (!overall_coeff.is_zero()) {
                        if (overall_coeff.info(info_flags::positive))
                                c.s << '+';
-                       overall_coeff.bp->print(c, precedence());
+                       overall_coeff.print(c, precedence());
                }
        
                if (precedence() <= level)
@@ -307,11 +307,15 @@ ex add::coeff(const ex & s, int n) const
        return (new add(coeffseq, n==0 ? overall_coeff : _ex0()))->setflag(status_flags::dynallocated);
 }
 
+/** Perform automatic term rewriting rules in this class.  In the following
+ *  x stands for a symbolic variables of type ex and c stands for such
+ *  an expression that contain a plain number.
+ *  - +(;c) -> c
+ *  - +(x;1) -> x
+ *
+ *  @param level cut-off in recursive evaluation */
 ex add::eval(int level) const
 {
-       // simplifications: +(;c) -> c
-       //                  +(x;1) -> x
-       
        debugmsg("add eval",LOGLEVEL_MEMBER_FUNCTION);
        
        epvector *evaled_seqp = evalchildren(level);
@@ -339,12 +343,14 @@ ex add::eval(int level) const
        }
        
        int seq_size = seq.size();
-       if (seq_size==0) {
+       if (seq_size == 0) {
                // +(;c) -> c
                return overall_coeff;
-       } else if ((seq_size==1) && overall_coeff.is_zero()) {
+       } else if (seq_size == 1 && overall_coeff.is_zero()) {
                // +(x;0) -> x
                return recombine_pair_to_ex(*(seq.begin()));
+       } else if (!overall_coeff.is_zero() && seq[0].rest.return_type() != return_types::commutative) {
+               throw (std::logic_error("add::eval(): sum of non-commutative objects has non-zero numeric term"));
        }
        return this->hold();
 }
@@ -511,9 +517,6 @@ ex add::recombine_pair_to_ex(const expair & p) const
 
 ex add::expand(unsigned options) const
 {
-       if (options == 0 && (flags & status_flags::expanded))
-               return *this;
-       
        epvector *vp = expandchildren(options);
        if (vp == NULL) {
                // the terms have not changed, so it is safe to declare this expanded