
    3j                    l    S SK Jr  S SKrS SKJr  S SKJrJr  SS	S jjr " S S\R                  5      r	g)
    )annotationsN)nn)KORNIA_CHECKKORNIA_CHECK_SHAPEc                   [        U [        R                  5      (       d  [        S[	        U 5       35      e[        U / SQ5        [        US;   SU S35        U SSS2SS24   U SSS	2SS24   -
  nU SSS2SS24   U SSS2SS	24   -
  nUR                  5       nUR                  5       nS
nUS:X  a  U R                  5       (       a@  UR                  U 5      R                  US9nUR                  U 5      R                  US9nXE-   $ UR                  5       R                  US9nUR                  5       R                  US9n XE-   $ US:X  a"  UR                  US9nUR                  US9nXE-   $ [        S5      e)as  Compute Total Variation according to [1].

Args:
    img: the input image with shape :math:`(*, H, W)`.
    reduction : Specifies the reduction to apply to the output: ``'mean'`` | ``'sum'``.
     ``'mean'``: the sum of the output will be divided by the number of elements
     in the output, ``'sum'``: the output will be summed.

Return:
     a torch.Tensor with shape :math:`(*,)`.

Examples:
    >>> total_variation(torch.ones(4, 4))
    tensor(0.)
    >>> total_variation(torch.ones(2, 5, 3, 4, 4)).shape
    torch.Size([2, 5, 3])

.. note::
   See a working example `here <https://kornia.github.io/tutorials/nbs/total_variation_denoising.html>`__.
   Total Variation is formulated with summation, however this is not resolution invariant.
   Thus, `reduction='mean'` was added as an optional reduction method.

Reference:
    [1] https://en.wikipedia.org/wiki/Total_variation

zNot a torch.Tensor type. Got: )*HW)meansumz7Expected reduction to be one of 'mean'/'sum', but got 'z'..   N)r   r   )dimr   zInvalid reduction option.)
isinstancetorchTensor	TypeErrortyper   r   absis_floating_pointtor   floatr   NotImplementedError)img	reduction
pixel_dif1
pixel_dif2res1res2reduce_axess          W/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/losses/total_variation.pytotal_variationr#      s   8 c5<<((8cDEEsO,o-1hirhssu/vwS!"aZ3sCRC{#33JS!QRZ3sAss{#33J>>D>>DKF  ""773<$$$5D773<$$$5D ; ::<$$$5D::<$$$5D ; 
e	xxKx(xxKx( ; ""=>>    c                  "    \ rS rSrSrSS jrSrg)TotalVariationS   a  Compute the Total Variation according to [1].

Shape:
    - Input: :math:`(*, H, W)`.
    - Output: :math:`(*,)`.

Examples:
    >>> tv = TotalVariation()
    >>> output = tv(torch.ones((2, 3, 4, 4), requires_grad=True))
    >>> output.data
    tensor([[0., 0., 0.],
            [0., 0., 0.]])
    >>> output.sum().backward()  # grad can be implicitly created only for scalar outputs

Reference:
    [1] https://en.wikipedia.org/wiki/Total_variation

c                    [        U5      $ )N)r#   )selfr   s     r"   forwardTotalVariation.forwardg   s    s##r$    N)r   torch.Tensorreturnr-   )__name__
__module____qualname____firstlineno____doc__r*   __static_attributes__r,   r$   r"   r&   r&   S   s    &$r$   r&   )r   )r   r-   r   strr.   r-   )

__future__r   r   r   kornia.core.checkr   r   r#   Moduler&   r,   r$   r"   <module>r9      s)   $ #   >6r$RYY $r$   