o
    Ö­j@>  ã                   @   sê   d Z g d¢ZddlZddlmZmZmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZ e ¡ Zddefdd	„Zdefd
d„Zdefdd„Zefdd„Zefdd„Zdefdd„Zdefdd„Zdefdd„Zdefdd„Zdefdd„ZdS )z1
Differential and pseudo-differential operators.
)
ÚdiffÚtilbertÚitilbertÚhilbertÚihilbertÚcs_diffÚcc_diffÚsc_diffÚss_diffÚshifté    N)ÚpiÚasarrayÚsinÚcosÚsinhÚcoshÚtanhÚiscomplexobjé   )Úconvolve)Ú_datacopiedc           
      C   s  t |tjƒrt|dƒsi |_|j}t| ƒ}|dkr|S t|ƒr1t|j|||ƒdt|j	|||ƒ  S |dur<dt
 | }nd}t| ƒ}| |||f¡}|du rst|ƒdkr\|r\| ¡  |sV||fdd	„}tj|||d
d�}|||||f< t|| ƒ}	tj|||d |	d�S )a*  
    Return kth derivative (or integral) of a periodic sequence x.

    If x_j and y_j are Fourier coefficients of periodic functions x
    and y, respectively, then::

      y_j = pow(sqrt(-1)*j*2*pi/period, order) * x_j
      y_0 = 0 if order is not 0.

    Parameters
    ----------
    x : array_like
        Input array.
    order : int, optional
        The order of differentiation. Default order is 1. If order is
        negative, then integration is carried out under the assumption
        that ``x_0 == 0``.
    period : float, optional
        The assumed period of the sequence. Default is ``2*pi``.

    Notes
    -----
    If ``sum(x, axis=0) = 0`` then ``diff(diff(x, k), -k) == x`` (within
    numerical accuracy).

    For odd order and even ``len(x)``, the Nyquist mode is taken zero.

    Ú
diff_cacher   ù              ð?Né   ç      ð?é   c                 S   s   | r	t ||  |ƒS dS ©Nr   )Úpow)ÚkÚorderÚc© r!   úX/var/www/html/CropPilot/venv/lib/python3.10/site-packages/scipy/fftpack/_pseudo_diffs.pyÚkernelI   ó   zdiff.<locals>.kernelr   ©ÚdÚzero_nyquist©Úswap_real_imagÚoverwrite_x)Ú
isinstanceÚ	threadingÚlocalÚhasattrr   r   r   r   ÚrealÚimagr   ÚlenÚgetÚpopitemr   Úinit_convolution_kernelr   )
Úxr   ÚperiodÚ_cacheÚtmpr    ÚnÚomegar#   r*   r!   r!   r"   r      s<   

ÿÿ
ÿ
ÿr   c           	      C   óì   t |tjƒrt|dƒsi |_|j}t| ƒ}t|ƒr+t|j|||ƒdt|j	|||ƒ  S |dur7|d t
 | }t| ƒ}| ||f¡}|du rht|ƒdkrT|rT| ¡  |sN|fdd„}tj||dd	�}||||f< t|| ƒ}tj||d|d
�S )a�  
    Return h-Tilbert transform of a periodic sequence x.

    If x_j and y_j are Fourier coefficients of periodic functions x
    and y, respectively, then::

        y_j = sqrt(-1)*coth(j*h*2*pi/period) * x_j
        y_0 = 0

    Parameters
    ----------
    x : array_like
        The input array to transform.
    h : float
        Defines the parameter of the Tilbert transform.
    period : float, optional
        The assumed period of the sequence. Default period is ``2*pi``.

    Returns
    -------
    tilbert : ndarray
        The result of the transform.

    Notes
    -----
    If ``sum(x, axis=0) == 0`` and ``n = len(x)`` is odd, then
    ``tilbert(itilbert(x)) == x``.

    If ``2 * pi * h / period`` is approximately 10 or larger, then
    numerically ``tilbert == hilbert``
    (theoretically oo-Tilbert == Hilbert).

    For even ``len(x)``, the Nyquist mode of ``x`` is taken zero.

    Útilbert_cacher   Nr   r   c                 S   s   | r
dt ||  ƒ S dS )Nr   r   ©r   ©r   Úhr!   r!   r"   r#   �   s   ztilbert.<locals>.kernelr   ©r&   r(   )r+   r,   r-   r.   r<   r   r   r   r/   r0   r   r1   r2   r3   r   r4   r   ©	r5   r?   r6   r7   r8   r9   r:   r#   r*   r!   r!   r"   r   U   s.   $
ÿÿ
r   c           	      C   r;   )a  
    Return inverse h-Tilbert transform of a periodic sequence x.

    If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x
    and y, respectively, then::

      y_j = -sqrt(-1)*tanh(j*h*2*pi/period) * x_j
      y_0 = 0

    For more details, see `tilbert`.

    Úitilbert_cacher   Nr   r   c                 S   s   | r	t ||  ƒ S dS r   r=   r>   r!   r!   r"   r#   ¹   r$   zitilbert.<locals>.kernelr   r@   r(   )r+   r,   r-   r.   rB   r   r   r   r/   r0   r   r1   r2   r3   r   r4   r   rA   r!   r!   r"   r   š   s.   
ÿÿ
r   c                 C   sÀ   t |tjƒrt|dƒsi |_|j}t| ƒ}t|ƒr't|j|ƒdt|j	|ƒ  S t
| ƒ}| |¡}|du rRt
|ƒdkrB|rB| ¡  |s<dd„ }tj||dd�}|||< t|| ƒ}tj||d|d	�S )
aú  
    Return Hilbert transform of a periodic sequence x.

    If x_j and y_j are Fourier coefficients of periodic functions x
    and y, respectively, then::

      y_j = sqrt(-1)*sign(j) * x_j
      y_0 = 0

    Parameters
    ----------
    x : array_like
        The input array, should be periodic.
    _cache : dict, optional
        Dictionary that contains the kernel used to do a convolution with.

    Returns
    -------
    y : ndarray
        The transformed input.

    See Also
    --------
    scipy.signal.hilbert : Compute the analytic signal, using the Hilbert
                           transform.

    Notes
    -----
    If ``sum(x, axis=0) == 0`` then ``hilbert(ihilbert(x)) == x``.

    For even len(x), the Nyquist mode of x is taken zero.

    The sign of the returned transform does not have a factor -1 that is more
    often than not found in the definition of the Hilbert transform. Note also
    that `scipy.signal.hilbert` does have an extra -1 factor compared to this
    function.

    Úhilbert_cacher   Nr   c                 S   s   | dkrdS | dk rdS dS )Nr   r   g      ð¿g        r!   )r   r!   r!   r"   r#   ù   s
   zhilbert.<locals>.kernelr   r@   r(   )r+   r,   r-   r.   rC   r   r   r   r/   r0   r1   r2   r3   r   r4   r   )r5   r7   r8   r9   r:   r#   r*   r!   r!   r"   r   Ã   s&   '

ÿ
r   c                 C   s.   t |tjƒrt|dƒsi |_|j}t| |ƒ S )zå
    Return inverse Hilbert transform of a periodic sequence x.

    If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x
    and y, respectively, then::

      y_j = -sqrt(-1)*sign(j) * x_j
      y_0 = 0

    Úihilbert_cache)r+   r,   r-   r.   rD   r   )r5   r7   r!   r!   r"   r     s
   
r   c           
      C   ó  t |tjƒrt|dƒsi |_|j}t| ƒ}t|ƒr-t|j||||ƒdt|j	||||ƒ  S |durA|d t
 | }|d t
 | }t| ƒ}| |||f¡}|du rut|ƒdkr_|r_| ¡  |sY||fdd„}tj||dd	�}|||||f< t|| ƒ}	tj||d|	d
�S )aô  
    Return (a,b)-cosh/sinh pseudo-derivative of a periodic sequence.

    If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x
    and y, respectively, then::

      y_j = -sqrt(-1)*cosh(j*a*2*pi/period)/sinh(j*b*2*pi/period) * x_j
      y_0 = 0

    Parameters
    ----------
    x : array_like
        The array to take the pseudo-derivative from.
    a, b : float
        Defines the parameters of the cosh/sinh pseudo-differential
        operator.
    period : float, optional
        The period of the sequence. Default period is ``2*pi``.

    Returns
    -------
    cs_diff : ndarray
        Pseudo-derivative of periodic sequence `x`.

    Notes
    -----
    For even len(`x`), the Nyquist mode of `x` is taken as zero.

    Úcs_diff_cacher   Nr   r   c                 S   s"   | rt ||  ƒ t||  ƒ S dS r   )r   r   ©r   ÚaÚbr!   r!   r"   r#   H  s   zcs_diff.<locals>.kernelr   r@   r(   )r+   r,   r-   r.   rF   r   r   r   r/   r0   r   r1   r2   r3   r   r4   r   ©
r5   rH   rI   r6   r7   r8   r9   r:   r#   r*   r!   r!   r"   r     s0   
ÿÿ
r   c           
      C   rE   )aˆ  
    Return (a,b)-sinh/cosh pseudo-derivative of a periodic sequence x.

    If x_j and y_j are Fourier coefficients of periodic functions x
    and y, respectively, then::

      y_j = sqrt(-1)*sinh(j*a*2*pi/period)/cosh(j*b*2*pi/period) * x_j
      y_0 = 0

    Parameters
    ----------
    x : array_like
        Input array.
    a,b : float
        Defines the parameters of the sinh/cosh pseudo-differential
        operator.
    period : float, optional
        The period of the sequence x. Default is 2*pi.

    Notes
    -----
    ``sc_diff(cs_diff(x,a,b),b,a) == x``
    For even ``len(x)``, the Nyquist mode of x is taken as zero.

    Úsc_diff_cacher   Nr   r   c                 S   s    | rt ||  ƒt||  ƒ S dS r   )r   r   rG   r!   r!   r"   r#     s   zsc_diff.<locals>.kernelr   r@   r(   )r+   r,   r-   r.   rK   r   r   r   r/   r0   r   r1   r2   r3   r   r4   r   rJ   r!   r!   r"   r   R  s0   
ÿÿ
r   c           
      C   ó   t |tjƒrt|dƒsi |_|j}t| ƒ}t|ƒr-t|j||||ƒdt|j	||||ƒ  S |durA|d t
 | }|d t
 | }t| ƒ}| |||f¡}|du rst|ƒdkr_|r_| ¡  |sY||fdd„}t ||¡}|||||f< t|| ƒ}	tj|||	d�S )	ac  
    Return (a,b)-sinh/sinh pseudo-derivative of a periodic sequence x.

    If x_j and y_j are Fourier coefficients of periodic functions x
    and y, respectively, then::

      y_j = sinh(j*a*2*pi/period)/sinh(j*b*2*pi/period) * x_j
      y_0 = a/b * x_0

    Parameters
    ----------
    x : array_like
        The array to take the pseudo-derivative from.
    a,b
        Defines the parameters of the sinh/sinh pseudo-differential
        operator.
    period : float, optional
        The period of the sequence x. Default is ``2*pi``.

    Notes
    -----
    ``ss_diff(ss_diff(x,a,b),b,a) == x``

    Úss_diff_cacher   Nr   r   c                 S   s(   | rt ||  ƒt ||  ƒ S t|ƒ| S ©N)r   ÚfloatrG   r!   r!   r"   r#   µ  s   zss_diff.<locals>.kernel©r*   )r+   r,   r-   r.   rM   r   r   r	   r/   r0   r   r1   r2   r3   r   r4   r   rJ   r!   r!   r"   r	   ‰  s0   
ÿÿ
r	   c           
      C   rL   )	a¶  
    Return (a,b)-cosh/cosh pseudo-derivative of a periodic sequence.

    If x_j and y_j are Fourier coefficients of periodic functions x
    and y, respectively, then::

      y_j = cosh(j*a*2*pi/period)/cosh(j*b*2*pi/period) * x_j

    Parameters
    ----------
    x : array_like
        The array to take the pseudo-derivative from.
    a,b : float
        Defines the parameters of the sinh/sinh pseudo-differential
        operator.
    period : float, optional
        The period of the sequence x. Default is ``2*pi``.

    Returns
    -------
    cc_diff : ndarray
        Pseudo-derivative of periodic sequence `x`.

    Notes
    -----
    ``cc_diff(cc_diff(x,a,b),b,a) == x``

    Úcc_diff_cacher   Nr   r   c                 S   s   t ||  ƒt ||  ƒ S rN   )r   rG   r!   r!   r"   r#   ï  s   zcc_diff.<locals>.kernelrP   )r+   r,   r-   r.   rQ   r   r   r   r/   r0   r   r1   r2   r3   r   r4   r   rJ   r!   r!   r"   r   ¿  s0   
ÿÿ
r   c                 C   s  t |tjƒrt|dƒsi |_|j}t| ƒ}t|ƒr+t|j|||ƒdt|j	|||ƒ  S |dur7|d t
 | }t| ƒ}| ||f¡}|du r{t|ƒdkrT|rT| ¡  |sN|fdd„}|fdd	„}tj||d
d
d�}	tj||dd
d�}
|	|
f|||f< n|\}	}
t|| ƒ}tj||	|
|d�S )aò  
    Shift periodic sequence x by a: y(u) = x(u+a).

    If x_j and y_j are Fourier coefficients of periodic functions x
    and y, respectively, then::

          y_j = exp(j*a*2*pi/period*sqrt(-1)) * x_f

    Parameters
    ----------
    x : array_like
        The array to take the pseudo-derivative from.
    a : float
        Defines the parameters of the sinh/sinh pseudo-differential
    period : float, optional
        The period of the sequences x and y. Default period is ``2*pi``.
    Úshift_cacher   Nr   r   c                 S   ó   t ||  ƒS rN   )r   ©r   rH   r!   r!   r"   Úkernel_real  ó   zshift.<locals>.kernel_realc                 S   rS   rN   )r   rT   r!   r!   r"   Úkernel_imag  rV   zshift.<locals>.kernel_imagr   r%   r   rP   )r+   r,   r-   r.   rR   r   r   r
   r/   r0   r   r1   r2   r3   r   r4   r   Ú
convolve_z)r5   rH   r6   r7   r8   r9   r:   rU   rW   Ú
omega_realÚ
omega_imagr*   r!   r!   r"   r
   ÷  s@   

ÿÿ
ÿ
ÿ

ÿr
   )Ú__doc__Ú__all__r,   Únumpyr   r   r   r   r   r   r   r   Ú r   Úscipy.fft._pocketfft.helperr   r-   r7   r   r   r   r   r   r   r   r	   r   r
   r!   r!   r!   r"   Ú<module>   s"    (?E)B;768