/usr/include/polymake/next/numerical_functions.h is in libpolymake-dev-common 3.2r2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* Copyright (c) 1997-2018
Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
http://www.polymake.org
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 2, or (at your option) any
later version: http://www.gnu.org/licenses/gpl.txt.
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.
--------------------------------------------------------------------------------
*/
#ifndef POLYMAKE_NUMERICAL_FUNCTIONS_H
#define POLYMAKE_NUMERICAL_FUNCTIONS_H
#include "polymake/GenericStruct.h"
#include "polymake/internal/type_manip.h"
namespace pm {
template <typename TNum1, typename TNum2> inline
typename std::enable_if<(std::is_arithmetic<pure_type_t<TNum1>>::value &&
std::is_arithmetic<pure_type_t<TNum2>>::value), bool>::type
abs_equal(TNum1 x, TNum2 y)
{
return x==y || -x==y;
}
long gcd(long a, long b) noexcept;
inline
long lcm(long a, long b)
{
return (a/gcd(a,b))*b;
}
inline
long gcd(long a, int b)
{
return gcd(a, long(b));
}
inline
long gcd(int a, long b)
{
return gcd(long(a), b);
}
inline
long gcd(int a, int b)
{
return gcd(long(a), long(b));
}
inline
long lcm(long a, int b)
{
return lcm(a, long(b));
}
inline
long lcm(int a, long b)
{
return lcm(long(a), b);
}
inline
long lcm(int a, int b)
{
return lcm(long(a), long(b));
}
/// result of the extended gcd calculation for two numbers (a, b)
template <typename T>
struct ExtGCD : GenericStruct< ExtGCD<T> > {
DeclSTRUCT( DeclTemplFIELD(g, T) // g = gcd(a, b)
DeclTemplFIELD(p, T) // g == p * a + q * b
DeclTemplFIELD(q, T)
DeclTemplFIELD(k1, T) // a == k1 * g
DeclTemplFIELD(k2, T) // b == k2 * g
);
};
ExtGCD<long> ext_gcd(long a, long b) noexcept;
/// result of integer division of two numbers (a,b)
template <typename T>
struct Div : GenericStruct< Div<T> > {
DeclSTRUCT( DeclTemplFIELD(quot, T) // quotient=a/b
DeclTemplFIELD(rem, T) // remainder=a-quot*b
);
};
inline
Div<long> div(long a, long b) noexcept
{
Div<long> result;
result.quot=a/b;
result.rem=a%b;
return result;
}
inline
long div_exact(long a, long b) noexcept
{
return a/b;
}
#if defined(__GNUC__)
inline
int log2_floor(unsigned int x) noexcept
{
return sizeof(x)*8 -1 - __builtin_clz(x);
}
inline
int log2_floor(unsigned long x) noexcept
{
return sizeof(x)*8 -1 - __builtin_clzl(x);
}
inline
int log2_ceil(unsigned int x) noexcept
{
return x > 1 ? log2_floor(x-1)+1 : 0;
}
inline
int log2_ceil(unsigned long x) noexcept
{
return x > 1 ? log2_floor(x-1)+1 : 0;
}
#else // !GCC
int log2_round(unsigned long x, int round) noexcept;
inline int log2_floor(unsigned long x) { return log2_round(x,0); }
inline int log2_ceil(unsigned long x) { return log2_round(x,1); }
#endif
inline int log2_floor(int x) { return log2_floor((unsigned int)x); }
inline int log2_floor(long x) { return log2_floor((unsigned long)x); }
inline int log2_ceil(int x) { return log2_ceil((unsigned int)x); }
inline int log2_ceil(long x) { return log2_ceil((unsigned long)x); }
template <typename T>
T pow_impl(T base, T odd, int exp)
{
while (exp > 1) {
if (exp % 2 == 0) {
base = base * base;
exp = exp / 2;
} else {
odd = base * odd;
base = base * base;
exp = (exp - 1) / 2;
}
}
return base * odd;
}
template <typename T, typename std::enable_if<std::is_same<typename object_traits<T>::generic_tag,is_scalar>::value, int>::type=0>
T pow(const T& base, int exp)
{
auto one = one_value<T>();
if (exp < 0) {
return pow_impl<T>(one/base,one,abs(exp));
} else if (exp == 0) {
return one;
}
return pow_impl<T>(base,one,exp);
}
namespace operations {
template <typename Base, typename Exp, typename=typename std::enable_if<std::is_same<Exp,int>::value>::type>
struct pow_impl {
typedef Base first_argument_type;
typedef Exp second_argument_type;
typedef const Base result_type;
result_type operator() (typename function_argument<Base>::type a, Exp b) const {
return pm::pow(a,b);
}
};
template <typename Base, typename Exp>
struct pow : pow_impl<Base,Exp> {};
}
}
namespace polymake {
using pm::gcd;
using pm::ext_gcd;
using pm::ExtGCD;
using pm::lcm;
using pm::div;
using pm::Div;
using pm::div_exact;
using pm::log2_floor;
using pm::log2_ceil;
using pm::abs_equal;
using pm::pow;
namespace operations {
using pm::operations::pow;
}
}
#endif // POLYMAKE_NUMERICAL_FUNCTIONS_H
// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
|