carl  24.04
Computer ARithmetic Library
OldGinacConverter.h
Go to the documentation of this file.
1 /*
2  * File: GinacConverter.h
3  * Author: Florian Corzilius
4  *
5  * Created on December 13, 2013, 4:25 PM
6  */
7 
8 #pragma once
9 
10 #include <carl-common/config.h>
11 
12 #ifdef USE_GINAC
13 
14 #include "../core/VariablePool.h"
17 #include <carl-arith/core/Common.h>
20 #include "ginac.h"
21 
22 #include <cln/cln.h>
23 #include <mutex>
24 #include <unordered_map>
25 
26 namespace carl
27 {
28  template<typename Poly>
29  class OldGinacConverter : public Singleton<OldGinacConverter<Poly>>
30  {
31  friend Singleton<OldGinacConverter>;
32  private:
33  std::recursive_mutex mMutex;
34  std::shared_ptr<typename Poly::CACHE> mpPolynomialCache;
35  public:
36  ~OldGinacConverter() = default;
37 
38  void setPolynomialCache( const std::shared_ptr<typename Poly::CACHE>& _cache )
39  {
40  mpPolynomialCache = _cache;
41  }
42 
43  template<typename P = Poly, EnableIf<needs_cache_type<P>> = dummy>
44  P createPolynomial( typename P::PolyType&& _poly )
45  {
46  return P( std::move(_poly), mpPolynomialCache );
47  }
48 
49  template<typename P = Poly, EnableIf<needs_cache_type<P>> = dummy>
50  P createPolynomial( Variable::Arg _var )
51  {
52  return P( std::move(typename P::PolyType(_var)), mpPolynomialCache );
53  }
54 
55  template<typename P = Poly, EnableIf<needs_cache_type<P>> = dummy>
56  P createPolynomial( const typename P::PolyType& _poly )
57  {
58  return createPolynomial(typename P::PolyType(_poly));
59  }
60 
61  template<typename P = Poly, DisableIf<needs_cache_type<P>> = dummy>
62  P createPolynomial( Variable::Arg _var )
63  {
64  return P( _var );
65  }
66 
67  bool similar( const GiNaC::ex& a, const GiNaC::ex& b);
68 
69  GiNaC::ex convertToGinac(const typename Poly::PolyType& poly, const std::map<carl::Variable, GiNaC::ex>& vars)
70  {
71  std::lock_guard<std::recursive_mutex> lock( mMutex );
72  GiNaC::ex result = 0;
73  for(auto term = poly.begin(); term != poly.end(); ++term)
74  {
75  GiNaC::ex factor = GiNaC::ex( GiNaC::numeric( carl::convert<typename Poly::PolyType::CoeffType, cln::cl_RA>(term->coeff()) ) );
76  if((*term).monomial())
77  {
78  for (auto it: *(term->monomial())) {
79  auto carlToGinacVar = vars.find(it.first);
80  assert(carlToGinacVar != vars.end());
81  factor *= GiNaC::pow(carlToGinacVar->second, static_cast<unsigned long>(it.second));
82  }
83  }
84  result += factor;
85  }
86  return result;
87  }
88 
89  template<typename P = Poly, EnableIf<is_factorized_type<P>> = dummy>
90  GiNaC::ex convertToGinac(const P& poly, const std::map<carl::Variable, GiNaC::ex>& vars)
91  {
92  std::lock_guard<std::recursive_mutex> lock( mMutex );
93  if( existsFactorization( poly ) )
94  {
95  if( poly.factorizedTrivially() )
96  return convertToGinac( poly.polynomial(), vars );
97  GiNaC::ex result = GiNaC::ex( GiNaC::numeric( carl::rationalize<cln::cl_RA>(PreventConversion<typename Poly::PolyType::CoeffType>( poly.coefficient() ))));
98  for( const auto& factor : poly.factorization() )
99  {
100  result *= GiNaC::pow(convertToGinac( factor.first, vars ), factor.second );
101  }
102  return result;
103  }
104  return GiNaC::ex( GiNaC::numeric( carl::rationalize<cln::cl_RA>(PreventConversion<typename Poly::PolyType::CoeffType>( poly.coefficient() ))));
105  }
106 
107  Poly convertToCarl(const GiNaC::ex& _toConvert, const std::map<GiNaC::ex, carl::Variable, GiNaC::ex_is_less>& vars);
108 
109  void gatherVariables(const Poly& poly, std::map<Variable, GiNaC::ex>& carlToGinacVarMap, std::map<GiNaC::ex, Variable, GiNaC::ex_is_less>& ginacToCarlVarMap)
110  {
111  std::lock_guard<std::recursive_mutex> lock( mMutex );
112  for (auto var: carl::variables(poly)) {
113  GiNaC::symbol vg(var.name());
114  if( carlToGinacVarMap.emplace(var, vg).second )
115  {
116  ginacToCarlVarMap.emplace(vg, var);
117  }
118  }
119  }
120 
121  Poly ginacGcd(const Poly& polyA, const Poly& polyB);
122 
123  bool ginacDivide(const Poly& polyA, const Poly& polyB, Poly& result);
124 
125  Factors<Poly> ginacFactorization(const Poly& poly);
126 
127  bool checkConversion(const Poly& polyA);
128  };
129 
130  template<typename Poly>
131  bool similar(const GiNaC::ex& a, const GiNaC::ex& b)
132  {
133  return OldGinacConverter<Poly>::getInstance().similar(a, b);
134  }
135 
136  template<typename Poly>
137  void gatherVariables(const Poly& poly, std::map<Variable, GiNaC::ex>& carlToGinacVarMap, std::map<GiNaC::ex, Variable, GiNaC::ex_is_less>& ginacToCarlVarMap)
138  {
139  return OldGinacConverter<Poly>::getInstance().gatherVariables(poly, carlToGinacVarMap, ginacToCarlVarMap);
140  }
141 
142  template<typename Poly>
143  GiNaC::ex convertToGinac(const Poly& poly, const std::map<carl::Variable, GiNaC::ex>& vars)
144  {
145  return OldGinacConverter<Poly>::getInstance().convertToGinac(poly, vars);
146  }
147 
148  template<typename Poly>
149  Poly convertToCarl(const GiNaC::ex& _toConvert, const std::map<GiNaC::ex, carl::Variable, GiNaC::ex_is_less>& vars)
150  {
151  return OldGinacConverter<Poly>::getInstance().convertToCarl(_toConvert, vars);
152  }
153 
154  template<typename Poly>
155  Poly ginacGcd(const Poly& polyA, const Poly& polyB)
156  {
157  return OldGinacConverter<Poly>::getInstance().ginacGcd(polyA, polyB);
158  }
159 
160  template<typename Poly>
161  bool ginacDivide(const Poly& polyA, const Poly& polyB, Poly& result)
162  {
163  return OldGinacConverter<Poly>::getInstance().ginacDivide(polyA, polyB, result);
164  }
165 
166  template<typename Poly>
167  Factors<Poly> ginacFactorization(const Poly& poly)
168  {
169  return OldGinacConverter<Poly>::getInstance().ginacFactorization(poly);
170  }
171 
172  template<typename Poly>
173  bool checkConversion(const Poly& polyA)
174  {
175  return OldGinacConverter<Poly>::getInstance().checkConversion(polyA);
176  }
177 
178  template<typename Poly>
179  void setGinacConverterPolynomialCache( const std::shared_ptr<typename Poly::CACHE>& _cache )
180  {
181  OldGinacConverter<Poly>::getInstance().setPolynomialCache( _cache );
182  }
183 }
184 
185 #include "OldGinacConverter.tpp"
186 
187 #endif
carl is the main namespace for the library.
void variables(const BasicConstraint< Pol > &c, carlVariables &vars)
Interval< Number > pow(const Interval< Number > &i, Integer exp)
Definition: Power.h:11
const Variable & Arg
Argument type for variables being function arguments.
Definition: Variable.h:89