1 #ifndef SYMENGINE_BASIC_CONVERSIONS_H
2 #define SYMENGINE_BASIC_CONVERSIONS_H
13 RCP<const P> from_basic(
const RCP<const Basic> &basic,
14 const RCP<const Basic> &gen,
bool ex =
false);
21 enable_if_t<is_a_UPoly<P>::value, RCP<const P>>
22 from_basic(
const RCP<const Basic> &basic,
bool ex =
false);
24 template <
typename T,
typename P>
25 enable_if_t<std::is_same<T, UExprDict>::value, T>
26 _basic_to_upoly(
const RCP<const Basic> &basic,
const RCP<const Basic> &gen);
28 template <
typename T,
typename P>
29 enable_if_t<std::is_base_of<UIntPolyBase<T, P>, P>::value, T>
30 _basic_to_upoly(
const RCP<const Basic> &basic,
const RCP<const Basic> &gen);
32 template <
typename T,
typename P>
33 enable_if_t<std::is_base_of<URatPolyBase<T, P>, P>::value, T>
34 _basic_to_upoly(
const RCP<const Basic> &basic,
const RCP<const Basic> &gen);
36 template <
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)
57 down_cast<V *>(
this)->dict_set(pow, x);
60 void bvisit(
const Pow &x)
62 if (is_a<const Integer>(*x.
get_exp())) {
63 int i = numeric_cast<int>(
64 down_cast<const Integer &>(*x.
get_exp()).as_int());
67 = pow_upoly(*P::from_container(gen, _basic_to_upoly<D, P>(
75 RCP<const Basic> genbase = gen, genpow = one, coef = one, tmp;
76 if (is_a<const Pow>(*gen)) {
77 genbase = down_cast<const Pow &>(*gen).get_base();
78 genpow = down_cast<const Pow &>(*gen).get_exp();
85 if (is_a<const Add>(*x.
get_exp())) {
86 RCP<const Add> addx = rcp_static_cast<const Add>(x.
get_exp());
87 for (
auto const &it : addx->get_dict())
89 if (not addx->get_coef()->is_zero())
90 expos.
insert(addx->get_coef());
96 for (
auto const &it : expos) {
97 tmp =
div(it, genpow);
98 if (is_a<const Integer>(*tmp)) {
99 RCP<const Integer> i = rcp_static_cast<const Integer>(tmp);
100 if (i->is_positive()) {
101 powr =
static_cast<int>(i->as_int());
105 coef =
mul(coef, pow(genbase, it));
107 dict_set(powr, *coef);
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)
163 RCP<const Basic> genpow = one, genbase = gen, powr;
164 if (is_a<const Pow>(*gen)) {
165 genpow = down_cast<const Pow &>(*gen).get_exp();
166 genbase = down_cast<const Pow &>(*gen).get_base();
168 if (
eq(*genbase, x)) {
169 powr =
div(one, genpow);
170 if (is_a<const Integer>(*powr)) {
171 int i = numeric_cast<int>(
172 down_cast<const Integer &>(*powr).as_int());
174 dict = P::container_from_dict(
175 gen, {{i,
typename P::coef_type(1)}});
180 if (is_a<const Symbol>(*gen)) {
181 if (has_symbol(x, *gen)) {
182 throw SymEngineException(
"Not a Polynomial");
189 template <
typename Poly>
203 throw SymEngineException(
"Non-integer found");
206 void dict_set(
unsigned int pow,
const Basic &x)
208 if (is_a<const Integer>(x))
209 this->dict = Poly::container_from_dict(
211 {{pow, down_cast<const Integer &>(x).as_integer_class()}});
213 throw SymEngineException(
"Non-integer found");
230 void dict_set(
unsigned int pow,
const Basic &x)
236 template <
typename Poly>
253 void dict_set(
unsigned int pow,
const Basic &x)
255 if (is_a<const Integer>(x))
256 this->dict = Poly::container_from_dict(
257 this->gen, {{pow, rational_class(
static_cast<const Integer &
>(x)
258 .as_integer_class())}});
259 else if (is_a<const Rational>(x))
260 this->dict = Poly::container_from_dict(
262 {{pow,
static_cast<const Rational &
>(x).as_rational_class()}});
264 throw SymEngineException(
"Non-rational found");
268 template <
typename T,
typename P>
269 enable_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);
276 template <
typename T,
typename P>
277 enable_if_t<std::is_base_of<UIntPolyBase<T, P>, P>::value, T>
278 _basic_to_upoly(
const RCP<const Basic> &basic,
const RCP<const Basic> &gen)
280 BasicToUIntPoly<P> v(gen);
281 return v.apply(*basic);
284 template <
typename T,
typename P>
285 enable_if_t<std::is_base_of<URatPolyBase<T, P>, P>::value, T>
286 _basic_to_upoly(
const RCP<const Basic> &basic,
const RCP<const Basic> &gen)
288 BasicToURatPoly<P> v(gen);
289 return v.apply(*basic);
292 template <
typename P>
293 RCP<const P> from_basic(
const RCP<const Basic> &basic,
294 const RCP<const Basic> &gen,
bool ex)
296 RCP<const Basic>
exp = basic;
299 return P::from_container(
300 gen, _basic_to_upoly<typename P::container_type, P>(
exp, gen));
303 template <
typename P>
304 enable_if_t<is_a_UPoly<P>::value, RCP<const P>>
305 from_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(
318 gen, _basic_to_upoly<typename P::container_type, P>(
exp, gen));
321 template <
typename P>
322 enable_if_t<std::is_same<MIntPoly, P>::value,
typename P::container_type>
323 _basic_to_mpoly(
const RCP<const Basic> &basic,
const set_basic &gens);
325 template <
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());
342 RCP<const Basic> genpow, genbase;
345 for (
auto it : gens) {
348 if (is_a<const Pow>(*it)) {
349 genpow = down_cast<const Pow &>(*it).get_exp();
350 genbase = down_cast<const Pow &>(*it).get_base();
352 auto ite = gens_pow.
find(genbase);
353 if (ite == gens_pow.
end())
354 gens_pow[genbase] = {genpow};
356 gens_pow[genbase].push_back(genpow);
361 Dict apply(
const Basic &b)
367 void dict_set(Vec pow,
const Basic &x)
369 down_cast<V *>(
this)->dict_set(pow, x);
372 void bvisit(
const Pow &x)
374 if (is_a<const Integer>(*x.
get_exp())) {
375 int i = numeric_cast<int>(
376 down_cast<const Integer &>(*x.
get_exp()).as_int());
378 dict = Dict::pow(_basic_to_mpoly<P>(x.
get_base(), gens), i);
383 Vec zero_v(gens.
size(), 0);
384 RCP<const Basic> coef = one, tmp;
385 RCP<const Integer> i;
389 if (ite != gens_pow.
end()) {
393 if (is_a<const Add>(*x.
get_exp())) {
394 RCP<const Add> addx = rcp_static_cast<const Add>(x.
get_exp());
395 for (
auto const &it : addx->get_dict())
397 if (not addx->get_coef()->is_zero())
398 expos.
insert(addx->get_coef());
403 for (
auto const &it : expos) {
407 for (
auto powr : ite->second) {
409 if (is_a<const Integer>(*tmp)) {
410 i = rcp_static_cast<const Integer>(tmp);
411 if (i->is_positive()) {
412 zero_v[gens_map[pow(ite->first, powr)]]
413 =
static_cast<int>(i->as_int());
421 coef =
mul(coef, pow(ite->first, it));
423 dict_set(zero_v, *coef);
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));
449 Vec zero_v(gens.
size(), 0);
450 dict = P::container_from_dict(gens, {{zero_v, i}});
453 void bvisit(
const Basic &x)
455 RCP<const Basic> powr;
456 Vec zero_v(gens.
size(), 0);
459 if (it != gens_pow.
end()) {
461 for (
auto pows : it->second) {
462 powr =
div(one, pows);
463 if (is_a<const Integer>(*powr)) {
464 int i = numeric_cast<int>(
465 down_cast<const Integer &>(*powr).as_int());
468 zero_v[gens_map[pow(it->first, pows)]] = i;
469 dict = P::container_from_dict(
470 gens, {{zero_v,
typename P::coef_type(1)}});
491 throw SymEngineException(
"Non-integer found");
496 if (is_a<const Integer>(x))
497 dict = MIntPoly::container_from_dict(
499 {{pow, down_cast<const Integer &>(x).as_integer_class()}});
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()}});
525 template <
typename P>
526 enable_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);
533 template <
typename P>
534 enable_if_t<std::is_same<MExprPoly, P>::value,
typename P::container_type>
535 _basic_to_mpoly(
const RCP<const Basic> &basic,
const set_basic &gens)
537 BasicToMExprPoly v(gens);
538 return v.apply(*basic);
541 template <
typename P>
542 RCP<const P> from_basic(
const RCP<const Basic> &basic, set_basic &gens,
545 RCP<const Basic>
exp = basic;
550 return P::from_container(gens, _basic_to_mpoly<P>(
exp, gens));
553 template <
typename P>
557 from_basic(
const RCP<const Basic> &basic,
bool ex =
false)
559 RCP<const Basic>
exp = basic;
563 umap_basic_num tmp = _find_gens_poly(
exp);
568 return P::from_container(gens, _basic_to_mpoly<P>(
exp, gens));
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_exp() const
RCP< const Basic > get_base() 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
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.
RCP< const Basic > expand(const RCP< const Basic > &self, bool deep=true)
Expands self