/usr/include/givaro/givpoly1muldiv.inl is in libgivaro-dev 3.2.13-1.2.
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 | // ==========================================================================
// $Source: /var/lib/cvs/Givaro/src/library/poly1/givpoly1muldiv.inl,v $
// Copyright(c)'94-97 by Givaro Team
// see the copyright file.
// Authors: T. Gautier
// $Id: givpoly1muldiv.inl,v 1.6 2007-01-11 18:42:51 jgdumas Exp $
// ==========================================================================
#include "givaro/givpower.h"
#include "givaro/giverror.h"
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::shiftin ( Rep& R, int s) const
{
Type_t zer;
R.insert(R.begin(), s, this->_domain.init(zer,0) );
return R;
}
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::shift ( Rep& R, const Rep& a, int s) const
{
R = a;
return R.shiftin(R, s);
}
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::mulin( Rep& R, const Type_t& u ) const
{
for(typename Rep::iterator ri = R.begin();ri!=R.end();++ri)
_domain.mulin(*ri, u);
return R;
// return _supportdomain.mulin(R,u);
}
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::mulin( Rep& R, const Rep& P ) const
{
size_t sR = R.size();
size_t sP = P.size();
Rep tmp(sR+sP);
mul(tmp, R, P);
// R.logcopy(tmp);
// return R;
return assign(R,tmp);
}
// template <class Domain>
// Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::mul( Rep& R, const Rep& P, const Rep& Q ) const
// {
// size_t sR = R.size();
// size_t sP = P.size();
// size_t sQ = Q.size();
// if ((sQ ==0) || (sP ==0)) { R.reallocate(0); return R; }
// if (sR != sQ+sP) R.reallocate(sR = sP+sQ-1);
// size_t i,j;
// for (i=0; i<sR; ++i) _domain.init(R[i], _domain.zero);
// for (i=0; i<sP; ++i)
// if (! _domain.isZero(P[i]))
// for (j=0; j<sQ; ++j)
// _domain.axpy(R[i+j], P[i], Q[j], R[i+j]);
// return setdegree(R);
// }
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::mul( Rep& R, const Rep& P, const Rep& Q ) const
{
Type_t _zero;
_domain.init( _zero, 0.0);
size_t sR = R.size();
size_t sP = P.size();
size_t sQ = Q.size();
if ((sQ ==0) || (sP ==0)) { R.reallocate(0); return R; }
if (sR != sQ+sP) R.reallocate(sR = sP+sQ-1);
typename Rep::const_iterator ai=P.begin(),bi=Q.begin();
typename Rep::iterator ri=R.begin(), rig=R.begin();
if (_domain.isZero(*ai))
for(;bi!=Q.end();++bi,++ri)
*ri = _zero;
else
for(;bi!=Q.end();++bi,++ri)
if (_domain.isZero(*bi))
*ri = _zero;
else
_domain.mul(*ri,*ai,*bi);
for(;ri!=R.end();++ri)
*ri = _zero;
for(++ai,++rig;ai!=P.end();++ai,++rig)
if (! _domain.isZero(*ai))
for(ri=rig,bi=Q.begin();bi!=Q.end();++bi,++ri)
_domain.axpyin(*ri,*ai,*bi);
return setdegree(R);
}
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::mul
( Rep& R, const Rep& P, const Type_t& val ) const
{
typename Rep::const_iterator ip = P.begin();
R.resize(P.size());
for(typename Rep::iterator ir = R.begin(); ir != R.end(); ++ir, ++ip)
this->_domain.mul(*ir, *ip, val);
return R;
}
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::mul
( Rep& R, const Type_t& val, const Rep& P ) const
{
return this->mul(R,P,val);
}
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::divin(Rep& R, const Type_t& u) const
{
#ifdef GIVARO_DEBUG
if (_domain.isZero(u)) GivError::throw_error(GivMathDivZero("[Poly1Dom<D>::divin]"));
#endif
size_t sz =R.size();
for (unsigned int i=0; i<sz; ++i)
_domain.divin(R[i],u);
return setdegree(R);
}
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::div(Rep& R, const Rep& P, const Type_t& u) const
{
#ifdef GIVARO_DEBUG
if (_domain.isZero(u)) GivError::throw_error(GivMathDivZero("[Poly1Dom<D>::div]"));
#endif
size_t sP =P.size();
R.reallocate(sP);
for (unsigned int i=0; i<sP; ++i)
_domain.div(R[i],P[i],u);
return setdegree(R);
}
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::div(Rep& R, const Type_t& u, const Rep& P) const
{
#ifdef GIVARO_DEBUG
if (isZero(P)) GivError::throw_error(GivMathDivZero("[Poly1Dom<D>::divin]"));
#endif
if (_domain.isZero(u)) { return assign(R,zero);}
size_t sP =P.size();
if (sP >1) { R.reallocate(0); return R; }
size_t sR =R.size();
if (sR !=1) R.reallocate(1);
_domain.div(R[0], u, P[0]);
return setdegree(R);
}
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::div(Rep& Q, const Rep& A, const Rep& B) const
{
Rep R;
return divmod(Q,R,A,B);
}
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::divin(Rep& Q, const Rep& A) const
{
Rep R, B;
divmod(B,R,Q,A);
return assign(Q,B);
}
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::modin(Rep& R, const Type_t& u) const
{
#ifdef GIVARO_DEBUG
if (_domain.isZero(u)) GivError::throw_error(GivMathDivZero("[Poly1Dom<D>::modin]"));
#endif
R.reallocate(0);
return R;
}
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::mod(Rep& R, const Rep& P, const Type_t& u) const
{
#ifdef GIVARO_DEBUG
if (_domain.isZero(u)) GivError::throw_error(GivMathDivZero("[Poly1Dom<D>::mod]"));
#endif
R.reallocate(0);
return R;
}
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::mod(Rep& R, const Type_t& u, const Rep& P) const
{
#ifdef GIVARO_DEBUG
if (isZero(P)) GivError::throw_error(GivMathDivZero("[Poly1Dom<D>::mod]"));
#endif
if (_domain.isZero(u)) { return assign(P,R); }
size_t sP =P.size();
if (sP >1) {
R.reallocate(1);
_domain.assign(R[0], u);
return R;
}
R.reallocate(1);
_domain.mod(R[0],u,P[0]);
return R;
}
// template <class Domain>
// inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::modin(Rep& R, const Rep& A) const {
// Rep tR; assign(tR,R);
// return mod(R,tR,A);
// }
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::modin(Rep& A, const Rep& B) const {
// In place remainder
// A is written with next remainder in
// the division algorithm written at the end.
// Last step is erasing of the first values.
// write(std::cerr << "Rem(", A) << " ,";
// write(std::cerr, B) << ", X) mod " << _domain.size();
Type_t _zero;
_domain.init( _zero, 0.0);
long i = A.size()-B.size();
if (i >= 0) {
typedef typename Rep::value_type TT;
TT l;
typename Rep::reverse_iterator ai,aai;
typename Rep::const_reverse_iterator bi;
for (; i>=0; --i) {
ai = A.rbegin();
bi = B.rbegin();
_domain.div(l,*ai,*bi);
aai = A.rbegin();
for(++bi,++ai;bi!=B.rend();++bi,++ai,--i) {
_domain.amxy(*aai,l,*bi,*ai);
if (! _domain.isZero(*aai)) break;
}
if (bi!=B.rend())
for(++bi,++ai,++aai;bi!=B.rend();++bi,++ai,++aai)
_domain.amxy(*aai,l,*bi,*ai);
for(;ai!=A.rend();++ai,++aai)
*aai = *ai;
*aai = _zero;
}
// write(std::cerr << " = ", A) << ";" << std::endl;
A.erase(A.begin(), A.begin()+(A.size()-B.size()-i));
}
// write(std::cerr << " = ", setdegree(A)) << ";" << std::endl;
return setdegree(A);
}
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::mod(Rep& R, const Rep& A, const Rep& B) const
{
Rep Q;
// write(std::cerr, A) << " = (";
// write(std::cerr, B) << ") * (";
divmod(Q,R,A,B);
// write(std::cerr, Q) << ") + (";
// write(std::cerr, R) << ");" << std::endl;
return R;
}
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::divmod( Rep& Q, Rep& R, const Rep& A, const Rep& B) const
// returns Q such that A = B Q + R
{
Degree degB; degree(degB, B);
Type_t _zero;
_domain.init( _zero, 0.0);
#ifdef GIVARO_DEBUG
if (degB == Degree::deginfty)
GivError::throw_error(GivMathDivZero("[Poly1Dom<D>::div]"));
#endif
Degree degA; degree(degA, A);
if (degA == Degree::deginfty) {
assign(R, zero);
return assign(Q, zero);
}
if (degB == 0) // cste
{
assign(R, zero);
return div(Q, A, B[0]);
}
// JGD 15.12.1999 :
// if (degA ==0)
// {
// assign(R, zero);
// return assign(Q, zero);
// }
if (degB > degA) {
assign(R, A);
return assign(Q, zero);
}
long degQuo = value(degA-degB);
long degRem = value(degA);
Q.reallocate(degQuo+1);
assign(R,A);
Type_t tmp;
long i,j;
for (i=degQuo; i>=0; --i)
{
// == ld X^ (degRem-degQ)
_domain.div(Q[degQuo], R[degRem], B[degB.value()]);
_domain.neg(tmp, Q[degQuo]);
for (j=0; degB>j; j++) { // rem <- rem - ld*x^(degRem-degB)*B
_domain.axpyin(R[j+degQuo], tmp, B[j]);
}
_domain.assign(R[degRem],_zero) ; degQuo--; degRem--;
}
R.reallocate(degRem+1);
setdegree(R);
return setdegree(Q);
}
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::pdivmod
( Rep& Q, Rep& R, Type_t& m, const Rep& A, const Rep& B) const
// returns Q ...
{
Type_t _zero, _one;
_domain.init( _one, 1.0);
_domain.init( _zero, 0.0);
Degree degB; degree(degB, B);
#ifdef GIVARO_DEBUG
if (degB == Degree::deginfty)
GivError::throw_error(GivMathDivZero("[Poly1Dom<D>::div]"));
#endif
Degree degA; degree(degA, A);
if (degA == Degree::deginfty) {
assign(R, zero);
_domain.assign(m, _domain.one);
return assign(Q, zero);
}
if (degB == 0) // cste
{
assign(R, zero);
_domain.assign(m, B[0]);
return assign(Q, A);
}
if (degA ==0)
{
assign(R, zero);
_domain.assign(m, _domain.one);
return assign(Q, zero);
}
if (degB > degA) {
assign(R, A);
_domain.assign(m, _domain.one);
return assign(Q, zero);
}
long degQuo = value(degA-degB);
long degRem = value(degA);
Q.reallocate(degQuo+1);
assign(R,A);
Type_t tmp, lB;
_domain.assign(lB, B[degB.value()]);
_domain.assign(m, _domain.one);
long i,j;
for (i=degQuo; i>=0; --i)
{
// == ld X^ (degRem-degQ)
_domain.assign(Q[degQuo], R[degRem]);
// rem <- lB*rem - lQ*x^(degRem-degB)*B
for (j=0; j<degQuo; j++)
_domain.mulin (R[j], lB);
for (j=0; degB>j; j++)
{
_domain.mulin(R[j+degQuo], lB);
_domain.axmyin(R[j+degQuo], Q[degQuo], B[j]);
}
_domain.assign(R[degRem],_zero); degQuo--; degRem--;
_domain.mulin(m, lB);
}
R.reallocate(degRem+1);
setdegree(R);
return setdegree(Q);
// Poly1Dom<Domain,Dense>::Rep U,V;
// assign(U,A);
// mulin(U,m);
// write(std::cout << "m*A:", U) << std::endl;
// mul(U,Q,B);
// write(std::cout << "Q*B:", U) << std::endl;
}
template <class Domain>
inline typename Poly1Dom<Domain,Dense>::Rep& Poly1Dom<Domain,Dense>::pmod
( Rep& R, Type_t& m, const Rep& A, const Rep& B) const
{
Degree degB; degree(degB, B);
#ifdef GIVARO_DEBUG
if (degB == Degree::deginfty)
GivError::throw_error(GivMathDivZero("[Poly1Dom<D>::div]"));
#endif
Degree degA; degree(degA, A);
if (degA == Degree::deginfty) {
_domain.assign(m, _domain.one);
return assign(R, zero);
}
if (degB == 0) // cste
{
_domain.assign(m, B[0]);
return assign(R, zero);
}
if (degA ==0)
{
_domain.assign(m, _domain.one);
return assign(R, zero);
}
if (degB > degA) {
_domain.assign(m, _domain.one);
return assign(R, A);
}
Degree degR = degA;
assign(R,A);
Type_t tmp, lB;
_domain.assign(lB, B[degB.value()]);
long i,j;
//write(std::cout << "B:", B) << std::endl;
//_domain.write(std::cout << "lB:", lB) << "^" << degA-degB+1 << std::endl;
// _domain.pow(m, lB, degA.value()-degB.value()+1);
dom_power(m, lB, degA.value()-degB.value()+1,_domain);
//_domain.write(std::cout << "m:", m) << std::endl;
for (; degB<= degR; )
{
long d = degR.value()-degB.value();
// R <- lB*R - lR*x^(degR-degB)*B
for (j=0; degB>j; j++)
{
_domain.mulin (R[j+d], lB);
_domain.axmyin(R[j+d], R[degR.value()], B[j]);
}
for (j=0; j<d; ++j)
_domain.mulin (R[j], lB);
_domain.assign(R[degR.value()],_domain.zero);
degree(degR, R);
}
R.reallocate(degR.value()+1);
return setdegree(R);
}
|