
    MZd                        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
ee             Zi Zded<   d Zy)z1Implementation of :class:`ModularInteger` class.     )annotations)AnyN)PicklableWithSlots)CoercionFailed)DomainElement)publicc                      e Zd ZdZd\  ZZZZdZd Z	d Z
d Zd Zd Zd	 Zd
 Zd Zd Zed        Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d  Z&ed!        Z'd" Z(y#)$ModularIntegerz(A class representing a modular integer. )NNNN)valc                    | j                   S N)_parentselfs    D/usr/lib/python3/dist-packages/sympy/polys/domains/modularinteger.pyparentzModularInteger.parent   s    ||    c                    t        || j                        r|j                  | j                  z  | _        y | j                  j                  |      | j                  z  | _        y r   )
isinstance	__class__r   moddomconvert)r   r   s     r   __init__zModularInteger.__init__   sC    c4>>*ww)DHxx'',txx7DHr   c                D    t        | j                  | j                  f      S r   )hashr   r   r   s    r   __hash__zModularInteger.__hash__   s    TXXtxx())r   c                N    | j                   j                  d| j                  dS )N())r   __name__r   r   s    r   __repr__zModularInteger.__repr__"   s    >>22DHH==r   c                8    | j                   d| j                  S )Nz mod )r   r   r   s    r   __str__zModularInteger.__str__%   s    "hh11r   c                4    t        | j                               S r   )intto_intr   s    r   __int__zModularInteger.__int__(   s    4;;=!!r   c                    | j                   rA| j                  | j                  dz  k  r| j                  S | j                  | j                  z
  S | j                  S )N   )symr   r   r   s    r   r'   zModularInteger.to_int+   sB    88xx488q=(xxxx$((**88Or   c                    | S r    r   s    r   __pos__zModularInteger.__pos__4   s    r   c                :    | j                  | j                         S r   )r   r   r   s    r   __neg__zModularInteger.__neg__7   s    ~~txxi((r   c                    t        ||       r|j                  S 	 | j                  j                  |      S # t        $ r Y y w xY wr   )r   r   r   r   r   )clsothers     r   _get_valzModularInteger._get_val:   s@    eS!99wwu--! s   5 	A Ac                p    | j                  |      }|| j                  | j                  |z         S t        S r   r4   r   r   NotImplementedr   r3   r   s      r   __add__zModularInteger.__add__D   2    mmE"?>>$((S.11!!r   c                $    | j                  |      S r   r9   r   r3   s     r   __radd__zModularInteger.__radd__L       ||E""r   c                p    | j                  |      }|| j                  | j                  |z
        S t        S r   r6   r8   s      r   __sub__zModularInteger.__sub__O   r:   r   c                &    |  j                  |      S r   r<   r=   s     r   __rsub__zModularInteger.__rsub__W   s    u%%r   c                p    | j                  |      }|| j                  | j                  |z        S t        S r   r6   r8   s      r   __mul__zModularInteger.__mul__Z   r:   r   c                $    | j                  |      S r   )rE   r=   s     r   __rmul__zModularInteger.__rmul__b   r?   r   c                    | j                  |      }|-| j                  | j                  | j                  |      z        S t        S r   )r4   r   r   _invertr7   r8   s      r   __truediv__zModularInteger.__truediv__e   s<    mmE"?>>$((T\\#->">??!!r   c                @    | j                         j                  |      S r   )invertrE   r=   s     r   __rtruediv__zModularInteger.__rtruediv__m   s    {{}$$U++r   c                p    | j                  |      }|| j                  | j                  |z        S t        S r   r6   r8   s      r   __mod__zModularInteger.__mod__p   r:   r   c                p    | j                  |      }|| j                  || j                  z        S t        S r   r6   r8   s      r   __rmod__zModularInteger.__rmod__x   s2    mmE"?>>#.11!!r   c                   |s%| j                  | j                  j                        S |dk  r| j                         j                  | }}n| j                  }| j                  t        |t        |      | j                              S )Nr   )r   r   onerL   r   powr&   r   )r   expr   s      r   __pow__zModularInteger.__pow__   sc    >>$((,,//7{{}((3$C((C~~c#s3x:;;r   c                t    | j                  |      }|  || j                  || j                  z        S t        S r   )r4   r   r   r7   )r   r3   opr   s       r   _comparezModularInteger._compare   s4    mmE"?dhhdhh//!!r   c                B    | j                  |t        j                        S r   )rY   operatoreqr=   s     r   __eq__zModularInteger.__eq__       }}UHKK00r   c                B    | j                  |t        j                        S r   )rY   r[   ner=   s     r   __ne__zModularInteger.__ne__   r^   r   c                B    | j                  |t        j                        S r   )rY   r[   ltr=   s     r   __lt__zModularInteger.__lt__   r^   r   c                B    | j                  |t        j                        S r   )rY   r[   ler=   s     r   __le__zModularInteger.__le__   r^   r   c                B    | j                  |t        j                        S r   )rY   r[   gtr=   s     r   __gt__zModularInteger.__gt__   r^   r   c                B    | j                  |t        j                        S r   )rY   r[   ger=   s     r   __ge__zModularInteger.__ge__   r^   r   c                ,    t        | j                        S r   )boolr   r   s    r   __bool__zModularInteger.__bool__   s    DHH~r   c                N    | j                   j                  || j                        S r   )r   rL   r   )r2   values     r   rI   zModularInteger._invert   s    ww~~eSWW--r   c                V    | j                  | j                  | j                              S r   )r   rI   r   r   s    r   rL   zModularInteger.invert   s    ~~dll488455r   N))r!   
__module____qualname____doc__r   r   r+   r   	__slots__r   r   r   r"   r$   r(   r'   r.   r0   classmethodr4   r9   r>   rA   rC   rE   rG   rJ   rM   rO   rQ   rV   rY   r]   ra   rd   rg   rj   rm   rp   rI   rL   r-   r   r   r
   r
      s    23Cc7I8*>2")  "#"&"#",""	<"111111 . .6r   r
   z0dict[tuple[Any, Any, Any], type[ModularInteger]]_modular_integer_cachec                (    	 j                          d}|r dk  rt        d z         f}	 t        |   }|S # t        $ r d}Y 3w xY w# t        $ r8  G  fddt
              }rd z  |_        n
d z  |_        |t        |<   Y |S w xY w)	z1Create custom class for specific integer modulus.TF   z*modulus must be a positive integer, got %sc                  (    e Zd ZW W  W cZZZW Zy)"ModularIntegerFactory.<locals>.clsN)r!   rt   ru   r   r   r+   r   )_dom_mod_symr   s   r   r2   r}      s     $MCcGr   r2   zSymmetricModularIntegerMod%szModularIntegerMod%s)r   r   
ValueErrorry   KeyErrorr
   r!   )r   r~   r   r   okkeyr2   s   ````   r   ModularIntegerFactoryr      s    ||D! ELMM
d
C*$S) J1    
*	 	. 	 9D@CL047CL&)s#J
*s    ? 	A AA=BB)rv   
__future__r   typingr   r[   sympy.polys.polyutilsr   sympy.polys.polyerrorsr   !sympy.polys.domains.domainelementr   sympy.utilitiesr   r
   ry   __annotations__r   r-   r   r   <module>r      sR    7 "   4 1 ; "^6' ^6 ^6@ LN H Mr   