/usr/include/linbox/field/PID-integer.h is in liblinbox-dev 1.1.6~rc0-4.1.
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 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 | /* -*- mode: C++; tab-width:8; indent-tabs-mode: t; c-basic-offset:8 -*- */
/* linbox/field/PID-integer.h
* Copyright (C) 2004 Pascal Giorgi
*
* Written by :
* Pascal Giorgi pascal.giorgi@ens-lyon.fr
*
*
* This library is free software; 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; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __PID_INTEGER_H
#define __PID_INTEGER_H
#include <limits.h>
#include <iostream>
#include <linbox/integer.h>
#include <linbox/field/unparametric.h>
#include <linbox/field/field-traits.h>
namespace LinBox {
template <class Ring>
class ClassifyRing;
/// \ingroup ring
class PID_integer : public UnparametricField<integer>
{
public:
typedef integer Element;
inline Element& axpyin (integer &r, const integer& a, const integer& x) const {
return Integer::axpyin(r,a,x);
}
inline bool isUnit (const Element& x) const {
return (x == Element(1)) || (x== Element(-1));
}
inline Element& abs(Element& x, const Element& a) const {
x= (a>0)? a: -a;
return x;
}
inline Element abs(const Element& a) const {
return (a>0)? a: -a;
}
/** compare two elements, a and b
* return 1, if a > b
* return 0, if a = b;
* return -1. if a < b
*/
inline long compare (const Element& a, const Element& b) const {
return (a>b)? 1: ((a<b)? -1 : 0);
}
/** @brief gcd (g, a, b)
* return g = gcd (a, b)
*/
inline Element& gcd (Element& g, const Element& a, const Element& b) const {
return ::gcd(g,a,b);
}
/** @brief gcdin(g, b)
* return g = gcd (g, b)
*/
inline Element& gcdin (Element& g, const Element& b) const {
gcd(g, g, b);
return g;
}
/** @brief xgcd (g, s, t, a, b)
* g = gcd(a, b) = a*s + b*t.
* The coefficients s and t are defined according to the standard
* Euclidean algorithm applied to |a| and |b|, with the signs then
* adjusted according to the signs of a and b.
*/
inline Element& xgcd (Element& g, Element& s, Element& t, const Element& a, const Element& b) const {
return ::gcd(g,a,b,s,t);
}
/** @brief lcm (c, a, b)
* c = lcm (a, b)
*/
inline Element& lcm (Element& c, const Element& a, const Element& b) const {
if ((a==Element(0)) || (b==Element(0))) return c = Element(0);
else {
Element g;
gcd (g, a, b);
c= a*b;
c /= g;
c=abs (c);
return c;
}
}
/** @brief lcmin (l, b)
* l = lcm (l, b)
*/
inline Element& lcmin (Element& l, const Element& b) const {
if ((l==Element(0)) || (b==Element(0))) return l = Element(0);
else {
Element g;
gcd (g, l, b);
l*= b;
l/= g;
l=abs (l);
return l;
}
}
inline void reconstructRational (Element& a, Element& b, const Element& x, const Element& m) const {
RationalReconstruction(a,b, x, m, ::sqrt(m), true, true);
}
inline void reconstructRational (Element& a, Element& b, const Element& x, const Element& m, const Element& bound) const {
RationalReconstruction(a,b, x, m, bound, true, true);
}
inline long reconstructRational (Element& a, Element& b,
const Element& x, const Element& m,
const Element& a_bound, const Element& b_bound) const {
Element bound = x/b_bound;
// if (bound>a_bound) std::cerr << "a_bound: " << a_bound << ", x/b_bound: " << bound << std::endl;
RationalReconstruction(a,b,x,m, (bound>a_bound?bound:a_bound), true, false);
return (b > b_bound)? 0: 1;
}
/** @brief quo (q, x, y)
* q = floor (x/y);
*/
inline Element& quo (Element& q, const Element& a, const Element& b) const {
return q = a/b;
}
/** @brief rem (r, a, b)
* r = remindar of a / b
*/
inline Element& rem (Element& r, const Element& a, const Element& b) const {
return Integer::mod(r,a,b);
}
/** @brief quoin (a, b)
* a = quotient (a, b)
*/
inline Element& quoin (Element& a, const Element& b) const {
return quo(a,a,b);
}
/** @brief quoin (a, b)
* a = quotient (a, b)
*/
inline Element& remin (Element& a, const Element& b) const {
return rem(a,a,b);
}
/** @brief quoRem (q, r, a, b)
* q = [a/b], r = a - b*q
* |r| < |b|, and if r != 0, sign(r) = sign(b)
*/
inline void quoRem (Element& q, Element& r, const Element& a, const Element& b) const {
quo(q,a,b);
r = a - q*b;
}
/** @brief isDivisor (a, b)
* Test if b | a.
*/
inline bool isDivisor (const Element& a, const Element& b) const {
Element r;
return rem(r,a,b)==Element(0);
}
/** @brief sqrt(x,y)
* x=floor(sqrt(y))
*/
inline Element& sqrt(Element& x, const Element& y) const {
return ::sqrt(x,y);
}
inline Element powtwo(Element& z, const Element& x) const {
z = 1;
//cout << "max" << ULONG_MAX << "x" << x << "?" << (x < ULONG_MAX);
if (x < LONG_MAX) {
z<<=(long int)x;
//cout << "z"<< z;
return z;
} else {
Element n,m;
quoRem(n,m,x,(Element)(LONG_MAX-1));
for (int i=0; i < n; ++i) {
z <<=(long int)(LONG_MAX-1);
}
z <= (long int)m;
return z;
}
//for (Element i=0; i < x; ++i) {
// z <<= 1;
//}
return z;
}
inline Element logtwo(Element& z, const Element& x) const {
//cout << "x" << x;
if (x<1) return z=-1;
z = 0;
Element cur = x;
cur >>=1;//cout << "cur" << cur;
while (cur > 0) {
//cout << "cur" << cur;
++z;
cur >>=1;
}
//cout << "z" << z;
return z;
}
// some specializations and conversions
inline double& convert(double& x, const Element& y) const
{ return x= (double)y;}
inline Element& init(Element& x, const double& y) const
{ return x=Element(y);}
inline integer& convert(integer& x, const Element& y) const
{ return x=y;}
inline Element& init(Element& x, const integer& y = 0) const
{ return x=y;}
protected:
// Rational number reconstruction:
// num/den \equiv f modulo m, with |num|<k and 0 < |den| \leq f/k
// See [von zur Gathen & Gerhard, Modern Computer Algebra,
// 5.10, Cambridge Univ. Press 1999]
inline void RationalReconstruction( Element& a, Element& b,
const Element& f, const Element& m,
const Element& k,
bool reduce, bool recursive ) const {
Element x(f);
if (x<0) {
if ((-x)>m)
x %= m;
if (x<0)
x += m;
} else {
if (x>m)
x %= m;
}
if (x == 0) {
a = 0;
b = 1;
} else {
bool res = ratrecon(a,b,x,m,k, reduce, recursive);
if (recursive)
for( Element newk = k + 1; (!res) && (newk<f) ; ++newk)
res = ratrecon(a,b,x,m,newk,reduce, true);
}
}
// Precondition f is suppposed strictly positive and strictly less than m
inline bool ratrecon( Element& num, Element& den,
const Element& f, const Element& m,
const Element& k,
bool reduce, bool recursive ) const {
//std::cerr << "RatRecon : " << f << " " << m << " " << k << std::endl;
Element r0, t0, q, u;
r0=m;
t0=0;
num=f;
den=1;
while(num>=k)
{
q = r0;
q /= num; // r0/num
u = num;
num = r0; // num <-- r0
r0 = u; // r0 <-- num
// u *= q;
// num -= u; // num <-- r0-q*num
Integer::axmyin(num,u,q);
if (num == 0) return false;
u = den;
den = t0; // num <-- r0
t0 = u; // r0 <-- num
// u *= q;
// den -= u; // num <-- r0-q*num
Integer::axmyin(den,u,q);
}
// if (den < 0) {
// Integer::negin(num);
// Integer::negin(den);
// }
if (reduce) {
// [GG, MCA, 1999] Theorem 5.26
// (i)
// if (den < 0) {
// Integer::negin(num);
// Integer::negin(den);
// }
// (ii)
Element gg;
if (gcd(gg,num,den) != 1) {
Element ganum, gar2;
for( q = 1, ganum = r0-num, gar2 = r0 ; (ganum >= k) || (gar2<k); ++q ) {
ganum -= num;
gar2 -= num;
}
// r0 -= q * num;
// t0 -= q * den;
Integer::axmyin(r0,q,num);
Integer::axmyin(t0,q,den);
if (t0 < 0) {
num = -r0;
den = -t0;
} else {
num = r0;
den = t0;
}
// if (t0 > m/k) {
if (den > m/k) {
if (!recursive)
std::cerr
<< "*** Error *** No rational reconstruction of "
<< f
<< " modulo "
<< m
<< " with denominator <= "
<< (m/k)
<< std::endl;
}
if (gcd(gg,num,den) != 1) {
if (!recursive)
std::cerr
<< "*** Error *** There exists no rational reconstruction of "
<< f
<< " modulo "
<< m
<< " with |numerator| < "
<< k
<< std::endl
<< "*** Error *** But "
<< num
<< " = "
<< den
<< " * "
<< f
<< " modulo "
<< m
<< std::endl;
return false;
}
}
}
// (i)
if (den < 0) {
Integer::negin(num);
Integer::negin(den);
}
// std::cerr << "RatRecon End " << num << "/" << den << std::endl;
return true;
}
}; //end of class PID_integer
template<>
struct ClassifyRing<PID_integer> {
typedef RingCategories::IntegerTag categoryTag;
};
template<>
inline std::ostream &UnparametricField<integer>::write (std::ostream &os) const
{ return os << "unparam<integer>"; }
/*
// Specialization for Homomorphism
template <class _Target>
class Hom<PID_integer, _Target> {
public:
typedef PID_integer Source;
typedef _Target Target;
typedef typename Source::Element SrcElt;
typedef typename Target::Element Elt;
Hom(const Source& S, const Target& T) : _source (S), _target(T){}
Elt& image(Elt& t, const SrcElt& s) {
if (s.bitsize() > 52 )
_target.init(t,s);
else
_target.init(t, (double)s);
return t;
}
SrcElt& preimage(SrcElt& s, const Elt& t) {
_source.convert(s,t);
return s;
}
const Source& source() { return _source;}
const Target& target() { return _target;}
protected:
double tmp;
Source _source;
Target _target;
};
*/
} //end of namespace LinBox
#endif
|