o
    GZŽ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   úD/var/www/auris/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   )Z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_dictZ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   

" 
€
ãrI   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>)r2   r0   )rO   r   rA   r	   r   rN   r
   r5   J   s   r5   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   )ÚrowrF   r   r
   r   T   ó   € z)_matrix_mul.<locals>.<listcomp>.<genexpr>)Úsumr2   r1   r(   ©rF   )rQ   r
   r   T   s   , z_matrix_mul.<locals>.<listcomp>r   )rA   rF   r   rT   r
   r6   S   s   r6   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   [   rR   z_update.<locals>.<genexpr>c                    s4   g | ]}ˆ ˆ | ˆ ˆ | ˆˆ  ˆˆ   ‘qS r   r   rU   ©rE   r   r%   Úrr   r
   r   _   s   4 z_update.<locals>.<listcomp>c                    s    g | ]}ˆ ˆ | ˆˆ  ‘qS r   r   rU   )rE   r   r%   r   r
   r   a   s     )Úminr2   r1   )r!   r   rE   r   rW   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   )rJ   )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   r1   rL   )Úbasisr   r   r
   r   s   s    zG_representing_matrices.<locals>.representing_matrix.<locals>.<listcomp>)	r2   r1   Ú	enumerater7   r   r0   ÚremZtermsÚindex)rK   rA   r	   rF   rX   ZmonomZcoeffrV   )r)   r\   r   r@   r   r
   Úrepresenting_matrixr   s   
þz3_representing_matrices.<locals>.representing_matrixc                    s   g | ]}ˆ ˆ|ƒƒ‘qS r   r   r   )r`   r[   r   r
   r   ~   rP   z*_representing_matrices.<locals>.<listcomp>)r   r+   r2   )r\   r)   r@   r   )r)   r\   r   r`   r@   rZ   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%   rD   r   r
   r   ’   s   € ÿz$_basis.<locals>.<listcomp>.<genexpr>)r'   r   r(   ©Zleading_monomialsrD   )r%   r
   r   ‘   s
    ÿÿTr   )r   )r   r/   r4   r9   r2   r+   r;   r3   r<   r=   r>   )r)   r@   r   Ú
candidatesr\   Znew_candidatesr   ra   r
   r-      s   

ø
r-   N)Ú__doc__Zsympy.polys.monomialsr   r   rI   r   r5   r6   r:   r.   r-   r   r   r   r
   Ú<module>   s    @	