CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

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

Views: 418426
1
/*
2
* Normaliz
3
* Copyright (C) 2007-2014 Winfried Bruns, Bogdan Ichim, Christof Soeger
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 3 of the License, or
7
* (at your option) any later version.
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*
17
* As an exception, when this program is distributed through (i) the App Store
18
* by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or (iii) Google Play
19
* by Google Inc., then that store may impose any digital rights management,
20
* device limits and/or redistribution restrictions that are required by its
21
* terms of service.
22
*/
23
24
#ifndef INTEGER_H_
25
#define INTEGER_H_
26
27
#include <libnormaliz/general.h>
28
29
#include <list>
30
#include <vector>
31
#include <string>
32
#include <limits.h>
33
34
35
// Integer should (may) support:
36
// Integer abs(Integer); here implemented as Iabs
37
// Integer min(Integer, Integer); here we use the template min in <algorithm>
38
// It provides abs, gcd and lcm
39
//---------------------------------------------------------------------------
40
41
namespace libnormaliz {
42
using namespace std;
43
44
//---------------------------------------------------------------------------
45
// Basic functions
46
//---------------------------------------------------------------------------
47
48
// returns the absolute value of a
49
template<typename Integer> inline Integer Iabs(const Integer& a) {
50
return (a>=0) ? (a) : Integer(-a);
51
}
52
53
//returns gcd of a and b, if one is 0 returns the other integer
54
template<typename Integer> Integer gcd(const Integer& a, const Integer& b);
55
template<> mpz_class gcd<mpz_class>(const mpz_class& a, const mpz_class& b);
56
57
//returns lcm of a and b, returns 0 if one is 0
58
template<typename Integer> Integer lcm(const Integer& a, const Integer& b);
59
template<> mpz_class lcm(const mpz_class& a, const mpz_class& b);
60
61
// integer division a/b. Returns quot and rem = minimal remainder <= |b|/2
62
template<typename Integer>
63
void minimal_remainder(const Integer& a, const Integer&b, Integer& quot, Integer& rem);
64
65
// extended Euclidean algorithm: d=ua+vb
66
template <typename Integer>
67
Integer ext_gcd(const Integer& a, const Integer& b, Integer& u, Integer&v);
68
69
// minimizes u and v and makes d >= 0.
70
template <typename Integer>
71
void sign_adjust_and_minimize(const Integer& a, const Integer& b, Integer& d, Integer& u, Integer&v);
72
73
//---------------------------------------------------------------------------
74
// Conversions and checks
75
//---------------------------------------------------------------------------
76
77
// convert val to ret
78
// does the conversion and returns false if it fails
79
bool try_convert(long& ret, const long long& val);
80
inline bool try_convert(long long& ret, const long& val) {ret = val; return true;}
81
bool try_convert(long& ret, const mpz_class& val);
82
bool try_convert(long long& ret, const mpz_class& val);
83
inline bool try_convert(mpz_class& ret, const long& val) {ret = val; return true;}
84
bool try_convert(mpz_class& ret, const long long& val);
85
86
template<typename FromType>
87
bool try_convert(nmz_float& ret, const FromType& val);
88
//bool try_convert(nmz_float& ret, const long& val);
89
bool try_convert(nmz_float& ret, const mpz_class& val);
90
template<typename ToType>
91
bool try_convert(ToType& ret, const nmz_float& val);
92
// bool try_convert(long& ret, const nmz_float& val);
93
bool try_convert(mpz_class& ret, const nmz_float& val);
94
95
96
97
// template for same type "conversion"
98
template<typename Type>
99
inline bool try_convert(Type& ret, const Type& val) {ret = val; return true;}
100
101
102
bool fits_long_range(long long a);
103
104
template<typename Integer>
105
inline bool using_GMP() {
106
return false;
107
}
108
109
template<>
110
inline bool using_GMP<mpz_class>() {
111
return true;
112
}
113
114
template<typename Integer>
115
Integer int_max_value_dual();
116
117
template<typename Integer>
118
Integer int_max_value_primary();
119
120
//---------------------------------------------------------------------------
121
122
template<typename Integer>
123
inline bool check_range(const Integer& m) {
124
const Integer max_primary = int_max_value_primary<Integer>();
125
return (Iabs(m) <= max_primary);
126
}
127
128
template<>
129
inline bool check_range<mpz_class>(const mpz_class& m) {
130
return true;
131
}
132
133
template<>
134
inline bool check_range<nmz_float>(const nmz_float& m) {
135
return true;
136
}
137
138
//---------------------------------------------------------------------------
139
140
template<typename Integer>
141
void check_range_list(const std::list<std::vector<Integer> >& ll);
142
143
template<typename Integer> class CandidateList;
144
template<typename Integer> class Candidate;
145
146
template<typename Integer>
147
void check_range_list(const CandidateList<Integer>& ll);
148
template<typename Integer>
149
void check_range_list(const std::list<Candidate<Integer> >& ll);
150
151
152
153
//---------------------------------------------------------------------------
154
// Special functions
155
//---------------------------------------------------------------------------
156
157
//return the number of decimals, needed to write the Integer a
158
template<typename Integer> size_t decimal_length(Integer a);
159
160
//returns b!/a!
161
template<typename Integer> Integer permutations(const size_t& a, const size_t& b);
162
template<typename Integer> Integer permutations_modulo(const size_t& a, const size_t& b, long m);
163
164
//---------------------------------------------------------------------------
165
// String conversion functions
166
//---------------------------------------------------------------------------
167
168
// forward declaration to silence clang error:
169
// 'operator<<' should be declared prior to the call site or in the global namespace
170
template <typename T> std::ostream& operator<< (std::ostream& out, const vector<T>& vec);
171
172
template<typename Integer> string toString(Integer a) {
173
ostringstream ostream;
174
ostream << a;
175
return ostream.str();
176
}
177
template<> inline string toString(mpz_class a) {
178
return a.get_str();
179
}
180
template<> inline string toString(mpq_class a) {
181
return a.get_str();
182
}
183
184
// for the interpretation of a string as a decimal fraction or floating point number
185
mpq_class dec_fraction_to_mpq(string s);
186
187
//----------------------------------------------------------------------
188
// the next function produce an integer quotient and determine whether
189
// there is a remainder
190
191
bool int_quotient(long& Quot, const long long& Num, const long& Den);
192
bool int_quotient(long long& Quot, const long& Num, const long& Den);
193
bool int_quotient(mpz_class& Quot, const mpz_class& Num, const mpz_class& Den);
194
template<typename IntegerRet>
195
bool int_quotient(IntegerRet& Quot, const nmz_float& Num, const nmz_float& Den);
196
197
} // end libnormaliz
198
199
//---------------------------------------------------------------------------
200
#endif /* INTEGER_H_ */
201
//---------------------------------------------------------------------------
202
203