1#ifndef SYMENGINE_BASIC_CONVERSIONS_H
2#define SYMENGINE_BASIC_CONVERSIONS_H
14 const RCP<const Basic> &gen,
bool ex =
false);
22from_basic(
const RCP<const Basic> &basic,
bool ex =
false);
24template <
typename T,
typename P>
26_basic_to_upoly(
const RCP<const Basic> &basic,
const RCP<const Basic> &gen);
28template <
typename T,
typename P>
30_basic_to_upoly(
const RCP<const Basic> &basic,
const RCP<const Basic> &gen);
32template <
typename T,
typename P>
34_basic_to_upoly(
const RCP<const Basic> &basic,
const RCP<const Basic> &gen);
36template <
typename P,
typename V>
41 using D =
typename P::container_type;
49 D apply(
const Basic &b)
55 void dict_set(
unsigned int pow,
const Basic &x)
60 void bvisit(
const Pow &x)
87 for (
auto const &
it :
addx->get_dict())
89 if (
not addx->get_coef()->is_zero())
100 if (i->is_positive()) {
101 powr =
static_cast<int>(i->as_int());
109 this->bvisit((
const Basic &)x);
113 void bvisit(
const Add &x)
116 for (
auto const &
it : x.get_dict())
117 res += apply(*
it.first) * apply(*
it.second);
121 void bvisit(
const Mul &x)
123 D
res = apply(*x.get_coef());
124 for (
auto const &
it : x.get_dict())
125 res *= apply(*pow(
it.first,
it.second));
132 dict = P::container_from_dict(gen, {{0,
typename P::coef_type(i)}});
137 typename = enable_if_t<
156 void bvisit(
const Poly &x)
158 dict = (P::from_poly(x))->get_poly();
161 void bvisit(
const Basic &x)
174 dict = P::container_from_dict(
175 gen, {{i,
typename P::coef_type(1)}});
181 if (has_symbol(x, *gen)) {
182 throw SymEngineException(
"Not a Polynomial");
189template <
typename Poly>
203 throw SymEngineException(
"Non-integer found");
206 void dict_set(
unsigned int pow,
const Basic &x)
209 this->dict = Poly::container_from_dict(
213 throw SymEngineException(
"Non-integer found");
230 void dict_set(
unsigned int pow,
const Basic &x)
236template <
typename Poly>
253 void dict_set(
unsigned int pow,
const Basic &x)
256 this->dict = Poly::container_from_dict(
257 this->gen, {{pow, rational_class(
static_cast<const Integer &
>(x)
258 .as_integer_class())}});
260 this->dict = Poly::container_from_dict(
262 {{pow,
static_cast<const Rational &
>(x).as_rational_class()}});
264 throw SymEngineException(
"Non-rational found");
268template <
typename T,
typename P>
269enable_if_t<std::is_same<T, UExprDict>::value, T>
270_basic_to_upoly(
const RCP<const Basic> &basic,
const RCP<const Basic> &gen)
273 return v.apply(*basic);
276template <
typename T,
typename P>
278_basic_to_upoly(
const RCP<const Basic> &basic,
const RCP<const Basic> &gen)
281 return v.apply(*basic);
284template <
typename T,
typename P>
286_basic_to_upoly(
const RCP<const Basic> &basic,
const RCP<const Basic> &gen)
289 return v.apply(*basic);
294 const RCP<const Basic> &gen,
bool ex)
296 RCP<const Basic>
exp = basic;
299 return P::from_container(
305from_basic(
const RCP<const Basic> &basic,
bool ex)
307 RCP<const Basic>
exp = basic;
311 umap_basic_num
tmp = _find_gens_poly(
exp);
314 throw SymEngineException(
"Did not find exactly 1 generator");
316 RCP<const Basic> gen =
pow(
tmp.begin()->first,
tmp.begin()->second);
317 return P::from_container(
323_basic_to_mpoly(
const RCP<const Basic> &basic,
const set_basic &gens);
325template <
typename P,
typename V>
329 using Dict =
typename P::container_type;
330 using Vec =
typename Dict::vec_type;
340 dict.vec_size =
static_cast<int>(gens.
size());
345 for (
auto it : gens) {
353 if (
ite == gens_pow.
end())
361 Dict apply(
const Basic &b)
367 void dict_set(Vec pow,
const Basic &x)
372 void bvisit(
const Pow &x)
384 RCP<const Basic>
coef = one,
tmp;
389 if (
ite != gens_pow.
end()) {
395 for (
auto const &
it :
addx->get_dict())
397 if (
not addx->get_coef()->is_zero())
407 for (
auto powr :
ite->second) {
411 if (i->is_positive()) {
413 =
static_cast<int>(i->as_int());
430 void bvisit(
const Add &x)
433 for (
auto const &
it : x.get_dict())
434 res += apply(*
it.first) * apply(*
it.second);
438 void bvisit(
const Mul &x)
440 Dict
res = apply(*x.get_coef());
441 for (
auto const &
it : x.get_dict())
442 res *= apply(*pow(
it.first,
it.second));
450 dict = P::container_from_dict(gens, {{
zero_v, i}});
453 void bvisit(
const Basic &x)
455 RCP<const Basic>
powr;
459 if (
it != gens_pow.
end()) {
461 for (
auto pows :
it->second) {
469 dict = P::container_from_dict(
470 gens, {{
zero_v,
typename P::coef_type(1)}});
491 throw SymEngineException(
"Non-integer found");
497 dict = MIntPoly::container_from_dict(
501 throw SymEngineException(
"Non-integer found");
515 Vec v(gens.
size(), 0);
516 dict = MExprPoly::container_from_dict(gens, {{v, x.
rcp_from_this()}});
521 dict = MExprPoly::container_from_dict(gens, {{pow, x.
rcp_from_this()}});
526enable_if_t<std::is_same<MIntPoly, P>::value,
typename P::container_type>
527_basic_to_mpoly(
const RCP<const Basic> &basic,
const set_basic &gens)
530 return v.apply(*basic);
535_basic_to_mpoly(
const RCP<const Basic> &basic,
const set_basic &gens)
537 BasicToMExprPoly v(gens);
538 return v.apply(*basic);
542RCP<const P> from_basic(
const RCP<const Basic> &basic, set_basic &gens,
545 RCP<const Basic>
exp = basic;
557from_basic(
const RCP<const Basic> &basic,
bool ex =
false)
559 RCP<const Basic>
exp = basic;
563 umap_basic_num
tmp = _find_gens_poly(
exp);
The base class for representing addition in symbolic expressions.
const RCP< const Number > & get_coef() const
The lowest unit of symbolic representation.
RCP< T > rcp_from_this()
Get RCP<T> pointer to self (it will cast the pointer to T)
const integer_class & as_integer_class() const
Convert to integer_class.
RCP< const Basic > get_base() const
RCP< const Basic > get_exp() const
const rational_class & as_rational_class() const
Convert to rational_class.
Main namespace for SymEngine package.
RCP< const Basic > div(const RCP< const Basic > &a, const RCP< const Basic > &b)
Division.
bool eq(const Basic &a, const Basic &b)
Checks equality for a and b
void hash_combine(hash_t &seed, const T &v)
RCP< const Basic > exp(const RCP< const Basic > &x)
Returns the natural exponential function E**x = pow(E, x)
RCP< const Basic > mul(const RCP< const Basic > &a, const RCP< const Basic > &b)
Multiplication.
void insert(T1 &m, const T2 &first, const T3 &second)
RCP< const Basic > expand(const RCP< const Basic > &self, bool deep=true)
Expands self