U
    9%e                     @   s   d 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mZmZ ddlmZ dd ZdddZdS )z'Tools for arithmetic error propagation.    )repeatcombinations)Add)Mul)Pow)S)Symbol)exp)simplify)RandomSymbolVariance
Covariance)	is_randomc                 C   s   t | jdkr| jd S | S )Nr   )lenargs)var r   U/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/stats/error_prop.py<lambda>       r   r   Fc           
      C   s  | j }t|dkrT| |kr tjS t| r4t|  S t| trNtt	|  S tjS t|}t
tt|t||t||}t| trt| }|rdd t|dD }|t| 7 }nt| trdd t||D }t| d t|  }|r~dd tt|dt|dD }|t| 7 }ndt| trT|d }|d | | |d  d  }	t|	}n*t| trvt|d | d  }nt| }|S )a  Symbolically propagates variance (`\sigma^2`) for expressions.
    This is computed as as seen in [1]_.

    Parameters
    ==========

    expr : Expr
        A SymPy expression to compute the variance for.
    consts : sequence of Symbols, optional
        Represents symbols that are known constants in the expr,
        and thus have zero variance. All symbols not in consts are
        assumed to be variant.
    include_covar : bool, optional
        Flag for whether or not to include covariances, default=False.

    Returns
    =======

    var_expr : Expr
        An expression for the total variance of the expr.
        The variance for the original symbols (e.g. x) are represented
        via instance of the Variance symbol (e.g. Variance(x)).

    Examples
    ========

    >>> from sympy import symbols, exp
    >>> from sympy.stats.error_prop import variance_prop
    >>> x, y = symbols('x y')

    >>> variance_prop(x + y)
    Variance(x) + Variance(y)

    >>> variance_prop(x * y)
    x**2*Variance(y) + y**2*Variance(x)

    >>> variance_prop(exp(2*x))
    4*exp(4*x)*Variance(x)

    References
    ==========

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

    r   c                 S   s*   g | ]"\}}d t t|t|  qS    r   _arg0_or_varexpand).0xyr   r   r   
<listcomp>P   s   z!variance_prop.<locals>.<listcomp>r   c                 S   s   g | ]\}}||d   qS r   r   )r   avr   r   r   r   T   s     c                 S   s:   g | ]2\\}}\}}d t t|t|  ||  qS r   r   )r   r   br   r   r   r   r   r   W   s      )r   r   r   ZZeror   r   Zdoit
isinstancer   r   listmapvariance_propr   r   r   r   zipr
   r   r	   )
exprZconstsZinclude_covarr   nargsZvar_argsZvar_exprZtermsr!   r    r   r   r   r&      sL    .



r&   N)r   F)__doc__	itertoolsr   r   Zsympy.core.addr   Zsympy.core.mulr   Zsympy.core.powerr   Zsympy.core.singletonr   Zsympy.core.symbolr   Z&sympy.functions.elementary.exponentialr	   Zsympy.simplify.simplifyr
   Z sympy.stats.symbolic_probabilityr   r   r   Zsympy.stats.rvr   r   r&   r   r   r   r   <module>   s   