⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zz_px.txt

📁 密码大家Shoup写的数论算法c语言实现
💻 TXT
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************\MODULE: ZZ_pXSUMMARY:The class ZZ_pX implements polynomial arithmetic modulo p.Polynomial arithmetic is implemented using the FFT, combined with theChinese Remainder Theorem.  A more detailed description of thetechniques used here can be found in [Shoup, J. SymbolicComp. 20:363-397, 1995].Small degree polynomials are multiplied either with classical or Karatsuba algorithms.\**************************************************************************/#include <NTL/ZZ_p.h>#include <NTL/vec_ZZ_p.h>class ZZ_pX {public:   ZZ_pX(); // initialize to 0   ZZ_pX(const ZZ_pX& a); // copy constructor   ZZ_pX& operator=(const ZZ_pX& a); // assignment   ZZ_pX& operator=(const ZZ_p& a); // assignment   ZZ_pX& operator=(const long a); // assignment   ZZ_pX(long i, const ZZ_p& c);  // initialize to X^i*c   ZZ_pX(long i, long c);   ~ZZ_pX(); // destructor   };/**************************************************************************\                                  Comparison\**************************************************************************/long operator==(const ZZ_pX& a, const ZZ_pX& b);long operator!=(const ZZ_pX& a, const ZZ_pX& b);// PROMOTIONS: operators ==, != promote {long, ZZ_p} to ZZ_pX on (a, b).long IsZero(const ZZ_pX& a); // test for 0long IsOne(const ZZ_pX& a); // test for 1/**************************************************************************\                                   Addition\**************************************************************************/// operator notation:ZZ_pX operator+(const ZZ_pX& a, const ZZ_pX& b);ZZ_pX operator-(const ZZ_pX& a, const ZZ_pX& b);ZZ_pX operator-(const ZZ_pX& a); // unary -ZZ_pX& operator+=(ZZ_pX& x, const ZZ_pX& a);ZZ_pX& operator+=(ZZ_pX& x, const ZZ_p& a);ZZ_pX& operator+=(ZZ_pX& x, long a);ZZ_pX& operator-=(ZZ_pX& x, const ZZ_pX& a);ZZ_pX& operator-=(ZZ_pX& x, const ZZ_p& a);ZZ_pX& operator-=(ZZ_pX& x, long a);ZZ_pX& operator++(ZZ_pX& x);  // prefixvoid operator++(ZZ_pX& x, int);  // postfixZZ_pX& operator--(ZZ_pX& x);  // prefixvoid operator--(ZZ_pX& x, int);  // postfix// procedural versions:void add(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b); // x = a + bvoid sub(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b); // x = a - bvoid negate(ZZ_pX& x, const ZZ_pX& a); // x = -a// PROMOTIONS: binary +, - and procedures add, sub promote// {long, ZZ_p} to ZZ_pX on (a, b)./**************************************************************************\                               Multiplication\**************************************************************************/// operator notation:ZZ_pX operator*(const ZZ_pX& a, const ZZ_pX& b);ZZ_pX& operator*=(ZZ_pX& x, const ZZ_pX& a);ZZ_pX& operator*=(ZZ_pX& x, const ZZ_p& a);ZZ_pX& operator*=(ZZ_pX& x, long a);// procedural versions:void mul(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b); // x = a * bvoid sqr(ZZ_pX& x, const ZZ_pX& a); // x = a^2ZZ_pX sqr(const ZZ_pX& a); // PROMOTIONS: operator * and procedure mul promote {long, ZZ_p} to ZZ_pX// on (a, b).void power(ZZ_pX& x, const ZZ_pX& a, long e);  // x = a^e (e >= 0)ZZ_pX power(const ZZ_pX& a, long e); /**************************************************************************\                               Shift OperationsLeftShift by n means multiplication by X^nRightShift by n means division by X^nA negative shift amount reverses the direction of the shift.\**************************************************************************/// operator notation:ZZ_pX operator<<(const ZZ_pX& a, long n);ZZ_pX operator>>(const ZZ_pX& a, long n);ZZ_pX& operator<<=(ZZ_pX& x, long n);ZZ_pX& operator>>=(ZZ_pX& x, long n);// procedural versions:void LeftShift(ZZ_pX& x, const ZZ_pX& a, long n); ZZ_pX LeftShift(const ZZ_pX& a, long n);void RightShift(ZZ_pX& x, const ZZ_pX& a, long n); ZZ_pX RightShift(const ZZ_pX& a, long n); /**************************************************************************\                                  Division\**************************************************************************/// operator notation:ZZ_pX operator/(const ZZ_pX& a, const ZZ_pX& b);ZZ_pX operator/(const ZZ_pX& a, const ZZ_p& b);ZZ_pX operator/(const ZZ_pX& a, long b);ZZ_pX& operator/=(ZZ_pX& x, const ZZ_pX& b);ZZ_pX& operator/=(ZZ_pX& x, const ZZ_p& b);ZZ_pX& operator/=(ZZ_pX& x, long b);ZZ_pX operator%(const ZZ_pX& a, const ZZ_pX& b);ZZ_pX& operator%=(ZZ_pX& x, const ZZ_pX& b);// procedural versions:void DivRem(ZZ_pX& q, ZZ_pX& r, const ZZ_pX& a, const ZZ_pX& b);// q = a/b, r = a%bvoid div(ZZ_pX& q, const ZZ_pX& a, const ZZ_pX& b);void div(ZZ_pX& q, const ZZ_pX& a, const ZZ_p& b);void div(ZZ_pX& q, const ZZ_pX& a, long b);// q = a/bvoid rem(ZZ_pX& r, const ZZ_pX& a, const ZZ_pX& b);// r = a%blong divide(ZZ_pX& q, const ZZ_pX& a, const ZZ_pX& b);// if b | a, sets q = a/b and returns 1; otherwise returns 0long divide(const ZZ_pX& a, const ZZ_pX& b);// if b | a, sets q = a/b and returns 1; otherwise returns 0/**************************************************************************\                                   GCD'sThese routines are intended for use when p is prime.\**************************************************************************/void GCD(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b);ZZ_pX GCD(const ZZ_pX& a, const ZZ_pX& b); // x = GCD(a, b),  x is always monic (or zero if a==b==0).void XGCD(ZZ_pX& d, ZZ_pX& s, ZZ_pX& t, const ZZ_pX& a, const ZZ_pX& b);// d = gcd(a,b), a s + b t = d // NOTE: A classical algorithm is used, switching over to a// "half-GCD" algorithm for large degree/**************************************************************************\                                  Input/OutputI/O format:   [a_0 a_1 ... a_n],represents the polynomial a_0 + a_1*X + ... + a_n*X^n.On output, all coefficients will be integers between 0 and p-1, anda_n not zero (the zero polynomial is [ ]).  On input, the coefficientsare arbitrary integers which are reduced modulo p, and leading zerosstripped.\**************************************************************************/istream& operator>>(istream& s, ZZ_pX& x);ostream& operator<<(ostream& s, const ZZ_pX& a);/**************************************************************************\                              Some utility routines\**************************************************************************/long deg(const ZZ_pX& a);  // return deg(a); deg(0) == -1.const ZZ_p& coeff(const ZZ_pX& a, long i);// returns a read-only reference to the coefficient of X^i, or zero if// i not in rangeconst ZZ_p& LeadCoeff(const ZZ_pX& a);// read-only reference to leading term of a, or zero if a == 0const ZZ_p& ConstTerm(const ZZ_pX& a);// read-only reference to constant term of a, or zero if a == 0void SetCoeff(ZZ_pX& x, long i, const ZZ_p& a);void SetCoeff(ZZ_pX& x, long i, long a);// makes coefficient of X^i equal to a;  error is raised if i < 0void SetCoeff(ZZ_pX& x, long i);// makes coefficient of X^i equal to 1;  error is raised if i < 0void SetX(ZZ_pX& x); // x is set to the monomial Xlong IsX(const ZZ_pX& a); // test if x = Xvoid diff(ZZ_pX& x, const ZZ_pX& a); // x = derivative of aZZ_pX diff(const ZZ_pX& a); void MakeMonic(ZZ_pX& x); // if x != 0 makes x into its monic associate; LeadCoeff(x) must be// invertible in this case.void reverse(ZZ_pX& x, const ZZ_pX& a, long hi);ZZ_pX reverse(const ZZ_pX& a, long hi);void reverse(ZZ_pX& x, const ZZ_pX& a);ZZ_pX reverse(const ZZ_pX& a);// x = reverse of a[0]..a[hi] (hi >= -1);// hi defaults to deg(a) in second versionvoid VectorCopy(vec_ZZ_p& x, const ZZ_pX& a, long n);vec_ZZ_p VectorCopy(const ZZ_pX& a, long n);// x = copy of coefficient vector of a of length exactly n.// input is truncated or padded with zeroes as appropriate./**************************************************************************\                             Random Polynomials\**************************************************************************/void random(ZZ_pX& x, long n);ZZ_pX random_ZZ_pX(long n);// generate a random polynomial of degree < n /**************************************************************************\                    Polynomial Evaluation and related problems\**************************************************************************/void BuildFromRoots(ZZ_pX& x, const vec_ZZ_p& a);ZZ_pX BuildFromRoots(const vec_ZZ_p& a);// computes the polynomial (X-a[0]) ... (X-a[n-1]), where n = a.length()void eval(ZZ_p& b, const ZZ_pX& f, const ZZ_p& a);ZZ_p eval(const ZZ_pX& f, const ZZ_p& a);// b = f(a)void eval(vec_ZZ_p& b, const ZZ_pX& f, const vec_ZZ_p& a);vec_ZZ_p eval(const ZZ_pX& f, const vec_ZZ_p& a);//  b.SetLength(a.length()).  b[i] = f(a[i]) for 0 <= i < a.length()void interpolate(ZZ_pX& f, const vec_ZZ_p& a, const vec_ZZ_p& b);ZZ_pX interpolate(const vec_ZZ_p& a, const vec_ZZ_p& b);// interpolates the polynomial f satisfying f(a[i]) = b[i].  p should// be prime./**************************************************************************\                       Arithmetic mod X^nAll routines require n >= 0, otherwise an error is raised.\**************************************************************************/void trunc(ZZ_pX& x, const ZZ_pX& a, long n); // x = a % X^nZZ_pX trunc(const ZZ_pX& a, long n); void MulTrunc(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b, long n);ZZ_pX MulTrunc(const ZZ_pX& a, const ZZ_pX& b, long n);// x = a * b % X^nvoid SqrTrunc(ZZ_pX& x, const ZZ_pX& a, long n);ZZ_pX SqrTrunc(const ZZ_pX& a, long n);// x = a^2 % X^nvoid InvTrunc(ZZ_pX& x, const ZZ_pX& a, long n);ZZ_pX InvTrunc(const ZZ_pX& a, long n);// computes x = a^{-1} % X^m.  Must have ConstTerm(a) invertible./**************************************************************************\                Modular Arithmetic (without pre-conditioning)Arithmetic mod f.All inputs and outputs are polynomials of degree less than deg(f), anddeg(f) > 0.NOTE: if you want to do many computations with a fixed f, use theZZ_pXModulus data structure and associated routines below for betterperformance.\**************************************************************************/void MulMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b, const ZZ_pX& f);ZZ_pX MulMod(const ZZ_pX& a, const ZZ_pX& b, const ZZ_pX& f);// x = (a * b) % fvoid SqrMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& f);ZZ_pX SqrMod(const ZZ_pX& a, const ZZ_pX& f);// x = a^2 % fvoid MulByXMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& f);ZZ_pX MulByXMod(const ZZ_pX& a, const ZZ_pX& f);// x = (a * X) mod fvoid InvMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& f);ZZ_pX InvMod(const ZZ_pX& a, const ZZ_pX& f);// x = a^{-1} % f, error is a is not invertiblelong InvModStatus(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& f);// if (a, f) = 1, returns 0 and sets x = a^{-1} % f; otherwise,// returns 1 and sets x = (a, f)// for modular exponentiation, see below/**************************************************************************\                     Modular Arithmetic with Pre-ConditioningIf you need to do a lot of arithmetic modulo a fixed f, build aZZ_pXModulus F for f.  This pre-computes information about f thatspeeds up subsequent computations.It is required that deg(f) > 0 and that LeadCoeff(f) is invertible.As an example, the following routine computes the product modulo f of a vectorof polynomials.#include <NTL/ZZ_pX.h>void product(ZZ_pX& x, const vec_ZZ_pX& v, const ZZ_pX& f){   ZZ_pXModulus F(f);   ZZ_pX res;   res = 1;   long i;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -