carl  24.04
Computer ARithmetic Library
LPRan.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <carl-common/config.h>
4 #ifdef USE_LIBPOLY
5 
6 #include <limits>
7 
9 
10 #include <boost/logic/tribool.hpp>
11 #include <list>
12 
13 #include "helper.h"
14 #include "../../core/Relation.h"
15 
16 #include "../common/Operations.h"
17 #include "../common/NumberOperations.h"
18 
19 
20 namespace carl {
21 
22 class LPRealAlgebraicNumber {
23 
24 public:
25  using NumberType = mpq_class;
26 
27 private:
28  struct Content {
29  lp_value_t c;
30  ~Content() {
31  lp_value_destruct(&c);
32  }
33  };
34  mutable std::shared_ptr<Content> m_content;
35 
36  static const Variable auxVariable;
37 
38 public:
39  ~LPRealAlgebraicNumber();
40 
41  /**
42  * Construct as zero
43  */
44  LPRealAlgebraicNumber();
45 
46  /**
47  * Construct from libpoly Algebraic NumberType and takes ownership
48  * @param num, LibPoly Algebraic Number
49  */
50  LPRealAlgebraicNumber(const lp_value_t& num);
51 
52 
53  /**
54  * Move from libpoly Algebraic NumberType (C Interface)
55  * @param num, LibPoly Algebraic Number
56  */
57  LPRealAlgebraicNumber(lp_value_t&& num);
58 
59  /**
60  * Construct from Polynomial and Interval
61  * Asserts that the polynomial has exactly one root in the given interval
62  * Libpoly uses dyadic intervals, to not approximate, we calculate the roots of the polynomial and filter out all roots not in the given interval
63  * @param p Polynomial, which roots represents the algebraic number
64  * @param i Interval, in which the root must lie
65  */
66  LPRealAlgebraicNumber(const carl::UnivariatePolynomial<NumberType>& p, const Interval<NumberType>& i);
67 
68  /**
69  * Construct from NumberType (usually mpq_class)
70  */
71  LPRealAlgebraicNumber(const NumberType& num);
72 
73  LPRealAlgebraicNumber(const LPRealAlgebraicNumber& ran);
74  LPRealAlgebraicNumber(LPRealAlgebraicNumber&& ran);
75  LPRealAlgebraicNumber& operator=(const LPRealAlgebraicNumber& n);
76  LPRealAlgebraicNumber& operator=(LPRealAlgebraicNumber&& n);
77 
78  /**
79  * Create from univariate polynomial and interval with correctness checks
80  */
81  static LPRealAlgebraicNumber create_safe(const carl::UnivariatePolynomial<NumberType>& p, const Interval<NumberType>& i);
82 
83  /**
84  * @return Pointer to the internal libpoly algebraic number (C interface)
85  */
86  inline lp_value_t* get_internal() {
87  return &(m_content->c);
88  }
89 
90  /**
91  * @return Pointer to the internal libpoly algebraic number (C interface)
92  */
93  inline const lp_value_t* get_internal() const {
94  return &(m_content->c);
95  }
96 
97  /**
98  * @return true if the interval is a point or the poly has degree 1
99  */
100  bool is_numeric() const;
101 
102 
103  const UnivariatePolynomial<NumberType> polynomial() const;
104 
105  const Interval<NumberType> interval() const;
106 
107  const NumberType get_upper_bound() const;
108 
109  const NumberType get_lower_bound() const;
110 
111  /**
112  * Converts to Algebraic NumberType to Number(usually mpq_class)
113  * Asserts that the interval is a point or the poly has max degree 1
114  */
115  const NumberType value() const;
116 
117  friend bool compare(const LPRealAlgebraicNumber&, const LPRealAlgebraicNumber&, const Relation);
118  friend bool compare(const LPRealAlgebraicNumber&, const NumberType&, const Relation);
119 
120  friend NumberType branching_point(const LPRealAlgebraicNumber& n);
121  friend NumberType sample_above(const LPRealAlgebraicNumber& n);
122  friend NumberType sample_below(const LPRealAlgebraicNumber& n);
123  friend NumberType sample_between(const LPRealAlgebraicNumber& lower, const LPRealAlgebraicNumber& upper);
124  friend NumberType sample_between(const LPRealAlgebraicNumber& lower, const NumberType& upper);
125  friend NumberType sample_between(const NumberType& lower, const LPRealAlgebraicNumber& upper);
126  friend NumberType floor(const LPRealAlgebraicNumber& n);
127  friend NumberType ceil(const LPRealAlgebraicNumber& n);
128 
129  void refine() const;
130 };
131 
132 bool compare(const LPRealAlgebraicNumber&, const LPRealAlgebraicNumber&, const Relation);
133 bool compare(const LPRealAlgebraicNumber&, const LPRealAlgebraicNumber::NumberType&, const Relation);
134 
135 LPRealAlgebraicNumber::NumberType branching_point(const LPRealAlgebraicNumber& n);
136 LPRealAlgebraicNumber::NumberType sample_above(const LPRealAlgebraicNumber& n);
137 LPRealAlgebraicNumber::NumberType sample_below(const LPRealAlgebraicNumber& n);
138 LPRealAlgebraicNumber::NumberType sample_between(const LPRealAlgebraicNumber& lower, const LPRealAlgebraicNumber& upper);
139 LPRealAlgebraicNumber::NumberType sample_between(const LPRealAlgebraicNumber& lower, const LPRealAlgebraicNumber::NumberType& upper);
140 LPRealAlgebraicNumber::NumberType sample_between(const LPRealAlgebraicNumber::NumberType& lower, const LPRealAlgebraicNumber& upper);
141 LPRealAlgebraicNumber::NumberType floor(const LPRealAlgebraicNumber& n);
142 LPRealAlgebraicNumber::NumberType ceil(const LPRealAlgebraicNumber& n);
143 
144 void refine(const LPRealAlgebraicNumber& n);
145 
146 inline bool is_zero(const LPRealAlgebraicNumber& n) {
147  refine(n);
148  return lp_value_sgn(n.get_internal()) == 0;
149 }
150 
151 bool is_integer(const LPRealAlgebraicNumber& n);
152 LPRealAlgebraicNumber::NumberType integer_below(const LPRealAlgebraicNumber& n);
153 LPRealAlgebraicNumber abs(const LPRealAlgebraicNumber& n);
154 
155 std::size_t bitsize(const LPRealAlgebraicNumber& n);
156 Sign sgn(const LPRealAlgebraicNumber& n);
157 Sign sgn(const LPRealAlgebraicNumber& n, const UnivariatePolynomial<LPRealAlgebraicNumber::NumberType>& p);
158 bool contained_in(const LPRealAlgebraicNumber& n, const Interval<LPRealAlgebraicNumber::NumberType>& i);
159 
160 std::ostream& operator<<(std::ostream& os, const LPRealAlgebraicNumber& ran);
161 
162 template<>
163 struct is_ran_type<LPRealAlgebraicNumber> : std::true_type {};
164 } // namespace carl
165 
166 namespace std {
167 template<>
168 struct hash<carl::LPRealAlgebraicNumber> {
169  std::size_t operator()(const carl::LPRealAlgebraicNumber& n) const {
170  //Todo test if the precisions needs to be adjusted
171  return lp_value_hash_approx(n.get_internal(), 0);
172  }
173 };
174 } // namespace std
175 
176 #endif
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,...
Definition: Interval.h:1535
Interval< Number > abs(const Interval< Number > &_in)
Method which returns the absolute value of the passed number.
Definition: Interval.h:1511
Interval< Number > floor(const Interval< Number > &_in)
Method which returns the next smaller integer of this number or the number itself,...
Definition: Interval.h:1523
const Number & branching_point(const Number &n)
Number sample_above(const Number &n)
std::ostream & operator<<(std::ostream &os, const BasicConstraint< Poly > &c)
Prints the given constraint on the given stream.
signed compare(const BasicConstraint< Pol > &_constraintA, const BasicConstraint< Pol > &_constraintB)
Compares _constraintA with _constraintB.
Definition: Comparison.h:25
Sign
This class represents the sign of a number .
Definition: Sign.h:20
bool is_zero(const Interval< Number > &i)
Check if this interval is a point-interval containing 0.
Definition: Interval.h:1453
Number integer_below(const IntRepRealAlgebraicNumber< Number > &n)
Definition: Ran.h:312
Sign sgn(const Number &n)
Obtain the sign of the given number.
Definition: Sign.h:54
bool contained_in(const IntRepRealAlgebraicNumber< Number > &n, const Interval< Number > &i)
Definition: Ran.h:373
bool is_integer(const Interval< Number > &n)
Definition: Interval.h:1445
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.
Definition: operations.h:96
Relation
Definition: Relation.h:20
Number sample_between(const Number &lower, const Number &upper)
This class represents a univariate polynomial with coefficients of an arbitrary type.