o
    ÐeÎ  ã                   @   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   úED:\Projects\ConvertPro\env\Lib\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   

" 
€
ãrJ   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   )rP   r   rB   r
   r   rO   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                    s   g | ]
}ˆ | ˆ|  ‘qS r   r   r   )ÚrowrG   r   r   r   T   ó    z*_matrix_mul.<locals>.<listcomp>.<listcomp>)Úsumr3   r2   r)   ©rG   )rR   r   r   T   s   , z_matrix_mul.<locals>.<listcomp>r   )rB   rG   r   rU   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                    s   g | ]
}ˆ | d kr|‘qS r   r   ©r	   Új)r   r   r   r   [   rS   z_update.<locals>.<listcomp>c                    s4   g | ]}ˆ ˆ | ˆ ˆ | ˆˆ  ˆˆ   ‘qS r   r   rV   ©rF   r   r&   Úrr   r   r   _   s   4 c                    s    g | ]}ˆ ˆ | ˆˆ  ‘qS r   r   rV   )rF   r   r&   r   r   r   a   s     )Úminr3   r2   )r"   r   rF   r   rX   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   )rK   )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   rM   )Úbasisr   r   r   r   s   rS   zG_representing_matrices.<locals>.representing_matrix.<locals>.<listcomp>)	r3   r2   Ú	enumerater8   r   r1   ÚremZtermsÚindex)rL   rB   r
   rG   rY   ZmonomZcoeffrW   )r*   r]   r   rA   r   r   Úrepresenting_matrixr   s   
þz3_representing_matrices.<locals>.representing_matrixc                    s   g | ]}ˆ ˆ|ƒƒ‘qS r   r   r   )ra   r\   r   r   r   ~   rQ   z*_representing_matrices.<locals>.<listcomp>)r   r,   r3   )r]   r*   rA   r   )r*   r]   r   ra   rA   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&   rE   r   r   r    ’   s   € ÿz$_basis.<locals>.<listcomp>.<genexpr>)r(   r   r)   ©Zleading_monomialsrE   )r&   r   r   ‘   s
    ÿÿTr   )r   )r   r0   r5   r:   r3   r,   r<   r4   r=   r>   r?   )r*   rA   r   Ú
candidatesr]   Znew_candidatesr   rb   r   r.      s   

ø
r.   N)Ú__doc__Zsympy.polys.monomialsr   r   rJ   r   r6   r7   r;   r/   r.   r   r   r   r   Ú<module>   s    @	