@cacheit
def minimal_polynomial(ex, method=None, **args):
    '\n    Computes the minimal polynomial of an algebraic element.\n\n    Parameters\n    ==========\n\n    ex : algebraic element expression\n    method : str, optional\n        If ``compose``, the minimal polynomial of the subexpressions\n        of ``ex`` are computed, then the arithmetic operations on them are\n        performed using the resultant and factorization.  If ``groebner``,\n        a bottom-up algorithm, using Gröbner bases is used.\n        Defaults are determined by :func:`~diofant.config.setup`.\n    domain : Domain, optional\n        If no ground domain is given, it will be generated automatically\n        from the expression.\n\n    Examples\n    ========\n\n    >>> minimal_polynomial(sqrt(2))(x)\n    x**2 - 2\n    >>> minimal_polynomial(sqrt(2), domain=QQ.algebraic_field(sqrt(2)))(x)\n    x - sqrt(2)\n    >>> minimal_polynomial(sqrt(2) + sqrt(3))(x)\n    x**4 - 10*x**2 + 1\n    >>> minimal_polynomial(solve(x**3 + x + 3)[0][x])(x)\n    x**3 + x + 3\n    >>> minimal_polynomial(sqrt(y))(x)\n    x**2 - y\n\n    '
    if (method is None):
        method = query('minpoly_method')
    _minpoly_methods = {'compose': _minpoly_compose, 'groebner': minpoly_groebner}
    try:
        _minpoly = _minpoly_methods[method]
    except KeyError:
        raise ValueError(f"'{method}' is not a valid algorithm for computing minimal  polynomial")
    ex = sympify(ex)
    if ex.is_number:
        ex = _mexpand(ex, recursive=True)
    x = Dummy('x')
    domain = args.get('domain', (QQ.inject(*ex.free_symbols).field if ex.free_symbols else QQ))
    result = _minpoly(ex, x, domain)
    (_, factors) = factor_list(result, x, domain=domain)
    result = _choose_factor(factors, x, ex, dom=domain)
    result = result.primitive()[1]
    return PurePoly(result, x, domain=domain)
Computes the minimal polynomial of an algebraic element.

Parameters
==========

ex : algebraic element expression
method : str, optional
    If ``compose``, the minimal polynomial of the subexpressions
    of ``ex`` are computed, then the arithmetic operations on them are
    performed using the resultant and factorization.  If ``groebner``,
    a bottom-up algorithm, using Gröbner bases is used.
    Defaults are determined by :func:`~diofant.config.setup`.
domain : Domain, optional
    If no ground domain is given, it will be generated automatically
    from the expression.

Examples
========

>>> minimal_polynomial(sqrt(2))(x)
x**2 - 2
>>> minimal_polynomial(sqrt(2), domain=QQ.algebraic_field(sqrt(2)))(x)
x - sqrt(2)
>>> minimal_polynomial(sqrt(2) + sqrt(3))(x)
x**4 - 10*x**2 + 1
>>> minimal_polynomial(solve(x**3 + x + 3)[0][x])(x)
x**3 + x + 3
>>> minimal_polynomial(sqrt(y))(x)
x**2 - y
@cacheit
def minimal_polynomial(ex, method=None, **args):
    '\n    Computes the minimal polynomial of an algebraic element.\n\n    Parameters\n    ==========\n\n    ex : algebraic element expression\n    method : str, optional\n        If ``compose``, the minimal polynomial of the subexpressions\n        of ``ex`` are computed, then the arithmetic operations on them are\n        performed using the resultant and factorization.  If ``groebner``,\n        a bottom-up algorithm, using Gröbner bases is used.\n        Defaults are determined by :func:`~diofant.config.setup`.\n    domain : Domain, optional\n        If no ground domain is given, it will be generated automatically\n        from the expression.\n\n    Examples\n    ========\n\n    >>> minimal_polynomial(sqrt(2))(x)\n    x**2 - 2\n    >>> minimal_polynomial(sqrt(2), domain=QQ.algebraic_field(sqrt(2)))(x)\n    x - sqrt(2)\n    >>> minimal_polynomial(sqrt(2) + sqrt(3))(x)\n    x**4 - 10*x**2 + 1\n    >>> minimal_polynomial(solve(x**3 + x + 3)[0][x])(x)\n    x**3 + x + 3\n    >>> minimal_polynomial(sqrt(y))(x)\n    x**2 - y\n\n    '
    if (method is None):
        method = query('minpoly_method')
    _minpoly_methods = {'compose': _minpoly_compose, 'groebner': minpoly_groebner}
    try:
        _minpoly = _minpoly_methods[method]
    except KeyError:
        raise ValueError(f"'{method}' is not a valid algorithm for computing minimal  polynomial")
    ex = sympify(ex)
    if ex.is_number:
        ex = _mexpand(ex, recursive=True)
    x = Dummy('x')
    domain = args.get('domain', (QQ.inject(*ex.free_symbols).field if ex.free_symbols else QQ))
    result = _minpoly(ex, x, domain)
    (_, factors) = factor_list(result, x, domain=domain)
    result = _choose_factor(factors, x, ex, dom=domain)
    result = result.primitive()[1]
    return PurePoly(result, x, domain=domain)