
    wgm,                         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y)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                 2   dx}x}x}}g }|j                   du rd }nd }| D ]  }|j                  r|j                  rd}|j                  r|r yd}|j	                  |       Ct        |      }	|	rxd}|	\  }
}|
j                  r'|j                  r|
j                  r|j                  sd}d}|
j                  r|j	                  |
       |j                  s|j	                  |        ||      r|r yd} y |rt        d |D              nd}|rt        | |      \  }}||fS |r|rt        |      }n:|rt        |      }n+|s|j                  r|rt        nt        }n|rt        nt        }| D cg c]  }|j                  |       }}||fS c c}w )	z?Handle simple domains, e.g.: ZZ, QQ, RR and algebraic domains. FTc                 6    | j                   xr | j                  S N	is_numberis_algebraiccoeffs    \/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/polys/constructor.py<lambda>z#_construct_simple.<locals>.<lambda>   s    U__%K9K9K     c                      y)NF r   s    r   r   z#_construct_simple.<locals>.<lambda>   s    r   Nc              3   4   K   | ]  }|j                     y wr   _prec.0cs     r   	<genexpr>z$_construct_simple.<locals>.<genexpr>>        2q1772   5   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ZM
}}K* !## 	^^$$U+%e,J 	!1==Q]]LLQ\\$(	!Fzz%,,Q/zz%,,Q/e$ !
 C!J 7Ds2M22H-fc: 6> i!x0FH-F#))&TBF&TBF8>?u&##E*??6> @s   6Fc                    ddl m} t               fd |       }t        t	                     |dd      \  }}t        d t        |      D              }t        j                  |f      j                  j                         c|D cg c]#  }j                  j                  |t              % }}t        t        |            fd|D 	cg c]
  }	 |	       }
}	|
fS c c}w c c}	w )zDWe know that coefficients are algebraic so construct the extension. r   )primitive_elementc                 2   g }| D ]  }|j                   rdt        j                  |      f}nW|j                  rd |j                        f}n6|j
                  rd |j                        f}nd|f}j                  |       |j                  |        |S )NQ+*e)r+   r   r2   is_Addargsis_Muladdr.   )rI   treesatreebuild_treesextss       r   rO   z)_construct_algebraic.<locals>.build_treesW   s     
	A}}R]]1-.[01[01QxLL
	 r   T)expolysc              3   ,   K   | ]  \  }}||z    y wr   r   )r"   sexts      r   r$   z'_construct_algebraic.<locals>.<genexpr>j   s     3Cqu3s   c                     | \  }}|dk(  r"j                   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        )NrD   rE   c              3   .   K   | ]  } |        y wr   r   r"   rM   convert_trees     r   r$   z=_construct_algebraic.<locals>.convert_tree.<locals>.<genexpr>v        6AQ6   rF   c              3   .   K   | ]  } |        y wr   r   rX   s     r   r$   z=_construct_algebraic.<locals>.convert_tree.<locals>.<genexpr>x   rZ   r[   rG   )dtype	from_listr   sumzeror   RuntimeError)rN   oprI   rY   r>   exts_mapgs      r   rY   z*_construct_algebraic.<locals>.convert_treeq   s{    D9<<))4&!R883Y66DD3Y66663YD>!r   )sympy.polys.numberfieldsrB   setlistr   r_   zipr   algebraic_fieldrepto_listr]   r^   dict)r3   r4   rB   rL   spanHroothexts_domrN   r?   rO   rY   r>   rP   rc   rd   s              @@@@@@r   r0   r0   Q   s    :5D ED"4D=JAtQ33tT?33D""At9-quu}}IFA:;<Q&&q!R0<H<Ch'(H .33Tl4 3F36>% =  4s   (C:%C?c                    g g }}| D ]7  }|j                         \  }}|j                  |       |j                  |       9 t        ||z         \  }}|sy|j                  <t	        d |D              ryt               }	|D ]  }
|
j                  }|	|z  r y|	|z  }	 t        |      }t        |      dz  }|d| }||d }|j                  rd}n$dd|z  }}|D ]  }t        |      dkD  s||vsd} n t               } |sMt        ||      D ]=  \  }}|   }|j                         D ]   \  }}||z  }| j                  |       |||<   " ? ndt        ||      D ]U  \  }}| j                  t        |j                                      | j                  t        |j                                      W dx}x}}g }| D ]  }|j                  r|j                   rd}|j"                  rd}|j                  |       ?t%        |      }|Md}|\  }}|j                  r(|j                  r|j                   r|j                   rd}d}|j"                  r|j                  |       |j"                  s|j                  |        |rt'        d |D              nd	}|r|rt)        |
      }n0|rt+        |
      }n!|r|rt,        }nt.        }n|rt0        }nt2        }g }|s] |j4                  | }|D ]E  }|j                         D ]  \  }}|j7                  |      ||<    |j                   ||             G ||fS  |j8                  | }t        ||      D ]v  \  }}|j                         D ]  \  }}|j7                  |      ||<    |j                         D ]  \  }}|j7                  |      ||<    |j                   |||f             x ||fS )z<Handle composite domains, e.g.: ZZ[X], QQ[X], ZZ(X), QQ(X). Nc              3   P   K   | ]  }|j                   xr |j                     y wr   r   )r"   gens     r   r$   z'_construct_composite.<locals>.<genexpr>   s"     Bcs}}1!1!11Bs   $&   TF)r      c              3   4   K   | ]  }|j                     y wr   r   r!   s     r   r$   z'_construct_composite.<locals>.<genexpr>   r%   r&   r'   r(   )as_numer_denomr.   r   	compositeanyrf   free_symbolslenr1   rh   itemsrK   updaterg   valuesr+   r,   r-   r   r/   r   r   r
   r	   r   r   	poly_ringr2   
frac_field)r3   r4   numersdenomsr   numerdenomrR   gensall_symbolsrt   symbolsnk	fractionszerosmonomr5   r6   r7   r9   r:   r;   r<   r=   groundr?   r>   s                               r   _construct_compositer      s   FF ++-uee	 +6F?;KE4
}}BTBBe 	'C&&GW$w&	' 	D	AE
AA2AYF12YF
yy	 $q&5	 	E5zA~e!3 		
 UF/ 	%LE5%LE % %u

5!$e%	%  / 	0LE5MM$u||~./MM$u||~./	0 &+*I*M 0## 	^^F  '%e,J% 	!1==Q]]LLQ\\$(	!Fzz%,,Q/zz%,,Q/)0, 7Ds2M22H)8,	)	FF	F!!!4( 	)E % 8u%007e8 MM&-(		)" 6> #""D)/ 	2LE5 % 8u%007e8 !& 8u%007e8 MM&%01	2 6>r   c                 h    t         g }}| D ]"  }|j                  |j                  |             $ ||fS )z6The last resort case, i.e. use the expression domain. )r   r.   r2   )r3   r4   r>   r?   r   s        r   _construct_expressionr      s=    FF 0f''./0 6>r   c           	      :   t        |      }t        | d      rFt        | t              r3| sg g }}n2t	        t        t	        | j                                      \  }}n| }n| g}t	        t        t        |            }t        ||      }||dur|\  }}nDt        ||      \  }}n4|j                  du rd}nt        ||      }||\  }}nt        ||      \  }}t        | d      r4t        | 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
isinstancerl   rg   rh   r}   mapr   r@   r   ry   r   )objrI   r4   monomsr3   r?   r>   s          r   construct_domainr   
  s"   f 
CsJc4 !#R!%c4		+<&=!>F#gv&'Fvs+F#NFF263?NFF==E!F)&#6F#NFF263?NFFsJc4 4S%8 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   r@   r0   r   r   r   r   r   r   <module>r      sW    6   ) & 6 6 9 3 1 : "?D/dzz x! x!r   