o
    *Î®cÎ  ã                   @   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   ú</tmp/pip-target-vg8gfxp4/lib/python/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   

" 
€
ãrO   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>)r5   r3   )rU   r    rG   r
   r   rT   r   r8   J   s   r8   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   )ÚrowrL   r   r   r   T   ó    z*_matrix_mul.<locals>.<listcomp>.<listcomp>)Úsumr5   r4   r*   ©rL   )rW   r   r   T   s   , z_matrix_mul.<locals>.<listcomp>r   )rG   rL   r   rZ   r   r9   S   s   r9   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   [   rX   z_update.<locals>.<listcomp>c                    s4   g | ]}ˆ ˆ | ˆ ˆ | ˆˆ  ˆˆ   ‘qS r   r   r[   ©rK   r   r'   Úrr   r   r   _   s   4 c                    s    g | ]}ˆ ˆ | ˆˆ  ‘qS r   r   r[   )rK   r   r'   r   r   r   a   s     )Úminr5   r4   )r#   r   rK   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   )rP   )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   r4   rR   )Úbasisr    r   r   r   s   rX   zG_representing_matrices.<locals>.representing_matrix.<locals>.<listcomp>)	r5   r4   Ú	enumerater:   r   r3   ÚremÚtermsÚindex)rQ   rG   r
   rL   r^   ÚmonomÚcoeffr\   )r+   rb   r    rD   r   r   Úrepresenting_matrixr   s   
þz3_representing_matrices.<locals>.representing_matrixc                    s   g | ]}ˆ ˆ|ƒƒ‘qS r   r   r   )ri   ra   r   r   r   ~   rV   z*_representing_matrices.<locals>.<listcomp>)r    r.   r5   )rb   r+   rD   r   )r+   rb   r    ri   rD   r`   ra   r   r1   g   s
   
r1   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'   rJ   r   r   r!   ’   s   € ÿz$_basis.<locals>.<listcomp>.<genexpr>)r)   r   r*   ©Úleading_monomialsrJ   )r'   r   r   ‘   s
    ÿÿTr   )r   )r   r2   r7   r=   r5   r.   r?   r6   r@   rA   rB   )r+   rD   r   Ú
candidatesrb   Únew_candidatesr   rk   r   r0      s   

ø
r0   N)Ú__doc__Úsympy.polys.monomialsr   r   rO   r   r8   r9   r>   r1   r0   r   r   r   r   Ú<module>   s    @	