o
    oh;                     @   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$   t/var/www/html/construction_image-detection-poc/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""