o
    ohU                     @   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   sb   |j r-|tju rtjS |jrtjS |jr|jrtddddddd}|dkr/t|| S d S d S )	Nzk should be a positive integer         ,      )r         r      r   )		is_Numberr   Infinityis_zeroZero
is_integeris_negative
ValueErrorr   )clskfirst_known_schur_numbers r   t/var/www/html/construction_image-detection-poc/venv/lib/python3.10/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r%d| |d   d S d| d d S )	Nr      i     i  r   r   r   )argsr   
is_Integerfunclower_bound)self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   sX   | t ju r	td| dkrtd| dkrd}t|S 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   )nmin_kr   r   r   _schur_subsets_numberA   s   
r.   c                 C   s   t | tr| jstdt| }| dkrdgg}n| dkr#ddgg}n| dkr-g dg}nddgddgg}t||k r`t|| }dd tt|| d d d D }|d	  |7  < t||k s;|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   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,   number_of_subsetssum_free_subsetsmissed_elementsr   r   r   schur_partitionO   s    /

$r;   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   r1   numberr,   r   r   r2      s     z'_generate_next_list.<locals>.<listcomp>c                    s(   g | ]}|d  d  kr|d  d qS r/   r   r<   r>   r   r   r2         ( c                    s(   g | ]}d | d  krd | d qS r/   r   r0   r>   r   r   r2      r?   r   )appendr7   r5   )current_listr,   new_listitemtemp_1temp_2new_item	last_listr   r>   r   r6      s   
r6   )r'   r)   
sympy.corer   sympy.core.basicr   sympy.core.functionr   sympy.core.numbersr   r   r.   r;   r6   r   r   r   r   <module>   s    5D