CatgirlIntelligenceAgency/run/test-data/url-343223418

17 lines
18 KiB
Plaintext

<html>
<head></head>
<body>
/**************************************************************************\ MODULE: ZZX SUMMARY: The class ZZX implements polynomials in ZZ[X], i.e., univariate polynomials with integer coefficients. Polynomial multiplication is implemented using one of 4 different algorithms: 1) classical 2) Karatsuba 3) Schoenhage &amp; Strassen --- performs an FFT by working modulo a "Fermat number" of appropriate size... good for polynomials with huge coefficients and moderate degree 4) CRT/FFT --- performs an FFT by working modulo several small primes...good for polynomials with moderate coefficients and huge degree. The choice of algorithm is somewhat heuristic, and may not always be perfect. Many thanks to Juergen Gerhard <jngerhar@plato.uni-paderborn.de>
for pointing out the deficiency in the NTL-1.0 ZZX arithmetic, and for contributing the Schoenhage/Strassen code. Extensive use is made of modular algorithms to enhance performance (e.g., the GCD algorithm and amny others). \**************************************************************************/ #include
<ntl vec_zz.h>
#include "zz_pX.h" #include
<ntl zz_px.h>
class ZZX { public: ZZX(); // initial value 0 ZZX(const ZZX&amp; a); // copy explicit ZZX(const ZZ&amp; a); // promotion explicit ZZX(long a); // promotion ~ZZX(); ZZX(ZZX&amp;&amp; a); // move constructor (C++11 only) // declared noexcept unless NTL_EXCEPTIONS flag is set ZZX&amp; operator=(ZZX&amp;&amp; a); // move assignment (C++11 only) // declared noexcept unless NTL_EXCEPTIONS flag is set ZZX(INIT_MONO_TYPE, long i, const ZZ&amp; c); ZZX(INIT_MONO_TYPE, long i, long c); // initial value c*X^i, invoke as ZZX(INIT_MONO, i, c) ZZX(INIT_MONO_TYPE, long i); // initial value X^i, invoke as ZZX(INIT_MONO, i) ZZX&amp; operator=(const ZZX&amp; a); // assignment ZZX&amp; operator=(const ZZ&amp; a); ZZX&amp; operator=(long a); typedef ZZ coeff_type; // ... }; /**************************************************************************\ Accessing coefficients The degree of a polynomial f is obtained as deg(f), where the zero polynomial, by definition, has degree -1. A polynomial f is represented as a coefficient vector. Coefficients may be accesses in one of two ways. The safe, high-level method is to call the function coeff(f, i) to get the coefficient of X^i in the polynomial f, and to call the function SetCoeff(f, i, a) to set the coefficient of X^i in f to the scalar a. One can also access the coefficients more directly via a lower level interface. The coefficient of X^i in f may be accessed using subscript notation f[i]. In addition, one may write f.SetLength(n) to set the length of the underlying coefficient vector to n, and f.SetMaxLength(n) to allocate space for n coefficients, without changing the coefficient vector itself. After setting coefficients using this low-level interface, one must ensure that leading zeros in the coefficient vector are stripped afterwards by calling the function f.normalize(). NOTE: the coefficient vector of f may also be accessed directly as f.rep; however, this is not recommended. Also, for a properly normalized polynomial f, we have f.rep.length() == deg(f)+1, and deg(f) &gt;= 0 =&gt; f.rep[deg(f)] != 0. \**************************************************************************/ long deg(const ZZX&amp; a); // return deg(a); deg(0) == -1. const ZZ&amp; coeff(const ZZX&amp; a, long i); // returns the coefficient of X^i, or zero if i not in range const ZZ&amp; LeadCoeff(const ZZX&amp; a); // returns leading term of a, or zero if a == 0 const ZZ&amp; ConstTerm(const ZZX&amp; a); // returns constant term of a, or zero if a == 0 void SetCoeff(ZZX&amp; x, long i, const ZZ&amp; a); void SetCoeff(ZZX&amp; x, long i, long a); // makes coefficient of X^i equal to a; error is raised if i &lt; 0 void SetCoeff(ZZX&amp; x, long i); // makes coefficient of X^i equal to 1; error is raised if i &lt; 0 void SetX(ZZX&amp; x); // x is set to the monomial X long IsX(const ZZX&amp; a); // test if x = X ZZ&amp; ZZX::operator[](long i); const ZZ&amp; ZZX::operator[](long i) const; // indexing operators: f[i] is the coefficient of X^i --- // i should satsify i &gt;= 0 and i &lt;= deg(f). // No range checking (unless NTL_RANGE_CHECK is defined). void ZZX::SetLength(long n); // f.SetLength(n) sets the length of the inderlying coefficient // vector to n --- after this call, indexing f[i] for i = 0..n-1 // is valid. void ZZX::normalize(); // f.normalize() strips leading zeros from coefficient vector of f void ZZX::SetMaxLength(long n); // f.SetMaxLength(n) pre-allocate spaces for n coefficients. The // polynomial that f represents is unchanged. /**************************************************************************\ Comparison \**************************************************************************/ long operator==(const ZZX&amp; a, const ZZX&amp; b); long operator!=(const ZZX&amp; a, const ZZX&amp; b); long IsZero(const ZZX&amp; a); // test for 0 long IsOne(const ZZX&amp; a); // test for 1 // PROMOTIONS: operators ==, != promote {long, ZZ} to ZZX on (a, b). /**************************************************************************\ Addition \**************************************************************************/ // operator notation: ZZX operator+(const ZZX&amp; a, const ZZX&amp; b); ZZX operator-(const ZZX&amp; a, const ZZX&amp; b); ZZX operator-(const ZZX&amp; a); // unary - ZZX&amp; operator+=(ZZX&amp; x, const ZZX&amp; a); ZZX&amp; operator-=(ZZX&amp; x, const ZZX&amp; a); ZZX&amp; operator++(ZZX&amp; x); // prefix void operator++(ZZX&amp; x, int); // postfix ZZX&amp; operator--(ZZX&amp; x); // prefix void operator--(ZZX&amp; x, int); // postfix // procedural versions: void add(ZZX&amp; x, const ZZX&amp; a, const ZZX&amp; b); // x = a + b void sub(ZZX&amp; x, const ZZX&amp; a, const ZZX&amp; b); // x = a - b void negate(ZZX&amp; x, const ZZX&amp; a); // x = -a // PROMOTIONS: binary +, - and procedures add, sub promote {long, ZZ} // to ZZX on (a, b). /**************************************************************************\ Multiplication \**************************************************************************/ // operator notation: ZZX operator*(const ZZX&amp; a, const ZZX&amp; b); ZZX&amp; operator*=(ZZX&amp; x, const ZZX&amp; a); // procedural versions: void mul(ZZX&amp; x, const ZZX&amp; a, const ZZX&amp; b); // x = a * b void sqr(ZZX&amp; x, const ZZX&amp; a); // x = a^2 ZZX sqr(const ZZX&amp; a); // PROMOTIONS: operator * and procedure mul promote {long, ZZ} to ZZX // on (a, b). /**************************************************************************\ Shift Operations LeftShift by n means multiplication by X^n RightShift by n means division by X^n A negative shift amount reverses the direction of the shift. \**************************************************************************/ // operator notation: ZZX operator&lt;&lt;(const ZZX&amp; a, long n); ZZX operator&gt;&gt;(const ZZX&amp; a, long n); ZZX&amp; operator&lt;&lt;=(ZZX&amp; x, long n); ZZX&amp; operator&gt;&gt;=(ZZX&amp; x, long n); // procedural versions: void LeftShift(ZZX&amp; x, const ZZX&amp; a, long n); ZZX LeftShift(const ZZX&amp; a, long n); void RightShift(ZZX&amp; x, const ZZX&amp; a, long n); ZZX RightShift(const ZZX&amp; a, long n); /**************************************************************************\ Division \**************************************************************************/ // Given polynomials a, b in ZZ[X], there exist polynomials // q, r in QQ[X] such that a = b*q + r, deg(r) &lt; deg(b). // These routines return q and/or r if q and/or r lie(s) in ZZ[X], // and otherwise raise an error. // Note that if the leading coefficient of b is 1 or -1, // then q and r always lie in ZZ[X], and no error can occur. // For example, you can write f/2 for a ZZX f. If all coefficients // of f are even, the result is f with a factor of two removed; // otherwise, an error is raised. More generally, f/g will be // evaluate q in ZZ[X] such that f = q*g if such a q exists, // and will otherwise raise an error. // See also below the routines for pseudo-division and division // predicates for routines that are perhaps more useful in // some situations. // operator notation: ZZX operator/(const ZZX&amp; a, const ZZX&amp; b); ZZX operator/(const ZZX&amp; a, const ZZ&amp; b); ZZX operator/(const ZZX&amp; a, long b); ZZX operator%(const ZZX&amp; a, const ZZX&amp; b); ZZX&amp; operator/=(ZZX&amp; x, const ZZX&amp; b); ZZX&amp; operator/=(ZZX&amp; x, const ZZ&amp; b); ZZX&amp; operator/=(ZZX&amp; x, long b); ZZX&amp; operator%=(ZZX&amp; x, const ZZX&amp; b); // procedural versions: void DivRem(ZZX&amp; q, ZZX&amp; r, const ZZX&amp; a, const ZZX&amp; b); // computes q, r such that a = b q + r and deg(r) &lt; deg(b). void div(ZZX&amp; q, const ZZX&amp; a, const ZZX&amp; b); void div(ZZX&amp; q, const ZZX&amp; a, const ZZ&amp; b); void div(ZZX&amp; q, const ZZX&amp; a, long b); // same as DivRem, but only computes q void rem(ZZX&amp; r, const ZZX&amp; a, const ZZX&amp; b); // same as DivRem, but only computes r // divide predicates: long divide(ZZX&amp; q, const ZZX&amp; a, const ZZX&amp; b); long divide(ZZX&amp; q, const ZZX&amp; a, const ZZ&amp; b); long divide(ZZX&amp; q, const ZZX&amp; a, long b); // if b | a, sets q = a/b and returns 1; otherwise returns 0 long divide(const ZZX&amp; a, const ZZX&amp; b); long divide(const ZZX&amp; a, const ZZ&amp; b); long divide(const ZZX&amp; a, long b); // if b | a, returns 1; otherwise returns 0 // These algorithms employ a modular approach, performing the division // modulo small primes (reconstructing q via the CRT). It is // usually much faster than the general division routines above // (especially when b does not divide a). void content(ZZ&amp; d, const ZZX&amp; f); ZZ content(const ZZX&amp; f); // d = content of f, sign(d) == sign(LeadCoeff(f)); content(0) == 0 void PrimitivePart(ZZX&amp; pp, const ZZX&amp; f); ZZX PrimitivePart(const ZZX&amp; f); // pp = primitive part of f, LeadCoeff(pp) &gt;= 0; PrimitivePart(0) == 0 // pseudo-division: void PseudoDivRem(ZZX&amp; q, ZZX&amp; r, const ZZX&amp; a, const ZZX&amp; b); // performs pseudo-division: computes q and r with deg(r) &lt; deg(b), // and LeadCoeff(b)^(deg(a)-deg(b)+1) a = b q + r. Only the classical // algorithm is used. void PseudoDiv(ZZX&amp; q, const ZZX&amp; a, const ZZX&amp; b); ZZX PseudoDiv(const ZZX&amp; a, const ZZX&amp; b); // same as PseudoDivRem, but only computes q void PseudoRem(ZZX&amp; r, const ZZX&amp; a, const ZZX&amp; b); ZZX PseudoRem(const ZZX&amp; a, const ZZX&amp; b); // same as PseudoDivRem, but only computes r /**************************************************************************\ GCD's \**************************************************************************/ void GCD(ZZX&amp; d, const ZZX&amp; a, const ZZX&amp; b); ZZX GCD(const ZZX&amp; a, const ZZX&amp; b); // d = gcd(a, b), LeadCoeff(d) &gt;= 0. Uses a modular algorithm. void XGCD(ZZ&amp; r, ZZX&amp; s, ZZX&amp; t, const ZZX&amp; a, const ZZX&amp; b, long deterministic=0); // r = resultant of a and b; if r != 0, then computes s and t such // that: a*s + b*t = r; otherwise s and t not affected. if // !deterministic, then resultant computation may use a randomized // strategy that errs with probability no more than 2^{-80}. /**************************************************************************\ Input/Output I/O format: [a_0 a_1 ... a_n], represents the polynomial a_0 + a_1*X + ... + a_n*X^n. \**************************************************************************/ istream&amp; operator&gt;&gt;(istream&amp; s, ZZX&amp; x); ostream&amp; operator&lt;&lt;(ostream&amp; s, const ZZX&amp; a); /**************************************************************************\ Some utility routines \**************************************************************************/ void diff(ZZX&amp; x, const ZZX&amp; a); // x = derivative of a ZZX diff(const ZZX&amp; a); long MaxBits(const ZZX&amp; f); // returns max NumBits of coefficients of f void reverse(ZZX&amp; x, const ZZX&amp; a, long hi); ZZX reverse(const ZZX&amp; a, long hi); void reverse(ZZX&amp; x, const ZZX&amp; a); ZZX reverse(const ZZX&amp; a); // x = reverse of a[0]..a[hi] (hi &gt;= -1); // hi defaults to deg(a) in second version void VectorCopy(vec_ZZ&amp; x, const ZZX&amp; a, long n); vec_ZZ VectorCopy(const ZZX&amp; a, long n); // x = copy of coefficient vector of a of length exactly n. // input is truncated or padded with zeroes as appropriate. /**************************************************************************\ Arithmetic mod X^n All routines require n &gt;= 0, otherwise an error is raised. \**************************************************************************/ void trunc(ZZX&amp; x, const ZZX&amp; a, long m); // x = a % X^m ZZX trunc(const ZZX&amp; a, long m); void MulTrunc(ZZX&amp; x, const ZZX&amp; a, const ZZX&amp; b, long n); ZZX MulTrunc(const ZZX&amp; a, const ZZX&amp; b, long n); // x = a * b % X^n void SqrTrunc(ZZX&amp; x, const ZZX&amp; a, long n); ZZX SqrTrunc(const ZZX&amp; a, long n); // x = a^2 % X^n void InvTrunc(ZZX&amp; x, const ZZX&amp; a, long n); ZZX InvTrunc(const ZZX&amp; a, long n); // computes x = a^{-1} % X^m. Must have ConstTerm(a) invertible. /**************************************************************************\ Modular Arithmetic The modulus f must be monic with deg(f) &gt; 0, and other arguments must have smaller degree. \**************************************************************************/ void MulMod(ZZX&amp; x, const ZZX&amp; a, const ZZX&amp; b, const ZZX&amp; f); ZZX MulMod(const ZZX&amp; a, const ZZX&amp; b, const ZZX&amp; f); // x = a * b mod f void SqrMod(ZZX&amp; x, const ZZX&amp; a, const ZZX&amp; f); ZZX SqrMod(const ZZX&amp; a, const ZZX&amp; f); // x = a^2 mod f void MulByXMod(ZZX&amp; x, const ZZX&amp; a, const ZZX&amp; f); ZZX MulByXMod(const ZZX&amp; a, const ZZX&amp; f); // x = a*X mod f /**************************************************************************\ traces, norms, resultants, discriminants, minimal and characteristic polynomials \**************************************************************************/ void TraceMod(ZZ&amp; res, const ZZX&amp; a, const ZZX&amp; f); ZZ TraceMod(const ZZX&amp; a, const ZZX&amp; f); // res = trace of (a mod f). f must be monic, 0 &lt; deg(f), deg(a) &lt; // deg(f) void TraceVec(vec_ZZ&amp; S, const ZZX&amp; f); vec_ZZ TraceVec(const ZZX&amp; f); // S[i] = Trace(X^i mod f), for i = 0..deg(f)-1. // f must be a monic polynomial. // The following routines use a modular approach. void resultant(ZZ&amp; res, const ZZX&amp; a, const ZZX&amp; b, long deterministic=0); ZZ resultant(const ZZX&amp; a, const ZZX&amp; b, long deterministic=0); // res = resultant of a and b. If !deterministic, then it may use a // randomized strategy that errs with probability no more than // 2^{-80}. void NormMod(ZZ&amp; res, const ZZX&amp; a, const ZZX&amp; f, long deterministic=0); ZZ NormMod(const ZZX&amp; a, const ZZX&amp; f, long deterministic=0); // res = norm of (a mod f). f must be monic, 0 &lt; deg(f), deg(a) &lt; // deg(f). If !deterministic, then it may use a randomized strategy // that errs with probability no more than 2^{-80}. void discriminant(ZZ&amp; d, const ZZX&amp; a, long deterministic=0); ZZ discriminant(const ZZX&amp; a, long deterministic=0); // d = discriminant of a = (-1)^{m(m-1)/2} resultant(a, a')/lc(a), // where m = deg(a). If !deterministic, then it may use a randomized // strategy that errs with probability no more than 2^{-80}. void CharPolyMod(ZZX&amp; g, const ZZX&amp; a, const ZZX&amp; f, long deterministic=0); ZZX CharPolyMod(const ZZX&amp; a, const ZZX&amp; f, long deterministic=0); // g = char poly of (a mod f). f must be monic. If !deterministic, // then it may use a randomized strategy that errs with probability no // more than 2^{-80}. void MinPolyMod(ZZX&amp; g, const ZZX&amp; a, const ZZX&amp; f); ZZX MinPolyMod(const ZZX&amp; a, const ZZX&amp; f); // g = min poly of (a mod f). f must be monic, 0 &lt; deg(f), deg(a) &lt; // deg(f). May use a probabilistic strategy that errs with // probability no more than 2^{-80}. /**************************************************************************\ Incremental Chinese Remaindering \**************************************************************************/ long CRT(ZZX&amp; a, ZZ&amp; prod, const zz_pX&amp; A); long CRT(ZZX&amp; a, ZZ&amp; prod, const ZZ_pX&amp; A); // Incremental Chinese Remaindering: If p is the current zz_p/ZZ_p modulus with // (p, prod) = 1; Computes a' such that a' = a mod prod and a' = A mod p, // with coefficients in the interval (-p*prod/2, p*prod/2]; // Sets a := a', prod := p*prod, and returns 1 if a's value changed. /**************************************************************************\ vectors of ZZX's \**************************************************************************/ typedef Vec
<zzx>
vec_ZZX; // backward compatibility /**************************************************************************\ Miscellany \**************************************************************************/ void clear(ZZX&amp; x); // x = 0 void set(ZZX&amp; x); // x = 1 void ZZX::kill(); // f.kill() sets f to 0 and frees all memory held by f. Equivalent to // f.rep.kill(). ZZX::ZZX(INIT_SIZE_TYPE, long n); // ZZX(INIT_SIZE, n) initializes to zero, but space is pre-allocated // for n coefficients static const ZZX&amp; zero(); // ZZX::zero() is a read-only reference to 0 void ZZX::swap(ZZX&amp; x); void swap(ZZX&amp; x, ZZX&amp; y); // swap (by swapping pointers) ZZX::ZZX(long i, const ZZ&amp; c); ZZX::ZZX(long i, long c); // initial value c*X^i, provided for backward compatibility
</zzx>
</ntl>
</ntl>
</jngerhar@plato.uni-paderborn.de>
</body>
</html>