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

📄 lzz_pex.txt

📁 密码大家Shoup写的数论算法c语言实现
💻 TXT
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************\MODULE: zz_pEXSUMMARY:The class zz_pEX represents polynomials over zz_pE,and so can be used, for example, for arithmentic in GF(p^n)[X].However, except where mathematically necessary (e.g., GCD computations),zz_pE need not be a field.\**************************************************************************/#include <NTL/lzz_pE.h>#include <NTL/vec_lzz_pE.h>class zz_pEX {public:   zz_pEX(); // initial value 0   zz_pEX(const zz_pEX& a); // copy   zz_pEX& operator=(const zz_pEX& a); // assignment   zz_pEX& operator=(const zz_pE& a);   zz_pEX& operator=(const zz_p& a);   zz_pEX& operator=(long a);   ~zz_pEX(); // destructor   zz_pEX(long i, const zz_pE& c); // initilaize to X^i*c   zz_pEX(long i, const zz_p& c);    zz_pEX(long i, long c);    };/**************************************************************************\                                  Comparison\**************************************************************************/long operator==(const zz_pEX& a, const zz_pEX& b);long operator!=(const zz_pEX& a, const zz_pEX& b);long IsZero(const zz_pEX& a); // test for 0long IsOne(const zz_pEX& a); // test for 1// PROMOTIONS: ==, != promote {long,zz_p,zz_pE} to zz_pEX on (a, b)./**************************************************************************\                                   Addition\**************************************************************************/// operator notation:zz_pEX operator+(const zz_pEX& a, const zz_pEX& b);zz_pEX operator-(const zz_pEX& a, const zz_pEX& b);zz_pEX operator-(const zz_pEX& a);zz_pEX& operator+=(zz_pEX& x, const zz_pEX& a);zz_pEX& operator+=(zz_pEX& x, const zz_pE& a);zz_pEX& operator+=(zz_pEX& x, const zz_p& a);zz_pEX& operator+=(zz_pEX& x, long a);zz_pEX& operator++(zz_pEX& x);  // prefixvoid operator++(zz_pEX& x, int);  // postfixzz_pEX& operator-=(zz_pEX& x, const zz_pEX& a);zz_pEX& operator-=(zz_pEX& x, const zz_pE& a);zz_pEX& operator-=(zz_pEX& x, const zz_p& a);zz_pEX& operator-=(zz_pEX& x, long a);zz_pEX& operator--(zz_pEX& x);  // prefixvoid operator--(zz_pEX& x, int);  // postfix// procedural versions:void add(zz_pEX& x, const zz_pEX& a, const zz_pEX& b); // x = a + bvoid sub(zz_pEX& x, const zz_pEX& a, const zz_pEX& b); // x = a - b void negate(zz_pEX& x, const zz_pEX& a); // x = - a // PROMOTIONS: +, -, add, sub promote {long,zz_p,zz_pE} to zz_pEX on (a, b)./**************************************************************************\                               Multiplication\**************************************************************************/// operator notation:zz_pEX operator*(const zz_pEX& a, const zz_pEX& b);zz_pEX& operator*=(zz_pEX& x, const zz_pEX& a);zz_pEX& operator*=(zz_pEX& x, const zz_pE& a);zz_pEX& operator*=(zz_pEX& x, const zz_p& a);zz_pEX& operator*=(zz_pEX& x, long a);// procedural versions:void mul(zz_pEX& x, const zz_pEX& a, const zz_pEX& b); // x = a * bvoid sqr(zz_pEX& x, const zz_pEX& a); // x = a^2zz_pEX sqr(const zz_pEX& a); // PROMOTIONS: *, mul promote {long,zz_p,zz_pE} to zz_pEX on (a, b).void power(zz_pEX& x, const zz_pEX& a, long e);  // x = a^e (e >= 0)zz_pEX power(const zz_pEX& 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_pEX operator<<(const zz_pEX& a, long n);zz_pEX operator>>(const zz_pEX& a, long n);zz_pEX& operator<<=(zz_pEX& x, long n);zz_pEX& operator>>=(zz_pEX& x, long n);// procedural versions:void LeftShift(zz_pEX& x, const zz_pEX& a, long n); zz_pEX LeftShift(const zz_pEX& a, long n);void RightShift(zz_pEX& x, const zz_pEX& a, long n); zz_pEX RightShift(const zz_pEX& a, long n); /**************************************************************************\                                  Division\**************************************************************************/// operator notation:zz_pEX operator/(const zz_pEX& a, const zz_pEX& b);zz_pEX operator/(const zz_pEX& a, const zz_pE& b);zz_pEX operator/(const zz_pEX& a, const zz_p& b);zz_pEX operator/(const zz_pEX& a, long b);zz_pEX operator%(const zz_pEX& a, const zz_pEX& b);zz_pEX& operator/=(zz_pEX& x, const zz_pEX& a);zz_pEX& operator/=(zz_pEX& x, const zz_pE& a);zz_pEX& operator/=(zz_pEX& x, const zz_p& a);zz_pEX& operator/=(zz_pEX& x, long a);zz_pEX& operator%=(zz_pEX& x, const zz_pEX& a);// procedural versions:void DivRem(zz_pEX& q, zz_pEX& r, const zz_pEX& a, const zz_pEX& b);// q = a/b, r = a%bvoid div(zz_pEX& q, const zz_pEX& a, const zz_pEX& b);void div(zz_pEX& q, const zz_pEX& a, const zz_pE& b);void div(zz_pEX& q, const zz_pEX& a, const zz_p& b);void div(zz_pEX& q, const zz_pEX& a, long b);// q = a/bvoid rem(zz_pEX& r, const zz_pEX& a, const zz_pEX& b);// r = a%blong divide(zz_pEX& q, const zz_pEX& a, const zz_pEX& b);// if b | a, sets q = a/b and returns 1; otherwise returns 0long divide(const zz_pEX& a, const zz_pEX& b);// if b | a, sets q = a/b and returns 1; otherwise returns 0/**************************************************************************\                                   GCD'sThese routines are intended for use when zz_pE is a field.\**************************************************************************/void GCD(zz_pEX& x, const zz_pEX& a, const zz_pEX& b);zz_pEX GCD(const zz_pEX& a, const zz_pEX& b); // x = GCD(a, b),  x is always monic (or zero if a==b==0).void XGCD(zz_pEX& d, zz_pEX& s, zz_pEX& t, const zz_pEX& a, const zz_pEX& b);// d = gcd(a,b), a s + b t = d /**************************************************************************\                                  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 polynomials of degree < zz_pE::degree() anda_n not zero (the zero polynomial is [ ]).  On input, the coefficientsare arbitrary polynomials which are reduced modulo zz_pE::modulus(), and leading zeros stripped.\**************************************************************************/istream& operator>>(istream& s, zz_pEX& x);ostream& operator<<(ostream& s, const zz_pEX& a);/**************************************************************************\                              Some utility routines\**************************************************************************/long deg(const zz_pEX& a);  // return deg(a); deg(0) == -1.const zz_pE& coeff(const zz_pEX& a, long i);// returns a read-only reference to the coefficient of X^i, or zero if// i not in rangeconst zz_pE& LeadCoeff(const zz_pEX& a);// read-only reference to leading term of a, or zero if a == 0const zz_pE& ConstTerm(const zz_pEX& a);// read-only reference to constant term of a, or zero if a == 0void SetCoeff(zz_pEX& x, long i, const zz_pE& a);void SetCoeff(zz_pEX& x, long i, const zz_p& a);void SetCoeff(zz_pEX& x, long i, long a);// makes coefficient of X^i equal to a;  error is raised if i < 0void SetCoeff(zz_pEX& x, long i);// makes coefficient of X^i equal to 1;  error is raised if i < 0void SetX(zz_pEX& x); // x is set to the monomial Xlong IsX(const zz_pEX& a); // test if x = Xvoid diff(zz_pEX& x, const zz_pEX& a); // x = derivative of azz_pEX diff(const zz_pEX& a); void MakeMonic(zz_pEX& x); // if x != 0 makes x into its monic associate; LeadCoeff(x) must be// invertible in this casevoid reverse(zz_pEX& x, const zz_pEX& a, long hi);zz_pEX reverse(const zz_pEX& a, long hi);void reverse(zz_pEX& x, const zz_pEX& a);zz_pEX reverse(const zz_pEX& a);// x = reverse of a[0]..a[hi] (hi >= -1);// hi defaults to deg(a) in second versionvoid VectorCopy(vec_zz_pE& x, const zz_pEX& a, long n);vec_zz_pE VectorCopy(const zz_pEX& 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_pEX& x, long n);zz_pEX random_zz_pEX(long n);// x = random polynomial of degree < n /**************************************************************************\                    Polynomial Evaluation and related problems\**************************************************************************/void BuildFromRoots(zz_pEX& x, const vec_zz_pE& a);zz_pEX BuildFromRoots(const vec_zz_pE& a);// computes the polynomial (X-a[0]) ... (X-a[n-1]), where n = a.length()void eval(zz_pE& b, const zz_pEX& f, const zz_pE& a);zz_pE eval(const zz_pEX& f, const zz_pE& a);// b = f(a)void eval(zz_pE& b, const zz_pX& f, const zz_pE& a);zz_pE eval(const zz_pEX& f, const zz_pE& a);// b = f(a); uses ModComp algorithm for zz_pXvoid eval(vec_zz_pE& b, const zz_pEX& f, const vec_zz_pE& a);vec_zz_pE eval(const zz_pEX& f, const vec_zz_pE& a);//  b.SetLength(a.length()); b[i] = f(a[i]) for 0 <= i < a.length()void interpolate(zz_pEX& f, const vec_zz_pE& a, const vec_zz_pE& b);zz_pEX interpolate(const vec_zz_pE& a, const vec_zz_pE& b);// interpolates the polynomial f satisfying f(a[i]) = b[i].  /**************************************************************************\                       Arithmetic mod X^nRequired: n >= 0; otherwise, an error is raised.\**************************************************************************/void trunc(zz_pEX& x, const zz_pEX& a, long n); // x = a % X^nzz_pEX trunc(const zz_pEX& a, long n); void MulTrunc(zz_pEX& x, const zz_pEX& a, const zz_pEX& b, long n);zz_pEX MulTrunc(const zz_pEX& a, const zz_pEX& b, long n);// x = a * b % X^nvoid SqrTrunc(zz_pEX& x, const zz_pEX& a, long n);zz_pEX SqrTrunc(const zz_pEX& a, long n);// x = a^2 % X^nvoid InvTrunc(zz_pEX& x, const zz_pEX& a, long n);zz_pEX InvTrunc(zz_pEX& x, const zz_pEX& 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_pEXModulus data structure and associated routines below for betterperformance.\**************************************************************************/void MulMod(zz_pEX& x, const zz_pEX& a, const zz_pEX& b, const zz_pEX& f);zz_pEX MulMod(const zz_pEX& a, const zz_pEX& b, const zz_pEX& f);// x = (a * b) % fvoid SqrMod(zz_pEX& x, const zz_pEX& a, const zz_pEX& f);zz_pEX SqrMod(const zz_pEX& a, const zz_pEX& f);// x = a^2 % fvoid MulByXMod(zz_pEX& x, const zz_pEX& a, const zz_pEX& f);zz_pEX MulByXMod(const zz_pEX& a, const zz_pEX& f);// x = (a * X) mod fvoid InvMod(zz_pEX& x, const zz_pEX& a, const zz_pEX& f);zz_pEX InvMod(const zz_pEX& a, const zz_pEX& f);// x = a^{-1} % f, error is a is not invertiblelong InvModStatus(zz_pEX& x, const zz_pEX& a, const zz_pEX& f);// if (a, f) = 1, returns 0 and sets x = a^{-1} % f; otherwise,// returns 1 and sets x = (a, f)/**************************************************************************\                     Modular Arithmetic with Pre-ConditioningIf you need to do a lot of arithmetic modulo a fixed f, buildzz_pEXModulus F for f.  This pre-computes information about f thatspeeds up subsequent computations.As an example, the following routine the product modulo f of a vectorof polynomials.#include <NTL/lzz_pEX.h>void product(zz_pEX& x, const vec_zz_pEX& v, const zz_pEX& f){   zz_pEXModulus F(f);   zz_pEX res;   res = 1;   long i;   for (i = 0; i < v.length(); i++)      MulMod(res, res, v[i], F);    x = res;}

⌨️ 快捷键说明

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