void Generator::helpConvert(const Polynomial & p1,const Monomial & onRight, const Monomial & onLeft,const Polynomial & p2,Polynomial & result) const { AdmissibleOrder & ord = AdmissibleOrder::s_getCurrent(); typedef PolynomialIterator PI; PI w1 = p1.begin(), w2 = p2.begin(); int sz1 = p1.numberOfTerms(); int sz2 = p2.numberOfTerms(); Term t1,t2; if(sz1 && sz2) { t1 = *w1; t1 *= onRight; t2.assign(onLeft); t2 *= *w2; while(sz1 && sz2) { const Monomial & m1 = t1.MonomialPart(); const Monomial & m2 = t1.MonomialPart(); if(ord.monomialLess(m1,m2)) { result += t1; --sz1;++w1; t1 = *w1; t1 *= onRight; } else if(ord.monomialGreater(m1,m2)) { result += t2; --sz2;++w2; t2.assign(onLeft); t2 *= *w2; } else { t1.Coefficient() += t2.Coefficient(); if(t1.Coefficient().zero()) { --sz1;++w1; t1 = *w1; t1 *= onRight; }; --sz2;++w2; t2.assign(onLeft); t2 *= *w2; }; }; } else if(sz1) { --sz1;++w1; t1 = *w1; t1 *= onRight; } else if(sz2) { --sz2;++w2; t2.assign(onLeft); t2 *= *w2; }; while(sz1) { result += t1; --sz1;++w1; t1 = *w1; t1 *= onRight; }; while(sz2) { result += t2; --sz2;++w2; t2.assign(onLeft); t2 *= onRight; }; };
// INEFFICIENT void Polynomial::operator -= (const Polynomial & bPolynomial) { PolynomialIterator i = bPolynomial.begin(); const int len = bPolynomial.numberOfTerms(); for(int j=1;j<=len;++j,++i) { operator -= (*i); } };
void Polynomial::doubleProduct(const Field & f,const Monomial & x, const Polynomial & poly,const Monomial & y) { if(&poly==this) errorc(__LINE__); #ifdef CHECK_FOR_ZERO_COEFFICIENTS GBStream << "MXS:doubleProduct:poly:" << poly << '\n'; GBStream << "MXS:doubleProduct:aTerm:" << aTerm << '\n'; GBStream << "MXS:doubleProduct:bTerm:" << bTerm << '\n'; #endif setToZero(); if(!f.zero()) { const int sz = poly.numberOfTerms(); PolynomialIterator w = poly.begin(); Term t; for(int i=1;i<=sz;++i,++w) { t = *w; t.Coefficient() *= f; Monomial & m = t.MonomialPart(); Monomial result(x); result *= m; result *= y; m = result; addTermToEnd(t); } } };
void Polynomial::operator +=(const Polynomial & p) { //GBStream << *this << "+=" << p << '\n'; if(!p.zero()) { if(zero()) { operator =(p); } else { Polynomial temp(*this); setToZero(); const int sz1 = temp.numberOfTerms(); const int sz2 = p.numberOfTerms(); PolynomialIterator w1 = temp.begin(); PolynomialIterator w2 = p.begin(); int i1=1; int i2=1; Term t1 = * w1; Term t2 = * w2; while(i1<=sz1 && i2<=sz2) { int cmp = compareTwoMonomials(t1.MonomialPart(),t2.MonomialPart()); //GBStream << "Add: cmp is " << cmp << '\n'; if(cmp==0) { t1.Coefficient() += t2.CoefficientPart(); if(!t1.CoefficientPart().zero()) { //GBStream << "Adding element" << TERM(c,t1.MonomialPart()) << '\n'; addTermToEnd(t1); } ++w1; ++i1; ++w2; ++i2; if(i1<=sz1 && i2<=sz2) { t1 = * w1; t2 = * w2; } } else if(cmp<0) { //GBStream << "Adding element" << t2 << '\n'; addTermToEnd(t2); ++w2; ++i2; if(i2<=sz2) t2 = * w2; } else // if(cmp>0) { //GBStream << "Adding element" << t1 << '\n'; addTermToEnd(t1); ++w1; ++i1; if(i1<=sz1) t1 = * w1; } } for(;i1<=sz1;++i1,++w1) { //GBStream << "Adding element" << *w1 << '\n'; addTermToEnd(*w1); } for(;i2<=sz2;++i2,++w2) { //GBStream << "Adding element" << *w2 << '\n'; addTermToEnd(*w2); } } } };
void NCASink::put(const Polynomial& x) { int len = x.numberOfTerms(); if(len==0) { d_ofs << '0'; } else if(len==1) { put(*x.begin()); } else { PolynomialIterator w = x.begin(); put(*w); --len;++w; while(len) { d_ofs << " + "; put(*w); --len;++w; }; }; };
void Polynomial::doubleProduct(const Monomial & x, const Polynomial & poly,const Monomial & y) { if(&poly==this) errorc(__LINE__); #ifdef CHECK_FOR_ZERO_COEFFICIENTS GBStream << "MXS:doubleProduct:poly:" << poly << '\n'; GBStream << "MXS:doubleProduct:aTerm:" << aTerm << '\n'; GBStream << "MXS:doubleProduct:bTerm:" << bTerm << '\n'; #endif setToZero(); const int sz = poly.numberOfTerms(); PolynomialIterator w = poly.begin(); Term result; for(int i=1;i<=sz;++i,++w) { result.assign(x); result *= (*w); result *= y; addTermToEnd(result); } };
bool Polynomial::operator==(const Polynomial & x) const { bool result = true; if(this!=&x) { const int num = numberOfTerms(); if(num!=x.numberOfTerms()) { result = false; } else { PolynomialIterator j = begin(); PolynomialIterator k = x.begin(); for (int i=1;i<=num;++i,++j,++k) { if((*j)!=(*k)) { result = false; break; }; }; }; }; return result; };
void MmaSink::put(const Polynomial& x) { #ifdef DEBUG_MMASINK GBStream << "sink:polynomial " << this << ' ' << x << '\n'; #endif int len = x.numberOfTerms(); if(len==0) { MLPutInteger(d_mlink,0); ++d_count; } else if(len==1) { put(*x.begin()); } else { MLPutFunction(d_mlink,"Plus",len); ++d_count; d_count -= len; PolynomialIterator w = x.begin(); while(len) { put(*w); --len;++w; }; }; #ifdef DEBUG_MMASINK checkforerror(); #endif };
void Polynomial::operator -=(const Polynomial & p) { if(!p.zero()) { #if 0 if(zero()) { InternalContainerType::iterator w = _terms.begin(); const int sz = numberOfTerms(); for(int i=1;i<=sz;++i,++w) { (*w).Coefficient() = -1; } } else { #endif Polynomial temp(*this); setToZero(); const int sz1 = temp.numberOfTerms(); const int sz2 = p.numberOfTerms(); PolynomialIterator w1 = temp.begin(); PolynomialIterator w2 = p.begin(); int i1=1; int i2=1; #if 1 if(sz1>0) { #endif Term t1 = * w1; Term t2 = * w2; while(i1<=sz1 && i2<=sz2) { int cmp = compareTwoMonomials(t1.MonomialPart(),t2.MonomialPart()); //GBStream << "Subtract: cmp is " << cmp << '\n'; if(cmp==0) { t1.Coefficient() -= t2.CoefficientPart(); if(!t1.Coefficient().zero()) { //GBStream << "-=: Adding element" << TERM(c,t1.MonomialPart()) << '\n'; addTermToEnd(t1); } ++w1; ++i1; ++w2; ++i2; if(i1<=sz1 && i2<=sz2) { t1 = * w1; t2 = * w2; } } else if(cmp<0) { //GBStream << "-=Adding element" << t2 << '\n'; addTermToEnd(-t2); ++w2; ++i2; if(i2<=sz2) t2 = * w2; } else // if(cmp>0) { //GBStream << "-=Adding element" << t1 << '\n'; addTermToEnd(t1); ++w1; ++i1; if(i1<=sz1) t1 = * w1; } } #if 1 } #endif for(;i1<=sz1;++i1,++w1) { //GBStream << "-=Adding element" << *w1 << '\n'; addTermToEnd(*w1); } for(;i2<=sz2;++i2,++w2) { //GBStream << "-=Adding element" << -*w2 << '\n'; addTermToEnd(-(*w2)); } #if 0 } #endif } };