B
    [                 @   s   d Z ddlmZ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 ddlmZ ddlmZ d	d
 Zdd Zdd Zdd Zedd ZdS )z0Tools for constructing domains for expressions.     )print_functiondivision)parallel_dict_from_basic)build_options)ZZQQRREX)	RealField)public)sympifyc       
      C   s   i dddf\}}}}|j dkr(dd }ndd }xP| D ]H}|jrL|js~d}q6|jrb|s\d}q~dS q6||rz|std}q~dS q6dS q6W |rt| |\}}nX|rtdd | D }	t|	d	}n|js|rt}nt	}g }x| D ]}|
|| qW ||fS )
z?Handle simple domains, e.g.: ZZ, QQ, RR and algebraic domains. FTc             S   s   | j o
| jS )N)	is_numberis_algebraic)coeff r   6lib/python3.7/site-packages/sympy/polys/constructor.py<lambda>   s    z#_construct_simple.<locals>.<lambda>c             S   s   dS )NFr   )r   r   r   r   r      s    Nc             S   s   g | ]
}|j qS r   )_prec).0cr   r   r   
<listcomp>1   s    z%_construct_simple.<locals>.<listcomp>)prec)	extensionis_Rational
is_Integeris_Float_construct_algebraicmaxr
   fieldr   r   append
from_sympy)
coeffsoptresult	rationalsrealsZ
algebraicsr   r   domainmax_precr   r   r   _construct_simple   s:    




r(   c             C   sR  ddl m} g tg  }}x~| D ]v}|jr<ddt|f}nP| d }||8 }| d }|| }|| t|}t|}|||f}|	| q W t
|}||ddd\}}	}
tdd t|	|D }t||f|jj }}xbt|D ]V\}\}}}|dk	r,||j|
|| |t | }n|j|g|t}|||< qW ||fS )zDWe know that coefficients are algebraic so construct the extension. r   )primitive_elementNT)expolysc             S   s   g | ]\}}|| qS r   r   )r   sZextr   r   r   r   ]   s    z(_construct_algebraic.<locals>.<listcomp>)Zsympy.polys.numberfieldsr)   setr   r   r    Zas_coeff_addZas_coeff_muladdr   listsumzipZalgebraic_fieldZrep	enumerateZdtype	from_listindex)r!   r"   r)   r#   Zextsr   abgspanHrootr&   ir   r   r   r   A   s0    





$r   c             C   s  g g  }}x,| D ]$}|  \}}|| || qW t|| \}}|sPdS |jdkrtdd |D rpdS tg }	x&|D ]}
|
j}|	|@ rdS |	|O }	q~W t|}t|d }|d| }||d }|jrd}n8dd|  }}x(|D ] }t|dks||krd}P qW tg } |stxt	||D ]D\}}|| }x0|
 D ]$\}}|| }| | |||< qDW q*W n>x<t	||D ].\}}| t|  | t|  qW d	\}}x2| D ]*}|jr|jsd}n|jrd}P qW |rtd
d | D }t|d}n|rt}nt}g }|s||j| }x|D ]:}x$|
 D ]\}}||||< qJW ||| q<W n|j| }xvt	||D ]h\}}x$|
 D ]\}}||||< qW x$|
 D ]\}}||||< qW ||||f qW ||fS )z<Handle composite domains, e.g.: ZZ[X], QQ[X], ZZ(X), QQ(X). Nc             s   s   | ]}|j o|jV  qd S )N)r   r   )r   genr   r   r   	<genexpr>{   s    z'_construct_composite.<locals>.<genexpr>   TF)r      )FFc             S   s   g | ]
}|j qS r   )r   )r   r   r   r   r   r      s    z(_construct_composite.<locals>.<listcomp>)r   )Zas_numer_denomr   r   	compositeanyr-   Zfree_symbolslenr   r1   itemsr.   updater/   valuesr   r   r   r   r
   r   r   Z	poly_ringr    Z
frac_field)r!   r"   ZnumersZdenomsr   ZnumerZdenomr+   ZgensZall_symbolsr<   ZsymbolsnkZ	fractionsZzerosZmonomr$   r%   r'   Zgroundr#   r&   r   r   r   _construct_compositel   s    










rH   c             C   s0   t g  }}x| D ]}||| qW ||fS )z6The last resort case, i.e. use the expression domain. )r	   r   r    )r!   r"   r&   r#   r   r   r   r   _construct_expression   s    

rI   c             K   s  t |}t| drLt| trF| s,g g  }}qJttt|   \}}qR| }n| g}ttt|}t	||}|dk	r|dk	r|\}}qt
||\}}n:|jdkrd}n
t||}|dk	r|\}}nt
||\}}t| drt| tr|ttt||fS ||fS n||d fS dS )z9Construct a minimal domain for the list of coefficients. __iter__NFr   )r   hasattr
isinstancedictr/   r1   rC   mapr   r(   rI   r@   rH   )objargsr"   Zmonomsr!   r#   r&   r   r   r   construct_domain   s2    








rQ   N)__doc__Z
__future__r   r   Zsympy.polys.polyutilsr   Zsympy.polys.polyoptionsr   Zsympy.polys.domainsr   r   r   r	   Zsympy.polys.domains.realfieldr
   Zsympy.utilitiesr   Z
sympy.corer   r(   r   rH   rI   rQ   r   r   r   r   <module>   s   4+f
