3 #include <symengine/symengine_exception.h>
13 if (SymEngine::get_den(x) == 1)
16 if (SymEngine::get_num(x) != SymEngine::get_num(
i))
18 if (SymEngine::get_den(x) != SymEngine::get_den(
i))
26 if (SymEngine::get_den(
i) == 1) {
27 return integer(SymEngine::get_num(
i));
30 return make_rcp<const Rational>(
std::move(j));
37 if (SymEngine::get_den(
i) == 1) {
38 return integer(SymEngine::get_num(
i));
53 #if SYMENGINE_INTEGER_CLASS == SYMENGINE_BOOSTMP
80 #if SYMENGINE_INTEGER_CLASS == SYMENGINE_BOOSTMP
87 rational_class q(n, d);
100 hash_t seed = SYMENGINE_RATIONAL;
101 hash_combine<long long int>(seed, mp_get_si(SymEngine::get_num(this->
i)));
102 hash_combine<long long int>(seed, mp_get_si(SymEngine::get_den(this->
i)));
108 if (is_a<Rational>(o)) {
109 const Rational &s = down_cast<const Rational &>(o);
110 return this->
i == s.
i;
117 if (is_a<Rational>(o)) {
118 const Rational &s = down_cast<const Rational &>(o);
121 return i < s.
i ? -1 : 1;
123 if (is_a<Integer>(o)) {
124 const Integer &s = down_cast<const Integer &>(o);
127 throw NotImplementedError(
"unhandled comparison of Rational");
131 const Ptr<RCP<const Integer>> &den)
137 bool Rational::is_perfect_power(
bool is_expected)
const
139 const integer_class &num = SymEngine::get_num(
i);
141 return mp_perfect_power_p(SymEngine::get_den(
i));
143 const integer_class &den = SymEngine::get_den(
i);
145 if (not is_expected) {
146 if (mp_cmpabs(num, den) > 0) {
147 if (!mp_perfect_power_p(den))
150 if (!mp_perfect_power_p(num))
154 integer_class prod = num * den;
155 return mp_perfect_power_p(prod);
158 bool Rational::nth_root(
const Ptr<RCP<const Number>> &the_rat,
159 unsigned long n)
const
162 throw SymEngineException(
"i_nth_root: Can not find Zeroth root");
164 #if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
166 int ret = mp_root(SymEngine::get_num(r), SymEngine::get_num(
i), n);
169 ret = mp_root(SymEngine::get_den(r), SymEngine::get_den(
i), n);
175 integer_class num, den;
176 int ret = mp_root(num, SymEngine::get_num(
i), n);
179 ret = mp_root(den, SymEngine::get_den(
i), n);
182 rational_class r(num, den);
185 *the_rat = make_rcp<const Rational>(
std::move(r));
192 other.
neg()->rpowrat(*this->get_den()));
200 RCP<const Integer> res;
201 if (mp_fits_ulong_p(SymEngine::get_den(
i))) {
202 unsigned long den = mp_get_ui(SymEngine::get_den(
i));
204 RCP<const Integer> neg_other = other.
neg();
206 and
i_nth_root(outArg(res), *neg_other, den)) {
208 res->powint(*get_num()));
210 }
else if (
i_nth_root(outArg(res), other, den)) {
213 return res->powint(*get_num());
217 auto num = SymEngine::get_num(
i);
218 auto den = SymEngine::get_den(
i);
220 mp_fdiv_qr(q, r, num, den);
228 imulnum(outArg(coef), I);
The lowest unit of symbolic representation.
RCP< T > rcp_from_this()
Get RCP<T> pointer to self (it will cast the pointer to T)
bool is_negative() const override
const integer_class & as_integer_class() const
Convert to integer_class.
bool is_minus_one() const override
bool is_one() const override
RCP< const Integer > neg() const
RCP< const Number > powint(const Integer &other) const
Fast Power Evaluation.
static RCP< const Basic > from_dict(const RCP< const Number > &coef, map_basic_basic &&d)
Create a Mul from a dict.
RCP< const Basic > rpowrat(const Integer &other) const
static RCP< const Number > from_mpq(const rational_class &i)
const rational_class & as_rational_class() const
Convert to rational_class.
hash_t __hash__() const override
int compare(const Basic &o) const override
rational_class i
i : object of rational_class
static RCP< const Number > from_two_ints(const Integer &n, const Integer &d)
bool is_canonical(const rational_class &i) const
RCP< const Rational > neg() const
RCP< const Number > powrat(const Integer &other) const
bool __eq__(const Basic &o) const override
Main namespace for SymEngine package.
void get_num_den(const Rational &rat, const Ptr< RCP< const Integer >> &num, const Ptr< RCP< const Integer >> &den)
returns the num and den of rational rat as RCP<const Integer>
std::enable_if< std::is_integral< T >::value, RCP< const Integer > >::type integer(T i)
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)
int i_nth_root(const Ptr< RCP< const Integer >> &r, const Integer &a, unsigned long int n)
Integer nth root.