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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
|
// Copyright (c) 2005,2006 INRIA Sophia-Antipolis (France)
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org); you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; version 2.1 of the License.
// See the file LICENSE.LGPL distributed with CGAL.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.2-branch/Number_types/include/CGAL/Root_of_2.h $
// $Id: Root_of_2.h 30034 2006-04-06 09:10:38Z spion $
//
//
// Author(s) : Sylvain Pion, Monique Teillaud, Athanasios Kakargias
#ifndef CGAL_ROOT_OF_2_H
#define CGAL_ROOT_OF_2_H
#include <iostream>
#include <CGAL/basic.h>
#include <CGAL/Root_of_2_fwd.h>
#include <CGAL/Root_of_traits.h>
#include <CGAL/NT_converter.h>
#include <CGAL/enum.h>
#include <CGAL/tags.h>
#include <CGAL/Number_types/internal_functions_comparison_root_of_2.h>
#include <CGAL/Interval_arithmetic.h>
#include <CGAL/Quotient.h>
#include <CGAL/assertions.h>
#include <CGAL/Binary_operator_result.h>
#define CGAL_int(T) typename First_if_different<int, T>::Type
#define CGAL_double(T) typename First_if_different<double, T>::Type
namespace CGAL {
// Number type representing a real root of a polynomial
// of degree 1 or 2 over RT.
//
// It supports :
// - constructor from degree 2 polynomial coefficients and a boolean
// - constructor from degree 1 polynomial coefficients
// - constructor from RT
// - unary operator-()
// - additions, subtractions, multiplications with an RT.
// - additions, subtractions, multiplications with an RootOf_1.
// - square()
// - <, >, <=, >=, ==, != (symetric, mixed with RT, mixed with RootOf_1, mixed with int)
// - compare() (symetric, mixed with RT, mixed with RootOf_1, mixed with int)
// - sign()
// - to_double()
// - to_interval()
// - is_valid()
// - operator[] to access the coefficients (leading coeff is always positive)
// - .conjuguate()
// - .discriminant()
// - .eval_at()
// - .sign_at()
// - .degree()
// - .is_valid()
// - io_tag()
// - operator<<()
// - print() ("pretty" printing)
// - make_root_of_2()
//
// TODO :
// - use Boost.Operators.
// - add inverse(), and division of/by an RT.
// - add subtraction/addition with a degree 2 Root_of of the same field ?
// - add sqrt() (when it's degree 1), or a make_sqrt<RT>(const RT &r) ?
// - add +, -, *, / (when it is possible) ?
// - add constructor from CGAL::Polynomial ?
// There should be a proper separate class Polynomial.
// - in compare_roots, we evaluate the polynomial at some FT, or at some
// root of degree 1 polynomials. It would be nice to have a separate
// polynomial class which performed this task (and others)...
// - overloaded versions of make_root_of_2<>() for Lazy_exact_nt<> and others.
template < typename RT_ >
class Root_of_2 {
RT_ C0,C1,C2; // Coefficients (see below)
char _smaller; // Is it the smaller of the two roots (for degree 2) ?
// (we use a char because it's smaller than a bool)
// the value is the root of P(X) = C2.X^2 + C1.X + C0,
// and C2 > 0.
// _smaller indicates if it's the smaller of the 2 roots.
public:
typedef RT_ RT;
typedef typename Root_of_traits<RT>::RootOf_1 FT;
Root_of_2()
: C0(0), C1(0), C2(1)
{
CGAL_assertion(is_valid());
}
Root_of_2(const RT& c0)
: C0(- CGAL_NTS square(c0)), C1(0), C2(1), _smaller(c0<0)
{
CGAL_assertion(is_valid());
}
Root_of_2(const typename First_if_different<int, RT>::Type & c0)
: C0(- CGAL_NTS square(RT(c0))), C1(0), C2(1), _smaller(c0<0)
{
CGAL_assertion(is_valid());
}
Root_of_2(const typename First_if_different<FT, RT>::Type & c0)
{
typedef CGAL::Rational_traits< FT > Rational;
Rational r;
CGAL_assertion( r.denominator(c0) != 0 );
*this = Root_of_2(r.denominator(c0), - r.numerator(c0));
CGAL_assertion(is_valid());
}
Root_of_2(const RT& c1, const RT& c0)
: C0( - CGAL_NTS square(c0)), C1(0), C2(CGAL_NTS square(c1)),
_smaller( CGAL_NTS sign(c1) == CGAL_NTS sign(c0) )
{
CGAL_assertion(is_valid());
}
Root_of_2(const RT& a, const RT& b, const RT& c, const bool s)
: C0(c), C1(b), C2(a), _smaller(s)
{
CGAL_assertion(a != 0);
if (a < 0) {
C0 = -c;
C1 = -b;
C2 = -a;
}
CGAL_assertion(is_valid());
}
template <typename RT2>
Root_of_2(const Root_of_2<RT2>& r)
: C0(r[0]), C1(r[1]), C2(r[2]), _smaller(r.is_smaller())
{
CGAL_assertion(is_valid());
}
Root_of_2 operator-() const
{
return Root_of_2 (C2, -C1, C0, !_smaller);
}
bool is_valid() const
{
return (C2 > 0) && ! is_negative(discriminant());
}
// The following functions deal with the internal polynomial.
// Probably they should move to a separate polynomial class.
bool is_smaller() const
{
return _smaller;
}
const RT & operator[](int i) const
{
CGAL_assertion(i<3);
return (&C0)[i];
}
RT discriminant() const
{
return CGAL_NTS square(C1) - 4*C0*C2;
}
template < typename T >
T eval_at(const T& x) const
{
return C0 + x * (C1 + x * C2);
}
template < typename T >
Sign sign_at(const T &x) const
{
// Maybe there is slightly more efficient.
return CGAL_NTS sign(eval_at(x));
}
Root_of_2 conjugate() const
{
return Root_of_2(C2, C1, C0, !_smaller);
}
}; // Root_of_2
template < class NT1,class NT2 >
struct NT_converter < Root_of_2<NT1> , Root_of_2<NT2> >
: public std::unary_function< NT1, NT2 >
{
Root_of_2<NT2>
operator()(const Root_of_2<NT1> &a) const
{
return make_root_of_2(NT_converter<NT1,NT2>()(a[2]),NT_converter<NT1,NT2>()(a[1]),
NT_converter<NT1,NT2>()(a[0]),a.is_smaller());
}
};
template < class NT1,class NT2 >
struct NT_converter < NT1 , Root_of_2<NT2> >
: public std::unary_function< NT1, NT2 >
{
Root_of_2<NT2>
operator()(const NT1 &a) const
{
return Root_of_2<NT2>(NT_converter<NT1,NT2>()(a));
}
};
template < class NT1 >
struct NT_converter < Root_of_2<NT1>, Root_of_2<NT1> >
: public std::unary_function< NT1, NT1 >
{
const Root_of_2<NT1> &
operator()(const Root_of_2<NT1> &a) const
{
return a;
}
};
template < typename RT >
Sign
sign(const Root_of_2<RT> &a)
{
// We use an optimized version of the equivalent to :
// return static_cast<Sign>((int) compare(a, 0));
CGAL_assertion(is_valid(a));
// First, we compare 0 to the root of the derivative of a.
// (which is equivalent to a[1])
int sgn = CGAL_NTS sign(a[1]);
if (sgn > 0)
return a.is_smaller() ? NEGATIVE : opposite(CGAL_NTS sign(a[0]));
if (sgn < 0)
return a.is_smaller() ? CGAL_NTS sign(a[0]) : POSITIVE;
if (CGAL_NTS is_zero(a[0]))
return ZERO;
return a.is_smaller() ? NEGATIVE : POSITIVE;
}
template < typename RT >
Comparison_result
compare(const Root_of_2<RT> &a, const Root_of_2<RT> &b)
{
CGAL_assertion(is_valid(a) && is_valid(b));
// Now a and b are both of degree 2.
if (a.is_smaller())
{
if (b.is_smaller())
return CGALi::compare_22_11(a[2], a[1], a[0], b[2], b[1], b[0]);
return CGALi::compare_22_12(a[2], a[1], a[0], b[2], b[1], b[0]);
}
if (b.is_smaller())
return CGALi::compare_22_21(a[2], a[1], a[0], b[2], b[1], b[0]);
return CGALi::compare_22_22(a[2], a[1], a[0], b[2], b[1], b[0]);
}
template < typename RT >
Comparison_result
compare(const Root_of_2<RT> &a,
const typename Root_of_traits< RT >::RootOf_1 &b)
{
typedef typename Root_of_traits< RT >::RootOf_1 RootOf_1;
CGAL_assertion(is_valid(a) && is_valid(b));
RootOf_1 d_a(-a[1],2*a[2]);
int cmp = CGAL_NTS compare(b,d_a);
if (cmp > 0)
return (a.is_smaller() ?
SMALLER :
static_cast<Comparison_result>( -a.sign_at(b)));
if (cmp < 0)
return (a.is_smaller() ?
static_cast<Comparison_result>((int) a.sign_at(b)) :
LARGER);
if (is_zero(a.discriminant()))
return EQUAL;
return (a.is_smaller() ? SMALLER : LARGER);
}
template < typename RT > inline
Comparison_result
compare(const typename Root_of_traits< RT >::RootOf_1 &a,
const Root_of_2<RT> &b)
{
return opposite(CGAL_NTS compare(b, a));
}
template < typename RT >
Comparison_result
compare(const Root_of_2<RT> &a, const RT &b)
{
CGAL_assertion(is_valid(a));
// First, we compare b to the root of the derivative of a.
int cmp = CGAL_NTS compare(2*a[2]*b, -a[1]);
if (cmp > 0)
return a.is_smaller() ? SMALLER
: (Comparison_result) - a.sign_at(b);
if (cmp < 0)
return a.is_smaller() ? (Comparison_result) a.sign_at(b)
: LARGER;
if (is_zero(a.discriminant()))
return EQUAL;
return a.is_smaller() ? SMALLER : LARGER;
}
template < typename RT > inline
Comparison_result
compare(const RT &a, const Root_of_2<RT> &b)
{
return opposite(CGAL_NTS compare(b, a));
}
template < typename RT > inline
Comparison_result
compare(const Root_of_2<RT> &a, const CGAL_int(RT) &b)
{
return CGAL_NTS compare(a, RT(b));
}
template < typename RT > inline
Comparison_result
compare(const CGAL_int(RT) &a, const Root_of_2<RT> &b)
{
return opposite(CGAL_NTS compare(b, RT(a)));
}
template < typename RT >
inline
bool
operator<(const Root_of_2<RT> &a, const Root_of_2<RT> &b)
{
return CGAL_NTS compare(a, b) < 0;
}
template < typename RT >
inline
bool
operator<(const typename Root_of_traits< RT >::RootOf_1 &a,
const Root_of_2<RT> &b)
{
return CGAL_NTS compare(a, b) < 0;
}
template < typename RT >
inline
bool
operator<(const Root_of_2<RT> &a,
const typename Root_of_traits< RT >::RootOf_1 &b)
{
return CGAL_NTS compare(a, b) < 0;
}
template < typename RT >
inline
bool
operator<(const RT &a, const Root_of_2<RT> &b)
{
return CGAL_NTS compare(a, b) < 0;
}
template < typename RT >
inline
bool
operator<(const Root_of_2<RT> &a, const RT &b)
{
return CGAL_NTS compare(a, b) < 0;
}
template < typename RT >
inline
bool
operator<(const CGAL_int(RT) &a, const Root_of_2<RT> &b)
{
return CGAL_NTS compare(a, b) < 0;
}
template < typename RT >
inline
bool
operator<(const Root_of_2<RT> &a, const CGAL_int(RT) &b)
{
return CGAL_NTS compare(a, b) < 0;
}
template < typename RT >
inline
bool
operator>(const Root_of_2<RT> &a, const Root_of_2<RT> &b)
{
return b < a;
}
template < typename RT >
inline
bool
operator>(const typename Root_of_traits< RT >::RootOf_1 &a,
const Root_of_2<RT> &b)
{
return b < a;
}
template < typename RT >
inline
bool
operator>(const Root_of_2<RT> &a,
const typename Root_of_traits< RT >::RootOf_1 &b)
{
return b < a;
}
template < typename RT >
inline
bool
operator>(const RT &a, const Root_of_2<RT> &b)
{
return b < a;
}
template < typename RT >
inline
bool
operator>(const Root_of_2<RT> &a, const RT &b)
{
return b < a;
}
template < typename RT >
inline
bool
operator>(const CGAL_int(RT) &a, const Root_of_2<RT> &b)
{
return b < a;
}
template < typename RT >
inline
bool
operator>(const Root_of_2<RT> &a, const CGAL_int(RT) &b)
{
return b < a;
}
template < typename RT >
inline
bool
operator>=(const Root_of_2<RT> &a, const Root_of_2<RT> &b)
{
return !(a < b);
}
template < typename RT >
inline
bool
operator>=(const typename Root_of_traits< RT >::RootOf_1 &a,
const Root_of_2<RT> &b)
{
return !(a < b);
}
template < typename RT >
inline
bool
operator>=(const Root_of_2<RT> &a,
const typename Root_of_traits< RT >::RootOf_1 &b)
{
return !(a < b);
}
template < typename RT >
inline
bool
operator>=(const RT &a, const Root_of_2<RT> &b)
{
return !(a < b);
}
template < typename RT >
inline
bool
operator>=(const Root_of_2<RT> &a, const RT &b)
{
return !(a < b);
}
template < typename RT >
inline
bool
operator>=(const CGAL_int(RT) &a, const Root_of_2<RT> &b)
{
return !(a < b);
}
template < typename RT >
inline
bool
operator>=(const Root_of_2<RT> &a, const CGAL_int(RT) &b)
{
return !(a < b);
}
template < typename RT >
inline
bool
operator<=(const Root_of_2<RT> &a, const Root_of_2<RT> &b)
{
return !(a > b);
}
template < typename RT >
inline
bool
operator<=(const typename Root_of_traits< RT >::RootOf_1 &a,
const Root_of_2<RT> &b)
{
return !(a > b);
}
template < typename RT >
inline
bool
operator<=(const Root_of_2<RT> &a,
const typename Root_of_traits< RT >::RootOf_1 &b)
{
return !(a > b);
}
template < typename RT >
inline
bool
operator<=(const RT &a, const Root_of_2<RT> &b)
{
return !(a > b);
}
template < typename RT >
inline
bool
operator<=(const Root_of_2<RT> &a, const RT &b)
{
return !(a > b);
}
template < typename RT >
inline
bool
operator<=(const CGAL_int(RT) &a, const Root_of_2<RT> &b)
{
return !(a > b);
}
template < typename RT >
inline
bool
operator<=(const Root_of_2<RT> &a, const CGAL_int(RT) &b)
{
return !(a > b);
}
template < typename RT >
inline
bool
operator==(const Root_of_2<RT> &a, const Root_of_2<RT> &b)
{
return CGAL_NTS compare(a, b) == 0;
}
template < typename RT >
inline
bool
operator==(const typename Root_of_traits< RT >::RootOf_1 &a,
const Root_of_2<RT> &b)
{
return CGAL_NTS compare(a, b) == 0;
}
template < typename RT >
inline
bool
operator==(const Root_of_2<RT> &a,
const typename Root_of_traits< RT >::RootOf_1 &b)
{
return CGAL_NTS compare(a, b) == 0;
}
template < typename RT >
inline
bool
operator==(const RT &a, const Root_of_2<RT> &b)
{
return CGAL_NTS compare(a, b) == 0;
}
template < typename RT >
inline
bool
operator==(const Root_of_2<RT> &a, const RT &b)
{
return CGAL_NTS compare(a, b) == 0;
}
template < typename RT >
inline
bool
operator==(const CGAL_int(RT) &a, const Root_of_2<RT> &b)
{
return CGAL_NTS compare(a, b) == 0;
}
template < typename RT >
inline
bool
operator==(const Root_of_2<RT> &a, const CGAL_int(RT) &b)
{
return CGAL_NTS compare(a, b) == 0;
}
template < typename RT >
inline
bool
operator!=(const Root_of_2<RT> &a, const Root_of_2<RT> &b)
{
return !(a == b);
}
template < typename RT >
inline
bool
operator!=(const typename Root_of_traits< RT >::RootOf_1 &a,
const Root_of_2<RT> &b)
{
return !(a == b);
}
template < typename RT >
inline
bool
operator!=(const Root_of_2<RT> &a,
const typename Root_of_traits< RT >::RootOf_1 &b)
{
return !(a == b);
}
template < typename RT >
inline
bool
operator!=(const RT &a, const Root_of_2<RT> &b)
{
return !(a == b);
}
template < typename RT >
inline
bool
operator!=(const Root_of_2<RT> &a, const RT &b)
{
return !(a == b);
}
template < typename RT >
inline
bool
operator!=(const CGAL_int(RT) &a, const Root_of_2<RT> &b)
{
return !(a == b);
}
template < typename RT >
inline
bool
operator!=(const Root_of_2<RT> &a, const CGAL_int(RT) &b)
{
return !(a == b);
}
template < typename RT >
Root_of_2<RT>
square(const Root_of_2<RT> &a)
{
CGAL_assertion(is_valid(a));
// It's easy to get the explicit formulas for the square of the two roots.
// Then it's easy to compute their sum and their product, which gives the
// coefficients of the polynomial (X^2 - Sum X + Product).
return Root_of_2<RT> ( CGAL_NTS square(a[2]),
2 * a[2] * a[0] - CGAL_NTS square(a[1]),
CGAL_NTS square(a[0]),
(a.is_smaller() ^ (a[1]>0)));
}
// Mixed operators with Root_of_2 and RT/FT.
// Specializations of Binary_operator_result.
// Note : T1 can be different from T2 because of quotient types...
template < typename T1, typename T2 >
struct Binary_operator_result <Root_of_2<T1>, Root_of_2<T2> >;
// {
// typedef void type;
// };
template < typename T1, typename T2 >
struct Binary_operator_result <T1, Root_of_2<T2> > {
typedef Root_of_2<T2> type;
};
template < typename T1, typename T2 >
struct Binary_operator_result <Root_of_2<T1>, T2> {
typedef Root_of_2<T1> type;
};
template < typename RT >
struct Binary_operator_result <Root_of_2<RT>, typename Root_of_traits<RT>::RootOf_1 > {
typedef Root_of_2<RT> type;
};
template < typename RT >
struct Binary_operator_result <typename Root_of_traits<RT>::RootOf_1, Root_of_2<RT> > {
typedef Root_of_2<RT> type;
};
template < typename RT >
Root_of_2<RT>
operator-(const Root_of_2<RT> &a,
const typename Root_of_traits< RT >::RootOf_1& b)
{
typedef typename Root_of_traits< RT >::RootOf_1 RO1;
typedef CGAL::Rational_traits< RO1 > Rational;
//RT should be the same as Rational::RT
Rational r;
CGAL_assertion(is_valid(a) && is_valid(b));
const RT &b1 = r.denominator(b);
const RT &b0 = r.numerator(b);
//CGAL_assertion(b1>0);
RT sqb1 = CGAL_NTS square(b1);
RT sqb0 = CGAL_NTS square(b0);
RT b0b1 = b0 * b1;
return Root_of_2<RT>(a[2] * sqb1,
2*a[2]*b0b1 + a[1]*sqb1,
a[2]* sqb0 + a[1]*b0b1 + a[0]*sqb1,
a.is_smaller());
}
template < typename RT >
inline
Root_of_2<RT>
operator-(const typename Root_of_traits< RT >::RootOf_1 &a,
const Root_of_2<RT> &b)
{
return -(b-a);
}
template < typename RT >
Root_of_2<RT>
operator-(const Root_of_2<RT> &a, const RT& b)
{
CGAL_assertion(is_valid(a) && is_valid(b));
// It's easy to see it using the formula (X^2 - sum X + prod).
//RT p = a[0] + b * a[1] + a[2] * CGAL_NTS square(b);
//RT s = a[1] + b * a[2] * 2;
RT tmp = a[2] * b;
RT s = a[1] + tmp;
RT p = a[0] + s * b;
s += tmp;
return Root_of_2<RT>(a[2], s, p, a.is_smaller());
}
template < typename RT >
inline
Root_of_2<RT>
operator-(const RT &a, const Root_of_2<RT> &b)
{
return -(b-a);
}
template < typename RT >
inline
Root_of_2<RT>
operator+(const Root_of_2<RT> &a,
const typename Root_of_traits< RT >::RootOf_1& b)
{
return a - typename Root_of_traits< RT >::RootOf_1(-b);
}
template < typename RT >
inline
Root_of_2<RT>
operator+(const typename Root_of_traits< RT >::RootOf_1 &a,
const Root_of_2<RT> &b)
{
return b - typename Root_of_traits< RT >::RootOf_1(-a);
}
template < typename RT >
inline
Root_of_2<RT>
operator+(const Root_of_2<RT> &a, const RT& b)
{
return a - RT(-b);
}
template < typename RT >
inline
Root_of_2<RT>
operator+(const RT &a, const Root_of_2<RT> &b)
{
return b - RT(-a);
}
template < typename RT >
Root_of_2<RT>
operator*(const Root_of_2<RT> &a,
const typename Root_of_traits< RT >::RootOf_1& b)
{
typedef typename Root_of_traits< RT >::RootOf_1 RO1;
typedef CGAL::Rational_traits< RO1 > Rational;
//RT should be the same as Rational::RT
Rational r;
CGAL_assertion(is_valid(a) && is_valid(b));
const RT &b1 = r.denominator(b);
const RT &b0 = r.numerator(b);
CGAL_assertion(b1>0);
return Root_of_2<RT>(a[2] * CGAL_NTS square(b1), a[1] * b1 * b0,
a[0] * CGAL_NTS square(b0),
CGAL_NTS sign(b) < 0 ? !a.is_smaller()
: a.is_smaller());
}
template < typename RT >
inline
Root_of_2<RT>
operator*(const typename Root_of_traits< RT >::RootOf_1 &a,
const Root_of_2<RT> &b)
{
return b * a;
}
template < typename RT >
Root_of_2<RT>
operator*(const Root_of_2<RT> &a, const RT& b)
{
CGAL_assertion(is_valid(a));
return Root_of_2<RT>(a[2], a[1] * b, a[0] * CGAL_NTS square(b),
b < 0 ? !a.is_smaller() : a.is_smaller());
}
template < typename RT >
inline
Root_of_2<RT>
operator*(const RT &a, const Root_of_2<RT> &b)
{
return b * a;
}
template < typename RT >
Root_of_2<RT>
operator/(const Root_of_2<RT> &a, const RT& b)
{
CGAL_assertion(is_valid(a));
CGAL_assertion(b != 0);
return Root_of_2<RT>(a[2] * CGAL_NTS square(b), a[1] * b, a[0],
b < 0 ? !a.is_smaller() : a.is_smaller());
}
template < typename RT >
Root_of_2<RT>
operator/(const Root_of_2<RT> &a,
const typename Root_of_traits< RT >::RootOf_1& b)
{
typedef typename Root_of_traits< RT >::RootOf_1 RO1;
typedef CGAL::Rational_traits< RO1 > Rational;
//RT should be the same as Rational::RT
Rational r;
CGAL_assertion(is_valid(a) && is_valid(b));
CGAL_assertion(b != 0);
RT b0 = r.denominator(b);
RT b1 = r.numerator(b);
if (b1<0)
b0 = -b0, b1 = -b1;
CGAL_assertion(b1>0);
return Root_of_2<RT>(a[2] * CGAL_NTS square(b1), a[1] * b1 * b0,
a[0] * CGAL_NTS square(b0),
CGAL_NTS sign(b) < 0 ? !a.is_smaller()
: a.is_smaller());
}
template < typename RT >
double
to_double(const Root_of_2<RT> &x)
{
CGAL_assertion(is_valid(x));
double a = CGAL::to_double(x[2]);
double b = CGAL::to_double(x[1]);
double d = std::sqrt(CGAL_NTS to_double(x.discriminant()));
CGAL_assertion(a > 0);
if (x.is_smaller())
d = -d;
return (d-b)/(a*2);
}
template < typename RT >
std::pair<double, double>
to_interval(const Root_of_2<RT> &x)
{
CGAL_assertion(is_valid(x));
Interval_nt<> a = to_interval(x[2]);
Interval_nt<> b = to_interval(x[1]);
Interval_nt<> disc = to_interval(x.discriminant());
Interval_nt<> d = sqrt(disc);
if (x.is_smaller())
d = -d;
return ((d-b)/(a*2)).pair();
}
template < typename RT >
std::ostream &
operator<<(std::ostream &os, const Root_of_2<RT> &r)
{
return os << r[2] << " "
<< r[1] << " "
<< r[0] << " "
<< r.is_smaller() << " ";
//return os << to_double(r);
}
template < typename RT >
std::istream &
operator>>(std::istream &is, Root_of_2<RT> &r)
{
RT a,b,c;
bool s;
is >> a >> b >> c >> s;
if(is)
r = Root_of_2<RT>(a,b,c,s);
return is;
}
template < typename RT >
void
print(std::ostream &os, const Root_of_2<RT> &r)
{
os << "Root_of_2( ("
<< r[2] << ") * X^2 + ("
<< r[1] << ") * X + ("
<< r[0] << ") , "
<< (r.is_smaller() ? "smaller" : "larger") << " )";
}
template < typename RT >
bool
is_valid(const Root_of_2<RT> &r)
{
return r.is_valid();
}
template < class RT >
struct Number_type_traits < Root_of_2<RT> >
{
typedef Tag_false Has_gcd;
typedef Tag_false Has_division;
typedef Tag_false Has_sqrt;
typedef Tag_false Has_exact_sqrt;
typedef Tag_false Has_exact_division;
typedef Tag_false Has_exact_ring_operations;
};
template < typename RT >
inline
io_Operator
io_tag(const Root_of_2<RT>&)
{
return io_Operator();
}
} // namespace CGAL
#undef CGAL_int
#undef CGAL_double
#endif // CGAL_ROOT_OF_2_H
|