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

📄 lzz_px.txt

📁 数值算法库for Windows
💻 TXT
📖 第 1 页 / 共 2 页
字号:

/**************************************************************************\

MODULE: zz_pX

SUMMARY:

The class zz_pX implements polynomial arithmetic modulo p.

Polynomial arithmetic is implemented using a combination of classical
routines, Karatsuba, and FFT.

\**************************************************************************/

#include "zz_p.h"
#include "vec_zz_p.h"

class zz_pX {
public:

   zz_pX(); // initial value 0

   zz_pX(const zz_pX& a); // copy

   zz_pX& operator=(const zz_pX& a); // assignment
   zz_pX& operator=(zz_p a); 
   zz_pX& operator=(long a); 

   ~zz_pX(); // destructor

   zz_pX(long i, zz_p c); // initialize to X^i*c
   zz_pX(long i, long c); 
   
};





/**************************************************************************\

                                  Comparison

\**************************************************************************/


long operator==(const zz_pX& a, const zz_pX& b);
long operator!=(const zz_pX& a, const zz_pX& b);

long IsZero(const zz_pX& a); // test for 0
long IsOne(const zz_pX& a); // test for 1

// PROMOTIONS: operators ==, != promote {long, zz_p} to zz_pX on (a, b)


/**************************************************************************\

                                   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, 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, zz_p a);
zz_pX& operator-=(zz_pX& x, long a);

zz_pX& operator++(zz_pX& x);  // prefix
void operator++(zz_pX& x, int);  // postfix

zz_pX& operator--(zz_pX& x);  // prefix
void operator--(zz_pX& x, int);  // postfix

// procedural versions:


void add(zz_pX& x, const zz_pX& a, const zz_pX& b); // x = a + b
void sub(zz_pX& x, const zz_pX& a, const zz_pX& b); // x = a - b
void 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, 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 * b

void sqr(zz_pX& x, const zz_pX& a); // x = a^2
zz_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 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:

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_pX& b);

zz_pX& operator/=(zz_pX& x, const zz_pX& a);
zz_pX& operator/=(zz_pX& x, zz_p a);
zz_pX& operator/=(zz_pX& x, long a);

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%b

void div(zz_pX& q, const zz_pX& a, const zz_pX& b);
// q = a/b

void rem(zz_pX& r, const zz_pX& a, const zz_pX& b);
// r = a%b

long divide(zz_pX& q, const zz_pX& a, const zz_pX& b);
// if b | a, sets q = a/b and returns 1; otherwise returns 0

long divide(const zz_pX& a, const zz_pX& b);
// if b | a, sets q = a/b and returns 1; otherwise returns 0

// PROMOTIONS: operator / and procedure div promote {long, zz_p} to zz_pX
// on (a, b).


/**************************************************************************\

                                   GCD's

These 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/Output

I/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, amd
a_n not zero (the zero polynomial is [ ]).  On input, the coefficients
are arbitrary integers which are reduced modulo p, and leading zeros
stripped.

\**************************************************************************/

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.

zz_p coeff(const zz_pX& a, long i);
// returns the coefficient of X^i, or zero if i not in range

zz_p LeadCoeff(const zz_pX& a);
// returns leading term of a, or zero if a == 0

zz_p ConstTerm(const zz_pX& a);
// returns constant term of a, or zero if a == 0

void SetCoeff(zz_pX& x, long i, 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 < 0

void SetCoeff(zz_pX& x, long i);
// makes coefficient of X^i equal to 1;  error is raised if i < 0

void SetX(zz_pX& x); // x is set to the monomial X

long IsX(const zz_pX& a); // test if x = X

void diff(zz_pX& x, const zz_pX& a);
zz_pX diff(const zz_pX& a); 
// x = derivative of 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 version

void 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);
// x = 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, zz_p a);
zz_p eval(const zz_pX& f, 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^n

It is required that n >= 0, otherwise an error is raised.

\**************************************************************************/

void trunc(zz_pX& x, const zz_pX& a, long n); // x = a % X^n
zz_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^n

void SqrTrunc(zz_pX& x, const zz_pX& a, long n);
zz_pX SqrTrunc(const zz_pX& a, long n);
// x = a^2 % X^n

void InvTrunc(zz_pX& x, const zz_pX& a, long n);
zz_pX InvTrunc(const zz_pX& a, long n);
// computes x = a^{-1} % X^n.  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), and
deg(f) > 0.

NOTE: if you want to do many computations with a fixed f, use the
zz_pXModulus data structure and associated routines below for better
performance.

\**************************************************************************/

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) % f

void 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 % f

void 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 f

void 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 invertible

long 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-Conditioning

If you need to do a lot of arithmetic modulo a fixed f, build
zz_pXModulus F for f.  This pre-computes information about f that
speeds up subsequent computations. Required: deg(f) > 0 and LeadCoeff(f)
invertible.

As an example, the following routine computes the product modulo f of a vector
of polynomials.

#include "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;
   for (i = 0; i < v.length(); i++)
      MulMod(res, res, v[i], F); 
   x = res;
}


Note that automatic conversions are provided so that a zz_pX can
be used wherever a zz_pXModulus is required, and a zz_pXModulus
can be used wherever a zz_pX is required.

⌨️ 快捷键说明

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