U
    ZÊ¹j5#  ã                   @   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rÌt| | t|< qxt| |d  t|< qxd S )Né † r   r   ç      à?é   é   )Ú_factorÚ_totientÚintÚrange)ZmaxnZlimÚiÚjÚxÚy© r(   úX/var/www/html/bwave/python/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�r¾d| }	|	||	  }|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�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   é   é	   é   é   éÿÿÿÿé@   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�rªtdƒ‚t}
tttdd|ƒ|ƒt|ƒ|ƒ}ttdƒ|ƒ}td|	ƒD ]H}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   )Ú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ý