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 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
|
/*
* Normaliz
* Copyright (C) 2007-2025 W. Bruns, B. Ichim, Ch. Soeger, U. v. d. Ohe
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* As an exception, when this program is distributed through (i) the App Store
* by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or (iii) Google Play
* by Google Inc., then that store may impose any digital rights management,
* device limits and/or redistribution restrictions that are required by its
* terms of service.
*/
#ifndef LIBNORMALIZ_INTEGER_H_
#define LIBNORMALIZ_INTEGER_H_
#include <list>
#include <vector>
#include <string>
#include <climits>
#include <cmath>
#include <iosfwd>
#include <libnormaliz/general.h>
// Integer should (may) support:
// Integer abs(Integer); here implemented as Iabs
// Integer min(Integer, Integer); here we use the template min in <algorithm>
// It provides abs, gcd and lcm
//---------------------------------------------------------------------------
namespace libnormaliz {
using std::cerr;
using std::endl;
using std::istream;
using std::ostream;
using std::ostringstream;
using std::string;
using std::stringstream;
using std::vector;
using std::ws;
//---------------------------------------------------------------------------
// Basic functions
//---------------------------------------------------------------------------
// returns the absolute value of a
template <typename Integer>
inline Integer Iabs(const Integer& a) {
return (a >= 0) ? (a) : Integer(-a);
}
// returns gcd of a and b, if one is 0 returns the other integer
template <typename Integer>
Integer gcd(const Integer& a, const Integer& b);
template <>
mpz_class gcd<mpz_class>(const mpz_class& a, const mpz_class& b);
// returns lcm of a and b, returns 0 if one is 0
template <typename Integer>
Integer lcm(const Integer& a, const Integer& b);
template <>
mpz_class lcm(const mpz_class& a, const mpz_class& b);
// integer division a/b. Returns quot and rem = minimal remainder <= |b|/2
template <typename Integer>
void minimal_remainder(const Integer& a, const Integer& b, Integer& quot, Integer& rem);
// extended Euclidean algorithm: d=ua+vb
template <typename Integer>
Integer ext_gcd(const Integer& a, const Integer& b, Integer& u, Integer& v);
// minimizes u and v and makes d >= 0.
template <typename Integer>
void sign_adjust_and_minimize(const Integer& a, const Integer& b, Integer& d, Integer& u, Integer& v);
// the following functions behave like the C++ floating point functions with the same name
mpz_class floor(const mpq_class& q);
mpz_class ceil(const mpq_class& q);
mpz_class round(const mpq_class& q);
//---------------------------------------------------------------------------
// Conversions and checks
//---------------------------------------------------------------------------
// convert val to ret
// does the conversion and returns false if it fails
bool try_convert(long& ret, const long long& val);
inline bool try_convert(long long& ret, const long& val) {
ret = val;
return true;
}
bool try_convert(long& ret, const mpz_class& val);
bool try_convert(long long& ret, const mpz_class& val);
inline bool try_convert(mpz_class& ret, const long& val) {
ret = val;
return true;
}
bool try_convert(mpz_class& ret, const long long& val);
bool try_convert(nmz_float& ret, const long& val);
bool try_convert(nmz_float& ret, const long long& val);
bool try_convert(nmz_float& ret, const mpz_class& val);
bool try_convert(long& ret, const nmz_float& val);
bool try_convert(long long& ret, const nmz_float& val);
bool try_convert(mpz_class& ret, const nmz_float& val);
bool try_convert(long& ret, const nmz_float& val);
bool try_convert(long long& ret, const nmz_float& val);
bool try_convert(mpz_class& ret, const nmz_float& val);
nmz_float mpq_to_nmz_float(const mpq_class& val);
#ifdef ENFNORMALIZ
bool try_convert(renf_elem_class& ret, const mpz_class& val);
bool try_convert(mpz_class& ret, const renf_elem_class& val);
bool try_convert(renf_elem_class& ret, const long long& val);
bool try_convert(long long& ret, const renf_elem_class& val);
bool try_convert(renf_elem_class& ret, const long& val);
bool try_convert(long& ret, const renf_elem_class& val);
bool try_convert(mpq_class& ret, const renf_elem_class& val);
bool try_convert(nmz_float& ret, const renf_elem_class& val);
#endif
// template for same type "conversion"
template <typename Type>
inline bool try_convert(Type& ret, const Type& val) {
ret = val;
return true;
}
inline bool try_convert(mpq_class& ret, const mpz_class& val) {
ret = val;
return true;
}
inline bool try_convert(mpq_class& ret, const long& val) {
ret = val;
return true;
}
bool fits_long_range(long long a);
//--------------------------------------------------------------------
template <typename Integer>
inline bool using_GMP() {
return false;
}
template <>
inline bool using_GMP<mpz_class>() {
return true;
}
template <>
inline bool using_GMP<mpq_class>() {
return true;
}
template <typename Integer>
inline bool using_mpq_class() {
return false;
}
template <>
inline bool using_mpq_class<mpq_class>() {
return true;
}
//--------------------------------------------------------------------
template <typename Integer>
inline bool using_float() {
return false;
}
template <>
inline bool using_float<nmz_float>() {
return true;
}
//--------------------------------------------------------------------
template <typename Number>
inline bool using_renf() {
return false;
}
#ifdef ENFNORMALIZ
template <>
inline bool using_renf<renf_elem_class>() {
return true;
}
#endif
//--------------------------------------------------------------------
// for the interpretation of a string as a decimal fraction or floating point number
mpq_class dec_fraction_to_mpq(std::string s);
//--------------------------------------------------------------------
template <typename Integer>
Integer int_max_value_dual();
template <typename Integer>
Integer int_max_value_primary();
//---------------------------------------------------------------------------
/*template<typename Integer>
inline bool is_scalar_zero(const Integer& m){
return m==0;
}
template<>
inline bool is_scalar_zero<nmz_float>(const nmz_float& m){
return (Iabs(m) < 1000000.0*nmz_epsilon);
}*/
template <typename Integer>
inline bool check_range(const Integer& m) {
static Integer max_primary = int_max_value_primary<Integer>();
return (Iabs(m) <= max_primary);
}
template <>
inline bool check_range<mpz_class>(const mpz_class&) {
return true;
}
template <>
inline bool check_range<nmz_float>(const nmz_float&) {
return true;
}
template <>
inline bool check_range<mpq_class>(const mpq_class&) {
return true;
}
#ifdef ENFNORMALIZ
template <>
inline bool check_range<renf_elem_class>(const renf_elem_class&) {
return true;
}
#endif
//---------------------------------------------------------------------------
template <typename Integer>
void check_range_list(const std::list<std::vector<Integer> >& ll);
template <typename Integer>
class CandidateList;
template <typename Integer>
class Candidate;
template <typename Integer>
void check_range_list(const CandidateList<Integer>& ll);
template <typename Integer>
void check_range_list(const std::list<Candidate<Integer> >& ll);
//---------------------------------------------------------------------------
// String conversion functions
//---------------------------------------------------------------------------
// forward declaration to silence clang error:
// 'operator<<' should be declared prior to the call site or in the global namespace
template <typename T>
std::ostream& operator<<(std::ostream& out, const vector<T>& vec);
template <typename Integer>
string toString(Integer a) {
ostringstream ostream;
ostream << a;
return ostream.str();
}
template <>
inline string toString(mpz_class a) {
return a.get_str();
}
template <>
inline string toString(mpq_class a) {
return a.get_str();
}
//----------------------------------------------------------------------
// the next functions produce an integer quotient of absolute values and determine whether
// there is a remainder
bool int_quotient(long long& Quot, const mpz_class& Num, const mpz_class& Den);
bool int_quotient(long& Quot, const long& Num, const long& Den);
bool int_quotient(long long& Quot, const long long& Num, const long long& Den);
bool int_quotient(mpz_class& Quot, const mpz_class& Num, const mpz_class& Den);
template <typename IntegerRet>
bool int_quotient(IntegerRet& Quot, const nmz_float& Num, const nmz_float& Den);
// find the floor and ceol of Num/Den
template <typename IntegerRet, typename IntegerVal>
IntegerRet floor_quot(const IntegerVal Num, IntegerVal Den);
template <typename IntegerRet, typename IntegerVal>
IntegerRet ceil_quot(const IntegerVal Num, IntegerVal Den);
//---------------------------------------------------------------------------
template <typename Integer>
void minimal_remainder(const Integer& a, const Integer& b, Integer& quot, Integer& rem) {
quot = a / b;
rem = a - quot * b;
if (rem == 0)
return;
Integer test = 2 * Iabs(rem) - Iabs(b);
if (test > 0) {
if ((rem < 0 && b > 0) || (rem > 0 && b < 0)) {
rem += b;
quot--;
}
else {
rem -= b;
quot++;
}
}
if (test == 0 && rem < 0) {
rem = -rem;
if (b > 0)
quot--;
else
quot++;
}
}
template <typename Integer>
void sign_adjust_and_minimize(const Integer& a, const Integer& b, Integer& d, Integer& u, Integer& v) {
if (d < 0) {
d = -d;
u = -u;
v = -v;
}
// cout << u << " " << v << endl;
if (b == 0)
return;
Integer sign = 1;
if (a < 0)
sign = -1;
Integer u1 = (sign * u) % (Iabs(b) / d);
if (u1 == 0)
u1 += Iabs(b) / d;
u = sign * u1;
v = (d - a * u) / b;
}
template <typename Integer>
Integer ext_gcd(const Integer& a, const Integer& b, Integer& u, Integer& v) {
u = 1;
v = 0;
Integer d = a;
if (b == 0) {
sign_adjust_and_minimize(a, b, d, u, v);
return (d);
}
Integer v1 = 0;
Integer v3 = b;
Integer q, t1, t3;
while (v3 != 0) {
q = d / v3;
t3 = d - q * v3;
t1 = u - q * v1;
u = v1;
d = v3;
v1 = t1;
v3 = t3;
}
sign_adjust_and_minimize(a, b, d, u, v);
return (d);
}
template <typename Integer>
size_t decimal_length(Integer a) {
ostringstream test;
test << a;
return test.str().size();
}
//---------------------------------------------------------------------------
// Special functions
//---------------------------------------------------------------------------
// return the number of decimals, needed to write the Integer a
template <typename Integer>
size_t decimal_length(Integer a);
template <typename Integer>
mpz_class nmz_factorial(Integer n);
// conversion for integers, throws ArithmeticException if conversion fails
template <typename ToType, typename FromType>
inline void convert(ToType& ret, const FromType& val) {
if (!try_convert(ret, val)) {
throw ArithmeticException(val);
}
}
// conversion of vectors
template <typename ToType, typename FromType>
inline void convert(vector<ToType>& ret_vect, const vector<FromType>& from_vect) {
size_t s = from_vect.size();
ret_vect.resize(s);
for (size_t i = 0; i < s; ++i)
convert(ret_vect[i], from_vect[i]);
}
// general conversion with return, throws ArithmeticException if conversion fails
template <typename ToType, typename FromType>
ToType convertTo(const FromType& val) {
ToType copy;
convert(copy, val);
return copy;
}
// Conversion of mpq_classs to nmz_float needs its own function
// since we did not manage to do it via templates alone
template <typename FromType>
nmz_float convertTo_nmz_float(const FromType& val) {
nmz_float copy;
convert(copy, val);
return copy;
}
template <>
inline nmz_float convertTo_nmz_float(const mpq_class& val) {
return mpq_to_nmz_float(val);
}
// Now the try_convert
inline bool try_convert(mpz_class& ret, const mpq_class&) {
assert(false); // must never be used
return false;
}
#ifdef ENFNORMALIZ
inline bool try_convert(renf_elem_class& ret, const mpz_class& val) {
ret = val;
return true;
}
inline bool try_convert(mpz_class& ret, const renf_elem_class& val) {
renf_elem_class help = val;
if (!help.is_integer())
throw ArithmeticException(". Field element cannot be converted to integer");
ret = help.num();
return true;
}
inline bool try_convert(renf_elem_class& ret, const long long& val) {
ret = convertTo<long>(val);
return true;
}
inline bool try_convert(long long& ret, const renf_elem_class& val) {
mpz_class bridge;
try_convert(bridge, val);
return try_convert(ret, bridge);
}
inline bool try_convert(renf_elem_class& ret, const long& val) {
ret = val;
return true;
}
inline bool try_convert(long& ret, const renf_elem_class& val) {
mpz_class bridge;
try_convert(bridge, val);
return try_convert(ret, bridge);
}
inline bool try_convert(mpq_class& ret, const renf_elem_class& val) {
nmz_float ret_double = static_cast<double>(val);
ret = mpq_class(ret_double);
return true;
}
inline bool try_convert(nmz_float& ret, const renf_elem_class& val) {
ret = static_cast<double>(val);
return true;
}
#endif
inline bool try_convert(long& ret, const long long& val) {
if (fits_long_range(val)) {
ret = val;
return true;
}
return false;
}
inline bool try_convert(long& ret, const mpz_class& val) {
if (!val.fits_slong_p()) {
return false;
}
ret = val.get_si();
return true;
}
inline bool try_convert(long long& ret, const mpz_class& val) {
if (val.fits_slong_p()) {
ret = val.get_si();
return true;
}
if (sizeof(long long) == sizeof(long)) {
return false;
}
mpz_class quot;
ret = mpz_fdiv_q_ui(quot.get_mpz_t(), val.get_mpz_t(), LONG_MAX); // returns remainder
if (!quot.fits_slong_p()) {
return false;
}
ret += ((long long)quot.get_si()) * ((long long)LONG_MAX);
return true;
}
inline bool try_convert(mpz_class& ret, const long long& val) {
if (fits_long_range(val)) {
ret = mpz_class(long(val));
}
else {
ret = mpz_class(long(val % LONG_MAX)) + mpz_class(LONG_MAX) * mpz_class(long(val / LONG_MAX));
}
return true;
}
inline bool try_convert(float& ret, const mpz_class& val) {
if (!val.fits_slong_p())
return false;
long dummy = convertTo<long>(val);
ret = (float)dummy;
return true;
}
inline bool fits_long_range(long long a) {
return sizeof(long long) == sizeof(long) || (a <= LONG_MAX && a >= LONG_MIN);
}
inline bool try_convert(nmz_float& ret, const long& val) {
ret = (nmz_float)val;
return true;
}
inline bool try_convert(nmz_float& ret, const mpz_class& val) {
ret = val.get_d();
return true;
}
inline bool try_convert(mpz_class& ret, const nmz_float& val) {
ret = mpz_class(val);
return true;
}
inline bool try_convert(nmz_float& ret, const long long& val) {
ret = (nmz_float)val;
return true;
}
inline bool try_convert(long& ret, const nmz_float& val) {
mpz_class bridge;
if (!try_convert(bridge, val))
return false;
return try_convert(ret, bridge);
}
inline bool try_convert(long long& ret, const nmz_float& val) {
mpz_class bridge;
if (!try_convert(bridge, val))
return false;
return try_convert(ret, bridge);
}
/*
inline bool fits_short_range(long long a) {
return (a <= SHRT_MAX && a >= SHRT_MIN);
}
inline bool try_convert(short& ret, const long long& val) {
if(!fits_short_range(val))
return false;
ret = val;
return true;
}
inline bool try_convert(short& ret, const mpz_class& val) {
long long bridge = convertTo<long long>(val);
return try_convert(ret,bridge);
}
*/
//---------------------------------------------------------------------------
template <typename Integer>
Integer gcd(const Integer& a, const Integer& b) {
if (a == 0) {
return Iabs<Integer>(b);
}
if (b == 0) {
return Iabs<Integer>(a);
}
Integer q0, q1, r;
q0 = Iabs<Integer>(a);
r = Iabs<Integer>(b);
do {
q1 = r;
r = q0 % q1;
q0 = q1;
} while (r != 0);
return q1;
}
template <>
inline nmz_float gcd(const nmz_float& a, const nmz_float& b) {
if (a == 0 && b == 0)
return 0;
return 1.0;
}
template <>
inline mpz_class gcd(const mpz_class& a, const mpz_class& b) {
mpz_class g;
mpz_gcd(g.get_mpz_t(), a.get_mpz_t(), b.get_mpz_t());
return g;
}
#ifdef ENFNORMALIZ
template <>
inline renf_elem_class gcd(const renf_elem_class& a, const renf_elem_class& b) {
if (a == 0 && b == 0)
return 0;
return 1;
}
#endif
//---------------------------------------------------------------------------
template <typename Integer>
Integer lcm(const Integer& a, const Integer& b) {
if ((a == 0) || (b == 0)) {
return 0;
}
else
return Iabs<Integer>(a * b / gcd<Integer>(a, b));
}
template <>
inline mpz_class lcm<mpz_class>(const mpz_class& a, const mpz_class& b) {
mpz_class g;
mpz_lcm(g.get_mpz_t(), a.get_mpz_t(), b.get_mpz_t());
return g;
}
#ifdef ENFNORMALIZ
template <>
inline renf_elem_class lcm<renf_elem_class>(const renf_elem_class& a, const renf_elem_class& b) {
return 1;
}
#endif
template<typename Val, typename Ret>
void convert_via_string(Ret& V, const Val& W){
stringstream bridge;
bridge << W;
bridge >> V;
}
//---------------------------------------------------------------------------
template <typename Integer>
Integer int_max_value_dual() {
Integer k = sizeof(Integer) * 8 - 2; // number of bytes convetred to number of bits
Integer test = 1;
test = test << k; // 2^k
return test;
}
// bool int_max_value_dual_long_computed = false;
template <>
inline long int_max_value_dual() {
static long max_value;
if (int_max_value_dual_long_computed)
return max_value;
long k = sizeof(long) * 8 - 2; // number of bytes convetred to number of bits
long test = 1;
test = test << k; // 2^k
// test=0; // 10000;
max_value = test;
int_max_value_dual_long_computed = true;
return test;
}
// bool int_max_value_dual_long_long_computed = false;
template <>
inline long long int_max_value_dual() {
static long long max_value;
if (int_max_value_dual_long_long_computed)
return max_value;
long long k = sizeof(long long) * 8 - 2; // number of bytes convetred to number of bits
long long test = 1;
test = test << k; // 2^k
// test=0; // 10000;
max_value = test;
int_max_value_dual_long_long_computed = true;
return test;
}
//---------------------------------------------------------------------------
template <>
inline mpz_class int_max_value_dual<mpz_class>() {
assert(false);
return 0;
}
//---------------------------------------------------------------------------
template <typename Integer>
Integer int_max_value_primary() {
Integer k = sizeof(Integer) * 8 - 12; // number of bytes convetred to number of bits
Integer test = 1;
test = test << k; // 2^k
// test=0; // 10000;
return test;
}
// bool int_max_value_primary_long_computed = false;
template <>
inline long int_max_value_primary() {
static long max_value;
if (int_max_value_primary_long_computed)
return max_value;
long k = sizeof(long) * 8 - 12; // number of bytes convetred to number of bits
long test = 1;
test = test << k; // 2^k
// test=0; // 10000;
int_max_value_primary_long_computed = true;
max_value = test;
return test;
}
// bool int_max_value_primary_long_long_computed = false;
template <>
inline long long int_max_value_primary() {
static long long max_value;
if (int_max_value_primary_long_long_computed)
return max_value;
long long k = sizeof(long long) * 8 - 12; // number of bytes convetred to number of bits
long long test = 1;
test = test << k; // 2^k
#ifdef NMZ_EXTENDED_TESTS
if (test_linear_algebra_GMP)
test = 0;
#endif
max_value = test;
int_max_value_primary_long_long_computed = true;
return test;
}
//---------------------------------------------------------------------------
template <>
inline mpz_class int_max_value_primary<mpz_class>() {
assert(false);
return 0;
}
#ifdef ENFNORMALIZ
template <>
inline renf_elem_class int_max_value_primary<renf_elem_class>() {
assert(false);
return 0;
}
#endif
//---------------------------------------------------------------------------
template <typename Integer>
void check_range_list(const CandidateList<Integer>& ll) {
check_range_list(ll.Candidates);
}
//---------------------------------------------------------------------------
template <typename Integer>
void check_range_list(const std::list<Candidate<Integer> >& ll) {
if (using_GMP<Integer>())
return;
Integer test = int_max_value_dual<Integer>();
// cout << "test " << test << endl;
for (const auto& v : ll) {
for (size_t i = 0; i < v.values.size(); ++i)
if (Iabs(v.values[i]) >= test) {
// cout << v;
// cout << "i " << i << " " << Iabs(v[i]) << endl;
throw ArithmeticException("Vector entry out of range. Imminent danger of arithmetic overflow.");
}
}
}
//---------------------------------------------------------------------------
inline mpq_class dec_fraction_to_mpq(string s) {
size_t skip = 0; // skip leading spaces
for (; skip < s.size(); ++skip) {
if (!isspace(s[skip]))
break;
}
s = s.substr(skip);
mpz_class sign = 1;
if (s[0] == '+')
s = s.substr(1);
else if (s[0] == '-') {
s = s.substr(1);
sign = -1;
}
if (s[0] == '+' || s[0] == '-')
throw BadInputException("Error in decimal fraction " + s);
string int_string, frac_string, exp_string;
size_t frac_part_length = 0;
size_t pos_point = s.find(".");
size_t pos_E = s.find("e");
if (pos_point != string::npos) {
int_string = s.substr(0, pos_point);
if (pos_E != string::npos) {
frac_part_length = pos_E - (pos_point + 1);
}
else
frac_part_length = s.size() - (pos_point + 1);
frac_string = s.substr(pos_point + 1, frac_part_length);
if (frac_string[0] == '+' || frac_string[0] == '-')
throw BadInputException("Error in decimal fraction " + s);
}
else
int_string = s.substr(0, pos_E);
if (pos_E != string::npos)
exp_string = s.substr(pos_E + 1, s.size() - (pos_E + 1));
/* cout << "int " << int_string << endl;
cout << "frac " << frac_string << endl;
cout << "exp " << exp_string << endl; */
// remove leading 0 and +
if (int_string.size() > 0 && int_string[0] == '+')
int_string = int_string.substr(1);
while (int_string.size() > 0 && int_string[0] == '0')
int_string = int_string.substr(1);
while (frac_string.size() > 0 && frac_string[0] == '0')
frac_string = frac_string.substr(1);
if (exp_string.size() > 0 && exp_string[0] == '+')
exp_string = exp_string.substr(1);
bool exponent_could_be_zero = false;
while (exp_string.size() > 0 && exp_string[0] == '0') {
exponent_could_be_zero = true;
exp_string = exp_string.substr(1);
}
if (pos_E != string::npos && exp_string == "" && !exponent_could_be_zero)
throw BadInputException("No exponent following character e in floating point number");
mpq_class int_part, frac_part, exp_part;
if (!int_string.empty())
int_part = mpz_class(int_string);
if (pos_E == 0)
int_part = 1;
// cout << "int_part " << int_part << endl;
mpz_class den = 1;
if (!frac_string.empty()) {
frac_part = mpz_class(frac_string);
for (size_t i = 0; i < frac_part_length; ++i)
den *= 10;
}
// cout << "frac_part " << frac_part << endl;
mpq_class result = int_part;
if (frac_part != 0)
result += frac_part / den;
if (!exp_string.empty()) {
mpz_class expo(exp_string); // we take mpz_class because it has better error checking
// long expo=stol(exp_string);
mpz_class abs_expo = Iabs(expo);
mpz_class factor = 1;
for (long i = 0; i < abs_expo; ++i)
factor *= 10;
if (expo >= 0)
result *= factor;
else
result /= factor;
}
/* cout <<" result " << sign*result << endl;
cout << "==========" << endl; */
return sign * result;
}
//----------------------------------------------------------------------
// the next function produce an integer quotient and determine whether
// there is a remainder
inline bool int_quotient(long& Quot, const long& Num, const long& Den) {
Quot = Iabs(Num) / Iabs(Den);
return Quot * Iabs(Den) != Iabs(Num);
}
inline bool int_quotient(long long& Quot, const long long& Num, const long long& Den) {
Quot = Iabs(Num) / Iabs(Den);
return Quot * Iabs(Den) != Iabs(Num);
}
inline bool int_quotient(mpz_class& Quot, const mpz_class& Num, const mpz_class& Den) {
Quot = Iabs(Num) / Iabs(Den);
return Quot * Iabs(Den) != Iabs(Num);
}
inline bool int_quotient(long long& Quot, const mpz_class& Num, const mpz_class& Den) {
mpz_class mpz_Quot = (Iabs(Num) / Iabs(Den));
convert(Quot, mpz_Quot);
return mpz_Quot * Iabs(Den) != Iabs(Num);
}
template <typename IntegerRet>
inline bool int_quotient(IntegerRet& Quot, const nmz_float& Num, const nmz_float& Den) {
nmz_float FloatQuot = Iabs(Num) / Iabs(Den); // cout << "FF " << FloatQuot << endl;
nmz_float IntQuot = trunc(FloatQuot + nmz_epsilon); // cout << "II " << IntQuot << endl;
Quot = convertTo<IntegerRet>(IntQuot); // cout << "QQ " << Quot << endl;
return FloatQuot - IntQuot > nmz_epsilon;
}
template <typename IntegerRet, typename IntegerVal>
IntegerRet floor_quot(const IntegerVal Num, IntegerVal Den) {
IntegerRet Quot;
bool frac = int_quotient(Quot, Num, Den);
if ((Num >= 0 && Den >= 0) || (Num < 0 && Den < 0)) {
return Quot;
}
else {
if (frac) {
return -Quot - 1;
}
return -Quot;
}
}
template <typename IntegerRet, typename IntegerVal>
IntegerRet ceil_quot(const IntegerVal Num, IntegerVal Den) {
IntegerRet Quot;
bool frac = int_quotient(Quot, Num, Den);
if ((Num >= 0 && Den >= 0) || (Num < 0 && Den < 0)) {
if (frac)
return Quot + 1;
return Quot;
}
else {
return -Quot;
}
}
#ifdef ENFNORMALIZ
template <>
inline mpz_class floor_quot(const renf_elem_class Num, renf_elem_class Den) {
return floor(Num / Den);
}
template <>
inline mpz_class ceil_quot(const renf_elem_class Num, renf_elem_class Den) {
return ceil(Num / Den);
}
#endif
//----------------------------------------------------------------------
inline mpz_class floor(const mpq_class& q) {
mpz_class num = q.get_num();
mpz_class den = q.get_den();
mpz_class ent = num / den;
if (num < 0 && den * ent != num)
ent--;
return ent;
}
inline mpz_class ceil(const mpq_class& q) {
mpz_class num = q.get_num();
mpz_class den = q.get_den();
mpz_class ent = num / den;
if (num > 0 && den * ent != num)
ent++;
return ent;
}
inline mpz_class round(const mpq_class& q) {
mpq_class work;
if (q >= 0) {
work = q - mpq_class(1, 2);
return ceil(work);
}
work = q + mpq_class(1, 2);
return floor(work);
}
template <typename Integer>
mpz_class nmz_factorial(Integer n) {
assert(n >= 0);
mpz_class f = 1;
long nlong = convertTo<long>(n);
for (long i = 1; i <= nlong; ++i)
f *= i;
return f;
}
template <typename Integer>
mpz_class nmz_binomial(Integer n, Integer k) {
if (k > n)
return 0;
return nmz_factorial(n) / nmz_factorial(k);
}
inline nmz_float mpq_to_nmz_float(const mpq_class& val) {
mpz_class bound = 1;
for (size_t i = 0; i < 60; ++i)
bound *= 10;
mpz_class gmp_num = val.get_num(), gmp_den = val.get_den();
while (Iabs(gmp_num) > bound && Iabs(gmp_den) > bound) {
gmp_num /= 10;
gmp_den /= 10;
}
nmz_float num, den;
convert(num, gmp_num);
convert(den, gmp_den);
return num / den;
}
template <typename Integer>
long convertToLong(const Integer& val) {
long ret;
try {
ret = convertTo<long>(val);
} catch (const ArithmeticException& e) {
throw LongException(val);
}
return ret;
}
template <typename Integer>
long convertToLongLong(const Integer& val) {
long ret;
try {
ret = convertTo<long long>(val);
} catch (const ArithmeticException& e) {
throw LongLongException(val);
}
return ret;
}
} // namespace libnormaliz
//---------------------------------------------------------------------------
#endif /* INTEGER_H_ */
//---------------------------------------------------------------------------
|