classmethod galois.Poly.Str(string: str, field: Type[Array] | None = None) Poly

Constructs a polynomial over \(\mathrm{GF}(p^m)\) from its string representation.

Str() and __str__() are inverse operations.

Parameters
string: str

The string representation of the polynomial \(f(x)\).

field: Type[Array] | None = None

The Galois field \(\mathrm{GF}(p^m)\) the polynomial is over. The default is None which corresponds to GF2.

Returns

The polynomial \(f(x)\).

Notes

The string parsing rules include:

  • Either ^ or ** may be used for indicating the polynomial degrees. For example, "13x^3 + 117" or "13x**3 + 117".

  • Multiplication operators * may be used between coefficients and the polynomial indeterminate x, but are not required. For example, "13x^3 + 117" or "13*x^3 + 117".

  • Polynomial coefficients of 1 may be specified or omitted. For example, "x^3 + 117" or "1*x^3 + 117".

  • The polynomial indeterminate can be any single character, but must be consistent. For example, "13x^3 + 117" or "13y^3 + 117".

  • Spaces are not required between terms. For example, "13x^3 + 117" or "13x^3+117".

  • Any combination of the above rules is acceptable.

Examples

Construct a polynomial over \(\mathrm{GF}(2)\) from its string representation.

In [1]: f = galois.Poly.Str("x^2 + 1"); f
Out[1]: Poly(x^2 + 1, GF(2))

In [2]: str(f)
Out[2]: 'x^2 + 1'

Construct a polynomial over \(\mathrm{GF}(3^5)\) from its string representation.

In [3]: GF = galois.GF(3**5)

In [4]: f = galois.Poly.Str("13x^3 + 117", field=GF); f
Out[4]: Poly(13x^3 + 117, GF(3^5))

In [5]: str(f)
Out[5]: '13x^3 + 117'

Last update: Aug 27, 2022