o
    §ƒðj##  ã                   @   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 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ƒdedefdd„ƒZdedef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é   )Ú_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r6t|| | |ƒD ]}t | dkr5|t |< q)qtd| ƒD ]4}t | dkrO|t |< |d t|< q<t | }|| }|| dkrft| | t|< q<t| |d  t|< q<d S )Né † r   r   ç      à?é   é   )Ú_factorÚ_totientÚintÚrange)ÚmaxnÚlimÚiÚjÚxÚy© r'   ú›/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/ntheory/partitions_.pyÚ_pre   s*   

€ör)   c                 C   sf  |dkrt S |}d}t| }|| dkr"|| }|d7 }|| dks|| }dd|   }t|ƒ}|dk�rX|dkr…d| }	|	||	  }|td|d |	ƒ |	 }t|d|d ƒd }
tttd|
 ƒ||ƒt|	ƒ|ƒ}tttd	| d|
d   ƒtt|ƒ|ƒ|ƒt	||ƒ|ƒS |dkrãd| }	|	||	  }|dkr¥|td
|d d |	ƒ |	 }t|d|d ƒd }
tttd|
 ƒ||ƒt|	ƒ|ƒ}tttdd	|d   dd|
d    ƒtt|d ƒ|ƒ|ƒt	||ƒ|ƒS |||  }|d dv rñd	nd}|| dk�r|dk�rtt|ƒ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t|ƒ|ƒ|ƒt||ƒ|ƒS |dk�sb|dk�rÃt |d¡t |d¡}}d||  }|| |  |d d |  t|| | | t| d |ƒ | }|| |  |d d |  t|| | | t| d |ƒ | }tt|||ƒt|||ƒ|ƒS |dk�rd|  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   é   é	   é   é   éÿÿÿÿé@   é   )r   é   i@  r   é€   é    )r   r   r   Úpowr   r
   r	   r   r   r   r   r   r   ÚmathÚgcdr   Ú_a)ÚnÚkÚprecÚk1ÚeÚpÚk2ÚvÚpiÚmodÚmÚargÚjacobi3Ú_phiÚd1Úd2Ún1Ún2r'   r'   r(   r8   "   s¼   	þ
ÿÿþ
ýÿ"þ
ý
þþ
þ
ýÿÿÿÿ
$,þ
ý$ r8   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	   )r9   r$   r;   Úsq23piÚsqrt8rA   ÚaÚbÚcÚchÚshÚDÚEr'   r'   r(   Ú_dy   s   
þ
rT   r9   Úreturnc                 C   s|   d}d}t ƒ D ]4}|d| d 7 }| | }|dk r |S || }||d 8 }d|kr0||| 7 }||d r8| n|7 }q|S )zt Calculate the partition function P(n)

    Parameters
    ==========

    n : int
        nonnegative integer

    r   r-   r   r   r   )r9   Úprevr@   Úpentar#   ÚnpÚsr'   r'   r(   Ú_partition_rec‹   s   
úrZ   c                    sL  | dk rdS | dkr| t  ¡  dk st  ¡ dkr | dk r t | ƒS dtƒ vr(tƒ  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�J ‚|dkr§|| |ƒdk r§|d }|dkr§|| |ƒdk s˜|}|d }|| dkrÑ|| d }|| |ƒ }dk rÅ|}n|dkrË|}|| dks³|}	|	dkrÛtdƒ‚t}
tttdd|ƒ|ƒt|ƒ|ƒ}ttdƒ|ƒ}td|	ƒD ]#}t| ||ƒ}t| ||||ƒ}t|
t||ƒ|ƒ}
ttt|ƒƒƒd }qøt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   )r6   ÚsqrtÚsinh)r9   ÚNra   ©Úc1Úc2Úc3r'   r(   Ú_MÏ   s   6z_partition.<locals>._Mr,   é(   r   zInput too bigr+   é2   )rZ   Úcache_lengthÚglobalsr)   r   r6   rA   ÚlogÚlog2ra   ÚmaxÚceilÚ
ValueErrorr   r	   r   r   r   r   r    r8   rT   r   r   Úabsr   r   )r9   Úpbitsr;   r>   rh   ÚbigÚsmallrc   ÚerÚMrY   rK   rL   ÚqrM   Údr'   rd   r(   Ú
_partition¦   s`   	
ÿþþýÿûrz   zpThe `sympy.ntheory.partitions_.npartitions` has been moved to `sympy.functions.combinatorial.numbers.partition`.z1.13z%deprecated-ntheory-symbolic-functions)Údeprecated_since_versionÚ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)Ú%sympy.functions.combinatorial.numbersr}   )r9   ÚverboseÚfunc_partitionr'   r'   r(   Únpartitionsó   s   !r�   )F)"Úmpmath.libmpr   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   Úresidue_ntheoryr   r   Úsympy.utilities.decoratorr   Úsympy.utilities.memoizationr   r6   Ú	itertoolsr   r)   r8   rT   r   rZ   rz   r�   r'   r'   r'   r(   Ú<module>   s"   H W
Mý