o
    ¦ƒðjŽ&  ã                   @   sî   d Z ddlmZ ddlmZ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mZmZ dd	lmZ dd
lmZ ddlmZ edd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZeddd„ƒZ dS )z¥
This module implements sums and products containing the Kronecker Delta function.

References
==========

.. [1] https://mathworld.wolfram.com/KroneckerDelta.html

é   )Úproduct)ÚSumÚ	summationé    )ÚAddÚMulÚSÚDummy)Úcacheit)Údefault_sort_key)ÚKroneckerDeltaÚ	PiecewiseÚpiecewise_fold)Úfactor)ÚInterval)Úsolvec                    sx   | j s| S d}t}tjg‰| jD ]'‰ |du r.ˆ jr.tˆ |ƒr.d}ˆ j}‡fdd„ˆ jD ƒ‰q‡ fdd„ˆD ƒ‰q|ˆŽ S )zB
    Expand the first Add containing a simple KroneckerDelta.
    NTc                    s   g | ]}ˆ d  | ‘qS )r   © ©Ú.0Út)Útermsr   ú–/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/concrete/delta.pyÚ
<listcomp>#   ó    z!_expand_delta.<locals>.<listcomp>c                    s   g | ]}|ˆ  ‘qS r   r   r   )Úhr   r   r   %   s    )Úis_Mulr   r   ÚOneÚargsÚis_AddÚ_has_simple_deltaÚfunc)ÚexprÚindexÚdeltar    r   )r   r   r   Ú_expand_delta   s   
r$   c                 C   sx   t | |ƒs	d| fS t| tƒr| tjfS | jstdƒ‚d}g }| jD ]}|du r/t||ƒr/|}q!| 	|¡ q!|| j
|Ž fS )a”  
    Extract a simple KroneckerDelta from the expression.

    Explanation
    ===========

    Returns the tuple ``(delta, newexpr)`` where:

      - ``delta`` is a simple KroneckerDelta expression if one was found,
        or ``None`` if no simple KroneckerDelta expression was found.

      - ``newexpr`` is a Mul containing the remaining terms; ``expr`` is
        returned unchanged if no simple KroneckerDelta expression was found.

    Examples
    ========

    >>> from sympy import KroneckerDelta
    >>> from sympy.concrete.delta import _extract_delta
    >>> from sympy.abc import x, y, i, j, k
    >>> _extract_delta(4*x*y*KroneckerDelta(i, j), i)
    (KroneckerDelta(i, j), 4*x*y)
    >>> _extract_delta(4*x*y*KroneckerDelta(i, j), k)
    (None, 4*x*y*KroneckerDelta(i, j))

    See Also
    ========

    sympy.functions.special.tensor_functions.KroneckerDelta
    deltaproduct
    deltasummation
    NzIncorrect expr)r   Ú
isinstancer   r   r   r   Ú
ValueErrorr   Ú_is_simple_deltaÚappendr    )r!   r"   r#   r   Úargr   r   r   Ú_extract_delta)   s   
"


r*   c                    s@   |   t¡rt| ˆ ƒrdS | js| jrt‡ fdd„| jD ƒƒS dS )zØ
    Returns True if ``expr`` is an expression that contains a KroneckerDelta
    that is simple in the index ``index``, meaning that this KroneckerDelta
    is nonzero for a single value of the index ``index``.
    Tc                 3   s   � | ]}t |ˆ ƒV  qd S )N)r   )r   r)   ©r"   r   r   Ú	<genexpr>g   s   € z$_has_simple_delta.<locals>.<genexpr>F)Úhasr   r'   r   r   Úanyr   )r!   r"   r   r+   r   r   \   s   

r   c                 C   sB   t | tƒr|  |¡r| jd | jd   |¡}|r| ¡ dkS dS )zu
    Returns True if ``delta`` is a KroneckerDelta and is nonzero for a single
    value of the index ``index``.
    r   r   F)r%   r   r-   r   Úas_polyÚdegree)r#   r"   Úpr   r   r   r'   k   s
   r'   c                 C   sÔ   | j r| jttt| jƒƒŽ S | js| S g }g }| jD ]}t|tƒr/| 	|jd |jd  ¡ q| 	|¡ q|s9| S t
|dd�}t|ƒdkrHtjS t|ƒdkrh|dd„ |d  ¡ D ƒ7 }| j|Ž }| |krht|ƒS | S )z0
    Evaluate products of KroneckerDelta's.
    r   r   T©Údictc                 S   s   g | ]	\}}t ||ƒ‘qS r   ©r   )r   ÚkÚvr   r   r   r   Ž   s    z*_remove_multiple_delta.<locals>.<listcomp>)r   r    ÚlistÚmapÚ_remove_multiple_deltar   r   r%   r   r(   r   Úlenr   ÚZeroÚitems)r!   ÚeqsÚnewargsr)   ÚsolnsÚexpr2r   r   r   r9   x   s*   


r9   c                 C   sx   t | tƒr:z*t| jd | jd  dd�}|r*t|ƒdkr-tdd„ |d  ¡ D ƒŽ W S W | S W | S  ty9   Y | S w | S )zB
    Rewrite a KroneckerDelta's indices in its simplest form.
    r   r   Tr2   c                 S   s   g | ]
\}}t ||fŽ ‘qS r   r4   )r   ÚkeyÚvaluer   r   r   r   ž   s    ÿz#_simplify_delta.<locals>.<listcomp>)r%   r   r   r   r:   r   r<   ÚNotImplementedError)r!   Úslnsr   r   r   Ú_simplify_delta•   s    


ÿÿûþþrE   c              	      s  ˆd ˆd  dk dkrt jS |  t¡st| ˆƒS | jr·d‰ g }t| jtd�D ]}ˆ du r7t	|ˆd ƒr7|‰ q'| 
|¡ q'| j|Ž ‰tddd�}tˆd tƒrytˆd tƒrytˆˆƒt‡ ‡‡fd	d
„ttˆd ƒtˆd d ƒƒD ƒƒ }t|ƒS tˆˆƒttˆˆd ˆd |d fƒˆ  ˆd |¡ tˆˆd |d ˆd fƒ |ˆd ˆd ft	ˆˆd ƒd� }t|ƒS t| ˆd ƒ\‰ }ˆ sét| ˆd ƒ}| |kräztt|ˆƒƒW S  tyã   t|ˆƒ Y S w t| ˆƒS t|  ˆd ˆd ¡tˆd ˆd ƒ ƒt jttˆd ˆd d ƒƒ  S )zÅ
    Handle products containing a KroneckerDelta.

    See Also
    ========

    deltasummation
    sympy.functions.special.tensor_functions.KroneckerDelta
    sympy.concrete.products.product
    é   r   r   TN)rA   Úkprime)Úintegerc                 3   sX   � | ]'}t ˆˆd  ˆd |d fƒˆ  ˆd  |¡ t ˆˆd  |d ˆd fƒ V  qdS )r   r   rF   N)ÚdeltaproductÚsubs)r   Úik©r#   ÚlimitÚnewexprr   r   r,   Ã   s   € þÿþzdeltaproduct.<locals>.<genexpr>)Úno_piecewise)r   r   r-   r   r   r   Úsortedr   r   r   r(   r    r	   r%   ÚintrI   ÚsumÚrangeÚdeltasummationrJ   r9   r*   r$   r   ÚAssertionErrorrE   )ÚfrM   r   r)   r5   ÚresultÚ_Úgr   rL   r   rI   ¥   sR   



þ
ùÿþûÿ
(ÿrI   Fc                    sV  ˆ d ˆ d  dk dkrt jS |  t¡st| ˆ ƒS ˆ d }t| |ƒ}|jr5t|j‡ ‡fdd„|j	D ƒŽ ƒS t
||ƒ\}}|dur`|jdur`|j\}}ˆ d | dkdkr`ˆ d | dkdkr`d‰|sgt| ˆ ƒS t|j	d |j	d  |ƒ}	t|	ƒdkr}t jS t|	ƒdkrˆt| ˆ ƒS |	d }
ˆr”| ||
¡S t| ||
¡tˆ dd… Ž  |
¡ft jdfƒS )	aw  
    Handle summations containing a KroneckerDelta.

    Explanation
    ===========

    The idea for summation is the following:

    - If we are dealing with a KroneckerDelta expression, i.e. KroneckerDelta(g(x), j),
      we try to simplify it.

      If we could simplify it, then we sum the resulting expression.
      We already know we can sum a simplified expression, because only
      simple KroneckerDelta expressions are involved.

      If we could not simplify it, there are two cases:

      1) The expression is a simple expression: we return the summation,
         taking care if we are dealing with a Derivative or with a proper
         KroneckerDelta.

      2) The expression is not simple (i.e. KroneckerDelta(cos(x))): we can do
         nothing at all.

    - If the expr is a multiplication expr having a KroneckerDelta term:

      First we expand it.

      If the expansion did work, then we try to sum the expansion.

      If not, we try to extract a simple KroneckerDelta term, then we have two
      cases:

      1) We have a simple KroneckerDelta term, so we return the summation.

      2) We did not have a simple term, but we do have an expression with
         simplified KroneckerDelta terms, so we sum this expression.

    Examples
    ========

    >>> from sympy import oo, symbols
    >>> from sympy.abc import k
    >>> i, j = symbols('i, j', integer=True, finite=True)
    >>> from sympy.concrete.delta import deltasummation
    >>> from sympy import KroneckerDelta
    >>> deltasummation(KroneckerDelta(i, k), (k, -oo, oo))
    1
    >>> deltasummation(KroneckerDelta(i, k), (k, 0, oo))
    Piecewise((1, i >= 0), (0, True))
    >>> deltasummation(KroneckerDelta(i, k), (k, 1, 3))
    Piecewise((1, (i >= 1) & (i <= 3)), (0, True))
    >>> deltasummation(k*KroneckerDelta(i, j)*KroneckerDelta(j, k), (k, -oo, oo))
    j*KroneckerDelta(i, j)
    >>> deltasummation(j*KroneckerDelta(i, j), (j, -oo, oo))
    i
    >>> deltasummation(i*KroneckerDelta(i, j), (i, -oo, oo))
    j

    See Also
    ========

    deltaproduct
    sympy.functions.special.tensor_functions.KroneckerDelta
    sympy.concrete.sums.summation
    rF   r   r   Tc                    s   g | ]}t |ˆ ˆƒ‘qS r   )rT   )r   r   ©rM   rO   r   r   r   /  r   z"deltasummation.<locals>.<listcomp>Né   )r   r;   r-   r   r   r$   r   r   r    r   r*   Údelta_ranger   r:   r   rJ   r   r   Úas_relational)rV   rM   rO   ÚxrY   r#   r!   ÚdinfÚdsupr?   rB   r   rZ   r   rT   à   s:   D


ÿ
(

 þrT   N)F)!Ú__doc__Úproductsr   Ú
summationsr   r   Ú
sympy.corer   r   r   r	   Úsympy.core.cacher
   Úsympy.core.sortingr   Úsympy.functionsr   r   r   Úsympy.polys.polytoolsr   Úsympy.sets.setsr   Úsympy.solvers.solversr   r$   r*   r   r'   r9   rE   rI   rT   r   r   r   r   Ú<module>   s4    	

2




: