U
    9%eU                     @   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distributionZdim_nZdim_m r"   _/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/stats/matrix_distributions.pyr      s
    zMatrixPSpace.__new__c                 C   s
   | j d S )N   argsselfr"   r"   r#   <lambda>        zMatrixPSpace.<lambda>c                 C   s
   | j d S 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   r-   r&   r'   r"   r"   r#   value'   s    zMatrixPSpace.valuec                 C   s   | j hS r,   )r2   r'   r"   r"   r#   values+   s    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.)Zatomsr   len
isinstanceNotImplementedErrorr!   pdf)r(   exprr&   Z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)r2   r!   sample)r(   sizer;   r<   r"   r"   r#   r=   7   s    zMatrixPSpace.sample)r"   r:   N)__name__
__module____qualname____doc__r   propertyr!   r-   r/   r2   r3   r9   r=   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   r2   )r-   r   r&   distdimZpspacer"   r"   r#   rv@   s    
rJ   c                   @   s&   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   |  |||S r,   )_sample_scipyr   rH   r>   r<   r"   r"   r#   r   K   s    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|krXdS |dksjt|trz|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)Zdfscaler>   )Zwishartrvsintnr   scale_matrixfloatrH   r>   
rand_stateZ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)Zmeanrowcovcolcovr>   Zrandom_state)Zmatrix_normalrP   r   location_matrixrT   scale_matrix_1scale_matrix_2rU   rW   r"   r#   r)   W   s   


  WishartDistributionMatrixNormalDistributionc                 S   s   | j jS r,   rS   shaperH   r"   r"   r#   r)   ^   r*   c                 S   s   | j jS r,   rZ   ra   rb   r"   r"   r#   r)   _   r*   r<   )r:   rN   numpykeys	__class__r?   r5   rQ   randomdefault_rngr   reshape)
r   rH   r>   r<   re   Zscipy_rv_mapsample_shape	dist_listrV   sampr"   rW   r#   rL   N   s     


zSampleMatrixScipy._sample_scipy)N)r?   r@   rA   rB   r   classmethodrL   r"   r"   r"   r#   rK   I   s   
rK   c                   @   s&   e Zd ZdZdddZedd ZdS )SampleMatrixNumpyz7Returns the sample from numpy of the given distributionNc                 C   s   |  |||S r,   )_sample_numpyrM   r"   r"   r#   r   s   s    zSampleMatrixNumpy.__new__c           
      C   s   i }i }|  }|jj|kr dS ddl}|dks:t|trJ|jj|d}n|}||jj |t||}	|		|||jj | S )zSample from NumPy.Nr   rd   )
rf   rg   r?   re   r5   rQ   rh   ri   r   rj   )
r   rH   r>   r<   Znumpy_rv_maprk   rl   re   rV   rm   r"   r"   r#   rp   v   s    zSampleMatrixNumpy._sample_numpy)N)r?   r@   rA   rB   r   rn   rp   r"   r"   r"   r#   ro   o   s   
ro   c                   @   s&   e Zd ZdZdddZedd ZdS )SampleMatrixPymcz6Returns the sample from pymc of the given distributionNc                 C   s   |  |||S r,   )_sample_pymcrM   r"   r"   r#   r      s    zSampleMatrixPymc.__new__c           	   	      s   zddl  W n tk
r(   ddl Y nX  fdd fddd}dd dd d	}| }|jj|krndS ddl}|d
|j	  
 4 ||jj |  jt|dd|dddd }W 5 Q R X ||||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)murX   rY   ra   )MatrixNormalr   rZ   rT   r[   r\   ra   rb   pymcr"   r#   r)      s
   


z/SampleMatrixPymc._sample_pymc.<locals>.<lambda>c                    s    j dt| jt| jtdS )Nrs   )nur   )ZWishartBartlettrQ   rR   r   rS   rT   rb   rv   r"   r#   r)      s    
)r_   r^   c                 S   s   | j jS r,   r`   rb   r"   r"   r#   r)      r*   c                 S   s   | j jS r,   rc   rb   r"   r"   r#   r)      r*   r]   rw   r$   F)ZdrawschainsZprogressbarZrandom_seedZreturn_inferencedataZcompute_convergence_checksrs   )rw   ImportErrorpymc3rf   rg   r?   logging	getLoggersetLevelERRORZModelr=   r   rj   )	r   rH   r>   r<   Zpymc_rv_maprk   rl   r|   sampsr"   rv   r#   rr      s&    


(zSampleMatrixPymc._sample_pymc)N)r?   r@   rA   rB   r   rn   rr   r"   r"   r"   r#   rq      s   
rq   )r:   r{   rw   re   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| f| S )Nc                 S   s&   g | ]}t |trt|nt|qS r"   )r5   rD   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#   rF      s    zMatrixDistribution.checkc                 C   s   t |trt|}| |S r,   )r5   rD   r
   r7   )r(   r8   r"   r"   r#   __call__   s    
zMatrixDistribution.__call__r"   r:   Nc                 C   sl   ddddg}||kr$t dt| t|s8td| t| | ||}|dk	rT|S t d| jj|f dS )	zo
        Internal sample method

        Returns dictionary mapping RandomSymbol to realization value.
        r:   re   r{   rw   z&Sampling from %s is not supported yet.zFailed to import %sNz4Sampling for %s is not currently implemented from %s)r6   strr   r   _get_sample_class_matrixrvrg   r?   )r(   r>   r;   r<   	librariesr   r"   r"   r#   r=      s    
zMatrixDistribution.sample)r"   r:   N)	r?   r@   rA   rB   r   staticmethodrF   r   r=   r"   r"   r"   r#   r      s   
r   c                   @   s<   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betarS   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.r5   r   r   is_positive_definite	is_squareis_positiver   r"   r"   r#   rF      s
    
zMatrixGammaDistribution.checkc                 C   s   | j jd }t||tjS r+   rS   ra   r   r   Realsr(   kr"   r"   r#   r.      s    zMatrixGammaDistribution.setc                 C   s   | j jS r,   r`   r'   r"   r"   r#   rG     s    z!MatrixGammaDistribution.dimensionc           
      C   s   | j | j| j  }}}|jd }t|tr2t|}t|ttfsPt	dt
| t| | | }tt||||  t||  }t||  }t||t|d d   }	|| |	 S )Nr   4%s should be an isinstance of Matrix or MatrixSymbolr$   r0   )r   r   rS   ra   r5   rD   r
   r   r   r   r   r   r   r   r   r   r   )
r(   xr   r   rS   psigma_inv_xterm1term2term3r"   r"   r#   r7     s    

"zMatrixGammaDistribution.pdfN
r?   r@   rA   Z	_argnamesr   rF   rC   r.   rG   r7   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

    )r5   rD   r
   rJ   r   )r-   r   r   rS   r"   r"   r#   MatrixGamma  s    +
r   c                   @   s<   e Zd ZdZedd Zedd Zedd Zdd	 Z	d
S )r^   rR   rS   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#   rF   L  s    
zWishartDistribution.checkc                 C   s   | j jd }t||tjS r+   r   r   r"   r"   r#   r.   U  s    zWishartDistribution.setc                 C   s   | j jS r,   r`   r'   r"   r"   r#   rG   Z  s    zWishartDistribution.dimensionc           	      C   s   | j | j }}|jd }t|tr*t|}t|ttfsHtdt	| t
| | td }tt|d|| td  t|td |  }t|| td  }t|t|| d d  }|| | S )Nr   r   r0   r$   )rR   rS   ra   r5   rD   r
   r   r   r   r   r   r   r   r   r   r   )	r(   r   rR   rS   r   r   r   r   r   r"   r"   r#   r7   ^  s    

2zWishartDistribution.pdfNr   r"   r"   r"   r#   r^   H  s   


r^   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

    )r5   rD   r
   rJ   r^   )r-   rR   rS   r"   r"   r#   Wishartl  s    (
r   c                   @   s<   e Zd ZdZedd Zedd Zedd Zdd	 Z	d
S )r_   )rZ   r[   r\   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)r5   r   r   r   r   ra   r   )rZ   r[   r\   rR   r   r"   r"   r#   rF     s    



zMatrixNormalDistribution.checkc                 C   s   | j j\}}t||tjS r,   rZ   ra   r   r   r   r(   rR   r   r"   r"   r#   r.     s    zMatrixNormalDistribution.setc                 C   s   | j jS r,   rc   r'   r"   r"   r#   rG     s    z"MatrixNormalDistribution.dimensionc           
      C   s   | j | j| j  }}}|j\}}t|tr2t|}t|ttfsPt	dt
| t|t||  t| ||  }tt| td }dt t|| d  t|t|d   t|t|d   }	||	 S )Nr   r0   )rZ   r[   r\   ra   r5   rD   r
   r   r   r   r   r   r   r   r   r   r   r   )
r(   r   MUVrR   r   r   numZdenr"   r"   r#   r7     s    

$@zMatrixNormalDistribution.pdfNr   r"   r"   r"   r#   r_     s   


r_   c                 C   sL   t |trt|}t |tr$t|}t |tr6t|}|||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

    )r5   rD   r
   rJ   r_   )r-   rZ   r[   r\   r&   r"   r"   r#   ru     s    )



ru   c                   @   s<   e Zd ZdZedd Zedd Zedd Zdd	 Z	d
S )MatrixStudentTDistribution)rx   rZ   r[   r\   c                 C   s   t |tst|jdkd t |ts4t|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)r5   r   r   r   r   ra   r   r   )rx   rZ   r[   r\   rR   r   r"   r"   r#   rF     s    



z MatrixStudentTDistribution.checkc                 C   s   | j j\}}t||tjS r,   r   r   r"   r"   r#   r.     s    zMatrixStudentTDistribution.setc                 C   s   | j jS r,   rc   r'   r"   r"   r#   rG     s    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$   r0   )Zsympy.matrices.denser   r5   rD   r
   r   r   r   r   rx   rZ   r[   r\   ra   r   r   r   r   r   )
r(   r   r   rx   r   OmegaSigmarR   r   Kr"   r"   r#   r7     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r6t|}||||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

    )r5   rD   r
   rJ   r   )r-   rx   rZ   r[   r\   r&   r"   r"   r#   MatrixStudentT/  s    ,


r   N)2mathr   Zsympy.core.basicr   Zsympy.core.numbersr   Zsympy.core.singletonr   Z&sympy.functions.elementary.exponentialr   Z'sympy.functions.special.gamma_functionsr   Zsympy.core.sympifyr   r	   Zsympy.matricesr
   r   r   r   r   r   r   r   r   Zsympy.stats.rvr   r   r   r   r   r   r   Zsympy.externalr   r   rJ   rK   ro   rq   r   r   r   r   r^   r   r_   ru   r   r   r"   r"   r"   r#   <module>   s8   ,$,	&)0%2$/-52