B
    ddU                 @   s  d Z dgZddlmZmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZmZmZ ddlmZmZmZ ddlmZmZ dd	lZdd	lZG d
d dZG dd deZG dd deZG dd deZeG dd deZG dd deedZ G dd deedZ!G dd deedZ"G dd deedZ#G dd deedZ$G dd deedZ%G d d! d!eedZ&G d"d# d#eedZ'G d$d% d%eedZ(G d&d' d'eedZ)G d(d) d)eedZ*G d*d+ d+eedZ+G d,d- d-eedZ,G d.d/ d/eedZ-G d0d1 d1eedZ.G d2d3 d3eeedZ/G d4d5 d5eeedZ0G d6d7 d7eeedZ1G d8d9 d9eeedZ2G d:d; d;eeedZ3G d<d= d=eeedZ4G d>d? d?eedZ5G d@dA dAeeedZ6G dBdC dCeedZ7G dDdE dEeedZ8dLdFdGZ9dHdI Z:dJdK Z;e<  d	S )Mz>Options manager for :class:`~.Poly` and public API functions. Options    )DictType)ListOptional)Basicsympify)GeneratorsErrorOptionError	FlagError)numbered_symbolstopological_sortpublic)has_dupsis_sequenceNc               @   sL   e Zd ZdZdZdZg Zg Zg Zg Z	e
dd Ze
dd Ze
dd	 ZdS )
Optionz%Base class for all kinds of options. NFc             C   s   d S )N )clsr   r   d/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/sympy/polys/polyoptions.pydefault   s    zOption.defaultc             C   s   d S )Nr   )r   optionr   r   r   
preprocess#   s    zOption.preprocessc             C   s   d S )Nr   )r   optionsr   r   r   postprocess'   s    zOption.postprocess)__name__
__module____qualname____doc__r   is_Flagrequiresexcludesafterbeforeclassmethodr   r   r   r   r   r   r   r      s   r   c               @   s   e Zd ZdZdZdS )Flagz#Base class for all kinds of flags. TN)r   r   r   r   r   r   r   r   r   r$   ,   s   r$   c               @   s   e Zd ZdZedd ZdS )BooleanOptionzAAn option that must have a boolean value or equivalent assigned. c             C   s&   |dkrt |S td| j|f d S )N)TFz/'%s' must have a boolean value assigned, got %s)boolr
   r   )r   valuer   r   r   r   5   s    zBooleanOption.preprocessN)r   r   r   r   r#   r   r   r   r   r   r%   2   s   r%   c               @   s   e Zd ZdZdd ZdS )
OptionTypez7Base type for all options that does registers options. c                s.   t  fdd}tt j|  tj j< d S )Nc                s(   y
|  j  S  tk
r"     S X d S )N)r   KeyErrorr   )self)r   r   r   getterA   s    
z#OptionType.__init__.<locals>.getter)propertysetattrr   r   __options__)r   argskwargsr+   r   )r   r   __init__@   s    zOptionType.__init__N)r   r   r   r   r1   r   r   r   r   r(   =   s   r(   c                   sn   e Zd ZdZdZi ZdddZedd Zi fdd	Z	 fd
dZ
edd Zedd Zedd Z  ZS )r   aB  
    Options manager for polynomial manipulation module.

    Examples
    ========

    >>> from sympy.polys.polyoptions import Options
    >>> from sympy.polys.polyoptions import build_options

    >>> from sympy.abc import x, y, z

    >>> Options((x, y, z), {'domain': 'ZZ'})
    {'auto': False, 'domain': ZZ, 'gens': (x, y, z)}

    >>> build_options((x, y, z), {'domain': 'ZZ'})
    {'auto': False, 'domain': ZZ, 'gens': (x, y, z)}

    **Options**

    * Expand --- boolean option
    * Gens --- option
    * Wrt --- option
    * Sort --- option
    * Order --- option
    * Field --- boolean option
    * Greedy --- boolean option
    * Domain --- option
    * Split --- boolean option
    * Gaussian --- boolean option
    * Extension --- option
    * Modulus --- option
    * Symmetric --- boolean option
    * Strict --- boolean option

    **Flags**

    * Auto --- boolean flag
    * Frac --- boolean flag
    * Formal --- boolean flag
    * Polys --- boolean flag
    * Include --- boolean flag
    * All --- boolean flag
    * Gen --- flag
    * Series --- boolean flag

    NFc                s^  t  |r$|ddr$tdn|r8t |}||d< |di } fdd}|| xVt | D ]F\}}|kr||= qjx, D ] }	j|	 }
||
jkr||= P qW qjW || xz D ]n}	j|	 }
x,|
j	D ]"}|d krtd|	|f qW x0|
jD ]&}|d k	r
td|	|f q
W qW x j
D ]}	j|	  q@W d S )	Ngensr   z1both '*gens' and keyword argument 'gens' supplieddefaultsc          	      s   x|   D ]x\}}yj| }W n  tk
r@   td| Y nX t|trl d ks\| krlrltd| |d k	r
|||< q
W d S )Nz'%s' is not a valid optionz('%s' flag is not allowed in this context)itemsr.   r)   r
   
issubclassr$   r   )r/   r   r'   r   )flagsr*   strictr   r   preprocess_options   s    
z,Options.__init__.<locals>.preprocess_optionsz.'%s' option is only allowed together with '%s'z-'%s' option is not allowed together with '%s')dictr1   getr
   popr4   keysr.   r    r   	__order__r   )r*   r2   r/   r6   r7   r3   r8   keyr'   r   r   Zrequire_optionZexclude_optionr   )r6   r*   r7   r   r1      s:    




zOptions.__init__c             C   s   | j dkrg t  }}x\| j D ]N\}}|| x|jD ]}|||f q<W x|jD ]}|||f qZW q"W yt|t	|f| _ W n t
k
r   tdY nX dS )z*Resolve the order of options' processing. Nz/cycle detected in sympy.polys options framework)r=   setr.   r4   appendr!   addr"   r   list
ValueErrorRuntimeError)r   Zverticesedgesnamer   _namer   r   r   _init_dependencies_order   s    

z Options._init_dependencies_orderc             C   sL   t | j}x|  D ]\}}|||< qW x| D ]\}}|||< q4W |S )z-Clone ``self`` and update specified options. )r9   __new__	__class__r4   )r*   Zupdatesobjr   r'   r   r   r   clone   s    zOptions.clonec                s&   || j kr|| |< nt || d S )N)r.   super__setattr__)r*   attrr'   )rJ   r   r   rN      s    

zOptions.__setattr__c             C   sJ   i }x@|   D ]4\}}|d k	r|dkr| j| }t|ts|||< qW |S )Nr2   )r4   r.   r5   r$   )r*   r/   r   r'   r   r   r   r   r/      s    

zOptions.argsc             C   s8   i }x.| j  D ] \}}t|tst| |||< qW |S )N)r.   r4   r5   r$   getattr)r*   r   r   r   r   r   r   r      s
    
zOptions.optionsc             C   s8   i }x.| j  D ] \}}t|trt| |||< qW |S )N)r.   r4   r5   r$   rP   )r*   r6   r   r   r   r   r   r6      s
    
zOptions.flags)NF)r   r   r   r   r=   r.   r1   r#   rH   rL   rN   r,   r/   r   r6   __classcell__r   r   )rJ   r   r   L   s   /
8
c               @   s(   e Zd ZdZdZg Zg Zedd ZdS )Expandz8``expand`` option to polynomial manipulation functions. expandc             C   s   dS )NTr   )r   r   r   r   r   	  s    zExpand.defaultN)	r   r   r   r   r   r   r    r#   r   r   r   r   r   rR     s
   rR   )	metaclassc               @   s4   e Zd ZdZdZg Zg Zedd Zedd Z	dS )Gensz6``gens`` option to polynomial manipulation functions. r2   c             C   s   dS )Nr   r   )r   r   r   r   r     s    zGens.defaultc             C   s   t |tr|f}n t|dkr2t|d r2|d }|dkr@d}n<t|rZtdt| n"tdd |D r|tdt| t|S )	N   r   )Nr   zduplicated generators: %sc             s   s   | ]}|j d kV  qdS )FN)Zis_commutative).0genr   r   r   	<genexpr>%  s    z"Gens.preprocess.<locals>.<genexpr>znon-commutative generators: %s)	
isinstancer   lenr   r   r	   stranytuple)r   r2   r   r   r   r     s    
zGens.preprocessN)
r   r   r   r   r   r   r    r#   r   r   r   r   r   r   rU     s   rU   c               @   s2   e Zd ZdZdZg Zg ZedZ	e
dd ZdS )Wrtz5``wrt`` option to polynomial manipulation functions. wrtz\s*,\s*|\s+c             C   sz   t |trt|gS t |trV| }|dr8td|s@g S dd | j|D S t|drnt	t
t|S tdd S )N,zBad input: missing parameter.c             S   s   g | ]}|qS r   r   )rW   rX   r   r   r   
<listcomp>?  s    z"Wrt.preprocess.<locals>.<listcomp>__getitem__z!invalid argument for 'wrt' option)rZ   r   r\   stripendswithr
   	_re_splitsplithasattrrB   map)r   r`   r   r   r   r   5  s    




zWrt.preprocessN)r   r   r   r   r   r   r    recompilerf   r#   r   r   r   r   r   r_   +  s   
r_   c               @   s4   e Zd ZdZdZg Zg Zedd Zedd Z	dS )Sortz6``sort`` option to polynomial manipulation functions. sortc             C   s   g S )Nr   )r   r   r   r   r   N  s    zSort.defaultc             C   sB   t |trdd |dD S t|dr6ttt|S tdd S )Nc             S   s   g | ]}|  qS r   )rd   )rW   rX   r   r   r   rb   U  s    z#Sort.preprocess.<locals>.<listcomp>>rc   z"invalid argument for 'sort' option)rZ   r\   rg   rh   rB   ri   r
   )r   rm   r   r   r   r   R  s
    

zSort.preprocessN)
r   r   r   r   r   r   r    r#   r   r   r   r   r   r   rl   F  s   rl   c               @   s4   e Zd ZdZdZg Zg Zedd Zedd Z	dS )Orderz7``order`` option to polynomial manipulation functions. orderc             C   s
   t jjjS )N)sympypolys	orderingslex)r   r   r   r   r   d  s    zOrder.defaultc             C   s   t jj|S )N)rq   rr   rs   Zmonomial_key)r   rp   r   r   r   r   h  s    zOrder.preprocessN)
r   r   r   r   r   r   r    r#   r   r   r   r   r   r   ro   \  s   ro   c               @   s"   e Zd ZdZdZg ZdddgZdS )Fieldz7``field`` option to polynomial manipulation functions. fielddomainrg   gaussianN)r   r   r   r   r   r   r    r   r   r   r   ru   m  s   ru   c               @   s(   e Zd ZdZdZg ZddddddgZd	S )
Greedyz8``greedy`` option to polynomial manipulation functions. greedyrw   rg   rx   	extensionmodulus	symmetricN)r   r   r   r   r   r   r    r   r   r   r   ry   v  s   ry   c               @   s4   e Zd ZdZdZedd Zg Zddddd	d
gZdS )	Compositez;``composite`` option to polynomial manipulation functions. Z	compositec             C   s   d S )Nr   )r   r   r   r   r     s    zComposite.defaultrw   rg   rx   r{   r|   r}   N)	r   r   r   r   r   r#   r   r   r    r   r   r   r   r~     s
   r~   c               @   s   e Zd ZdZdZg ZdddddgZdgZe	d	Z
e	d
Ze	dZe	dZe	dZe	dZedd Zedd ZdS )Domainz8``domain`` option to polynomial manipulation functions. rw   rv   rz   rg   rx   r{   r2   z^(R|RR)(_(\d+))?$z^(C|CC)(_(\d+))?$z^(FF|GF)\((\d+)\)$z)^(Z|ZZ|Q|QQ|ZZ_I|QQ_I|R|RR|C|CC)\[(.+)\]$z^(Z|ZZ|Q|QQ)\((.+)\)$z^(Q|QQ)\<(.+)\>$c             C   s  t |tjjjr|S t|dr&| S t |tr|dkrDtjjjS |dkrVtjjj	S |dkrhtjjj
S |dkrztjjjS |dkrtjjjS | j|}|d k	r| \}}}|d krtjjjS tjjt|S | j|}|d k	r| \}}}|d kr
tjjjS tjjt|S | j|}|d k	rLtjjt| d S | j|}|d k	r| \}}ttt|d}|dkrtjjjj| S |dkrtjjj	j| S |d	krtjjjj| S |dkrtjjj
j| S |dkrtjjjj| S tjjjj| S | j|}|d k	rt| \}}ttt|d}|dkrdtjjjj| S tjjj	j| S | j |}|d k	rttt| d d}tjjj	j!| S t"d
| d S )N	to_domain)ZZZ)QQQZZ_IQQ_IEXrV   ra   )RRRz-expected a valid domain specification, got %s)#rZ   rq   rr   domainsr   rh   r   r\   r   r   r   r   r   _re_realfieldmatchgroupsr   Z	RealFieldint_re_complexfieldCCZComplexField_re_finitefieldFF_re_polynomialrB   ri   r   rg   Z	poly_ring_re_fractionZ
frac_field_re_algebraicalgebraic_fieldr
   )r   rw   r_precZgroundr2   r   r   r   r     sr    



















zDomain.preprocessc             C   st   d|kr>d|kr>|d j r>t|d jt|d @ r>tdn2d|ksN|d spd|krp|d tjjjkrptdd S )Nr2   rw   z/ground domain and generators interfere togetherz>you have to provide generators because EX domain was requested)Zis_Compositer?   symbolsr	   rq   rr   r   r   )r   r   r   r   r   r     s    zDomain.postprocessN)r   r   r   r   r   r   r    r!   rj   rk   r   r   r   r   r   r   r#   r   r   r   r   r   r   r     s   





Wr   c               @   s6   e Zd ZdZdZg Zddddddd	gZed
d ZdS )Splitz7``split`` option to polynomial manipulation functions. rg   rv   rz   rw   rx   r{   r|   r}   c             C   s   d|krt dd S )Nrg   z%'split' option is not implemented yet)NotImplementedError)r   r   r   r   r   r     s    zSplit.postprocessN)	r   r   r   r   r   r   r    r#   r   r   r   r   r   r     s   
r   c               @   s6   e Zd ZdZdZg Zddddddd	gZed
d ZdS )Gaussianz:``gaussian`` option to polynomial manipulation functions. rx   rv   rz   rw   rg   r{   r|   r}   c             C   s0   d|kr,|d dkr,t jjj|d< t| d S )Nrx   Trw   )rq   rr   r   r   	Extensionr   )r   r   r   r   r   r     s    zGaussian.postprocessN)	r   r   r   r   r   r   r    r#   r   r   r   r   r   r     s   
r   c               @   s@   e Zd ZdZdZg ZddddddgZed	d
 Zedd Z	dS )r   z;``extension`` option to polynomial manipulation functions. r{   rz   rw   rg   rx   r|   r}   c             C   sN   |dkrt |S |dkr"tdn(t|ds4|h}n|s>d }nt|}|S d S )NrV   r   z.'False' is an invalid argument for 'extension'__iter__)r&   r
   rh   r?   )r   r{   r   r   r   r   '  s    

zExtension.preprocessc             C   s0   d|kr,|d dk	r,t jjjj|d  |d< d S )Nr{   Trw   )rq   rr   r   r   r   )r   r   r   r   r   r   8  s    
zExtension.postprocessN)
r   r   r   r   r   r   r    r#   r   r   r   r   r   r   r     s   
r   c               @   s>   e Zd ZdZdZg ZdddddgZedd	 Zed
d Z	dS )Modulusz9``modulus`` option to polynomial manipulation functions. r|   rz   rg   rw   rx   r{   c             C   s.   t |}|jr|dkrt|S td| d S )Nr   z)'modulus' must a positive integer, got %s)r   Z
is_Integerr   r
   )r   r|   r   r   r   r   G  s
    zModulus.preprocessc             C   s4   d|kr0|d }| dd}tjj|||d< d S )Nr|   r}   Trw   )r:   rq   rr   r   r   )r   r   r|   r}   r   r   r   r   Q  s    zModulus.postprocessN)
r   r   r   r   r   r   r    r#   r   r   r   r   r   r   r   ?  s   
r   c               @   s(   e Zd ZdZdZdgZdddddgZd	S )
	Symmetricz;``symmetric`` option to polynomial manipulation functions. r}   r|   rz   rw   rg   rx   r{   N)r   r   r   r   r   r   r    r   r   r   r   r   Y  s   r   c               @   s    e Zd ZdZdZedd ZdS )Strictz8``strict`` option to polynomial manipulation functions. r7   c             C   s   dS )NTr   )r   r   r   r   r   g  s    zStrict.defaultN)r   r   r   r   r   r#   r   r   r   r   r   r   b  s   r   c               @   s8   e Zd ZdZdZddddgZedd Zed	d
 ZdS )Autoz4``auto`` flag to polynomial manipulation functions. autorv   rw   r{   rx   c             C   s   dS )NTr   )r   r   r   r   r   s  s    zAuto.defaultc             C   s$   d|ksd|kr d|kr d|d< d S )Nrw   rv   r   Fr   )r   r   r   r   r   r   w  s    zAuto.postprocessN)	r   r   r   r   r   r!   r#   r   r   r   r   r   r   r   l  s
   r   c               @   s    e Zd ZdZdZedd ZdS )Fracz6``auto`` option to polynomial manipulation functions. fracc             C   s   dS )NFr   )r   r   r   r   r     s    zFrac.defaultN)r   r   r   r   r   r#   r   r   r   r   r   r   }  s   r   c               @   s    e Zd ZdZdZedd ZdS )Formalz6``formal`` flag to polynomial manipulation functions. Zformalc             C   s   dS )NFr   )r   r   r   r   r     s    zFormal.defaultN)r   r   r   r   r   r#   r   r   r   r   r   r     s   r   c               @   s   e Zd ZdZdZdS )Polysz5``polys`` flag to polynomial manipulation functions. rr   N)r   r   r   r   r   r   r   r   r   r     s   r   c               @   s    e Zd ZdZdZedd ZdS )Includez7``include`` flag to polynomial manipulation functions. includec             C   s   dS )NFr   )r   r   r   r   r     s    zInclude.defaultN)r   r   r   r   r   r#   r   r   r   r   r   r     s   r   c               @   s    e Zd ZdZdZedd ZdS )Allz3``all`` flag to polynomial manipulation functions. allc             C   s   dS )NFr   )r   r   r   r   r     s    zAll.defaultN)r   r   r   r   r   r#   r   r   r   r   r   r     s   r   c               @   s,   e Zd ZdZdZedd Zedd ZdS )Genz3``gen`` flag to polynomial manipulation functions. rX   c             C   s   dS )Nr   r   )r   r   r   r   r     s    zGen.defaultc             C   s   t |ttfr|S tdd S )Nz!invalid argument for 'gen' option)rZ   r   r   r
   )r   rX   r   r   r   r     s    zGen.preprocessN)r   r   r   r   r   r#   r   r   r   r   r   r   r     s   r   c               @   s    e Zd ZdZdZedd ZdS )Seriesz6``series`` flag to polynomial manipulation functions. Zseriesc             C   s   dS )NFr   )r   r   r   r   r     s    zSeries.defaultN)r   r   r   r   r   r#   r   r   r   r   r   r     s   r   c               @   s,   e Zd ZdZdZedd Zedd ZdS )Symbolsz7``symbols`` flag to polynomial manipulation functions. r   c             C   s   t dddS )NsrV   )start)r   )r   r   r   r   r     s    zSymbols.defaultc             C   s"   t |drt|S td| d S )Nr   z2expected an iterator or iterable container, got %s)rh   iterr
   )r   r   r   r   r   r     s    
zSymbols.preprocessN)r   r   r   r   r   r#   r   r   r   r   r   r   r     s   r   c               @   s    e Zd ZdZdZedd ZdS )Methodz6``method`` flag to polynomial manipulation functions. methodc             C   s"   t |tr| S td| d S )Nzexpected a string, got %s)rZ   r\   lowerr
   )r   r   r   r   r   r     s    
zMethod.preprocessN)r   r   r   r   r   r#   r   r   r   r   r   r     s   r   c             C   s@   |dkrd|  } }t |dks*d|ks*| r4t| |S |d S dS )z9Construct options from keyword arguments or ... options. Nr   rV   opt)r[   r   )r2   r/   r   r   r   build_options  s
    

r   c          	   C   sd   t |}xV|  D ]J}y$tj| jr8||kr8td| W q tk
rZ   td| Y qX qW dS )a  
    Allow specified flags to be used in the given context.

    Examples
    ========

    >>> from sympy.polys.polyoptions import allowed_flags
    >>> from sympy.polys.domains import ZZ

    >>> allowed_flags({'domain': ZZ}, [])

    >>> allowed_flags({'domain': ZZ, 'frac': True}, [])
    Traceback (most recent call last):
    ...
    FlagError: 'frac' flag is not allowed in this context

    >>> allowed_flags({'domain': ZZ, 'frac': True}, ['frac'])

    z('%s' flag is not allowed in this contextz'%s' is not a valid optionN)r?   r<   r   r.   r   r   r)   r
   )r/   r6   argr   r   r   allowed_flags  s    r   c             K   s   d| krt | } || d< | S )z$Update options with default values. r3   )r9   )r   r3   r   r   r   set_defaults  s    r   )N)=r   __all__typingr   ZtDictr   r   r   Z
sympy.corer   r   Zsympy.polys.polyerrorsr	   r
   r   Zsympy.utilitiesr   r   r   Zsympy.utilities.iterablesr   r   Zsympy.polysrq   rj   r   r$   r%   typer(   r9   r   rR   rU   r_   rl   ro   ru   ry   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rH   r   r   r   r   <module>   sZ    5		s!	






