6 #ifndef SYMENGINE_RATIONAL_H
7 #define SYMENGINE_RATIONAL_H
10 #include <symengine/symengine_exception.h>
24 Rational(rational_class &&_i) : i(std::move(_i))
26 SYMENGINE_ASSIGN_TYPEID()
31 static RCP<const Number> from_mpq(
const rational_class &i);
32 static RCP<const Number> from_mpq(rational_class &&i);
34 hash_t __hash__()
const override;
39 bool __eq__(
const Basic &o)
const override;
40 int compare(
const Basic &o)
const override;
42 bool is_canonical(
const rational_class &i)
const;
47 static RCP<const Number> from_two_ints(
const Integer &n,
const Integer &d);
48 static RCP<const Number> from_two_ints(
const long n,
const long d);
92 inline RCP<const Rational>
neg()
const
94 return make_rcp<const Rational>(-i);
97 bool is_perfect_power(
bool is_expected =
false)
const override;
99 bool nth_root(
const Ptr<RCP<const Number>> &,
100 unsigned long n)
const override;
107 return from_mpq(this->i + other.
i);
121 return from_mpq(this->i - other.
i);
130 inline RCP<const Number> rsubrat(
const Integer &other)
const
139 return from_mpq(this->i * other.
i);
160 return from_mpq(this->i / other.
i);
178 inline RCP<const Number> rdivrat(
const Integer &other)
const
199 if (not mp_fits_ulong_p(exp_))
200 throw SymEngineException(
"powrat: 'exp' does not fit ulong.");
201 unsigned long exp = mp_get_ui(exp_);
203 #if SYMENGINE_INTEGER_CLASS == SYMENGINE_BOOSTMP
208 mp_pow_ui(num, SymEngine::get_num(i),
exp);
209 mp_pow_ui(den, SymEngine::get_den(i),
exp);
210 rational_class val(num, den);
213 mp_pow_ui(SymEngine::get_num(val), SymEngine::get_num(i),
exp);
214 mp_pow_ui(SymEngine::get_den(val), SymEngine::get_den(i),
exp);
228 RCP<const Basic> powrat(
const Rational &other)
const;
233 RCP<const Basic> rpowrat(
const Integer &other)
const;
236 RCP<const Number>
add(
const Number &other)
const override
238 if (is_a<Rational>(other)) {
239 return addrat(down_cast<const Rational &>(other));
240 }
else if (is_a<Integer>(other)) {
241 return addrat(down_cast<const Integer &>(other));
243 return other.
add(*
this);
247 RCP<const Number>
sub(
const Number &other)
const override
249 if (is_a<Rational>(other)) {
250 return subrat(down_cast<const Rational &>(other));
251 }
else if (is_a<Integer>(other)) {
252 return subrat(down_cast<const Integer &>(other));
254 return other.rsub(*
this);
260 if (is_a<Integer>(other)) {
261 return rsubrat(down_cast<const Integer &>(other));
263 throw NotImplementedError(
"Not Implemented");
267 RCP<const Number>
mul(
const Number &other)
const override
269 if (is_a<Rational>(other)) {
270 return mulrat(down_cast<const Rational &>(other));
271 }
else if (is_a<Integer>(other)) {
272 return mulrat(down_cast<const Integer &>(other));
274 return other.
mul(*
this);
278 RCP<const Number>
div(
const Number &other)
const override
280 if (is_a<Rational>(other)) {
281 return divrat(down_cast<const Rational &>(other));
282 }
else if (is_a<Integer>(other)) {
283 return divrat(down_cast<const Integer &>(other));
285 return other.rdiv(*
this);
291 if (is_a<Integer>(other)) {
292 return rdivrat(down_cast<const Integer &>(other));
294 throw NotImplementedError(
"Not Implemented");
298 RCP<const Number>
pow(
const Number &other)
const override
300 if (is_a<Integer>(other)) {
301 return powrat(down_cast<const Integer &>(other));
303 return other.rpow(*
this);
307 RCP<const Number> rpow(
const Number &other)
const override
309 throw NotImplementedError(
"Not Implemented");
312 RCP<const Integer> get_num()
const
314 return integer(SymEngine::get_num(i));
317 RCP<const Integer> get_den()
const
319 return integer(SymEngine::get_den(i));
325 void get_num_den(
const Rational &rat,
const Ptr<RCP<const Integer>> &num,
326 const Ptr<RCP<const Integer>> &den);
#define IMPLEMENT_TYPEID(SYMENGINE_ID)
Inline members and functions.
The lowest unit of symbolic representation.
bool is_negative() const override
const integer_class & as_integer_class() const
Convert to integer_class.
bool is_zero() const override
virtual RCP< const Number > mul(const Number &other) const =0
Multiplication.
virtual RCP< const Number > add(const Number &other) const =0
Addition.
RCP< const Number > rsub(const Number &other) const override
Converts the param other appropriately and then calls rsubrat
bool is_positive() const override
static RCP< const Number > from_mpq(const rational_class &i)
RCP< const Number > pow(const Number &other) const override
Converts the param other appropriately and then calls powrat
const rational_class & as_rational_class() const
Convert to rational_class.
bool is_minus_one() const override
RCP< const Number > mulrat(const Integer &other) const
RCP< const Number > addrat(const Rational &other) const
bool is_complex() const override
rational_class i
i : object of rational_class
RCP< const Number > rdiv(const Number &other) const override
Converts the param other appropriately and then calls rdivrat
RCP< const Number > divrat(const Rational &other) const
bool is_zero() const override
RCP< const Number > mul(const Number &other) const override
Converts the param other appropriately and then calls mulrat
RCP< const Number > divrat(const Integer &other) const
RCP< const Number > sub(const Number &other) const override
Converts the param other appropriately and then calls subrat
static RCP< const Number > from_two_ints(const Integer &n, const Integer &d)
RCP< const Number > add(const Number &other) const override
Converts the param other appropriately and then calls addrat
RCP< const Number > subrat(const Rational &other) const
RCP< const Rational > neg() const
RCP< const Number > powrat(const Integer &other) const
RCP< const Number > subrat(const Integer &other) const
bool is_one() const override
RCP< const Number > mulrat(const Rational &other) const
RCP< const Number > div(const Number &other) const override
Converts the param other appropriately and then calls divrat
RCP< const Number > addrat(const Integer &other) const
bool is_negative() const override
Main namespace for SymEngine package.
std::enable_if< std::is_integral< T >::value, RCP< const Integer > >::type integer(T i)
RCP< const Basic > neg(const RCP< const Basic > &a)
Negation.
RCP< const Basic > exp(const RCP< const Basic > &x)
Returns the natural exponential function E**x = pow(E, x)
RCP< const Number > rational(long n, long d)
convenience creator from two longs
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>