a
    ¬ˆcû„  ã                   @   s&  d Z ddlZddlmZmZmZmZmZmZm	Z	 ddl
mZ ddlZddlmZmZ ddlmZ ddlmZ ddlmZ dd	lmZmZmZ dd
lmZ G dd„ dƒZdd„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"G dd„ dƒZ#G dd„ dƒZ$G dd„ dƒZ%G dd„ dƒZ&G dd „ d ƒZ'G d!d"„ d"ƒZ(dS )#z7
Unit tests for optimization routines from minpack.py.
é    N)Úassert_Úassert_almost_equalÚassert_array_equalÚassert_array_almost_equalÚassert_allcloseÚassert_warnsÚsuppress_warnings)Úraises)ÚarrayÚfloat64)Ú
ThreadPool)Úoptimize)Úlambertw)ÚleastsqÚ	curve_fitÚfixed_point)ÚOptimizeWarningc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚReturnShapezúThis class exists to create a callable that does not have a '__name__' attribute.

    __init__ takes the argument 'shape', which should be a tuple of ints. When an instance
    is called with a single argument 'x', it returns numpy.ones(shape).
    c                 C   s
   || _ d S ©N)Úshape)Úselfr   © r   úd/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/scipy/optimize/tests/test_minpack.pyÚ__init__   s    zReturnShape.__init__c                 C   s   t  | j¡S r   )ÚnpÚonesr   )r   Úxr   r   r   Ú__call__   s    zReturnShape.__call__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r      s   r   c                 C   s
   t  |¡S )zUA function that returns an array of ones of the given shape.
    `x` is ignored.
    )r   r   )r   r   r   r   r   Ú
dummy_func"   s    r"   c                 C   s@   t t| ƒƒ� }| dd„ | ¡W  d   ƒ S 1 s20    Y  d S )Nc                 S   s   | ƒ S r   r   )Úfr   r   r   Ú<lambda>+   ó    z#sequence_parallel.<locals>.<lambda>)r   ÚlenÚmap)ÚfsÚpoolr   r   r   Úsequence_parallel)   s    r*   c                 C   s6   || d  }t  |dd… |d  |  ¡ | f¡}|S )aš  Evaluate non-linear equation system representing
    the pressures and flows in a system of n parallel pipes::

        f_i = P_i - P_0, for i = 1..n
        f_0 = sum(Q_i) - Qtot

    where Q_i is the flow rate in pipe i and P_i the pressure in that pipe.
    Pressure is modeled as a P=kQ**2 where k is a valve coefficient and
    Q is the flow rate.

    Parameters
    ----------
    flow_rates : float
        A 1-D array of n flow rates [kg/s].
    k : float
        A 1-D array of n valve coefficients [1/kg m].
    Qtot : float
        A scalar, the total input flow rate [kg/s].

    Returns
    -------
    F : float
        A 1-D array, F[i] == f_i.

    é   é   Nr   )r   ZhstackÚsum)Ú
flow_ratesÚQtotÚkÚPÚFr   r   r   Úpressure_network2   s    &r3   c                 C   s¢   t | ƒ}t | dd… d |dd…  d| d  |d   ¡}t ||f¡}|d |d|d …d|d …f< d|d|d …|d f< t |¡||d dd…f< |S )a…  Return the jacobian of the equation system F(flow_rates)
    computed by `pressure_network` with respect to
    *flow_rates*. See `pressure_network` for the detailed
    description of parrameters.

    Returns
    -------
    jac : float
        *n* by *n* matrix ``df_i/dQ_i`` where ``n = len(flow_rates)``
        and *f_i* and *Q_i* are described in the doc for `pressure_network`
    r,   Nr+   r   )r&   r   ÚdiagÚemptyr   )r.   r/   r0   ÚnZpdiffÚjacr   r   r   Úpressure_network_jacobianQ   s    6 r8   c                 C   s   t | ||ƒt| ||ƒfS r   )r3   r8   )r.   r/   r0   r   r   r   Úpressure_network_fun_and_gradh   s    

ÿr9   c                   @   st   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )Ú
TestFSolvec                 C   s\   t  dd¡}d}tg d¢ƒ}tjt|||fdd�\}}}}t|t  d¡ƒ t|dk|ƒ d S )Né   ç      à?©ç       @ç        r>   r?   T©ÚargsÚfull_outputr,   )	r   Úfullr
   r   Úfsolver3   r   r   r   )r   r0   r/   Úinitial_guessÚfinal_flowsÚinfoÚierÚmesgr   r   r   Ú!test_pressure_network_no_gradientn   s    
þz,TestFSolve.test_pressure_network_no_gradientc                 C   sF   t  dd¡}d}tg d¢ƒ}tjt|||ftd�}t|t  d¡ƒ d S )Nr;   r<   r=   ©rA   Úfprime)	r   rC   r
   r   rD   r3   r8   r   r   ©r   r0   r/   rE   rF   r   r   r   Ú#test_pressure_network_with_gradienty   s    
þz.TestFSolve.test_pressure_network_with_gradientc                 C   s$   t dƒ}ddg}tttj||ƒ d S ©Nr,   ç      ø?r>   ©r   Úassert_raisesÚ	TypeErrorr   rD   ©r   ÚfuncÚx0r   r   r   Útest_wrong_shape_func_callableƒ   s    z)TestFSolve.test_wrong_shape_func_callablec                 C   s    ddg}t ttjt|dd� d S ©NrP   r>   )©r,   ©rA   )rR   rS   r   rD   r"   ©r   rV   r   r   r   Útest_wrong_shape_func_functionŠ   s    z)TestFSolve.test_wrong_shape_func_functionc                 C   s,   t dƒ}t dƒ}tttj|ddg|d� d S )Nr,   ©r+   r+   r   ©rV   rL   rQ   ©r   rU   Ú
deriv_funcr   r   r   Ú test_wrong_shape_fprime_callable�   s    z+TestFSolve.test_wrong_shape_fprime_callablec                 C   s,   dd„ }dd„ }t ttj|ddg|d� d S )Nc                 S   s
   t | dƒS ©N)r+   ©r"   ©r   r   r   r   r$   –   r%   z=TestFSolve.test_wrong_shape_fprime_function.<locals>.<lambda>c                 S   s
   t | dƒS ©N)é   rf   rc   rd   r   r   r   r$   —   r%   r   r,   r^   )rR   rS   r   rD   r_   r   r   r   Ú test_wrong_shape_fprime_function•   s    z+TestFSolve.test_wrong_shape_fprime_functionc                 C   sH   dd„ }t tdd��  tj|dgd� W d   ƒ n1 s:0    Y  d S )Nc                  W   s   t dƒ‚d S ©NúI raised©Ú
ValueErrorrZ   r   r   r   rU   ›   s    z,TestFSolve.test_func_can_raise.<locals>.funcri   ©Úmatchr   ©rV   ©rR   rk   r   rD   ©r   rU   r   r   r   Útest_func_can_raiseš   s    zTestFSolve.test_func_can_raisec                 C   sR   dd„ }dd„ }t tdd��" tj|dg|d� W d   ƒ n1 sD0    Y  d S )	Nc                 S   s   | t  dg¡ S ©Né
   ©r   r
   rd   r   r   r   r$   ¢   r%   z0TestFSolve.test_Dfun_can_raise.<locals>.<lambda>c                  W   s   t dƒ‚d S rh   rj   rZ   r   r   r   r`   ¤   s    z2TestFSolve.test_Dfun_can_raise.<locals>.deriv_funcri   rl   r   r^   ro   r_   r   r   r   Útest_Dfun_can_raise¡   s    zTestFSolve.test_Dfun_can_raisec                 C   s<   dd„ }t  |t ddgtj¡¡}t||ƒddgdd� d S )Nc                 S   s(   t j| d d | d d gt jd�d S )Nr   éd   r,   iè  ©Zdtyper+   )r   r
   Úfloat32rd   r   r   r   r$   «   r%   z)TestFSolve.test_float32.<locals>.<lambda>r,   r   çü©ñÒMbP?©Úatol)r   rD   r   r
   rx   r   )r   rU   Úpr   r   r   Útest_float32ª   s    zTestFSolve.test_float32c           	         sh   ‡ fdd„}t  dd¡}d}tg d¢ƒ}tj||||fdd�\}}}}t|t  d¡ƒ t|dk|ƒ d S )	Nc                     s   ˆ   ¡  t| Ž S r   )rJ   r3   rZ   ©r   r   r   rU   °   s    z,TestFSolve.test_reentrant_func.<locals>.funcr;   r<   r=   Tr@   r,   )r   rC   r
   r   rD   r   r   r   )	r   rU   r0   r/   rE   rF   rG   rH   rI   r   r~   r   Útest_reentrant_func¯   s    
þzTestFSolve.test_reentrant_funcc                    sR   ‡ fdd„}t  dd¡}d}tg d¢ƒ}tjt|||f|d�}t|t  d¡ƒ d S )Nc                     s   ˆ   ¡  t| Ž S r   )rN   r8   rZ   r~   r   r   r`   ¿   s    z3TestFSolve.test_reentrant_Dfunc.<locals>.deriv_funcr;   r<   r=   rK   )r   rC   r
   r   rD   r3   r   r   )r   r`   r0   r/   rE   rF   r   r~   r   Útest_reentrant_Dfunc¾   s    
þzTestFSolve.test_reentrant_Dfuncc                 C   s   t | jgd ƒS rr   )r*   rJ   r~   r   r   r   Útest_concurrent_no_gradientÌ   s    z&TestFSolve.test_concurrent_no_gradientc                 C   s   t | jgd ƒS rr   )r*   rN   r~   r   r   r   Útest_concurrent_with_gradientÏ   s    z(TestFSolve.test_concurrent_with_gradientN)r   r   r    rJ   rN   rW   r\   ra   rg   rq   ru   r}   r   r€   r�   r‚   r   r   r   r   r:   m   s   
	r:   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚTestRootHybrc                 C   sH   t  dd¡}d}tg d¢ƒ}tjt|d||fd�j}t|t  d¡ƒ d S )Nr;   r<   r=   Úhybr©ÚmethodrA   ©	r   rC   r
   r   Úrootr3   r   r   r   rM   r   r   r   rJ   Ô   s    ÿz.TestRootHybr.test_pressure_network_no_gradientc                 C   sL   t  dd¡}d}tg d¢gƒ}tjt|||fdtd�j}t|t  	d¡ƒ d S )Nr;   r<   r=   r„   ©rA   r†   r7   )
r   rC   r
   r   rˆ   r3   r8   r   r   r   rM   r   r   r   rN   Ý   s    þz0TestRootHybr.test_pressure_network_with_gradientc                 C   sJ   t  dd¡}d}tg d¢ƒ}tjt|||fddd�j}t|t  d¡ƒ d S )Nr;   r<   r=   r„   Tr‰   )	r   rC   r
   r   rˆ   r9   r   r   r   rM   r   r   r   Ú,test_pressure_network_with_gradient_combinedç   s    þz9TestRootHybr.test_pressure_network_with_gradient_combinedN)r   r   r    rJ   rN   rŠ   r   r   r   r   rƒ   Ó   s   	
rƒ   c                   @   s   e Zd Zdd„ ZdS )Ú
TestRootLMc                 C   sH   t  dd¡}d}tg d¢ƒ}tjt|d||fd�j}t|t  d¡ƒ d S )Nr;   r<   r=   Úlmr…   r‡   rM   r   r   r   rJ   ô   s    ÿz,TestRootLM.test_pressure_network_no_gradientN)r   r   r    rJ   r   r   r   r   r‹   ó   s   r‹   c                   @   s¤   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'S )(ÚTestLeastSqc                 C   sj   t  ddd¡}d\}}}|| _|||f| _||d  ||  | }t j d¡ |dt j |j¡  | _d S )Nr   rs   é(   )gÍÌÌÌÌÌ@é*   g33333sÀr+   g{®Gáz„?)	r   Úlinspacer   ÚabcÚrandomÚseedZstandard_normalr   Úy_meas)r   r   ÚaÚbÚcZy_truer   r   r   Úsetup_methodÿ   s    
zTestLeastSq.setup_methodc                 C   s*   |\}}}|||d  ||  |  }|S ©Nr+   r   )r   r|   Úyr   r•   r–   r—   Úerrr   r   r   Ú	residuals  s    
zTestLeastSq.residualsc                 C   s   t  |d |t  |¡g¡j S r™   )r   ÚvstackZ	ones_likeÚT)r   Z_pZ_yr   r   r   r   Úresiduals_jacobian  s    zTestLeastSq.residuals_jacobianc                 C   sN   t g d¢ƒ}t| j|| j| jfd�\}}t|dv d| ƒ t|| jdd� d S )N©r   r   r   rZ   ©r,   r+   rf   r;   úsolution not found (ier=%d)r+   ©Údecimal©r
   r   rœ   r”   r   r   r   r‘   ©r   Úp0Ú
params_fitrH   r   r   r   Ú
test_basic  s    
ÿ
zTestLeastSq.test_basicc                 C   sR   t g d¢ƒ}t| j|| j| jf| jd�\}}t|dv d| ƒ t|| jdd� d S )Nr    ©rA   ÚDfunr¡   r¢   r+   r£   )	r
   r   rœ   r”   r   rŸ   r   r   r‘   r¦   r   r   r   Útest_basic_with_gradient  s    
þ
z$TestLeastSq.test_basic_with_gradientc                 C   sL   t g d¢gƒ}t| j|| j| jfdd�}|\}}}}}t|dv d| ƒ d S )Nr    Tr@   r¡   úsolution not found: %s)r
   r   rœ   r”   r   r   )r   r§   rB   r¨   Úcov_xÚinfodictrI   rH   r   r   r   Útest_full_output!  s    
þzTestLeastSq.test_full_outputc           	      C   sd   t g d¢td�}t |dd�}t| j|| j| jfdd�}|\}}}}}t|dv d| ƒ t||ƒ d S )Nr    rw   T)Úcopyr@   r¡   r­   )r
   r   r   rœ   r”   r   r   r   )	r   r§   Zp0_copyrB   r¨   r®   r¯   rI   rH   r   r   r   Útest_input_untouched)  s    
þz TestLeastSq.test_input_untouchedc                 C   s$   t dƒ}ddg}tttj||ƒ d S rO   ©r   rR   rS   r   r   rT   r   r   r   rW   3  s    z*TestLeastSq.test_wrong_shape_func_callablec                 C   s    ddg}t ttjt|dd� d S rX   )rR   rS   r   r   r"   r[   r   r   r   r\   :  s    z*TestLeastSq.test_wrong_shape_func_functionc                 C   s,   t dƒ}t dƒ}tttj|ddg|d� d S )Nr,   r]   r   ©rV   r«   r³   r_   r   r   r   Útest_wrong_shape_Dfun_callable@  s    z*TestLeastSq.test_wrong_shape_Dfun_callablec                 C   s,   dd„ }dd„ }t ttj|ddg|d� d S )Nc                 S   s
   t | dƒS rb   rc   rd   r   r   r   r$   F  r%   z<TestLeastSq.test_wrong_shape_Dfun_function.<locals>.<lambda>c                 S   s
   t | dƒS re   rc   rd   r   r   r   r$   G  r%   r   r,   r´   ©rR   rS   r   r   r_   r   r   r   Útest_wrong_shape_Dfun_functionE  s    z*TestLeastSq.test_wrong_shape_Dfun_functionc                 C   s–   dd„ }t jg d¢t jd�}t jg d¢t jd�}t  g d¢¡}tj||||fd�\}}t|dv ƒ t||||ƒd	  ¡ d
||||ƒd	  ¡  k ƒ d S )Nc                 S   s@   | d t  || d  d  d| d d   ¡ | d  }|| S )Nr   r,   r+   r>   rf   ©r   Úexp)r|   r   rš   Úqr   r   r   rU   L  s    8z&TestLeastSq.test_float32.<locals>.func)
gš™™™™™÷?gw¾Ÿ/Ýö?g%�•C‹ö?gNbX9´ö?gHáz®G÷?gçû©ñÒMø?gZd;ßO�÷?gÙÎ÷Sãõ?gú~j¼t“ô?gV-²ó?rw   )
gŒJê4‘?g¥N@aÃ“?g^KÈ=›•?gM„O¯”?gßà“©‚‘?g?W[±¿ìŽ?g‹lçû©ñ’?g³{ò°Pkš?gœÄ °rh¡?gõÛ×�sF¤?)ç      ð?r»   r»   r»   rZ   r¡   r+   g-Cëâ6?)r   r
   rx   r   r   r   r-   )r   rU   r   rš   r§   Úp1Úsuccessr   r   r   r}   J  s    
ÿ
ÿzTestLeastSq.test_float32c                 C   sH   dd„ }t tdd��  tj|dgd� W d   ƒ n1 s:0    Y  d S )Nc                  W   s   t dƒ‚d S rh   rj   rZ   r   r   r   rU   [  s    z-TestLeastSq.test_func_can_raise.<locals>.funcri   rl   r   rn   ©rR   rk   r   r   rp   r   r   r   rq   Z  s    zTestLeastSq.test_func_can_raisec                 C   sR   dd„ }dd„ }t tdd��" tj|dg|d� W d   ƒ n1 sD0    Y  d S )	Nc                 S   s   | t  dg¡ S rr   rt   rd   r   r   r   r$   b  r%   z1TestLeastSq.test_Dfun_can_raise.<locals>.<lambda>c                  W   s   t dƒ‚d S rh   rj   rZ   r   r   r   r`   d  s    z3TestLeastSq.test_Dfun_can_raise.<locals>.deriv_funcri   rl   r   r´   r¾   r_   r   r   r   ru   a  s    zTestLeastSq.test_Dfun_can_raisec                    sX   ‡ fdd„}t g d¢ƒ}t||ˆ jˆ jfd�\}}t|dv d| ƒ t|ˆ jdd� d S )	Nc                     s   ˆ   ¡  ˆ j| Ž S r   )r©   rœ   rZ   r~   r   r   rU   k  s    z-TestLeastSq.test_reentrant_func.<locals>.funcr    rZ   r¡   r¢   r+   r£   )r
   r   r”   r   r   r   r‘   )r   rU   r§   r¨   rH   r   r~   r   r   j  s    
ÿ
zTestLeastSq.test_reentrant_funcc                    s\   ‡ fdd„}t g d¢ƒ}tˆ j|ˆ jˆ jf|d�\}}t|dv d| ƒ t|ˆ jdd� d S )	Nc                     s   ˆ   ¡  ˆ j| Ž S r   )r©   rŸ   rZ   r~   r   r   r`   w  s    z3TestLeastSq.test_reentrant_Dfun.<locals>.deriv_funcr    rª   r¡   r¢   r+   r£   r¥   )r   r`   r§   r¨   rH   r   r~   r   Útest_reentrant_Dfunv  s    
þ
zTestLeastSq.test_reentrant_Dfunc                 C   s   t | jgd ƒS rr   )r*   r©   r~   r   r   r   r�   ƒ  s    z'TestLeastSq.test_concurrent_no_gradientc                 C   s   t | jgd ƒS rr   )r*   r¬   r~   r   r   r   r‚   †  s    z)TestLeastSq.test_concurrent_with_gradientc                 C   sJ   dd„ }t tdd��" tj|ddgd� W d   ƒ n1 s<0    Y  d S )Nc                 S   s   d| d d d  d S )Nr+   r   rf   r,   r   rd   r   r   r   rU   ‹  s    z=TestLeastSq.test_func_input_output_length_check.<locals>.funcz+Improper input: func input vector length N=rl   r   r,   rn   r¶   rp   r   r   r   Ú#test_func_input_output_length_check‰  s
    ÿz/TestLeastSq.test_func_input_output_length_checkN)r   r   r    r˜   rœ   rŸ   r©   r¬   r°   r²   rW   r\   rµ   r·   r}   rq   ru   r   r¿   r�   r‚   rÀ   r   r   r   r   r�   þ   s&   		
	r�   c                   @   sÔ   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3S )4ÚTestCurveFitc                 C   s    t g d¢ƒ| _t g d¢ƒ| _d S )N)r»   gš™™™™™	@ç      #@gffffff+@)r»   r>   ç      @g      @)r
   rš   r   r~   r   r   r   r˜   ”  s    zTestCurveFit.setup_methodc           
      C   s�   dd„ }t || j| jƒ\}}tt|ƒdkƒ t|jdkƒ t|d ddd� t|d	 d
dd� t || j| jddd�}|\}}}}}	t||ƒ d S )Nc                 S   s   | | S r   r   ©r   r•   r   r   r   rU   ™  s    z,TestCurveFit.test_one_argument.<locals>.funcr,   )r,   r,   r   g3Ä±.n£þ?r;   r£   )r   r   g-Cëâ6Z?F)rB   Úcheck_finite)r   r   rš   r   r&   r   r   r   )
r   rU   ÚpoptÚpcovÚresÚpopt2Úpcov2r¯   ÚerrmsgrH   r   r   r   Útest_one_argument˜  s    ÿzTestCurveFit.test_one_argumentc                 C   sj   dd„ }t || j| jƒ\}}tt|ƒdkƒ t|jdkƒ t|ddgdd� t|d	d
gd
dggdd� d S )Nc                 S   s   || |  S r   r   ©r   r•   r–   r   r   r   rU   ©  s    z,TestCurveFit.test_two_argument.<locals>.funcr+   r]   çñcÌ]KÈü?çà-� ò?r;   r£   çäƒžÍªÏµ?çTã¥›Ä À¿ç·bÙ=yÈ?)r   r   rš   r   r&   r   r   )r   rU   rÆ   rÇ   r   r   r   Útest_two_argument¨  s    ÿzTestCurveFit.test_two_argumentc                 C   sh   G dd„ dƒ}|ƒ }t |j| j| jƒ\}}t|jdkƒ t|ddgdd� t|dd	gd	d
ggdd� d S )Nc                   @   s   e Zd ZdZdd„ ZdS )z8TestCurveFit.test_func_is_classmethod.<locals>.test_selfz”This class tests if curve_fit passes the correct number of
               arguments when the model function is a class instance method.
            c                 S   s   |||  S r   r   )r   r   r•   r–   r   r   r   rU   ¸  s    z=TestCurveFit.test_func_is_classmethod.<locals>.test_self.funcN)r   r   r    r!   rU   r   r   r   r   Ú	test_self³  s   rÔ   r]   rÎ   rÏ   r;   r£   rÐ   rÑ   rÒ   )r   rU   r   rš   r   r   r   )r   rÔ   Ztest_self_instrÆ   rÇ   r   r   r   Útest_func_is_classmethod²  s    ÿz%TestCurveFit.test_func_is_classmethodc                 C   sP   g d¢}g d¢}g d¢}g d¢}dd„ }t ||||dd�\}}t||d	d
� d S )N)
gB`åÐ"ñ�@g¬Zd;ñ�@g¸…ëQñ�@g#Ûù~jñ�@g/Ý$�ñ�@g;ßO�—ñ�@g¦›Ä °ñ�@g²�ï§Æñ�@gZd;ßñ�@g)\�Âõñ�@)
ç     ØŠ@g     (�@g     Œš@g     X¤@g     tŸ@g     °ž@g     £@g     8˜@g     ¨�@g     HŠ@)g€^w8}ñ�@gU]ƒ}¹ñ�@ç     X”@r×   g2ï9°l?rÖ   )gäƒžÍjñ�@gºÂj¬ñ�@g…”ŸT{7›@g|'f½ØÉ˜@g ‘Æž„?g©„'ôšËŠ@c                 S   sL   |t  | | d  d|d   ¡ |t  | | d  d|d   ¡  | S )Nr+   r>   r¸   )r   rV   Úx1ZA0ÚA1Úsigmar—   r   r   r   Úf_double_gaussÏ  s
    ""ÿÿz9TestCurveFit.test_regression_2639.<locals>.f_double_gaussi'  )Úmaxfevgñhãˆµøä>©Úrtol)r   r   )r   r   rš   ÚguessZgoodrÛ   rÆ   rÇ   r   r   r   Útest_regression_2639Â  s    z!TestCurveFit.test_regression_2639c              
   C   s  t  g d¢¡}t  g d¢¡}t  g d¢¡}dd„ }dD �]}t|||ddg||d	�\}}t  t  |¡¡}t|d
dgdd� t|||ddgd| |d	�\}}t  t  |¡¡}t|d
dgdd� t|||ddg|d|d�\}}t  t  |¡¡}	t|	ddgdd� t|||ddgd| d|d�\}}t  t  |¡¡}	t|	ddgdd� q6dd„ }
t  t jgd ¡ dd¡}tƒ �^}| 	t
d¡ t|
||ddg|d�\}}t||d d… |d d… ddgd�\}}W d   ƒ n1 �sÐ0    Y  t|jdkƒ t||ƒ t|jdkƒ t||ƒ d S )N)r   r,   r+   rf   r;   é   )r,   r,   rá   é   é   é   )r,   r+   r,   r+   r,   r+   c                 S   s   ||  | S r   r   rÍ   r   r   r   r#   Ú  s    z!TestCurveFit.test_pcov.<locals>.f©rŒ   ÚtrfÚdogboxr+   r   )r§   rÚ   r†   g´N"ãÍqÊ?g²~uM/Nâ?ry   rÝ   rf   T)r§   rÚ   Úabsolute_sigmar†   g´ÁO=N¨Ó?g5=¬`é6ë?gŽ¢÷[u|í?gè-�/i@c                 S   s   ||  S r   r   rÍ   r   r   r   Úf_flatô  s    z&TestCurveFit.test_pcov.<locals>.f_flatr;   z3Covariance of the parameters could not be estimated)r§   rÚ   ©r§   r]   )r   r
   r   Úsqrtr4   r   ÚinfZreshaper   Úfilterr   r   r   r   )r   ÚxdataÚydatarÚ   r#   r†   rÆ   rÇ   Zperr_scaledZperrré   Zpcov_expectedÚsupÚpopt1Úpcov1r   r   r   Ú	test_pcovÕ  sJ    
ÿ
ÿ
ÿ
ÿ
ÿH
zTestCurveFit.test_pcovc                 C   s:   dd„ }g d¢}g d¢}t t|||ƒd ddgdd	� d S )
Nc                 S   s   ||  | S r   r   rÍ   r   r   r   Úf_linear  s    z.TestCurveFit.test_array_like.<locals>.f_linearr¡   )rf   rá   râ   é	   r   r+   r,   g»½×Ùß|Û=rz   )r   r   )r   rô   r   rš   r   r   r   Útest_array_like  s    zTestCurveFit.test_array_likec                 C   s4   t  g d¢¡}t  g d¢¡}tttdd„ ||ƒ d S )N)r,   r+   rf   r;   rá   é   )r,   r+   rf   r;   g      @r÷   c                 S   s   ||  S r   r   rÍ   r   r   r   r$     r%   z<TestCurveFit.test_indeterminate_covariance.<locals>.<lambda>)r   r
   r   r   r   ©r   rî   rï   r   r   r   Útest_indeterminate_covariance  s
    
ÿz*TestCurveFit.test_indeterminate_covariancec                 C   sl   t  dt jdg¡}t  g d¢¡}tttdd„ ||ƒ tttdd„ ||ƒ tttdd„ ||fi dd	i¤Ž d S )
Nr,   rf   )r,   r+   rf   c                 S   s   ||  | S r   r   rÍ   r   r   r   r$     r%   z0TestCurveFit.test_NaN_handling.<locals>.<lambda>c                 S   s   ||  | S r   r   rÍ   r   r   r   r$     r%   c                 S   s   ||  | S r   r   rÍ   r   r   r   r$   !  r%   rÅ   T)r   r
   ÚnanrR   rk   r   rø   r   r   r   Útest_NaN_handling  s    
ÿ
ÿÿÿzTestCurveFit.test_NaN_handlingc                 C   s`   t ttdd„ g g ƒ t ttdd„ g g dd� t ttdd„ dgg ƒ t ttdd„ d	gg dd� d S )
Nc                 S   s   ||  S r   r   rÄ   r   r   r   r$   &  r%   z0TestCurveFit.test_empty_inputs.<locals>.<lambda>c                 S   s   ||  S r   r   rÄ   r   r   r   r$   '  r%   )r,   r+   )Úboundsc                 S   s   ||  S r   r   rÄ   r   r   r   r$   )  r%   r,   c                 S   s   ||  S r   r   rÄ   r   r   r   r$   *  r%   r+   ©rR   rk   r   r~   r   r   r   Útest_empty_inputs$  s    ÿÿzTestCurveFit.test_empty_inputsc                 C   s    t ttdd„ ddgddgƒ d S )Nc                 S   s   | S r   r   rd   r   r   r   r$   /  r%   z8TestCurveFit.test_function_zero_params.<locals>.<lambda>r,   r+   rf   r;   rý   r~   r   r   r   Útest_function_zero_params-  s    z&TestCurveFit.test_function_zero_paramsc                 C   s.   t dd„ d dt d¡ ƒ\}}t|dgƒ d S )Nc                 S   s   |t  d¡ S rr   )r   Úarange)Ú_r•   r   r   r   r$   2  r%   z*TestCurveFit.test_None_x.<locals>.<lambda>r+   rs   r>   )r   r   r   r   )r   rÆ   rÇ   r   r   r   Útest_None_x1  s    ÿzTestCurveFit.test_None_xc                 C   sf   dd„ }t  ddd¡}||ddƒ}dD ]&}t||||d�\}}t|ddgƒ q&ttt|||d	d� d S )
Nc                 S   s   |t  | |  ¡ S r   r¸   rÍ   r   r   r   r#   7  s    z,TestCurveFit.test_method_argument.<locals>.fr   r,   é   r>   ©ræ   rç   rŒ   N©r†   Úunknown)r   r�   r   r   rR   rk   )r   r#   rî   rï   r†   rÆ   rÇ   r   r   r   Útest_method_argument6  s    z!TestCurveFit.test_method_argumentc           
      C   sÀ   dd„ }t  ddd¡}||ddƒ}dD ]”}t||||dd	�\}}}}}	t|ddgƒ d
|v s`J ‚d|v slJ ‚|dks||d u r d|v sˆJ ‚d|v s”J ‚d|v s J ‚t|tƒs®J ‚|	dv s&J ‚q&d S )Nc                 S   s   |t  | |  ¡ S r   r¸   rÍ   r   r   r   r#   D  s    z(TestCurveFit.test_full_output.<locals>.fr   r,   r  r>   r  T)r†   rB   ZnfevZfvecrŒ   ZfjacZipvtZqtfr¡   )r   r�   r   r   Ú
isinstanceÚstr)
r   r#   rî   rï   r†   rÆ   rÇ   r¯   rË   rH   r   r   r   r°   C  s     
ÿzTestCurveFit.test_full_outputc              	   C   s¬   dd„ }t  ddd¡}||ddƒ}ddgdd	gf}d
D ](}t|||||d�\}}t|d dƒ q6t|||dddgdt jgfd�\}}t|d dƒ ttt||||dd� d S )Nc                 S   s   |t  | |  ¡ S r   r¸   rÍ   r   r   r   r#   X  s    z#TestCurveFit.test_bounds.<locals>.fr   r,   r  r>   r»   rP   rÃ   )Nræ   rç   )rü   r†   ræ   r?   g333333ã?)r†   rü   rŒ   )r   r�   r   r   rì   rR   rk   )r   r#   rî   rï   rü   r†   rÆ   rÇ   r   r   r   Útest_boundsW  s     
ÿ

ÿ
ÿzTestCurveFit.test_boundsc           	   	   C   s�   dd„ }t  dt j dt j d¡}t  |¡}dt j dt j f}dD ]F}t|||d	t j d
�\}}t|||d	t j ||d�\}}t||ƒ qDd S )Nc                 S   s   t  | | ¡S r   )r   ÚsinrÄ   r   r   r   r#   r  s    z&TestCurveFit.test_bounds_p0.<locals>.féþÿÿÿr+   rŽ   éýÿÿÿrf   ©ræ   rç   gÍÌÌÌÌÌ @rê   )r§   rü   r†   )r   r�   Úpir  r   r   )	r   r#   rî   rï   rü   r†   Zpopt_1r  Zpopt_2r   r   r   Útest_bounds_p0o  s    
ÿ
zTestCurveFit.test_bounds_p0c           
   	   C   sè   dd„ }dd„ }t  ddd¡}||ddƒ}d	D ]2}d
D ](}t|||||d�\}}t|ddgƒ q6q.dD ](}t|||||d�\}}t|ddgƒ qfd|d< t  |jd ¡}	d|	d< dD ].}t||||	||d�\}}t|ddgdd� q´d S )Nc                 S   s   |t  | |  ¡ S r   r¸   rÍ   r   r   r   r#   ƒ  s    z TestCurveFit.test_jac.<locals>.fc                 S   s*   t  | |  ¡}t  || |  | f¡jS r   ©r   r¹   r�   rž   ©r   r•   r–   Úer   r   r   r7   †  s    z"TestCurveFit.test_jac.<locals>.jacr   r,   r  r>   r  )z2-pointz3-pointÚcs)r7   r†   r+   rå   )r†   r7   rv   rá   éÈ   )rÚ   r†   r7   ry   rÝ   )r   r�   r   r   r   r   )
r   r#   r7   rî   rï   r†   ÚschemerÆ   rÇ   rÚ   r   r   r   Útest_jac€  s*    
ÿ
ÿ
zTestCurveFit.test_jacc                 C   sh   t  dd¡}d| }tdd„ ||ddd�\}}td	d„ ||ddd
�\}}t|ddd� t|ddd� d S )Nr   rs   r+   c                 S   s   ||  S r   r   ©r   r|   r   r   r   r$   ©  r%   z5TestCurveFit.test_maxfev_and_bounds.<locals>.<lambda>)r   rf   rv   )rü   rÜ   c                 S   s   ||  S r   r   r  r   r   r   r$   ª  r%   )rü   Zmax_nfevç›+¡†›„=rz   )r   r   r   r   )r   r   rš   rñ   r  rÉ   r   r   r   Útest_maxfev_and_bounds¤  s    z#TestCurveFit.test_maxfev_and_boundsc              
   C   sà   dd„ }dd„ }t j d¡ t  ddd¡}||dd	ƒ}|d
t jjt|ƒd�  }t  t|ƒ¡d
 }t  |d ¡}||fdfD ]^\}}	dD ]P}
t||||||
d�\}}t|||||	|
d�\}}t	||dd� t	||dd� qˆq|d S )Nc                 S   s   |t  | |  ¡ S r   r¸   rÍ   r   r   r   rU   ±  s    z9TestCurveFit.test_curvefit_simplecovariance.<locals>.funcc                 S   s*   t  | |  ¡}t  || |  | f¡jS r   r  r  r   r   r   r7   ´  s    z8TestCurveFit.test_curvefit_simplecovariance.<locals>.jacr   r;   é2   ç      @çÍÌÌÌÌÌô?çš™™™™™É?©Úsizer+   ©NN©FT©rÚ   r7   rè   r  rz   )
r   r’   r“   r�   Únormalr&   Úzerosr4   r   r   )r   rU   r7   rî   rš   rï   rÚ   ÚcovarÚjac1Újac2rè   rñ   rò   rÉ   rÊ   r   r   r   Útest_curvefit_simplecovariance¯  s$    
ÿ

ÿ
z+TestCurveFit.test_curvefit_simplecovariancec              
   C   sV  dd„ }dd„ }dd„ }dd„ }t j d	¡ t  d
d¡}||ddƒ}|dt jjt|ƒd�  }t  t|ƒ¡d }t  |d ¡}	t  dt  	d¡ dt  	d¡ d	gdt  	d¡ dt  	d¡ d	gg d¢g¡}
|
 
|¡}|
 
|	¡ 
|
j¡}||fdfD ]b\}}dD ]T}t||||||d�\}}t||||||d�\}}t||ddd� t||ddd� qúqîd S )Nc                 S   sb   t  dt  d¡ dt  d¡ dgdt  d¡ dt  d¡ dgg d¢g¡}| |t  | |  ¡ ¡S ©Nr»   r+   ç      ð¿r   ©r   r   r»   )r   r
   rë   Údotr¹   )r   r•   r–   Úrotnr   r   r   ÚfuncpÌ  s    Hz4TestCurveFit.test_curvefit_covariance.<locals>.funcpc                 S   sx   t  dt  d¡ dt  d¡ dgdt  d¡ dt  d¡ dgg d¢g¡}t  | |  ¡}| t  || |  | f¡j¡S r*  )r   r
   rë   r¹   r-  r�   rž   )r   r•   r–   r.  r  r   r   r   ÚjacpÐ  s    Hz3TestCurveFit.test_curvefit_covariance.<locals>.jacpc                 S   s   |t  | |  ¡ S r   r¸   rÍ   r   r   r   rU   Õ  s    z3TestCurveFit.test_curvefit_covariance.<locals>.funcc                 S   s*   t  | |  ¡}t  || |  | f¡jS r   r  r  r   r   r   r7   Ø  s    z2TestCurveFit.test_curvefit_covariance.<locals>.jacr   r,   r;   r  r»   r  r  r+   r+  r,  r!  r"  r#  g+i¤)+€>r  )rÞ   r{   )r   r’   r“   r   r$  r&   r%  r4   r
   rë   r-  rž   r   r   )r   r/  r0  rU   r7   rî   rš   rï   rÚ   r&  r.  ZydatapZcovarpr'  r(  rè   rñ   rò   rÉ   rÊ   r   r   r   Útest_curvefit_covarianceÊ  s.    H

ÿ

ÿ
z%TestCurveFit.test_curvefit_covariancec           	   
   C   sÞ   t  dd¡}d| d dt  |¡  }dd„ }dD ]¦}t jt jfD ]”}t jt jfD ]}| |¡}| |¡}qRt ¡ �R t d	t	¡ t
||||d
�\}}t  |¡ ¡ s¨J ‚t  |d¡r¸J ‚W d   ƒ qB1 sÌ0    Y  qBq2d S )Nr  rá   rP   rÃ   r<   c                 S   s   ||  | S r   r   rÍ   r   r   r   rU   û  s    z&TestCurveFit.test_dtypes.<locals>.funcrå   Úerrorr  r,   )r   r   r  rx   r   ÚastypeÚwarningsÚcatch_warningsÚsimplefilterr   r   ÚisfiniteÚallZallclose)	r   r   rš   rU   r†   ZdtxZdtyr|   Zcovr   r   r   Útest_dtypesö  s    

zTestCurveFit.test_dtypesc                 C   sº   dd„ }t  g d¢¡}t  g d¢¡}t  g d¢¡}g d¢}t  g d¢¡}||g|¢R Ž }t||||||fd�\}}	| t j¡}||g|¢R Ž }t||||||fd�\}
}	t|
|d	d
� d S )Nc                 S   sJ   || d }|| d }||| |   |t  | | d |d d  ¡  S )Nr+   r;   )r   rë   )r   Zs_1Zs_2Zo_xZo_yr—   Zb_2Zb_1r   r   r   Ú	hyperbola  s    z,TestCurveFit.test_dtypes2.<locals>.hyperbola)g      Àr?   ç       Àg      $Àr?   )r?   rÃ   rÃ   r?   g      $@)g«ªªªªªê¿gUUUUUUõ?r»   g      Àr<   )r  gš™™™™™Ù?éÿÿÿÿéûÿÿÿrÂ   )iàÿÿÿiðÿÿÿiøÿÿÿr;   r;   rã   é   é    )r#   rî   rï   r§   rü   gñhãˆµøô>rz   )r   r
   r   r3  rx   r   )r   r:  Zmin_fitZmax_fitrß   Úparamsrî   rï   Zpopt_64r  Zpopt_32r   r   r   Útest_dtypes2  s     
ÿ

ÿ
zTestCurveFit.test_dtypes2c                    s€   t  d¡}d|d  d|  t j t|ƒ¡ ‰ ‡ fdd„}dD ]<}t||t  |¡|d�\}}t||d	|d�\}}t||ƒ q>d S )
Nrs   gÍÌÌÌÌÌ@r+   g      @c                    s   || d  ||   ˆ  S r™   r   rÍ   ©Útargetr   r   r$   *  r%   z/TestCurveFit.test_broadcast_y.<locals>.<lambda>rå   )rî   rï   r†   r   )r   r   r’   Zrandr&   r   Z
zeros_liker   )r   rî   Zfit_funcr†   Zpopt0Zpcov0rñ   rò   r   rB  r   Útest_broadcast_y'  s    
$ý
ý
zTestCurveFit.test_broadcast_yc                 C   sP   dd„ }t tƒ�, t|g d¢g d¢dgdd� W d   ƒ n1 sB0    Y  d S )Nc                 S   s   ||  | S r   r   rÍ   r   r   r   rU   9  s    z.TestCurveFit.test_args_in_kwargs.<locals>.funcr¡   ©rá   rõ   é   é   r,   rY   )rî   rï   r§   rA   rý   rp   r   r   r   Útest_args_in_kwargs6  s    
üz TestCurveFit.test_args_in_kwargsc                 C   sN   dd„ }t tdd��& t|g d¢g d¢d� W d   ƒ n1 s@0    Y  d S )Nc                 S   s    |t  | |  ¡ | | | S r   r¸   )r   r•   r–   r—   Údr  r   r   r   rU   D  s    z<TestCurveFit.test_data_point_number_validation.<locals>.funczThe number of func parameters=rl   r¡   rE  )rî   rï   )rR   rS   r   rp   r   r   r   Ú!test_data_point_number_validationC  s    þz.TestCurveFit.test_data_point_number_validationN)r   r   r    r˜   rÌ   rÓ   rÕ   rà   ró   rö   rù   rû   rþ   rÿ   r  r  r°   r
  r  r  r  r)  r1  r9  rA  rD  rH  rJ  r   r   r   r   rÁ   “  s2   
0		$,rÁ   c                   @   sL   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dS )ÚTestFixedPointc                 C   s$   dd„ }d}t ||ƒ}t|dƒ d S )Nc                 S   s   d|  S ©Nr>   r   rd   r   r   r   rU   Q  s    z0TestFixedPoint.test_scalar_trivial.<locals>.funcr»   r?   ©r   r   ©r   rU   rV   r   r   r   r   Útest_scalar_trivialO  s    
z"TestFixedPoint.test_scalar_trivialc                 C   s$   dd„ }d}t ||ƒ}t|dƒ d S )Nc                 S   s   | d S r™   r   rd   r   r   r   rU   Y  s    z/TestFixedPoint.test_scalar_basic1.<locals>.funcçÍÌÌÌÌÌð?r»   rM  rN  r   r   r   Útest_scalar_basic1W  s    
z!TestFixedPoint.test_scalar_basic1c                 C   s$   dd„ }d}t ||ƒ}t|dƒ d S )Nc                 S   s   | d S ©Nr<   r   rd   r   r   r   rU   a  s    z/TestFixedPoint.test_scalar_basic2.<locals>.funcrP  r»   rM  rN  r   r   r   Útest_scalar_basic2_  s    
z!TestFixedPoint.test_scalar_basic2c                 C   sX   dd„ }ddg}t jdd�� t||ƒ}W d   ƒ n1 s<0    Y  t|ddgƒ d S )Nc                 S   s   d|  S rL  r   rd   r   r   r   rU   h  s    z/TestFixedPoint.test_array_trivial.<locals>.funcg333333Ó?g333333Ã?Úignore©r8  r?   )r   Úerrstater   r   rN  r   r   r   Útest_array_trivialg  s
    (z!TestFixedPoint.test_array_trivialc                 C   sj   dd„ }t g d¢ƒ}g d¢}tjdd��  t|||fd�}W d   ƒ n1 sN0    Y  t|d| ƒ d S )	Nc                 S   s   || d  S r™   r   ©r   r—   r   r   r   rU   q  s    z.TestFixedPoint.test_array_basic1.<locals>.func©g      è?r»   g      ô?)çš™™™™™ñ?gffffffò?gÍÌÌÌÌÌì?rT  rU  rZ   r»   )r
   r   rV  r   r   ©r   rU   r—   rV   r   r   r   r   Útest_array_basic1o  s    .z TestFixedPoint.test_array_basic1c                 C   s>   dd„ }t g d¢ƒ}g d¢}t|||fd�}t||d ƒ d S )Nc                 S   s   || d  S rR  r   rX  r   r   r   rU   {  s    z.TestFixedPoint.test_array_basic2.<locals>.funcrY  )gš™™™™™é?rZ  rZ  rZ   r+   )r
   r   r   r[  r   r   r   Útest_array_basic2y  s
    z TestFixedPoint.test_array_basic2c                 C   sD   t dd„ ddddd�}t|t d| ¡d	 ƒ t|td
ƒd ƒ d S )Nc                 S   s   t  d|  ¡d S )Nr;  r>   r¸   )Úxxr   r   r   r$   „  r%   z.TestFixedPoint.test_lambertw.<locals>.<lambda>r»   r   gê-�™—q=iô  )rA   ZxtolÚmaxiterr;  r>   r,   r+   )r   r   r   r¹   r   )r   Zxxrootr   r   r   Útest_lambertw‚  s
    
ÿzTestFixedPoint.test_lambertwc                    s`   d‰d‰d}d}|d | ˆˆ | d|d    ‰ ‡ ‡‡fdd„}t ||dd	�}t||ƒ d S )
Nr+   r÷   r  gj¼t“ð?r,   c                    s,   t  ˆˆ |  ¡t  ˆ |  | d  ¡ d S )Nr,   )r   Úlog)r6   ©Zi0ÚklÚksr   r   rU   ‘  s    z1TestFixedPoint.test_no_acceleration.<locals>.funcÚ	iterationr  )r   r   )r   ÚmZn0rU   r6   r   rb  r   Útest_no_acceleration‰  s    $z#TestFixedPoint.test_no_accelerationN)r   r   r    rO  rQ  rS  rW  r\  r]  r`  rg  r   r   r   r   rK  M  s   
	rK  ))r!   r4  Znumpy.testingr   r   r   r   r   r   r   Zpytestr	   rR   Únumpyr   r
   r   Zmultiprocessing.poolr   Zscipyr   Zscipy.specialr   Zscipy.optimize._minpack_pyr   r   r   Zscipy.optimizer   r   r"   r*   r3   r8   r9   r:   rƒ   r‹   r�   rÁ   rK  r   r   r   r   Ú<module>   s4   $	f     =