o
    oha                     @   sX  d Z ddlmZmZmZmZ ddlmZmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ G dd deZG dd deZG dd dZ d(ddZ!dd Z"d)ddZ#dd Z$d*ddZ%d d! Z&e'd"krdd#l(Z(d$e(j)d%d# v Z*d&e(j)d%d# v Z+e%e*e+d'Z,e-e, d#S d#S )+a  
Galois resolvents

Each of the functions in ``sympy.polys.numberfields.galoisgroups`` that
computes Galois groups for a particular degree $n$ uses resolvents. Given the
polynomial $T$ whose Galois group is to be computed, a resolvent is a
polynomial $R$ whose roots are defined as functions of the roots of $T$.

One way to compute the coefficients of $R$ is by approximating the roots of $T$
to sufficient precision. This module defines a :py:class:`~.Resolvent` class
that handles this job, determining the necessary precision, and computing $R$.

In some cases, the coefficients of $R$ are symmetric in the roots of $T$,
meaning they are equal to fixed functions of the coefficients of $T$. Therefore
another approach is to compute these functions once and for all, and record
them in a lookup table. This module defines code that can compute such tables.
The tables for polynomials $T$ of degrees 4 through 6, produced by this code,
are recorded in the resolvent_lookup.py module.

    )evalffastlog_evalf_with_bounded_errorquad_to_mpmath)symbolsDummy)dup_eval)ZZ)lex)preprocess_roots)Poly)xring)symmetric_polylambdify)	MPContext)prec_to_dpsc                   @      e Zd ZdS )GaloisGroupExceptionN__name__
__module____qualname__ r   r   ~/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/polys/numberfields/galois_resolvents.pyr   '       r   c                   @   r   )ResolventExceptionNr   r   r   r   r   r   +   r   r   c                   @   sJ   e Zd ZdZdd ZdddZdddZed	d
 Zdd Z	dddZ
dS )	Resolventa  
    If $G$ is a subgroup of the symmetric group $S_n$,
    $F$ a multivariate polynomial in $\mathbb{Z}[X_1, \ldots, X_n]$,
    $H$ the stabilizer of $F$ in $G$ (i.e. the permutations $\sigma$ such that
    $F(X_{\sigma(1)}, \ldots, X_{\sigma(n)}) = F(X_1, \ldots, X_n)$), and $s$
    a set of left coset representatives of $H$ in $G$, then the resolvent
    polynomial $R(Y)$ is the product over $\sigma \in s$ of
    $Y - F(X_{\sigma(1)}, \ldots, X_{\sigma(n)})$.

    For example, consider the resolvent for the form
    $$F = X_0 X_2 + X_1 X_3$$
    and the group $G = S_4$. In this case, the stabilizer $H$ is the dihedral
    group $D4 = < (0123), (02) >$, and a set of representatives of $G/H$ is
    $\{I, (01), (03)\}$. The resolvent can be constructed as follows:

    >>> from sympy.combinatorics.permutations import Permutation
    >>> from sympy.core.symbol import symbols
    >>> from sympy.polys.numberfields.galoisgroups import Resolvent
    >>> X = symbols('X0 X1 X2 X3')
    >>> F = X[0]*X[2] + X[1]*X[3]
    >>> s = [Permutation([0, 1, 2, 3]), Permutation([1, 0, 2, 3]),
    ... Permutation([3, 1, 2, 0])]
    >>> R = Resolvent(F, X, s)

    This resolvent has three roots, which are the conjugates of ``F`` under the
    three permutations in ``s``:

    >>> R.root_lambdas[0](*X)
    X0*X2 + X1*X3
    >>> R.root_lambdas[1](*X)
    X0*X3 + X1*X2
    >>> R.root_lambdas[2](*X)
    X0*X1 + X2*X3

    Resolvents are useful for computing Galois groups. Given a polynomial $T$
    of degree $n$, we will use a resolvent $R$ where $Gal(T) \leq G \leq S_n$.
    We will then want to substitute the roots of $T$ for the variables $X_i$
    in $R$, and study things like the discriminant of $R$, and the way $R$
    factors over $\mathbb{Q}$.

    From the symmetry in $R$'s construction, and since $Gal(T) \leq G$, we know
    from Galois theory that the coefficients of $R$ must lie in $\mathbb{Z}$.
    This allows us to compute the coefficients of $R$ by approximating the
    roots of $T$ to sufficient precision, plugging these values in for the
    variables $X_i$ in the coefficient expressions of $R$, and then simply
    rounding to the nearest integer.

    In order to determine a sufficient precision for the roots of $T$, this
    ``Resolvent`` class imposes certain requirements on the form ``F``. It
    could be possible to design a different ``Resolvent`` class, that made
    different precision estimates, and different assumptions about ``F``.

    ``F`` must be homogeneous, and all terms must have unit coefficient.
    Furthermore, if $r$ is the number of terms in ``F``, and $t$ the total
    degree, and if $m$ is the number of conjugates of ``F``, i.e. the number
    of permutations in ``s``, then we require that $m < r 2^t$. Again, it is
    not impossible to work with forms ``F`` that violate these assumptions, but
    this ``Resolvent`` class requires them.

    Since determining the integer coefficients of the resolvent for a given
    polynomial $T$ is one of the main problems this class solves, we take some
    time to explain the precision bounds it uses.

    The general problem is:
    Given a multivariate polynomial $P \in \mathbb{Z}[X_1, \ldots, X_n]$, and a
    bound $M \in \mathbb{R}_+$, compute an $\varepsilon > 0$ such that for any
    complex numbers $a_1, \ldots, a_n$ with $|a_i| < M$, if the $a_i$ are
    approximated to within an accuracy of $\varepsilon$ by $b_i$, that is,
    $|a_i - b_i| < \varepsilon$ for $i = 1, \ldots, n$, then
    $|P(a_1, \ldots, a_n) - P(b_1, \ldots, b_n)| < 1/2$. In other words, if it
    is known that $P(a_1, \ldots, a_n) = c$ for some $c \in \mathbb{Z}$, then
    $P(b_1, \ldots, b_n)$ can be rounded to the nearest integer in order to
    determine $c$.

    To derive our error bound, consider the monomial $xyz$. Defining
    $d_i = b_i - a_i$, our error is
    $|(a_1 + d_1)(a_2 + d_2)(a_3 + d_3) - a_1 a_2 a_3|$, which is bounded
    above by $|(M + \varepsilon)^3 - M^3|$. Passing to a general monomial of
    total degree $t$, this expression is bounded by
    $M^{t-1}\varepsilon(t + 2^t\varepsilon/M)$ provided $\varepsilon < M$,
    and by $(t+1)M^{t-1}\varepsilon$ provided $\varepsilon < M/2^t$.
    But since our goal is to make the error less than $1/2$, we will choose
    $\varepsilon < 1/(2(t+1)M^{t-1})$, which implies the condition that
    $\varepsilon < M/2^t$, as long as $M \geq 2$.

    Passing from the general monomial to the general polynomial is easy, by
    scaling and summing error bounds.

    In our specific case, we are given a homogeneous polynomial $F$ of
    $r$ terms and total degree $t$, all of whose coefficients are $\pm 1$. We
    are given the $m$ permutations that make the conjugates of $F$, and
    we want to bound the error in the coefficients of the monic polynomial
    $R(Y)$ having $F$ and its conjugates as roots (i.e. the resolvent).

    For $j$ from $1$ to $m$, the coefficient of $Y^{m-j}$ in $R(Y)$ is the
    $j$th elementary symmetric polynomial in the conjugates of $F$. This sums
    the products of these conjugates, taken $j$ at a time, in all possible
    combinations. There are $\binom{m}{j}$ such combinations, and each product
    of $j$ conjugates of $F$ expands to a sum of $r^j$ terms, each of unit
    coefficient, and total degree $jt$. An error bound for the $j$th coeff of
    $R$ is therefore
    $$\binom{m}{j} r^j (jt + 1) M^{jt - 1} \varepsilon$$
    When our goal is to evaluate all the coefficients of $R$, we will want to
    use the maximum of these error bounds. It is clear that this bound is
    strictly increasing for $j$ up to the ceiling of $m/2$. After that point,
    the first factor $\binom{m}{j}$ begins to decrease, while the others
    continue to increase. However, the binomial coefficient never falls by more
    than a factor of $1/m$ at a time, so our assumptions that $M \geq 2$ and
    $m < r 2^t$ are enough to tell us that the constant coefficient of $R$,
    i.e. that where $j = m$, has the largest error bound. Therefore we can use
    $$r^m (mt + 1) M^{mt - 1} \varepsilon$$
    as our error bound for all the coefficients.

    Note that this bound is also (more than) adequate to determine whether any
    of the roots of $R$ is an integer. Each of these roots is a single
    conjugate of $F$, which contains less error than the trace, i.e. the
    coefficient of $Y^{m - 1}$. By rounding the roots of $R$ to the nearest
    integers, we therefore get all the candidates for integer roots of $R$. By
    plugging these candidates into $R$, we can check whether any of them
    actually is a root.

    Note: We take the definition of resolvent from Cohen, but the error bound
    is ours.

    References
    ==========

    .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory*.
       (Def 6.3.2)

    c                    s   | _ | _| _t| _d| _d| _t  D ]*\}}t	|dkr(t
dt|}|| jkr:| jdur:t
d|| _|  jd7  _q| j| j| j}}}||d|  k s\t
dtd}	t|| || d  |	|| d   | _t||d  |	|d   | _ fd	d
t| jD | _td}
tddd t|D d}D ]}||
| 9 }qt||
 }fdd
|D | _dS )a[  
        Parameters
        ==========

        F : :py:class:`~.Expr`
            polynomial in the symbols in *X*
        X : list of :py:class:`~.Symbol`
        s : list of :py:class:`~.Permutation`
            representing the cosets of the stabilizer of *F* in
            some subgroup $G$ of $S_n$, where $n$ is the length of *X*.
        Nr      z.Resolvent class expects forms with unit coeffsz)Resolvent class expects homogeneous forms   z!Resolvent class expects m < r*2^tMc                    s    g | ]}t |   qS r   r   ).0j)FXsr   r   
<listcomp>       z&Resolvent.__init__.<locals>.<listcomp>Y c                 s   s    | ]}d | V  qdS )RNr   r!   ir   r   r   	<genexpr>   s    z%Resolvent.__init__.<locals>.<genexpr>c                    s   g | ]}t  |qS r   r   r!   c)r*   r   r   r&      s    )r#   r$   r%   lenmtrr   termsabsr   sumr   coeff_prec_funcroot_prec_funcrangeroot_lambdasjoincoeffsesf_lambdas)selfr#   r$   r%   monomcoeffr2   r1   r3   r    r(   fCr   )r#   r*   r$   r%   r   __init__   s<   
*zResolvent.__init__r<   c                 C   sF   t |d}|dkr| jn| j}td|| di \}}}}t|d S )a  
        For a given upper bound *M* on the magnitude of the complex numbers to
        be plugged in for this resolvent's symbols, compute a sufficient
        precision for evaluating those complex numbers, such that the
        coefficients, or the integer roots, of the resolvent can be determined.

        Parameters
        ==========

        M : real number
            Upper bound on magnitude of the complex numbers to be plugged in.

        target : str, 'coeffs' or 'roots', default='coeffs'
            Name the task for which a sufficient precision is desired.
            This is either determining the coefficients of the resolvent
            ('coeffs') or determining its possible integer roots ('roots').
            The latter may require significantly lower precision.

        Returns
        =======

        int $m$
            such that $2^{-m}$ is a sufficient upper bound on the
            error in approximating the complex numbers to be plugged in.

        r   r<   r   )maxr7   r8   r   r   )r>   r    targetrA   r3   _r   r   r   get_prec   s   
zResolvent.get_precc           
         s   t  t|\ }t  |jdd} fdd|D }tdd |D d }| j||d}t|jd }|| d }|_	t
|fd	d|D }	 fd
d|	D }	|	S )a  
        Approximate the roots of a given polynomial *T* to sufficient precision
        in order to evaluate this resolvent's coefficients, or determine
        whether the resolvent has an integer root.

        Parameters
        ==========

        T : :py:class:`~.Poly`

        target : str, 'coeffs' or 'roots', default='coeffs'
            Set the approximation precision to be sufficient for the desired
            task, which is either determining the coefficients of the resolvent
            ('coeffs') or determining its possible integer roots ('roots').
            The latter may require significantly lower precision.

        Returns
        =======

        list of elements of :ref:`CC`

        F)radicalsc                    s    g | ]} t t|d d qS )r   )r1   )r   r   r!   r3   )r@   r   r   r&   8  s     z7Resolvent.approximate_roots_of_poly.<locals>.<listcomp>c                 s       | ]}t |V  qd S N)r5   )r!   br   r   r   r-   :      z6Resolvent.approximate_roots_of_poly.<locals>.<genexpr>r   rE   c                    s   g | ]	}|j  d dqS )T)return_mpmath)eval_approxrI   )dr   r   r&   A      c                    s   g | ]	}  | qS r   )mpcrI   )r@   ctxr   r   r&   B  rR   )r   r   mpfstr	all_rootsrD   rG   r   _mpf_precr   )
r>   TrE   scaled_rootsapprox0r    r1   npapprox1r   )r@   rT   rQ   r   approximate_roots_of_poly  s   z#Resolvent.approximate_roots_of_polyc                 C   s"   t | tr| S tt| j| S rK   )
isinstanceintr	   contextnint)ar   r   r   	round_mpfF  s   
zResolvent.round_mpfc                    s8   j |dd  fddjD }fddt|D S )a  
        For a given polynomial *T*, round the roots of this resolvent to the
        nearest integers.

        Explanation
        ===========

        None of the integers returned by this method is guaranteed to be a
        root of the resolvent; however, if the resolvent has any integer roots
        (for the given polynomial *T*), then they must be among these.

        If the coefficients of the resolvent are also desired, then this method
        should not be used. Instead, use the ``eval_for_poly`` method. This
        method may be significantly faster than ``eval_for_poly``.

        Parameters
        ==========

        T : :py:class:`~.Poly`

        Returns
        =======

        dict
            Keys are the indices of those permutations in ``self.s`` such that
            the corresponding root did round to a rational integer.

            Values are :ref:`ZZ`.


        rootsrN   c                       g | ]}|  qS r   r   rI   approx_roots_of_Tr   r   r&   r      z>Resolvent.round_roots_to_integers_for_poly.<locals>.<listcomp>c                    s.   i | ]\}}  |jd kr|  |jqS )r   )rf   imagreal)r!   r,   r3   )r>   r   r   
<dictcomp>s  s
    z>Resolvent.round_roots_to_integers_for_poly.<locals>.<dictcomp>)r`   r:   	enumerate)r>   rZ   approx_roots_of_selfr   )rj   r>   r    round_roots_to_integers_for_polyQ  s
    
z*Resolvent.round_roots_to_integers_for_polyFc                    s   | j |dd  fdd| jD fdd| jD }g }|D ]}| |jdkr0td| || |j qd\}}|retD ] \}}	| |	jdkrQqDt	|| |	j }
t
sd|
|}} nqD|||fS )	a  
        Compute the integer values of the coefficients of this resolvent, when
        plugging in the roots of a given polynomial.

        Parameters
        ==========

        T : :py:class:`~.Poly`

        find_integer_root : ``bool``, default ``False``
            If ``True``, then also determine whether the resolvent has an
            integer root, and return the first one found, along with its
            index, i.e. the index of the permutation ``self.s[i]`` it
            corresponds to.

        Returns
        =======

        Tuple ``(R, a, i)``

            ``R`` is this resolvent as a dense univariate polynomial over
            :ref:`ZZ`, i.e. a list of :ref:`ZZ`.

            If *find_integer_root* was ``True``, then ``a`` and ``i`` are the
            first integer root found, and its index, if one exists.
            Otherwise ``a`` and ``i`` are both ``None``.

        r<   rN   c                    rh   r   r   rI   ri   r   r   r&     rk   z+Resolvent.eval_for_poly.<locals>.<listcomp>c                    rh   r   r   r.   )rp   r   r   r&     rk   r   z%Got non-integer coeff for resolvent: )NN)r`   r:   r=   rf   rl   r   appendrm   ro   r   r	   )r>   rZ   find_integer_rootapprox_coeffs_of_selfr*   r/   a0i0r,   r3   re   r   )rj   rp   r   eval_for_polyy  s$   

zResolvent.eval_for_polyN)r<   F)r   r   r   __doc__rC   rG   r`   staticmethodrf   rq   rw   r   r   r   r   r   /   s     
>
"2

(r   P   c                 C   s@   d}d}| D ]}|dkr||krd\}}n|d7 }||7 }q|S )z#Line wrap a polynomial expression.  r   r)   )
r   r   r   )textwidthoutcolr/   r   r   r   wrap  s   

r   c                 C   s   t dd t| D S )zAForm the symbols s1, s2, ..., sn to stand for elem. symm. polys. c                 S   s   g | ]	}d |d  qS )r%   r   r   r+   r   r   r   r&     rR   zs_vars.<locals>.<listcomp>)r   r9   )r]   r   r   r   s_vars  s   r   Fc              
      s  ddl }ddl} fdd|D }dd tt|D }g }tdt|d D ]a}	|r=td td|	 d	 |j  |  }
t|	g|R  }|  }|rbtd
||
  d td |j  |  }
t|d|	 | }|  }|rtd
||
  d |j  |	| q(g }t
|D ];\}	}|rtd td|	d  d	 |j  |  }
|| }|  }|	| |rtd
||
  d |j  qg }g }tt}tt
|D ]k\}	}|rtd td|	d  d	 |j  |  }
| \}}}|  }|dkrd| d  d d| d	}t|t|j| }|	| |	||
  |rGtt| td
||
  d |j  q||fS )z
    Compute the coefficients of a resolvent as functions of the coefficients of
    the associated polynomial.

    F must be a sparse polynomial.
    r   Nc              	      s$   g | ]}  tt|qS r   composelistzip)r!   sigmar#   r$   r   r   r&     s    z6sparse_symmetrize_resolvent_coeffs.<locals>.<listcomp>c                 S   s   g | ]	}t d | qS )r(   )r   r+   r   r   r   r&     rR   r   z----z#Computing symmetric poly of degree z...ztook z secondszlambdifying...z(Plugging root forms into elem symm poly z-----zCoeff zGot nonzero remainder z for resolvent (F, X, s) = (, ))timesysr9   r0   printstdoutflushr   r   rr   ro   r   r   
symmetrizer   rV   as_exprr   )r#   r$   r%   verboser   r   
root_formsr(   coeff_formsr,   t0Gt1rB   r<   rA   gsymmetrizedsymmetrization_timesssABremrF   msgB_strr   r   r   "sparse_symmetrize_resolvent_coeffs  s|   









r   c                     sJ  ddl m}  ddlm  tdtt\}}|d d d  d d d   d d d   d d d   } d ddd ddd ddd ddd dddg}d d  d d   } d ddd dddg}tdtt\}}|d d d d	  d d    d d d d  d d	     d d d d  d	 d     d d d	 d  d d     d	 d d d  d d     }	 d	 d	dd d	dd d	dd d	dd	 d	dd	g}
td
tt\}}||  }d d d d  d d	  d d    fdd|jD }t|} dg fddt	ddD  }d d  d  d d	  d   } dg fddt	dD  }|||f|||f|	||
f|||f|||fdS )z=Define all the resolvents for polys T of degree 4 through 6. r   )PGL2F5PermutationzX0,X1,X2,X3r   r      zX0,X1,X2,X3,X4   zX0,X1,X2,X3,X4,X5   c              	      s$   h | ]} tt | qS r   r   )r!   r%   )r$   term0r   r   	<setcomp>B  s   $ z$define_resolvents.<locals>.<setcomp>c                    s   g | ]	} d d|qS )r   r   r   )r!   r]   r   r   r   r&   D  rR   z%define_resolvents.<locals>.<listcomp>   c                    s,   g | ]}t d D ]} d||d  qqS )r   r   )r9   )r!   r,   r"   r   r   r   r&   H  s
    ))r   r   )r   r   )r   r   r   r   )r   r   )
sympy.combinatorics.galoisr    sympy.combinatorics.permutationsr   r   r	   r
   elementsr6   r9   )r   R4X4F40s40F41s41R5X5F51s51R6X6Hr4   F61s61F62s62r   )r   r$   r   r   define_resolvents  sh   P 
*****	8"0r   c                    s  t  }i }d}d}d}| D ]I\}\}}	}
t||	|
| d\}}|t|7 }|dkr2t|}|d }tt|	}dddd |D  d	 d
 fdd|D ||< |rX nqd|dd|dd|dd}| D ]\}}|d| d7 }|d| d 7 }|d7 }ql|d7 }|S )zO
    Generate the whole lookup table of coeff lambdas, for all resolvents.
    r   )r   r   r   zlambda r   c                 s   rJ   rK   )rV   )r!   vr   r   r   r-   g  rM   z)generate_lambda_lookup.<locals>.<genexpr>:z
,
        c                    s    g | ]}  d t | dqS )z (r   )r   )r!   rA   headr   r   r&   h  r'   z*generate_lambda_lookup.<locals>.<listcomp>z# This table was generated by a call to
# `sympy.polys.numberfields.galois_resolvents.generate_lambda_lookup()`.
# The entire job took z.2fzs.
# Of this, Case (6, 1) took z4s.
# The final polynomial of Case (6, 1) alone took zs.
resolvent_coeff_lambdas = {
z    z: [
z        r}   z    ],
z}
)r   itemsr   r6   r   r0   r;   )r   	trial_runjobslambda_lists
total_timetime_for_61time_for_61_lastkr#   r$   r%   r   timessvtableLr   r   r   generate_lambda_lookupU  sB   
r   c                    sN   ddl m} |  }|||f }| j dd  tdg fdd|D  S )a[  
    Use the lookup table, to return a resolvent (as dup) for a given
    polynomial *T*.

    Parameters
    ==========

    T : Poly
        The polynomial whose resolvent is needed

    number : int
        For some degrees, there are multiple resolvents.
        Use this to indicate which one you want.

    Returns
    =======

    dup

    r   )resolvent_coeff_lambdasr   Nc                    rh   r   r   r.   T_coeffsr   r   r&     rk   z+get_resolvent_by_lookup.<locals>.<listcomp>))sympy.polys.numberfields.resolvent_lookupr   degreerepto_listr	   )rZ   numberr   r   r   r   r   r   get_resolvent_by_lookup  s
   r   __main__Nz-vr   z-t)r   r   )r{   rx   )FF).ry   sympy.core.evalfr   r   r   r   sympy.core.symbolr   r   sympy.polys.densetoolsr   sympy.polys.domainsr	   sympy.polys.orderingsr
   sympy.polys.polyrootsr   sympy.polys.polytoolsr   sympy.polys.ringsr   sympy.polys.specialpolysr   sympy.utilities.lambdifyr   mpmathr   mpmath.libmp.libmpfr   	Exceptionr   r   r   r   r   r   r   r   r   r   r   argvr   r   r   r   r   r   r   r   <module>   s@       

N
G+