14 #include "../core/VariablePool.h"
24 #include <unordered_map>
28 template<
typename Poly>
29 class OldGinacConverter :
public Singleton<OldGinacConverter<Poly>>
31 friend Singleton<OldGinacConverter>;
33 std::recursive_mutex mMutex;
34 std::shared_ptr<typename Poly::CACHE> mpPolynomialCache;
36 ~OldGinacConverter() =
default;
38 void setPolynomialCache(
const std::shared_ptr<typename Poly::CACHE>& _cache )
40 mpPolynomialCache = _cache;
43 template<
typename P = Poly, EnableIf<needs_cache_type<P>> = dummy>
44 P createPolynomial(
typename P::PolyType&& _poly )
46 return P( std::move(_poly), mpPolynomialCache );
49 template<
typename P = Poly, EnableIf<needs_cache_type<P>> = dummy>
52 return P( std::move(
typename P::PolyType(_var)), mpPolynomialCache );
55 template<
typename P = Poly, EnableIf<needs_cache_type<P>> = dummy>
56 P createPolynomial(
const typename P::PolyType& _poly )
58 return createPolynomial(
typename P::PolyType(_poly));
61 template<
typename P = Poly, DisableIf<needs_cache_type<P>> = dummy>
67 bool similar(
const GiNaC::ex& a,
const GiNaC::ex& b);
69 GiNaC::ex convertToGinac(
const typename Poly::PolyType& poly,
const std::map<carl::Variable, GiNaC::ex>& vars)
71 std::lock_guard<std::recursive_mutex> lock( mMutex );
73 for(
auto term = poly.begin(); term != poly.end(); ++term)
75 GiNaC::ex factor = GiNaC::ex( GiNaC::numeric( carl::convert<typename Poly::PolyType::CoeffType, cln::cl_RA>(term->coeff()) ) );
76 if((*term).monomial())
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));
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)
92 std::lock_guard<std::recursive_mutex> lock( mMutex );
93 if( existsFactorization( poly ) )
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() )
100 result *=
GiNaC::pow(convertToGinac( factor.first, vars ), factor.second );
104 return GiNaC::ex( GiNaC::numeric( carl::rationalize<cln::cl_RA>(PreventConversion<typename Poly::PolyType::CoeffType>( poly.coefficient() ))));
107 Poly convertToCarl(
const GiNaC::ex& _toConvert,
const std::map<GiNaC::ex, carl::Variable, GiNaC::ex_is_less>& vars);
109 void gatherVariables(
const Poly& poly, std::map<Variable, GiNaC::ex>& carlToGinacVarMap, std::map<GiNaC::ex, Variable, GiNaC::ex_is_less>& ginacToCarlVarMap)
111 std::lock_guard<std::recursive_mutex> lock( mMutex );
113 GiNaC::symbol vg(var.name());
114 if( carlToGinacVarMap.emplace(var, vg).second )
116 ginacToCarlVarMap.emplace(vg, var);
121 Poly ginacGcd(
const Poly& polyA,
const Poly& polyB);
123 bool ginacDivide(
const Poly& polyA,
const Poly& polyB, Poly& result);
125 Factors<Poly> ginacFactorization(
const Poly& poly);
127 bool checkConversion(
const Poly& polyA);
130 template<
typename Poly>
131 bool similar(
const GiNaC::ex& a,
const GiNaC::ex& b)
133 return OldGinacConverter<Poly>::getInstance().similar(a, b);
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)
139 return OldGinacConverter<Poly>::getInstance().gatherVariables(poly, carlToGinacVarMap, ginacToCarlVarMap);
142 template<
typename Poly>
143 GiNaC::ex convertToGinac(
const Poly& poly,
const std::map<carl::Variable, GiNaC::ex>& vars)
145 return OldGinacConverter<Poly>::getInstance().convertToGinac(poly, vars);
148 template<
typename Poly>
149 Poly convertToCarl(
const GiNaC::ex& _toConvert,
const std::map<GiNaC::ex, carl::Variable, GiNaC::ex_is_less>& vars)
151 return OldGinacConverter<Poly>::getInstance().convertToCarl(_toConvert, vars);
154 template<
typename Poly>
155 Poly ginacGcd(
const Poly& polyA,
const Poly& polyB)
157 return OldGinacConverter<Poly>::getInstance().ginacGcd(polyA, polyB);
160 template<
typename Poly>
161 bool ginacDivide(
const Poly& polyA,
const Poly& polyB, Poly& result)
163 return OldGinacConverter<Poly>::getInstance().ginacDivide(polyA, polyB, result);
166 template<
typename Poly>
167 Factors<Poly> ginacFactorization(
const Poly& poly)
169 return OldGinacConverter<Poly>::getInstance().ginacFactorization(poly);
172 template<
typename Poly>
173 bool checkConversion(
const Poly& polyA)
175 return OldGinacConverter<Poly>::getInstance().checkConversion(polyA);
178 template<
typename Poly>
179 void setGinacConverterPolynomialCache(
const std::shared_ptr<typename Poly::CACHE>& _cache )
181 OldGinacConverter<Poly>::getInstance().setPolynomialCache( _cache );
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)
const Variable & Arg
Argument type for variables being function arguments.