a
    ¬ˆc
&  ã                   @   s€   d Z ddlZddlmZ ddlmZmZ ddlm	Z	m
Z
 g d¢Ze d¡Zdd
d„Zddd„Zddd„Zddd„Zddd„ZdS )zµFast Hankel transforms using the FFTLog algorithm.

The implementation closely follows the Fortran code of Hamilton (2000).

added: 14/11/2020 Nicolas Tessore <n.tessore@ucl.ac.uk>
é    N)Úwarné   )ÚrfftÚirffté   )ÚloggammaÚpoch)ÚfhtÚifhtÚ	fhtoffsetç        c           
      C   s�   t  | ¡d }|dkrH|d d }t  |¡}| t  | ||  | ¡ } t|||||d�}t| |ƒ}	|dkrŒ|	t  | || | |  ¡9 }	|	S )a  Compute the fast Hankel transform.

    Computes the discrete Hankel transform of a logarithmically spaced periodic
    sequence using the FFTLog algorithm [1]_, [2]_.

    Parameters
    ----------
    a : array_like (..., n)
        Real periodic input array, uniformly logarithmically spaced.  For
        multidimensional input, the transform is performed over the last axis.
    dln : float
        Uniform logarithmic spacing of the input array.
    mu : float
        Order of the Hankel transform, any positive or negative real number.
    offset : float, optional
        Offset of the uniform logarithmic spacing of the output array.
    bias : float, optional
        Exponent of power law bias, any positive or negative real number.

    Returns
    -------
    A : array_like (..., n)
        The transformed output array, which is real, periodic, uniformly
        logarithmically spaced, and of the same shape as the input array.

    See Also
    --------
    ifht : The inverse of `fht`.
    fhtoffset : Return an optimal offset for `fht`.

    Notes
    -----
    This function computes a discrete version of the Hankel transform

    .. math::

        A(k) = \int_{0}^{\infty} \! a(r) \, J_\mu(kr) \, k \, dr \;,

    where :math:`J_\mu` is the Bessel function of order :math:`\mu`.  The index
    :math:`\mu` may be any real number, positive or negative.

    The input array `a` is a periodic sequence of length :math:`n`, uniformly
    logarithmically spaced with spacing `dln`,

    .. math::

        a_j = a(r_j) \;, \quad
        r_j = r_c \exp[(j-j_c) \, \mathtt{dln}]

    centred about the point :math:`r_c`.  Note that the central index
    :math:`j_c = (n-1)/2` is half-integral if :math:`n` is even, so that
    :math:`r_c` falls between two input elements.  Similarly, the output
    array `A` is a periodic sequence of length :math:`n`, also uniformly
    logarithmically spaced with spacing `dln`

    .. math::

       A_j = A(k_j) \;, \quad
       k_j = k_c \exp[(j-j_c) \, \mathtt{dln}]

    centred about the point :math:`k_c`.

    The centre points :math:`r_c` and :math:`k_c` of the periodic intervals may
    be chosen arbitrarily, but it would be usual to choose the product
    :math:`k_c r_c = k_j r_{n-1-j} = k_{n-1-j} r_j` to be unity.  This can be
    changed using the `offset` parameter, which controls the logarithmic offset
    :math:`\log(k_c) = \mathtt{offset} - \log(r_c)` of the output array.
    Choosing an optimal value for `offset` may reduce ringing of the discrete
    Hankel transform.

    If the `bias` parameter is nonzero, this function computes a discrete
    version of the biased Hankel transform

    .. math::

        A(k) = \int_{0}^{\infty} \! a_q(r) \, (kr)^q \, J_\mu(kr) \, k \, dr

    where :math:`q` is the value of `bias`, and a power law bias
    :math:`a_q(r) = a(r) \, (kr)^{-q}` is applied to the input sequence.
    Biasing the transform can help approximate the continuous transform of
    :math:`a(r)` if there is a value :math:`q` such that :math:`a_q(r)` is
    close to a periodic sequence, in which case the resulting :math:`A(k)` will
    be close to the continuous transform.

    References
    ----------
    .. [1] Talman J. D., 1978, J. Comp. Phys., 29, 35
    .. [2] Hamilton A. J. S., 2000, MNRAS, 312, 257 (astro-ph/9905191)

    éÿÿÿÿr   r   r   ©ÚoffsetÚbias©ÚnpÚshapeZarangeÚexpÚfhtcoeffÚ_fhtq)
ÚaÚdlnÚmur   r   ÚnÚj_cÚjÚuÚA© r   úT/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/scipy/fft/_fftlog.pyr	      s    ]

 r	   c           
      C   s’   t  | ¡d }|dkrJ|d d }t  |¡}| t  ||| | |  ¡ } t|||||d�}t| |dd�}	|dkrŽ|	t  | ||  | ¡ }	|	S )a®  Compute the inverse fast Hankel transform.

    Computes the discrete inverse Hankel transform of a logarithmically spaced
    periodic sequence. This is the inverse operation to `fht`.

    Parameters
    ----------
    A : array_like (..., n)
        Real periodic input array, uniformly logarithmically spaced.  For
        multidimensional input, the transform is performed over the last axis.
    dln : float
        Uniform logarithmic spacing of the input array.
    mu : float
        Order of the Hankel transform, any positive or negative real number.
    offset : float, optional
        Offset of the uniform logarithmic spacing of the output array.
    bias : float, optional
        Exponent of power law bias, any positive or negative real number.

    Returns
    -------
    a : array_like (..., n)
        The transformed output array, which is real, periodic, uniformly
        logarithmically spaced, and of the same shape as the input array.

    See Also
    --------
    fht : Definition of the fast Hankel transform.
    fhtoffset : Return an optimal offset for `ifht`.

    Notes
    -----
    This function computes a discrete version of the Hankel transform

    .. math::

        a(r) = \int_{0}^{\infty} \! A(k) \, J_\mu(kr) \, r \, dk \;,

    where :math:`J_\mu` is the Bessel function of order :math:`\mu`.  The index
    :math:`\mu` may be any real number, positive or negative.

    See `fht` for further details.

    r   r   r   r   r   T)Úinverser   )
r   r   r   r   r   r   r   r   r   r   r   r   r    r
   ‹   s    /
r
   c                 C   sV  || }}|d | d }|d | d }t  dt j| d  | |  | d d ¡}	t j| d d td�}
t j| d d td�}|	|
jdd…< ||
jdd…< t|
|d� ||
jdd…< t|
|
d� |	dt|  9 }	|
 j|j8  _|
 jt| 7  _|
 j|j7  _|
 j|	7  _t j	|
|
d� d|
jd< t  
|
d ¡�sRd| t||| ƒ |
d< |
S )z?Compute the coefficient array for a fast Hankel transform.
    r   r   r   )ZdtypeN)Úoutr   )r   ZlinspaceÚpiÚemptyÚcomplexÚimagÚrealr   ÚLN_2r   Úisfiniter   )r   r   r   r   r   ÚlnkrÚqÚxpÚxmÚyr   Úvr   r   r    r   Ñ   s*    
(
r   c                 C   sŒ   || }}|d | d }|d | d }t jd|   }t|d|  ƒ}	t|d|  ƒ}
t| |  |	j|
j t j  }||t  |¡ |   S )a  Return optimal offset for a fast Hankel transform.

    Returns an offset close to `initial` that fulfils the low-ringing
    condition of [1]_ for the fast Hankel transform `fht` with logarithmic
    spacing `dln`, order `mu` and bias `bias`.

    Parameters
    ----------
    dln : float
        Uniform logarithmic spacing of the transform.
    mu : float
        Order of the Hankel transform, any positive or negative real number.
    initial : float, optional
        Initial value for the offset. Returns the closest value that fulfils
        the low-ringing condition.
    bias : float, optional
        Exponent of power law bias, any positive or negative real number.

    Returns
    -------
    offset : float
        Optimal offset of the uniform logarithmic spacing of the transform that
        fulfils a low-ringing condition.

    See Also
    --------
    fht : Definition of the fast Hankel transform.

    References
    ----------
    .. [1] Hamilton A. J. S., 2000, MNRAS, 312, 257 (astro-ph/9905191)

    r   r   y              ð?)r   r#   r   r(   r&   Úround)r   r   Úinitialr   r*   r+   r,   r-   r.   ZzpZzmÚargr   r   r    r   ù   s    #
r   Fc                 C   s®   t  | ¡d }t  |d ¡r:|s:tdƒ | ¡ }d|d< n*|d dkrd|rdtdƒ | ¡ }t j|d< t| dd�}|s~||9 }n|| ¡  }t||dd�}|dddd…f }|S )zUCompute the biased fast Hankel transform.

    This is the basic FFTLog routine.
    r   r   z.singular transform; consider changing the biasz6singular inverse transform; consider changing the bias)Zaxis.N)	r   r   Úisinfr   ÚcopyÚinfr   Zconjr   )r   r   r!   r   r   r   r   r    r   '  s     


r   )r   r   )r   r   )r   r   )r   r   )F)Ú__doc__Únumpyr   Úwarningsr   Z_basicr   r   Zspecialr   r   Ú__all__Úlogr(   r	   r
   r   r   r   r   r   r   r    Ú<module>   s   

t
F
(
.