7 using NumberType = LPRealAlgebraicNumber::NumberType;
9 LPRealAlgebraicNumber::~LPRealAlgebraicNumber() {
12 LPRealAlgebraicNumber::LPRealAlgebraicNumber() : m_content(std::make_shared<LPRealAlgebraicNumber::Content>()) {
13 lp_value_construct_zero(get_internal());
16 LPRealAlgebraicNumber::LPRealAlgebraicNumber(
const lp_value_t& num) : m_content(std::make_shared<LPRealAlgebraicNumber::Content>()) {
17 lp_value_construct_copy(get_internal(), &num);
20 LPRealAlgebraicNumber::LPRealAlgebraicNumber(lp_value_t&& num) : m_content(std::make_shared<LPRealAlgebraicNumber::Content>()) {
21 *get_internal() = num;
25 CARL_LOG_DEBUG(
"carl.ran.libpoly",
" Create from poly: " << p <<
" in interval: " << i);
27 lp_upolynomial_t* upoly = to_libpoly_upolynomial(p);
28 lp_interval_t* inter_poly = to_libpoly_interval(i);
30 lp_algebraic_number_t* roots =
new lp_algebraic_number_t[lp_upolynomial_degree(upoly)];
31 std::size_t roots_size;
32 lp_upolynomial_roots_isolate(upoly, roots, &roots_size);
35 for (std::size_t i = 0; i < roots_size; ++i) {
37 lp_value_construct(&tmp, LP_VALUE_ALGEBRAIC, &roots[i]);
38 if (lp_interval_contains(inter_poly, &tmp)) {
39 *get_internal() = tmp;
43 lp_value_destruct(&tmp);
47 for (std::size_t i = 0; i < roots_size; ++i) {
48 lp_algebraic_number_destruct(&roots[i]);
52 lp_upolynomial_delete(upoly);
54 lp_interval_destruct(inter_poly);
61 LPRealAlgebraicNumber::LPRealAlgebraicNumber(
const NumberType& num) : LPRealAlgebraicNumber() {
62 lp_value_construct(get_internal(), LP_VALUE_RATIONAL, (lp_rational_t*)num.get_mpq_t());
65 LPRealAlgebraicNumber::LPRealAlgebraicNumber(
const LPRealAlgebraicNumber& ran) : m_content(ran.m_content) {
67 LPRealAlgebraicNumber::LPRealAlgebraicNumber(LPRealAlgebraicNumber&& ran) : m_content(std::move(ran.m_content)) {
69 LPRealAlgebraicNumber& LPRealAlgebraicNumber::operator=(
const LPRealAlgebraicNumber& n) {
70 m_content = n.m_content;
73 LPRealAlgebraicNumber& LPRealAlgebraicNumber::operator=(LPRealAlgebraicNumber&& n) {
74 m_content = std::move(n.m_content);
82 return LPRealAlgebraicNumber(p, i);
85 bool is_integer(
const LPRealAlgebraicNumber& n) {
86 return lp_value_is_integer(n.get_internal());
88 LPRealAlgebraicNumber::NumberType
integer_below(
const LPRealAlgebraicNumber& n) {
90 lp_integer_construct(&result);
91 lp_value_floor(n.get_internal(), &result);
92 NumberType num = to_rational(&result) ;
93 lp_integer_destruct(&result) ;
100 bool LPRealAlgebraicNumber::is_numeric()
const {
101 return lp_value_is_rational(get_internal());
104 const UnivariatePolynomial<NumberType> LPRealAlgebraicNumber::polynomial()
const {
105 assert(!is_numeric());
106 return to_carl_univariate_polynomial(get_internal()->value.a.f, auxVariable);
109 const Interval<NumberType> LPRealAlgebraicNumber::interval()
const {
111 return Interval<NumberType>(value());
113 return Interval<NumberType>(get_lower_bound(), BoundType::STRICT, get_upper_bound(), BoundType::STRICT);
117 const NumberType LPRealAlgebraicNumber::get_lower_bound()
const {
118 if (is_numeric())
return value();
119 else return to_rational(&get_internal()->value.a.I.a);
122 const NumberType LPRealAlgebraicNumber::get_upper_bound()
const {
123 if (is_numeric())
return value();
124 else return to_rational(&get_internal()->value.a.I.b);
131 const NumberType LPRealAlgebraicNumber::value()
const {
132 assert(is_numeric());
133 lp_rational_t result;
134 lp_rational_construct(&result);
135 lp_value_get_rational(get_internal(), &result);
136 NumberType num = to_rational(&result) ;
137 lp_rational_destruct(&result) ;
145 LPRealAlgebraicNumber
abs(
const LPRealAlgebraicNumber& n) {
146 if (n.is_numeric()) {
147 CARL_LOG_DEBUG(
"carl.ran.libpoly",
"Algebraic NumberType abs got numeric value");
148 return LPRealAlgebraicNumber(
carl::abs(n.value()));
151 int sign = lp_value_sgn(n.get_internal());
152 CARL_LOG_DEBUG(
"carl.ran.libpoly",
"Algebraic NumberType abs got sign : " << sign <<
" of " << n);
155 return LPRealAlgebraicNumber(n);
158 lp_value_construct_none(&val);
159 lp_value_neg(&val, n.get_internal());
160 auto ret = LPRealAlgebraicNumber(std::move(val));
165 std::size_t
bitsize(
const LPRealAlgebraicNumber& n) {
166 if (n.is_numeric()) {
169 return carl::bitsize(n.get_lower_bound()) +
carl::bitsize(n.get_upper_bound()) + lp_upolynomial_degree(n.get_internal()->value.a.f);
173 Sign sgn(
const LPRealAlgebraicNumber& n) {
174 return static_cast<Sign>(lp_value_sgn(n.get_internal()));
177 Sign sgn(
const LPRealAlgebraicNumber&,
const UnivariatePolynomial<LPRealAlgebraicNumber::NumberType>&) {
182 bool contained_in(
const LPRealAlgebraicNumber& n,
const Interval<LPRealAlgebraicNumber::NumberType>& i) {
183 CARL_LOG_DEBUG(
"carl.ran.libpoly",
"ran " << n <<
" contained in " << i);
184 lp_interval_t* inter = to_libpoly_interval(i);
185 bool res = lp_interval_contains(inter, n.get_internal());
186 lp_interval_destruct(inter);
197 void refine(
const LPRealAlgebraicNumber& n) {
198 CARL_LOG_DEBUG(
"carl.ran.libpoly",
"Refining Algebraic NumberType : " << n);
200 if (n.is_numeric())
return;
202 while (lp_dyadic_interval_size(&n.get_internal()->value.a.I) > 0 && !n.is_numeric()) {
203 lp_algebraic_number_refine_const(&n.get_internal()->value.a);
205 CARL_LOG_DEBUG(
"carl.ran.libpoly",
"Finished Refining Algebraic NumberType : " << n);
208 void LPRealAlgebraicNumber::refine()
const {
213 if (n.is_numeric())
return n.value();
215 lp_dyadic_rational_t res;
216 lp_dyadic_rational_construct(&res);
217 lp_algebraic_number_get_dyadic_midpoint(&n.get_internal()->value.a, &res);
218 auto result = to_rational(&res);
219 lp_dyadic_rational_destruct(&res);
224 NumberType
sample_above(
const LPRealAlgebraicNumber& n) {
228 lp_value_construct(&inf, LP_VALUE_PLUS_INFINITY, 0);
231 lp_value_construct_none(&res);
232 lp_value_get_value_between(n.get_internal(),
true, &inf,
true, &res);
233 auto val = to_rational(&res);
234 lp_value_destruct(&res);
239 NumberType
sample_below(
const LPRealAlgebraicNumber& n) {
244 lp_value_construct(&inf, LP_VALUE_MINUS_INFINITY, 0);
247 lp_value_construct_none(&res);
248 lp_value_get_value_between(&inf,
true, n.get_internal(),
true, &res);
249 auto val = to_rational(&res);
250 lp_value_destruct(&res);
255 NumberType
sample_between(
const LPRealAlgebraicNumber& lower,
const LPRealAlgebraicNumber& upper) {
256 CARL_LOG_DEBUG(
"carl.ran.libpoly",
"Sampling between: " << lower <<
" and " << upper);
259 lp_value_construct_none(&res);
260 lp_value_get_value_between(lower.get_internal(),
true, upper.get_internal(),
true, &res);
261 auto val = to_rational(&res);
262 lp_value_destruct(&res);
266 NumberType
sample_between(
const LPRealAlgebraicNumber& lower,
const NumberType& upper) {
267 CARL_LOG_DEBUG(
"carl.ran.libpoly",
"Sampling between: " << lower <<
" and " << upper);
270 lp_value_construct(&v_upper, LP_VALUE_RATIONAL, upper.get_mpq_t());
273 lp_value_construct_none(&res);
274 lp_value_get_value_between(lower.get_internal(),
true, &v_upper,
true, &res);
276 lp_value_destruct(&v_upper);
278 auto val = to_rational(&res);
279 lp_value_destruct(&res);
283 NumberType
sample_between(
const NumberType& lower,
const LPRealAlgebraicNumber& upper) {
284 CARL_LOG_DEBUG(
"carl.ran.libpoly",
"Sampling between: " << lower <<
" and " << upper);
287 lp_value_construct(&v_lower, LP_VALUE_RATIONAL, lower.get_mpq_t());
290 lp_value_construct_none(&res);
291 lp_value_get_value_between(&v_lower,
true, upper.get_internal(),
true, &res);
293 lp_value_destruct(&v_lower);
295 auto val = to_rational(&res);
296 lp_value_destruct(&res);
300 NumberType
floor(
const LPRealAlgebraicNumber& n) {
303 lp_integer_construct(&val);
304 lp_value_floor(n.get_internal(), &val);
305 NumberType ret = to_rational(&val);
306 lp_integer_destruct(&val) ;
311 NumberType
ceil(
const LPRealAlgebraicNumber& n) {
314 lp_integer_construct(&val);
315 lp_value_ceiling(n.get_internal(), &val);
316 NumberType ret = to_rational(&val);
317 lp_integer_destruct(&val) ;
321 bool compare(
const LPRealAlgebraicNumber& lhs,
const LPRealAlgebraicNumber& rhs,
const Relation relation) {
322 if (lhs.m_content == rhs.m_content) {
336 int cmp = lp_value_cmp(lhs.get_internal(), rhs.get_internal());
339 if (cmp == 0) lhs.m_content = rhs.m_content;
361 bool compare(
const LPRealAlgebraicNumber& lhs,
const NumberType& rhs,
const Relation relation) {
362 int cmp = lp_value_cmp_rational(lhs.get_internal(), (lp_rational_t*)rhs.get_mpq_t());
384 std::ostream&
operator<<(std::ostream& os,
const LPRealAlgebraicNumber& ran) {
385 char* str = lp_value_to_string(ran.get_internal());
#define CARL_LOG_DEBUG(channel, msg)
std::ostream & operator<<(std::ostream &os, const GbBenchmark< C, O, P > &b)
carl is the main namespace for the library.
Interval< Number > ceil(const Interval< Number > &_in)
Method which returns the next larger integer of the passed number or the number itself,...
Interval< Number > abs(const Interval< Number > &_in)
Method which returns the absolute value of the passed number.
Interval< Number > floor(const Interval< Number > &_in)
Method which returns the next smaller integer of this number or the number itself,...
const Number & branching_point(const Number &n)
Number sample_above(const Number &n)
signed compare(const BasicConstraint< Pol > &_constraintA, const BasicConstraint< Pol > &_constraintB)
Compares _constraintA with _constraintB.
Sign
This class represents the sign of a number .
Number integer_below(const IntRepRealAlgebraicNumber< Number > &n)
Sign sgn(const Number &n)
Obtain the sign of the given number.
Variable fresh_real_variable() noexcept
bool contained_in(const IntRepRealAlgebraicNumber< Number > &n, const Interval< Number > &i)
bool is_integer(const Interval< Number > &n)
Number sample_below(const Number &n)
std::size_t bitsize(const cln::cl_I &n)
Get the bit size of the representation of a integer.
Number sample_between(const Number &lower, const Number &upper)
A Variable represents an algebraic variable that can be used throughout carl.
This class represents a univariate polynomial with coefficients of an arbitrary type.