4 return Rational(a.get_num()+b.get_num(), a.get_den()+b.get_den());
8 if (r.is_numeric())
return r.value();
9 return carl::branching_point(r);
13 if (r.is_numeric())
return r.value();
14 return carl::sample_stern_brocot(r.interval(),
false);
18 if (r.is_numeric())
return r.value();
19 Rational res = carl::branching_point(r);
21 res = carl::sample_between(r.interval().lower(), res);
27 if (r.is_numeric())
return r.value();
28 Rational res = carl::branching_point(r);
30 res = carl::sample_between(res, r.interval().upper());
42 inline Rational approximate_root_above<ApxRoot::SAMPLE_MID>(
const RAN& inner,
const RAN& outer) {
43 return carl::sample_between(inner, outer);
46 inline Rational approximate_root_below<ApxRoot::SAMPLE_MID>(
const RAN& inner,
const RAN& outer) {
47 return carl::sample_between(outer, inner);
51 inline Rational approximate_root_above<ApxRoot::SIMPLE_REPRESENTATION>(
const RAN& inner,
const RAN& outer) {
52 assert(inner < outer);
55 if (carl::is_integer(outer)) outer_simple = outer.value() - 1;
56 else if (outer.is_numeric()) outer_simple = carl::floor(outer.value());
57 else outer_simple = carl::floor(outer.interval().lower());
59 if (outer_simple > inner)
return outer_simple;
63 assert(inner_simple < outer_simple);
65 return carl::sample_stern_brocot(region,
false);
69 inline Rational approximate_root_below<ApxRoot::SIMPLE_REPRESENTATION>(
const RAN& inner,
const RAN& outer) {
70 assert(inner > outer);
73 if (carl::is_integer(outer)) outer_simple = outer.value() + 1;
74 else if (outer.is_numeric()) outer_simple = carl::ceil(outer.value());
75 else outer_simple = carl::ceil(outer.interval().upper());
77 if (outer_simple < inner)
return outer_simple;
82 assert(inner_simple > outer_simple);
84 return carl::sample_stern_brocot(region,
false);
88 inline Rational approximate_root_above<ApxRoot::STERN_BROCOT>(
const RAN& inner,
const RAN& outer) {
89 Rational inner_simple, outer_simple, mid;
91 if (inner.is_numeric()) inner_simple = carl::ceil(inner.value());
92 else inner_simple = carl::ceil(inner.interval().upper());
93 if (outer.is_numeric()) outer_simple = carl::ceil(outer.value());
94 else outer_simple = carl::ceil(outer.interval().upper());
97 while (inner_simple >= outer) {
98 if (inner_simple < outer_simple) outer_simple = inner_simple;
99 inner_simple = inner_simple - 1;
103 mid =
mediant(inner_simple, outer_simple);
104 if (mid >= outer) outer_simple = mid;
107 if (inner < mid) ++i;
115 inline Rational approximate_root_below<ApxRoot::STERN_BROCOT>(
const RAN& inner,
const RAN& outer) {
116 Rational inner_simple, outer_simple, mid;
118 if (inner.is_numeric()) inner_simple = carl::floor(inner.value());
119 else inner_simple = carl::floor(inner.interval().lower());
120 if (outer.is_numeric()) outer_simple = carl::floor(outer.value());
121 else outer_simple = carl::floor(outer.interval().lower());
124 while (inner_simple <= outer) {
125 if (inner_simple > outer_simple) outer_simple = inner_simple;
126 inner_simple = inner_simple + 1;
130 mid =
mediant(inner_simple, outer_simple);
131 if (mid <= outer) outer_simple = mid;
134 if (inner > mid) ++i;
142 inline Rational approximate_root_above<ApxRoot::FIXED_RATIO>(
const RAN& inner,
const RAN& outer) {
148 return carl::sample_stern_brocot(region,
false);
152 inline Rational approximate_root_below<ApxRoot::FIXED_RATIO>(
const RAN& inner,
const RAN& outer) {
158 return carl::sample_stern_brocot(region,
false);
163 return below ? approximate_root_below<AR>(inner, outer) : approximate_root_above<AR>(inner, outer);
Rational approximate_RAN_above(const RAN &r)
Rational approximate_RAN(const RAN &r)
Rational approximate_root_above(const RAN &inner, const RAN &outer)
Rational mediant(Rational a, Rational b)
Rational approximate_RAN_sb(const RAN &r)
const ApxSettings & apx_settings()
Rational approximate_RAN_below(const RAN &r)
Rational approximate_root_below(const RAN &inner, const RAN &outer)
Rational approximate_root(const RAN &inner, const RAN &outer, bool below)
carl::Interval< Rational > RationalInterval
const double root_ratio_lower
const std::size_t n_sb_iterations
const double root_ratio_upper