]> www.ginac.de Git - ginac.git/blobdiff - ginac/mul.cpp
- This helps mul::expand() and friends to recognize objects which have
[ginac.git] / ginac / mul.cpp
index dd96a70fafdb15114985a1dce12d6d4aeba3569b..4e95a638310e313444c45736af5b1d090a8f6076 100644 (file)
@@ -34,6 +34,8 @@
 #include "lst.h"
 #include "archive.h"
 #include "utils.h"
+#include "symbol.h"
+#include "compiler.h"
 
 namespace GiNaC {
 
@@ -315,7 +317,11 @@ int mul::degree(const ex & s) const
        epvector::const_iterator i = seq.begin(), end = seq.end();
        while (i != end) {
                if (ex_to<numeric>(i->coeff).is_integer())
-                       deg_sum += i->rest.degree(s) * ex_to<numeric>(i->coeff).to_int();
+                       deg_sum += recombine_pair_to_ex(*i).degree(s);
+               else {
+                       if (i->rest.has(s))
+                               throw std::runtime_error("mul::degree() undefined degree because of non-integer exponent");
+               }
                ++i;
        }
        return deg_sum;
@@ -328,7 +334,11 @@ int mul::ldegree(const ex & s) const
        epvector::const_iterator i = seq.begin(), end = seq.end();
        while (i != end) {
                if (ex_to<numeric>(i->coeff).is_integer())
-                       deg_sum += i->rest.ldegree(s) * ex_to<numeric>(i->coeff).to_int();
+                       deg_sum += recombine_pair_to_ex(*i).ldegree(s);
+               else {
+                       if (i->rest.has(s))
+                               throw std::runtime_error("mul::ldegree() undefined degree because of non-integer exponent");
+               }
                ++i;
        }
        return deg_sum;
@@ -414,7 +424,7 @@ ex mul::eval(int level) const
                return *this;
        }
        
-       int seq_size = seq.size();
+       size_t seq_size = seq.size();
        if (overall_coeff.is_zero()) {
                // *(...,x;0) -> 0
                return _ex0;
@@ -438,9 +448,81 @@ ex mul::eval(int level) const
                }
                return (new add(distrseq,
                                ex_to<numeric>(addref.overall_coeff).
-                               mul_dyn(ex_to<numeric>(overall_coeff))))
-                     ->setflag(status_flags::dynallocated | status_flags::evaluated);
+                               mul_dyn(ex_to<numeric>(overall_coeff)))
+                      )->setflag(status_flags::dynallocated | status_flags::evaluated);
+       } else if ((seq_size >= 2) && (! (flags & status_flags::expanded))) {
+               // Strip the content and the unit part from each term. Thus
+               // things like (-x+a)*(3*x-3*a) automagically turn into - 3*(x-a)2
+
+               epvector::const_iterator last = seq.end();
+               epvector::const_iterator i = seq.begin();
+               epvector::const_iterator j = seq.begin();
+               std::auto_ptr<epvector> s(new epvector);
+               numeric oc = *_num1_p;
+               bool something_changed = false;
+               while (i!=last) {
+                       if (likely(! (is_a<add>(i->rest) && i->coeff.is_equal(_ex1)))) {
+                               // power::eval has such a rule, no need to handle powers here
+                               ++i;
+                               continue;
+                       }
+
+                       // XXX: What is the best way to check if the polynomial is a primitive? 
+                       numeric c = i->rest.integer_content();
+                       const numeric& lead_coeff =
+                               ex_to<numeric>(ex_to<add>(i->rest).seq.begin()->coeff).div_dyn(c);
+                       const bool canonicalizable = lead_coeff.is_integer();
+
+                       // XXX: The main variable is chosen in a random way, so this code 
+                       // does NOT transform the term into the canonical form (thus, in some
+                       // very unlucky event it can even loop forever). Hopefully the main
+                       // variable will be the same for all terms in *this
+                       const bool unit_normal = lead_coeff.is_pos_integer();
+                       if (likely((c == *_num1_p) && ((! canonicalizable) || unit_normal))) {
+                               ++i;
+                               continue;
+                       }
+
+                       if (! something_changed) {
+                               s->reserve(seq_size);
+                               something_changed = true;
+                       }
+
+                       while ((j!=i) && (j!=last)) {
+                               s->push_back(*j);
+                               ++j;
+                       }
+
+                       if (! unit_normal)
+                               c = c.mul(*_num_1_p);
+
+                       oc = oc.mul(c);
+
+                       // divide add by the number in place to save at least 2 .eval() calls
+                       const add& addref = ex_to<add>(i->rest);
+                       add* primitive = new add(addref);
+                       primitive->setflag(status_flags::dynallocated);
+                       primitive->clearflag(status_flags::hash_calculated);
+                       primitive->overall_coeff = ex_to<numeric>(primitive->overall_coeff).div_dyn(c);
+                       for (epvector::iterator ai = primitive->seq.begin();
+                                       ai != primitive->seq.end(); ++ai)
+                               ai->coeff = ex_to<numeric>(ai->coeff).div_dyn(c);
+                       
+                       s->push_back(expair(*primitive, _ex1));
+
+                       ++i;
+                       ++j;
+               }
+               if (something_changed) {
+                       while (j!=last) {
+                               s->push_back(*j);
+                               ++j;
+                       }
+                       return (new mul(s, ex_to<numeric>(overall_coeff).mul_dyn(oc))
+                              )->setflag(status_flags::dynallocated);
+               }
        }
+
        return this->hold();
 }
 
@@ -911,6 +993,22 @@ bool mul::can_be_further_expanded(const ex & e)
 
 ex mul::expand(unsigned options) const
 {
+       {
+       // trivial case: expanding the monomial (~ 30% of all calls)
+               epvector::const_iterator i = seq.begin(), seq_end = seq.end();
+               while ((i != seq.end()) &&  is_a<symbol>(i->rest) && i->coeff.info(info_flags::integer))
+                       ++i;
+               if (i == seq_end)
+                       return (new mul(*this))->setflag(status_flags::dynallocated | status_flags::expanded);
+       }
+
+       // do not rename indices if the object has no indices at all
+       if ((!(options & expand_options::expand_rename_idx)) && 
+                       this->info(info_flags::has_indices))
+               options |= expand_options::expand_rename_idx;
+
+       const bool skip_idx_rename = !(options & expand_options::expand_rename_idx);
+
        // First, expand the children
        std::auto_ptr<epvector> expanded_seqp = expandchildren(options);
        const epvector & expanded_seq = (expanded_seqp.get() ? *expanded_seqp : seq);
@@ -966,28 +1064,34 @@ ex mul::expand(unsigned options) const
                                ex tmp_accu = (new add(distrseq, add1.overall_coeff*add2.overall_coeff))->setflag(status_flags::dynallocated);
 
                                exvector add1_dummy_indices, add2_dummy_indices, add_indices;
+                               lst dummy_subs;
 
-                               for (epvector::const_iterator i=add1begin; i!=add1end; ++i) {
-                                       add_indices = get_all_dummy_indices_safely(i->rest);
-                                       add1_dummy_indices.insert(add1_dummy_indices.end(), add_indices.begin(), add_indices.end());
-                               }
-                               for (epvector::const_iterator i=add2begin; i!=add2end; ++i) {
-                                       add_indices = get_all_dummy_indices_safely(i->rest);
-                                       add2_dummy_indices.insert(add2_dummy_indices.end(), add_indices.begin(), add_indices.end());
-                               }
+                               if (!skip_idx_rename) {
+                                       for (epvector::const_iterator i=add1begin; i!=add1end; ++i) {
+                                               add_indices = get_all_dummy_indices_safely(i->rest);
+                                               add1_dummy_indices.insert(add1_dummy_indices.end(), add_indices.begin(), add_indices.end());
+                                       }
+                                       for (epvector::const_iterator i=add2begin; i!=add2end; ++i) {
+                                               add_indices = get_all_dummy_indices_safely(i->rest);
+                                               add2_dummy_indices.insert(add2_dummy_indices.end(), add_indices.begin(), add_indices.end());
+                                       }
 
-                               sort(add1_dummy_indices.begin(), add1_dummy_indices.end(), ex_is_less());
-                               sort(add2_dummy_indices.begin(), add2_dummy_indices.end(), ex_is_less());
-                               lst dummy_subs = rename_dummy_indices_uniquely(add1_dummy_indices, add2_dummy_indices);
+                                       sort(add1_dummy_indices.begin(), add1_dummy_indices.end(), ex_is_less());
+                                       sort(add2_dummy_indices.begin(), add2_dummy_indices.end(), ex_is_less());
+                                       dummy_subs = rename_dummy_indices_uniquely(add1_dummy_indices, add2_dummy_indices);
+                               }
 
                                // Multiply explicitly all non-numeric terms of add1 and add2:
                                for (epvector::const_iterator i2=add2begin; i2!=add2end; ++i2) {
                                        // We really have to combine terms here in order to compactify
                                        // the result.  Otherwise it would become waayy tooo bigg.
-                                       numeric oc;
-                                       distrseq.clear();
-                                       ex i2_new = (dummy_subs.op(0).nops()>0? 
-                                                                i2->rest.subs((lst)dummy_subs.op(0), (lst)dummy_subs.op(1), subs_options::no_pattern) : i2->rest);
+                                       numeric oc(*_num0_p);
+                                       epvector distrseq2;
+                                       distrseq2.reserve(add1.seq.size());
+                                       const ex i2_new = (skip_idx_rename || (dummy_subs.op(0).nops() == 0) ?
+                                                       i2->rest :
+                                                       i2->rest.subs(ex_to<lst>(dummy_subs.op(0)), 
+                                                               ex_to<lst>(dummy_subs.op(1)), subs_options::no_pattern));
                                        for (epvector::const_iterator i1=add1begin; i1!=add1end; ++i1) {
                                                // Don't push_back expairs which might have a rest that evaluates to a numeric,
                                                // since that would violate an invariant of expairseq:
@@ -995,13 +1099,12 @@ ex mul::expand(unsigned options) const
                                                if (is_exactly_a<numeric>(rest)) {
                                                        oc += ex_to<numeric>(rest).mul(ex_to<numeric>(i1->coeff).mul(ex_to<numeric>(i2->coeff)));
                                                } else {
-                                                       distrseq.push_back(expair(rest, ex_to<numeric>(i1->coeff).mul_dyn(ex_to<numeric>(i2->coeff))));
+                                                       distrseq2.push_back(expair(rest, ex_to<numeric>(i1->coeff).mul_dyn(ex_to<numeric>(i2->coeff))));
                                                }
                                        }
-                                       tmp_accu += (new add(distrseq, oc))->setflag(status_flags::dynallocated);
-                               }
+                                       tmp_accu += (new add(distrseq2, oc))->setflag(status_flags::dynallocated);
+                               } 
                                last_expanded = tmp_accu;
-
                        } else {
                                if (!last_expanded.is_equal(_ex1))
                                        non_adds.push_back(split_ex_to_pair(last_expanded));
@@ -1019,12 +1122,18 @@ ex mul::expand(unsigned options) const
                size_t n = last_expanded.nops();
                exvector distrseq;
                distrseq.reserve(n);
-               exvector va = get_all_dummy_indices_safely(mul(non_adds));
-               sort(va.begin(), va.end(), ex_is_less());
+               exvector va;
+               if (! skip_idx_rename) {
+                       va = get_all_dummy_indices_safely(mul(non_adds));
+                       sort(va.begin(), va.end(), ex_is_less());
+               }
 
                for (size_t i=0; i<n; ++i) {
                        epvector factors = non_adds;
-                       factors.push_back(split_ex_to_pair(rename_dummy_indices_uniquely(va, last_expanded.op(i))));
+                       if (skip_idx_rename)
+                               factors.push_back(split_ex_to_pair(last_expanded.op(i)));
+                       else
+                               factors.push_back(split_ex_to_pair(rename_dummy_indices_uniquely(va, last_expanded.op(i))));
                        ex term = (new mul(factors, overall_coeff))->setflag(status_flags::dynallocated);
                        if (can_be_further_expanded(term)) {
                                distrseq.push_back(term.expand());