+
    im,                         R t ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ RI	H
t
HtHtHtHt ^ RIHt ^ RIHt ^ RIHt ^ R	IHt ^ R
IHt R tR tR tR t]R 4       tR# )z0Tools for constructing domains for expressions. )prod)sympify)pure_complex)ordered)ZZQQZZ_IQQ_IEX)ComplexField)	RealField)build_options)parallel_dict_from_basic)publicc                R   R;p;p;rE. pVP                   RJ d   R pMR pV  EF1  pVP                  '       d   VP                  '       g   RpK,  K.  VP                  '       d!   V'       d    R# RpVP	                  V4       K`  \        V4      p	V	'       d   RpV	w  rV
P                  '       d;   VP                  '       d)   V
P                  '       d   VP                  '       g   RpK  RpV
P                  '       d   VP	                  V
4       VP                  '       d   VP	                  V4       EK  EK  V! V4      '       d   V'       d    R# RpEK1   R# 	  V'       d   \        R V 4       4      M^5pV'       d   \        W4      w  rW3# V'       d   V'       d   \        VR7      pMXV'       d   \        VR7      pMCV'       g   VP                  '       d   V'       d   \        M\        pMV'       d   \        M\        pV  Uu. uF  qP                  V4      NK  	  ppW3# u upi )z?Handle simple domains, e.g.: ZZ, QQ, RR and algebraic domains. FTc                 B    V P                   ;'       d    V P                  # N	is_numberis_algebraiccoeffs   &w/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/site-packages/sympy/polys/constructor.py<lambda>#_construct_simple.<locals>.<lambda>   s    U__%K%K9K9K%K    c                     R # )F r   s   &r   r   r      s    Ur   Nc              3   8   "   T F  qP                   x  K  	  R # 5ir   _prec.0cs   & r   	<genexpr>$_construct_simple.<locals>.<genexpr>>        2Mq77M   prec)	extensionis_Rational
is_Integeris_Floatappendr   max_construct_algebraicr   r   fieldr	   r   r   r   
from_sympy)coeffsopt	rationalsfloats	complexes
algebraicsfloat_numbersr   r   
is_complexxymax_precdomainresults   &&             r   _construct_simpler@      s   277I77M
}}K*### 	 $^^^$$U+%e,J 	!===Q]]]LLLQ\\\$(	!Fzzz%,,Q/zzz%,,Q/ "e$$ !
 C J 7Ds2M22H-f: > i!x0FH-F#)))&TBF&TBF8>?u##E*?> @s   H$c                  aaaaaa ^ RI Hp \        4       oVV3R loS! V 4      p\        \	        S4      4      oV! SRRR7      w  orE\        R \        VS4       4       4      p\        P                  ! SV34      SP                  P                  4       uooV Uu. uF$  pSP                  P                  VS\        4      NK&  	  pp\        \        SV4      4      oVVVV3R loV U	u. uF  p	S! V	4      NK  	  p
p	SV
3# u upi u up	i )zDWe know that coefficients are algebraic so construct the extension. )primitive_elementc                 \  < . pV  F  pVP                   '       d   R \        P                  ! V4      3pMcVP                  '       d   RS! VP                  4      3pM<VP
                  '       d   RS! VP                  4      3pMRV3pSP                  V4       VP                  V4       K  	  V# )Q+*e)r+   r   r2   is_Addargsis_Muladdr.   )rI   treesatreebuild_treesextss   &   r   rO   )_construct_algebraic.<locals>.build_treesW   s    A}}}R]]1-.[01[01QxLL  r   T)expolysc              3   6   "   T F  w  rW,          x  K  	  R # 5ir   r   )r"   sexts   &  r   r$   '_construct_algebraic.<locals>.<genexpr>j   s     3?quu?s   c                   < V w  rVR 8X  d#   SP                   P                  V.S\        4      # VR8X  d!   \        V3R lV 4       SP                  4      # VR8X  d   \        V3R lV 4       4      # VR8X  d
   SV,          # \        h)rD   rE   c              3   4   <"   T F  pS! V4      x  K  	  R # 5ir   r   r"   rM   convert_trees   & r   r$   =_construct_algebraic.<locals>.convert_tree.<locals>.<genexpr>v        6AQ   rF   c              3   4   <"   T F  pS! V4      x  K  	  R # 5ir   r   rZ   s   & r   r$   r\   x   r]   r^   rG   )dtype	from_listr   sumzeror   RuntimeError)rN   oprI   r[   r>   exts_mapgs   &  r   r[   *_construct_algebraic.<locals>.convert_treeq   sy    9<<))4&!R883Y66DD3Y66663YD>!r   )sympy.polys.numberfieldsrB   setlistr   rb   zipr   algebraic_fieldrepto_listr`   ra   dict)r3   r4   rB   rL   spanHroothexts_domrN   r?   rO   r[   r>   rP   rf   rg   s   &&         @@@@@@r   r0   r0   Q   s    :5D ED"4D=JAt33tT?33D""At9-quu}}IFA:;<!Q&&q!R0!H<Ch'(H  .33UTl4 UF36>% =  4s   *D )Dc                `	   . . r2V  F7  pVP                  4       w  rVVP                  V4       VP                  V4       K9  	  \        W#,           4      w  rxV'       g   R# VP                  fu   \        ;QJ d    R V 4       F  '       g   K   RM	  RM! R V 4       4      '       d   R# \        4       p	V F(  p
V
P                  pW,          '       d    R# W,          p	K*  	  \        V4      p\        V4      ^,          pVRV pW}R pVP                  '       d   RpM-RRV,          rV F  p\        V4      ^8  g	   W9  g   K  Rp M	  \        4       p V'       gV   \        W#4       FE  w  rVVX,          pVP                  4        F#  w  ppWF,          pV P                  V4       WEV&   K%  	  KG  	  Md\        W#4       FU  w  rVV P                  \        VP                  4       4      4       V P                  \        VP                  4       4      4       KW  	  R;p;pp. pV  EF  pVP                  '       d   VP                   '       g   RpK,  K.  VP"                  '       d   RpVP                  V4       KU  \%        V4      pVf   Kf  RpVw  ppVP                  '       d=   VP                  '       d+   VP                   '       d   VP                   '       g   RpK  K  RpVP"                  '       d   VP                  V4       VP"                  '       g   K  VP                  V4       EK  	  V'       d   \'        R V 4       4      M^5pV'       d   V'       d   \)        VR7      pMHV'       d   \+        VR7      pM3V'       d   V'       d   \,        pM\.        pMV'       d   \0        pM\2        p. pV'       gc   VP4                  ! V!  pV FH  pVP                  4        F  w  ppVP7                  V4      VV&   K  	  VP                  V! V4      4       KJ  	  VV3# VP8                  ! V!  p\        W#4       Fy  w  rVVP                  4        F  w  ppVP7                  V4      VV&   K  	  VP                  4        F  w  ppVP7                  V4      VV&   K  	  VP                  V! WV34      4       K{  	  VV3# )z<Handle composite domains, e.g.: ZZ[X], QQ[X], ZZ(X), QQ(X). Nc              3   `   "   T F$  qP                   ;'       d    VP                  x  K&  	  R # 5ir   r   )r"   gens   & r   r$   '_construct_composite.<locals>.<genexpr>   s#     BTc}}11!1!11Ts   ..TFc              3   8   "   T F  qP                   x  K  	  R # 5ir   r   r!   s   & r   r$   ry      r&   r'   r(   )    )as_numer_denomr.   r   	compositeanyrj   free_symbolslenr1   rl   itemsrK   updaterk   valuesr+   r,   r-   r   r/   r   r   r	   r   r   r   	poly_ringr2   
frac_field)r3   r4   numersdenomsr   numerdenomrS   gensall_symbolsrx   symbolsnk	fractionszerosmonomr5   r6   r7   r9   r:   r;   r<   r=   groundr?   r>   s   &&                          r   _construct_compositer      s   F++-ee	  +6?;KE
}}3BTB333BTBBBeC&&G$$&  	D	AE
AA2AYF2YF
yyy	 $q&5E5zA~!3 	 
 UF/LE%LE %u

5!$e !. 0  /LEMM$u||~./MM$u||~./ 0 &+*I*M### 	 $^^^F  '%e,J% 	!1===Q]]]LLLQ\\\$(	 .: "Fzzz%,,Q/zzz%,,Q/) , 7Ds2M22H)8,	)	FF	F!!4(E %u%007e !. MM&-(	 " 6> ""D)/LE %u%007e !. !&u%007e !. MM&%01 0 6>r   c                h    \         . r2V  F#  pVP                  VP                  V4      4       K%  	  W#3# )z6The last resort case, i.e. use the expression domain. )r
   r.   r2   )r3   r4   r>   r?   r   s   &&   r   _construct_expressionr      s4    Ff''./  >r   c           	     x   \        V4      p\        V R4      '       dR   \        V \        4      '       d9   V '       g   . . rCM2\	        \        \	        V P                  4       4      !  4      w  r4MT pMV .p\	        \        \        V4      4      p\        WB4      pVe   VRJd   Vw  rdMB\        WB4      w  rdM4VP                  RJ d   RpM\        WB4      pVe   Vw  rdM\        WB4      w  rd\        V R4      '       d:   \        V \        4      '       d!   V\        \	        \        XV4      4      4      3# Wd3# Wd^ ,          3# )ad  Construct a minimal domain for a list of expressions.

Explanation
===========

Given a list of normal SymPy expressions (of type :py:class:`~.Expr`)
``construct_domain`` will find a minimal :py:class:`~.Domain` that can
represent those expressions. The expressions will be converted to elements
of the domain and both the domain and the domain elements are returned.

Parameters
==========

obj: list or dict
    The expressions to build a domain for.

**args: keyword arguments
    Options that affect the choice of domain.

Returns
=======

(K, elements): Domain and list of domain elements
    The domain K that can represent the expressions and the list or dict
    of domain elements representing the same expressions as elements of K.

Examples
========

Given a list of :py:class:`~.Integer` ``construct_domain`` will return the
domain :ref:`ZZ` and a list of integers as elements of :ref:`ZZ`.

>>> from sympy import construct_domain, S
>>> expressions = [S(2), S(3), S(4)]
>>> K, elements = construct_domain(expressions)
>>> K
ZZ
>>> elements
[2, 3, 4]
>>> type(elements[0])  # doctest: +SKIP
<class 'int'>
>>> type(expressions[0])
<class 'sympy.core.numbers.Integer'>

If there are any :py:class:`~.Rational` then :ref:`QQ` is returned
instead.

>>> construct_domain([S(1)/2, S(3)/4])
(QQ, [1/2, 3/4])

If there are symbols then a polynomial ring :ref:`K[x]` is returned.

>>> from sympy import symbols
>>> x, y = symbols('x, y')
>>> construct_domain([2*x + 1, S(3)/4])
(QQ[x], [2*x + 1, 3/4])
>>> construct_domain([2*x + 1, y])
(ZZ[x,y], [2*x + 1, y])

If any symbols appear with negative powers then a rational function field
:ref:`K(x)` will be returned.

>>> construct_domain([y/x, x/(1 - y)])
(ZZ(x,y), [y/x, -x/(y - 1)])

Irrational algebraic numbers will result in the :ref:`EX` domain by
default. The keyword argument ``extension=True`` leads to the construction
of an algebraic number field :ref:`QQ(a)`.

>>> from sympy import sqrt
>>> construct_domain([sqrt(2)])
(EX, [EX(sqrt(2))])
>>> construct_domain([sqrt(2)], extension=True)  # doctest: +SKIP
(QQ<sqrt(2)>, [ANP([1, 0], [1, 0, -2], QQ)])

See also
========

Domain
Expr
__iter__NF)r   hasattr
isinstancerp   rk   rl   r   mapr   r@   r   r}   r   )objrI   r4   monomsr3   r?   r>   s   &,     r   construct_domainr   
  s   f 
CsJc4  !#R!%c4		+<&=!>F#gv&'Fv+F#NFF26?NFF==E!F)&6F#NFF26?NFsJc4  4S%8 9:::>!ay  r   N)__doc__mathr   
sympy.corer   sympy.core.evalfr   sympy.core.sortingr   sympy.polys.domainsr   r   r   r	   r
    sympy.polys.domains.complexfieldr   sympy.polys.domains.realfieldr   sympy.polys.polyoptionsr   sympy.polys.polyutilsr   sympy.utilitiesr   r@   r0   r   r   r   r   r   r   <module>r      sW    6   ) & 6 6 9 3 1 : "?D/dzz x! x!r   