a
    ùcý  ã                   @   sJ   d Z ddlZddlZddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dS )zMiscellaneous utilities.é    Né   c                 C   s*   t  | ¡}t  ||¡}t  |¡jd dkS )aÑ  
    Check if an array is linear.

    :param x: Array to be checked for linearity.
    :param decimals: decimal places upto which the derivative of the array
        should be rounded off (default=5)
    :type x: numpy.ndarray
    :type decimals: int
    :return: If the array is linear
    :rtype: boolean
    :Example:
    >>> import numpy as np
    >>> x = np.linspace(0, 2 * np.pi, 100)
    >>> is_linear(x)
    True
    >>> is_linear(np.sin(x))
    False
    r   é   )ÚnpZdiffZaroundÚuniqueÚshape)ÚxZdecimalsZ
derivative© r   úW/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/tftb/utils.pyÚ	is_linear	   s    
r
   c                 C   s„   | j dkr| j\}}n| jd d }}tj|| ftd}t|ƒD ]<}t |¡tjj| dd…|f dd ||t 	|¡|  < qB|S )zInverse Zak transform.é   r   r   )ZdtypeN)Zaxis)
Úndimr   r   ÚzerosÚcomplexÚrangeÚsqrtZfftZifftZarange)r   ÚnÚmÚsigZimr   r   r	   Úizak!   s    
:r   c                 C   s   t  t  | ¡¡S )a{  
    Compute the integer exponent of the next higher power of 2.

    :param n: Number whose next higest power of 2 needs to be computed.
    :type n: int, np.ndarray
    :rtype: int, np.ndarray
    :Example:
    >>> from __future__ import print_function
    >>> import numpy as np
    >>> x = np.arange(1, 9)
    >>> print(nextpow2(x))
    [ 0.  1.  2.  2.  3.  3.  3.  3.]
    )ÚmathÚceilÚlog2)r   r   r   r	   Únextpow2-   s    r   c                 C   sb   t  | ¡}|d dkr&t|ƒ}||fS t  |¡}t|ddƒD ]}| | }| | dkr< qZq<||fS )aç  
    Compute two factors of N such that they are as close as possible to sqrt(N).

    :param N: Number to be divided.
    :type N: int
    :return: A tuple of two integers such that their product is `N` and they
        are the closest possible to :math:`\sqrt(N)`  # NOQA: W605
    :rtype: tuple(int)
    :Example:
    >>> from __future__ import print_function
    >>> print(divider(256))
    (16, 16)
    >>> print(divider(10))
    (2, 5)
    >>> print(divider(101))
    (1, 101)
    r   r   éÿÿÿÿ)r   r   Úintr   r   )ÚNÚsÚiÚfactorr   r   r	   Údivider>   s    

r   c                 C   s   t | ttjfƒr@t | ¡}|t |d¡dk  d7  < | t¡S | d dkrT| d S t | ¡d dkrpt 	| ¡S t | ¡d dkrŒt | ¡S | S )a\  
    Get the nearest odd number for each value of N.

    :param N: int / sequence of ints
    :return: int / sequence of ints
    :Example:
    >>> from __future__ import print_function
    >>> print(nearest_odd(range(1, 11)))
    [  1.   3.   3.   5.   5.   7.   7.   9.   9.  11.]
    >>> nearest_odd(0)
    1
    >>> nearest_odd(3)
    3.0
    r   r   r   )
Ú
isinstanceÚlistr   ZndarrayÚfloorÚ	remainderZastyper   r   r   )r   Úyr   r   r	   Únearest_odd\   s    



r%   c                 C   sT   t t | ¡ƒr(t | |¡}|||dk< n(t t | ¡|¡dt t | ¡|¡  }|S )zþ
    Compute the congruence of each element of x modulo N.

    :type x: array-like
    :type N: int
    :return: array-like
    :Example:
    >>> from __future__ import print_function
    >>> print(modulo(range(1, 11), 2))
    [1 2 1 2 1 2 1 2 1 2]
    r   y              ð?)Úanyr   ZisrealÚmodÚrealÚimag)r   r   r$   r   r   r	   Úmodulox   s
    (r*   )r   )
Ú__doc__r   Únumpyr   r
   r   r   r   r%   r*   r   r   r   r	   Ú<module>   s   
