Exemplo n.º 1
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;
}
Exemplo n.º 2
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;
}