U ã€C^K$ã@s¦dZddlmZmZmZddlZddlmZddgZ Gdd„de ƒZ d d „Z dd d„Z dd„Zddd„Zeedd�ddd„ƒZdd„Zdd„Zeedd�dd„ƒZdS)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. é)ÚdivisionÚabsolute_importÚprint_functionN)Úarray_function_dispatchÚ broadcast_toÚbroadcast_arraysc@seZdZdZddd„ZdS)Ú DummyArrayz„Dummy object that just exists to hang __array_interface__ dictionaries and possibly keep alive a reference to a base array. NcCs||_||_dS©N)Ú__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)rÚviewZ__array_finalize__)Zoriginal_arrayZ new_arrayrrrÚ_maybe_view_as_subclasss  rFTcCs~tj|d|d�}t|jƒ}|dk r.t|ƒ|d<|dk rBt|ƒ|d<t t||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. F©ÚcopyÚsubokNÚshapeÚstrides)r ) ÚnpÚarrayÚdictr ÚtupleZasarrayrZdtyperÚflagsÚ writeable)Úxrrrr"r rrrrrÚ as_strided's9     r$c Cs¾t |¡rt|ƒn|f}tj|d|d�}|s:|jr:tdƒ‚tdd„|DƒƒrTtdƒ‚g}tj|fddd g|d g|d d �}|�|jd }W5QRXt ||ƒ}|sº|j j rºd|j _ d|j _ |S)NFrz/cannot broadcast a non-scalar to a scalar arraycss|]}|dkVqdS)rNr)Ú.0ÚsizerrrÚ ysz _broadcast_to..z4all elements of broadcast shape must be non-negativeZ multi_indexZrefs_okZ zerosize_okÚreadonlyÚC)r!Zop_flagsZ itershapeÚorderrT)rÚiterabler rrÚ ValueErrorÚanyZnditerZitviewsrr!Z_writeable_no_warnr"Z_warn_on_write)rrrr(ÚextrasÚitÚ broadcastÚresultrrrÚ _broadcast_tots*  þ  r2cCs|fSr r©rrrrrrÚ_broadcast_to_dispatcher‹sr4Únumpy)ÚmodulecCst|||dd�S)aÌBroadcast 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]]) T©rr(©r2r3rrrr�s'cGsVtj|dd…Ž}tdt|ƒdƒD],}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)rr0ÚrangeÚlenrr)ÚargsÚbÚposrrrÚ_broadcast_shape¹s  r@cOs|Sr r©r=ÚkwargsrrrÚ_broadcast_arrays_dispatcherÊsrCcsr| dd¡‰|r*td t| ¡ƒd¡ƒ‚‡fdd„|Dƒ}t|މt‡fdd„|Dƒƒr^|S‡‡fd d„|DƒS) 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|dˆd�‘qS)Fr)rr)r%Ú_m)rrrÚ sz$broadcast_arrays..c3s|]}|jˆkVqdSr ©r©r%rrFrrr' sz#broadcast_arrays..csg|]}t|ˆˆdd�‘qS)Fr7r8rG©rrrrrEsÿ)ÚpopÚ TypeErrorÚformatÚlistÚkeysr@ÚallrArrHrrÎs4 ÿ ÿ)NNFT)N)F)rÚ __future__rrrr5rZnumpy.core.overridesrÚ__all__Úobjectrrr$r2r4rr@rCrrrrrÚs   M   )