a
    ߙfb7                     @   sp   d Z ddlZddlmZ ddlZg dZG dd dZG dd deejd	Z	G d
d de
Zdd Zdd ZdS )z>The ShapedLikeNDArray mixin class and shape-related functions.    N)zip_longest)NDArrayShapeMethodsShapedLikeNDArraycheck_broadcastIncompatibleShapeErrorunbroadcastc                   @   sn   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
dd Zdd Zdd Zdd ZdddZdS )r   a  Mixin class to provide shape-changing methods.

    The class proper is assumed to have some underlying data, which are arrays
    or array-like structures. It must define a ``shape`` property, which gives
    the shape of those data, as well as an ``_apply`` method that creates a new
    instance in which a `~numpy.ndarray` method has been applied to those.

    Furthermore, for consistency with `~numpy.ndarray`, it is recommended to
    define a setter for the ``shape`` property, which, like the
    `~numpy.ndarray.shape` property allows in-place reshaping the internal data
    (and, unlike the ``reshape`` method raises an exception if this is not
    possible).

    This class only provides the shape-changing methods and is meant in
    particular for `~numpy.ndarray` subclasses that need to keep track of
    other arrays.  For other classes, `~astropy.utils.shapes.ShapedLikeNDArray`
    is recommended.

    c                 C   s   |  d|S )N__getitem___applyselfitem r   3lib/python3.9/site-packages/astropy/utils/shapes.pyr   )   s    zNDArrayShapeMethods.__getitem__c                 O   s   | j dg|R i |S )zReturn an instance containing copies of the internal data.

        Parameters are as for :meth:`~numpy.ndarray.copy`.
        copyr	   r   argskwargsr   r   r   r   ,   s    zNDArrayShapeMethods.copyc                 O   s   | j dg|R i |S )a  Returns an instance containing the same data with a new shape.

        Parameters are as for :meth:`~numpy.ndarray.reshape`.  Note that it is
        not always possible to change the shape of an array without copying the
        data (see :func:`~numpy.reshape` documentation). If you want an error
        to be raise if the data is copied, you should assign the new shape to
        the shape attribute (note: this may not be implemented for all classes
        using ``NDArrayShapeMethods``).
        reshaper	   r   r   r   r   r   3   s    
zNDArrayShapeMethods.reshapec                 O   s   | j dg|R i |S )ah  Return an instance with the array collapsed into one dimension.

        Parameters are as for :meth:`~numpy.ndarray.ravel`. Note that it is
        not always possible to unravel an array without copying the data.
        If you want an error to be raise if the data is copied, you should
        should assign shape ``(-1,)`` to the shape attribute.
        ravelr	   r   r   r   r   r   ?   s    zNDArrayShapeMethods.ravelc                 O   s   | j dg|R i |S )zReturn a copy with the array collapsed into one dimension.

        Parameters are as for :meth:`~numpy.ndarray.flatten`.
        flattenr	   r   r   r   r   r   I   s    zNDArrayShapeMethods.flattenc                 O   s   | j dg|R i |S )zReturn an instance with the data transposed.

        Parameters are as for :meth:`~numpy.ndarray.transpose`.  All internal
        data are views of the data of the original.
        	transposer	   r   r   r   r   r   P   s    zNDArrayShapeMethods.transposec                 C   s   | j dk r| S |  S dS )zReturn an instance with the data transposed.

        Parameters are as for :attr:`~numpy.ndarray.T`.  All internal
        data are views of the data of the original.
           N)ndimr   r   r   r   r   TX   s    
zNDArrayShapeMethods.Tc                 O   s   | j dg|R i |S )zReturn an instance with the given axes interchanged.

        Parameters are as for :meth:`~numpy.ndarray.swapaxes`:
        ``axis1, axis2``.  All internal data are views of the data of the
        original.
        swapaxesr	   r   r   r   r   r   d   s    zNDArrayShapeMethods.swapaxesc                 O   s   | j dg|R i |S )zReturn an instance with the specified diagonals.

        Parameters are as for :meth:`~numpy.ndarray.diagonal`.  All internal
        data are views of the data of the original.
        diagonalr	   r   r   r   r   r   m   s    zNDArrayShapeMethods.diagonalc                 O   s   | j dg|R i |S )zReturn an instance with single-dimensional shape entries removed

        Parameters are as for :meth:`~numpy.ndarray.squeeze`.  All internal
        data are views of the data of the original.
        squeezer	   r   r   r   r   r   u   s    zNDArrayShapeMethods.squeezeNraisec                 C   s"   |durt dS | jd|||dS )zReturn a new instance formed from the elements at the given indices.

        Parameters are as for :meth:`~numpy.ndarray.take`, except that,
        obviously, no output array can be given.
        Nz$cannot pass 'out' argument to 'take.take)axismode)NotImplementedErrorr
   )r   indicesr!   outr"   r   r   r   r    }   s    zNDArrayShapeMethods.take)NNr   )__name__
__module____qualname____doc__r   r   r   r   r   r   propertyr   r   r   r   r    r   r   r   r   r      s   

	r   c                   @   s   e Zd ZdZeejdd Zejdd Zedd Z	edd	 Z
ed
d Zdd Zdd Zdd Zdd ZejejejejejejejejejejejejejhZdd ejj j!D Z"de"ej#< dd Z$dS )r   a  Mixin class to provide shape-changing methods.

    The class proper is assumed to have some underlying data, which are arrays
    or array-like structures. It must define a ``shape`` property, which gives
    the shape of those data, as well as an ``_apply`` method that creates a new
    instance in which a `~numpy.ndarray` method has been applied to those.

    Furthermore, for consistency with `~numpy.ndarray`, it is recommended to
    define a setter for the ``shape`` property, which, like the
    `~numpy.ndarray.shape` property allows in-place reshaping the internal data
    (and, unlike the ``reshape`` method raises an exception if this is not
    possible).

    This class also defines default implementations for ``ndim`` and ``size``
    properties, calculating those from the ``shape``.  These can be overridden
    by subclasses if there are faster ways to obtain those numbers.

    c                 C   s   dS )z!The shape of the underlying data.Nr   r   r   r   r   shape   s    zShapedLikeNDArray.shapec                 O   s   dS )a  Create a new instance, with ``method`` applied to underlying data.

        The method is any of the shape-changing methods for `~numpy.ndarray`
        (``reshape``, ``swapaxes``, etc.), as well as those picking particular
        elements (``__getitem__``, ``take``, etc.). It will be applied to the
        underlying arrays (e.g., ``jd1`` and ``jd2`` in `~astropy.time.Time`),
        with the results used to create a new instance.

        Parameters
        ----------
        method : str
            Method to be applied to the instance's internal data arrays.
        args : tuple
            Any positional arguments for ``method``.
        kwargs : dict
            Any keyword arguments for ``method``.

        Nr   )methodr   r   r   r   r   r
      s    zShapedLikeNDArray._applyc                 C   s
   t | jS )z?The number of dimensions of the instance and underlying arrays.)lenr+   r   r   r   r   r      s    zShapedLikeNDArray.ndimc                 C   s   d}| j D ]}||9 }q
|S )z5The size of the object, as calculated from its shape.   r+   )r   sizeZshr   r   r   r0      s    

zShapedLikeNDArray.sizec                 C   s
   | j dkS )Nr   r/   r   r   r   r   isscalar   s    zShapedLikeNDArray.isscalarc                 C   s"   | j rtd| jj| jd S )NzScalar {!r} object has no len()r   )r1   	TypeErrorformat	__class__r&   r+   r   r   r   r   __len__   s
    zShapedLikeNDArray.__len__c                 C   s
   | j dkS )z>Any instance should evaluate to True, except when it is empty.r   )r0   r   r   r   r   __bool__   s    zShapedLikeNDArray.__bool__c                 C   sB   z|  d|W S  ty<   | jr6td| jjn Y n0 d S )Nr   z(scalar {!r} object is not subscriptable.)r
   
IndexErrorr1   r2   r3   r4   r&   r   r   r   r   r      s    zShapedLikeNDArray.__getitem__c                    s*    j rtd jj fdd}| S )Nz#scalar {!r} object is not iterable.c                  3   s    t t D ]}  |  V  qd S N)ranger-   )idxr   r   r   	self_iter   s    z-ShapedLikeNDArray.__iter__.<locals>.self_iter)r1   r2   r3   r4   r&   )r   r;   r   r   r   __iter__   s    zShapedLikeNDArray.__iter__c              
   C   s6   i | ].}|d vrt t|ddddddd||qS ))alensort	partitionmaxminroundallany)ZamaxZaminZaroundZround_ZalltrueZsometrue)getattrnpget).0namer   r   r   
<dictcomp>   s   zShapedLikeNDArray.<dictcomp>r   c                    s    | j v r tju r"dd n8 tjtjtjhv rZt|dkrZt fdd|D S | |d urjt	S | j
 g|dd R i S | |d u r܈ | jv rt| | j  d}|durt|r||dd i S |S  j|i S )z%Wrap numpy functions that make sense.ZsubokTr.   c                 3   s   | ]} |fi V  qd S r8   r   )rH   argfunctionr   r   r   	<genexpr>      z7ShapedLikeNDArray.__array_function__.<locals>.<genexpr>r   N)_APPLICABLE_FUNCTIONSrF   broadcast_to
setdefault
atleast_1d
atleast_2d
atleast_3dr-   tupleNotImplementedr
   _METHOD_FUNCTIONSrE   callable__wrapped__)r   rM   typesr   r   r,   r   rL   r   __array_function__  s(    


 z$ShapedLikeNDArray.__array_function__N)%r&   r'   r(   r)   r*   abcabstractmethodr+   r
   r   r0   r1   r5   r6   r   r<   rF   ZmoveaxisZrollaxisrS   rT   rU   Zexpand_dimsrQ   ZflipZfliplrZflipudZrot90ZrolldeleterP   coreZfromnumeric__all__rX   r   r\   r   r   r   r   r      s4   







r   )	metaclassc                       s   e Zd Z fddZ  ZS )r   c                    s   t  |||| d S r8   )super__init__)r   Zshape_aZshape_a_idxZshape_bZshape_b_idxr4   r   r   rd   0  s    zIncompatibleShapeError.__init__)r&   r'   r(   rd   __classcell__r   r   re   r   r   /  s   r   c                  G   s   t | dkrdS t | dkr$| d S dd | D }g }t|ddiD ]b}d}d}t|D ]B\}}|dkrjqX|dkr||}|}qX||krXt| | || | |qX|| qDt|ddd S )	a  
    Determines whether two or more Numpy arrays can be broadcast with each
    other based on their shape tuple alone.

    Parameters
    ----------
    *shapes : tuple
        All shapes to include in the comparison.  If only one shape is given it
        is passed through unmodified.  If no shapes are given returns an empty
        `tuple`.

    Returns
    -------
    broadcast : `tuple`
        If all shapes are mutually broadcastable, returns a tuple of the full
        broadcast shape.
    r   r   r.   c                 s   s   | ]}t |V  qd S r8   )reversed)rH   r+   r   r   r   rN   L  rO   z"check_broadcast.<locals>.<genexpr>	fillvalueN)r-   r   	enumerater   appendrV   )ZshapesZreversed_shapesZ
full_shapeZdimsZmax_dimZmax_dim_idxr:   Zdimr   r   r   r   4  s*    r   c                 C   sV   | j dkr| S | tdd | jD  } tdd t| jD | j }| | j|d S )a  
    Given an array, return a new array that is the smallest subset of the
    original array that can be re-broadcasted back to the original array.

    See https://stackoverflow.com/questions/40845769/un-broadcasting-numpy-arrays
    for more details.
    r   c                 s   s(   | ] }|d krt d dnt dV  qdS )r   r.   N)slice)rH   Zstrider   r   r   rN   p  s   zunbroadcast.<locals>.<genexpr>c                 s   s   | ]\}}|d kr|V  qdS )r.   Nr   )rH   isr   r   r   rN   t  rO   N)r   rV   stridesnextrj   r+   r   )ZarrayZfirst_not_unityr   r   r   r   d  s    	


r   )r)   r]   	itertoolsr   ZnumpyrF   ra   r   ABCMetar   
ValueErrorr   r   r   r   r   r   r   <module>   s   | '0