Initializing Polynominals

An NTL discussion forum

Initializing Polynominals

Postby kellyw » Fri Oct 07, 2016 12:00 am

Are there any examples illustrating how to construct a particular polynomial in code? All of the examples I have seen either read from input or generate random polynomials.

I'm interested in particular in polynomials of type ZZ_pE and for example, I'd like to create polynomial 2x^2 + 3x + 1.

I start by initializing the modulus to a irreducible polynomial.

I then construct a polynomial of type ZZ_pX:
ZZ_pX ff;
SetCoeff(ff, 0, 1);
SetCoeff(ff, 1, 3);
SetCoeff(ff, 2, 2);

and then use it to initialize a polynomial of type ZZ_pE:
ZZ_pE f;
conv(f, ff);

This works, but seems very round-about. Is there any easier/more direct way of constructing such a polynomial?

Cheers, Wayne.
Posts: 1
Joined: Thu Oct 06, 2016 11:39 pm

Re: Initializing Polynominals

Postby victorshoup » Fri Oct 07, 2016 12:55 pm

Navigate to the relevant doc page, in this case:

Scroll down a few lines, and voila:


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) >= 0 => f.rep[deg(f)] != 0.

Site Admin
Posts: 32
Joined: Mon Jan 13, 2014 3:18 am

Re: Initializing Polynominals

Postby victorshoup » Fri Jul 07, 2017 3:18 pm

I added a more convenient initialization.
Noe you can write ZZX x = conv<ZZX>("[1 2 3]"), which gives you the polynomial 1 + 2*X + 3*X^2
Site Admin
Posts: 32
Joined: Mon Jan 13, 2014 3:18 am

Return to NTL

Who is online

Users browsing this forum: No registered users and 1 guest