o
    *ήcU                     @   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      d S N clsr   r   >/tmp/pip-target-vg8gfxp4/lib/python/sympy/polys/polyoptions.pydefault      zOption.defaultc                 C   r   r   r   )r   optionr   r   r   
preprocess#   r   zOption.preprocessc                 C   r   r   r   r   optionsr   r   r   postprocess'   r   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                   @      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*   ,       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v rt |S td| j|f )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(   z|  j  W S  ty      Y S w r   )r   KeyErrorr   )selfr   r   r   getterA   s
   z#OptionType.__init__.<locals>.getter)propertysetattrr   r   __options__)r   argskwargsr2   r   r   r   __init__@   s   zOptionType.__init__N)r   r    r!   r"   r8   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|rt |}||d< |di } fdd}|| t | D ]"\}}|v r?||= q3 D ]}	j|	 }
||
jv rT||=  nqCq3||  D ]1}	j|	 }
|
j	D ]}|d u rytd|	|f qh|
jD ]}|d urtd|	|f q}q^j
D ]
}	j|	  qd S )	Ngensr   z1both '*gens' and keyword argument 'gens' supplieddefaultsc              	      s   |   D ]9\}}zj| }W n ty   td| w t|tr2 d u s*| vr2r2td| |d ur=|||< qd S )N'%s' is not a valid option('%s' flag is not allowed in this context)itemsr5   r0   r
   
issubclassr*   r   )r6   r   r.   r   flagsr1   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')dictr8   getr
   popr=   keysr5   r%   r$   	__order__r   )r1   r9   r6   r@   rA   r:   rB   keyr.   r   r   require_optionexclude_optionr   r?   r   r8      sH   






zOptions.__init__c                 C   s   | j du rLg t }}| j D ]#\}}|| |jD ]	}|||f q|jD ]	}|||f q)qzt|t	|f| _ W dS  t
yK   tdw dS )z*Resolve the order of options' processing. Nz/cycle detected in sympy.polys options framework)rG   setr5   r=   appendr&   addr'   r   list
ValueErrorRuntimeError)r   verticesedgesnamer   _namer   r   r   _init_dependencies_order   s"   



z Options._init_dependencies_orderc                 C   sD   t | j}|  D ]\}}|||< q
| D ]\}}|||< q|S )z-Clone ``self`` and update specified options. )rC   __new__	__class__r=   )r1   updatesobjr   r.   r   r   r   clone   s   

zOptions.clonec                    s(   || j v r|| |< d S t || d S r   )r5   super__setattr__)r1   attrr.   rW   r   r   r\      s   
zOptions.__setattr__c                 C   sF   i }|   D ]\}}|d ur |dkr | j| }t|ts |||< q|S )Nr9   )r=   r5   r>   r*   )r1   r6   r   r.   r   r   r   r   r6      s   

zOptions.argsc                 C   s4   i }| j  D ]\}}t|tst| |||< q|S r   r5   r=   r>   r*   getattr)r1   r   r   r   r   r   r   r         
zOptions.optionsc                 C   s4   i }| j  D ]\}}t|trt| |||< q|S r   r_   )r1   r@   r   r   r   r   r   r@      ra   zOptions.flagsNF)r   r    r!   r"   rG   r5   r8   r(   rU   rZ   r\   r3   r6   r   r@   __classcell__r   r   r^   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      dS NTr   r   r   r   r   r   	  r   zExpand.defaultN)	r   r    r!   r"   r   r$   r%   r(   r   r   r   r   r   rd     s    rd   )	metaclassc                   @   4   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. r9   c                 C   rf   )Nr   r   r   r   r   r   r     r   zGens.defaultc                 C   s   t |tr	|f}nt|dkrt|d r|d }|dkr#d}t|S t|r/tdt| tdd |D r@tdt| t|S )	N   r   r   r   zduplicated generators: %sc                 s   s    | ]}|j d u V  qdS )FN)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   r9   r   r   r   r     s   
zGens.preprocessN
r   r    r!   r"   r   r$   r%   r(   r   r   r   r   r   r   rj         
rj   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   sv   t |tr
t|gS t |tr+| }|drtd|s g S dd | j|D S t|dr7t	t
t|S td)N,zBad input: missing parameter.c                 S   s   g | ]}|qS r   r   rm   r   r   r   
<listcomp>?  s    z"Wrt.preprocess.<locals>.<listcomp>__getitem__z!invalid argument for 'wrt' option)rq   r   rs   stripendswithr
   	_re_splitsplithasattrrN   map)r   ry   r   r   r   r   5  s   




zWrt.preprocessN)r   r    r!   r"   r   r$   r%   recompiler   r(   r   r   r   r   r   rx   +  s    
rx   c                   @   ri   )Sortz6``sort`` option to polynomial manipulation functions. sortc                 C   s   g S r   r   r   r   r   r   r   N  r   zSort.defaultc                 C   s>   t |trdd |dD S t|drttt|S td)Nc                 S   s   g | ]}|  qS r   )r}   rm   r   r   r   r{   U  s    z#Sort.preprocess.<locals>.<listcomp>>r|   z"invalid argument for 'sort' option)rq   rs   r   r   rN   r   r
   )r   r   r   r   r   r   R  s
   

zSort.preprocessNrv   r   r   r   r   r   F  rw   r   c                   @   ri   )Orderz7``order`` option to polynomial manipulation functions. orderc                 C   s
   t jjjS r   )sympypolys	orderingslexr   r   r   r   r   d  s   
zOrder.defaultc                 C   s   t jj|S r   )r   r   r   monomial_key)r   r   r   r   r   r   h  s   zOrder.preprocessNrv   r   r   r   r   r   \  rw   r   c                   @       e Zd ZdZdZg Zg dZdS )Fieldz7``field`` option to polynomial manipulation functions. field)domainr   gaussianNr   r    r!   r"   r   r$   r%   r   r   r   r   r   m  
    r   c                   @   r   )Greedyz8``greedy`` option to polynomial manipulation functions. greedyr   r   r   	extensionmodulus	symmetricNr   r   r   r   r   r   v  r   r   c                   @   s,   e Zd ZdZdZedd Zg Zg dZdS )	Compositez;``composite`` option to polynomial manipulation functions. 	compositec                 C   r   r   r   r   r   r   r   r     r   zComposite.defaultr   N)	r   r    r!   r"   r   r(   r   r$   r%   r   r   r   r   r     s    
r   c                   @   sz   e Zd ZdZdZg Zg d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. r   )r   r   r   r   r   r9   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rS|dv r"tjjjS |dv r+tjjj	S |dkr4tjjj
S |dkr=tjjjS |dkrFtjjjS | j|}|d uri| \}}}|d u r`tjjjS tjjt|S | j|}|d ur| \}}}|d u rtjjjS tjjt|S | j|}|d urtjjt| d S | j|}|d ur| \}}ttt|d}|dv rtjjjj| S |dv rtjjj	j| S |d	v rtjjjj| S |dkrtjjj
j| S |dkrtjjjj| S tjjjj| S | j|}|d ur2| \}}ttt|d}|dv r*tjjjj| S tjjj	j| S | j |}|d urSttt| d d}tjjj	j!| S t"d
| )N	to_domain)ZZZ)QQQZZ_IQQ_IEXrk   rz   )RRRz-expected a valid domain specification, got %s)#rq   r   r   domainsr   r   r   rs   r   r   r   r   r   _re_realfieldmatchgroupsr   	RealFieldint_re_complexfieldCCComplexField_re_finitefieldFF_re_polynomialrN   r   r   r   	poly_ring_re_fraction
frac_field_re_algebraicalgebraic_fieldr
   )r   r   r_precgroundr9   r   r   r   r     sr   











zDomain.preprocessc                 C   sz   d|v rd|v r|d j rt|d jt|d @ rtdd|vs&|d s9d|v r7|d tjjjkr;tdd S d S d S )Nr9   r   z/ground domain and generators interfere togetherz>you have to provide generators because EX domain was requested)is_CompositerK   symbolsr	   r   r   r   r   r   r   r   r   r     s   zDomain.postprocessN)r   r    r!   r"   r   r$   r%   r&   r   r   r   r   r   r   r   r   r(   r   r   r   r   r   r   r     s     






Vr   c                   @   ,   e Zd ZdZdZg Zg dZedd ZdS )Splitz7``split`` option to polynomial manipulation functions. r   )r   r   r   r   r   r   r   c                 C   s   d|v rt dd S )Nr   z%'split' option is not implemented yet)NotImplementedErrorr   r   r   r   r     s   zSplit.postprocessN	r   r    r!   r"   r   r$   r%   r(   r   r   r   r   r   r         r   c                   @   r   )Gaussianz:``gaussian`` option to polynomial manipulation functions. r   )r   r   r   r   r   r   r   c                 C   s8   d|v r|d du rt jjj|d< t| d S d S d S )Nr   Tr   )r   r   r   r   	Extensionr   r   r   r   r   r     s   zGaussian.postprocessNr   r   r   r   r   r     r   r   c                   @   8   e Zd ZdZdZg Zg dZedd Zedd Z	dS )	r   z;``extension`` option to polynomial manipulation functions. r   )r   r   r   r   r   r   c                 C   sL   |dkrt |S |dkrtdt|ds|h}|S |s d }|S t|}|S )Nrk   r   z.'False' is an invalid argument for 'extension'__iter__)r-   r
   r   rK   )r   r   r   r   r   r   '  s   
zExtension.preprocessc                 C   s8   d|v r|d durt jjjj|d  |d< d S d S d S )Nr   Tr   )r   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                   @   r   )	Modulusz9``modulus`` option to polynomial manipulation functions. r   )r   r   r   r   r   c                 C   s*   t |}|jr|dkrt|S td| )Nr   z)'modulus' must a positive integer, got %s)r   
is_Integerr   r
   )r   r   r   r   r   r   G  s   zModulus.preprocessc                 C   s8   d|v r|d }| dd}tjj|||d< d S d S )Nr   r   Tr   )rD   r   r   r   r   )r   r   r   r   r   r   r   r   Q  s
   zModulus.postprocessNr   r   r   r   r   r   ?  s    
	r   c                   @   s"   e Zd ZdZdZdgZg dZdS )	Symmetricz;``symmetric`` option to polynomial manipulation functions. r   r   )r   r   r   r   r   Nr   r   r   r   r   r   Y  s
    r   c                   @       e Zd ZdZdZedd ZdS )Strictz8``strict`` option to polynomial manipulation functions. rA   c                 C   rf   rg   r   r   r   r   r   r   g  r   zStrict.defaultNr   r    r!   r"   r   r(   r   r   r   r   r   r   b  
    r   c                   @   s4   e Zd ZdZdZg dZedd Zedd ZdS )	Autoz4``auto`` flag to polynomial manipulation functions. auto)r   r   r   r   c                 C   rf   rg   r   r   r   r   r   r   s  r   zAuto.defaultc                 C   s,   d|v sd|v rd|vrd|d< d S d S d S )Nr   r   r   Fr   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                   @   r   )Fracz6``auto`` option to polynomial manipulation functions. fracc                 C   rf   rb   r   r   r   r   r   r     r   zFrac.defaultNr   r   r   r   r   r   }  r   r   c                   @   r   )Formalz6``formal`` flag to polynomial manipulation functions. formalc                 C   rf   rb   r   r   r   r   r   r     r   zFormal.defaultNr   r   r   r   r   r     r   r   c                   @   r)   )Polysz5``polys`` flag to polynomial manipulation functions. r   N)r   r    r!   r"   r   r   r   r   r   r     r+   r   c                   @   r   )Includez7``include`` flag to polynomial manipulation functions. includec                 C   rf   rb   r   r   r   r   r   r     r   zInclude.defaultNr   r   r   r   r   r     r   r   c                   @   r   )Allz3``all`` flag to polynomial manipulation functions. allc                 C   rf   rb   r   r   r   r   r   r     r   zAll.defaultNr   r   r   r   r   r     r   r   c                   @   ,   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. ro   c                 C   rf   )Nr   r   r   r   r   r   r     r   zGen.defaultc                 C   s   t |ttfr	|S td)Nz!invalid argument for 'gen' option)rq   r   r   r
   )r   ro   r   r   r   r     s   zGen.preprocessNr   r    r!   r"   r   r(   r   r   r   r   r   r   r         
r   c                   @   r   )Seriesz6``series`` flag to polynomial manipulation functions. seriesc                 C   rf   rb   r   r   r   r   r   r     r   zSeries.defaultNr   r   r   r   r   r     r   r   c                   @   r   )Symbolsz7``symbols`` flag to polynomial manipulation functions. r   c                 C   s   t dddS )Nsrk   )start)r   r   r   r   r   r     s   zSymbols.defaultc                 C   s   t |dr	t|S td| )Nr   z2expected an iterator or iterable container, got %s)r   iterr
   )r   r   r   r   r   r        
zSymbols.preprocessNr   r   r   r   r   r     r   r   c                   @   r   )Methodz6``method`` flag to polynomial manipulation functions. methodc                 C   s   t |tr	| S td| )Nzexpected a string, got %s)rq   rs   lowerr
   )r   r   r   r   r   r     r   zMethod.preprocessN)r   r    r!   r"   r   r(   r   r   r   r   r   r     r   r   c                 C   s<   |du r	d| } }t |dksd|vs| rt| |S |d S )z9Construct options from keyword arguments or ... options. Nr   rk   opt)rr   r   )r9   r6   r   r   r   build_options  s
   

r   c              	   C   sX   t |}|  D ]!}ztj| jr||vrtd| W q ty)   td| 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'])

    r<   r;   N)rK   rF   r   r5   r#   r   r0   r
   )r6   r@   argr   r   r   allowed_flags  s   r   c                 K   s   d| vrt | } || d< | S )z$Update options with default values. r:   )rC   )r   r:   r   r   r   set_defaults  s   r   r   )=r"   __all__typingr   tDictr   r   r   
sympy.corer   r   sympy.polys.polyerrorsr	   r
   r   sympy.utilitiesr   r   r   sympy.utilities.iterablesr   r   sympy.polysr   r   r   r*   r,   typer/   rC   r   rd   rj   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rU   r   r   r   r   <module>   s\     5		s!	






