
    Edk,                         d Z ddlm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mZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ d Zd Zd Zd Zed             ZdS )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                    dx}x}x}}g }|j         du rd }nd }| D ]}|j        r
|j        sd}|j        r|r dS d}|                    |           7t          |          }	|	rad}|	\  }
}|
j        r|j        r|
j        r|j        sd}nd}|
j        r|                    |
           |j        r|                    |            ||          r|r dS d} dS |rt          d |D                       nd}|rt          | |          \  }n`|r|rt          |          n=|rt          |          n*|s|j
        r|rt          nt          n|rt          nt          fd	| D             }|fS )
z?Handle simple domains, e.g.: ZZ, QQ, RR and algebraic domains. FTc                     | j         o| j        S N	is_numberis_algebraiccoeffs    7lib/python3.11/site-packages/sympy/polys/constructor.py<lambda>z#_construct_simple.<locals>.<lambda>   s    U_%K9K     c                     dS )NF r   s    r   r   z#_construct_simple.<locals>.<lambda>   s    U r   Nc              3   $   K   | ]}|j         V  d S r   _prec.0cs     r   	<genexpr>z$_construct_simple.<locals>.<genexpr>>   $      22q17222222r   5   precc                 :    g | ]}                     |          S r   )
from_sympy)r"   r   domains     r   
<listcomp>z%_construct_simple.<locals>.<listcomp>L   s'    ???u&##E**???r   )	extensionis_Rational
is_Integeris_Floatappendr   max_construct_algebraicr   r   fieldr
   r   r	   r   )coeffsopt	rationalsfloats	complexes
algebraicsfloat_numbersr   r   
is_complexxymax_precresultr+   s                 @r   _construct_simplerA      s5   277I77ZM
} +KK** ! !  	# ! 	^ 	 ,uu$$U++++%e,,J  	!1= 	0Q] 	0L )Q\ )$(	!Fz 0%,,Q///z 0%,,Q///e$$  ! 55!

 tt 7DKs22M222222H @-fc:: 	/i 	/!x000FF 	/H---FF 	/#) 	/&.TTBFF&.TTBF???????6>r   c                   	
 ddl m} t                      	fd	 	|           }t          t	                               |dd          \  }}t          d t          |          D                       }t          j        |f          j	        j	        cfd|D             }t          t          |                    
fd

fd	|D             }|fS )
zDWe know that coefficients are algebraic so construct the extension. r   )primitive_elementc                    g }| D ]}|j         rdt          j        |          f}nM|j        rd |j                  f}n3|j        rd |j                  f}nd|f}                    |           |                    |           |S )NQ+*e)r.   r   r*   is_Addargsis_Muladdr1   )rJ   treesatreebuild_treesextss       r   rP   z)_construct_algebraic.<locals>.build_treesW   s     
	 
	A} R]1--. [[001 [[001QxLLr   T)expolysc                     g | ]
\  }}||z  S r   r   )r"   sexts      r   r,   z(_construct_algebraic.<locals>.<listcomp>j   s     66661c3666r   c                 R    g | ]#}j                             |t                    $S r   )dtype	from_listr   )r"   hr+   gs     r   r,   z(_construct_algebraic.<locals>.<listcomp>n   s-    <<<Q&&q!R00<<<r   c                    | \  }}|dk    r"j                             |gt                    S |dk    r!t          fd|D             j                  S |dk    rt          fd|D                       S |dk    r|         S t          )NrE   rF   c              3   .   K   | ]} |          V  d S r   r   r"   rN   convert_trees     r   r$   z=_construct_algebraic.<locals>.convert_tree.<locals>.<genexpr>v   +      66AQ666666r   rG   c              3   .   K   | ]} |          V  d S r   r   r^   s     r   r$   z=_construct_algebraic.<locals>.convert_tree.<locals>.<genexpr>x   r`   r   rH   )rX   rY   r   sumzeror   RuntimeError)rO   oprJ   r_   r+   exts_mapr[   s      r   r_   z*_construct_algebraic.<locals>.convert_treeq   s    D9 		<))4&!R8883Y 	6666666DDD3Y 	66666666663Y 	D>!r   c                 &    g | ]} |          S r   r   )r"   rO   r_   s     r   r,   z(_construct_algebraic.<locals>.<listcomp>~   s#    333Tll4  333r   )sympy.polys.numberfieldsrC   setlistr   rb   zipr   algebraic_fieldrepdict)r5   r6   rC   rM   spanHrootexts_domr@   rP   r_   r+   rQ   rf   r[   s            @@@@@@r   r3   r3   Q   sD   ::::::55D      KED""4D===JAtQ66St__66677D"At9--quyIFA<<<<<!<<<HCh''((H        4333U333F6>r   c                 :   g g }}| D ]C}|                                 \  }}|                    |           |                    |           Dt          ||z             \  }}|sdS |j        Bt	          d |D                       rdS t                      }	|D ]}
|
j        }|	|z  r dS |	|z  }	t          |          }t          |          dz  }|d|         }||d         }|j        rd}n'dd|z  }}|D ]}t          |          dk    s||vrd} nt                      } |sXt          ||          D ]F\  }}||         }|
                                D ]$\  }}||z  }|                     |           |||<   %Gn~t          ||          D ]m\  }}|                     t          |                                                     |                     t          |                                                     ndx}x}}g }| D ]}|j        r
|j        sd}|j        rd}|                    |           2t%          |          }|`d}|\  }}|j        r|j        r|j        r|j        sd}id}|j        r|                    |           |j        r|                    |           |rt'          d |D                       nd	}|r|rt)          |
          }n8|rt+          |
          }n%|r|rt,          }nt.          }n|rt0          }nt2          }g }|s` |j        | }|D ]R}|
                                D ]\  }}|                    |          ||<   |                     ||                     Sn |j        | }t          ||          D ]\  }}|
                                D ]\  }}|                    |          ||<   |
                                D ]\  }}|                    |          ||<   |                     |||f                     ||fS )z<Handle composite domains, e.g.: ZZ[X], QQ[X], ZZ(X), QQ(X). Nc              3   2   K   | ]}|j         o|j        V  d S r   r   )r"   gens     r   r$   z'_construct_composite.<locals>.<genexpr>   s,      BBcs}1!1BBBBBBr      TF)r      c              3   $   K   | ]}|j         V  d S r   r   r!   s     r   r$   z'_construct_composite.<locals>.<genexpr>   r%   r   r&   r'   )as_numer_denomr1   r   	compositeanyri   free_symbolslenr4   rk   itemsrL   updaterj   valuesr.   r/   r0   r   r2   r   r   r
   r	   r   r   	poly_ringr*   
frac_field)r5   r6   numersdenomsr   numerdenomrS   gensall_symbolsru   symbolsnk	fractionszerosmonomr7   r8   r9   r;   r<   r=   r>   r?   groundr@   r+   s                               r   _construct_compositer      s+   FF  ++--uee*6F?;;KE4 t
} 'BBTBBBBB 	4ee 	' 	'C&GW$ 'ttw&D		AE

AA2A2YF122YF
y 		 $q&5	 	 	E5zzA~ e!3  	 UUF 0// 	% 	%LE5%LE % % %u

5!!!$e%	%  // 	0 	0LE5MM$u||~~..///MM$u||~~..////%**I*M 0 0 	0# ! 	^ 	0F  ''''%e,,J 0 	!1= 0Q] 0L )Q\ )$(	!Fz 0%,,Q///z 0%,,Q///6CKs22M222222H ) 8,,,	 
)))	  	FFFF	 F 2!!4( 	) 	)E % 8 8u%0077eMM&&--((((		) #"D)// 	2 	2LE5 % 8 8u%0077e % 8 8u%0077eMM&&%0011116>r   c                 v    t           g }}| D ]*}|                    |                    |                     +||fS )z6The last resort case, i.e. use the expression domain. )r   r1   r*   )r5   r6   r+   r@   r   s        r   _construct_expressionr      sG    FF 0 0f''..////6>r   c           	         t          |          }t          | d          rXt          | t                    r@| sg g }}n?t	          t          t	          |                                                      \  }}n| }n| g}t	          t          t          |                    }t          ||          }||dur|\  }}nKt          ||          \  }}n7|j        du rd}nt          ||          }||\  }}nt          ||          \  }}t          | d          rEt          | t                    r,|t          t	          t          ||                              fS ||fS ||d         fS )aT	  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__NFr   )r   hasattr
isinstancern   rj   rk   r~   mapr   rA   r   rz   r   )objrJ   r6   monomsr5   r@   r+   s          r   construct_domainr   
  s   f 

CsJ 	c4   	 ?!#R!%c4		+<+<&=!>!>FF#gv&&''Fvs++F @ 	@#NFFF263??NFFF=E! 	7FF)&#66F 	@#NFFF263??NFFsJ !c4   	"4S%8%8 9 9::::6>!vay  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   rA   r3   r   r   r   r   r   r   <module>r      sC   6 6             ) ) ) ) ) ) & & & & & & 6 6 6 6 6 6 6 6 6 6 6 6 6 6 9 9 9 9 9 9 3 3 3 3 3 3 1 1 1 1 1 1 : : : : : : " " " " " "? ? ?D/ / /dz z zz   x! x! x! x! x!r   