Skip to content

komm.RationalPolynomial

Rational polynomial. A rational polynomial is a polynomial whose coefficients are all rational numbers. This class supports addition, subtraction, multiplication, division, and exponentiation.

Examples:

>>> poly1 = komm.RationalPolynomial(['1/2', '0', '3'])  # 1/2 + 3 X^2
>>> poly1
RationalPolynomial(['1/2', '0', '3'])
>>> poly2 = komm.RationalPolynomial(['1/3', '2/3'])  # 1/3 + (2/3) X
>>> poly2
RationalPolynomial(['1/3', '2/3'])
>>> poly1 + poly2  # 5/6 + (2/3) X + 3 X^2
RationalPolynomial(['5/6', '2/3', '3'])
>>> poly1 * poly2  # 1/6 + (1/3) X + X^2 + 2 X^3
RationalPolynomial(['1/6', '1/3', '1', '2'])

__init__()

Constructor for the class.

Parameters:

  • coefficients (Array1D[int | str | Fraction])

    The coefficients of the rational polynomial—the $i$-th element of the array standing for the coefficient of $X^i$. For example, ['1/2', '0', '3'] represents the rational polynomial $1/2 + 3 X^2$.

Examples:

>>> komm.RationalPolynomial(['1/2', '0', '3'])  # 1/2 + 3 X^2
RationalPolynomial(['1/2', '0', '3'])

monomial() classmethod

Constructs a monomial. This is an polynomial of the form $cX^d$.

Parameters:

  • degree (int)

    The degree $d$ of the monomial.

  • coefficient (Optional[int])

    The coefficient $c$ of the monomial. The default value is $1$.

Examples:

>>> komm.RationalPolynomial.monomial(4, 2)  # 2 X^4
RationalPolynomial(['0', '0', '0', '0', '2'])

coefficients()

Returns the coefficients of the polynomial.

Parameters:

  • width (Optional[int])

    If this parameter is specified, the output will be filled with zeros on the right so that the its length will be the specified value.

Returns:

  • coefficients (Array1D[int])

    Coefficients of the polynomial. The $i$-th element of the array stands for the coefficient of $X^i$.

Examples:

>>> poly = komm.RationalPolynomial(['0', '1/3', '2/3'])  # (1/3) X + (2/3) X^2
>>> poly.coefficients()
array([Fraction(0, 1), Fraction(1, 3), Fraction(2, 3)], dtype=object)
>>> poly.coefficients(width=5)
array([Fraction(0, 1), Fraction(1, 3), Fraction(2, 3), Fraction(0, 1), Fraction(0, 1)], dtype=object)

degree property

The degree of the polynomial.

Examples:

>>> poly = komm.RationalPolynomial([1, 0, 3])  # 1 + 3 X^2
>>> poly.degree
2

evaluate()

Evaluates the polynomial at a given point. Uses Horner's method.

Parameters:

  • point (RingElement)

    Any Python object supporting the operations of addition, subtraction, and multiplication.

Returns:

  • result (RingElement)

    The result of evaluating the binary polynomial at point. It has the same type as point.

Examples:

>>> poly = komm.RationalPolynomial([0, 1, 0, -1, 2])  # X - X^3 + 2 X^4
>>> poly.evaluate(7)  # same as 7 - 7**3 + 2 * 7**4
Fraction(4466, 1)
>>> point = np.array([[1, 2], [3, 4]])
>>> poly.evaluate(point)  # same as point - point**3 + 2 * point**4
array([[Fraction(2, 1), Fraction(26, 1)],
       [Fraction(138, 1), Fraction(452, 1)]], dtype=object)

gcd() classmethod

Computes the greatest common divisor (gcd) of the arguments.

lcm() classmethod

Computes the least common multiple (lcm) of the arguments.