U
    L?h5#                     @   s   d dl mZmZmZmZmZmZmZmZm	Z	m
Z
mZmZmZ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mZ d dlmZ d dlZd dlmZ d	d
 Zdd Z dd Z!eddge"e"dddZ#e"e"dddZ$edddddddZ%dS )    )fzerofrom_intfrom_rationalfonefhalfbitcountto_intmpf_mulmpf_divmpf_submpf_addmpf_sqrtmpf_pimpf_cosh_sinhmpf_cosmpf_sin)gcdlegendrejacobi   )_sqrt_mod_prime_poweris_quad_residue)
deprecated)recurrence_memoNcountc                  C   s   d} dg|  a dg|  at| d d }td|D ]:}t | dkr2t|| | |D ]}t | dkrR|t |< qRq2td| D ]h}t | dkr|t |< |d t|< qxt | }|| }|| dkrt| | t|< qxt| |d  t|< qxd S )N順 r   r         ?      )_factor_totientintrange)ZmaxnZlimijxy r(   K/var/www/html/venv/lib/python3.8/site-packages/sympy/ntheory/partitions_.py_pre   s&    

r*   c                 C   sR  |dkrt S |}d}t| }|| dkr:|| }|d7 }q|| }dd|   }t|}|dkr|dkrd| }	|	||	  }|td|d |	 |	 }t|d|d d }
tttd|
 ||t|	|}tttd	| t|
d |
 t	t|||t
|||S |dkrd| }	|	||	  }|dkrH|td
|d d |	 |	 }t|d|d d }
tttd|
 ||t|	|}tttdd	|d   t|
d t	t|d ||t
|||S |||  }|| dkr|dkrtttd|t	t|||S tS t||stS ||d  |d  }|td|d | }t|||d }
tttd|
 ||t||}tttdtd| t	t|||t|||S |dks|dkrrt|dt|d }}d||  }|| |  |d d |  t|| | | t| d | | }|| |  |d d |  t|| | | t| d | | }tt|||t||||S |dkrd|  d tdt| d | | }d| d |d d d  |d  d  d }tttd	t||||t|||S d|  d tdt| d | | }d| |d d d  d  d }tt|||t||||S )z Compute the inner sum in HRR formula [1]_

    References
    ==========

    .. [1] https://msp.org/pjm/1956/6-1/pjm-v6-n1-p18-p.pdf

    r   r      r      	         @   i@  r          )r   r    r   powr   r
   r	   r   r   r   r   r   r   r   r   r   r!   _a)nkprecZk1epZk2vpimodmargZ_phiZd1Zd2Zn1Zn2r(   r(   r)   r5   $   s    	



     

   
    
$,
 
$ r5   c                 C   s   t |}t|}t|||}tt | tdd||}t||}tt|||\}	}
tt||tt||||}tt||	t|
|||}t||S )z
    Compute the sinh term in the outer sum of the HRR formula.
    The constants sqrt(2/3*pi) and sqrt(8) must be precomputed.
    r   r+   )r   r   r
   r   r   r   r   r	   )r6   r%   r8   sq23pisqrt8r<   abcchshDEr(   r(   r)   _dz   s    
 rI   )r6   returnc                 C   sz   d}d}t  D ]f}|d| d 7 }| | }|dk r6 qv|| }||d 8 }d|kr^||| 7 }||d rn| n|7 }q|S )zt Calculate the partition function P(n)

    Parameters
    ==========

    n : int
        nonnegative integer

    r   r.   r   r   r   )r6   prevr;   Zpentar$   npsr(   r(   r)   _partition_rec   s    
rN   c                    sB  | dk rdS | dkr$| t   dk s8t  dkr@| dk r@t | S dt krPt  ttjd|  d d  td	|   td
 d td
 }t|d d  }}dtjd  dt	d   tjt	d d tjt	d  fdd}t
dt| d }|| |dk st|dkrB|| |dk rB|d }q|}|d }|| dkr|| d }|| | }dk r|}n|dkrN|}qN|}	|	dkrtdt}
tttdd||t||}ttd|}td|	D ]H}t| ||}t| ||||}t|
t|||}
ttt|d }qttt|
t|S )zX Calculate the partition function P(n)

    Parameters
    ==========

    n : int

    r   i@ F   r   i@8  r    g      @r   r/   
   r   g?d   ,      r.   K   gUUUUUU?c                    s<   t j} || ||| d   t ||  |   S )Nr   )mathsqrtsinh)r6   NrV   c1c2c3r(   r)   _M   s    z_partition.<locals>._Mr-   (   r   zInput too bigr,   2   )rN   Zcache_lengthglobalsr*   r"   rU   r<   loglog2rV   maxceilAssertionError
ValueErrorr   r	   r   r   r   r   r#   r5   rI   r   r   absr   r   )r6   Zpbitsr8   r:   r]   bigZsmallrX   ZerMrM   r@   rA   qrB   dr(   rY   r)   
_partition   sb    	



rl   zpThe `sympy.ntheory.partitions_.npartitions` has been moved to `sympy.functions.combinatorial.numbers.partition`.z1.13z%deprecated-ntheory-symbolic-functions)Zdeprecated_since_versionZactive_deprecations_targetFc                 C   s   ddl m} || S )a
  
    Calculate the partition function P(n), i.e. the number of ways that
    n can be written as a sum of positive integers.

    .. deprecated:: 1.13

        The ``npartitions`` function is deprecated. Use :class:`sympy.functions.combinatorial.numbers.partition`
        instead. See its documentation for more information. See
        :ref:`deprecated-ntheory-symbolic-functions` for details.

    P(n) is computed using the Hardy-Ramanujan-Rademacher formula [1]_.


    The correctness of this implementation has been tested through $10^{10}$.

    Examples
    ========

    >>> from sympy.functions.combinatorial.numbers import partition
    >>> partition(25)
    1958

    References
    ==========

    .. [1] https://mathworld.wolfram.com/PartitionFunctionP.html

    r   )	partition)Z%sympy.functions.combinatorial.numbersrm   )r6   verboseZfunc_partitionr(   r(   r)   npartitions   s    !ro   )F)&Zmpmath.libmpr   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   Zsympy.external.gmpyr   r   r   Zresidue_ntheoryr   r   Zsympy.utilities.decoratorr   Zsympy.utilities.memoizationr   rU   	itertoolsr   r*   r5   rI   r"   rN   rl   ro   r(   r(   r(   r)   <module>   s"   HV
M