- // 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 );
-}
-*/
-
-ex mul::eval(int level) const
-{
- // simplifications *(...,x;0) -> 0
- // *(+(x,y,...);c) -> *(+(*(x,c),*(y,c),...)) (c numeric())
- // *(x;1) -> x
- // *(;c) -> c
-
- debugmsg("mul eval",LOGLEVEL_MEMBER_FUNCTION);
-
- epvector * evaled_seqp=evalchildren(level);
- if (evaled_seqp!=0) {
- // do more evaluation later
- return (new mul(evaled_seqp,overall_coeff))->
- setflag(status_flags::dynallocated);
- }
-
-#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())));
- ASSERT(!((*cit).is_numeric_with_coeff_1()));
- if (is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric)) {
- printtree(cerr,0);
- }
- ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric));
- /* for paranoia */
- expair p=split_ex_to_pair(recombine_pair_to_ex(*cit));
- ASSERT(p.rest.is_equal((*cit).rest));
- ASSERT(p.coeff.is_equal((*cit).coeff));
- /* end paranoia */
- }
-#endif // def DOASSERT
-
- if (flags & status_flags::evaluated) {
- ASSERT(seq.size()>0);
- ASSERT((seq.size()>1)||!overall_coeff.is_equal(exONE()));
- return *this;
- }
-
- int seq_size=seq.size();
- if (overall_coeff.is_equal(exZERO())) {
- // *(...,x;0) -> 0
- return exZERO();
- } else if (seq_size==0) {
- // *(;c) -> c
- return overall_coeff;
- } else if ((seq_size==1)&&overall_coeff.is_equal(exONE())) {
- // *(x;1) -> x
- return recombine_pair_to_ex(*(seq.begin()));
- } else if ((seq_size==1) &&
- is_ex_exactly_of_type((*seq.begin()).rest,add) &&
- ex_to_numeric((*seq.begin()).coeff).is_equal(numONE())) {
- // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
- add const & addref=ex_to_add((*seq.begin()).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,
- overall_coeff));
- }
- return (new add(distrseq,
- ex_to_numeric(addref.overall_coeff).
- mul_dyn(ex_to_numeric(overall_coeff))))
- ->setflag(status_flags::dynallocated |
- status_flags::evaluated );
- }
- return this->hold();
+ // simplifications *(...,x;0) -> 0
+ // *(+(x,y,...);c) -> *(+(*(x,c),*(y,c),...)) (c numeric())
+ // *(x;1) -> x
+ // *(;c) -> c
+
+ debugmsg("mul eval",LOGLEVEL_MEMBER_FUNCTION);
+
+ epvector * evaled_seqp=evalchildren(level);
+ if (evaled_seqp!=0) {
+ // do more evaluation later
+ return (new mul(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,mul))||
+ (!(ex_to_numeric((*cit).coeff).is_integer())));
+ GINAC_ASSERT(!((*cit).is_numeric_with_coeff_1()));
+ if (is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric)) {
+ printtree(cerr,0);
+ }
+ GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric));
+ /* for paranoia */
+ expair p=split_ex_to_pair(recombine_pair_to_ex(*cit));
+ GINAC_ASSERT(p.rest.is_equal((*cit).rest));
+ GINAC_ASSERT(p.coeff.is_equal((*cit).coeff));
+ /* end paranoia */
+ }
+#endif // def DO_GINAC_ASSERT
+
+ if (flags & status_flags::evaluated) {
+ GINAC_ASSERT(seq.size()>0);
+ GINAC_ASSERT((seq.size()>1)||!overall_coeff.is_equal(_ex1()));
+ return *this;
+ }
+
+ int seq_size=seq.size();
+ if (overall_coeff.is_equal(_ex0())) {
+ // *(...,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_ex_exactly_of_type((*seq.begin()).rest,add) &&
+ ex_to_numeric((*seq.begin()).coeff).is_equal(_num1())) {
+ // *(+(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 (epvector::const_iterator cit=addref.seq.begin(); cit!=addref.seq.end(); ++cit) {
+ distrseq.push_back(addref.combine_pair_with_coeff_to_pair(*cit, overall_coeff));
+ }
+ return (new add(distrseq,
+ ex_to_numeric(addref.overall_coeff).
+ mul_dyn(ex_to_numeric(overall_coeff))))
+ ->setflag(status_flags::dynallocated | status_flags::evaluated);
+ }
+ return this->hold();
+}
+
+ex mul::evalf(int level) const
+{
+ if (level==1)
+ return mul(seq,overall_coeff);
+
+ if (level==-max_recursion_level)
+ throw(std::runtime_error("max recursion level reached"));
+
+ epvector s;
+ s.reserve(seq.size());
+
+ --level;
+ for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
+ s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
+ (*it).coeff));
+ }
+ return mul(s,overall_coeff.evalf(level));