GF2++
Loading...
Searching...
No Matches
gf2::BitPoly< Word >

A BitPoly represents a polynomial over GF(2) where we store the polynomial coefficients in a bit-vector.
The template parameter Word sets the unsigned word type used by the BitVec that stores the coefficients. More...

#include <BitPoly.h>

Public Types

using coeffs_type = BitVec<Word>
 The type used to store the bit-polynomial coefficients.
using word_type = Word
 The underlying unsigned word type used to store the bits.

Public Member Functions

Constructors:
constexpr BitPoly ()
 The default constructor creates an empty bit-polynomial with no coefficients.
constexpr BitPoly (const BitVec< Word > &coeffs)
 Constructs a bit-polynomial with the given coefficients by copying them.
constexpr BitPoly (BitVec< Word > &&coeffs)
 Constructs a bit-polynomial with the given coefficients by moving them.
Simple Queries:
constexpr usize degree () const
 Returns the degree of the bit-polynomial.
constexpr usize size () const
 Returns the "size" of the bit-polynomial which is its total number of coefficients.
constexpr bool is_zero () const
 Returns true if the bit-polynomial is some form of the zero BitPoly p(x) := 0.
constexpr bool is_non_zero () const
 Returns true if the bit-polynomial is non-zero.
constexpr bool is_one () const
 Returns true if the bit-polynomial is p(x) := 1.
constexpr bool is_constant () const
 Returns true if the bit-polynomial is either p(x) := 0 or 1.
constexpr bool is_monic () const
 Returns true if the bit-polynomial is monic, i.e., no trailing zero coefficients.
constexpr bool is_empty () const
 Returns true if the bit-polynomial is empty, i.e., has no coefficients.
Coefficient Access:
constexpr bool operator[] (usize i) const
 Returns the coefficient of \(x^i\) in the bit-polynomial as a boolean.
constexpr BitRef< coeffs_typeoperator[] (usize i)
 Returns the coefficient of \(x^i\) in the bit-polynomial as a BitRef.
constexpr const coeffs_typecoefficients () const
 Read-only access to all the coefficients of the bit-polynomial.
constexpr coeffs_typecoefficients ()
 Read-write access to all the coefficients of the bit-polynomial.
constexpr void copy_coefficients (const coeffs_type &coeffs)
 Set the bit-polynomial coefficients by copying them from a pre-filled bit-vector.
constexpr void move_coefficients (coeffs_type &&coeffs)
 Set the BitPoly coefficients by moving a bit-vector of coefficients into place.
Resizing:
constexpr BitPolyclear ()
 Clears the BitPoly, i.e., sets it to the zero BitPoly & returns self.
constexpr BitPolyresize (usize n)
 Resizes the BitPoly to have the n coefficients and returns self.
constexpr BitPolyshrink_to_fit ()
 Shrinks the BitPoly to have the minimum number of coefficients and returns self.
constexpr BitPolymake_monic ()
 Kills any trailing zero coefficients, so e.g. p(x) := 0*x^4 + x^2 + x becomes p(x) := x^2 + x.
Arithmetic Operations:
constexpr BitPolyoperator+= (const BitPoly &rhs)
 In-place addition with another bit-polynomial, returning a reference to the result.
constexpr BitPolyoperator-= (const BitPoly &rhs)
 In-place subtraction with another bit-polynomial, returning a reference to the result.
constexpr BitPolyoperator*= (const BitPoly &rhs)
 In-place multiplication with another bit-polynomial, returning a reference to the result.
constexpr BitPoly< Word > operator+ (const BitPoly< Word > &rhs) const
 Returns the sum of two bit-polynomials.
constexpr BitPoly< Word > operator- (const BitPoly< Word > &rhs) const
 Returns the difference of two bit-polynomials.
constexpr BitPoly< Word > operator* (const BitPoly< Word > &rhs) const
 Returns the product of two bit-polynomials.
Specialised Arithmetic Operations:
constexpr void square_into (BitPoly &dst) const
 Fills dst with the square of this bit-polynomial.
constexpr BitPoly squared () const
 Returns a new BitPoly that is the square of this BitPoly.
constexpr BitPolytimes_x_to_the (usize n)
 Multiplies the BitPoly by x^n and returns self.
Polynomial Evaluation:
constexpr bool operator() (bool x) const
 Evaluates the BitPoly at the boolean scalar point x and returns the result.
constexpr BitMat< Word > operator() (const BitMat< Word > &M) const
 Evaluates the bit-polynomial for a square gf2::BitMat argument.
Modular Reduction:
BitPoly reduce_x_to_the (usize n, bool n_is_log2=false) const
 If this polynomial is P(x) we return x^e mod P(x) for e = n or e = 2^n depending on the n_is_log2 argument.
String Representations:
std::string to_string (std::string_view var="x") const
 Returns a readable string representation of the bit-polynomial p(x) = p0 + p1x + ....
std::string to_full_string (std::string_view var="x") const
 Returns a readable full string representation of the bit-polynomial.
std::ostream & operator<< (std::ostream &s) const
 The usual output stream operator for a bit-polynomial.

Static Public Member Functions

Factory Constructors:
static constexpr BitPoly zero ()
 Factory method to return the "zero" bit-polynomial p(x) := 0.
static constexpr BitPoly one ()
 Factory method to return the "one" bit-polynomial p(x) := 1.
static constexpr BitPoly constant (bool val)
 Factory method to return the constant bit-polynomial p(x) := val where val is a boolean.
static constexpr BitPoly zeros (usize n)
 Factory method to return a bit-polynomial with n + 1 coefficients, all initialized to zero.
static constexpr BitPoly ones (usize n)
 Factory method to return a monic bit-polynomial of degree n with n + 1 coefficients, all ones.
static constexpr BitPoly x_to_the (usize n)
 Factory method to return the bit-polynomial p(x) := x^n.
static constexpr BitPoly from (usize n, std::invocable< usize > auto f)
 Factory method to return a new bit-polynomial of degree n with coefficients set by calling the function f to set each coefficient to either 0 or 1.
Random Polynomial Constructors:
static constexpr BitPoly random (usize n)
 Factory method to return a new bit-polynomial of degree n with n + 1 coefficients picked uniformly at random.
static constexpr BitPoly seeded_random (usize n, std::uint64_t seed)
 Factory method to return a new bit-polynomial of degree n with n + 1 coefficients picked uniformly at random. The random coefficients are from independent fair coin flips.

Friends

Equality Operator:
constexpr bool operator== (const BitPoly &lhs, const BitPoly &rhs)
 Equality operator checks that any pair of bit-polynomials are equal in content.

Detailed Description

template<unsigned_word Word = usize>
class gf2::BitPoly< Word >

A BitPoly represents a polynomial over GF(2) where we store the polynomial coefficients in a bit-vector.
The template parameter Word sets the unsigned word type used by the BitVec that stores the coefficients.

Example

auto p = BitPoly<>::zeros(3);
p[0] = true;
p[1] = false;
p[2] = true;
assert_eq(p.to_string(), "1 + x^2");
static constexpr BitPoly zeros(usize n)
Factory method to return a bit-polynomial with n + 1 coefficients, all initialized to zero.
Definition BitPoly.h:120

Constructor & Destructor Documentation

◆ BitPoly() [1/3]

template<unsigned_word Word = usize>
gf2::BitPoly< Word >::BitPoly ( )
inlineconstexpr

The default constructor creates an empty bit-polynomial with no coefficients.

This is one possible form of the zero BitPoly.

Example

assert_eq(p.to_string(), "0");
std::string to_string(std::string_view var="x") const
Returns a readable string representation of the bit-polynomial p(x) = p0 + p1x + ....
Definition BitPoly.h:801
constexpr BitPoly()
The default constructor creates an empty bit-polynomial with no coefficients.
Definition BitPoly.h:54

◆ BitPoly() [2/3]

template<unsigned_word Word = usize>
gf2::BitPoly< Word >::BitPoly ( const BitVec< Word > & coeffs)
inlineconstexpr

Constructs a bit-polynomial with the given coefficients by copying them.

Example

assert_eq(p.to_string(), "1 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8 + x^9");
static constexpr BitVec ones(usize n)
Factory method to generate a bit-vector of length n where the elements are all 1.
Definition BitVec.h:220

◆ BitPoly() [3/3]

template<unsigned_word Word = usize>
gf2::BitPoly< Word >::BitPoly ( BitVec< Word > && coeffs)
inlineconstexpr

Constructs a bit-polynomial with the given coefficients by moving them.

Use std::move(coeffs)) in the constructor argument to get this version.

Note
The input coefficient bit-vector is moved into the bit-polynomial so it is no longer valid after this constructor.

Example

auto coeffs = BitVec<>::ones(10);
BitPoly p{std::move(coeffs)};
assert_eq(p.to_string(), "1 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8 + x^9");

Member Function Documentation

◆ clear()

template<unsigned_word Word = usize>
BitPoly & gf2::BitPoly< Word >::clear ( )
inlineconstexpr

Clears the BitPoly, i.e., sets it to the zero BitPoly & returns self.

Example

auto p = BitPoly<>::x_to_the(3);
assert_eq(p.to_string(), "x^3");
p.clear();
assert_eq(p.to_string(), "0");
static constexpr BitPoly x_to_the(usize n)
Factory method to return the bit-polynomial p(x) := x^n.
Definition BitPoly.h:140

◆ coefficients() [1/2]

template<unsigned_word Word = usize>
coeffs_type & gf2::BitPoly< Word >::coefficients ( )
inlineconstexpr

Read-write access to all the coefficients of the bit-polynomial.

This returns the coefficients as a mutable reference to a bit-vector.

Example

auto p = BitPoly<>::zeros(3);
assert_eq(p.to_string(), "0");
auto& c = p.coefficients();
c.set_all();
assert_eq(p.to_string(), "1 + x + x^2 + x^3");

◆ coefficients() [2/2]

template<unsigned_word Word = usize>
const coeffs_type & gf2::BitPoly< Word >::coefficients ( ) const
inlineconstexpr

Read-only access to all the coefficients of the bit-polynomial.

This returns the coefficients as an immutable reference to a bit-vector.

Example

auto p = BitPoly<>::ones(3);
auto c = p.coefficients();
assert_eq(c.to_string(), "1111");
static constexpr BitPoly ones(usize n)
Factory method to return a monic bit-polynomial of degree n with n + 1 coefficients,...
Definition BitPoly.h:131

◆ constant()

template<unsigned_word Word = usize>
constexpr BitPoly gf2::BitPoly< Word >::constant ( bool val)
inlinestaticconstexpr

Factory method to return the constant bit-polynomial p(x) := val where val is a boolean.

Example

auto p = BitPoly<>::constant(true);
assert_eq(p.to_string(), "1");
static constexpr BitPoly constant(bool val)
Factory method to return the constant bit-polynomial p(x) := val where val is a boolean.
Definition BitPoly.h:109

◆ copy_coefficients()

template<unsigned_word Word = usize>
void gf2::BitPoly< Word >::copy_coefficients ( const coeffs_type & coeffs)
inlineconstexpr

Set the bit-polynomial coefficients by copying them from a pre-filled bit-vector.

Example

assert_eq(p.to_string(), "0");
auto c = BitVec<>::ones(3);
assert_eq(c.to_string(), "111");
assert_eq(p.to_string(), "1 + x + x^2");
constexpr void copy_coefficients(const coeffs_type &coeffs)
Set the bit-polynomial coefficients by copying them from a pre-filled bit-vector.
Definition BitPoly.h:309

◆ degree()

template<unsigned_word Word = usize>
usize gf2::BitPoly< Word >::degree ( ) const
inlineconstexpr

Returns the degree of the bit-polynomial.

The degree of the bit-polynomial is the highest power of x with a non-zero coefficient.

Note
We return 0 for the zero polynomial.

Example

auto coeffs = BitVec<>::from_string("10101000").value();
BitPoly p{coeffs};
assert_eq(p.degree(), 4);
assert_eq(p.size(), 8);
constexpr usize size() const
Returns the "size" of the bit-polynomial which is its total number of coefficients.
Definition BitPoly.h:224
constexpr usize degree() const
Returns the degree of the bit-polynomial.
Definition BitPoly.h:211
static std::optional< BitVec > from_string(std::string_view sv)
Factory method to construct a bit-vector from a string s, returning std::nullopt on failure.
Definition BitVec.h:402

◆ from()

template<unsigned_word Word = usize>
constexpr BitPoly gf2::BitPoly< Word >::from ( usize n,
std::invocable< usize > auto f )
inlinestaticconstexpr

Factory method to return a new bit-polynomial of degree n with coefficients set by calling the function f to set each coefficient to either 0 or 1.

Example

auto p = BitPoly<>::from(10, [](usize i) { return i % 2 == 0; });
assert_eq(p.to_string(), "1 + x^2 + x^4 + x^6 + x^8 + x^10");
static constexpr BitPoly from(usize n, std::invocable< usize > auto f)
Factory method to return a new bit-polynomial of degree n with coefficients set by calling the functi...
Definition BitPoly.h:150
std::size_t usize
Word type alias for the platform's "native"-sized unsigned integer.
Definition unsigned_word.h:41

◆ is_empty()

template<unsigned_word Word = usize>
bool gf2::BitPoly< Word >::is_empty ( ) const
inlineconstexpr

Returns true if the bit-polynomial is empty, i.e., has no coefficients.

A bit-polynomial with no coefficients is treated as a form of p(x) := 0.

◆ make_monic()

template<unsigned_word Word = usize>
BitPoly & gf2::BitPoly< Word >::make_monic ( )
inlineconstexpr

Kills any trailing zero coefficients, so e.g. p(x) := 0*x^4 + x^2 + x becomes p(x) := x^2 + x.

Note

Does nothing to any form of the zero BitPoly.

Example

auto coeffs = BitVec<>::from_string("101010").value();
BitPoly p{coeffs};
assert_eq(p.is_monic(), false);
assert_eq(p.is_monic(), true);
constexpr BitPoly & make_monic()
Kills any trailing zero coefficients, so e.g. p(x) := 0*x^4 + x^2 + x becomes p(x) := x^2 + x.
Definition BitPoly.h:395
constexpr bool is_monic() const
Returns true if the bit-polynomial is monic, i.e., no trailing zero coefficients.
Definition BitPoly.h:239

◆ move_coefficients()

template<unsigned_word Word = usize>
void gf2::BitPoly< Word >::move_coefficients ( coeffs_type && coeffs)
inlineconstexpr

Set the BitPoly coefficients by moving a bit-vector of coefficients into place.

Use std::move(coeffs) in the argument to get this version of copy_coefficients.

Note
After the call the argument bit-vector is no longer valid.

Example

assert_eq(p.to_string(), "0");
auto coeffs = BitVec<>::ones(3);
p.copy_coefficients(std::move(coeffs));
assert_eq(p.to_string(), "1 + x + x^2");

◆ one()

template<unsigned_word Word = usize>
constexpr BitPoly gf2::BitPoly< Word >::one ( )
inlinestaticconstexpr

Factory method to return the "one" bit-polynomial p(x) := 1.

Example

auto p = BitPoly<>::one();
assert_eq(p.to_string(), "1");
static constexpr BitPoly one()
Factory method to return the "one" bit-polynomial p(x) := 1.
Definition BitPoly.h:100

◆ ones()

template<unsigned_word Word = usize>
constexpr BitPoly gf2::BitPoly< Word >::ones ( usize n)
inlinestaticconstexpr

Factory method to return a monic bit-polynomial of degree n with n + 1 coefficients, all ones.

This is the BitPoly x^n + x^(n-1) + ... + x + 1.

Example

auto p = BitPoly<>::ones(4);
assert_eq(p.to_string(), "1 + x + x^2 + x^3 + x^4");

◆ operator()() [1/2]

template<unsigned_word Word = usize>
bool gf2::BitPoly< Word >::operator() ( bool x) const
inlineconstexpr

Evaluates the BitPoly at the boolean scalar point x and returns the result.

Example

auto p = BitPoly<>::x_to_the(3);
assert_eq(p(true), true);
assert_eq(p(false), false);

◆ operator()() [2/2]

template<unsigned_word Word = usize>
BitMat< Word > gf2::BitPoly< Word >::operator() ( const BitMat< Word > & M) const
inlineconstexpr

Evaluates the bit-polynomial for a square gf2::BitMat argument.

Uses Horner's method to evaluate p(M) where M is a square matrix and returns the result as a bit-matrix.

Note
This method panics if the matrix is not square.

Example

auto m = BitMat<>::identity(6);
assert_eq(p1(m), BitMat<>::zeros(6, 6));
assert_eq(p2(m), BitMat<>::identity(6));
static constexpr BitMat zeros(usize m, usize n)
Factory method to create the m x n zero bit-matrix with all the elements set to 0.
Definition BitMat.h:124
static constexpr BitMat identity(usize m)
Factory method to create the m x m identity bit-matrix.
Definition BitMat.h:376
A BitPoly represents a polynomial over GF(2) where we store the polynomial coefficients in a bit-vect...
Definition BitPoly.h:31
static constexpr BitVec alternating(usize n)
Factory method to generate a bit-vector of length n looking like 101010....
Definition BitVec.h:255

◆ operator*()

template<unsigned_word Word = usize>
BitPoly< Word > gf2::BitPoly< Word >::operator* ( const BitPoly< Word > & rhs) const
inlineconstexpr

Returns the product of two bit-polynomials.

Example

auto p = BitPoly<>::x_to_the(3);
auto q = BitPoly<>::x_to_the(2);
auto r = p * q;
assert_eq(r.to_string(), "x^5");

◆ operator*=()

template<unsigned_word Word = usize>
BitPoly & gf2::BitPoly< Word >::operator*= ( const BitPoly< Word > & rhs)
inlineconstexpr

In-place multiplication with another bit-polynomial, returning a reference to the result.

Note
This method uses the convolution method for bit-vectors under the hood.

Example

auto p = BitPoly<>::ones(1);
assert_eq(p.to_string(), "1 + x");
auto q = BitPoly<>::ones(2);
assert_eq(q.to_string(), "1 + x + x^2");
p *= q;
assert_eq(p.to_string(), "1 + x^3");

◆ operator+()

template<unsigned_word Word = usize>
BitPoly< Word > gf2::BitPoly< Word >::operator+ ( const BitPoly< Word > & rhs) const
inlineconstexpr

Returns the sum of two bit-polynomials.

Example

auto p = BitPoly<>::x_to_the(3);
auto q = BitPoly<>::x_to_the(2);
auto r = p + q;
assert_eq(r.to_string(), "x^2 + x^3");

◆ operator+=()

template<unsigned_word Word = usize>
BitPoly & gf2::BitPoly< Word >::operator+= ( const BitPoly< Word > & rhs)
inlineconstexpr

In-place addition with another bit-polynomial, returning a reference to the result.

Example

auto p = BitPoly<>::x_to_the(2);
auto q = BitPoly<>::x_to_the(3);
p += q;
assert_eq(p.to_string(), "x^2 + x^3");

◆ operator-()

template<unsigned_word Word = usize>
BitPoly< Word > gf2::BitPoly< Word >::operator- ( const BitPoly< Word > & rhs) const
inlineconstexpr

Returns the difference of two bit-polynomials.

Note
In GF(2) subtraction is the same as addition.

Example

auto p = BitPoly<>::x_to_the(3);
auto q = BitPoly<>::x_to_the(2);
auto r = p - q;
assert_eq(r.to_string(), "x^2 + x^3");

◆ operator-=()

template<unsigned_word Word = usize>
BitPoly & gf2::BitPoly< Word >::operator-= ( const BitPoly< Word > & rhs)
inlineconstexpr

In-place subtraction with another bit-polynomial, returning a reference to the result.

Note
In GF(2) subtraction is the same as addition.

Example

auto p = BitPoly<>::x_to_the(2);
auto q = BitPoly<>::x_to_the(3);
p -= q;
assert_eq(p.to_string(), "x^2 + x^3");

◆ operator[]() [1/2]

template<unsigned_word Word = usize>
BitRef< coeffs_type > gf2::BitPoly< Word >::operator[] ( usize i)
inlineconstexpr

Returns the coefficient of \(x^i\) in the bit-polynomial as a BitRef.

This allows us to set the coefficient of \(x^i\) in the bit-polynomial via the = operator.

Example

auto p = BitPoly<>::zeros(3);
assert_eq(p.to_string(), "0");
p[2] = true;
assert_eq(p.to_string(), "x^2");

◆ operator[]() [2/2]

template<unsigned_word Word = usize>
bool gf2::BitPoly< Word >::operator[] ( usize i) const
inlineconstexpr

Returns the coefficient of \(x^i\) in the bit-polynomial as a boolean.

Example

auto p = BitPoly<>::zeros(3);
assert_eq(p[2], false);

◆ random()

template<unsigned_word Word = usize>
constexpr BitPoly gf2::BitPoly< Word >::random ( usize n)
inlinestaticconstexpr

Factory method to return a new bit-polynomial of degree n with n + 1 coefficients picked uniformly at random.

The random coefficients are from independent fair coin flips seeded with entropy.

Note
If n > 0 then the returned BitPoly is monic.

◆ reduce_x_to_the()

template<unsigned_word Word = usize>
BitPoly gf2::BitPoly< Word >::reduce_x_to_the ( usize n,
bool n_is_log2 = false ) const
inline

If this polynomial is P(x) we return x^e mod P(x) for e = n or e = 2^n depending on the n_is_log2 argument.

In general, we can write any polynomial h(x) as h(x) = q(x) * P(x) + r(x) where degree(r) < degree(P). q(x) is the quotient polynomial and r(x)the remainder polynomial that is returned by this method.

Here we consider h(x) = x^e where the exponent e is either n or 2^n depending on the final argument. Setting n_is_log2 = true allows for enormous powers of x which is useful for some applications. The default is n_is_log2 = false so we return x^n mod P(x).

Note
This method panics if the polynomial is the zero polynomial.

Example

auto p = BitPoly<>::x_to_the(3);
auto r = p.reduce_x_to_the(2);
assert_eq(r.to_string(), "x^2");

◆ resize()

template<unsigned_word Word = usize>
BitPoly & gf2::BitPoly< Word >::resize ( usize n)
inlineconstexpr

Resizes the BitPoly to have the n coefficients and returns self.

Note

If n > self.size() then the BitPoly is padded with zero coefficients. If n < self.size() then the BitPoly is truncated which can change the degree of the BitPoly.

Example

auto coeffs = BitVec<>::from_string("111010").value();
assert_eq(coeffs.to_string(), "111010");
BitPoly p{coeffs};
assert_eq(p.to_string(), "1 + x + x^2 + x^4");
assert_eq(p.to_full_string(), "1 + x + x^2 + 0x^3 + x^4 + 0x^5");
p.resize(2);
assert_eq(p.to_string(), "1 + x");
p.resize(4);
assert_eq(p.to_full_string(), "1 + x + 0x^2 + 0x^3");
constexpr BitPoly & resize(usize n)
Resizes the BitPoly to have the n coefficients and returns self.
Definition BitPoly.h:363
std::string to_full_string(std::string_view var="x") const
Returns a readable full string representation of the bit-polynomial.
Definition BitPoly.h:839

◆ seeded_random()

template<unsigned_word Word = usize>
constexpr BitPoly gf2::BitPoly< Word >::seeded_random ( usize n,
std::uint64_t seed )
inlinestaticconstexpr

Factory method to return a new bit-polynomial of degree n with n + 1 coefficients picked uniformly at random. The random coefficients are from independent fair coin flips.

For reproducibility, the underlying random number generator is seeded with the specified seed.

Example

std::uint64_t seed = 42;
auto p1 = BitPoly<>::seeded_random(3311, seed);
auto p2 = BitPoly<>::seeded_random(3311, seed);
assert_eq(p1, p2, "BitPolys with the same seed should be equal");
static constexpr BitPoly seeded_random(usize n, std::uint64_t seed)
Factory method to return a new bit-polynomial of degree n with n + 1 coefficients picked uniformly at...
Definition BitPoly.h:185

◆ shrink_to_fit()

template<unsigned_word Word = usize>
BitPoly & gf2::BitPoly< Word >::shrink_to_fit ( )
inlineconstexpr

Shrinks the BitPoly to have the minimum number of coefficients and returns self.

Example

auto p = BitPoly<>::x_to_the(3);
assert_eq(p.to_string(), "x^3");
p.shrink_to_fit();
assert_eq(p.to_string(), "x^3");

◆ size()

template<unsigned_word Word = usize>
usize gf2::BitPoly< Word >::size ( ) const
inlineconstexpr

Returns the "size" of the bit-polynomial which is its total number of coefficients.

Note
Contrast this to the degree() method which ignores trailing zero coefficients.

Example

auto coeffs = BitVec<>::from_string("10101000").value();
BitPoly p{coeffs};
assert_eq(p.degree(), 4);
assert_eq(p.size(), 8);

◆ square_into()

template<unsigned_word Word = usize>
void gf2::BitPoly< Word >::square_into ( BitPoly< Word > & dst) const
inlineconstexpr

Fills dst with the square of this bit-polynomial.

Note
This is more efficient than multiplying two BitPolys for this special case.

Example

auto coeffs = BitVec<>::from_string("111").value();
BitPoly p{coeffs};
assert_eq(p.to_string(), "1 + x + x^2");
assert_eq(q.to_string(), "1 + x^2 + x^4");
constexpr void square_into(BitPoly &dst) const
Fills dst with the square of this bit-polynomial.
Definition BitPoly.h:547

◆ squared()

template<unsigned_word Word = usize>
BitPoly gf2::BitPoly< Word >::squared ( ) const
inlineconstexpr

Returns a new BitPoly that is the square of this BitPoly.

Note
This is more efficient than multiplying two BitPolys for this special case.

Example

auto p = BitPoly<>::x_to_the(3);
assert_eq(p.to_string(), "x^3");
auto q = p.squared();
assert_eq(q.to_string(), "x^6");

◆ times_x_to_the()

template<unsigned_word Word = usize>
BitPoly & gf2::BitPoly< Word >::times_x_to_the ( usize n)
inlineconstexpr

Multiplies the BitPoly by x^n and returns self.

Note
This is faster than general multiplication for this special, common case.

Example

auto p = BitPoly<>::x_to_the(3);
assert_eq(p.to_string(), "x^3");
p.times_x_to_the(2);
assert_eq(p.to_string(), "x^5");

◆ to_full_string()

template<unsigned_word Word = usize>
std::string gf2::BitPoly< Word >::to_full_string ( std::string_view var = "x") const
inline

Returns a readable full string representation of the bit-polynomial.

You can specify the variable name using the var parameter. The default variable name is x.

Note
We show all terms including those with zero coefficients.

Example

auto coeffs = BitVec<>::from_string("101010").value();
BitPoly p{coeffs};
assert_eq(p.to_full_string("M"), "1 + 0M + M^2 + 0M^3 + M^4 + 0M^5");

◆ to_string()

template<unsigned_word Word = usize>
std::string gf2::BitPoly< Word >::to_string ( std::string_view var = "x") const
inline

Returns a readable string representation of the bit-polynomial p(x) = p0 + p1x + ....

You can specify the variable name using the var parameter. The default variable name is x.

Note
We do not show any terms with zero coefficients.

Example

auto coeffs = BitVec<>::from_string("101010").value();
BitPoly p{coeffs};
assert_eq(p.to_string("M"), "1 + M^2 + M^4");

◆ x_to_the()

template<unsigned_word Word = usize>
constexpr BitPoly gf2::BitPoly< Word >::x_to_the ( usize n)
inlinestaticconstexpr

Factory method to return the bit-polynomial p(x) := x^n.

Example

auto p = BitPoly<>::x_to_the(3);
assert_eq(p.to_string(), "x^3");

◆ zero()

template<unsigned_word Word = usize>
constexpr BitPoly gf2::BitPoly< Word >::zero ( )
inlinestaticconstexpr

Factory method to return the "zero" bit-polynomial p(x) := 0.

Example

auto p = BitPoly<>::zero();
assert_eq(p.to_string(), "0");
static constexpr BitPoly zero()
Factory method to return the "zero" bit-polynomial p(x) := 0.
Definition BitPoly.h:91

◆ zeros()

template<unsigned_word Word = usize>
constexpr BitPoly gf2::BitPoly< Word >::zeros ( usize n)
inlinestaticconstexpr

Factory method to return a bit-polynomial with n + 1 coefficients, all initialized to zero.

This is the BitPoly 0*x^n + 0*x^(n-1) + ... + 0*x + 0.

Example

auto p = BitPoly<>::zeros(3);
assert_eq(p.to_full_string(), "0 + 0x + 0x^2 + 0x^3");

◆ operator==

template<unsigned_word Word = usize>
bool operator== ( const BitPoly< Word > & lhs,
const BitPoly< Word > & rhs )
friend

Equality operator checks that any pair of bit-polynomials are equal in content.

Note
We ignore any high order trailing zero coefficients in either operand so 1 + x == 1 + x + 0x^2.

Example

auto p = BitPoly<>::x_to_the(3);
auto q = BitPoly<>::zeros(1000);
q[3] = true;
assert(p == q);