예제 #1
0
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;
  };
};
예제 #2
0
// 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); 
  }
};
예제 #3
0
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);
    }
  }
};  
예제 #4
0
파일: NCASink.c 프로젝트: lolmid/2015-2016
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;
    };
  };
};
예제 #5
0
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);
      }
    }
  }
};
예제 #6
0
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
};
예제 #7
0
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);
  }
};  
예제 #8
0
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;
};
예제 #9
0
int main() {
	vector<int> a;
	for (int i = 1; i < 5; ++i) {
	  a.push_back(i);
	}
	vector<int> b;
	for (int i = 0; i < 7; ++i) {
	  int value = 4 - i;
	  b.push_back(value);
	}
	Polynomial<int> A(a);
	Polynomial<int> B(b);
	cout << "A = " << A << '\n' << "B = " << B << '\n';
	Polynomial<int> C;
	cout << "NULL Polynomial: " << C << " Power: " << C.GetPower() << '\n';
	C = A + B;
	cout << "A + B = " << C << '\n';
	C = A - B;
	cout << "A - B = " << C << '\n';
	C = 0;
	cout << "C = 0 <=> C: " << C << '\n';
	C += A;
	cout << "C += A: " << C << '\n';
	C -= B;
	cout << "C -= B: " << C << '\n';
	C += 7;
	cout << "C += 7: " << C << '\n';
	C -= 4;
	cout << "C -= 4: " << C << '\n';
	C *= 5;
	cout << "C *= 5: " << C << '\n';
	C /= 5;
	cout << "C /= 5: " << C << '\n';
	C %= 3;
	cout << "C %= 3: " << C << '\n';
	C = A * 0;
	cout << "C = A * 0: " << C << '\n';
	try {
	  C = A / 0;
	} catch (Polynomial<>::DivisionByZeroException e) {
    cout << "C = A / 0: Div by Zero exception!\n";
  }
	C = A * B;
	cout << "C = A * B: " << C << '\n';
	
	cout << "\n\n NEW PolynomialS INCLUDED: \n";
	vector<int> d;
	d.push_back(1); d.push_back(2); d.push_back(2); d.push_back(1);
	vector<int> f;
	f.push_back(-2); f.push_back(-2); f.push_back(-1); f.push_back(1); f.push_back(1);
	vector<int> g;
	g.push_back(1); g.push_back(2); g.push_back(3); g.push_back(4); g.push_back(5); g.push_back(6);
	vector<int> h;
	h.push_back(-1); h.push_back(1); h.push_back(3); h.push_back(5); h.push_back(7); h.push_back(9);
	vector<int> k;
	k.push_back(-1); k.push_back(-1); k.push_back(1); k.push_back(1);
	vector<int> z;
	z.push_back(1); z.push_back(1);
	vector<int> l;
	l.push_back(-2); l.push_back(10); l.push_back(12); l.push_back(-5); l.push_back(1); l.push_back(2);
	vector<int> m;
	m.push_back(7); m.push_back(-2); m.push_back(0); m.push_back(1);
	vector<int> n;
	n.push_back(-8); n.push_back(-4); n.push_back(2); n.push_back(5); n.push_back(2); n.push_back(-4); n.push_back(1);
	vector<int> p;
	p.push_back(-4); p.push_back(-4); p.push_back(1); p.push_back(-1); p.push_back(-1); p.push_back(1);
	Polynomial<int> D(d);  cout << "D: " << D << '\n';
	Polynomial<int> F(f);  cout << "F: " << F << '\n';
	Polynomial<int> G(g);  cout << "G: " << G << '\n';
	Polynomial<int> H(h);  cout << "H: " << H << '\n';
	Polynomial<int> K(k);  cout << "K: " << K << '\n';
	Polynomial<int> Z(z);	cout << "Z: " << Z << '\n';
	Polynomial<int> L(l);  cout << "L: " << L << '\n';
	Polynomial<int> M(m);  cout << "M: " << M << '\n';
	Polynomial<int> N(n);  cout << "N: " << N << '\n';
	Polynomial<int> P(p);  cout << "P: " << P << '\n';
	
	cout << "\n\n START CALCULATIONS\n";
	C = (D, F);
	cout << "C = GCD(D, F): " << C << '\n';
	C = H / G;
	cout << "C = H / G: " << C << '\n';
	C = K / Z;
	cout << "C = K / Z: " << C << '\n';
	C = (L, M);
	cout << "MUTUAL PRIME: C = GCD(L, M): " << C << '\n';
	C = (P, N);
	cout << "Inverse order: C = GCD(P, N): " << C << '\n';
	C = F % D;
	cout << "C = F % D: " << C << '\n';
	C = N % P;
	cout << "C = N % P: " << C << '\n';
	C = P % (N % P);
	cout << "C = P % (N % P): " << C << '\n';
	C = 108;
	C %= P;
	cout << "108 % P: " << C << '\n';
	C = 108;
	C /= P;
	cout << "108 / P: " << C << '\n';
	C = P / 1;
	cout << "C = P / 1: " << C << '\n';
	C = P;
	C /= 7;
	cout << "C = P; C /= 7: " << C << '\n';
	C = P;
	C /= 4;
	cout << "C = P; C /= 4: " << C << '\n';
	C += Z;
	cout << "C = P; C /= 4; C += Z: " << '\n';
	C = P / 4 + 1;
	cout << "C = P / 4 + 1: " << C << '\n';
	C = P / 4 + 2;
	C += Z;
	cout << "C = P / 4 + 2; C += Z: " << C << '\n';
	C = P = N;
	cout << "C = P = N: " << C << '\n';
	cout << "Get Power of C: " << C.GetPower() << '\n';
	C = -F;
	cout << "C = -F: " << C << '\n';
	C *= L;
	cout << "C = -F; C *= L: " << C << '\n';
	cout << "C(3): " << C(3) << '\n';
	cout << "C(0): " << C(0) << "  C(-2): " << C(-2) << '\n';
	cout << "C\' - derivative: " << C.Derivative() << "  Power: " << C.Derivative().GetPower() << '\n';
	C = 16;
	cout << "C = 16 - scalar; C\': " << C.Derivative() << "  Power: " << C.Derivative().GetPower() << '\n';
	
	C = N;
	cout << "\n\n Iterator from C.begin to C.end\nC = N: " << C << '\n';
	for (Polynomial<int>::iterator it = C.begin(); it != C.end(); ++it) {
	  cout << *it << ' ';
	}
	
	cout << "\n\n Bool operations\nC = N\n";
	if (C == N) {
	  cout << "True: C == N\n";
	} else {
	  cout << "WRONG BOOL OPERATOR!\n";
	}
	if (C != L) {
	  cout << "True: C != L\n";
	} else {
	  cout << "WRONG BOOL OPERATOR!\n";
	}
	
	cout << "\n\n Get C[ index ]: \n";
	cout << "C[0]: " << C[0] << " C[3]: " << C[3] << '\n';
	try {
	  cout << C[11] << '\n';
	} catch (Polynomial<int>::ArrayOutOfBoundsException e) {
	  cout << "C[11] is out of bounds!\n";
	}
	
	cout << "\n\n SUPER Special Polynomials\n";
	vector<int> x;
	x.push_back(0); x.push_back(1);
	vector<int> y;
	y.push_back(1); y.push_back(2);
	Polynomial<int> X(x);	cout << "X: " << X << '\n';
	Polynomial<int> Y(y);  cout << "Y: " << Y << '\n';
	C = (X, Y);
	cout << "C = GCD(X, Y): " << C << '\n';
	C = (Y, X);
	cout << "C = GCD(Y, X): " << C << '\n';
	C = X / Y;
	cout << "C = X / Y: " << C << '\n';
	C = Y / X;
	cout << "C = Y / X: " << C << '\n';
	C = Y % X;
	cout << "C = Y % X: " << C << '\n';
	C = X % Y;
	cout << "C = X % Y: " << C << '\n';
	
	vector<int> q;
	q.push_back(0); q.push_back(1);
	vector<int> t;
	t.push_back(0); t.push_back(4);
	Polynomial<int> Q(q);  cout << "Q: " << Q << '\n';
	Polynomial<int> T(t);  cout << "T: " << T << '\n';
	C = (Q, T);
	cout << "C = GCD(Q, T): " << C << '\n';
	C = (T, Q);
	cout << "C = GCD(T, Q): " << C << '\n';
	
	cout << "\n\n Double examples\n";
	vector<double> d1;
	d1.push_back(1.5); d1.push_back(2.3); d1.push_back(-3.4);
	vector<double> d2;
	d2.push_back(-0.6); d2.push_back(1.8);
	Polynomial<double> D1(d1);  cout << "D1: " << D1 << '\n';
	Polynomial<double> D2(d2);  cout << "D2: " << D2 << '\n';
	Polynomial<double> Result;
	Result = D1 / D2;
	cout << "Result = D1 / D2: " << Result << '\n';
	
  cout << "\n\n Monom Demo\n";
  Polynomial<int>::Monom _x;
  Polynomial<int> f_x = (_x^1)*4 + (_x^3) + (_x^5) + _x;
  cout << "Polynomial from Monom: " << f_x << '\n';
  Polynomial<double>::Monom _y;
  Polynomial<double> f_y = (_y^2) + _y + (_y^7)*2.71 + 0.59 + _y*0.91;
  cout << "Polynomial from Monom: " << f_y << '\n';
 
	cout << "Thank You for attention!\n";
  _getch();
	return 0;
}
예제 #10
0
bool PseudoBooleanProcessor::decomposeAssertion(Node assertion, bool negated)
{
  if (assertion.getKind() != kind::GEQ)
  {
    return false;
  }
  Assert(assertion.getKind() == kind::GEQ);

  Debug("pbs::rewrites") << "decomposeAssertion" << assertion << std::endl;

  Node l = assertion[0];
  Node r = assertion[1];

  if (r.getKind() != kind::CONST_RATIONAL)
  {
    Debug("pbs::rewrites") << "not rhs constant" << assertion << std::endl;
    return false;
  }
  // don't bother matching on anything other than + on the left hand side
  if (l.getKind() != kind::PLUS)
  {
    Debug("pbs::rewrites") << "not plus" << assertion << std::endl;
    return false;
  }

  if (!Polynomial::isMember(l))
  {
    Debug("pbs::rewrites") << "not polynomial" << assertion << std::endl;
    return false;
  }

  Polynomial p = Polynomial::parsePolynomial(l);
  clear();
  if (negated)
  {
    // (not (>= p r))
    // (< p r)
    // (> (-p) (-r))
    // (>= (-p) (-r +1))
    d_off = (-r.getConst<Rational>());

    if (d_off.value().isIntegral())
    {
      d_off = d_off.value() + Rational(1);
    }
    else
    {
      d_off = Rational(d_off.value().ceiling());
    }
  }
  else
  {
    // (>= p r)
    d_off = r.getConst<Rational>();
    d_off = Rational(d_off.value().ceiling());
  }
  Assert(d_off.value().isIntegral());

  int adj = negated ? -1 : 1;
  for (Polynomial::iterator i = p.begin(), end = p.end(); i != end; ++i)
  {
    Monomial m = *i;
    const Rational& coeff = m.getConstant().getValue();
    if (!(coeff.isOne() || coeff.isNegativeOne()))
    {
      return false;
    }
    Assert(coeff.sgn() != 0);

    const VarList& vl = m.getVarList();
    Node v = vl.getNode();

    if (!isPseudoBoolean(v))
    {
      return false;
    }
    int sgn = adj * coeff.sgn();
    if (sgn > 0)
    {
      d_pos.push_back(v);
    }
    else
    {
      d_neg.push_back(v);
    }
  }
  // all of the variables are pseudoboolean
  // with coefficients +/- and the offsetoff
  return true;
}
예제 #11
0
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
  }
};