1 #include "../numbers.h"
11 if( mpq_sgn(a.__get_mp()) < 0 )
return false;
12 mpz_class den = a.get_den();
13 mpz_class num = a.get_num();
15 mpz_class root_den_rem;
16 mpz_sqrtrem(root_den.__get_mp(), root_den_rem.__get_mp(), den.__get_mp());
21 mpz_class root_num_rem;
22 mpz_sqrtrem(root_num.__get_mp(), root_num_rem.__get_mp(), num.__get_mp());
27 mpq_set_z(resNum.get_mpq_t(), root_num.get_mpz_t());
29 mpq_set_z(resDen.get_mpq_t(), root_den.get_mpz_t());
30 mpq_div(b.get_mpq_t(), resNum.get_mpq_t(), resDen.get_mpq_t());
34 mpq_class
sqrt(
const mpq_class& a) {
36 return (r.first + r.second) / 2;
39 std::pair<mpq_class,mpq_class>
sqrt_safe(
const mpq_class& a)
41 if (mpq_sgn(a.__get_mp()) == 0)
return std::make_pair(mpq_class(0),mpq_class(0));
42 assert( mpq_sgn(a.__get_mp()) > 0 );
43 mpz_class den = a.get_den();
44 mpz_class num = a.get_num();
46 mpz_class root_den_rem;
47 mpz_sqrtrem(root_den.__get_mp(), root_den_rem.__get_mp(), den.__get_mp());
50 mpz_class root_num_rem;
51 mpz_sqrtrem(root_num.__get_mp(), root_num_rem.__get_mp(), num.__get_mp());
69 return std::make_pair(lower,upper);
74 assert(mpq_sgn(a.__get_mp()) > 0);
75 mpz_class den = a.get_den();
76 mpz_class num = a.get_num();
78 int den_exact = mpz_root(root_den.__get_mp(), den.__get_mp(), n);
81 int num_exact = mpz_root(root_num.__get_mp(), num.__get_mp(), n);
98 return std::make_pair(lower,upper);
101 std::pair<mpq_class, mpq_class>
sqrt_fast(
const mpq_class& a)
107 mpz_sqrtrem(num.__get_mp(), num_rem.__get_mp(), a.get_num().__get_mp());
110 mpz_sqrtrem(den.__get_mp(), den_rem.__get_mp(), a.get_den().__get_mp());
114 mpq_class exact_root = mpq_class(num) / den;
115 return std::make_pair(exact_root, exact_root);
117 mpq_class lower = mpq_class(num) / (den + 1);
118 mpq_class upper = mpq_class(num) / den;
119 return std::make_pair(lower, upper);
123 mpq_class lower = mpq_class(num) / den;
124 mpq_class upper = (mpq_class(num) + 1) / den;
125 return std::make_pair(lower, upper);
127 mpq_class lower = mpq_class(num) / (den + 1);
128 mpq_class upper = (mpq_class(num) + 1) / den;
129 return std::make_pair(lower, upper);
133 mpq_class exact_root;
134 if (carl::sqrtp(a, exact_root)) {
136 return std::make_pair(exact_root, exact_root);
139 mpf_class af =
sqrt(mpf_class(a));
142 assert(lower * lower < a);
143 assert(upper * upper > a);
144 return std::make_pair(lower, upper);
175 std::string
toString(
const mpq_class& _number,
bool _infix)
178 bool negative = (_number < mpq_class(0));
179 if(negative) s <<
"(-" << (_infix ?
"" :
" ");
192 std::string
toString(
const mpz_class& _number,
bool _infix)
195 bool negative = (_number < mpz_class(0));
196 if(negative) s <<
"(-" << (_infix ?
"" :
" ");
carl is the main namespace for the library.
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 sqrt_exact(const cln::cl_RA &a, cln::cl_RA &b)
Calculate the square root of a fraction if possible.
std::pair< cln::cl_RA, cln::cl_RA > root_safe(const cln::cl_RA &a, uint n)
bool is_zero(const Interval< Number > &i)
Check if this interval is a point-interval containing 0.
cln::cl_I get_num(const cln::cl_RA &n)
Extract the numerator from a fraction.
Interval< Number > sqrt(const Interval< Number > &i)
bool try_parse< mpq_class >(const std::string &n, mpq_class &res)
cln::cl_I get_denom(const cln::cl_RA &n)
Extract the denominator from a fraction.
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.
bool try_parse< mpz_class >(const std::string &n, mpz_class &res)
mpq_class parse< mpq_class >(const std::string &n)
bool parseDecimal(const std::string &input, T &output)
bool parseRational(const std::string &input, T &output)