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

📄 gf2ex.txt

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

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

MODULE: GF2EX

SUMMARY:

The class GF2EX represents polynomials over GF2E,
and so can be used, for example, for arithmentic in GF(2^n)[X].
However, except where mathematically necessary (e.g., GCD computations),
GF2E need not be a field.

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

#include <NTL/GF2E.h>
#include <NTL/vec_GF2E.h>

class GF2EX {
public:

   GF2EX(); // initial value 0

   GF2EX(const GF2EX& a); // copy

   GF2EX& operator=(const GF2EX& a); // assignment
   GF2EX& operator=(const GF2E& a);
   GF2EX& operator=(GF2 a);
   GF2EX& operator=(long a);

   ~GF2EX(); // destructor

   GF2EX(long i, const GF2E& c); // initilaize to X^i*c
   GF2EX(long i, GF2 c); 
   GF2EX(long i, long c); 

   
};






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

                                  Comparison

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


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

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

// PROMOTIONS: ==, != promote {long,GF2,GF2E} to GF2EX on (a, b).

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

                                   Addition

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

// operator notation:

GF2EX operator+(const GF2EX& a, const GF2EX& b);
GF2EX operator-(const GF2EX& a, const GF2EX& b);
GF2EX operator-(const GF2EX& a);

GF2EX& operator+=(GF2EX& x, const GF2EX& a);
GF2EX& operator+=(GF2EX& x, const GF2E& a);
GF2EX& operator+=(GF2EX& x, GF2 a);
GF2EX& operator+=(GF2EX& x, long a);


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

GF2EX& operator-=(GF2EX& x, const GF2EX& a);
GF2EX& operator-=(GF2EX& x, const GF2E& a);
GF2EX& operator-=(GF2EX& x, GF2 a);
GF2EX& operator-=(GF2EX& x, long a);

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

// procedural versions:

void add(GF2EX& x, const GF2EX& a, const GF2EX& b); // x = a + b
void sub(GF2EX& x, const GF2EX& a, const GF2EX& b); // x = a - b 
void negate(GF2EX& x, const GF2EX& a); // x = - a 

// PROMOTIONS: +, -, add, sub promote {long,GF2,GF2E} to GF2EX on (a, b).



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

                               Multiplication

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

// operator notation:

GF2EX operator*(const GF2EX& a, const GF2EX& b);

GF2EX& operator*=(GF2EX& x, const GF2EX& a);
GF2EX& operator*=(GF2EX& x, const GF2E& a);
GF2EX& operator*=(GF2EX& x, GF2 a);
GF2EX& operator*=(GF2EX& x, long a);


// procedural versions:


void mul(GF2EX& x, const GF2EX& a, const GF2EX& b); // x = a * b

void sqr(GF2EX& x, const GF2EX& a); // x = a^2
GF2EX sqr(const GF2EX& a); 

// PROMOTIONS: *, mul promote {long,GF2,GF2E} to GF2EX on (a, b).

void power(GF2EX& x, const GF2EX& a, long e);  // x = a^e (e >= 0)
GF2EX power(const GF2EX& 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:

GF2EX operator<<(const GF2EX& a, long n);
GF2EX operator>>(const GF2EX& a, long n);

GF2EX& operator<<=(GF2EX& x, long n);
GF2EX& operator>>=(GF2EX& x, long n);

// procedural versions:

void LeftShift(GF2EX& x, const GF2EX& a, long n); 
GF2EX LeftShift(const GF2EX& a, long n);

void RightShift(GF2EX& x, const GF2EX& a, long n); 
GF2EX RightShift(const GF2EX& a, long n); 



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

                                  Division

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

// operator notation:

GF2EX operator/(const GF2EX& a, const GF2EX& b);
GF2EX operator/(const GF2EX& a, const GF2E& b);
GF2EX operator/(const GF2EX& a, GF2 b);
GF2EX operator/(const GF2EX& a, long b);

GF2EX operator%(const GF2EX& a, const GF2EX& b);

GF2EX& operator/=(GF2EX& x, const GF2EX& a);
GF2EX& operator/=(GF2EX& x, const GF2E& a);
GF2EX& operator/=(GF2EX& x, GF2 a);
GF2EX& operator/=(GF2EX& x, long a);

GF2EX& operator%=(GF2EX& x, const GF2EX& a);

// procedural versions:


void DivRem(GF2EX& q, GF2EX& r, const GF2EX& a, const GF2EX& b);
// q = a/b, r = a%b

void div(GF2EX& q, const GF2EX& a, const GF2EX& b);
void div(GF2EX& q, const GF2EX& a, const GF2E& b);
void div(GF2EX& q, const GF2EX& a, GF2 b);
void div(GF2EX& q, const GF2EX& a, long b);
// q = a/b

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

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

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


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

                                   GCD's

These routines are intended for use when GF2E is a field.

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


void GCD(GF2EX& x, const GF2EX& a, const GF2EX& b);
GF2EX GCD(const GF2EX& a, const GF2EX& b); 
// x = GCD(a, b),  x is always monic (or zero if a==b==0).


void XGCD(GF2EX& d, GF2EX& s, GF2EX& t, const GF2EX& a, const GF2EX& b);
// d = gcd(a,b), a s + b t = d 


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

                                  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 polynomials of degree < GF2E::degree() and
a_n not zero (the zero polynomial is [ ]).  On input, the coefficients
are arbitrary polynomials which are reduced modulo GF2E::modulus(), and leading
zeros stripped.

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

istream& operator>>(istream& s, GF2EX& x);
ostream& operator<<(ostream& s, const GF2EX& a);


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

                              Some utility routines

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

long deg(const GF2EX& a);  // return deg(a); deg(0) == -1.

const GF2E& coeff(const GF2EX& a, long i);
// returns a read-only reference to the coefficient of X^i, or zero if
// i not in range

const GF2E& LeadCoeff(const GF2EX& a);
// read-only reference to leading term of a, or zero if a == 0

const GF2E& ConstTerm(const GF2EX& a);
// read-only reference to constant term of a, or zero if a == 0

void SetCoeff(GF2EX& x, long i, const GF2E& a);
void SetCoeff(GF2EX& x, long i, GF2 a);
void SetCoeff(GF2EX& x, long i, long a);
// makes coefficient of X^i equal to a;  error is raised if i < 0

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

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

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

void diff(GF2EX& x, const GF2EX& a); // x = derivative of a
GF2EX diff(const GF2EX& a); 

void MakeMonic(GF2EX& x); 
// if x != 0 makes x into its monic associate; LeadCoeff(x) must be
// invertible in this case

void reverse(GF2EX& x, const GF2EX& a, long hi);
GF2EX reverse(const GF2EX& a, long hi);

void reverse(GF2EX& x, const GF2EX& a);
GF2EX reverse(const GF2EX& a);

// x = reverse of a[0]..a[hi] (hi >= -1);
// hi defaults to deg(a) in second version

void VectorCopy(vec_GF2E& x, const GF2EX& a, long n);
vec_GF2E VectorCopy(const GF2EX& 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(GF2EX& x, long n);
GF2EX random_GF2EX(long n);
// x = random polynomial of degree < n 


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

                    Polynomial Evaluation and related problems

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


void BuildFromRoots(GF2EX& x, const vec_GF2E& a);
GF2EX BuildFromRoots(const vec_GF2E& a);
// computes the polynomial (X-a[0]) ... (X-a[n-1]), where n = a.length()

void eval(GF2E& b, const GF2EX& f, const GF2E& a);
GF2E eval(const GF2EX& f, const GF2E& a);
// b = f(a)

void eval(GF2E& b, const GF2X& f, const GF2E& a);
GF2E eval(const GF2EX& f, const GF2E& a);
// b = f(a); uses ModComp algorithm for GF2X

void eval(vec_GF2E& b, const GF2EX& f, const vec_GF2E& a);
vec_GF2E eval(const GF2EX& f, const vec_GF2E& a);
//  b.SetLength(a.length()); b[i] = f(a[i]) for 0 <= i < a.length()

void interpolate(GF2EX& f, const vec_GF2E& a, const vec_GF2E& b);
GF2EX interpolate(const vec_GF2E& a, const vec_GF2E& b);
// interpolates the polynomial f satisfying f(a[i]) = b[i].  

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

                       Arithmetic mod X^n

Required: n >= 0; otherwise, an error is raised.

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

void trunc(GF2EX& x, const GF2EX& a, long n); // x = a % X^n
GF2EX trunc(const GF2EX& a, long n); 

void MulTrunc(GF2EX& x, const GF2EX& a, const GF2EX& b, long n);
GF2EX MulTrunc(const GF2EX& a, const GF2EX& b, long n);
// x = a * b % X^n

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

void InvTrunc(GF2EX& x, const GF2EX& a, long n);
GF2EX InvTrunc(GF2EX& x, const GF2EX& 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), and
deg(f) > 0.


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

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

void MulMod(GF2EX& x, const GF2EX& a, const GF2EX& b, const GF2EX& f);
GF2EX MulMod(const GF2EX& a, const GF2EX& b, const GF2EX& f);
// x = (a * b) % f

void SqrMod(GF2EX& x, const GF2EX& a, const GF2EX& f);
GF2EX SqrMod(const GF2EX& a, const GF2EX& f);
// x = a^2 % f

void MulByXMod(GF2EX& x, const GF2EX& a, const GF2EX& f);
GF2EX MulByXMod(const GF2EX& a, const GF2EX& f);
// x = (a * X) mod f

void InvMod(GF2EX& x, const GF2EX& a, const GF2EX& f);
GF2EX InvMod(const GF2EX& a, const GF2EX& f);
// x = a^{-1} % f, error is a is not invertible

long InvModStatus(GF2EX& x, const GF2EX& a, const GF2EX& 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-Conditioning

If you need to do a lot of arithmetic modulo a fixed f, build
GF2EXModulus F for f.  This pre-computes information about f that
speeds up subsequent computations.

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

#include <NTL/GF2EX.h>

void product(GF2EX& x, const vec_GF2EX& v, const GF2EX& f)
{
   GF2EXModulus F(f);
   GF2EX 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 + -