U
    ZÊ¹jU  ã                   @   sh   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 G dd„ deƒZ
d	d
„ Zdd„ Zdd„ ZdS )z¦
The Schur number S(k) is the largest integer n for which the interval [1,n]
can be partitioned into k sum-free sets.(https://mathworld.wolfram.com/SchurNumber.html)
é    N)ÚS)ÚBasic)ÚFunction)ÚIntegerc                   @   s$   e Zd ZdZedd„ ƒZdd„ ZdS )ÚSchurNumbera\  
    This function creates a SchurNumber object
    which is evaluated for `k \le 5` otherwise only
    the lower bound information can be retrieved.

    Examples
    ========

    >>> from sympy.combinatorics.schur_number import SchurNumber

    Since S(3) = 13, hence the output is a number
    >>> SchurNumber(3)
    13

    We do not know the Schur number for values greater than 5, hence
    only the object is returned
    >>> SchurNumber(6)
    SchurNumber(6)

    Now, the lower bound information can be retrieved using lower_bound()
    method
    >>> SchurNumber(6).lower_bound()
    536

    c                 C   s^   |j rZ|tjkrtjS |jr"tjS |jr.|jr6tdƒ‚ddddddœ}|dkrZt|| ƒS d S )	Nzk should be a positive integeré   é   é   é,   é    )r   é   é   r   é   r   )	Ú	is_Numberr   ÚInfinityÚis_zeroZZeroÚ
is_integerZis_negativeÚ
ValueErrorr   )ÚclsÚkZfirst_known_schur_numbers© r   ú_/var/www/html/bwave/python/venv/lib/python3.8/site-packages/sympy/combinatorics/schur_number.pyÚeval'   s    
zSchurNumber.evalc                 C   sZ   | j d }|dkrtdƒS |dkr*tdƒS |jrJd|  |d ¡ ¡  d S d| d d S )	Nr   é   i  é   i�  r   r   r   )Úargsr   Z
is_IntegerÚfuncÚlower_bound)ÚselfZf_r   r   r   r   4   s    
zSchurNumber.lower_boundN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr   r   r   r   r   r   r      s   
r   c                 C   sT   | t jkrtdƒ‚| dkr$tdƒ‚n(| dkr2d}nt t d|  d d¡¡}t|ƒS )NzInput must be finiter   z&n must be a non-zero positive integer.r   r   r   )r   r   r   ÚmathÚceilÚlogr   )ÚnZmin_kr   r   r   Ú_schur_subsets_numberA   s    

r(   c                 C   s¼   t | tƒr| jstdƒ‚t| ƒ}| dkr2dgg}n:| dkrFddgg}n&| dkr\dddgg}nddgddgg}t|ƒ|k r¸t|| ƒ}dd„ tt|ƒ| d d d ƒD ƒ}|d  |7  < ql|S )	a�  

    This function returns the partition in the minimum number of sum-free subsets
    according to the lower bound given by the Schur Number.

    Parameters
    ==========

    n: a number
        n is the upper limit of the range [1, n] for which we need to find and
        return the minimum number of free subsets according to the lower bound
        of schur number

    Returns
    =======

    List of lists
        List of the minimum number of sum-free subsets

    Notes
    =====

    It is possible for some n to make the partition into less
    subsets since the only known Schur numbers are:
    S(1) = 1, S(2) = 4, S(3) = 13, S(4) = 44.
    e.g for n = 44 the lower bound from the function above is 5 subsets but it has been proven
    that can be done with 4 subsets.

    Examples
    ========

    For n = 1, 2, 3 the answer is the set itself

    >>> from sympy.combinatorics.schur_number import schur_partition
    >>> schur_partition(2)
    [[1, 2]]

    For n > 3, the answer is the minimum number of sum-free subsets:

    >>> schur_partition(5)
    [[3, 2], [5], [1, 4]]

    >>> schur_partition(8)
    [[3, 2], [6, 5, 8], [1, 4, 7]]
    zInput value must be a numberr   r   r   r   c                 S   s   g | ]}d | d ‘qS ©r   r   r   ©Ú.0r   r   r   r   Ú
<listcomp>�   s     z#schur_partition.<locals>.<listcomp>éÿÿÿÿ)Ú
isinstancer   r   r   r(   ÚlenÚ_generate_next_listÚrange)r'   Znumber_of_subsetsZsum_free_subsetsZmissed_elementsr   r   r   Úschur_partitionO   s    /

$r2   c                    st   g }| D ]:}‡ fdd„|D ƒ}‡ fdd„|D ƒ}|| }|  |¡ q‡ fdd„tt| ƒd ƒD ƒ}|  |¡ |} | S )Nc                    s    g | ]}|d  ˆ kr|d  ‘qS )r   r   ©r+   Únumber©r'   r   r   r,   —   s      z'_generate_next_list.<locals>.<listcomp>c                    s(   g | ] }|d  d ˆ kr|d  d ‘qS r)   r   r3   r5   r   r   r,   ˜   s      c                    s(   g | ] }d | d ˆ krd | d ‘qS r)   r   r*   r5   r   r   r,   œ   s      r   )Úappendr1   r/   )Zcurrent_listr'   Znew_listÚitemZtemp_1Ztemp_2Znew_itemZ	last_listr   r5   r   r0   “   s    
r0   )r"   r$   Z
sympy.corer   Zsympy.core.basicr   Zsympy.core.functionr   Zsympy.core.numbersr   r   r(   r2   r0   r   r   r   r   Ú<module>   s   5D