o
    ohU                     @   sz  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 d dl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mZmZmZm Z  d d
l!m"Z" G dd deZ#dd Z$G dd dZ%G dd dZ&G dd dZ'e%e'e'e&dZ(G dd de eZ)G dd de)Z*dd Z+G dd de)Z,dd Z-G d d! d!e)Z.d"d# Z/G d$d% d%e)Z0d&d' Z1d(S ))    )prod)Basic)pi)S)exp)
multigamma)sympify_sympify)	ImmutableMatrixInverseTraceDeterminantMatrixSymbol
MatrixBase	Transpose	MatrixSetmatrix2numpy)_value_checkRandomMatrixSymbolNamedArgsMixinPSpace_symbol_converterMatrixDomainDistribution)import_modulec                   @   sf   e Zd ZdZdd Zedd Zedd Zedd Zed	d
 Z	edd Z
dd ZdddZdS )MatrixPSpacezD
    Represents probability space for
    Matrix Distributions.
    c                 C   s@   t |}t|t|}}|jr|jstdt| ||||S )NzDimensions should be integers)r   r	   
is_integer
ValueErrorr   __new__)clssymdistributiondim_ndim_m r$   t/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/stats/matrix_distributions.pyr      s
   zMatrixPSpace.__new__c                 C   
   | j d S )N   argsselfr$   r$   r%   <lambda>       
 zMatrixPSpace.<lambda>c                 C   r&   Nr   r(   r*   r$   r$   r%   r,   !   r-   c                 C   s   t | j| jjS N)r   symbolr!   setr*   r$   r$   r%   domain#   s   zMatrixPSpace.domainc                 C   s   t | j| jd | jd | S )N      )r   r0   r)   r*   r$   r$   r%   value'   s   zMatrixPSpace.valuec                 C   s   | j hS r/   )r5   r*   r$   r$   r%   values+      zMatrixPSpace.valuesc                 G   s4   | t}t|dkst|tstd| j|S )Nr'   ztCurrently, no algorithm has been implemented to handle general expressions containing multiple matrix distributions.)atomsr   len
isinstanceNotImplementedErrorr!   pdf)r+   exprr)   rmsr$   r$   r%   compute_density/   s   
zMatrixPSpace.compute_densityr$   scipyNc                 C   s   | j | jj|||diS )zu
        Internal sample method

        Returns dictionary mapping RandomMatrixSymbol to realization value.
        )libraryseed)r5   r!   sample)r+   sizerA   rB   r$   r$   r%   rC   7   s   zMatrixPSpace.sampler$   r@   N)__name__
__module____qualname____doc__r   propertyr!   r0   r2   r5   r6   r?   rC   r$   r$   r$   r%   r      s    


r   c                 C   sB   t tt|}|| }|j|  |j}t| ||d |d }|jS )Nr   r'   )listmapr   check	dimensionr   r5   )r0   r   r)   distdimpspacer$   r$   r%   rv@   s   
rR   c                   @   &   e Zd ZdZdddZedd ZdS )SampleMatrixScipyz7Returns the sample from scipy of the given distributionNc                 C      |  |||S r/   )_sample_scipyr   rO   rD   rB   r$   r$   r%   r   K      zSampleMatrixScipy.__new__c           
         s   ddl m  ddl} fdd fddd}dd d	d d}| }|jj|vr,dS |du s5t|tr=|jj	|d
}n|}||jj |t
||}	|	|||jj | S )zSample from SciPy.r   )statsNc                    s     j jt| jt| jt|dS )N)dfscalerD   )wishartrvsintnr   scale_matrixfloatrO   rD   
rand_statescipy_statsr$   r%   r,   U   s    z1SampleMatrixScipy._sample_scipy.<locals>.<lambda>c                    s.    j jt| jtt| jtt| jt||dS )N)meanrowcovcolcovrD   random_state)matrix_normalr]   r   location_matrixra   scale_matrix_1scale_matrix_2rb   rd   r$   r%   r,   W   s
    

WishartDistributionMatrixNormalDistributionc                 S      | j jS r/   r`   shaperO   r$   r$   r%   r,   ^       c                 S   rq   r/   rk   rs   rt   r$   r$   r%   r,   _   ru   rB   )r@   rY   numpykeys	__class__rF   r:   r^   randomdefault_rngr   reshape)
r   rO   rD   rB   rx   scipy_rv_mapsample_shape	dist_listrc   sampr$   rd   r%   rV   N   s    


zSampleMatrixScipy._sample_scipyr/   )rF   rG   rH   rI   r   classmethodrV   r$   r$   r$   r%   rT   I   s
    
rT   c                   @   rS   )SampleMatrixNumpyz7Returns the sample from numpy of the given distributionNc                 C   rU   r/   )_sample_numpyrW   r$   r$   r%   r   s   rX   zSampleMatrixNumpy.__new__c           
      C   s   i }i }|  }|jj|vrdS ddl}|du st|tr%|jj|d}n|}||jj |t||}	|		|||jj | S )zSample from NumPy.Nr   rw   )
ry   rz   rF   rx   r:   r^   r{   r|   r   r}   )
r   rO   rD   rB   numpy_rv_mapr   r   rx   rc   r   r$   r$   r%   r   v   s   zSampleMatrixNumpy._sample_numpyr/   )rF   rG   rH   rI   r   r   r   r$   r$   r$   r%   r   o   s
    
r   c                   @   rS   )SampleMatrixPymcz6Returns the sample from pymc of the given distributionNc                 C   rU   r/   )_sample_pymcrW   r$   r$   r%   r      rX   zSampleMatrixPymc.__new__c           	   	      s   zddl  W n ty   ddl Y nw  fdd fddd}dd dd d	}| }|jj|vr6dS ddl}|d
|j	  
  ||jj |  jt|dd|dddd }W d   n1 siw   Y  ||||jj | S )zSample from PyMC.r   Nc                    s0    j dt| jtt| jtt| jt| jjdS )NX)murg   rh   rs   )MatrixNormalr   rk   ra   rl   rm   rs   rt   pymcr$   r%   r,      s    


z/SampleMatrixPymc._sample_pymc.<locals>.<lambda>c                    s    j dt| jt| jtdS )Nr   )nur   )WishartBartlettr^   r_   r   r`   ra   rt   r   r$   r%   r,      s    )rp   ro   c                 S   rq   r/   rr   rt   r$   r$   r%   r,      ru   c                 S   rq   r/   rv   rt   r$   r$   r%   r,      ru   rn   r   r'   F)drawschainsprogressbarrandom_seedreturn_inferencedatacompute_convergence_checksr   )r   ImportErrorpymc3ry   rz   rF   logging	getLoggersetLevelERRORModelrC   r   r}   )	r   rO   rD   rB   pymc_rv_mapr   r   r   sampsr$   r   r%   r      s*   


 zSampleMatrixPymc._sample_pymcr/   )rF   rG   rH   rI   r   r   r   r$   r$   r$   r%   r      s
    
r   )r@   r   r   rx   c                   @   s6   e Zd ZdZdd Zedd Zdd ZdddZd
S )MatrixDistributionz1
    Abstract class for Matrix Distribution.
    c                 G   s    dd |D }t j| g|R  S )Nc                 S   s&   g | ]}t |trt|nt|qS r$   )r:   rK   r
   r	   ).0argr$   r$   r%   
<listcomp>   s
    z.MatrixDistribution.__new__.<locals>.<listcomp>)r   r   )r   r)   r$   r$   r%   r      s   zMatrixDistribution.__new__c                  G   s   d S r/   r$   r(   r$   r$   r%   rM      s   zMatrixDistribution.checkc                 C   s   t |tr	t|}| |S r/   )r:   rK   r
   r<   )r+   r=   r$   r$   r%   __call__   s   

zMatrixDistribution.__call__r$   r@   Nc                 C   sd   g d}||vrt dt| t|std| t| | ||}|dur(|S t d| jj|f )zo
        Internal sample method

        Returns dictionary mapping RandomSymbol to realization value.
        )r@   rx   r   r   z&Sampling from %s is not supported yet.zFailed to import %sNz4Sampling for %s is not currently implemented from %s)r;   strr   r   _get_sample_class_matrixrvrz   rF   )r+   rD   rA   rB   	librariesr   r$   r$   r%   rC      s   
zMatrixDistribution.samplerE   )	rF   rG   rH   rI   r   staticmethodrM   r   rC   r$   r$   r$   r%   r      s    
r   c                   @   <   e Zd ZdZedd Zedd Zedd Zdd	 Z	d
S )MatrixGammaDistributionalphabetar`   c                 C   s>   t |tst|jd t|jd t| jd t|jd d S )N+The shape matrix must be positive definite.Should be square matrix#Shape parameter should be positive.z#Scale parameter should be positive.r:   r   r   is_positive_definite	is_squareis_positiver   r$   r$   r%   rM      s
   
zMatrixGammaDistribution.checkc                 C      | j jd }t||tjS r.   r`   rs   r   r   Realsr+   kr$   r$   r%   r1         zMatrixGammaDistribution.setc                 C   rq   r/   rr   r*   r$   r$   r%   rN     r7   z!MatrixGammaDistribution.dimensionc           
      C   s   | j | j| j}}}|jd }t|trt|}t|ttfs(t	dt
| t| | | }tt||||  t||  }t||  }t||t|d d   }	|| |	 S )Nr   4%s should be an isinstance of Matrix or MatrixSymbolr'   r3   )r   r   r`   rs   r:   rK   r
   r   r   r   r   r   r   r   r   r   r   )
r+   xr   r   r`   psigma_inv_xterm1term2term3r$   r$   r%   r<     s   

"zMatrixGammaDistribution.pdfN
rF   rG   rH   	_argnamesr   rM   rJ   r1   rN   r<   r$   r$   r$   r%   r      s    
	

r   c                 C   s$   t |tr	t|}t| t|||fS )a  
    Creates a random variable with Matrix Gamma Distribution.

    The density of the said distribution can be found at [1].

    Parameters
    ==========

    alpha: Positive Real number
        Shape Parameter
    beta: Positive Real number
        Scale Parameter
    scale_matrix: Positive definite real square matrix
        Scale Matrix

    Returns
    =======

    RandomSymbol

    Examples
    ========

    >>> from sympy.stats import density, MatrixGamma
    >>> from sympy import MatrixSymbol, symbols
    >>> a, b = symbols('a b', positive=True)
    >>> M = MatrixGamma('M', a, b, [[2, 1], [1, 2]])
    >>> X = MatrixSymbol('X', 2, 2)
    >>> density(M)(X).doit()
    exp(Trace(Matrix([
    [-2/3,  1/3],
    [ 1/3, -2/3]])*X)/b)*Determinant(X)**(a - 3/2)/(3**a*sqrt(pi)*b**(2*a)*gamma(a)*gamma(a - 1/2))
    >>> density(M)([[1, 0], [0, 1]]).doit()
    exp(-4/(3*b))/(3**a*sqrt(pi)*b**(2*a)*gamma(a)*gamma(a - 1/2))


    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Matrix_gamma_distribution

    )r:   rK   r
   rR   r   )r0   r   r   r`   r$   r$   r%   MatrixGamma  s   
+r   c                   @   r   )ro   r_   r`   c                 C   s2   t |tst|jd t|jd t| jd d S )Nr   r   r   r   r   r$   r$   r%   rM   L  s   
zWishartDistribution.checkc                 C   r   r.   r   r   r$   r$   r%   r1   U  r   zWishartDistribution.setc                 C   rq   r/   rr   r*   r$   r$   r%   rN   Z  r7   zWishartDistribution.dimensionc           	      C   s   | j | j}}|jd }t|trt|}t|ttfs$tdt	| t
| | td }tt|d|| td  t|td |  }t|| td  }t|t|| d d  }|| | S )Nr   r   r3   r'   )r_   r`   rs   r:   rK   r
   r   r   r   r   r   r   r   r   r   r   )	r+   r   r_   r`   r   r   r   r   r   r$   r$   r%   r<   ^  s   

2zWishartDistribution.pdfNr   r$   r$   r$   r%   ro   H  s    


ro   c                 C   s"   t |tr	t|}t| t||fS )a  
    Creates a random variable with Wishart Distribution.

    The density of the said distribution can be found at [1].

    Parameters
    ==========

    n: Positive Real number
        Represents degrees of freedom
    scale_matrix: Positive definite real square matrix
        Scale Matrix

    Returns
    =======

    RandomSymbol

    Examples
    ========

    >>> from sympy.stats import density, Wishart
    >>> from sympy import MatrixSymbol, symbols
    >>> n = symbols('n', positive=True)
    >>> W = Wishart('W', n, [[2, 1], [1, 2]])
    >>> X = MatrixSymbol('X', 2, 2)
    >>> density(W)(X).doit()
    exp(Trace(Matrix([
    [-1/3,  1/6],
    [ 1/6, -1/3]])*X))*Determinant(X)**(n/2 - 3/2)/(2**n*3**(n/2)*sqrt(pi)*gamma(n/2)*gamma(n/2 - 1/2))
    >>> density(W)([[1, 0], [0, 1]]).doit()
    exp(-2/3)/(2**n*3**(n/2)*sqrt(pi)*gamma(n/2)*gamma(n/2 - 1/2))

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Wishart_distribution

    )r:   rK   r
   rR   ro   )r0   r_   r`   r$   r$   r%   Wishartl  s   
(r   c                   @   r   )rp   )rk   rl   rm   c                 C   s   t |tst|jd t |tst|jd t|jd t|jd | jd }| jd }t|jd |kdt|t|f  t|jd |kdt|t|f  d S )Nr   )Scale matrix 1 should be be square matrix)Scale matrix 2 should be be square matrixr   r'   )Scale matrix 1 should be of shape %s x %s)Scale matrix 2 should be of shape %s x %s)r:   r   r   r   r   rs   r   )rk   rl   rm   r_   r   r$   r$   r%   rM     s   




zMatrixNormalDistribution.checkc                 C      | j j\}}t||tjS r/   rk   rs   r   r   r   r+   r_   r   r$   r$   r%   r1     r   zMatrixNormalDistribution.setc                 C   rq   r/   rv   r*   r$   r$   r%   rN     r7   z"MatrixNormalDistribution.dimensionc           
      C   s   | j | j| j}}}|j\}}t|trt|}t|ttfs(t	dt
| t|t||  t| ||  }tt| td }dt t|| d  t|t|d   t|t|d   }	||	 S )Nr   r3   )rk   rl   rm   rs   r:   rK   r
   r   r   r   r   r   r   r   r   r   r   r   )
r+   r   MUVr_   r   r   numdenr$   r$   r%   r<     s   

$@zMatrixNormalDistribution.pdfNr   r$   r$   r$   r%   rp     s    


rp   c                 C   sL   t |tr	t|}t |trt|}t |trt|}|||f}t| t|S )a  
    Creates a random variable with Matrix Normal Distribution.

    The density of the said distribution can be found at [1].

    Parameters
    ==========

    location_matrix: Real ``n x p`` matrix
        Represents degrees of freedom
    scale_matrix_1: Positive definite matrix
        Scale Matrix of shape ``n x n``
    scale_matrix_2: Positive definite matrix
        Scale Matrix of shape ``p x p``

    Returns
    =======

    RandomSymbol

    Examples
    ========

    >>> from sympy import MatrixSymbol
    >>> from sympy.stats import density, MatrixNormal
    >>> M = MatrixNormal('M', [[1, 2]], [1], [[1, 0], [0, 1]])
    >>> X = MatrixSymbol('X', 1, 2)
    >>> density(M)(X).doit()
    exp(-Trace((Matrix([
    [-1],
    [-2]]) + X.T)*(Matrix([[-1, -2]]) + X))/2)/(2*pi)
    >>> density(M)([[3, 4]]).doit()
    exp(-4)/(2*pi)

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Matrix_normal_distribution

    )r:   rK   r
   rR   rp   )r0   rk   rl   rm   r)   r$   r$   r%   r     s   
)


r   c                   @   r   )MatrixStudentTDistribution)r   rk   rl   rm   c                 C   s   t |tst|jdkd t |tst|jdkd t|jdkd t|jdkd |jd }|jd }t|jd |kdt|t|f  t|jd |kdt|t|f  t| jdkd	 d S )
NFr   r   r   r   r'   r   r   z#Degrees of freedom must be positive)r:   r   r   r   r   rs   r   r   )r   rk   rl   rm   r_   r   r$   r$   r%   rM     s   



z MatrixStudentTDistribution.checkc                 C   r   r/   r   r   r$   r$   r%   r1     r   zMatrixStudentTDistribution.setc                 C   rq   r/   rv   r*   r$   r$   r%   rN     r7   z$MatrixStudentTDistribution.dimensionc           
      C   s  ddl m} t|trt|}t|ttfstdt| | j	| j
| j| jf\}}}}|j\}}t|| | d d |t|| d   t|| d   t|| d  t|| d d |  }	|	t||t|||  t| t||   || | d  d   S )Nr   )eyer   r'   r3   )sympy.matrices.denser   r:   rK   r
   r   r   r   r   r   rk   rl   rm   rs   r   r   r   r   r   )
r+   r   r   r   r   OmegaSigmar_   r   Kr$   r$   r%   r<     s   

<$0zMatrixStudentTDistribution.pdfNr   r$   r$   r$   r%   r     s    


r   c                 C   sN   t |tr	t|}t |trt|}t |trt|}||||f}t| t|S )a  
    Creates a random variable with Matrix Gamma Distribution.

    The density of the said distribution can be found at [1].

    Parameters
    ==========

    nu: Positive Real number
        degrees of freedom
    location_matrix: Positive definite real square matrix
        Location Matrix of shape ``n x p``
    scale_matrix_1: Positive definite real square matrix
        Scale Matrix of shape ``p x p``
    scale_matrix_2: Positive definite real square matrix
        Scale Matrix of shape ``n x n``

    Returns
    =======

    RandomSymbol

    Examples
    ========

    >>> from sympy import MatrixSymbol,symbols
    >>> from sympy.stats import density, MatrixStudentT
    >>> v = symbols('v',positive=True)
    >>> M = MatrixStudentT('M', v, [[1, 2]], [[1, 0], [0, 1]], [1])
    >>> X = MatrixSymbol('X', 1, 2)
    >>> density(M)(X)
    gamma(v/2 + 1)*Determinant((Matrix([[-1, -2]]) + X)*(Matrix([
    [-1],
    [-2]]) + X.T) + Matrix([[1]]))**(-v/2 - 1)/(pi**1.0*gamma(v/2)*Determinant(Matrix([[1]]))**1.0*Determinant(Matrix([
    [1, 0],
    [0, 1]]))**0.5)

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Matrix_t-distribution

    )r:   rK   r
   rR   r   )r0   r   rk   rl   rm   r)   r$   r$   r%   MatrixStudentT/  s   
,

r   N)2mathr   sympy.core.basicr   sympy.core.numbersr   sympy.core.singletonr   &sympy.functions.elementary.exponentialr   'sympy.functions.special.gamma_functionsr   sympy.core.sympifyr   r	   sympy.matricesr
   r   r   r   r   r   r   r   r   sympy.stats.rvr   r   r   r   r   r   r   sympy.externalr   r   rR   rT   r   r   r   r   r   r   ro   r   rp   r   r   r   r$   r$   r$   r%   <module>   s:    ,$,	&)0%2$/-52