12 #ifndef INCLUDED_FROM_NUMBERS_H
13 static_assert(
false,
"This file may only be included indirectly by numbers.h");
43 inline bool is_one(
const mpz_class& n) {
47 inline bool is_one(
const mpq_class& n) {
67 inline mpz_class
get_num(
const mpq_class& n) {
71 inline mpz_class
get_num(
const mpz_class& n) {
84 return 0 != mpz_divisible_p(n.get_num_mpz_t(), n.get_den_mpz_t());
96 inline std::size_t
bitsize(
const mpz_class& n) {
97 return mpz_sizeinbase(n.__get_mp(),2);
104 inline std::size_t
bitsize(
const mpq_class& n) {
105 return mpz_sizeinbase(
get_num(n).__get_mp(),2) + mpz_sizeinbase(
get_denom(n).__get_mp(),2);
121 template<
typename Integer>
126 assert(n <= std::numeric_limits<signed long>::max());
127 assert(n >= std::numeric_limits<signed long>::min());
128 return mpz_get_si(n.get_mpz_t());
132 assert(n <= std::numeric_limits<unsigned long>::max());
133 assert(n >= std::numeric_limits<unsigned long>::min());
134 return mpz_get_ui(n.get_mpz_t());
137 template<
typename Integer>
152 template<
typename To,
typename From>
158 mpz_set_ui(res.get_mpz_t(), n);
168 mpz_set_si(res.get_mpz_t(), n);
177 return from_int<mpz_class>(n);
182 return from_int<mpz_class>(n);
204 assert(!std::isinf(n) && !std::isnan(n));
210 assert(!std::isinf(n) && !std::isnan(n));
221 return mpq_class(
static_cast<unsigned long>(n));
226 return mpq_class(
static_cast<signed long>(n));
252 inline mpz_class
abs(
const mpz_class& n) {
254 mpz_abs(res.get_mpz_t(), n.get_mpz_t());
258 inline mpq_class
abs(
const mpq_class& n) {
260 mpq_abs(res.get_mpq_t(), n.get_mpq_t());
264 inline mpz_class
round(
const mpq_class& n) {
268 mpz_fdiv_qr(res.get_mpz_t(), rem.get_mpz_t(), n.get_num_mpz_t(), n.get_den_mpz_t());
274 inline mpz_class
round(
const mpz_class& n) {
278 inline mpz_class
floor(
const mpq_class& n) {
281 mpz_fdiv_q(res.get_mpz_t(), n.get_num_mpz_t(), n.get_den_mpz_t());
285 inline mpz_class
floor(
const mpz_class& n) {
289 inline mpz_class
ceil(
const mpq_class& n) {
292 mpz_cdiv_q(res.get_mpz_t(), n.get_num_mpz_t(), n.get_den_mpz_t());
295 inline mpz_class
ceil(
const mpz_class& n) {
299 inline mpz_class
gcd(
const mpz_class& a,
const mpz_class& b) {
301 mpz_gcd(res.get_mpz_t(), a.get_mpz_t(), b.get_mpz_t());
305 inline mpz_class
lcm(
const mpz_class& a,
const mpz_class& b) {
307 mpz_lcm(res.get_mpz_t(), a.get_mpz_t(), b.get_mpz_t());
311 inline mpq_class
gcd(
const mpq_class& a,
const mpq_class& b) {
313 mpz_gcd(resNum.get_mpz_t(),
get_num(a).get_mpz_t(),
get_num(b).get_mpz_t());
317 mpq_set_z(resqNum.get_mpq_t(), resNum.get_mpz_t());
319 mpq_set_z(resqDen.get_mpq_t(), resDen.get_mpz_t());
321 mpq_div(res.get_mpq_t(), resqNum.get_mpq_t(), resqDen.get_mpq_t());
332 inline mpz_class&
gcd_assign(mpz_class& a,
const mpz_class& b) {
344 inline mpq_class&
gcd_assign(mpq_class& a,
const mpq_class& b) {
349 inline mpq_class
lcm(
const mpq_class& a,
const mpq_class& b) {
351 mpz_lcm(resNum.get_mpz_t(),
get_num(a).get_mpz_t(),
get_num(b).get_mpz_t());
355 mpq_set_z(resqNum.get_mpq_t(), resNum.get_mpz_t());
357 mpq_set_z(resqDen.get_mpq_t(), resDen.get_mpz_t());
359 mpq_div(res.get_mpq_t(), resqNum.get_mpq_t(), resqDen.get_mpq_t());
363 inline mpq_class
log(
const mpq_class& n) {
366 inline mpq_class
log10(
const mpq_class& n) {
370 inline mpq_class
sin(
const mpq_class& n) {
374 inline mpq_class
cos(
const mpq_class& n) {
379 inline mpz_class
pow(
const mpz_class& basis, std::size_t
exp) {
381 mpz_pow_ui(res.get_mpz_t(), basis.get_mpz_t(),
exp);
386 inline mpq_class
pow(
const mpq_class& basis, std::size_t
exp) {
387 mpz_class den = basis.get_den();
389 mpz_pow_ui(powDen.get_mpz_t(), den.get_mpz_t(),
exp);
390 mpz_class num = basis.get_num();
392 mpz_pow_ui(powNum.get_mpz_t(), num.get_mpz_t(),
exp);
394 mpq_set_z(resNum.get_mpq_t(), powNum.get_mpz_t());
396 mpq_set_z(resDen.get_mpq_t(), powDen.get_mpz_t());
398 mpq_div(res.get_mpq_t(), resNum.get_mpq_t(), resDen.get_mpq_t());
410 bool sqrt_exact(
const mpq_class& a, mpq_class& b);
412 mpq_class
sqrt(
const mpq_class& a);
414 std::pair<mpq_class,mpq_class>
sqrt_safe(
const mpq_class& a);
420 std::pair<mpq_class,mpq_class>
root_safe(
const mpq_class& a,
uint n);
429 std::pair<mpq_class,mpq_class>
sqrt_fast(
const mpq_class& a);
431 inline mpz_class
mod(
const mpz_class& n,
const mpz_class& m) {
435 mpz_mod(res.get_mpz_t(),
abs(n).get_mpz_t(), m.get_mpz_t());
439 inline mpz_class
remainder(
const mpz_class& n,
const mpz_class& m) {
443 inline mpz_class
quotient(
const mpz_class& n,
const mpz_class& d)
448 mpz_div(res.get_mpz_t(),
abs(n).get_mpz_t(),
abs(d).get_mpz_t());
452 inline mpz_class
operator/(
const mpz_class& n,
const mpz_class& d)
457 inline mpq_class
quotient(
const mpq_class& n,
const mpq_class& d)
460 mpq_div(res.get_mpq_t(), n.get_mpq_t(), d.get_mpq_t());
464 inline mpq_class
operator/(
const mpq_class& n,
const mpq_class& d)
470 mpz_divmod(
quotient.get_mpz_t(),
remainder.get_mpz_t(), dividend.get_mpz_t(), divisor.get_mpz_t());
479 inline mpq_class
div(
const mpq_class& a,
const mpq_class& b) {
490 inline mpz_class
div(
const mpz_class& a,
const mpz_class& b) {
503 inline mpz_class&
div_assign(mpz_class& a,
const mpz_class& b) {
515 inline mpq_class&
div_assign(mpq_class& a,
const mpq_class& b) {
522 mpq_inv(res.get_mpq_t(), a.get_mpq_t());
526 inline mpq_class
operator *(
const mpq_class& lhs,
const mpq_class& rhs)
529 mpq_mul(res.get_mpq_t(), lhs.get_mpq_t(), rhs.get_mpq_t());
533 std::string
toString(
const mpq_class& _number,
bool _infix=
true);
535 std::string
toString(
const mpz_class& _number,
bool _infix=
true);
carl is the main namespace for the library.
T rationalize(const PreventConversion< mpq_class > &)
Interval< Number > ceil(const Interval< Number > &_in)
Method which returns the next larger integer of the passed number or the number itself,...
Interval< Number > operator/(const Interval< Number > &lhs, const Number &rhs)
Operator for the division of an interval and a number.
std::pair< cln::cl_RA, cln::cl_RA > sqrt_fast(const cln::cl_RA &a)
Compute square root in a fast but less precise way.
std::string toString(Relation r)
Interval< Number > abs(const Interval< Number > &_in)
Method which returns the absolute value of the passed number.
bool is_positive(const cln::cl_I &n)
Interval< Number > floor(const Interval< Number > &_in)
Method which returns the next smaller integer of this number or the number itself,...
Interval< Number > quotient(const Interval< Number > &_lhs, const Interval< Number > &_rhs)
Implements the division with remainder.
mpq_class rationalize< mpq_class >(float n)
bool sqrt_exact(const cln::cl_RA &a, cln::cl_RA &b)
Calculate the square root of a fraction if possible.
cln::cl_I mod(const cln::cl_I &a, const cln::cl_I &b)
Calculate the remainder of the integer division.
std::pair< cln::cl_RA, cln::cl_RA > root_safe(const cln::cl_RA &a, uint n)
cln::cl_RA reciprocal(const cln::cl_RA &a)
cln::cl_I gcd(const cln::cl_I &a, const cln::cl_I &b)
Calculate the greatest common divisor of two integers.
Interval< Number > exp(const Interval< Number > &i)
cln::cl_RA log10(const cln::cl_RA &n)
bool is_zero(const Interval< Number > &i)
Check if this interval is a point-interval containing 0.
Interval< Number > operator*(const Interval< Number > &lhs, const Interval< Number > &rhs)
Operator for the multiplication of two intervals.
Integer to_int(const Interval< Number > &_floatInterval)
Casts the Interval to an arbitrary integer type which has a constructor for a native int.
cln::cl_I get_num(const cln::cl_RA &n)
Extract the numerator from a fraction.
Interval< Number > cos(const Interval< Number > &i)
Interval< Number > sqrt(const Interval< Number > &i)
uint to_int< uint >(const cln::cl_I &n)
void divide(const cln::cl_I ÷nd, const cln::cl_I &divisor, cln::cl_I "ient, cln::cl_I &remainder)
bool try_parse< mpq_class >(const std::string &n, mpq_class &res)
Interval< Number > div(const Interval< Number > &_lhs, const Interval< Number > &_rhs)
Implements the division which assumes that there is no remainder.
cln::cl_RA & div_assign(cln::cl_RA &a, const cln::cl_RA &b)
Divide two fractions.
Interval< Number > log(const Interval< Number > &i)
bool is_integer(const Interval< Number > &n)
bool is_negative(const cln::cl_I &n)
cln::cl_I get_denom(const cln::cl_RA &n)
Extract the denominator from a fraction.
double to_double(const cln::cl_RA &n)
Converts the given fraction to a double.
cln::cl_I lcm(const cln::cl_I &a, const cln::cl_I &b)
Calculate the least common multiple of two integers.
cln::cl_I round(const cln::cl_RA &n)
Round a fraction to next integer.
Interval< Number > sin(const Interval< Number > &i)
mpz_class parse< mpz_class >(const std::string &n)
std::pair< cln::cl_RA, cln::cl_RA > sqrt_safe(const cln::cl_RA &a)
Calculate the square root of a fraction.
sint to_int< sint >(const cln::cl_I &n)
mpz_class to_int< mpz_class >(const mpq_class &n)
Convert a fraction to an integer.
std::size_t bitsize(const cln::cl_I &n)
Get the bit size of the representation of a integer.
cln::cl_I remainder(const cln::cl_I &a, const cln::cl_I &b)
Calculate the remainder of the integer division.
To from_int(const From &n)
cln::cl_I & gcd_assign(cln::cl_I &a, const cln::cl_I &b)
Calculate the greatest common divisor of two integers.
bool try_parse< mpz_class >(const std::string &n, mpz_class &res)
Interval< Number > pow(const Interval< Number > &i, Integer exp)
mpq_class parse< mpq_class >(const std::string &n)
bool is_one(const Interval< Number > &i)
Check if this interval is a point-interval containing 1.
auto & get(const std::string &name)