U C^K$@sdZddlmZmZmZddlZddlmZddgZ Gddde Z d d Z dd dZ ddZdddZeedddddZddZddZeeddddZdS)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. )divisionabsolute_importprint_functionN)array_function_dispatch broadcast_tobroadcast_arraysc@seZdZdZdddZdS) DummyArrayzDummy object that just exists to hang __array_interface__ dictionaries and possibly keep alive a reference to a base array. NcCs||_||_dSN)__array_interface__base)self interfacer r:/tmp/pip-install-6_kvzl1k/numpy/numpy/lib/stride_tricks.py__init__szDummyArray.__init__)N)__name__ __module__ __qualname____doc__rrrrrrsrcCs4t|t|k r0|jt|d}|jr0|||S)N)type)rviewZ__array_finalize__)Zoriginal_arrayZ new_arrayrrr_maybe_view_as_subclasss  rFTcCs~tj|d|d}t|j}|dk r.t||d<|dk rBt||d<tt||d}|j|_t||}|j j rz|szd|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. 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. FcopysubokNshapestrides)r ) nparraydictr tupleZasarrayrZdtyperflags writeable)xrrrr"r rrrrr as_strided's9     r$c Cst|rt|n|f}tj|d|d}|s:|jr:tdtdd|DrTtdg}tj|fddd g|d g|d d }||jd }W5QRXt ||}|s|j j rd|j _ d|j _ |S)NFrz/cannot broadcast a non-scalar to a scalar arraycss|]}|dkVqdS)rNr).0sizerrr ysz _broadcast_to..z4all elements of broadcast shape must be non-negativeZ multi_indexZrefs_okZ zerosize_okreadonlyC)r!Zop_flagsZ itershapeorderrT)riterabler rr ValueErroranyZnditerZitviewsrr!Z_writeable_no_warnr"Z_warn_on_write)rrrr(extrasit broadcastresultrrr _broadcast_tots*    r2cCs|fSr rrrrrrr_broadcast_to_dispatchersr4numpy)modulecCst|||ddS)aBroadcast an array to a new shape. Parameters ---------- array : array_like The array to broadcast. shape : tuple The shape of the desired array. 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. 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]]) Trr(r2r3rrrrs'cGsVtj|dd}tdt|dD],}td|j}tj|f|||d}q"|jS)ztReturns the shape of the arrays that would result from broadcasting the supplied arrays against each other. N r)rr0rangelenrr)argsbposrrr_broadcast_shapes  r@cOs|Sr rr=kwargsrrr_broadcast_arrays_dispatchersrCcsr|dd|r*tdt|dfdd|D}t|tfdd|Dr^|Sfd d|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. 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]])] rFz:broadcast_arrays() got an unexpected keyword argument {!r}rcsg|]}tj|ddqS)Fr)rr)r%_m)rrr sz$broadcast_arrays..c3s|]}|jkVqdSr rr%rrFrrr' sz#broadcast_arrays..csg|]}t|ddqS)Fr7r8rGrrrrrEs)pop TypeErrorformatlistkeysr@allrArrHrrs4  )NNFT)N)F)r __future__rrrr5rZnumpy.core.overridesr__all__objectrrr$r2r4rr@rCrrrrrs   M   )