o
    Ðeæ  ã                   @  s„   U d Z ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
mZ ddlmZ eG d	d
„ d
eeƒƒZi Zded< dd„ ZdS )z1Implementation of :class:`ModularInteger` class. é    )Úannotations)ÚAnyN)ÚPicklableWithSlots)ÚCoercionFailed)ÚDomainElement)Úpublicc                   @  s   e Zd ZdZd\ZZZZdZdd„ Z	dd„ Z
dd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Z d2d3„ Z!d4d5„ Z"d6d7„ Z#d8d9„ Z$d:d;„ Z%d<d=„ Z&ed>d?„ ƒZ'd@dA„ Z(dBS )CÚModularIntegerz(A class representing a modular integer. )NNNN)Úvalc                 C  s   | j S ©N)Ú_parent©Úself© r   úRD:\Projects\ConvertPro\env\Lib\site-packages\sympy/polys/domains/modularinteger.pyÚparent   s   zModularInteger.parentc                 C  s6   t || jƒr|j| j | _d S | j |¡| j | _d S r
   )Ú
isinstanceÚ	__class__r	   ÚmodÚdomÚconvert)r   r	   r   r   r   Ú__init__   s   zModularInteger.__init__c                 C  s   t | j| jfƒS r
   )Úhashr	   r   r   r   r   r   Ú__hash__   ó   zModularInteger.__hash__c                 C  s   d| j j| jf S )Nz%s(%s))r   Ú__name__r	   r   r   r   r   Ú__repr__"   ó   zModularInteger.__repr__c                 C  s   d| j | jf S )Nz	%s mod %s)r	   r   r   r   r   r   Ú__str__%   r   zModularInteger.__str__c                 C  s   t |  ¡ ƒS r
   )ÚintÚto_intr   r   r   r   Ú__int__(   ó   zModularInteger.__int__c                 C  s.   | j r| j| jd kr| jS | j| j S | jS )Né   )Úsymr	   r   r   r   r   r   r   +   s
   zModularInteger.to_intc                 C  s   | S r
   r   r   r   r   r   Ú__pos__4   s   zModularInteger.__pos__c                 C  s   |   | j ¡S r
   )r   r	   r   r   r   r   Ú__neg__7   ó   zModularInteger.__neg__c                 C  s4   t || ƒr|jS z| j |¡W S  ty   Y d S w r
   )r   r	   r   r   r   )ÚclsÚotherr   r   r   Ú_get_val:   s   
ÿzModularInteger._get_valc                 C  s&   |   |¡}|d ur|  | j| ¡S tS r
   ©r)   r   r	   ÚNotImplemented©r   r(   r	   r   r   r   Ú__add__D   ó   
zModularInteger.__add__c                 C  ó
   |   |¡S r
   ©r-   ©r   r(   r   r   r   Ú__radd__L   ó   
zModularInteger.__radd__c                 C  s&   |   |¡}|d ur|  | j| ¡S tS r
   r*   r,   r   r   r   Ú__sub__O   r.   zModularInteger.__sub__c                 C  s   |    |¡S r
   r0   r1   r   r   r   Ú__rsub__W   r!   zModularInteger.__rsub__c                 C  s&   |   |¡}|d ur|  | j| ¡S tS r
   r*   r,   r   r   r   Ú__mul__Z   r.   zModularInteger.__mul__c                 C  r/   r
   )r6   r1   r   r   r   Ú__rmul__b   r3   zModularInteger.__rmul__c                 C  s,   |   |¡}|d ur|  | j|  |¡ ¡S tS r
   )r)   r   r	   Ú_invertr+   r,   r   r   r   Ú__truediv__e   s   
zModularInteger.__truediv__c                 C  s   |   ¡  |¡S r
   )Úinvertr6   r1   r   r   r   Ú__rtruediv__m   r&   zModularInteger.__rtruediv__c                 C  s&   |   |¡}|d ur|  | j| ¡S tS r
   r*   r,   r   r   r   Ú__mod__p   r.   zModularInteger.__mod__c                 C  s&   |   |¡}|d ur|  || j ¡S tS r
   r*   r,   r   r   r   Ú__rmod__x   r.   zModularInteger.__rmod__c                 C  sL   |s	|   | jj¡S |dk r|  ¡ j| }}n| j}|   t|t|ƒ| jƒ¡S )Nr   )r   r   Úoner:   r	   Úpowr   r   )r   Úexpr	   r   r   r   Ú__pow__€   s   zModularInteger.__pow__c                 C  s(   |   |¡}|d ur|| j|| j ƒS tS r
   )r)   r	   r   r+   )r   r(   Úopr	   r   r   r   Ú_compare‹   s   
zModularInteger._comparec                 C  ó   |   |tj¡S r
   )rC   ÚoperatorÚeqr1   r   r   r   Ú__eq__“   r&   zModularInteger.__eq__c                 C  rD   r
   )rC   rE   Úner1   r   r   r   Ú__ne__–   r&   zModularInteger.__ne__c                 C  rD   r
   )rC   rE   Últr1   r   r   r   Ú__lt__™   r&   zModularInteger.__lt__c                 C  rD   r
   )rC   rE   Úler1   r   r   r   Ú__le__œ   r&   zModularInteger.__le__c                 C  rD   r
   )rC   rE   Úgtr1   r   r   r   Ú__gt__Ÿ   r&   zModularInteger.__gt__c                 C  rD   r
   )rC   rE   Úger1   r   r   r   Ú__ge__¢   r&   zModularInteger.__ge__c                 C  s
   t | jƒS r
   )Úboolr	   r   r   r   r   Ú__bool__¥   r3   zModularInteger.__bool__c                 C  s   | j  || j¡S r
   )r   r:   r   )r'   Úvaluer   r   r   r8   ¨   s   zModularInteger._invertc                 C  s   |   |  | j¡¡S r
   )r   r8   r	   r   r   r   r   r:   ¬   r   zModularInteger.invertN))r   Ú
__module__Ú__qualname__Ú__doc__r   r   r#   r   Ú	__slots__r   r   r   r   r   r    r   r$   r%   Úclassmethodr)   r-   r2   r4   r5   r6   r7   r9   r;   r<   r=   rA   rC   rG   rI   rK   rM   rO   rQ   rS   r8   r:   r   r   r   r   r      sJ    	
	
r   z0dict[tuple[Any, Any, Any], type[ModularInteger]]Ú_modular_integer_cachec                   s¬   zˆ   ˆ¡‰W n ty   d}Y nw d}|rˆdk r!tdˆ ƒ‚ˆˆ ˆf}zt| }W |S  tyU   G ‡ ‡‡‡fdd„dtƒ}ˆrIdˆ |_ndˆ |_|t|< Y |S w )	z1Create custom class for specific integer modulus.FTé   z*modulus must be a positive integer, got %sc                      s    e Zd Z”” ”ZZZ”ZdS )z"ModularIntegerFactory.<locals>.clsN)r   rU   rV   r   r   r#   r   r   ©Ú_domÚ_modÚ_symr   r   r   r'   Â   s    r'   zSymmetricModularIntegerMod%szModularIntegerMod%s)r   r   Ú
ValueErrorrZ   ÚKeyErrorr   r   )r^   r]   r_   r   ÚokÚkeyr'   r   r\   r   ÚModularIntegerFactory±   s(   ÿ

ô

ôrd   )rW   Ú
__future__r   Útypingr   rE   Zsympy.polys.polyutilsr   Zsympy.polys.polyerrorsr   Z!sympy.polys.domains.domainelementr   Zsympy.utilitiesr   r   rZ   Ú__annotations__rd   r   r   r   r   Ú<module>   s     !