o
    *ήcU                     @   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)maxnlimijxy r'   @/tmp/pip-target-vg8gfxp4/lib/python/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rd| }	|	||	  }|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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rt|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] 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preck1epk2vpimodmarg_phid1d2n1n2r'   r'   r(   r4   "   s   	





$,
$ r4   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
   )r5   r$   r7   sq23pisqrt8r=   abcchshDEr'   r'   r(   _dx   s   

rO   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	|
||}|rt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+   stepof2   )r   globalsr)   mathr=   logr   max
ValueErrorr
   r   r   r   r   r    r4   rO   r   printr	   r   absr   r   )r5   verbosepbitsr7   r:   sMrF   rG   qrH   dr'   r'   r(   npartitions   s@   

rd   )F)mpmath.libmpr   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   sympy.core.numbersr   residue_ntheoryr   r   r   r   rX   r)   r4   rO   rd   __all__r'   r'   r'   r(   <module>   s   L V

6