bgEHdZddlZddlmZddlmZmZgdZGddZ dZ dd Z dddd d Z ee dd d d dZ dZddZeedddZdZeddZdddZeedd ddZdS)z Utilities that manipulate strides to achieve desirable effects. An explanation of strides can be found in the "ndarray.rst" file in the NumPy reference guide. N)normalize_axis_tuple)array_function_dispatch set_module) broadcast_tobroadcast_arraysbroadcast_shapesceZdZdZddZdS) DummyArrayzDummy object that just exists to hang __array_interface__ dictionaries and possibly keep alive a reference to a base array. Nc"||_||_dSN)__array_interface__base)self interfacers N/opt/cloudlinux/venv/lib64/python3.11/site-packages/numpy/lib/stride_tricks.py__init__zDummyArray.__init__s#,  r )__name__ __module__ __qualname____doc__rrrr r s2rr ct|t|ur?|t|}|jr|||S)N)type)rview__array_finalize__)original_array new_arrays r_maybe_view_as_subclassrs_ N4 ??22NN^(<(`_. 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 -------- >>> 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 `_: >>> 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). Fr!rz-`window_shape` cannot contain negative valuesNzOSince 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.c32K|]}j|VdSr )r%).0axr.s r z&sliding_window_view..Fs)#A#AbAIbM#A#A#A#A#A#Arz4window shape cannot be larger than input array shape)r%r$r#r-)r&iterabler)r'any ValueErrorrangendimlenrr%listr$zipr/) r.r1r2r#r-window_shape_array out_stridesx_shape_trimmedr9dim out_shapes ` rsliding_window_viewrI{sMh{<00)E,'''& e,,,A,// v 1$%%JHIII |U16]]## |  D ) ):$' $5$5::12:::;; ; * $D!&$GGG |  D ) )H*-l*;*;HH-0YYHHHII I)e#A#A#A#AD#A#A#AAAAK17mmOt\**''C 2  $ $FHH HsQw&o&&5I aI!Y 8 8 88rctj|rt|n|f}tj|d|}|s|jrt dt d|Drt dg}tj|fgd|zdg|d }|5|jd }dddn #1swxYwYt||}|s$|j j rd |j _ d |j _ |S) NFr!z/cannot broadcast a non-scalar to a scalar arrayc3"K|] }|dkV dS)rNr)r8sizes rr:z _broadcast_to..Ys& & &4!8 & & & & & &rz4all elements of broadcast shape must be non-negative) multi_indexrefs_ok zerosize_okreadonlyC)r,op_flags itershapeorderrT)r&r<r)r'r$r>r=nditeritviewsrr,_writeable_no_warnr-_warn_on_write)r'r$r#rPextrasit broadcastresults r _broadcast_tor]TsmK.. >> x = np.array([1, 2, 3]) >>> np.broadcast_to(x, (3, 3)) array([[1, 2, 3], [1, 2, 3], [1, 2, 3]]) Tr#rPr]r_s rrros\ UT B B BBrctj|dd}tdt|dD]4}t d|j}tj|g|||dzR}5|jS)ztReturns the shape of the arrays that would result from broadcasting the supplied arrays against each other. N r)r&r[r?rArr$)argsbposs r_broadcast_shaperlsy d3B3i ARTB''33 AG $ $ L 2T#sRx.1 2 2 2 7Nrc,d|D}t|S)a Broadcast the input shapes into a single shape. :ref:`Learn more about broadcasting here `. .. 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) c:g|]}tj|gS))r+)r&empty)r8r.s r z$broadcast_shapes..s' 2 2 2bhq### 2 2 2r)rl)riarrayss rrrs#N3 2T 2 2 2F V $$r)r#c|Sr r)r#ris r_broadcast_arrays_dispatcherrss Krcfd|D}t|tfd|Dr|Sfd|DS)a= 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 : list 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>g|]}tj|dS)Fr!)r&r')r8_mr#s rrpz$broadcast_arrays..s* A A AbBHRe5 1 1 1 A A Arc3.K|]}|jkVdSr )r$)r8r'r$s rr:z#broadcast_arrays..s* 2 2E5;%  2 2 2 2 2 2rc6g|]}t|dS)Frdre)r8r'r$r#s rrpz$broadcast_arrays.."s:    %ee D D D   r)rlall)r#rir$s` @rrrst B A A AD A A AD d #E 2 2 2 2T 2 2 222         r)NNFTr )F)rrar&numpy.core.numericrnumpy.core.overridesrr__all__r rr/r3rIr]r`rrlrrsrrrrr}s333333DDDDDDDD B B B   MMMM`*.$ 899U8#uU8U8U8U8:9U8p.1'BBB-C-C-CCB-C`" G'%'%'%T/35gFFF"'BBBBGFBBBr