1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
|
// Copyright (c) 2008 Max-Planck-Institute Saarbruecken (Germany).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org)
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1/Number_types/include/CGAL/leda_bigfloat_interval.h $
// $Id: include/CGAL/leda_bigfloat_interval.h b26b07a1242 $
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s) : Michael Hemmer
// TODO: add sign to RET
#ifndef CGAL_LEDA_BIGFLOAT_INTERVAL_H
#define CGAL_LEDA_BIGFLOAT_INTERVAL_H
#include <CGAL/basic.h>
#include <CGAL/LEDA_basic.h>
#include <LEDA/numbers/bigfloat.h>
#include <boost/numeric/interval.hpp>
#include <CGAL/Interval_traits.h>
#include <CGAL/Bigfloat_interval_traits.h>
#include <CGAL/ipower.h>
namespace CGAL {
namespace internal {
struct Rounding_for_leda_bigfloat {
private: typedef leda::bigfloat T;
public:
Rounding_for_leda_bigfloat(){};
~Rounding_for_leda_bigfloat(){};
T conv_down(const T& a){
return round(a,leda::bigfloat::get_precision(),leda::TO_N_INF);
};
T conv_up (const T& a){
return round(a,leda::bigfloat::get_precision(),leda::TO_P_INF);
};
// mathematical operations
T add_down(const T& a, const T& b){
return add(a,b,leda::bigfloat::get_precision(),leda::TO_N_INF);
};
T add_up (const T& a, const T& b){
return add(a,b,leda::bigfloat::get_precision(),leda::TO_P_INF);
};
T sub_down(const T& a, const T& b){
return sub(a, b, leda::bigfloat::get_precision(),leda::TO_N_INF);
};
T sub_up (const T& a, const T& b){
return sub(a, b, leda::bigfloat::get_precision(),leda::TO_P_INF);
};
T mul_down(const T& a, const T& b){
return mul(a, b, leda::bigfloat::get_precision(),leda::TO_N_INF);
};
T mul_up (const T& a, const T& b){
return mul(a, b, leda::bigfloat::get_precision(),leda::TO_P_INF);
};
T div_down(const T& a, const T& b){
return div(a, b, leda::bigfloat::get_precision(),leda::TO_N_INF);
};
T div_up (const T& a, const T& b){
return div(a, b, leda::bigfloat::get_precision(),leda::TO_P_INF);
};
T sqrt_down(const T& a){
return sqrt(a, leda::bigfloat::get_precision(),leda::TO_N_INF);
};
T sqrt_up (const T& a){
return sqrt(a, leda::bigfloat::get_precision(),leda::TO_P_INF);
};
T median(const T& a, const T& b){ return (a+b)/2; };
T int_down(const T& a) { return T(floor(a));};
T int_up (const T& a) { return T(ceil(a)); };
};
class Checking_for_leda_bigfloat {
typedef leda::bigfloat T;
public:
static T pos_inf() {
T b = T(5) / T(0);
CGAL_assertion(leda::ispInf(b));
return b;
}
static T neg_inf() {
T b = T(-5) / T(0);
CGAL_assertion(leda::isnInf(b));
return b;
}
static T nan() {
T b = T(0)*pos_inf();
CGAL_assertion(leda::isNaN(b));
return b;
}
static bool is_nan(const T& b) {
return leda::isNaN(b);
}
static T empty_lower() {
return T(1);
}
static T empty_upper() {
return T(0);
}
static bool is_empty(const T& a, const T& b) {
//return a==T(1) && b == T(0);
return a > b;
}
};
} // namespace internal
} //namespace CGAL
namespace boost {
namespace numeric {
inline
std::ostream& operator <<
(std::ostream& os, const boost::numeric::interval<leda::bigfloat>& x)
{
os << "["
<< x.lower().get_significant() << "*2^" << x.lower().get_exponent()
<< " , "
<< x.upper().get_significant() << "*2^" << x.upper().get_exponent()
<< "]";
return os;
}
}//namespace numeric
}//namespace boost
namespace CGAL {
typedef boost::numeric::interval
< leda::bigfloat,
boost::numeric::interval_lib::policies
< internal::Rounding_for_leda_bigfloat,
internal::Checking_for_leda_bigfloat > >
leda_bigfloat_interval;
} //end of namespace CGAL
#include <CGAL/leda_integer.h>
#include <CGAL/leda_rational.h>
#include <CGAL/leda_real.h>
#include <CGAL/leda_bigfloat.h>
namespace CGAL {
template <> class Algebraic_structure_traits< leda_bigfloat_interval >
: public Algebraic_structure_traits_base< leda_bigfloat_interval,
Field_with_sqrt_tag > {
public:
typedef Tag_false Is_exact;
typedef Tag_true Is_numerical_sensitive;
class Sqrt
: public CGAL::cpp98::unary_function< Type, Type > {
public:
Type operator()( const Type& x ) const {
return ::boost::numeric::sqrt(x);
}
};
};
template <> class Real_embeddable_traits< leda_bigfloat_interval >
: public INTERN_RET::Real_embeddable_traits_base< leda_bigfloat_interval , CGAL::Tag_true > {
public:
class Abs
: public CGAL::cpp98::unary_function< Type, Type > {
public:
Type operator()( const Type& x ) const {
return ::boost::numeric::abs(x);
}
};
class To_double
: public CGAL::cpp98::unary_function< Type, double > {
public:
double operator()( const Type& x ) const {
return CGAL::to_double(::boost::numeric::median(x));
}
};
class To_interval
: public CGAL::cpp98::unary_function< Type, std::pair< double, double > > {
public:
std::pair<double, double> operator()( const Type& x ) const {
std::pair<double, double> lower_I(CGAL::to_interval(x.lower()));
std::pair<double, double> upper_I(CGAL::to_interval(x.upper()));
return std::pair< double, double >(
(CGAL::min)(lower_I.first , upper_I.first ),
(CGAL::max)(lower_I.second, upper_I.second));
}
};
};
// Coercion traits:
CGAL_DEFINE_COERCION_TRAITS_FROM_TO(short ,leda_bigfloat_interval)
CGAL_DEFINE_COERCION_TRAITS_FROM_TO(int ,leda_bigfloat_interval)
CGAL_DEFINE_COERCION_TRAITS_FROM_TO(long ,leda_bigfloat_interval)
CGAL_DEFINE_COERCION_TRAITS_FROM_TO(float ,leda_bigfloat_interval)
CGAL_DEFINE_COERCION_TRAITS_FROM_TO(double ,leda_bigfloat_interval)
CGAL_DEFINE_COERCION_TRAITS_FROM_TO(::leda::bigfloat ,leda_bigfloat_interval)
template <>
struct Coercion_traits< leda_bigfloat_interval , ::leda::integer >{
typedef Tag_true Are_explicit_interoperable;
typedef Tag_false Are_implicit_interoperable;
typedef leda_bigfloat_interval Type;
struct Cast{
typedef Type result_type;
Type operator()(const leda_bigfloat_interval& x) const { return x;}
Type operator()(const ::leda::integer& x) const {
leda::bigfloat tmp(x);
leda_bigfloat_interval result(
round(tmp,leda::bigfloat::get_precision(),leda::TO_N_INF),
round(tmp,leda::bigfloat::get_precision(),leda::TO_P_INF));
CGAL_postcondition( result.lower() <= x );
CGAL_postcondition( result.upper() >= x );
return result;
}
};
};
template <>
struct Coercion_traits< leda_bigfloat_interval , ::leda::rational >{
typedef Tag_true Are_explicit_interoperable;
typedef Tag_false Are_implicit_interoperable;
typedef leda_bigfloat_interval Type;
struct Cast{
typedef Type result_type;
Type operator()(const leda_bigfloat_interval& x) const { return x;}
Type operator()(const ::leda::rational& x) const {
long prec = ::leda::bigfloat::get_precision();
leda_bigfloat_interval result (
leda_bigfloat::from_rational(x,prec,leda::TO_N_INF),
leda_bigfloat::from_rational(x,prec,leda::TO_P_INF));
CGAL_postcondition( result.lower() <= x );
CGAL_postcondition( result.upper() >= x );
return result;
}
};
};
template <>
struct Coercion_traits< leda_bigfloat_interval , ::leda::real >{
typedef Tag_true Are_explicit_interoperable;
typedef Tag_false Are_implicit_interoperable;
typedef leda_bigfloat_interval Type;
struct Cast{
typedef Type result_type;
Type operator()(const leda_bigfloat_interval& x) const { return x;}
Type operator()(const ::leda::real& x) const {
long current_prec = ::leda::bigfloat::get_precision();
x.guarantee_relative_error(current_prec);
leda_bigfloat_interval
result(x.get_lower_bound(), x.get_upper_bound());
CGAL_postcondition( result.lower() <= x );
CGAL_postcondition( result.upper() >= x );
return result;
}
};
};
template <> struct Coercion_traits< ::leda::integer, leda_bigfloat_interval >
:public Coercion_traits< leda_bigfloat_interval , ::leda::integer >{};
template <> struct Coercion_traits< ::leda::rational, leda_bigfloat_interval >
:public Coercion_traits< leda_bigfloat_interval , ::leda::rational >{};
template <> struct Coercion_traits< ::leda::real, leda_bigfloat_interval >
:public Coercion_traits< leda_bigfloat_interval , ::leda::real>{};
template<>
class Interval_traits<leda_bigfloat_interval>
:public internal::Interval_traits_base<leda_bigfloat_interval>
{
public:
typedef Interval_traits<leda_bigfloat_interval> Self;
typedef leda_bigfloat_interval Interval;
typedef leda::bigfloat Bound;
typedef CGAL::Tag_true Is_interval;
typedef CGAL::Tag_true With_empty_interval;
struct Construct :public CGAL::cpp98::binary_function<Bound,Bound,Interval>{
Interval operator()( const Bound& l,const Bound& r) const {
CGAL_precondition( l < r );
return Interval(l,r);
}
};
struct Lower :public CGAL::cpp98::unary_function<Interval,Bound>{
Bound operator()( const Interval& a ) const {
return a.lower();
}
};
struct Upper :public CGAL::cpp98::unary_function<Interval,Bound>{
Bound operator()( const Interval& a ) const {
return a.upper();
}
};
struct Width :public CGAL::cpp98::unary_function<Interval,Bound>{
Bound operator()( const Interval& a ) const {
return ::boost::numeric::width(a);
}
};
struct Median :public CGAL::cpp98::unary_function<Interval,Bound>{
Bound operator()( const Interval& a ) const {
return ::boost::numeric::median(a);
}
};
struct Norm :public CGAL::cpp98::unary_function<Interval,Bound>{
Bound operator()( const Interval& a ) const {
return ::boost::numeric::norm(a);
}
};
struct Empty :public CGAL::cpp98::unary_function<Interval,bool>{
bool operator()( const Interval& a ) const {
return ::boost::numeric::empty(a);
}
};
struct Singleton :public CGAL::cpp98::unary_function<Interval,bool>{
bool operator()( const Interval& a ) const {
return ::boost::numeric::singleton(a);
}
};
struct Zero_in :public CGAL::cpp98::unary_function<Interval,bool>{
bool operator()( const Interval& a ) const {
return ::boost::numeric::in_zero(a);
}
};
struct In :public CGAL::cpp98::binary_function<Bound,Interval,bool>{
bool operator()( Bound x, const Interval& a ) const {
return ::boost::numeric::in(x,a);
}
};
struct Equal :public CGAL::cpp98::binary_function<Interval,Interval,bool>{
bool operator()( const Interval& a, const Interval& b ) const {
return ::boost::numeric::equal(a,b);
}
};
struct Overlap :public CGAL::cpp98::binary_function<Interval,Interval,bool>{
bool operator()( const Interval& a, const Interval& b ) const {
return ::boost::numeric::overlap(a,b);
}
};
struct Subset :public CGAL::cpp98::binary_function<Interval,Interval,bool>{
bool operator()( const Interval& a, const Interval& b ) const {
return ::boost::numeric::subset(a,b);
}
};
struct Proper_subset :public CGAL::cpp98::binary_function<Interval,Interval,bool>{
bool operator()( const Interval& a, const Interval& b ) const {
return ::boost::numeric::proper_subset(a,b);
}
};
struct Hull :public CGAL::cpp98::binary_function<Interval,Interval,Interval>{
Interval operator()( const Interval& a, const Interval& b ) const {
return ::boost::numeric::hull(a,b);
}
};
struct Intersection :public CGAL::cpp98::binary_function<Interval,Interval,Interval>{
Interval operator()( const Interval& a, const Interval& b ) const {
Interval r = ::boost::numeric::intersect(a,b);
return r;
}
};
};
template<>
class Bigfloat_interval_traits<leda_bigfloat_interval>
:public Interval_traits<leda_bigfloat_interval>
{
typedef leda_bigfloat_interval NT;
typedef leda::bigfloat BF;
public:
typedef Bigfloat_interval_traits<leda_bigfloat_interval> Self;
typedef CGAL::Tag_true Is_bigfloat_interval;
// struct Get_significant_bits : public CGAL::cpp98::unary_function<NT,long>{
// long operator()( NT x) const {
// CGAL_precondition(!Singleton()(x));
// leda::bigfloat lower = x.lower();
// leda::bigfloat upper = x.upper();
// leda::integer lower_m = lower.get_significant();
// leda::integer upper_m = upper.get_significant();
// leda::integer lower_exp = lower.get_exponent();
// leda::integer upper_exp = upper.get_exponent();
// long shift = (upper_exp - lower_exp).to_long();
// if(shift >= 0 ) upper_m = (upper_m << shift);
// else lower_m = (lower_m << -shift);
// //CGAL_postcondition(lower_m.length() == upper_m.length());
// leda::integer err = upper_m - lower_m;
// std::cout <<"LEDA: " << lower_m << " " << err << " " << std::endl;
// return CGAL::abs(lower_m.length()-err.length());
// }
// };
struct Relative_precision: public CGAL::cpp98::unary_function<NT,long>{
long operator()(const NT& x) const {
CGAL_precondition(!Singleton()(x));
CGAL_precondition(!CGAL::zero_in(x));
leda::bigfloat w = Width()(x);
w = leda::div(w,Lower()(x),Get_precision()(),leda::TO_P_INF);
return -leda::ilog2(w).to_long();
}
};
struct Set_precision : public CGAL::cpp98::unary_function<long,long> {
long operator()( long prec ) const {
return BF::set_precision(prec);
}
};
struct Get_precision {
// type for the \c AdaptableGenerator concept.
typedef long result_type;
long operator()() const {
return BF::get_precision();
}
};
};
::leda::bigfloat inline relative_error(const leda_bigfloat_interval& x){
if(in_zero(x)){
return CGAL::abs(x).upper();
}else{
return (width(x) / CGAL::abs(x)).upper();
}
}
leda_bigfloat_interval inline ipower(const leda_bigfloat_interval& x, int i ){
return ::boost::numeric::pow(x,i);
}
} //namespace CGAL
#endif // CGAL_LEDA_BIGFLOAT_INTERVAL_H
|