carl  24.04
Computer ARithmetic Library
GiNaCConverter.h
Go to the documentation of this file.
1 /**
2  * @file GiNaCConverter.h
3  * @author Gereon Kremer <gereon.kremer@cs.rwth-aachen.de>
4  */
5 
6 #pragma once
7 
9 #include "ginac.h"
10 #ifdef USE_GINAC
11 
12 namespace carl {
13 
14 class GiNaCConverter {
15 private:
16  std::map<carl::Variable, GiNaC::symbol> vars;
17 public:
18  GiNaC::numeric operator()(const cln::cl_RA& n) {
19  return GiNaC::numeric(n);
20  }
21  GiNaC::numeric operator()(const mpq_class& n) {
22  std::stringstream ss;
23  ss << n;
24  return GiNaC::numeric(ss.str().c_str());
25  }
26  GiNaC::symbol operator()(const carl::Variable& v) {
27  auto it = vars.find(v);
28  if (it == vars.end()) {
29  it = vars.insert(std::make_pair(v, GiNaC::symbol(carl::VariablePool::getInstance().get_name(v)))).first;
30  }
31  return it->second;
32  }
33  GiNaC::ex operator()(const std::pair<carl::Variable, carl::exponent>& p) {
34  assert(carl::fits_within<unsigned long>(p.second));
35  return GiNaC::pow((*this)(p.first), static_cast<unsigned long>(p.second));
36  }
37  GiNaC::ex operator()(const carl::Monomial& m) {
38  GiNaC::ex res = 1;
39  for (auto it: m) res *= (*this)(it);
40  return res;
41  }
42  template<typename Coeff>
43  GiNaC::ex operator()(const carl::Term<Coeff>& t) {
44  GiNaC::ex res = (*this)(t.coeff());
45  if (t.monomial()) return res * (*this)(*t.monomial());
46  else return res;
47  }
48  template<typename Coeff>
49  GiNaC::ex operator()(const carl::MultivariatePolynomial<Coeff>& p) {
50  GiNaC::ex res;
51  for (auto t: p) res += (*this)(t);
52  return GiNaC::expand(res);
53  }
54  template<typename Coeff>
55  GiNaC::ex operator()(const carl::UnivariatePolynomial<Coeff>& p) {
56  GiNaC::ex res;
57  unsigned exp = 0;
58  GiNaC::symbol mainvar = (*this)(p.main_var());
59  for (auto c: p.coefficients()) {
60  res += GiNaC::pow(mainvar, exp) * (*this)(c);
61  exp++;
62  }
63  return GiNaC::expand(res);
64  }
65  /**
66  * Compute all carl variables mapped in this converter whose GiNaC variables are also in the given variable vector.
67  * The returned vector respects the order of the given variables.
68  * @param cadVariables
69  * @return carl variables corresponding to given GiNaC variables respecting the given order
70  */
71  std::vector<carl::Variable> variables(const std::vector<GiNaC::symbol>& cadVariables) {
72  std::vector<carl::Variable> v = std::vector<carl::Variable>();
73  for(std::vector<GiNaC::symbol>::const_iterator g_it = cadVariables.begin(); g_it != cadVariables.end(); ++g_it)
74  {
75  for(std::map<carl::Variable, GiNaC::symbol>::const_iterator it = vars.begin(); it != vars.end(); ++it)
76  {
77  if(*g_it == it->second)
78  v.push_back(it->first);
79  }
80  }
81  assert(cadVariables.size() == v.size());
82  return v;
83  }
84 };
85 
86 }
87 
88 #endif
carl is the main namespace for the library.
Interval< Number > exp(const Interval< Number > &i)
Definition: Exponential.h:10
void variables(const BasicConstraint< Pol > &c, carlVariables &vars)
Interval< Number > pow(const Interval< Number > &i, Integer exp)
Definition: Power.h:11
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85
This class represents a univariate polynomial with coefficients of an arbitrary type.
const std::vector< Coefficient > & coefficients() const &
Retrieves the coefficients defining this polynomial.
Variable main_var() const
Retrieves the main variable of this polynomial.
The general-purpose multivariate polynomial class.
The general-purpose monomials.
Definition: Monomial.h:59
Coefficient & coeff()
Get the coefficient.
Definition: Term.h:80
Monomial::Arg & monomial()
Get the monomial.
Definition: Term.h:91
static VariablePool & getInstance()
Returns the single instance of this class by reference.
Definition: Singleton.h:45