o
    8VaU                     @   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 dd	l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/d0 d0eedZ-G d1d2 d2eedZ.G d3d4 d4eeedZ/G d5d6 d6eeedZ0G d7d8 d8eeedZ1G d9d: d:e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 dAdB dBeeedZ6G dCdD dDeedZ7G dEdF dFeedZ8dMdGdHZ9dIdJ Z:dKdL Z;e<  d
S )Nz>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   9/usr/lib/python3/dist-packages/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   6   s   zBooleanOption.preprocessN)r   r    r!   r"   r(   r   r   r   r   r   r,   3   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   getterB   s
   z#OptionType.__init__.<locals>.getter)propertysetattrr   r   __options__)r   argskwargsr2   r   r   r   __init__A   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   Zrequire_optionZ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   ZverticesZ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   Z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.   rS   r   r   rW      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   rZ   )r1   r@   r   r   r   r   r   r@      r\   zOptions.flagsNF)r   r    r!   r"   rG   r5   r8   r(   rQ   rU   rW   r3   r6   r   r@   __classcell__r   r   rY   r   r   M   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   r_     s    r_   )	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   ra   )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)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   r9   r   r   r   r     s   
zGens.preprocessN
r   r    r!   r"   r   r$   r%   r(   r   r   r   r   r   r   re         
re   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   rg   r   r   r   
<listcomp>@  s    z"Wrt.preprocess.<locals>.<listcomp>__getitem__z!invalid argument for 'wrt' option)rk   r   rm   stripendswithr
   	_re_splitsplithasattrrL   map)r   rs   r   r   r   r   6  s   




zWrt.preprocessN)r   r    r!   r"   r   r$   r%   recompilery   r(   r   r   r   r   r   rr   ,  s    
rr   c                   @   rd   )Sortz6``sort`` option to polynomial manipulation functions. sortc                 C   s   g S r   r   r   r   r   r   r   O  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   )rw   rg   r   r   r   ru   V  s    z#Sort.preprocess.<locals>.<listcomp>>rv   z"invalid argument for 'sort' option)rk   rm   rz   r{   rL   r|   r
   )r   r   r   r   r   r   S  s
   

zSort.preprocessNrp   r   r   r   r   r   G  rq   r   c                   @   rd   )Orderz7``order`` option to polynomial manipulation functions. orderc                 C   s
   t jjjS r   )sympypolys	orderingsZlexr   r   r   r   r   e  s   
zOrder.defaultc                 C   s   t jj|S r   )r   r   r   Zmonomial_key)r   r   r   r   r   r   i  s   zOrder.preprocessNrp   r   r   r   r   r   ]  rq   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)domainrz   gaussianNr   r    r!   r"   r   r$   r%   r   r   r   r   r   n  
    r   c                   @   r   )Greedyz8``greedy`` option to polynomial manipulation functions. greedyr   rz   r   	extensionmodulus	symmetricNr   r   r   r   r   r   w  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. Z	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   rz   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EXrf   rt   )RRRz-expected a valid domain specification, got %s)#rk   r   r   domainsr   r{   r   rm   r   r   r   r   r   _re_realfieldmatchgroupsr   Z	RealFieldint_re_complexfieldZCCZComplexField_re_finitefieldFF_re_polynomialrL   r|   r   rz   Z	poly_ring_re_fractionZ
frac_field_re_algebraicalgebraic_fieldr
   )r   r   r_ZprecZ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)Zis_CompositerI   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. rz   )r   r   r   r   r   r   r   c                 C   s   d|v rt dd S )Nrz   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   rz   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   rz   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 )Nrf   r   z.'False' is an invalid argument for 'extension'__iter__)r-   r
   r{   rI   )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   9  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   rz   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   Z
is_Integerr   r
   )r   r   r   r   r   r   H  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   R  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   rz   r   r   Nr   r   r   r   r   r   Z  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   ra   rb   r   r   r   r   r   r   h  r   zStrict.defaultNr   r    r!   r"   r   r(   r   r   r   r   r   r   c  
    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   ra   rb   r   r   r   r   r   r   t  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   x  s   zAuto.postprocessN)	r   r    r!   r"   r   r&   r(   r   r   r   r   r   r   r   m  s    
r   c                   @   r   )Fracz6``auto`` option to polynomial manipulation functions. Zfracc                 C   ra   r]   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. Zformalc                 C   ra   r]   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. Zincludec                 C   ra   r]   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   ra   r]   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. ri   c                 C   ra   )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)rk   r   r   r
   )r   ri   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. Zseriesc                 C   ra   r]   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srf   )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)rk   rm   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   rf   Zopt)rl   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)rI   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   r   r   r   Z
sympy.corer   r   Zsympy.polys.polyerrorsr	   r
   r   Zsympy.utilitiesr   r   r   Zsympy.utilities.iterablesr   Zsympy.core.compatibilityr   Zsympy.polysr   r}   r   r*   r,   typer/   rC   r   r_   re   rr   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   rQ   r   r   r   r   <module>   s^     5		s!	






