- // 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 DOASSERT
- for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
- 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)) {
- ASSERT(all_numeric);
- if ((*cit).coeff.is_equal(exONE())) {
- ASSERT(all_coeff_1);
- } else {
- all_coeff_1=false;
- }
- } else {
- all_numeric=false;
- }
- } while (cit!=seq.begin());
- }
-#endif // def DOASSERT
- 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 DOASSERT
- for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
- 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)) {
- ASSERT(all_numeric);
- if ((*cit).coeff.is_equal(exONE())) {
- ASSERT(all_coeff_1);
- } else {
- all_coeff_1=false;
- }
- } else {
- all_numeric=false;
- }
- } while (cit!=seq.begin());
- }
-#endif // def DOASSERT
-
- 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 );
-}
-*/