o
    §ƒðjà;  ã                   @   sú  d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm Z m!Z!m"Z"m#Z# d dl$m%Z% d dl&m'Z' d dl(m)Z) g d¢Z*e# +e"¡dd„ ƒZ,G dd„ deƒZ-G dd„ de-ƒZ.G dd„ de.ƒZ/G dd„ de.ƒZ0G dd „ d e.ƒZ1d!d"„ Z2d#d$„ Z3d%d&„ Z4d'd(„ Z5G d)d*„ d*e-ƒZ6G d+d,„ d,e6ƒZ7G d-d.„ d.e6ƒZ8G d/d0„ d0e6ƒZ9d1d2„ Z:d3d4„ Z;d5d6„ Z<d7d8„ Z=d9d:„ Z>d;d<„ Z?d=d>„ Z@d?S )@é    )ÚProduct)ÚSum)ÚBasic)ÚLambda)ÚIÚpi)ÚS)ÚDummy)ÚAbs)Úexp)Úgamma)ÚIntegral)ÚMatrixSymbol)ÚTrace)ÚIndexedBase)Ú_sympify)Ú_symbol_converterÚDensityÚRandomMatrixSymbolÚ	is_random)ÚJointDistributionHandmade)ÚRandomMatrixPSpace)ÚArrayComprehension)ÚCircularEnsembleÚCircularUnitaryEnsembleÚCircularOrthogonalEnsembleÚCircularSymplecticEnsembleÚGaussianEnsembleÚGaussianUnitaryEnsembleÚGaussianOrthogonalEnsembleÚGaussianSymplecticEnsembleÚjoint_eigen_distributionÚJointEigenDistributionÚlevel_spacing_distributionc                 C   s   dS )NT© )Úxr$   r$   ú¢/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/stats/random_matrix_models.pyÚ_#   s   r'   c                   @   sB   e Zd ZdZddd„Zedd„ ƒZedd„ ƒZdd	„ Zd
d„ Z	dS )ÚRandomMatrixEnsembleModelz¶
    Base class for random matrix ensembles.
    It acts as an umbrella and contains
    the methods common to all the ensembles
    defined in sympy.stats.random_matrix_models.
    Nc                 C   s6   t |ƒt|ƒ}}|jdkrtd| ƒ‚t | ||¡S )NFzGDimension of the random matrices must be integers, received %s instead.)r   r   Ú
is_integerÚ
ValueErrorr   Ú__new__)ÚclsÚsymÚdimr$   r$   r&   r+   /   s   
ÿz!RandomMatrixEnsembleModel.__new__c                 C   ó
   | j d S )Nr   ©Úargs©Úselfr$   r$   r&   Ú<lambda>6   ó   
 z"RandomMatrixEnsembleModel.<lambda>c                 C   r/   )Né   r0   r2   r$   r$   r&   r4   7   r5   c                 C   s   t |ƒS ©N)r   ©r3   Úexprr$   r$   r&   Údensity9   s   z!RandomMatrixEnsembleModel.densityc                 C   s
   |   |¡S r7   )r:   r8   r$   r$   r&   Ú__call__<   s   
z"RandomMatrixEnsembleModel.__call__r7   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r+   ÚpropertyÚsymbolÚ	dimensionr:   r;   r$   r$   r$   r&   r(   (   s    
r(   c                   @   ó    e Zd ZdZdd„ Zdd„ ZdS )ÚGaussianEnsembleModela  
    Abstract class for Gaussian ensembles.
    Contains the properties common to all the
    gaussian ensembles.

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Random_matrix#Gaussian_ensembles
    .. [2] https://arxiv.org/pdf/1712.07903.pdf
    c                    s~   t |ƒ}‡ fdd„}tdddd�}t||ƒ|d|fƒ ¡ }dˆ |  ˆ | |d  d |d   }dt |d  }|| | S )	a  
        Helper function for computing normalization
        constant for joint probability density of eigen
        values of Gaussian ensembles.

        References
        ==========

        .. [1] https://en.wikipedia.org/wiki/Selberg_integral#Mehta's_integral
        c                    s.   t dˆ t| ƒ d  ƒt tjˆ tdƒ  ƒ S )Nr6   é   )r   r   ÚOne)Új©Úbetar$   r&   r4   W   s   . zGGaussianEnsembleModel._compute_normalization_constant.<locals>.<lambda>rG   T©ÚintegerÚpositiver6   rE   é   )r   r	   r   Údoitr   )r3   rI   ÚnÚ	prod_termrG   Úterm1Úterm2Úterm3r$   rH   r&   Ú_compute_normalization_constantK   s   (z5GaussianEnsembleModel._compute_normalization_constantc                 C   sî   | j }|  ||¡}tdƒ}tdddd�}tdddd�}tdddd�}tt|ƒ d t|| d |d|fƒ ¡  ƒ}t|t	t
|| ||  ƒ| ||d |fƒƒ}	t	|	|ƒ ¡ |d|d fƒ ¡ }
t|| |d|fƒ ¡ }tt|ƒ||
 | ƒS )	zˆ
        Helper function for computing the joint
        probability distribution of eigen values
        of the random matrix.
        ÚlÚiTrJ   rG   ÚkrE   r6   )rB   rT   r   r	   r   r   r   rN   r   r   r
   r   Útuple)r3   rI   rO   ÚZbnrU   rV   rG   rW   rQ   Úsub_termrR   Úsymsr$   r$   r&   Ú!_compute_joint_eigen_distribution^   s   .. z7GaussianEnsembleModel._compute_joint_eigen_distributionN)r<   r=   r>   r?   rT   r\   r$   r$   r$   r&   rD   ?   s    rD   c                   @   ó0   e Zd Zedd„ ƒZdd„ Zdd„ Zdd„ Zd	S )
ÚGaussianUnitaryEnsembleModelc                 C   s*   | j }dt|ƒd  tt|d ƒd   S ©NrE   )rB   r   r   )r3   rO   r$   r$   r&   Únormalization_constantq   s   $z3GaussianUnitaryEnsembleModel.normalization_constantc                 C   sV   | j | j}}td| d�}td|||d�}t|tt|ƒ d t|d ƒ ƒ| ƒ|ƒS ©NÚP©ÚmodelÚH©ÚpspacerE   ©rB   r`   r   r   r   r   r   r   )r3   r9   rO   ÚZGUEÚh_pspacere   r$   r$   r&   r:   v   ó   ,z$GaussianUnitaryEnsembleModel.densityc                 C   ó   |   tdƒ¡S r_   ©r\   r   r2   r$   r$   r&   r!   |   ó   z5GaussianUnitaryEnsembleModel.joint_eigen_distributionc                 C   s:   t dƒ}dtd  |d  tdt |d  ƒ }t||ƒS )NÚsé    rE   éüÿÿÿ©r	   r   r   r   ©r3   ro   Úfr$   r$   r&   r#      s   (
z7GaussianUnitaryEnsembleModel.level_spacing_distributionN©r<   r=   r>   r@   r`   r:   r!   r#   r$   r$   r$   r&   r^   p   s    
r^   c                   @   r]   )
ÚGaussianOrthogonalEnsembleModelc                 C   s4   | j }td||ƒ}ttt|ƒ d t|d ƒ ƒƒS )NÚ_HrM   rE   ©rB   r   r   r   r   r   ©r3   rO   rw   r$   r$   r&   r`   …   s   "z6GaussianOrthogonalEnsembleModel.normalization_constantc                 C   sV   | j | j}}td| d�}td|||d�}t|tt|ƒ d t|d ƒ ƒ| ƒ|ƒS )Nrb   rc   re   rf   rM   rE   rh   )r3   r9   rO   ÚZGOErj   re   r$   r$   r&   r:   ‹   rk   z'GaussianOrthogonalEnsembleModel.densityc                 C   ó   |   tj¡S r7   ©r\   r   rF   r2   r$   r$   r&   r!   ‘   ó   z8GaussianOrthogonalEnsembleModel.joint_eigen_distributionc                 C   s4   t dƒ}td | tt d |d  ƒ }t||ƒS )Nro   rE   rM   rr   rs   r$   r$   r&   r#   ”   s   "
z:GaussianOrthogonalEnsembleModel.level_spacing_distributionNru   r$   r$   r$   r&   rv   „   ó    
rv   c                   @   r]   )
ÚGaussianSymplecticEnsembleModelc                 C   s0   | j }td||ƒ}ttt|ƒ t|d ƒ ƒƒS )Nrw   rE   rx   ry   r$   r$   r&   r`   š   s   z6GaussianSymplecticEnsembleModel.normalization_constantc                 C   sR   | j | j}}td| d�}td|||d�}t|tt|ƒ t|d ƒ ƒ| ƒ|ƒS ra   rh   )r3   r9   rO   ÚZGSErj   re   r$   r$   r&   r:       s   (z'GaussianSymplecticEnsembleModel.densityc                 C   rl   ©NrM   rm   r2   r$   r$   r&   r!   ¦   rn   z8GaussianSymplecticEnsembleModel.joint_eigen_distributionc                 C   sR   t dƒ}tdƒd tdƒd td   |d  tddt  |d  ƒ }t||ƒS )	Nro   rE   é   é   é   rM   iÀÿÿÿé	   )r	   r   r   r   r   rs   r$   r$   r&   r#   ©   s   @
z:GaussianSymplecticEnsembleModel.level_spacing_distributionNru   r$   r$   r$   r&   r   ™   r~   r   c                 C   ó8   t | ƒt|ƒ} }t| |ƒ}t| |d�}t| |||d�S ©Nrc   rf   )r   r   rD   r   r   ©r-   r.   rd   Úrmpr$   r$   r&   r   ®   ó   
r   c                 C   r†   )a-  
    Represents Gaussian Unitary Ensembles.

    Examples
    ========

    >>> from sympy.stats import GaussianUnitaryEnsemble as GUE, density
    >>> from sympy import MatrixSymbol
    >>> G = GUE('U', 2)
    >>> X = MatrixSymbol('X', 2, 2)
    >>> density(G)(X)
    exp(-Trace(X**2))/(2*pi**2)
    rc   rf   )r   r   r^   r   r   rˆ   r$   r$   r&   r   ´   ó   
r   c                 C   r†   )aN  
    Represents Gaussian Orthogonal Ensembles.

    Examples
    ========

    >>> from sympy.stats import GaussianOrthogonalEnsemble as GOE, density
    >>> from sympy import MatrixSymbol
    >>> G = GOE('U', 2)
    >>> X = MatrixSymbol('X', 2, 2)
    >>> density(G)(X)
    exp(-Trace(X**2)/2)/Integral(exp(-Trace(_H**2)/2), _H)
    rc   rf   )r   r   rv   r   r   rˆ   r$   r$   r&   r   Ç   r‹   r   c                 C   r†   )aN  
    Represents Gaussian Symplectic Ensembles.

    Examples
    ========

    >>> from sympy.stats import GaussianSymplecticEnsemble as GSE, density
    >>> from sympy import MatrixSymbol
    >>> G = GSE('U', 2)
    >>> X = MatrixSymbol('X', 2, 2)
    >>> density(G)(X)
    exp(-2*Trace(X**2))/Integral(exp(-2*Trace(_H**2)), _H)
    rc   rf   )r   r   r   r   r   rˆ   r$   r$   r&   r    Ú   r‹   r    c                   @   rC   )ÚCircularEnsembleModelzÝ
    Abstract class for Circular ensembles.
    Contains the properties and methods
    common to all the circular ensembles.

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Circular_ensemble
    c                 C   s   t d|  ƒ‚)NzeSupport for Haar measure hasn't been implemented yet, therefore the density of %s cannot be computed.)ÚNotImplementedErrorr8   r$   r$   r&   r:   ø   s   þzCircularEnsembleModel.densityc           
      C   sè   | j }dt | t|| d d ƒtt|d d ƒƒ|   }tdƒ}tddd�tddd�tddd�}}}t|| |d|fƒ ¡ }ttt	t
t||  ƒt
t||  ƒ ƒ| ||d |fƒ ¡ |d|d fƒ ¡ }	tt|ƒ|	| ƒS )	zª
        Helper function to compute the joint distribution of phases
        of the complex eigen values of matrices belonging to any
        circular ensembles.
        rE   r6   ÚtrV   T)rK   rG   rW   )rB   r   r   r   r   r	   r   rN   r   r
   r   r   r   rX   )
r3   rI   rO   rY   rŽ   rV   rG   rW   r[   rt   r$   r$   r&   r\   ÿ   s   8

ÿ<ÿÿz7CircularEnsembleModel._compute_joint_eigen_distributionN)r<   r=   r>   r?   r:   r\   r$   r$   r$   r&   rŒ   í   s    
rŒ   c                   @   ó   e Zd Zdd„ ZdS )ÚCircularUnitaryEnsembleModelc                 C   rl   r_   rm   r2   r$   r$   r&   r!     rn   z5CircularUnitaryEnsembleModel.joint_eigen_distributionN©r<   r=   r>   r!   r$   r$   r$   r&   r�     ó    r�   c                   @   r�   )ÚCircularOrthogonalEnsembleModelc                 C   r{   r7   r|   r2   r$   r$   r&   r!     r}   z8CircularOrthogonalEnsembleModel.joint_eigen_distributionNr‘   r$   r$   r$   r&   r“     r’   r“   c                   @   r�   )ÚCircularSymplecticEnsembleModelc                 C   rl   r�   rm   r2   r$   r$   r&   r!     rn   z8CircularSymplecticEnsembleModel.joint_eigen_distributionNr‘   r$   r$   r$   r&   r”     r’   r”   c                 C   r†   r‡   )r   r   rŒ   r   r   rˆ   r$   r$   r&   r     rŠ   r   c                 C   r†   )a8  
    Represents Circular Unitary Ensembles.

    Examples
    ========

    >>> from sympy.stats import CircularUnitaryEnsemble as CUE
    >>> from sympy.stats import joint_eigen_distribution
    >>> C = CUE('U', 1)
    >>> joint_eigen_distribution(C)
    Lambda(t[1], Product(Abs(exp(I*t[_j]) - exp(I*t[_k]))**2, (_j, _k + 1, 1), (_k, 1, 0))/(2*pi))

    Note
    ====

    As can be seen above in the example, density of CiruclarUnitaryEnsemble
    is not evaluated because the exact definition is based on haar measure of
    unitary group which is not unique.
    rc   rf   )r   r   r�   r   r   rˆ   r$   r$   r&   r   !  ó   
r   c                 C   r†   )a>  
    Represents Circular Orthogonal Ensembles.

    Examples
    ========

    >>> from sympy.stats import CircularOrthogonalEnsemble as COE
    >>> from sympy.stats import joint_eigen_distribution
    >>> C = COE('O', 1)
    >>> joint_eigen_distribution(C)
    Lambda(t[1], Product(Abs(exp(I*t[_j]) - exp(I*t[_k])), (_j, _k + 1, 1), (_k, 1, 0))/(2*pi))

    Note
    ====

    As can be seen above in the example, density of CiruclarOrthogonalEnsemble
    is not evaluated because the exact definition is based on haar measure of
    unitary group which is not unique.
    rc   rf   )r   r   r“   r   r   rˆ   r$   r$   r&   r   :  r•   r   c                 C   r†   )aA  
    Represents Circular Symplectic Ensembles.

    Examples
    ========

    >>> from sympy.stats import CircularSymplecticEnsemble as CSE
    >>> from sympy.stats import joint_eigen_distribution
    >>> C = CSE('S', 1)
    >>> joint_eigen_distribution(C)
    Lambda(t[1], Product(Abs(exp(I*t[_j]) - exp(I*t[_k]))**4, (_j, _k + 1, 1), (_k, 1, 0))/(2*pi))

    Note
    ====

    As can be seen above in the example, density of CiruclarSymplecticEnsemble
    is not evaluated because the exact definition is based on haar measure of
    unitary group which is not unique.
    rc   rf   )r   r   r”   r   r   rˆ   r$   r$   r&   r   S  r•   r   c                 C   s"   t | tƒstd|  ƒ‚| jj ¡ S )aA  
    For obtaining joint probability distribution
    of eigen values of random matrix.

    Parameters
    ==========

    mat: RandomMatrixSymbol
        The matrix symbol whose eigen values are to be considered.

    Returns
    =======

    Lambda

    Examples
    ========

    >>> from sympy.stats import GaussianUnitaryEnsemble as GUE
    >>> from sympy.stats import joint_eigen_distribution
    >>> U = GUE('U', 2)
    >>> joint_eigen_distribution(U)
    Lambda((l[1], l[2]), exp(-l[1]**2 - l[2]**2)*Product(Abs(l[_i] - l[_j])**2, (_j, _i + 1, 2), (_i, 1, 1))/pi)
    z&%s is not of type, RandomMatrixSymbol.)Ú
isinstancer   r*   rg   rd   r!   ©Úmatr$   r$   r&   r!   l  s   
r!   c                 C   s2   | j dd�}tdd„ t|ƒD ƒƒstdƒ‚t|Ž S )a¦  
    Creates joint distribution of eigen values of matrices with random
    expressions.

    Parameters
    ==========

    mat: Matrix
        The matrix under consideration.

    Returns
    =======

    JointDistributionHandmade

    Examples
    ========

    >>> from sympy.stats import Normal, JointEigenDistribution
    >>> from sympy import Matrix
    >>> A = [[Normal('A00', 0, 1), Normal('A01', 0, 1)],
    ... [Normal('A10', 0, 1), Normal('A11', 0, 1)]]
    >>> JointEigenDistribution(Matrix(A))
    JointDistributionHandmade(-sqrt(A00**2 - 2*A00*A11 + 4*A01*A10 + A11**2)/2
    + A00/2 + A11/2, sqrt(A00**2 - 2*A00*A11 + 4*A01*A10 + A11**2)/2 + A00/2 + A11/2)

    T)Úmultiplec                 s   s   � | ]}t |ƒV  qd S r7   )r   )Ú.0Úeigenvalr$   r$   r&   Ú	<genexpr>¦  s   € z)JointEigenDistribution.<locals>.<genexpr>zWEigen values do not have any random expression, joint distribution cannot be generated.)Ú	eigenvalsÚallÚsetr*   r   )r˜   r�   r$   r$   r&   r"   ‰  s   r"   c                 C   s   | j j ¡ S )aƒ  
    For obtaining distribution of level spacings.

    Parameters
    ==========

    mat: RandomMatrixSymbol
        The random matrix symbol whose eigen values are
        to be considered for finding the level spacings.

    Returns
    =======

    Lambda

    Examples
    ========

    >>> from sympy.stats import GaussianUnitaryEnsemble as GUE
    >>> from sympy.stats import level_spacing_distribution
    >>> U = GUE('U', 2)
    >>> level_spacing_distribution(U)
    Lambda(_s, 32*_s**2*exp(-4*_s**2/pi)/pi**2)

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Random_matrix#Distribution_of_level_spacings
    )rg   rd   r#   r—   r$   r$   r&   r#   «  s   r#   N)AÚsympy.concrete.productsr   Úsympy.concrete.summationsr   Úsympy.core.basicr   Úsympy.core.functionr   Úsympy.core.numbersr   r   Úsympy.core.singletonr   Úsympy.core.symbolr	   Ú$sympy.functions.elementary.complexesr
   Ú&sympy.functions.elementary.exponentialr   Ú'sympy.functions.special.gamma_functionsr   Úsympy.integrals.integralsr   Ú"sympy.matrices.expressions.matexprr   Ú sympy.matrices.expressions.tracer   Úsympy.tensor.indexedr   Úsympy.core.sympifyr   Úsympy.stats.rvr   r   r   r   Úsympy.stats.joint_rv_typesr   Úsympy.stats.random_matrixr   Úsympy.tensor.arrayr   Ú__all__Úregisterr'   r(   rD   r^   rv   r   r   r   r   r    rŒ   r�   r“   r”   r   r   r   r   r!   r"   r#   r$   r$   r$   r&   Ú<module>   sT    
1""