
    ǄgF                     *   d Z ddlZddlmZ ddlmZmZ g dZ G d d      Z	d Z
 ed	      dd       ZdddddZ eed	      dd
d
dd       Zd ZddZ eed      dd       Zd Z ed      d        ZdddZ eed      d
dd       Zy)z
Utilities that manipulate strides to achieve desirable effects.

An explanation of strides can be found in the :ref:`arrays.ndarray`.

Functions
---------

.. autosummary::
   :toctree: generated/

    N)normalize_axis_tuple)array_function_dispatch
set_module)broadcast_tobroadcast_arraysbroadcast_shapesc                       e Zd ZdZddZy)
DummyArrayzDummy object that just exists to hang __array_interface__ dictionaries
    and possibly keep alive a reference to a base array.
    Nc                      || _         || _        y N)__array_interface__base)self	interfacer   s      e/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/numpy/lib/_stride_tricks_impl.py__init__zDummyArray.__init__   s    #, 	    r   )__name__
__module____qualname____doc__r    r   r   r
   r
      s    r   r
   c                     t        |       t        |      ur8|j                  t        |             }|j                  r|j                  |        |S )N)type)r   view__array_finalize__)original_array	new_arrays     r   _maybe_view_as_subclassr      sI    N4	?2 NN^(<N=	 ''((8r   znumpy.lib.stride_tricksFc                 l   t        j                  | d|      } t        | j                        }|t	        |      |d<   |t	        |      |d<   t        j
                  t        ||             }| j                  |_        t        | |      }|j                  j                  r|sd|j                  _
        |S )a  
    Create a view into the array with the given shape and strides.

    .. warning:: This function has to be used with extreme care, see notes.

    Parameters
    ----------
    x : ndarray
        Array to create a new.
    shape : sequence of int, optional
        The shape of the new array. Defaults to ``x.shape``.
    strides : sequence of int, optional
        The strides of the new array. Defaults to ``x.strides``.
    subok : bool, optional
        .. versionadded:: 1.10

        If True, subclasses are preserved.
    writeable : bool, optional
        .. versionadded:: 1.12

        If set to False, the returned array will always be readonly.
        Otherwise it will be writable if the original array was. It
        is advisable to set this to False if possible (see Notes).

    Returns
    -------
    view : ndarray

    See also
    --------
    broadcast_to : broadcast an array to a given shape.
    reshape : reshape an array.
    lib.stride_tricks.sliding_window_view :
        userfriendly and safe function for a creation of sliding window views.

    Notes
    -----
    ``as_strided`` creates a view into the array given the exact strides
    and shape. This means it manipulates the internal data structure of
    ndarray and, if done incorrectly, the array elements can point to
    invalid memory and can corrupt results or crash your program.
    It is advisable to always use the original ``x.strides`` when
    calculating new strides to avoid reliance on a contiguous memory
    layout.

    Furthermore, arrays created with this function often contain self
    overlapping memory, so that two elements are identical.
    Vectorized write operations on such arrays will typically be
    unpredictable. They may even give different results for small, large,
    or transposed arrays.

    Since writing to these arrays has to be tested and done with great
    care, you may want to use ``writeable=False`` to avoid accidental write
    operations.

    For these reasons it is advisable to avoid ``as_strided`` when
    possible.
    Ncopysubokshapestrides)r   F)nparraydictr   tupleasarrayr
   dtyper   flags	writeable)xr$   r%   r#   r-   r   r'   r   s           r   
as_stridedr/   +   s    z 	U+AQ**+I"5\	'$W~	)JJz)!45E ''EK"1e,DzzI$

Kr   )r#   r-   c                    | fS r   r   )r.   window_shapeaxisr#   r-   s        r   _sliding_window_view_dispatcherr3   |   s	    4Kr   )modulec                ^    t        j                  |      rt        |      n|f}t        j                   d|       t        j                  |      }t        j                  |dk        rt        d      |Zt        t         j                              }t        |      t        |      k7  rxt        dt        |       d j                   d      t        | j                  d	      }t        |      t        |      k7  r$t        d
t        |       dt        |       d       j                  t         fd|D              z   }t         j                        }t        ||      D ](  \  }}	||   |	k  rt        d      ||xx   |	dz
  z  cc<   * t        |      |z   }
t         ||
||      S )a  
    Create a sliding window view into the array with the given window shape.

    Also known as rolling or moving window, the window slides across all
    dimensions of the array and extracts subsets of the array at all window
    positions.
    
    .. versionadded:: 1.20.0

    Parameters
    ----------
    x : array_like
        Array to create the sliding window view from.
    window_shape : int or tuple of int
        Size of window over each axis that takes part in the sliding window.
        If `axis` is not present, must have same length as the number of input
        array dimensions. Single integers `i` are treated as if they were the
        tuple `(i,)`.
    axis : int or tuple of int, optional
        Axis or axes along which the sliding window is applied.
        By default, the sliding window is applied to all axes and
        `window_shape[i]` will refer to axis `i` of `x`.
        If `axis` is given as a `tuple of int`, `window_shape[i]` will refer to
        the axis `axis[i]` of `x`.
        Single integers `i` are treated as if they were the tuple `(i,)`.
    subok : bool, optional
        If True, sub-classes will be passed-through, otherwise the returned
        array will be forced to be a base-class array (default).
    writeable : bool, optional
        When true, allow writing to the returned view. The default is false,
        as this should be used with caution: the returned view contains the
        same memory location multiple times, so writing to one location will
        cause others to change.

    Returns
    -------
    view : ndarray
        Sliding window view of the array. The sliding window dimensions are
        inserted at the end, and the original dimensions are trimmed as
        required by the size of the sliding window.
        That is, ``view.shape = x_shape_trimmed + window_shape``, where
        ``x_shape_trimmed`` is ``x.shape`` with every entry reduced by one less
        than the corresponding window size.

    See Also
    --------
    lib.stride_tricks.as_strided: A lower-level and less safe routine for
        creating arbitrary views from custom shape and strides.
    broadcast_to: broadcast an array to a given shape.

    Notes
    -----
    For many applications using a sliding window view can be convenient, but
    potentially very slow. Often specialized solutions exist, for example:

    - `scipy.signal.fftconvolve`

    - filtering functions in `scipy.ndimage`

    - moving window functions provided by
      `bottleneck <https://github.com/pydata/bottleneck>`_.

    As a rough estimate, a sliding window approach with an input size of `N`
    and a window size of `W` will scale as `O(N*W)` where frequently a special
    algorithm can achieve `O(N)`. That means that the sliding window variant
    for a window size of 100 can be a 100 times slower than a more specialized
    version.

    Nevertheless, for small window sizes, when no custom algorithm exists, or
    as a prototyping and developing tool, this function can be a good solution.

    Examples
    --------
    >>> from numpy.lib.stride_tricks import sliding_window_view
    >>> x = np.arange(6)
    >>> x.shape
    (6,)
    >>> v = sliding_window_view(x, 3)
    >>> v.shape
    (4, 3)
    >>> v
    array([[0, 1, 2],
           [1, 2, 3],
           [2, 3, 4],
           [3, 4, 5]])

    This also works in more dimensions, e.g.

    >>> i, j = np.ogrid[:3, :4]
    >>> x = 10*i + j
    >>> x.shape
    (3, 4)
    >>> x
    array([[ 0,  1,  2,  3],
           [10, 11, 12, 13],
           [20, 21, 22, 23]])
    >>> shape = (2,2)
    >>> v = sliding_window_view(x, shape)
    >>> v.shape
    (2, 3, 2, 2)
    >>> v
    array([[[[ 0,  1],
             [10, 11]],
            [[ 1,  2],
             [11, 12]],
            [[ 2,  3],
             [12, 13]]],
           [[[10, 11],
             [20, 21]],
            [[11, 12],
             [21, 22]],
            [[12, 13],
             [22, 23]]]])

    The axis can be specified explicitly:

    >>> v = sliding_window_view(x, 3, 0)
    >>> v.shape
    (1, 4, 3)
    >>> v
    array([[[ 0, 10, 20],
            [ 1, 11, 21],
            [ 2, 12, 22],
            [ 3, 13, 23]]])

    The same axis can be used several times. In that case, every use reduces
    the corresponding original dimension:

    >>> v = sliding_window_view(x, (2, 3), (1, 1))
    >>> v.shape
    (3, 1, 2, 3)
    >>> v
    array([[[[ 0,  1,  2],
             [ 1,  2,  3]]],
           [[[10, 11, 12],
             [11, 12, 13]]],
           [[[20, 21, 22],
             [21, 22, 23]]]])

    Combining with stepped slicing (`::step`), this can be used to take sliding
    views which skip elements:

    >>> x = np.arange(7)
    >>> sliding_window_view(x, 5)[:, ::2]
    array([[0, 2, 4],
           [1, 3, 5],
           [2, 4, 6]])

    or views which move by multiple elements

    >>> x = np.arange(7)
    >>> sliding_window_view(x, 3)[::2, :]
    array([[0, 1, 2],
           [2, 3, 4],
           [4, 5, 6]])

    A common application of `sliding_window_view` is the calculation of running
    statistics. The simplest example is the
    `moving average <https://en.wikipedia.org/wiki/Moving_average>`_:

    >>> x = np.arange(6)
    >>> x.shape
    (6,)
    >>> v = sliding_window_view(x, 3)
    >>> v.shape
    (4, 3)
    >>> v
    array([[0, 1, 2],
           [1, 2, 3],
           [2, 3, 4],
           [3, 4, 5]])
    >>> moving_average = v.mean(axis=-1)
    >>> moving_average
    array([1., 2., 3., 4.])

    Note that a sliding window approach is often **not** optimal (see Notes).
    Nr!   r   z-`window_shape` cannot contain negative valueszOSince axis is `None`, must provide window_shape for all dimensions of `x`; got z' window_shape elements and `x.ndim` is .T)allow_duplicatez8Must provide matching length window_shape and axis; got z window_shape elements and z axes elements.c              3   <   K   | ]  }j                   |     y wr   )r%   ).0axr.   s     r   	<genexpr>z&sliding_window_view.<locals>.<genexpr>O  s     #AbAIIbM#A   z4window shape cannot be larger than input array shape   )r%   r$   r#   r-   )r&   iterabler)   r'   any
ValueErrorrangendimlenr   r%   listr$   zipr/   )r.   r1   r2   r#   r-   window_shape_arrayout_stridesx_shape_trimmedr:   dim	out_shapes   `          r   sliding_window_viewrK      s   n {{<0 ,'&  	U+A,/	vv 1$%HII|U166]#|D	)  $$'$5#6 7001xq: ; ;
 $D!&&$G|D	)  **-l*;)< =--0YKH I I ))e#AD#AAAK 177mOt\* 'C2$FH HsQw&	'
 o&5IaI!Y8 8r   c                    t        j                  |      rt        |      n|f}t        j                  | d |      } |s| j                  rt        d      t        d |D              rt        d      g }t        j                  | fg d|z   dg|d      }|5  |j                  d	   }d d d        t        |       }|s8| j                  j                  r"d
|j                  _        d
|j                  _        |S # 1 sw Y   QxY w)Nr!   z/cannot broadcast a non-scalar to a scalar arrayc              3   &   K   | ]	  }|d k    yw)r   Nr   )r9   sizes     r   r;   z _broadcast_to.<locals>.<genexpr>b  s     
&4!8
&s   z4all elements of broadcast shape must be non-negative)multi_indexrefs_okzerosize_okreadonlyC)r,   op_flags	itershapeorderr   T)r&   r>   r)   r'   r$   r@   r?   nditeritviewsr   r,   _writeable_no_warnr-   _warn_on_write)r'   r$   r#   rR   extrasit	broadcastresults           r   _broadcast_tor_   ]  s    KK.E%LUHEHHUU3EU[[JKK

&
&& $ % 	%F		AFJc
;B 
 "JJqM	" %UI6F66!%&*#M" "s   C77D c                     | fS r   r   r'   r$   r#   s      r   _broadcast_to_dispatcherrb   t  s	    8Or   numpyc                      t        | ||d      S )aa  Broadcast an array to a new shape.

    Parameters
    ----------
    array : array_like
        The array to broadcast.
    shape : tuple or int
        The shape of the desired array. A single integer ``i`` is interpreted
        as ``(i,)``.
    subok : bool, optional
        If True, then sub-classes will be passed-through, otherwise
        the returned array will be forced to be a base-class array (default).

    Returns
    -------
    broadcast : array
        A readonly view on the original array with the given shape. It is
        typically not contiguous. Furthermore, more than one element of a
        broadcasted array may refer to a single memory location.

    Raises
    ------
    ValueError
        If the array is not compatible with the new shape according to NumPy's
        broadcasting rules.

    See Also
    --------
    broadcast
    broadcast_arrays
    broadcast_shapes

    Notes
    -----
    .. versionadded:: 1.10.0

    Examples
    --------
    >>> x = np.array([1, 2, 3])
    >>> np.broadcast_to(x, (3, 3))
    array([[1, 2, 3],
           [1, 2, 3],
           [1, 2, 3]])
    Tr#   rR   r_   ra   s      r   r   r   x  s    \ UTBBr   c                      t        j                  | dd  }t        dt        |       d      D ]4  }t	        d|j
                        }t        j                  |g| ||dz     }6 |j
                  S )ztReturns the shape of the arrays that would result from broadcasting the
    supplied arrays against each other.
    N       r   )r&   r]   rA   rC   r   r$   )argsbposs      r   _broadcast_shaperm     so     	d3Bi ARTB' 3 AGG$LL2T#sRx123 77Nr   c                  d    | D cg c]  }t        j                  |g        }}t        | S c c}w )a  
    Broadcast the input shapes into a single shape.

    :ref:`Learn more about broadcasting here <basics.broadcasting>`.

    .. versionadded:: 1.20.0

    Parameters
    ----------
    *args : tuples of ints, or ints
        The shapes to be broadcast against each other.

    Returns
    -------
    tuple
        Broadcasted shape.

    Raises
    ------
    ValueError
        If the shapes are not compatible and cannot be broadcast according
        to NumPy's broadcasting rules.

    See Also
    --------
    broadcast
    broadcast_arrays
    broadcast_to

    Examples
    --------
    >>> np.broadcast_shapes((1, 2), (3, 1), (3, 2))
    (3, 2)

    >>> np.broadcast_shapes((6, 7), (5, 6, 1), (7,), (5, 1, 7))
    (5, 6, 7)
    )r+   )r&   emptyrm   )rj   r.   arrayss      r   r   r     s3    N .22bhhq#2F2V$$ 3s   -)r#   c                     |S r   r   )r#   rj   s     r   _broadcast_arrays_dispatcherrr     s    Kr   c                      t         fd|D              }t        | t        fd|D              r|S t         fd|D              S )aJ  
    Broadcast any number of arrays against each other.

    Parameters
    ----------
    *args : array_likes
        The arrays to broadcast.

    subok : bool, optional
        If True, then sub-classes will be passed-through, otherwise
        the returned arrays will be forced to be a base-class array (default).

    Returns
    -------
    broadcasted : tuple of arrays
        These arrays are views on the original arrays.  They are typically
        not contiguous.  Furthermore, more than one element of a
        broadcasted array may refer to a single memory location. If you need
        to write to the arrays, make copies first. While you can set the
        ``writable`` flag True, writing to a single output value may end up
        changing more than one location in the output array.

        .. deprecated:: 1.17
            The output is currently marked so that if written to, a deprecation
            warning will be emitted. A future version will set the
            ``writable`` flag False so writing to it will raise an error.

    See Also
    --------
    broadcast
    broadcast_to
    broadcast_shapes

    Examples
    --------
    >>> x = np.array([[1,2,3]])
    >>> y = np.array([[4],[5]])
    >>> np.broadcast_arrays(x, y)
    (array([[1, 2, 3],
            [1, 2, 3]]),
     array([[4, 4, 4],
            [5, 5, 5]]))

    Here is a useful idiom for getting contiguous copies instead of
    non-contiguous views.

    >>> [np.array(a) for a in np.broadcast_arrays(x, y)]
    [array([[1, 2, 3],
            [1, 2, 3]]),
     array([[4, 4, 4],
            [5, 5, 5]])]

    c              3   N   K   | ]  }t        j                  |d         y w)Nr!   )r&   r'   )r9   _mr#   s     r   r;   z#broadcast_arrays.<locals>.<genexpr>%  s!     E""4u55Es   "%c              3   <   K   | ]  }|j                   k(    y wr   )r$   )r9   r'   r$   s     r   r;   z#broadcast_arrays.<locals>.<genexpr>)  s     
2E5;;%
2r<   c              3   <   K   | ]  }t        |d         yw)Fre   Nrf   )r9   r'   r$   r#   s     r   r;   z#broadcast_arrays.<locals>.<genexpr>-  s'      $ ue55II $r<   )r)   rm   all)r#   rj   r$   s   ` @r   r   r     sM    x EEEDd#E

2T
22 $"$ $ $r   )NNFTr   )F)r   rc   r&   numpy._core.numericr   numpy._core.overridesr   r   __all__r
   r   r/   r3   rK   r_   rb   r   rm   r   rr   r   r   r   r   <module>r|      s     4 E
B 
 %&M 'M`*.$
 #,EV8#uV8V8r. 1'B-C C-C`" G'% '%T /3  5gF"' D$ GD$r   