- // TODO: optimize
- if (inf==info_flags::polynomial || inf==info_flags::integer_polynomial || inf==info_flags::rational_polynomial || inf==info_flags::rational_function) {
- for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
- if (!(recombine_pair_to_ex(*it).info(inf)))
- return false;
- }
- return true;
- } else {
- return expairseq::info(inf);
- }
-}
-
-typedef vector<int> intvector;
-
-int mul::degree(symbol const & s) const
-{
- int deg_sum=0;
- for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
- deg_sum+=(*cit).rest.degree(s) * ex_to_numeric((*cit).coeff).to_int();
- }
- return deg_sum;
-}
-
-int mul::ldegree(symbol const & s) const
-{
- int deg_sum=0;
- for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
- deg_sum+=(*cit).rest.ldegree(s) * ex_to_numeric((*cit).coeff).to_int();
- }
- return deg_sum;
-}
-
-ex mul::coeff(symbol const & s, int const n) const
-{
- exvector coeffseq;
- coeffseq.reserve(seq.size()+1);
-
- if (n==0) {
- // product of individual coeffs
- // if a non-zero power of s is found, the resulting product will be 0
- epvector::const_iterator it=seq.begin();
- while (it!=seq.end()) {
- coeffseq.push_back(recombine_pair_to_ex(*it).coeff(s,n));
- ++it;
- }
- coeffseq.push_back(overall_coeff);
- return (new mul(coeffseq))->setflag(status_flags::dynallocated);
- }
-
- epvector::const_iterator it=seq.begin();
- bool coeff_found=0;
- while (it!=seq.end()) {
- ex t=recombine_pair_to_ex(*it);
- ex c=t.coeff(s,n);
- if (!c.is_zero()) {
- coeffseq.push_back(c);
- coeff_found=1;
- } else {
- coeffseq.push_back(t);
- }
- ++it;
- }
- if (coeff_found) {
- coeffseq.push_back(overall_coeff);
- return (new mul(coeffseq))->setflag(status_flags::dynallocated);
- }
-
- return exZERO();
-}
-
-/*
-ex mul::eval(int level) const
-{
- // 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 );
-}
-*/
-
+ switch (inf) {
+ case info_flags::polynomial:
+ case info_flags::integer_polynomial:
+ case info_flags::cinteger_polynomial:
+ case info_flags::rational_polynomial:
+ case info_flags::crational_polynomial:
+ case info_flags::rational_function: {
+ 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: {
+ 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;
+ }
+ }
+ return inherited::info(inf);
+}
+
+int mul::degree(const ex & s) const
+{
+ // Sum up degrees of factors
+ int deg_sum = 0;
+ 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();
+ ++i;
+ }
+ return deg_sum;
+}
+
+int mul::ldegree(const ex & s) const
+{
+ // Sum up degrees of factors
+ int deg_sum = 0;
+ 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();
+ ++i;
+ }
+ return deg_sum;
+}
+
+ex mul::coeff(const ex & s, int n) const
+{
+ exvector coeffseq;
+ coeffseq.reserve(seq.size()+1);
+
+ if (n==0) {
+ // product of individual coeffs
+ // if a non-zero power of s is found, the resulting product will be 0
+ epvector::const_iterator i = seq.begin(), end = seq.end();
+ while (i != end) {
+ coeffseq.push_back(recombine_pair_to_ex(*i).coeff(s,n));
+ ++i;
+ }
+ coeffseq.push_back(overall_coeff);
+ return (new mul(coeffseq))->setflag(status_flags::dynallocated);
+ }
+
+ epvector::const_iterator i = seq.begin(), end = seq.end();
+ bool coeff_found = false;
+ while (i != end) {
+ ex t = recombine_pair_to_ex(*i);
+ ex c = t.coeff(s, n);
+ if (!c.is_zero()) {
+ coeffseq.push_back(c);
+ coeff_found = 1;
+ } else {
+ coeffseq.push_back(t);
+ }
+ ++i;
+ }
+ if (coeff_found) {
+ coeffseq.push_back(overall_coeff);
+ return (new mul(coeffseq))->setflag(status_flags::dynallocated);
+ }
+
+ return _ex0;
+}
+
+/** Perform automatic term rewriting rules in this class. In the following
+ * x, x1, x2,... stand for a symbolic variables of type ex and c, c1, c2...
+ * stand for such expressions that contain a plain number.
+ * - *(...,x;0) -> 0
+ * - *(+(x1,x2,...);c) -> *(+(*(x1,c),*(x2,c),...))
+ * - *(x;1) -> x
+ * - *(;c) -> c
+ *
+ * @param level cut-off in recursive evaluation */