
    sg                         d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
mZ ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZ e G d de	e             Ze G d	 d
e
e             Zej<                  Ze G d de             Z y)zReal and complex elements.     )MPQ)DomainElement)public)PythonMPContext_mpf_mpc	_constant)MPZ_ONEfzerofonefinffninffnanround_nearestmpf_mulrepr_dps	int_typesfrom_int
from_floatfrom_strto_rationalc                   4    e Zd ZdZdZd Z ed e      Zd Zy)RealElementzAn element of a real domain. __mpf__c                     || _         y Nr   selfvals     Q/var/www/html/venv/lib/python3.12/site-packages/sympy/polys/domains/mpelements.py_set_mpfzRealElement._set_mpf   	        c                     | j                   S r   r   r   s    r!   <lambda>zRealElement.<lambda>   
    $,, r$   c                 .    | j                   j                  S r   context_parentr&   s    r!   parentzRealElement.parent       ||###r$   N)	__name__
__module____qualname____doc__	__slots__r"   property_mpf_r-    r$   r!   r   r      s#    'I .9E$r$   r   c                   4    e Zd ZdZdZd Z ed e      Zd Zy)ComplexElementz An element of a complex domain. __mpc__c                     || _         y r   r9   r   s     r!   _set_mpczComplexElement._set_mpc"   r#   r$   c                     | j                   S r   r9   r&   s    r!   r'   zComplexElement.<lambda>%   r(   r$   c                 .    | j                   j                  S r   r*   r&   s    r!   r-   zComplexElement.parent'   r.   r$   N)	r/   r0   r1   r2   r3   r<   r4   _mpc_r-   r6   r$   r!   r8   r8      s#    *I .9E$r$   r8   c                   \    e Zd ZddZd Zd Zd Zd Zed        Z	ed        Z
dd	Zdd
Zy)	MPContextNc                    |t         g| _        || j                  |       n| j                  |       t        | _        t        | _        | j
                  t        | j                  g| j
                  _	        | j                  t        | j                  g| j                  _	        |r| | j
                  _
        n| | j                  _
        t        | _        | j
                  t        | j                  g| j                  _	        | | j                  _
        | j
                  | j                  | j                  g| _        d| _        d| _        || j!                         | _        n&|du rt$        | _        n| j'                  |      | _        | j)                  | j"                        | _        | j*                  sd| _        nt/        d| j*                  z        | _        | j)                  t$              | _        | j)                  t2              | _        | j7                  t$        t2        f      | _        | j)                  t:              | _        | j)                  t>              | _         | j)                  tB              | _"        y )NTFi@B    )#r   _prec_rounding	_set_prec_set_dpsr   mpfr8   mpcnew_ctxdatar+   r	   constanttypestrap_complexpretty	_make_toltolr   _convert_tolmake_mpf	tolerance	max_denomintzeror   onemake_mpcjr   infr   ninfr   nan)ctxprecdpsrP   reals        r!   __init__zMPContext.__init__/   s   "M2;MM$LL GGS#*<*<=GGS#*<*<=!CGGO!CGGO !$#s/A/A B"WWcggs||4	
;mmoCGE\CG&&s+CGSWW-}}#CM#--0CM<<&,,t$eT]+,,t$<<&,,t$r$   c                 L    d}dt         d| j                  z
  df}t        ||      S )N)r            r   rC   )r
   r^   r   )r]   hundredepss      r!   rO   zMPContext._make_tol^   s*    '1SXX:q)w$$r$   c                 @    | j                  | j                               S r   )rR   rO   r]   s    r!   make_tolzMPContext.make_tolc   s    ||CMMO,,r$   c                    t        |t              rt        |      S t        |t              rt	        |      S t        |d      r|j                  S | j                  \  }}t        |t              rt        |||      S t        d|z        )Nr5   zexpected a real number, got %s)
isinstancer   r   floatr   hasattrr5   rD   strr   
ValueError)r]   rP   r^   roundings       r!   rQ   zMPContext._convert_tolf   sw    c9%C= c5!c?"3 99++hc3Cx009C?@@r$   c                 0    t        dt        |      z         )Nzcannot create mpf from )	TypeErrorrepr)r]   xstringss      r!   _convert_fallbackzMPContext._convert_fallbackr   s    1DG;<<r$   c                 ,    t        | j                        S r   )r   _precri   s    r!   _repr_digitszMPContext._repr_digitsu   s    		""r$   c                     | j                   S r   )_dpsri   s    r!   _str_digitszMPContext._str_digitsy   s    xxr$   c                 *   t        |j                        \  }}t        |      }|r|| j                  k  r||fS d\  }}}}||}
}		 |	|
z  }|||z  z   }|| j                  kD  rn|||||z  z   |f\  }}}}|
|	||
z  z
  }
}	9| j                  |z
  |z  }t	        ||      }t	        |||z  z   |||z  z         }t	        ||      }|r|s||fS t        ||z
        t        ||z
        k  r|j                  |j                  fS |j                  |j                  fS )N)r   rC   rC   r   )r   r5   rU   rT   r   abs	numeratordenominator)r]   slimitpqp0q0p1q1ndaq2knumberbound1bound2s                    r!   r   zMPContext.to_rational}   sE   177#1
 FS]]*a4K#BB!11AadBCMM!R!B$Y2NBBa!A#gqA  ]]R"$QR!B$YQrT	*RVa4K&!S&%99##V%7%777##V%7%777r$   c                 @   | j                  |      }|"| | j                  xs | j                         x}}|| j                  |      }n|| j                  |      }t        ||z
        }||k  ryt        |      }t        |      }||k  r
||z  }||k  S ||z  }||k  S )NT)convertrS   rj   r   )	r]   r   trel_epsabs_epsdiffabssabsterrs	            r!   almosteqzMPContext.almosteq   s    KKN?w # ??Gg?kk'*G_kk'*G1Q3x7?1v1v$;t)C g~ t)Cg~r$   )5   NNF)T)NN)r/   r0   r1   ra   rO   rj   rQ   rw   r4   rz   r}   r   r   r6   r$   r!   rA   rA   ,   sP    -%^%
-
A= # #  !8Fr$   rA   N)!r2   sympy.external.gmpyr   !sympy.polys.domains.domainelementr   sympy.utilitiesr   mpmath.ctx_mp_pythonr   r   r   r	   mpmath.libmpr
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r8   object__new__rI   rA   r6   r$   r!   <module>r      s    ! $ ; " G G1 1 1 1
 $$ $ $ $T= $ $ nnD D Dr$   