o
    ôjÊ  ã                   @   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   úf/var/www/html/academy/tools/voices/clone/sv-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©Z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,   Zring_toZ	old_basisÚMÚVÚLÚtÚPÚvÚltÚrestr$   r   )r)   r   r   r   r   r!   r
   Úmatrix_fglm   sF   

" 
€
ãrK   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>)r3   r1   )rQ   r   rC   r	   r   rP   r
   r6   J   s   r6   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   )ÚrowrH   r   r
   r   T   ó   € z)_matrix_mul.<locals>.<listcomp>.<genexpr>)Úsumr3   r2   r(   ©rH   )rS   r
   r   T   s   , z_matrix_mul.<locals>.<listcomp>r   )rC   rH   r   rV   r
   r7   S   s   r7   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   [   rT   z_update.<locals>.<genexpr>c                    s4   g | ]}ˆ ˆ | ˆ ˆ | ˆˆ  ˆˆ   ‘qS r   r   rW   ©rG   r   r%   Úrr   r
   r   _   s   4 z_update.<locals>.<listcomp>c                    s    g | ]}ˆ ˆ | ˆˆ  ‘qS r   r   rW   )rG   r   r%   r   r
   r   a   s     )Úminr3   r2   )r!   r   rG   r   rY   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   )rL   )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   r2   rN   )Úbasisr   r   r
   r   s   s    zG_representing_matrices.<locals>.representing_matrix.<locals>.<listcomp>)	r3   r2   Ú	enumerater8   r   r1   ÚremÚtermsÚindex)rM   rC   r	   rH   rZ   ÚmonomÚcoeffrX   )r)   r^   r   rB   r   r
   Úrepresenting_matrixr   s   
þz3_representing_matrices.<locals>.representing_matrixc                    s   g | ]}ˆ ˆ|ƒƒ‘qS r   r   r   )re   r]   r   r
   r   ~   rR   z*_representing_matrices.<locals>.<listcomp>)r   r,   r3   )r^   r)   rB   r   )r)   r^   r   re   rB   r\   r]   r
   r/   g   s
   
r/   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   Zlmg)r%   rF   r   r
   r   ’   s   € ÿz$_basis.<locals>.<listcomp>.<genexpr>)r'   r   r(   ©Zleading_monomialsrF   )r%   r
   r   ‘   s
    ÿÿTr   )r   )r   r0   r5   r;   r3   r,   r=   r4   r>   r?   r@   )r)   rB   r   Ú
candidatesr^   Znew_candidatesr   rf   r
   r.   �   s   

ø
r.   N)Ú__doc__Úsympy.polys.monomialsr   r   rK   r   r6   r7   r<   r/   r.   r   r   r   r
   Ú<module>   s    @	