- // simplifications: *(...,x,(c1,1),(c2,1)) -> *(...,x,(c1*c2,1)) (c1, c2 numeric(), move pairs to end first)
- // *(...,x,1) -> *(...,x)
- // *(...,x,0) -> 0
- // *(+(x,y,...),(c,1)) -> *(+(*(x,c),*(y,c),...)) (c numeric())
- // *(x) -> x
- // *() -> 1
-
- debugmsg("mul eval",LOGLEVEL_MEMBER_FUNCTION);
-
- if ((level==1)&&(flags & status_flags::evaluated)) {
-#ifdef DO_GINAC_ASSERT
- for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
- GINAC_ASSERT((!is_ex_exactly_of_type((*cit).rest,mul))||
- (!(ex_to_numeric((*cit).coeff).is_integer())));
- }
-
- // test if all numerics were moved to the end and
- // all numerics with coeff 1 to the very end
- if (seq.size()!=0) {
- epvector::const_iterator cit=seq.end();
- bool all_coeff_1=true;
- bool all_numeric=true;
- do {
- cit--;
- if (is_ex_exactly_of_type((*cit).rest,numeric)) {
- GINAC_ASSERT(all_numeric);
- if ((*cit).coeff.is_equal(exONE())) {
- GINAC_ASSERT(all_coeff_1);
- } else {
- all_coeff_1=false;
- }
- } else {
- all_numeric=false;
- }
- } while (cit!=seq.begin());
- }
-#endif // def DO_GINAC_ASSERT
- return *this;
- }
-
- epvector newseq;
- epvector::iterator it1,it2;
- bool seq_copied=false;
-
- epvector * evaled_seqp=evalchildren(level);
- if (evaled_seqp!=0) {
- // do more evaluation later
- return (new mul(evaled_seqp))->setflag(status_flags::dynallocated);
- }
-
- // combine pairs with coeff 1 (all numerics should be at end, assert below)
- if (seq.size()>1) {
- // count number of pairs with coeff 1
- unsigned num_coeff_1=0;
- bool still_numeric=true;
- epvector::const_iterator cit=seq.end();
- unsigned first_pos;
- unsigned second_pos;
- do {
- cit--;
- if (is_ex_exactly_of_type((*cit).rest,numeric)) {
- if ((*cit).coeff.is_equal(exONE())) {
- num_coeff_1++;
- }
- } else {
- still_numeric=false;
- }
- } while ((cit!=seq.begin())&&still_numeric);
- if (num_coeff_1>1) {
- newseq=seq;
-
- }
-
-
-#ifdef DO_GINAC_ASSERT
- for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
- GINAC_ASSERT((!is_ex_exactly_of_type((*cit).rest,mul))||
- (!(ex_to_numeric((*cit).coeff).is_integer())));
- }
-
- // test if all numerics were moved to the end and
- // all numerics with coeff 1 to the very end
- if (seq.size()!=0) {
- epvector::const_iterator cit=seq.end();
- bool all_coeff_1=true;
- bool all_numeric=true;
- do {
- cit--;
- if (is_ex_exactly_of_type((*cit).rest,numeric)) {
- GINAC_ASSERT(all_numeric);
- if ((*cit).coeff.is_equal(exONE())) {
- GINAC_ASSERT(all_coeff_1);
- } else {
- all_coeff_1=false;
- }
- } else {
- all_numeric=false;
- }
- } while (cit!=seq.begin());
- }
-#endif // def DO_GINAC_ASSERT
-
- if (flags & status_flags::evaluated) {
- return *this;
- }
-
- expair const & last_expair=*(seq.end()-1);
- expair const & next_to_last_expair=*(seq.end()-2);
- int seq_size = seq.size();
-
- // *(...,x,(c1,1),(c2,1)) -> *(...,x,(c1*c2,1)) (c1, c2 numeric())
- if ((!seq_copied) && (seq_size>=2) &&
- is_ex_exactly_of_type(last_expair.rest,numeric) &&
- ex_to_numeric(last_expair.coeff).is_equal(numONE()) &&
- is_ex_exactly_of_type(next_to_last_expair.rest,numeric) &&
- ex_to_numeric(next_to_last_expair.coeff).is_equal(numONE()) ) {
- newseq=seq;
- seq_copied=true;
- it2=newseq.end()-1;
- it1=it2-1;
- }
- while (seq_copied && (newseq.size()>=2) &&
- is_ex_exactly_of_type((*it1).rest,numeric) &&
- ex_to_numeric((*it1).coeff).is_equal(numONE()) &&
- is_ex_exactly_of_type((*it2).rest,numeric) &&
- ex_to_numeric((*it2).coeff).is_equal(numONE()) ) {
- *it1=expair(ex_to_numeric((*it1).rest).mul_dyn(ex_to_numeric((*it2).rest)),exONE());
- newseq.pop_back();
- it2=newseq.end()-1;
- it1=it2-1;
- }
-
- // *(...,x,1) -> *(...,x)
- if ((!seq_copied) && (seq_size>=1) &&
- (is_ex_exactly_of_type(last_expair.rest,numeric)) &&
- (ex_to_numeric(last_expair.rest).compare(numONE())==0)) {
- newseq=seq;
- seq_copied=true;
- it2=newseq.end()-1;
- }
- if (seq_copied && (newseq.size()>=1) &&
- (is_ex_exactly_of_type((*it2).rest,numeric)) &&
- (ex_to_numeric((*it2).rest).compare(numONE())==0)) {
- newseq.pop_back();
- it2=newseq.end()-1;
- }
-
- // *(...,x,0) -> 0
- if ((!seq_copied) && (seq_size>=1) &&
- (is_ex_exactly_of_type(last_expair.rest,numeric)) &&
- (ex_to_numeric(last_expair.rest).is_zero())) {
- return exZERO();
- }
- if (seq_copied && (newseq.size()>=1) &&
- (is_ex_exactly_of_type((*it2).rest,numeric)) &&
- (ex_to_numeric((*it2).rest).is_zero())) {
- return exZERO();
- }
-
- // *(+(x,y,...),c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
- if ((!seq_copied) && (seq_size==2) &&
- is_ex_exactly_of_type(next_to_last_expair.rest,add) &&
- is_ex_exactly_of_type(last_expair.rest,numeric) &&
- ex_to_numeric(last_expair.coeff).is_equal(numONE()) &&
- (ex_to_numeric(next_to_last_expair.coeff).compare(numONE())==0)) {
- add const & addref=ex_to_add(next_to_last_expair.rest);
- epvector distrseq;
- distrseq.reserve(addref.seq.size());
- for (epvector::const_iterator cit=addref.seq.begin(); cit!=addref.seq.end(); ++cit) {
- distrseq.push_back(addref.combine_pair_with_coeff_to_pair(*cit,
- last_expair.rest));
- }
- // special treatment for the last element if it is numeric (to
- // avoid terms like (2/3)*(3/2)) is no longer necessary, this
- // is handled in add::combine_pair_with_coeff_to_pair()
- return (new add(distrseq,1))->setflag(status_flags::dynallocated |
- status_flags::evaluated );
- }
- if (seq_copied && (newseq.size()==2) &&
- is_ex_exactly_of_type(newseq[0].rest,add) &&
- is_ex_exactly_of_type(newseq[1].rest,numeric) &&
- ex_to_numeric(newseq[1].coeff).is_equal(numONE()) &&
- (ex_to_numeric(newseq[0].coeff).compare(numONE())==0)) {
- add const & addref=ex_to_add(newseq[0].rest);
- epvector distrseq;
- distrseq.reserve(addref.seq.size());
- for (epvector::const_iterator cit=addref.seq.begin(); cit!=addref.seq.end(); ++cit) {
- distrseq.push_back(addref.combine_pair_with_coeff_to_pair(*cit,
- newseq[1].rest));
- }
- // special treatment for the last element if it is numeric (to
- // avoid terms like (2/3)*(3/2)) is no longer necessary, this
- // is handled in add::combine_pair_with_coeff_to_pair()
- return (new add(distrseq,1))->setflag(status_flags::dynallocated |
- status_flags::evaluated );
- }
-
- // *() -> 1
- if ((!seq_copied) && (seq_size==0)) {
- return exONE();
- } else if (seq_copied && (newseq.size()==0)) {
- return exONE();
- }
-
- // *(x) -> x
- if ((!seq_copied) && (seq_size==1)) {
- return recombine_pair_to_ex(*(seq.begin()));
- } else if (seq_copied && (newseq.size()==1)) {
- return recombine_pair_to_ex(*(newseq.begin()));
- }
-
- if (!seq_copied) return this->hold();
-
- return (new mul(newseq,1))->setflag(status_flags::dynallocated |
- status_flags::evaluated );
-}
-*/
+ epvector evaled = evalchildren(level);
+ if (unlikely(!evaled.empty())) {
+ // do more evaluation later
+ return (new mul(std::move(evaled), overall_coeff))->
+ setflag(status_flags::dynallocated);
+ }
+
+ if (flags & status_flags::evaluated) {
+ GINAC_ASSERT(seq.size()>0);
+ GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_equal(_ex1));
+ return *this;
+ }
+
+ size_t seq_size = seq.size();
+ if (overall_coeff.is_zero()) {
+ // *(...,x;0) -> 0
+ return _ex0;
+ } else if (seq_size==0) {
+ // *(;c) -> c
+ return overall_coeff;
+ } else if (seq_size==1 && overall_coeff.is_equal(_ex1)) {
+ // *(x;1) -> x
+ return recombine_pair_to_ex(*(seq.begin()));
+ } else if ((seq_size==1) &&
+ is_exactly_a<add>((*seq.begin()).rest) &&
+ ex_to<numeric>((*seq.begin()).coeff).is_equal(*_num1_p)) {
+ // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
+ const add & addref = ex_to<add>((*seq.begin()).rest);
+ epvector distrseq;
+ distrseq.reserve(addref.seq.size());
+ for (auto & it : addref.seq) {
+ distrseq.push_back(addref.combine_pair_with_coeff_to_pair(it, overall_coeff));
+ }
+ return (new add(std::move(distrseq),
+ ex_to<numeric>(addref.overall_coeff).
+ 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
+
+ auto i = seq.begin(), last = seq.end();
+ auto j = seq.begin();
+ epvector s;
+ 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(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(std::move(s), ex_to<numeric>(overall_coeff).mul_dyn(oc))
+ )->setflag(status_flags::dynallocated);
+ }
+ }
+
+ return this->hold();
+}