carl  24.04
Computer ARithmetic Library
TarskiQueryManager.h
Go to the documentation of this file.
1 /*
2  * File: TarskiQueryManager.h
3  * Author: tobias
4  *
5  * Created on 27. August 2016, 15:58
6  */
7 
8 #pragma once
9 
10 #include <iterator>
11 
12 #include "MultiplicationTable.h"
14 #include "UnivariateTarskiQuery.h"
15 
17 
18 namespace carl {
19 
20 /*
21  * The Tarski query manager is a class designed to manage the computation of Tarski queries.
22  *
23  */
24 template<typename Number>
26 
27 public:
28  using QueryResultType = int;
29 
30 private:
32 
33  // for the univariate case
36 
37  // for the multivariate case
39  bool mTrivialGb = false;
40 
41  mutable std::map<Polynomial, QueryResultType> mCache;
42 
43 public:
44  TarskiQueryManager() = default;
45 
46  template<typename InputIt>
47  TarskiQueryManager(InputIt first, InputIt last) {
48  CARL_LOG_TRACE("carl.thom.tarski.manager", "setting up a taq manager on " << std::vector<Polynomial>(first, last));
49  // univariate manager
50  if(std::distance(first, last) == 1 && first->is_univariate()) {
51  CARL_LOG_TRACE("carl.thom.tarski.manager", "as a UNIVARIATE manager");
53  CARL_LOG_ASSERT("carl.thom.tarski.manager", !carl::is_zero(mZ), "");
54  mDer = derivative(mZ);
55  CARL_LOG_ASSERT("carl.thom.tarski.manager", this->isUnivariateManager(), "");
56  }
57  // multivariate manager
58  else {
59  CARL_LOG_TRACE("carl.thom.tarski.manager", "as a MULTIVARIATE manager");
60  GroebnerBase<Number> gb(first, last);
61  if(gb.isTrivialBase()) {
62  mTrivialGb = true;
63  }
64  else {
65  CARL_LOG_ASSERT("carl.thom.tarski.manager", gb.hasFiniteMon(), "");
66  if(!gb.hasFiniteMon()) {
67  std::cout << "aborting because it was tried to set up a tarki query manager on a non zero-dimensional zero set" << std::endl;
68  std::exit(23);
69  }
71  }
72  CARL_LOG_ASSERT("carl.thom.tarski.manager", !this->isUnivariateManager(), "");
73  }
74  }
75 
77  CARL_LOG_TRACE("carl.thom.tarski.manager", "computing taq on " << p << " ... ");
78  if(carl::is_zero(p)) return 0;
79  QueryResultType res;
80 
81  // return cached query result
82  if(getCached(p, res)) {
83  CARL_LOG_TRACE("carl.thom.tarski.manager", "found in cache: " << res);
84  return res;
85  }
86 
87  // univariate manager
88  if(this->isUnivariateManager()) {
89  CARL_LOG_ASSERT("carl.thom.tarski.manager", p.is_univariate(), "");
92  else pUniv = carl::to_univariate_polynomial(p);
93  CARL_LOG_ASSERT("carl.thom.tarski.manager", pUniv.main_var() == mZ.main_var(),
94  "cannot compute tarski query of " << p << " on " << mZ);
95  res = univariateTarskiQuery(pUniv, mZ, mDer);
96  }
97 
98  // multivariate manager
99  else {
100  if(mTrivialGb) res = 0;
101  else {
102  // todo: check if variables in p are also in the polynomials defining the zero set
103  res = multivariateTarskiQuery(p, mTab);
104  }
105  }
106  cache(p, res);
107  CARL_LOG_TRACE("carl.thom.tarski.manager", res);
108  return res;
109  }
110 
111  QueryResultType operator()(const Number& c) const {
112  return (*this)(Polynomial(c));
113  }
114 
115  Polynomial reduceProduct(const Polynomial& a, const Polynomial& b) const {
116  if(this->isUnivariateManager()) {
117  // todo: implement
118  return a * b;
119  }
120  else {
121  return mTab.baseReprToPolynomial(mTab.reduce(a * b));
122  }
123 
124  }
125 
126 
127 
128 private:
129 
130  bool isUnivariateManager() const {
131  return !carl::is_zero(mZ);
132  }
133 
134  /*
135  * looks for the normalization of p in the cache
136  */
137  bool getCached(const Polynomial& p, QueryResultType& res) const {
138  auto it = mCache.find(p.normalize());
139  if(it != mCache.end()) {
140  res = int(sgn(p.lcoeff())) * (it->second);
141  return true;
142  }
143  return false;
144  }
145 
146  /*
147  * writes normalized p with correspoding result in cache
148  */
149  void cache(const Polynomial& p, const QueryResultType res) const {
150  mCache.insert(std::make_pair(p.normalize(), int(sgn(p.lcoeff())) * res));
151  }
152 
153 }; // class TarskiQueryManager
154 
155 } // namespace carl
#define CARL_LOG_TRACE(channel, msg)
Definition: carl-logging.h:44
#define CARL_LOG_ASSERT(channel, condition, msg)
Definition: carl-logging.h:47
carl is the main namespace for the library.
UnivariatePolynomial< C > to_univariate_polynomial(const MultivariatePolynomial< C, O, P > &p)
Convert a univariate polynomial that is currently (mis)represented by a 'MultivariatePolynomial' into...
const T & derivative(const T &t, Variable, std::size_t n=1)
Computes the n'th derivative of a number, which is either the number itself (for n = 0) or zero.
Definition: Derivative.h:23
bool is_zero(const Interval< Number > &i)
Check if this interval is a point-interval containing 0.
Definition: Interval.h:1453
int multivariateTarskiQuery(const MultivariatePolynomial< Number > &Q, const MultiplicationTable< Number > &table)
Sign sgn(const Number &n)
Obtain the sign of the given number.
Definition: Sign.h:54
int univariateTarskiQuery(const UnivariatePolynomial< Number > &p, const UnivariatePolynomial< Number > &q, const UnivariatePolynomial< Number > &der_q)
static const Variable NO_VARIABLE
Instance of an invalid variable.
Definition: Variable.h:203
Variable main_var() const
Retrieves the main variable of this polynomial.
bool is_constant() const
Check if the polynomial is constant.
bool is_univariate() const
Checks whether only one variable occurs.
const Coeff & lcoeff() const
Returns the coefficient of the leading term.
MultivariatePolynomial normalize() const
For a polynomial p, returns p/lc(p)
bool isTrivialBase() const
Definition: GroebnerBase.h:56
bool hasFiniteMon() const
MultiplicationTable< Number > mTab
QueryResultType operator()(const Polynomial &p) const
Polynomial reduceProduct(const Polynomial &a, const Polynomial &b) const
std::map< Polynomial, QueryResultType > mCache
void cache(const Polynomial &p, const QueryResultType res) const
MultivariatePolynomial< Number > Polynomial
UnivariatePolynomial< Number > mZ
QueryResultType operator()(const Number &c) const
UnivariatePolynomial< Number > mDer
bool getCached(const Polynomial &p, QueryResultType &res) const
TarskiQueryManager(InputIt first, InputIt last)