B
    d                 @   s0   d Z ddlZddlmZmZ G dd deZdS )zConvolution Model    N   )CompoundModelSPECIAL_OPERATORSc                   sV   e Zd ZdZd fdd	Zdd Z fddZed	d
 Zedd Z	dd Z
  ZS )Convolutionas  
    Wrapper class for a convolution model.

    Parameters
    ----------
    operator: tuple
        The SPECIAL_OPERATORS entry for the convolution being used.
    model : Model
        The model for the convolution.
    kernel: Model
        The kernel model for the convolution.
    bounding_box : tuple
        A bounding box to define the limits of the integration
        approximation for the convolution.
    resolution : float
        The resolution for the approximation of the convolution.
    cache : bool, optional
        Allow convolution computation to be cached for reuse. This is
        enabled by default.

    Notes
    -----
    This is wrapper is necessary to handle the limitations of the
    pseudospectral convolution binary operator implemented in
    astropy.convolution under `~astropy.convolution.convolve_fft`. In this `~astropy.convolution.convolve_fft` it
    is assumed that the inputs ``array`` and ``kernel`` span a sufficient
    portion of the support of the functions of the convolution.
    Consequently, the ``Compound`` created by the `~astropy.convolution.convolve_models` function
    makes the assumption that one should pass an input array that
    sufficiently spans this space. This means that slightly different
    input arrays to this model will result in different outputs, even
    on points of intersection between these arrays.

    This issue is solved by requiring a ``bounding_box`` together with a
    resolution so that one can pre-calculate the entire domain and then
    (by default) cache the convolution values. The function then just
    interpolates the results from this cache.
    Tc                s2   t  ||| || _|| _|| _d | _d | _d S )N)super__init__bounding_box_resolution_cache_convolution_kwargs_convolution)selfoperatormodelZkernelr   
resolutioncache)	__class__ i/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/modeling/convolution.pyr   2   s    zConvolution.__init__c             C   s   d| _ d| _dS )z/
        Clears the cached convolution
        N)r   r   )r   r   r   r   clear_cache<   s    zConvolution.clear_cachec                s   | j d ks| j|kr| j| j}tj| }t j||}yddl	m
} |||}W n tk
rp   tdY nX | jr|| _|| _ n| j }|S )Nr   )RegularGridInterpolatorz!Convolution model requires scipy.)r   r   r   domainr	   npZmeshgridr   __call__Zscipy.interpolater   
ValueErrorImportErrorr
   )r   kwargsr   Zmeshdatar   convolution)r   r   r   _get_convolutionD   s    
zConvolution._get_convolutionc                 s   t dd  D d }t|dkr2t  dfS  |d  jt fdd|D s`tdg }x4 D ],}t |r|t 	| qj|| qjW t 
|t|dfjfS d S )	Nc             S   s   g | ]}t | qS r   )r   isscalar).0argr   r   r   
<listcomp>[   s    z3Convolution._convolution_inputs.<locals>.<listcomp>r   )r   c             3   s   | ]} | j kV  qd S )N)shape)r!   index)argsoutput_shaper   r   	<genexpr>a   s    z2Convolution._convolution_inputs.<locals>.<genexpr>zValues have differing Shapes)r   wherelenarrayr$   allr   r    appendfullreshapeT)r&   Z
not_scalarinputsr"   r   )r&   r'   r   _convolution_inputsY   s    

zConvolution._convolution_inputsc             C   s
   |  |S )N)r0   )outputsr'   r   r   r   _convolution_outputsm   s    z Convolution._convolution_outputsc             O   s.   | j | \}}| jf |}||}| ||S )N)r3   r   r5   )r   r&   kwr2   r'   r   r4   r   r   r   r   q   s    zConvolution.__call__)T)__name__
__module____qualname____doc__r   r   r   staticmethodr3   r5   r   __classcell__r   r   )r   r   r   
   s   &
r   )r:   numpyr   corer   r   r   r   r   r   r   <module>   s   