a
    RG5d+                     @   sx   d Z ddlZddlmZ ddlmZ ddlmZ ddlZddl	m
ZmZ ejjZejjZG dd dZeeeefe_dS )	a  
PythonMPQ: Rational number type based on Python integers.

This class is intended as a pure Python fallback for when gmpy2 is not
installed. If gmpy2 is installed then its mpq type will be used instead. The
mpq type is around 20x faster. We could just use the stdlib Fraction class
here but that is slower:

    from fractions import Fraction
    from sympy.external.pythonmpq import PythonMPQ
    nums = range(1000)
    dens = range(5, 1005)
    rats = [Fraction(n, d) for n, d in zip(nums, dens)]
    sum(rats) # <--- 24 milliseconds
    rats = [PythonMPQ(n, d) for n, d in zip(nums, dens)]
    sum(rats) # <---  7 milliseconds

Both mpq and Fraction have some awkward features like the behaviour of
division with // and %:

    >>> from fractions import Fraction
    >>> Fraction(2, 3) % Fraction(1, 4)
    1/6

For the QQ domain we do not want this behaviour because there should be no
remainder when dividing rational numbers. SymPy does not make use of this
aspect of mpq when gmpy2 is installed. Since this class is a fallback for that
case we do not bother implementing e.g. __mod__ so that we can be sure we
are not using it when gmpy2 is installed either.
    N)gcd)Decimal)Fraction)TupleTypec                       s  e Zd ZU dZdZd?ddZedd Ze f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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<Z"e#e$d=f e%d><   Z&S )@	PythonMPQa  Rational number implementation that is intended to be compatible with
    gmpy2's mpq.

    Also slightly faster than fractions.Fraction.

    PythonMPQ should be treated as immutable although no effort is made to
    prevent mutation (since that might slow down calculations).
    	numeratordenominatorNc                 C   s   |durDt |trt |trt||}|| }|| }| ||S nbt |trZ| |dS t |trt| |j|jS t |tt	t
frt|}t |tr| |j|jS tddS )z3Construct PythonMPQ with gcd computation and checksN   z/PythonMPQ() requires numeric or string argument)
isinstanceintr   
_new_check_newr   r	   r
   r   floatstrr   	TypeError)clsr	   r
   divisor r   T/var/www/html/django/DPS/env/lib/python3.9/site-packages/sympy/external/pythonmpq.py__new__9   s    



zPythonMPQ.__new__c                 C   s:   |st d| d| n|dk r.| }| }| ||S )z@Construct PythonMPQ, check divide by zero and canonicalize signszZero divisor /r   )ZeroDivisionErrorr   )r   r	   r
   r   r   r   r   ]   s    zPythonMPQ._new_checkc                    s   t  | }||_||_|S )z+Construct PythonMPQ efficiently (no checks))superr   r	   r
   )r   r	   r
   obj	__class__r   r   r   g   s    zPythonMPQ._newc                 C   s*   | j | j }}|dk r"| |  S || S )z'Convert to int (truncates towards zero)r   r   )selfpqr   r   r   __int__o   s    zPythonMPQ.__int__c                 C   s   | j | j S )z Convert to float (approximately)r   r   r   r   r   	__float__v   s    zPythonMPQ.__float__c                 C   s
   t | jS )zTrue/False if nonzero/zero)boolr	   r"   r   r   r   __bool__z   s    zPythonMPQ.__bool__c                 C   sD   t |tr"| j|jko | j|jkS t || jr<| t|S tS dS )z=Compare equal with PythonMPQ, int, float, Decimal or FractionN)r   r   r	   r
   _compatible_types__eq__NotImplementedr   otherr   r   r   r'   ~   s    

zPythonMPQ.__eq__c                 C   sd   zt | jdt}W n ty(   t}Y n0 ttt| j| }| jdkrN|n| }|dkr`dS |S )zhash - same as mpq/Fractionr   )powr
   _PyHASH_MODULUS
ValueError_PyHASH_INFhashabsr	   )r   Zdinvhash_resultr   r   r   __hash__   s    
zPythonMPQ.__hash__c                 C   s   t | | j| jffS )zDeconstruct for pickling)typer	   r
   r"   r   r   r   
__reduce__   s    zPythonMPQ.__reduce__c                 C   s(   | j dkr| j d| j  S | j S dS )Convert to stringr   r   N)r
   r	   r"   r   r   r   __str__   s    
zPythonMPQ.__str__c                 C   s   d| j  d| j dS )r8   zMPQ(,)r   r"   r   r   r   __repr__   s    zPythonMPQ.__repr__c                 C   s2   t || jstS | j|j }|j| j }|||S )zHelper for lt/le/gt/ge)r   r&   r(   r	   r
   )r   r*   oplhsrhsr   r   r   _cmp   s
    zPythonMPQ._cmpc                 C   s   |  |tjS )zself < other)r@   operatorltr)   r   r   r   __lt__   s    zPythonMPQ.__lt__c                 C   s   |  |tjS )zself <= other)r@   rA   ler)   r   r   r   __le__   s    zPythonMPQ.__le__c                 C   s   |  |tjS )zself > other)r@   rA   gtr)   r   r   r   __gt__   s    zPythonMPQ.__gt__c                 C   s   |  |tjS )zself >= other)r@   rA   ger)   r   r   r   __ge__   s    zPythonMPQ.__ge__c                 C   s   |  t| j| jS )zabs(q))r   r2   r	   r
   r"   r   r   r   __abs__   s    zPythonMPQ.__abs__c                 C   s   | S )z+qr   r"   r   r   r   __pos__   s    zPythonMPQ.__pos__c                 C   s   |  | j | jS )z-q)r   r	   r
   r"   r   r   r   __neg__   s    zPythonMPQ.__neg__c                 C   s   t |tr| j| j }}|j|j }}t||}|dkrR|| ||  }|| }q|| ||  }	}
||
 ||	  |	|
  }}t||}|| |||   }}n&t |tr| j| j|  }| j}ntS | ||S )zq1 + q2r   r   r   r	   r
   r   r   r(   r   r   r*   apaqbpbqgr   r    q1q2g2r   r   r   __add__   s     




zPythonMPQ.__add__c                 C   s4   t |tr,| j| j|  }| j}| ||S tS dS )zz1 + q2N)r   r   r	   r
   r   r(   r   r*   r   r    r   r   r   __radd__   s
    
zPythonMPQ.__radd__c                 C   s   t |tr| j| j }}|j|j }}t||}|dkrR|| ||  }|| }q|| ||  }	}
||
 ||	  |	|
  }}t||}|| |||   }}n&t |tr| j| j|  }| j}ntS | ||S )zq1 - q2r   rM   rN   r   r   r   __sub__   s     




zPythonMPQ.__sub__c                 C   s4   t |tr,| j| | j }| j}| ||S tS dS )zz1 - q2N)r   r   r
   r	   r   r(   rX   r   r   r   __rsub__  s
    
zPythonMPQ.__rsub__c                 C   s   t |tr^| j| j }}|j|j }}t||}t||}|| ||  || ||   }}	n4t |trt|| j}
| j||
  }| j|
 }	ntS | ||	S )zq1 * q2rM   r   r*   rO   rP   rQ   rR   x1x2r   r    xr   r   r   __mul__  s    


$
zPythonMPQ.__mul__c                 C   sB   t |tr:t| j|}| j||  }| j| }| ||S tS dS )zz1 * q2N)r   r   r   r
   r	   r   r(   r   r*   r_   r   r    r   r   r   __rmul__   s    

zPythonMPQ.__rmul__c                 C   s<   | j | j }}|dk r(|||   }}}| || || S )zq ** zr   )r	   r
   r   )r   expr   r    r   r   r   __pow__*  s    zPythonMPQ.__pow__c                 C   s   t |tr^| j| j }}|j|j }}t||}t||}|| ||  || ||   }}	n4t |trt|| j}
| j|
 }| j||
  }	ntS | ||	S )zq1 / q2)r   r   r	   r
   r   r   r(   r   r\   r   r   r   __truediv__3  s    


$

zPythonMPQ.__truediv__c                 C   sB   t |tr:t| j|}| j||  }| j| }| ||S tS dS )zz / qN)r   r   r   r	   r
   r   r(   ra   r   r   r   __rtruediv__D  s    

zPythonMPQ.__rtruediv__r   .r&   )N)'__name__
__module____qualname____doc__	__slots__r   classmethodr   r   r!   r#   r%   r'   r5   r7   r9   r<   r@   rC   rE   rG   rI   rJ   rK   rL   rW   rY   rZ   r[   r`   rb   rd   re   rf   r&   tTupler   __annotations____classcell__r   r   r   r   r   .   sB   

$
	
!		
	
r   )rj   rA   mathr   decimalr   Z	fractionsr   systypingr   rm   r   	hash_infomodulusr.   infr0   r   r   r&   r   r   r   r   <module>   s      )