/usr/include/linbox/algorithms/vector-fraction.h is in liblinbox-dev 1.3.2-1.1build2.
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 | /* linbox/algorithms/vector-fraction.h
* Copyright (C) 2004 David Pritchard
*
* Written by David Pritchard <daveagp@mit.edu>
*
* ========LICENCE========
* This file is part of the library LinBox.
*
* LinBox 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.1 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* ========LICENCE========
*/
#ifndef __LINBOX_vector_fraction_H
#define __LINBOX_vector_fraction_H
#include "linbox/linbox-config.h"
#include "linbox/util/debug.h"
#include <stdio.h>
#include "linbox/vector/vector-traits.h"
namespace LinBox
{
/** utility function to reduce a rational pair to lowest form */
template<class Domain>
void reduceIn(Domain& D, std::pair<typename Domain::Element, typename Domain::Element> &frac)
{
linbox_check(!D.isZero(frac.second));
if (D.isZero(frac.first)){
D.init(frac.second, 1);
return;
}
typename Domain::Element gcd;
D.gcd(gcd, frac.first, frac.second);
D.divin(frac.first, gcd);
D.divin(frac.second, gcd);
}
/** utility function to gcd-in a vector of elements over a domain */
//this could be replaced by a fancier version that combines elements linearly at random
template<class Domain, class Vector>
void vectorGcdIn(typename Domain::Element& result, Domain& D, Vector& v) {
for (typename Vector::iterator i = v.begin(); i != v.end(); i++)
D.gcdin(result, *i);
}
/** utility function, returns gcd of a vector of elements over a domain */
// this could be replaced by a fancier version that combines elements linearly at random
template<class Domain, class Vector>
typename Domain::Element vectorGcd(Domain& D, Vector& v) {
typename Domain::Element result;
D.init(result, 0);
vectorGcdIn(result, D, v);
return result;
}
/**
* \brief VectorFraction<Domain> is a vector of rational elements with common reduced denominator.
* Here Domain is a ring supporting the gcd, eg NTL_ZZ or PID_integer
* For compatability with the return type of rationalSolver, it allows conversion from/to
* std::vector<std::pair<Domain::Element> >.
* All functions will return the fraction in reduced form, calling reduce() if necessary.
*/
template<class Domain>
class VectorFraction{
public:
typedef typename Domain::Element Element;
typedef typename std::pair<Element, Element> Fraction;
typedef typename std::vector<Fraction> FVector;
typedef typename Vector<Domain>::Dense Vector;
Vector numer;
Element denom;
const Domain& _domain;
Element zero;
/**
* constructor from vector of rational numbers
* reduces individual pairs in-place first unless alreadyReduced=true
*/
VectorFraction(const Domain& D, FVector& frac
//,bool alreadyReduced = false
) :
_domain(D)
{
bool alreadyReduced = false;
typename FVector::iterator i;
D.init(zero, 0);
D.init(denom, 1);
if (!alreadyReduced)
for (i=frac.begin(); i!=frac.end(); i++)
reduceIn(D, *i);
for (i=frac.begin(); i!=frac.end(); i++) {
linbox_check(!D.isZero(i->second));
D.lcmin(denom, i->second);
}
numer = Vector(frac.size());
typename Vector::iterator j;
for (i=frac.begin(), j=numer.begin(); i!=frac.end(); i++, j++){
D.mul(*j, denom, i->first);
D.divin(*j, i->second);
}
}
/** allocating constructor, returns [0, 0, ... 0]/1 */
VectorFraction(const Domain& D, size_t n) :
_domain(D)
{
D.init(zero, 0);
D.init(denom, 1);
numer = Vector(n);
typename Vector::iterator j;
for (j=numer.begin(); j!=numer.end(); j++)
D.assign(*j, zero);
}
/** copy constructor */
VectorFraction(const VectorFraction<Domain>& VF) :
_domain(VF._domain)
{
copy(VF);
}
/** copy without construction */
void copy(const VectorFraction<Domain>& VF)
{
//assumes _domain = VF._domain
denom = VF.denom;
numer.resize(VF.numer.size());
typename Vector::iterator i;
typename Vector::const_iterator j;
for (i=numer.begin(), j=VF.numer.begin(); i!=numer.end(); i++, j++)
_domain.assign(*i, *j);
}
/** clear and resize without construction */
void clearAndResize(size_t size)
{
_domain.init(denom, 1);
typename Vector::iterator i;
numer.resize(size);
for (i=numer.begin(); i!=numer.end(); i++)
_domain.init(*i, 0);
}
/**
* Replaces *this with a linear combination of *this and other
* such that the result has denominator == gcd(this->denom, other.denom)
* see Mulders+Storjohann : 'Certified Dense Linear System Solving' Lemma 2.1
* return value of true means that there was some improvement (ie denom was reduced)
*/
bool combineSolution(const VectorFraction<Domain>& other)
{
if (_domain.isDivisor(other.denom, denom)) return false;
if (_domain.isDivisor(denom, other.denom)) {
denom = other.denom;
numer = other.numer;
return true;
}
Element s, t, g;
_domain.xgcd(g, s, t, denom, other.denom);
if (_domain.areEqual(g, denom)) ; //do nothing
else {
denom = g;
typename Vector::iterator it=numer.begin();
typename Vector::const_iterator io=other.numer.begin();
for (; it != numer.end(); it++, io++) {
_domain.mulin(*it, s);
_domain.axpyin(*it, t, *io);
}
return true;
}
return false;
}
/**
* Adds in-place to *this a multiple of other
* such that the result has gcd(denominator, denBound) == gcd(this->denom, other.denom, denBound)
* see Mulders+Storjohann : 'Certified Dense Linear System Solving' Lemma 6.1
* return value of true means that there was some improvement (ie gcd(denom, denBound) was reduced)
* g is gcd(denom, denBound), and is updated by this function when there is improvement
*/
bool boundedCombineSolution(const VectorFraction<Domain>& other, const Element& denBound, Element& g)
{
//this means that new solution won't reduce g
if (_domain.isDivisor(other.denom, g)) return false;
//short-circuit in case the new solution is completely better than old one
Element _dtmp;
if (_domain.isDivisor(g, _domain.gcd(_dtmp, denBound, other.denom))) {
denom = other.denom;
numer = other.numer;
g = _dtmp;
return true;
}
Element A, g2, lincomb;
_domain.gcd(g, other.denom, g); //we know this reduces g
// find A s.t. gcd(denBound, denom + A*other.denom) = g
// strategy: pick random values of A <= d(y_0)
integer tmp;
_domain.convert(tmp, denBound);
typename Domain::RandIter randiter(_domain, tmp); //seed omitted
// TODO: I don't think this random iterator has high-quality low order bits, which are needed
do {
randiter.random(A);
_domain.assign(lincomb, denom);
_domain.axpyin(lincomb, A, other.denom);
_domain.gcd(g2, lincomb, denBound);
}
while (!_domain.areEqual(g, g2));
_domain.assign(denom, lincomb);
typename Vector::iterator it=numer.begin();
typename Vector::const_iterator io=other.numer.begin();
for (; it != numer.end(); it++, io++)
_domain.axpyin(*it, A, *io);
return true;
}
/**
* Adds in-place to *this a multiple of other to create an improved certificate ("z")
* n1/d1 = *this . b, n2/d2 = other . b in reduced form
* n1/d1 are updated so that new denominator is lcm(d1, d2);
* see Mulders+Storjohann : 'Certified Dense Linear System Solving' Lemma 6.2
* return value of true means that there was some improvement (ie d1 was increased)
*/
bool combineCertificate(const VectorFraction<Domain>& other, Element& n1, Element& d1,
const Element& n2, const Element d2)
{
//this means that new solution won't reduce g
if (_domain.isDivisor(d1, d2)) return false;
//short-circuit in case the new solution is completely better than old one
if (_domain.isDivisor(d2, d1)) {
copy(other);
n1 = n2;
d1 = d2;
return true;
}
Element A, g, l, n1d2_g, n2d1_g, lincomb, g2, tmpe, one;
_domain.gcd(g, d1, d2); //compute gcd
_domain.mul(l, d1, d2);
_domain.divin(l, g); //compute lcm
_domain.div(n1d2_g, d2, g);
_domain.mulin(n1d2_g, n1); //compute n1.d2/g
_domain.div(n2d1_g, d1, g);
_domain.mulin(n2d1_g, n2); //compute n2.d1/g
// find A s.t. gcd(denBound, denom + A*other.denom) = g
// strategy: pick random values of A <= lcm(d(denom), d(other.denom))
integer tmp;
_domain.mul(tmpe, denom, other.denom);
_domain.convert(tmp, tmpe);
_domain.init(one, 1);
typename Domain::RandIter randiter(_domain, tmp); //seed omitted
// TODO: I don't think this random iterator has high-quality low order bits, which are needed
do {
randiter.random(A);
_domain.assign(lincomb, n1d2_g);
_domain.axpyin(lincomb, A, n2d1_g);
_domain.gcd(g2, lincomb, l);
}
while (!_domain.areEqual(one, g2));
this->axpyin(A, other);
_domain.lcmin(d1, d2);
return true;
}
/**
* this += a * x. performs a rational axpy with an integer multiplier
* returns (*this)
*/
VectorFraction<Domain>& axpyin(Element& a, const VectorFraction<Domain>& x)
{
Element a_prime, gcd_a_xdenom, xdenom_prime;
_domain.gcd(gcd_a_xdenom, a, x.denom);
_domain.div(a_prime, a, gcd_a_xdenom);
_domain.div(xdenom_prime, x.denom, gcd_a_xdenom);
Element cdf; //common denominator factor; multiply both sides by this and divide at end
_domain.gcd(cdf, denom, xdenom_prime);
_domain.divin(denom, cdf);
_domain.divin(xdenom_prime, cdf);
// we perform numer[i] = xdenom_prime * numer[i] + a_prime * denom * x.denom[i]
// so multiply denom into a_prime and save a multiplication on each entry
_domain.mulin(a_prime, denom);
typename Vector::iterator i = this->numer.begin();
typename Vector::const_iterator j = x.numer.begin();
for (; i != this->numer.end(); i++, j++) {
_domain.mulin(*i, xdenom_prime);
_domain.axpyin(*i, a_prime, *j);
}
_domain.mulin(denom, cdf);
_domain.mulin(denom, xdenom_prime);
simplify();
return *this;
}
/** write to a stream */
std::ostream& write(std::ostream& os) const
{
os << "[";
for (typename Vector::const_iterator it=numer.begin(); it != numer.end(); it++) {
if (it != numer.begin()) os << " ";
os << *it;
}
return os << "]/" << denom;
}
/** convert to 'answer' type of lifting container */
FVector& toFVector(FVector& result) const
{
linbox_check(numer.size()==result.size());
typename Vector::const_iterator it=numer.begin();
typename FVector::iterator ir=result.begin();
for (; it != numer.end(); it++, ir++) {
_domain.assign(ir->first, *it);
_domain.assign(ir->second, denom);
}
return result;
}
/** reduces to simplest form, returns (*this) */
VectorFraction<Domain>& simplify()
{
typename Vector::iterator i;
Element gcd;
_domain.init(gcd, denom);
vectorGcdIn(gcd, _domain, numer);
_domain.divin(denom, gcd);
for (i=numer.begin(); i!=numer.end(); i++)
_domain.divin(*i, gcd);
return (*this);
}
};
}
#endif //__LINBOX_vector_fraction_H
// vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,:0,t0,+0,=s
// Local Variables:
// mode: C++
// tab-width: 8
// indent-tabs-mode: nil
// c-basic-offset: 8
// End:
|