]> www.ginac.de Git - ginac.git/blobdiff - ginac/add.cpp
some more comments and cleanups to mul::expand() and ncmul::expand()
[ginac.git] / ginac / add.cpp
index 331f20495170e9ea8bf3615c3e7fad72ca515009..7764d3683fb5e97a0ccb618b71fcfe9aa7ea2cd3 100644 (file)
@@ -135,15 +135,15 @@ void add::print(const print_context & c, unsigned level) const
                        } 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) {
+                       } 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) {
+                               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());
+                               ex_to<numeric>(it->coeff).denom().print(c, precedence());
                        } else {
                                it->coeff.bp->print(c, precedence());
                                c.s << "*";
@@ -151,7 +151,7 @@ void add::print(const print_context & c, unsigned level) const
                        }
                
                        // Separator is "+", except if the following expression would have a leading minus sign
-                       it++;
+                       ++it;
                        if (it != itend && !(it->coeff.compare(_num0()) < 0 || (it->coeff.compare(_num1()) == 0 && is_exactly_a<numeric>(it->rest) && it->rest.compare(_num0()) < 0)))
                                c.s << "+";
                }
@@ -189,7 +189,7 @@ void add::print(const print_context & c, unsigned level) const
                // Then proceed with the remaining factors
                epvector::const_iterator it = seq.begin(), itend = seq.end();
                while (it != itend) {
-                       coeff = ex_to_numeric(it->coeff);
+                       coeff = ex_to<numeric>(it->coeff);
                        if (!first) {
                                if (coeff.csgn() == -1) c.s << '-'; else c.s << '+';
                        } else {
@@ -215,7 +215,7 @@ void add::print(const print_context & c, unsigned level) const
                                        c.s << '*';
                        }
                        it->rest.print(c, precedence());
-                       it++;
+                       ++it;
                }
 
                if (precedence() <= level) {
@@ -236,16 +236,20 @@ bool add::info(unsigned inf) const
                case info_flags::rational_polynomial:
                case info_flags::crational_polynomial:
                case info_flags::rational_function: {
-                       for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
+                       epvector::const_iterator i = seq.begin(), end = seq.end();
+                       while (i != end) {
                                if (!(recombine_pair_to_ex(*i).info(inf)))
                                        return false;
+                               ++i;
                        }
                        return overall_coeff.info(inf);
                }
                case info_flags::algebraic: {
-                       for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
+                       epvector::const_iterator i = seq.begin(), end = seq.end();
+                       while (i != end) {
                                if ((recombine_pair_to_ex(*i).info(inf)))
                                        return true;
+                               ++i;
                        }
                        return false;
                }
@@ -256,14 +260,16 @@ bool add::info(unsigned inf) const
 int add::degree(const ex & s) const
 {
        int deg = INT_MIN;
-       if (!overall_coeff.is_equal(_ex0()))
+       if (!overall_coeff.is_zero())
                deg = 0;
        
-       int cur_deg;
-       for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
-               cur_deg = (*cit).rest.degree(s);
-               if (cur_deg>deg)
+       // Find maximum of degrees of individual terms
+       epvector::const_iterator i = seq.begin(), end = seq.end();
+       while (i != end) {
+               int cur_deg = i->rest.degree(s);
+               if (cur_deg > deg)
                        deg = cur_deg;
+               ++i;
        }
        return deg;
 }
@@ -271,30 +277,34 @@ int add::degree(const ex & s) const
 int add::ldegree(const ex & s) const
 {
        int deg = INT_MAX;
-       if (!overall_coeff.is_equal(_ex0()))
+       if (!overall_coeff.is_zero())
                deg = 0;
        
-       int cur_deg;
-       for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
-               cur_deg = (*cit).rest.ldegree(s);
-               if (cur_deg<deg) deg=cur_deg;
+       // Find minimum of degrees of individual terms
+       epvector::const_iterator i = seq.begin(), end = seq.end();
+       while (i != end) {
+               int cur_deg = i->rest.ldegree(s);
+               if (cur_deg < deg)
+                       deg = cur_deg;
+               ++i;
        }
        return deg;
 }
 
 ex add::coeff(const ex & s, int n) const
 {
-       epvector coeffseq;
-       
-       epvector::const_iterator it=seq.begin();
-       while (it!=seq.end()) {
-               ex restcoeff = it->rest.coeff(s,n);
+       epvector *coeffseq = new epvector();
+
+       // Calculate sum of coefficients in each term
+       epvector::const_iterator i = seq.begin(), end = seq.end();
+       while (i != end) {
+               ex restcoeff = i->rest.coeff(s, n);
                if (!restcoeff.is_zero())
-                       coeffseq.push_back(combine_ex_with_coeff_to_pair(restcoeff,it->coeff));
-               ++it;
+                       coeffseq->push_back(combine_ex_with_coeff_to_pair(restcoeff, i->coeff));
+               ++i;
        }
-       
-       return (new add(coeffseq, n==0 ? overall_coeff : default_overall_coeff()))->setflag(status_flags::dynallocated);
+
+       return (new add(coeffseq, n==0 ? overall_coeff : _ex0()))->setflag(status_flags::dynallocated);
 }
 
 ex add::eval(int level) const
@@ -304,19 +314,21 @@ ex add::eval(int level) const
        
        debugmsg("add eval",LOGLEVEL_MEMBER_FUNCTION);
        
-       epvector * evaled_seqp = evalchildren(level);
-       if (evaled_seqp!=0) {
+       epvector *evaled_seqp = evalchildren(level);
+       if (evaled_seqp) {
                // do more evaluation later
-               return (new add(evaled_seqp,overall_coeff))->
+               return (new add(evaled_seqp, overall_coeff))->
                       setflag(status_flags::dynallocated);
        }
        
 #ifdef DO_GINAC_ASSERT
-       for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
-               GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,add));
-               if (is_ex_exactly_of_type((*cit).rest,numeric))
+       epvector::const_iterator i = seq.begin(), end = seq.end();
+       while (i != end) {
+               GINAC_ASSERT(!is_ex_exactly_of_type(i->rest,add));
+               if (is_ex_exactly_of_type(i->rest,numeric))
                        dbgprint();
-               GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,numeric));
+               GINAC_ASSERT(!is_ex_exactly_of_type(i->rest,numeric));
+               ++i;
        }
 #endif // def DO_GINAC_ASSERT
        
@@ -330,7 +342,7 @@ ex add::eval(int level) const
        if (seq_size==0) {
                // +(;c) -> c
                return overall_coeff;
-       } else if ((seq_size==1) && overall_coeff.is_equal(_ex0())) {
+       } else if ((seq_size==1) && overall_coeff.is_zero()) {
                // +(x;0) -> x
                return recombine_pair_to_ex(*(seq.begin()));
        }
@@ -354,37 +366,48 @@ ex add::evalm(void) const
                s->push_back(split_ex_to_pair(m));
                if (is_ex_of_type(m, matrix)) {
                        if (first_term) {
-                               sum = ex_to_matrix(m);
+                               sum = ex_to<matrix>(m);
                                first_term = false;
                        } else
-                               sum = sum.add(ex_to_matrix(m));
+                               sum = sum.add(ex_to<matrix>(m));
                } else
                        all_matrices = false;
                it++;
        }
 
-       if (all_matrices)
+       if (all_matrices) {
+               delete s;
                return sum + overall_coeff;
-       else
+       else
                return (new add(s, overall_coeff))->setflag(status_flags::dynallocated);
 }
 
 ex add::simplify_ncmul(const exvector & v) const
 {
-       if (seq.size()==0) {
+       if (seq.empty())
                return inherited::simplify_ncmul(v);
-       }
-       return (*seq.begin()).rest.simplify_ncmul(v);
+       else
+               return seq.begin()->rest.simplify_ncmul(v);
 }    
 
 // protected
 
 /** Implementation of ex::diff() for a sum. It differentiates each term.
  *  @see ex::diff */
-ex add::derivative(const symbol & s) const
+ex add::derivative(const symbol & y) const
 {
-       // D(a+b+c)=D(a)+D(b)+D(c)
-       return (new add(diffchildren(s)))->setflag(status_flags::dynallocated);
+       epvector *s = new epvector();
+       s->reserve(seq.size());
+       
+       // Only differentiate the "rest" parts of the expairs. This is faster
+       // than the default implementation in basic::derivative() although
+       // if performs the same function (differentiate each term).
+       epvector::const_iterator i = seq.begin(), end = seq.end();
+       while (i != end) {
+               s->push_back(combine_ex_with_coeff_to_pair(i->rest.diff(y), i->coeff));
+               ++i;
+       }
+       return (new add(s, _ex0()))->setflag(status_flags::dynallocated);
 }
 
 int add::compare_same_type(const basic & other) const
@@ -399,18 +422,18 @@ bool add::is_equal_same_type(const basic & other) const
 
 unsigned add::return_type(void) const
 {
-       if (seq.size()==0) {
+       if (seq.empty())
                return return_types::commutative;
-       }
-       return (*seq.begin()).rest.return_type();
+       else
+               return seq.begin()->rest.return_type();
 }
    
 unsigned add::return_type_tinfo(void) const
 {
-       if (seq.size()==0) {
+       if (seq.empty())
                return tinfo_key;
-       }
-       return (*seq.begin()).rest.return_type_tinfo();
+       else
+               return seq.begin()->rest.return_type_tinfo();
 }
 
 ex add::thisexpairseq(const epvector & v, const ex & oc) const
@@ -426,8 +449,8 @@ ex add::thisexpairseq(epvector * vp, const ex & oc) const
 expair add::split_ex_to_pair(const ex & e) const
 {
        if (is_ex_exactly_of_type(e,mul)) {
-               const mul &mulref = ex_to_mul(e);
-               ex numfactor(mulref.overall_coeff);
+               const mul &mulref(ex_to<mul>(e));
+               ex numfactor = mulref.overall_coeff;
                mul *mulcopyp = new mul(mulref);
                mulcopyp->overall_coeff = _ex1();
                mulcopyp->clearflag(status_flags::evaluated);
@@ -443,8 +466,8 @@ expair add::combine_ex_with_coeff_to_pair(const ex & e,
 {
        GINAC_ASSERT(is_ex_exactly_of_type(c, numeric));
        if (is_ex_exactly_of_type(e, mul)) {
-               const mul &mulref = ex_to_mul(e);
-               ex numfactor(mulref.overall_coeff);
+               const mul &mulref(ex_to<mul>(e));
+               ex numfactor = mulref.overall_coeff;
                mul *mulcopyp = new mul(mulref);
                mulcopyp->overall_coeff = _ex1();
                mulcopyp->clearflag(status_flags::evaluated);
@@ -455,11 +478,11 @@ expair add::combine_ex_with_coeff_to_pair(const ex & e,
                else if (are_ex_trivially_equal(numfactor, _ex1()))
                        return expair(*mulcopyp, c);
                else
-                       return expair(*mulcopyp, ex_to_numeric(numfactor).mul_dyn(ex_to_numeric(c)));
+                       return expair(*mulcopyp, ex_to<numeric>(numfactor).mul_dyn(ex_to<numeric>(c)));
        } else if (is_ex_exactly_of_type(e, numeric)) {
                if (are_ex_trivially_equal(c, _ex1()))
                        return expair(e, _ex1());
-               return expair(ex_to_numeric(e).mul_dyn(ex_to_numeric(c)), _ex1());
+               return expair(ex_to<numeric>(e).mul_dyn(ex_to<numeric>(c)), _ex1());
        }
        return expair(e, c);
 }
@@ -471,16 +494,16 @@ expair add::combine_pair_with_coeff_to_pair(const expair & p,
        GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
 
        if (is_ex_exactly_of_type(p.rest,numeric)) {
-               GINAC_ASSERT(ex_to_numeric(p.coeff).is_equal(_num1())); // should be normalized
-               return expair(ex_to_numeric(p.rest).mul_dyn(ex_to_numeric(c)),_ex1());
+               GINAC_ASSERT(ex_to<numeric>(p.coeff).is_equal(_num1())); // should be normalized
+               return expair(ex_to<numeric>(p.rest).mul_dyn(ex_to<numeric>(c)),_ex1());
        }
 
-       return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
+       return expair(p.rest,ex_to<numeric>(p.coeff).mul_dyn(ex_to<numeric>(c)));
 }
        
 ex add::recombine_pair_to_ex(const expair & p) const
 {
-       if (ex_to_numeric(p.coeff).is_equal(_num1()))
+       if (ex_to<numeric>(p.coeff).is_equal(_num1()))
                return p.rest;
        else
                return p.rest*p.coeff;
@@ -488,17 +511,13 @@ ex add::recombine_pair_to_ex(const expair & p) const
 
 ex add::expand(unsigned options) const
 {
-       if (flags & status_flags::expanded)
-               return *this;
-       
-       epvector * vp = expandchildren(options);
-       if (vp==0) {
+       epvector *vp = expandchildren(options);
+       if (vp == NULL) {
                // the terms have not changed, so it is safe to declare this expanded
-               setflag(status_flags::expanded);
-               return *this;
+               return (options == 0) ? setflag(status_flags::expanded) : *this;
        }
        
-       return (new add(vp,overall_coeff))->setflag(status_flags::expanded | status_flags::dynallocated);
+       return (new add(vp, overall_coeff))->setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
 }
 
 } // namespace GiNaC