SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
PolyTreePool.cpp
Go to the documentation of this file.
1 /**
2  * @file PolyTreePool.cpp
3  * @author Andreas Krueger <andreas.krueger@rwth-aachen.de>
4  */
5 
6 #include "PolyTreePool.h"
7 #include "PolyTree.h"
8 
9 namespace smtrat
10 {
11  const PolyTreeContent* PolyTreePool::get(const Poly& _poly)
12  {
13  auto it = mPool.find(_poly);
14  if(it == mPool.end()) {
15  return create(_poly);
16  }
17  return it->second;
18  }
19 
21  {
22  Poly poly(_poly);
23  poly.makeOrdered();
24 
25  std::size_t nrTerms = poly.nr_terms();
26 
27  if(nrTerms == 0) {
28  return create(Integer(0));
29  }
30  if(nrTerms > 1) {
31  auto lastTerm = poly.rbegin();
32  return create(poly, PolyTree::Type::SUM, poly - *lastTerm, Poly(*lastTerm));
33  }
34 
35  const TermT& term = poly[0];
36  Rational coeff = term.coeff();
37 
38  if(term.is_constant()) {
39  return create(carl::to_int<Integer>(coeff));
40  }
41 
42  const carl::Monomial::Arg monomial = term.monomial();
43 
44  if(! carl::is_one(coeff)) {
45  return create(poly, PolyTree::Type::PRODUCT, Poly(coeff), Poly(monomial));
46  }
47 
48  auto variableAndExponent = *(monomial->begin());
49 
50  if(monomial->num_variables() > 1) {
51  carl::Monomial::Arg head = carl::MonomialPool::getInstance().create(variableAndExponent.first, variableAndExponent.second);
52  carl::Monomial::Arg tail = monomial->drop_variable(variableAndExponent.first);
53  return create(poly, PolyTree::Type::PRODUCT, Poly(head), Poly(tail));
54  }
55 
56  if(variableAndExponent.second > 1) {
57  carl::Monomial::Arg remainder = carl::MonomialPool::getInstance().create(variableAndExponent.first, variableAndExponent.second - 1);
58  return create(poly, PolyTree::Type::PRODUCT, Poly(remainder), Poly(variableAndExponent.first));
59  }
60 
61  return create(variableAndExponent.first);
62  }
63 
64  const PolyTreeContent* PolyTreePool::create(carl::Variable::Arg _variable)
65  {
66  return add(new PolyTreeContent(_variable));
67  }
68 
70  {
71  return add(new PolyTreeContent(_constant));
72  }
73 
74  const PolyTreeContent* PolyTreePool::create(const Poly& _poly, PolyTree::Type _type, const Poly& _left, const Poly& _right)
75  {
76  return add(new PolyTreeContent(_poly, _type, PolyTree(_left), PolyTree(_right)));
77  }
78 
80  {
82 
83  auto insertion = mPool.insert(std::make_pair(_element->poly(), _element));
84  if(! insertion.second) {
85  delete _element;
86  }
87 
88  return insertion.first->second;
89  }
90 }
#define POOL_LOCK_GUARD
Definition: PolyTreePool.h:23
const Poly & poly() const
Definition: PolyTree.h:76
std::map< Poly, PolyTreeContent * > mPool
Definition: PolyTreePool.h:19
const PolyTreeContent * create(const Poly &_pol)
const PolyTreeContent * get(const Poly &_pol)
const PolyTreeContent * add(PolyTreeContent *_element)
Class to create the formulas for axioms.
carl::Term< Rational > TermT
Definition: types.h:23
carl::MultivariatePolynomial< Rational > Poly
Definition: types.h:25
mpq_class Rational
Definition: types.h:19
carl::IntegralType< Rational >::type Integer
Definition: types.h:21