o
    oÇhÊ  ã                   @   sP   d Z ddlmZmZ dd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dS )zCImplementation of matrix FGLM Groebner basis conversion algorithm. é    )Úmonomial_mulÚmonomial_divc                    sü  |j ‰|j}|jˆd}t| |ƒ}t|| |ƒ}|jg‰ˆjgˆjgt|ƒd   g}g ‰ dd„ t	|ƒD ƒ}|j
‡‡fdd„dd | ¡ }	tt|ƒˆƒ}
	 tˆƒ‰t||	d	  ||	d  ƒ}t|
|ƒ‰t‡‡fd
d„t	ˆt|ƒƒD ƒƒr¢| tˆ|	d  |	d	 ƒˆj¡}| ‡‡fdd„t	ˆƒD ƒ¡}||  |¡}|r¡ˆ  |¡ n9tˆˆ|
ƒ}
ˆ tˆ|	d  |	d	 ƒ¡ | |¡ | ‡fdd„t	|ƒD ƒ¡ tt|ƒƒ}|j
‡‡fdd„dd ‡ ‡fdd„|D ƒ}|sùdd„ ˆ D ƒ‰ tˆ ‡fdd„ddS | ¡ }	qM)aZ  
    Converts the reduced Groebner basis ``F`` of a zero-dimensional
    ideal w.r.t. ``O_from`` to a reduced Groebner basis
    w.r.t. ``O_to``.

    References
    ==========

    .. [1] J.C. Faugere, P. Gianni, D. Lazard, T. Mora (1994). Efficient
           Computation of Zero-dimensional Groebner Bases by Change of
           Ordering
    )Úorderé   c                 S   s   g | ]}|d f‘qS )r   © ©Ú.0Úir   r   úi/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/polys/fglmtools.pyÚ
<listcomp>    ó    zmatrix_fglm.<locals>.<listcomp>c                    ó   ˆ t ˆ| d  | d ƒƒS ©Nr   r   ©Ú_incr_k©Úk_l©ÚO_toÚSr   r
   Ú<lambda>!   ó    zmatrix_fglm.<locals>.<lambda>T©ÚkeyÚreverser   c                 3   s    | ]
}ˆ | ˆj kV  qd S ©N©Úzeror   )Ú_lambdaÚdomainr   r
   Ú	<genexpr>+   s   € zmatrix_fglm.<locals>.<genexpr>c                    s   i | ]	}ˆ | ˆ| “qS r   r   r   )r   r   r   r
   Ú
<dictcomp>.   r   zmatrix_fglm.<locals>.<dictcomp>c                    s   g | ]}|ˆ f‘qS r   r   r   )Úsr   r
   r   9   r   c                    r   r   r   r   r   r   r
   r   ;   r   c                    s2   g | ]\‰ ‰t ‡‡ ‡fd d„ˆD ƒƒrˆ ˆf‘qS )c                 3   s*    | ]}t tˆ ˆ ˆƒ|jƒd u V  qd S r   )r   r   ÚLM©r   Úg)r   ÚkÚlr   r
   r    =   s   €( z)matrix_fglm.<locals>.<listcomp>.<genexpr>)Úall©r   )ÚGr   )r&   r'   r
   r   =   s   2 c                 S   s   g | ]}|  ¡ ‘qS r   )Úmonicr$   r   r   r
   r   @   r   c                    s
   ˆ | j ƒS r   ©r#   )r%   )r   r   r
   r   A   s   
 )r   ÚngensÚcloneÚ_basisÚ_representing_matricesÚ
zero_monomÚoner   ÚlenÚrangeÚsortÚpopÚ_identity_matrixÚ_matrix_mulr(   Úterm_newr   Ú	from_dictÚset_ringÚappendÚ_updateÚextendÚlistÚsetÚsorted)ÚFÚringr   r-   Úring_toÚ	old_basisÚMÚVÚLÚtÚPÚvÚltÚrestr%   r   )r*   r   r   r   r   r"   r
   Úmatrix_fglm   sF   

" 
€
ãrN   c                 C   s6   t t| d |… ƒ| | d g t| |d d … ƒ ƒS )Nr   )Útupler?   )Úmr&   r   r   r
   r   F   s   6r   c                    s8   ‡ ‡fdd„t ˆƒD ƒ}t ˆƒD ]	}ˆ j|| |< q|S )Nc                    s   g | ]}ˆ j gˆ ‘qS r   r   ©r   Ú_©r   Únr   r
   r   K   ó    z$_identity_matrix.<locals>.<listcomp>)r4   r2   )rT   r   rF   r	   r   rS   r
   r7   J   s   r7   c                    s   ‡ fdd„| D ƒS )Nc                    s,   g | ]‰ t ‡ ‡fd d„ttˆƒƒD ƒƒ‘qS )c                 3   s     | ]}ˆ | ˆ|  V  qd S r   r   r   )ÚrowrK   r   r
   r    T   ó   € z)_matrix_mul.<locals>.<listcomp>.<genexpr>)Úsumr4   r3   r)   ©rK   )rV   r
   r   T   s   , z_matrix_mul.<locals>.<listcomp>r   )rF   rK   r   rY   r
   r8   S   s   r8   c                    s¦   t ‡fdd„t| tˆƒƒD ƒƒ‰ttˆƒƒD ]‰ˆˆkr0‡ ‡‡‡fdd„ttˆ ˆ ƒƒD ƒˆ ˆ< q‡ ‡‡fdd„ttˆ ˆ ƒƒD ƒˆ ˆ< ˆ |  ˆ ˆ ˆ ˆ< ˆ | < ˆ S )zE
    Update ``P`` such that for the updated `P'` `P' v = e_{s}`.
    c                 3   s     | ]}ˆ | d kr|V  qdS )r   Nr   ©r   Új)r   r   r
   r    [   rW   z_update.<locals>.<genexpr>c                    s4   g | ]}ˆ ˆ | ˆ ˆ | ˆˆ  ˆˆ   ‘qS r   r   rZ   ©rJ   r   r&   Úrr   r
   r   _   s   4 z_update.<locals>.<listcomp>c                    s    g | ]}ˆ ˆ | ˆˆ  ‘qS r   r   rZ   )rJ   r   r&   r   r
   r   a   s     )Úminr4   r3   )r"   r   rJ   r   r\   r
   r=   W   s    (€&r=   c                    sJ   ˆj ‰ˆjd ‰‡fdd„‰‡ ‡‡‡fdd„‰‡‡fdd„tˆd ƒD ƒS )zn
    Compute the matrices corresponding to the linear maps `m \mapsto
    x_i m` for all variables `x_i`.
    r   c                    s"   t dg|  dg dgˆ |    ƒS )Nr   r   )rO   )r	   )Úur   r
   Úvaro   s   "z#_representing_matrices.<locals>.varc                    st   ‡‡fdd„t tˆƒƒD ƒ}tˆƒD ]%\}}ˆ t| |ƒˆj¡ ˆ ¡}| ¡ D ]\}}ˆ |¡}||| |< q'q|S )Nc                    s   g | ]
}ˆj gtˆ ƒ ‘qS r   )r   r3   rQ   )Úbasisr   r   r
   r   s   s    zG_representing_matrices.<locals>.representing_matrix.<locals>.<listcomp>)	r4   r3   Ú	enumerater9   r   r2   ÚremÚtermsÚindex)rP   rF   r	   rK   r]   ÚmonomÚcoeffr[   )r*   ra   r   rC   r   r
   Úrepresenting_matrixr   s   
þz3_representing_matrices.<locals>.representing_matrixc                    s   g | ]}ˆ ˆ|ƒƒ‘qS r   r   r   )rh   r`   r   r
   r   ~   rU   z*_representing_matrices.<locals>.<listcomp>)r   r-   r4   )ra   r*   rC   r   )r*   ra   r   rh   rC   r_   r`   r
   r0   g   s
   
r0   c                    s„   |j }dd„ | D ƒ‰ |jg}g }|r6| ¡ ‰| ˆ¡ ‡ ‡fdd„t|jƒD ƒ}| |¡ |j|dd |stt	|ƒƒ}t
||dS )z°
    Computes a list of monomials which are not divisible by the leading
    monomials wrt to ``O`` of ``G``. These monomials are a basis of
    `K[X_1, \ldots, X_n]/(G)`.
    c                 S   s   g | ]}|j ‘qS r   r,   r$   r   r   r
   r   ‰   s    z_basis.<locals>.<listcomp>c                    s.   g | ]‰ t ‡ ‡fd d„ˆD ƒƒrtˆˆ ƒ‘qS )c                 3   s$    | ]}t tˆˆ ƒ|ƒd u V  qd S r   )r   r   )r   Úlmg)r&   rI   r   r
   r    ’   s   € ÿz$_basis.<locals>.<listcomp>.<genexpr>)r(   r   r)   ©Úleading_monomialsrI   )r&   r
   r   ‘   s
    ÿÿTr   )r   )r   r1   r6   r<   r4   r-   r>   r5   r?   r@   rA   )r*   rC   r   Ú
candidatesra   Únew_candidatesr   rj   r
   r/      s   

ø
r/   N)Ú__doc__Úsympy.polys.monomialsr   r   rN   r   r7   r8   r=   r0   r/   r   r   r   r
   Ú<module>   s    @	