- // 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);
- }
-}
-
-int add::degree(symbol const & s) const
-{
- int deg=INT_MIN;
- if (!overall_coeff.is_equal(exZERO())) {
- deg=0;
- }
- int cur_deg;
- for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
- cur_deg=(*cit).rest.degree(s);
- if (cur_deg>deg) deg=cur_deg;
- }
- return deg;
-}
-
-int add::ldegree(symbol const & s) const
-{
- int deg=INT_MAX;
- if (!overall_coeff.is_equal(exZERO())) {
- deg=0;
- }
- int cur_deg;
- for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
- cur_deg=(*cit).rest.ldegree(s);
- if (cur_deg<deg) deg=cur_deg;
- }
- return deg;
-}
-
-ex add::coeff(symbol const & s, int const n) const
-{
- epvector coeffseq;
- coeffseq.reserve(seq.size());
-
- epvector::const_iterator it=seq.begin();
- while (it!=seq.end()) {
- coeffseq.push_back(combine_ex_with_coeff_to_pair((*it).rest.coeff(s,n),
- (*it).coeff));
- ++it;
- }
- if (n==0) {
- return (new add(coeffseq,overall_coeff))->setflag(status_flags::dynallocated);
- }
- return (new add(coeffseq))->setflag(status_flags::dynallocated);
-}
-
-/*
-ex add::eval(int level) const
-{
- // simplifications: +(...,x,c1,c2) -> +(...,x,c1+c2) (c1, c2 numeric())
- // +(...,(c1,c2)) -> (...,(c1*c2,1)) (normalize)
- // +(...,x,0) -> +(...,x)
- // +(x) -> x
- // +() -> 0
-
- debugmsg("add eval",LOGLEVEL_MEMBER_FUNCTION);
-
- epvector newseq=seq;
- epvector::iterator it1,it2;
-
- // +(...,x,c1,c2) -> +(...,x,c1+c2) (c1, c2 numeric())
- it2=newseq.end()-1;
- it1=it2-1;
- while ((newseq.size()>=2)&&is_exactly_of_type(*(*it1).rest.bp,numeric)&&
- is_exactly_of_type(*(*it2).rest.bp,numeric)) {
- *it1=expair(ex_to_numeric((*it1).rest).mul(ex_to_numeric((*it1).coeff))
- .add(ex_to_numeric((*it2).rest).mul(ex_to_numeric((*it2).coeff))),exONE());
- newseq.pop_back();
- it2=newseq.end()-1;
- it1=it2-1;
- }
-
- if ((newseq.size()>=1)&&is_exactly_of_type(*(*it2).rest.bp,numeric)) {
- // +(...,(c1,c2)) -> (...,(c1*c2,1)) (normalize)
- *it2=expair(ex_to_numeric((*it2).rest).mul(ex_to_numeric((*it2).coeff)),exONE());
- // +(...,x,0) -> +(...,x)
- if (ex_to_numeric((*it2).rest).compare(0)==0) {
- newseq.pop_back();
- }
- }
-
- if (newseq.size()==0) {
- // +() -> 0
- return exZERO();
- } else if (newseq.size()==1) {
- // +(x) -> x
- return recombine_pair_to_ex(*(newseq.begin()));
- }
-
- return (new add(newseq,1))->setflag(status_flags::dynallocated |
- status_flags::evaluated );
-}
-*/
-
-/*
-ex add::eval(int level) const
-{
- // simplifications: +(...,x,c1,c2) -> +(...,x,c1+c2) (c1, c2 numeric())
- // +(...,(c1,c2)) -> (...,(c1*c2,1)) (normalize)
- // +(...,x,0) -> +(...,x)
- // +(x) -> x
- // +() -> 0
-
- debugmsg("add 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,add));
- GINAC_ASSERT(!(is_ex_exactly_of_type((*cit).rest,numeric)&&
- (ex_to_numeric((*cit).coeff).compare(numONE())!=0)));
- }
-#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 add(evaled_seqp))->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,add));
- GINAC_ASSERT(!(is_ex_exactly_of_type((*cit).rest,numeric)&&
- (ex_to_numeric((*cit).coeff).compare(numONE())!=0)));
- }
-#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,c2) -> +(...,x,c1+c2) (c1, c2 numeric())
- if ((!seq_copied)&&(seq_size>=2)&&
- is_ex_exactly_of_type(last_expair.rest,numeric)&&
- is_ex_exactly_of_type(next_to_last_expair.rest,numeric)) {
- 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)&&
- is_ex_exactly_of_type((*it2).rest,numeric)) {
- *it1=expair(ex_to_numeric((*it1).rest).mul(ex_to_numeric((*it1).coeff))
- .add_dyn(ex_to_numeric((*it2).rest).mul(ex_to_numeric((*it2).coeff))),exONE());
- newseq.pop_back();
- it2=newseq.end()-1;
- it1=it2-1;
- }
-
- // +(...,(c1,c2)) -> (...,(c1*c2,1)) (normalize)
- if ((!seq_copied)&&(seq_size>=1)&&
- (is_ex_exactly_of_type(last_expair.rest,numeric))&&
- (ex_to_numeric(last_expair.coeff).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).coeff).compare(numONE())!=0)) {
- *it2=expair(ex_to_numeric((*it2).rest).mul_dyn(ex_to_numeric((*it2).coeff)),exONE());
- }
-
- // +(...,x,0) -> +(...,x)
- if ((!seq_copied)&&(seq_size>=1)&&
- (is_ex_exactly_of_type(last_expair.rest,numeric))&&
- (ex_to_numeric(last_expair.rest).is_zero())) {
- 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).is_zero())) {
- newseq.pop_back();
- }
-
- // +() -> 0
- if ((!seq_copied)&&(seq_size==0)) {
- return exZERO();
- } else if (seq_copied&&(newseq.size()==0)) {
- return exZERO();
- }
-
- // +(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 add(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::real:
+ case info_flags::rational:
+ case info_flags::integer:
+ case info_flags::crational:
+ case info_flags::cinteger:
+ case info_flags::positive:
+ case info_flags::nonnegative:
+ case info_flags::posint:
+ case info_flags::nonnegint:
+ case info_flags::even:
+ 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;
+ }
+ if (overall_coeff.is_zero() && (inf == info_flags::positive || inf == info_flags::posint))
+ return true;
+ 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);
+}
+
+bool add::is_polynomial(const ex & var) const
+{
+ for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
+ if (!(i->rest).is_polynomial(var)) {
+ return false;
+ }
+ }
+ return true;
+}
+
+int add::degree(const ex & s) const
+{
+ int deg = std::numeric_limits<int>::min();
+ if (!overall_coeff.is_zero())
+ deg = 0;
+
+ // Find maximum of degrees of individual terms
+ epvector::const_iterator i = seq.begin(), end = seq.end();
+ while (i != end) {
+ int cur_deg = i->rest.degree(s);
+ if (cur_deg > deg)
+ deg = cur_deg;
+ ++i;
+ }
+ return deg;
+}
+
+int add::ldegree(const ex & s) const
+{
+ int deg = std::numeric_limits<int>::max();
+ if (!overall_coeff.is_zero())
+ deg = 0;
+
+ // Find minimum of degrees of individual terms
+ epvector::const_iterator i = seq.begin(), end = seq.end();
+ while (i != end) {
+ int cur_deg = i->rest.ldegree(s);
+ if (cur_deg < deg)
+ deg = cur_deg;
+ ++i;
+ }
+ return deg;
+}
+
+ex add::coeff(const ex & s, int n) const
+{
+ std::auto_ptr<epvector> coeffseq(new epvector);
+ std::auto_ptr<epvector> coeffseq_cliff(new epvector);
+ int rl = clifford_max_label(s);
+ bool do_clifford = (rl != -1);
+ bool nonscalar = false;
+
+ // Calculate sum of coefficients in each term
+ epvector::const_iterator i = seq.begin(), end = seq.end();
+ while (i != end) {
+ ex restcoeff = i->rest.coeff(s, n);
+ if (!restcoeff.is_zero()) {
+ if (do_clifford) {
+ if (clifford_max_label(restcoeff) == -1) {
+ coeffseq_cliff->push_back(combine_ex_with_coeff_to_pair(ncmul(restcoeff, dirac_ONE(rl)), i->coeff));
+ } else {
+ coeffseq_cliff->push_back(combine_ex_with_coeff_to_pair(restcoeff, i->coeff));
+ nonscalar = true;
+ }
+ }
+ coeffseq->push_back(combine_ex_with_coeff_to_pair(restcoeff, i->coeff));
+ }
+ ++i;
+ }
+
+ return (new add(nonscalar ? coeffseq_cliff : coeffseq,
+ n==0 ? overall_coeff : _ex0))->setflag(status_flags::dynallocated);
+}
+
+/** Perform automatic term rewriting rules in this class. In the following
+ * x stands for a symbolic variables of type ex and c stands for such
+ * an expression that contain a plain number.
+ * - +(;c) -> c
+ * - +(x;0) -> x
+ *
+ * @param level cut-off in recursive evaluation */