Skip to content

Polynomial Root

PolynomialRoot(value, multiplicity=1) dataclass

Defines a polynomial root with value and multiplicity.

is_real property

Check if the root is real.

real property

Return the real part of the root.

imag property

Return the imaginary part of the root.

monic_polynomial()

Return the monic polynomial for the root.

Source code in src/rational_functions/roots.py
def monic_polynomial(self) -> Polynomial:
    """Return the monic polynomial for the root."""
    return Polynomial([-self.value, 1.0]) ** self.multiplicity

with_multiplicity(multiplicity)

Return a new PolynomialRoot with a different multiplicity.

Source code in src/rational_functions/roots.py
def with_multiplicity(self, multiplicity: int) -> "PolynomialRoot":
    """Return a new PolynomialRoot with a different multiplicity."""
    return PolynomialRoot(
        value=self.value,
        multiplicity=multiplicity,
    )

is_equivalent(root)

Check if two roots are equivalent, multiplicity aside.

Source code in src/rational_functions/roots.py
def is_equivalent(self, root: "PolynomialRoot") -> bool:
    """Check if two roots are equivalent, multiplicity aside."""

    return self.value == root.value

highest(root)

Return the root with the highest multiplicity between two equivalent roots.

Parameters:

Name Type Description Default
root PolynomialRoot

Other root to compare with.

required

Returns:

Name Type Description
PolynomialRoot PolynomialRoot

Root with the highest multiplicity.

Raises:

Type Description
AssertionError

If the roots are not equivalent.

Source code in src/rational_functions/roots.py
def highest(self, root: "PolynomialRoot") -> "PolynomialRoot":
    """Return the root with the highest multiplicity between
    two equivalent roots.

    Args:
        root (PolynomialRoot): Other root to compare with.

    Returns:
        PolynomialRoot: Root with the highest multiplicity.

    Raises:
        AssertionError: If the roots are not equivalent.
    """

    assert self.is_equivalent(root), "Roots are not equivalent."

    return PolynomialRoot(
        value=self.value,
        multiplicity=max(self.multiplicity, root.multiplicity),
    )

__hash__()

Return a hash of the root.

Source code in src/rational_functions/roots.py
def __hash__(self) -> int:
    """Return a hash of the root."""
    return hash((self.value, self.multiplicity))