Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
Project: cocalc-sagemath-dev-slelievre
Path: gap4r8 / pkg / NormalizInterface-1.0.2 / Normaliz.git / DST / include / libnormaliz / integer.h
Views: 418426/*1* Normaliz2* Copyright (C) 2007-2014 Winfried Bruns, Bogdan Ichim, Christof Soeger3* This program is free software: you can redistribute it and/or modify4* it under the terms of the GNU General Public License as published by5* the Free Software Foundation, either version 3 of the License, or6* (at your option) any later version.7*8* This program is distributed in the hope that it will be useful,9* but WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11* GNU General Public License for more details.12*13* You should have received a copy of the GNU General Public License14* along with this program. If not, see <http://www.gnu.org/licenses/>.15*16* As an exception, when this program is distributed through (i) the App Store17* by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or (iii) Google Play18* by Google Inc., then that store may impose any digital rights management,19* device limits and/or redistribution restrictions that are required by its20* terms of service.21*/2223#ifndef INTEGER_H_24#define INTEGER_H_2526#include <libnormaliz/general.h>2728#include <list>29#include <vector>30#include <string>31#include <limits.h>323334// Integer should (may) support:35// Integer abs(Integer); here implemented as Iabs36// Integer min(Integer, Integer); here we use the template min in <algorithm>37// It provides abs, gcd and lcm38//---------------------------------------------------------------------------3940namespace libnormaliz {41using namespace std;4243//---------------------------------------------------------------------------44// Basic functions45//---------------------------------------------------------------------------4647// returns the absolute value of a48template<typename Integer> inline Integer Iabs(const Integer& a) {49return (a>=0) ? (a) : Integer(-a);50}5152//returns gcd of a and b, if one is 0 returns the other integer53template<typename Integer> Integer gcd(const Integer& a, const Integer& b);54template<> mpz_class gcd<mpz_class>(const mpz_class& a, const mpz_class& b);5556//returns lcm of a and b, returns 0 if one is 057template<typename Integer> Integer lcm(const Integer& a, const Integer& b);58template<> mpz_class lcm(const mpz_class& a, const mpz_class& b);5960// integer division a/b. Returns quot and rem = minimal remainder <= |b|/261template<typename Integer>62void minimal_remainder(const Integer& a, const Integer&b, Integer& quot, Integer& rem);6364// extended Euclidean algorithm: d=ua+vb65template <typename Integer>66Integer ext_gcd(const Integer& a, const Integer& b, Integer& u, Integer&v);6768// minimizes u and v and makes d >= 0.69template <typename Integer>70void sign_adjust_and_minimize(const Integer& a, const Integer& b, Integer& d, Integer& u, Integer&v);7172//---------------------------------------------------------------------------73// Conversions and checks74//---------------------------------------------------------------------------7576// convert val to ret77// does the conversion and returns false if it fails78bool try_convert(long& ret, const long long& val);79inline bool try_convert(long long& ret, const long& val) {ret = val; return true;}80bool try_convert(long& ret, const mpz_class& val);81bool try_convert(long long& ret, const mpz_class& val);82inline bool try_convert(mpz_class& ret, const long& val) {ret = val; return true;}83bool try_convert(mpz_class& ret, const long long& val);8485template<typename FromType>86bool try_convert(nmz_float& ret, const FromType& val);87//bool try_convert(nmz_float& ret, const long& val);88bool try_convert(nmz_float& ret, const mpz_class& val);89template<typename ToType>90bool try_convert(ToType& ret, const nmz_float& val);91// bool try_convert(long& ret, const nmz_float& val);92bool try_convert(mpz_class& ret, const nmz_float& val);93949596// template for same type "conversion"97template<typename Type>98inline bool try_convert(Type& ret, const Type& val) {ret = val; return true;}99100101bool fits_long_range(long long a);102103template<typename Integer>104inline bool using_GMP() {105return false;106}107108template<>109inline bool using_GMP<mpz_class>() {110return true;111}112113template<typename Integer>114Integer int_max_value_dual();115116template<typename Integer>117Integer int_max_value_primary();118119//---------------------------------------------------------------------------120121template<typename Integer>122inline bool check_range(const Integer& m) {123const Integer max_primary = int_max_value_primary<Integer>();124return (Iabs(m) <= max_primary);125}126127template<>128inline bool check_range<mpz_class>(const mpz_class& m) {129return true;130}131132template<>133inline bool check_range<nmz_float>(const nmz_float& m) {134return true;135}136137//---------------------------------------------------------------------------138139template<typename Integer>140void check_range_list(const std::list<std::vector<Integer> >& ll);141142template<typename Integer> class CandidateList;143template<typename Integer> class Candidate;144145template<typename Integer>146void check_range_list(const CandidateList<Integer>& ll);147template<typename Integer>148void check_range_list(const std::list<Candidate<Integer> >& ll);149150151152//---------------------------------------------------------------------------153// Special functions154//---------------------------------------------------------------------------155156//return the number of decimals, needed to write the Integer a157template<typename Integer> size_t decimal_length(Integer a);158159//returns b!/a!160template<typename Integer> Integer permutations(const size_t& a, const size_t& b);161template<typename Integer> Integer permutations_modulo(const size_t& a, const size_t& b, long m);162163//---------------------------------------------------------------------------164// String conversion functions165//---------------------------------------------------------------------------166167// forward declaration to silence clang error:168// 'operator<<' should be declared prior to the call site or in the global namespace169template <typename T> std::ostream& operator<< (std::ostream& out, const vector<T>& vec);170171template<typename Integer> string toString(Integer a) {172ostringstream ostream;173ostream << a;174return ostream.str();175}176template<> inline string toString(mpz_class a) {177return a.get_str();178}179template<> inline string toString(mpq_class a) {180return a.get_str();181}182183// for the interpretation of a string as a decimal fraction or floating point number184mpq_class dec_fraction_to_mpq(string s);185186//----------------------------------------------------------------------187// the next function produce an integer quotient and determine whether188// there is a remainder189190bool int_quotient(long& Quot, const long long& Num, const long& Den);191bool int_quotient(long long& Quot, const long& Num, const long& Den);192bool int_quotient(mpz_class& Quot, const mpz_class& Num, const mpz_class& Den);193template<typename IntegerRet>194bool int_quotient(IntegerRet& Quot, const nmz_float& Num, const nmz_float& Den);195196} // end libnormaliz197198//---------------------------------------------------------------------------199#endif /* INTEGER_H_ */200//---------------------------------------------------------------------------201202203