o
    à8VaR  ã                   @   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mZ d dlmZ ddlmZmZmZmZ d dlZdd„ Zdd	„ Zd
d„ Zddd„ZdgZdS )é    )ÚfzeroÚfrom_intÚfrom_rationalÚfoneÚfhalfÚbitcountÚto_intÚto_strÚmpf_mulÚmpf_divÚmpf_subÚmpf_addÚmpf_sqrtÚmpf_piÚmpf_cosh_sinhÚmpf_cosÚmpf_sin)Úigcdé   )Ú_sqrt_mod_prime_powerÚlegendre_symbolÚjacobi_symbolÚis_quad_residueNc                  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)ZmaxnZlimÚiÚjÚxÚy© r%   ú;/usr/lib/python3/dist-packages/sympy/ntheory/partitions_.pyÚ_pre
   s*   

€ör'   c                 C   sV  |dkrt S |}d}t| }|| dkr"|| }|d7 }|| dks|| }dd|   }t|ƒ}|dkrR|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rád| }	|	||	  }|dkr¦|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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] http://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&   r2   "   sº   	þ
ÿÿþ
ýÿþ
ý
þþþ
ýÿÿÿÿ
$,þ
ý$ r2   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
   )r3   r"   r5   Úsq23piÚsqrt8r9   ÚaÚbÚcZchÚshÚDÚEr%   r%   r&   Ú_dx   s   
þ
rE   Fc              
   C   sp  t | ƒ} | dk r
dS | dkrg d¢|  S dtƒ vrtƒ  t tjd|  d d  t d|  ¡ t d	¡ d
 ƒt d	d¡ }t |d d ƒ }}t}tdt d| d  d ƒƒ}|dkr^tdƒ‚t	t
tdd|ƒ|ƒt|ƒ|ƒ}t
tdƒ|ƒ}td
|ƒD ]4}	t| |	|ƒ}
t| |	|||ƒ}t|t	|
|ƒ|ƒ}|r£td|	d|t|
d	ƒt|d	ƒƒ ttt|ƒƒƒd }qyt tt|t|ƒƒƒ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.

    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.ntheory import npartitions
    >>> npartitions(25)
    1958

    References
    ==========

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

    r   r   )r   r   r   r+   r   é   r   r   g      @r   r,   é
   r   gš™™™™™ñ?éd   é   g¸…ëQ¸Î?r   zInput too bigr+   r)   ÚstepZofé2   )r   Úglobalsr'   Úmathr9   Úlogr   ÚmaxÚ
ValueErrorr
   r   r   r   r   r    r2   rE   r   Úprintr	   r   Úabsr   r   )r3   ÚverboseZpbitsr5   r7   ÚsÚMr=   r>   Úqr?   Údr%   r%   r&   ÚnpartitionsŠ   s@   
ÿþþ
ýrX   )F)Zmpmath.libmpr   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   Zsympy.core.numbersr   Zresidue_ntheoryr   r   r   r   rM   r'   r2   rE   rX   Ú__all__r%   r%   r%   r&   Ú<module>   s   L V

6