U
    /d                     @   s>  d Z ddl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mZ ddlmZ dd	lmZmZmZ dd
l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linalg)lambertw)leastsq	curve_fitfixed_point)OptimizeWarning)Boundsc                   @   s    e Zd ZdZdd Zdd ZdS )ReturnShapezThis 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   E/tmp/pip-unpacked-wheel-9gxwnfpp/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   s4   t t| }|dd | W  5 Q R  S Q R X 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_network4   s    &r5   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   )r0   r1   r2   nZpdiffjacr   r   r   pressure_network_jacobianS   s    6 r:   c                 C   s   t | ||t| ||fS r   )r5   r:   )r0   r1   r2   r   r   r   pressure_network_fun_and_gradj   s    

r;   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ddddg}tjt|||fdd\}}}}t|t d t|dk| d S )N         ?       @        Targsfull_outputr.   )	r   fullr
   r   fsolver5   r   r   r   )r   r2   r1   initial_guessfinal_flowsinfoiermesgr   r   r   !test_pressure_network_no_gradientp   s      z,TestFSolve.test_pressure_network_no_gradientc                 C   sJ   t dd}d}tddddg}tjt|||ftd}t|t d d S )Nr=   r>   r?   r@   rB   fprime)	r   rD   r
   r   rE   r5   r:   r   r   r   r2   r1   rF   rG   r   r   r   #test_pressure_network_with_gradient{   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   rE   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 NrQ   r?   )r.   rB   )rS   rT   r   rE   r$   r   rW   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   rW   rM   rR   r   rV   
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)   rg   rd   re   r   r   r   r&      r'   r   r.   r_   )rS   rT   r   rE   r`   r   r   r    test_wrong_shape_fprime_function   s    z+TestFSolve.test_wrong_shape_fprime_functionc              	   C   s4   dd }t tdd tj|dgd W 5 Q R X d S )Nc                  W   s   t dd S NI raised
ValueErrorr[   r   r   r   rV      s    z,TestFSolve.test_func_can_raise.<locals>.funcrj   matchr   rW   rS   rl   r   rE   r   rV   r   r   r   test_func_can_raise   s    zTestFSolve.test_func_can_raisec              	   C   s>   dd }dd }t tdd tj|dg|d W 5 Q R X d S )	Nc                 S   s   | t dg S N
   r   r
   re   r   r   r   r&      r'   z0TestFSolve.test_Dfun_can_raise.<locals>.<lambda>c                  W   s   t dd S ri   rk   r[   r   r   r   ra      s    z2TestFSolve.test_Dfun_can_raise.<locals>.deriv_funcrj   rm   r   r_   rp   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
   float32re   r   r   r   r&      r'   z)TestFSolve.test_float32.<locals>.<lambda>r.   r   MbP?atol)r   rE   r   r
   ry   r   )r   rV   pr   r   r   test_float32   s    zTestFSolve.test_float32c           	         sl    fdd}t dd}d}tddddg}tj||||fdd\}}}}t|t d t|d	k| d S )
Nc                     s       t|  S r   )rK   r5   r[   r   r   r   rV      s    z,TestFSolve.test_reentrant_func.<locals>.funcr=   r>   r?   r@   TrA   r.   )r   rD   r
   r   rE   r   r   r   )	r   rV   r2   r1   rF   rG   rH   rI   rJ   r   r   r   test_reentrant_func   s      zTestFSolve.test_reentrant_funcc                    sV    fdd}t dd}d}tddddg}tjt|||f|d}t|t d d S )Nc                     s       t|  S r   )rO   r:   r[   r   r   r   ra      s    z3TestFSolve.test_reentrant_Dfunc.<locals>.deriv_funcr=   r>   r?   r@   rL   )r   rD   r
   r   rE   r5   r   r   )r   ra   r2   r1   rF   rG   r   r   r   test_reentrant_Dfunc   s      zTestFSolve.test_reentrant_Dfuncc                 C   s*   t | jgd }tdd |D s&td S )Nrt   c                 S   s   g | ]}|d kqS r   r   .0resultr   r   r   
<listcomp>   s     z:TestFSolve.test_concurrent_no_gradient.<locals>.<listcomp>)r,   rK   allAssertionErrorr   vr   r   r   test_concurrent_no_gradient   s    z&TestFSolve.test_concurrent_no_gradientc                 C   s*   t | jgd }tdd |D s&td S )Nrt   c                 S   s   g | ]}|d kqS r   r   r   r   r   r   r      s     z<TestFSolve.test_concurrent_with_gradient.<locals>.<listcomp>)r,   rO   r   r   r   r   r   r   test_concurrent_with_gradient   s    z(TestFSolve.test_concurrent_with_gradientN)r    r!   r"   rK   rO   rX   r]   rb   rh   rr   rv   r~   r   r   r   r   r   r   r   r   r<   o   s   
	r<   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestRootHybrc                 C   sL   t dd}d}tddddg}tjt|d||fdj}t|t d d S )Nr=   r>   r?   r@   hybrmethodrB   	r   rD   r
   r   rootr5   r   r   r   rN   r   r   r   rK      s     z.TestRootHybr.test_pressure_network_no_gradientc                 C   sP   t dd}d}tddddgg}tjt|||fdtdj}t|t 	d d S )Nr=   r>   r?   r@   r   rB   r   r9   )
r   rD   r
   r   r   r5   r:   r   r   r   rN   r   r   r   rO      s     z0TestRootHybr.test_pressure_network_with_gradientc                 C   sN   t dd}d}tddddg}tjt|||fdddj}t|t d d S )Nr=   r>   r?   r@   r   Tr   )	r   rD   r
   r   r   r;   r   r   r   rN   r   r   r   ,test_pressure_network_with_gradient_combined   s      z9TestRootHybr.test_pressure_network_with_gradient_combinedN)r    r!   r"   rK   rO   r   r   r   r   r   r      s   	
r   c                   @   s   e Zd Zdd ZdS )
TestRootLMc                 C   sL   t dd}d}tddddg}tjt|d||fdj}t|t d d S )Nr=   r>   r?   r@   lmr   r   rN   r   r   r   rK      s     z,TestRootLM.test_pressure_network_no_gradientN)r    r!   r"   rK   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   rt   (   )g@*   g33333sr-   g{Gz?)	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   sP   t dddg}t| j|| j| jfd\}}t|dkd|  t|| jdd d S )Nr   r[   r.   r-   rg   r=   solution not found (ier=%d)r-   decimalr
   r   r   r   r   r   r   r   r   p0
params_fitrI   r   r   r   
test_basic  s    

zTestLeastSq.test_basicc                 C   sT   t dddg}t| j|| j| jf| jd\}}t|dkd|  t|| jdd d S )Nr   rB   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   sN   t dddgg}t| j|| j| jfdd}|\}}}}}t|dkd|  d S )Nr   TrA   r   solution not found: %s)r
   r   r   r   r   r   )r   r   rC   r   cov_xinfodictrJ   rI   r   r   r   test_full_output%  s    
zTestLeastSq.test_full_outputc           	      C   sf   t dddgtd}t |dd}t| j|| j| jfdd}|\}}}}}t|dkd|  t|| d S )Nr   rx   T)copyrA   r   r   )r
   r   r   r   r   r   r   r   )	r   r   Zp0_copyrC   r   r   r   rJ   rI   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 rP   r   rS   rT   r   r   rU   r   r   r   rX   7  s    z*TestLeastSq.test_wrong_shape_func_callablec                 C   s    ddg}t ttjt|dd d S rY   )rS   rT   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   rW   r   r   r`   r   r   r   test_wrong_shape_Dfun_callableD  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 rc   rd   re   r   r   r   r&   J  r'   z<TestLeastSq.test_wrong_shape_Dfun_function.<locals>.<lambda>c                 S   s
   t | dS rf   rd   re   r   r   r   r&   K  r'   r   r.   r   rS   rT   r   r   r`   r   r   r   test_wrong_shape_Dfun_functionI  s    z*TestLeastSq.test_wrong_shape_Dfun_functionc                 C   s   dd }t jddddddd	d
ddg
t jd}t jddddddddddg
t jd}t ddddg}tj||||fd\}}t|dk t||||d  d||||d   k  d S )Nc                 S   s@   | d t || d  d  d| d d    | d  }|| S )Nr   r.   r-   r?   rg   r   exp)r}   r   r   qr   r   r   rV   P  s    8z&TestLeastSq.test_float32.<locals>.funcg?gw/?g%C?gNbX9?gHzG?gM?gZd;O?gS?g~jt?gV-?rx   gJ4?gN@aÓ?g^K=?gMO?g?g?W[?gl?g{Pk?g rh?gׁsF?      ?r[   r   r-   g-C6?)r   r
   ry   r   r   r   r/   )r   rV   r   r   r   p1successr   r   r   r~   N  s      zTestLeastSq.test_float32c              	   C   s4   dd }t tdd tj|dgd W 5 Q R X d S )Nc                  W   s   t dd S ri   rk   r[   r   r   r   rV   _  s    z-TestLeastSq.test_func_can_raise.<locals>.funcrj   rm   r   ro   rS   rl   r   r   rq   r   r   r   rr   ^  s    zTestLeastSq.test_func_can_raisec              	   C   s>   dd }dd }t tdd tj|dg|d W 5 Q R X d S )	Nc                 S   s   | t dg S rs   ru   re   r   r   r   r&   f  r'   z1TestLeastSq.test_Dfun_can_raise.<locals>.<lambda>c                  W   s   t dd S ri   rk   r[   r   r   r   ra   h  s    z3TestLeastSq.test_Dfun_can_raise.<locals>.deriv_funcrj   rm   r   r   r   r`   r   r   r   rv   e  s    zTestLeastSq.test_Dfun_can_raisec                    sZ    fdd}t dddg}t|| j jfd\}}t|dkd|  t| jdd d S )	Nc                     s        j|  S r   )r   r   r[   r   r   r   rV   o  s    z-TestLeastSq.test_reentrant_func.<locals>.funcr   r[   r   r   r-   r   )r
   r   r   r   r   r   r   )r   rV   r   r   rI   r   r   r   r   n  s    

zTestLeastSq.test_reentrant_funcc                    s^    fdd}t dddg}t j| j jf|d\}}t|dkd|  t| jdd d S )	Nc                     s        j|  S r   )r   r   r[   r   r   r   ra   {  s    z3TestLeastSq.test_reentrant_Dfun.<locals>.deriv_funcr   r   r   r   r-   r   r   )r   ra   r   r   rI   r   r   r   test_reentrant_Dfunz  s    

zTestLeastSq.test_reentrant_Dfunc                 C   s*   t | jgd }tdd |D s&td S )Nrt   c                 S   s   g | ]}|d kqS r   r   r   r   r   r   r     s     z;TestLeastSq.test_concurrent_no_gradient.<locals>.<listcomp>)r,   r   r   r   r   r   r   r   r     s    z'TestLeastSq.test_concurrent_no_gradientc                 C   s*   t | jgd }tdd |D s&td S )Nrt   c                 S   s   g | ]}|d kqS r   r   r   r   r   r   r     s     z=TestLeastSq.test_concurrent_with_gradient.<locals>.<listcomp>)r,   r   r   r   r   r   r   r   r     s    z)TestLeastSq.test_concurrent_with_gradientc              	   C   s6   dd }t tdd tj|ddgd W 5 Q R X d S )Nc                 S   s   d| d d d  d S )Nr-   r   rg   r.   r   re   r   r   r   rV     s    z=TestLeastSq.test_func_input_output_length_check.<locals>.funcz+Improper input: func input vector length N=rm   r   r.   ro   r   rq   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   rX   r]   r   r   r~   rr   rv   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ejd3d4d5 Zd6d7 Z d8S )9TestCurveFitc                 C   s(   t ddddg| _t ddddg| _d S )Nr   g	@      #@gffffff+@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   rV     s    z,TestCurveFit.test_one_argument.<locals>.funcr.   )r.   r.   r   g3ı.n?r=   r   )r   r   g-C6Z?F)rC   check_finite)r   r   r   r   r(   r   r   r   )
r   rV   poptpcovrespopt2pcov2r   errmsgrI   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   rV     s    z,TestCurveFit.test_two_argument.<locals>.funcr-   r^   c]K?-?r=   r   䃞ͪϵ?T㥛 b=y?)r   r   r   r   r(   r   r   )r   rV   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_selfzThis 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   rV     s    z=TestCurveFit.test_func_is_classmethod.<locals>.test_self.funcN)r    r!   r"   r#   rV   r   r   r   r   	test_self  s   r   r^   r   r   r=   r   r   r   r   )r   rV   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   s   ddddddddd	d
g
}ddddddddddg
}ddddddg}ddddddg}dd  }t ||||d!d"\}}t||d#d$ d S )%NgB`"@gZd;@gQ@g#~j@g/$@g;O@g @g@gZd;@g)\@g     ؊@g     (@g     @g     X@g     t@g     @g     @g     8@g     @g     H@g^w8}@gU]}@g     X@g29l?g䃞j@gj@gT{7@g|'fɘ@g ƞ?g'ˊ@c                 S   sL   |t | | d  d|d    |t | | d  d|d     | S )Nr-   r?   r   )r   rW   x1ZA0A1sigmar   r   r   r   f_double_gauss  s
    ""z9TestCurveFit.test_regression_2639.<locals>.f_double_gaussi'  maxfevgh㈵>rtol)r   r   )r   r   r   guessZgoodr   r   r   r   r   r   test_regression_2639  s2            z!TestCurveFit.test_regression_2639c              
   C   s  t ddddddg}t dddddd	g}t ddddddg}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 qNdd }
t t jgd dd}t T}|	t
d t|
||ddg|d\}}t||d d |d d ddgd\}}W 5 Q R X t|jdk t|| t|jdk t|| d S )Nr   r.   r-   rg   r=               c                 S   s   ||  | S r   r   r   r   r   r   r%     s    z!TestCurveFit.test_pcov.<locals>.fr   trfdogbox)r   r   r   gN"q?g~uM/N?rz   r   T)r   r   absolute_sigmar   gO=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_flatz3Covariance of the parameters could not be estimated)r   r   r   r^   )r   r
   r   sqrtr6   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  sN    


 
 
2
zTestCurveFit.test_pcovc                 C   sB   dd }ddddg}dddd	g}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.   r-   rg   r=   r   r   	   r   g|=r{   )r   r   )r   r   r   r   r   r   r   test_array_like  s    zTestCurveFit.test_array_likec                 C   sD   t ddddddg}t ddddddg}tttdd	 || d S )
Nr.   r-   rg   r=   r      g      @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   sj   t dt jdg}t dddg}tttdd || tttdd || tttdd ||fdd	i d S )
Nr.   rg   r-   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
   nanrS   rl   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&   0  r'   r-   rS   rl   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   re   r   r   r   r&   5  r'   z8TestCurveFit.test_function_zero_params.<locals>.<lambda>r.   r-   rg   r=   r  r   r   r   r   test_function_zero_params3  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 rs   )r   arange)_r   r   r   r   r&   8  r'   z*TestCurveFit.test_None_x.<locals>.<lambda>r-   rt   r?   )r   r   r  r   )r   r   r   r   r   r   test_None_x7  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%   =  s    z,TestCurveFit.test_method_argument.<locals>.fr   r.      r?   r   r   r   Nr   unknown)r   r   r   r   rS   rl   )r   r%   r   r   r   r   r   r   r   r   test_method_argument<  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
|ks`td|kslt|dks||d krd|kstd|kstd|kstt|tst|	dks&tq&d S )Nc                 S   s   |t | |   S r   r   r   r   r   r   r%   J  s    z(TestCurveFit.test_full_output.<locals>.fr   r.   r
  r?   r  T)r   rC   ZnfevZfvecr   ZfjacZipvtZqtfr   )r   r   r   r   r   
isinstancestr)
r   r%   r   r   r   r   r   r   r   rI   r   r   r   r   I  s(        zTestCurveFit.test_full_outputc              	   C   s   dd }t ddd}||dd}ddg}dd	g}||f}t||}d
D ]H}t|||||d\}	}
t|	d d t|||||d\}}t||	 qHt|||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%   ^  s    z#TestCurveFit.test_bounds.<locals>.fr   r.   r
  r?   r   rQ   r   )Nr   r   )r  r   r   r@   g333333?)r   r  r   )r   r   r   r   r   r   rS   rl   )r   r%   r   r   ZlbZubr  Zbounds_classr   r   r   Z
popt_classZ
pcov_classr   r   r   test_bounds]  s0    





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%     s    z&TestCurveFit.test_bounds_p0.<locals>.fr-   r   rg   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_p0  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 qd 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   r9     s    z"TestCurveFit.test_jac.<locals>.jacr   r.   r
  r?   r  )z2-pointz3-pointcs)r9   r   r-   r   )r   r9   rw   r      )r   r   r9   rz   r   )r   r   r   r   r   r   )
r   r%   r9   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   rt   r-   c                 S   s   ||  S r   r   r   r}   r   r   r   r&     r'   z5TestCurveFit.test_maxfev_and_bounds.<locals>.<lambda>)r   rg   rw   )r  r   c                 S   s   ||  S r   r   r  r   r   r   r&     r'   )r  Zmax_nfev+=r{   )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 qq|d S )Nc                 S   s   |t | |   S r   r   r   r   r   r   rV     s    z9TestCurveFit.test_curvefit_simplecovariance.<locals>.funcc                 S   s*   t | |  }t || |  | fjS r   r  r  r   r   r   r9     s    z8TestCurveFit.test_curvefit_simplecovariance.<locals>.jacr   r=   2         @?皙?sizer-   NNFTr   r9   r   r   r{   )
r   r   r   r   normalr(   zerosr6   r   r   )r   rV   r9   r   r   r   r   covarjac1jac2r   r   r   r   r   r   r   r   test_curvefit_simplecovariance  s(    
 

 
z+TestCurveFit.test_curvefit_simplecovariancec              
   C   sX  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d	d	dgg}
|

|}|

|	
|
j}||fdfD ]b\}}dD ]T}t||||||d\}}t||||||d\}}t||ddd t||ddd qqd S )Nc                 S   sd   t dt d dt d dgdt d dt d dgdddgg}||t | |   S Nr   r-         r   )r   r
   r   dotr   )r   r   r   rotnr   r   r   funcp  s    Jz4TestCurveFit.test_curvefit_covariance.<locals>.funcpc                 S   sz   t dt d dt d dgdt d dt d dgdddgg}t | |  }|t || |  | fjS r1  )r   r
   r   r   r3  r   r   )r   r   r   r4  r  r   r   r   jacp  s    Jz3TestCurveFit.test_curvefit_covariance.<locals>.jacpc                 S   s   |t | |   S r   r   r   r   r   r   rV     s    z3TestCurveFit.test_curvefit_covariance.<locals>.funcc                 S   s*   t | |  }t || |  | fjS r   r  r  r   r   r   r9     s    z2TestCurveFit.test_curvefit_covariance.<locals>.jacr   r.   r=   r#  r   r%  r&  r-   r2  r(  r)  r*  g+i)+>r   )r   r|   )r   r   r   r  r+  r(   r,  r6   r
   r   r3  r   r   r   )r   r5  r6  rV   r9   r   r   r   r   r-  r4  ZydatapZcovarpr.  r/  r   r   r   r   r   r   r   r   test_curvefit_covariance  s2    J

 

 
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 H td	t	 t
||||d
\}}t | stt |drtW 5 Q R X qBq2d S )Nr  r   rQ   r   r>   c                 S   s   ||  | S r   r   r   r   r   r   rV     s    z&TestCurveFit.test_dtypes.<locals>.funcr   errorr  r.   )r   r  r  ry   r   astypewarningscatch_warningssimplefilterr   r   isfiniter   r   Zallclose)	r   r   r   rV   r   ZdtxZdtyr}   covr   r   r   test_dtypes  s    

zTestCurveFit.test_dtypesc              
   C   s   dd }t dddddg}t dddddg}t d	d
dddg}dddddg}t ddddddddg}||f| }t||||||fd\}}	|t j}||f| }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>.hyperbolag      r@          g      $r   g      $@ggUUUUUU?r   g      r>   r  g?r   iiir=   r          )r%   r   r   r   r  gh㈵>r{   )r   r
   r   r9  ry   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 )
Nrt   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   rH  r   test_broadcast_y7  s    
$

zTestCurveFit.test_broadcast_yc              	   C   sD   dd }t t* t|ddddgddd	d
gdgdd W 5 Q R X d S )Nc                 S   s   ||  | S r   r   r   r   r   r   rV   I  s    z.TestCurveFit.test_args_in_kwargs.<locals>.funcr.   r-   rg   r=   r   r         rZ   )r   r   r   rB   r  rq   r   r   r   test_args_in_kwargsF  s    


z TestCurveFit.test_args_in_kwargsc              	   C   sB   dd }t tdd$ t|ddddgd	d
ddgd W 5 Q R X d S )Nc                 S   s    |t | |   | | | S r   r   r   r   r   r   dr  r   r   r   rV   T  s    z<TestCurveFit.test_data_point_number_validation.<locals>.funczThe number of func parameters=rm   r.   r-   rg   r=   r   r   rK  rL  )r   r   )rS   rT   r   rq   r   r   r   !test_data_point_number_validationS  s    

z.TestCurveFit.test_data_point_number_validationzignore::RuntimeWarningc           	      C   s   dd }t jd}d}t |}t dd||| }tj|||dd\}}t t |d	ksht	t
|d	 }t |d
kst	t||j d S )Nc                 S   s0   |t | d |  |t | d |   | S Nr.   r   logrN  r   r   r   r%   b  s    z#TestCurveFit.test_gh4555.<locals>.f   MI9V$O-rw   r-   r   i r   r   g{Gz)r   r   default_rngr  r   r   r   r   r6   r   r   Zeighr   r   )	r   r%   rngr8   r   r   r}   r>  Zeigsr   r   r   test_gh4555\  s    
zTestCurveFit.test_gh4555c           
      C   s   t jd}dd }t ddd}||ddd	}d
|j|jd }|| }t|||\}}dddgdddgdddgg}	t||	d d S )NrT  c                 S   s   |t | |   | S r   r   )r   r   r   r   r   r   r   rV   u  s    z'TestCurveFit.test_gh4555b.<locals>.funcr   r=   r"  r#  r$  r>   r%  r&  gt^G?gd*.X|?gT}Hg1x?gu`v?gon2f?gH׊>)r   r   rU  r   r+  r'  r   r   )
r   rV  rV   r   r   Zy_noiser   r  r   refr   r   r   test_gh4555bp  s    zTestCurveFit.test_gh4555bN)!r    r!   r"   r   r   r   r   r   r   r   r   r  r  r  r	  r  r   r  r  r  r!  r0  r7  r?  rG  rJ  rM  rP  pytestmarkfilterwarningsrW  rY  r   r   r   r   r     s8   
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   re   r   r   r   rV     s    z0TestFixedPoint.test_scalar_trivial.<locals>.funcr   r@   r   r   r   rV   rW   r   r   r   r   test_scalar_trivial  s    
z"TestFixedPoint.test_scalar_trivialc                 C   s$   dd }d}t ||}t|d d S )Nc                 S   s   | d S r   r   re   r   r   r   rV     s    z/TestFixedPoint.test_scalar_basic1.<locals>.func?r   r_  r`  r   r   r   test_scalar_basic1  s    
z!TestFixedPoint.test_scalar_basic1c                 C   s$   dd }d}t ||}t|d d S )Nc                 S   s   | d S Nr>   r   re   r   r   r   rV     s    z/TestFixedPoint.test_scalar_basic2.<locals>.funcrb  r   r_  r`  r   r   r   test_scalar_basic2  s    
z!TestFixedPoint.test_scalar_basic2c              	   C   sD   dd }ddg}t jdd t||}W 5 Q R X t|ddg d S )Nc                 S   s   d|  S r^  r   re   r   r   r   rV     s    z/TestFixedPoint.test_array_trivial.<locals>.funcg333333?g333333?ignorer   r@   )r   errstater   r   r`  r   r   r   test_array_trivial  s
    z!TestFixedPoint.test_array_trivialc              	   C   sZ   dd }t dddg}dddg}tjd	d
 t|||fd}W 5 Q R X t|d|  d S )Nc                 S   s   || d  S r   r   r   r   r   r   r   rV     s    z.TestFixedPoint.test_array_basic1.<locals>.func      ?r         ?皙?gffffff?g?rf  rg  r[   )r
   r   rh  r   r   r   rV   r   rW   r   r   r   r   test_array_basic1  s    
z TestFixedPoint.test_array_basic1c                 C   sB   dd }t dddg}dddg}t|||fd}t||d	  d S )
Nc                 S   s   || d  S rd  r   rj  r   r   r   rV     s    z.TestFixedPoint.test_array_basic2.<locals>.funcrk  r   rl  g?rm  r[   r-   )r
   r   r   rn  r   r   r   test_array_basic2  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 )NrA  r?   r   )Zxxr   r   r   r&     r'   z.TestFixedPoint.test_lambertw.<locals>.<lambda>r   r   g-q=i  )rB   ZxtolmaxiterrA  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$  gjt?r.   c                    s,   t  |  t  |  | d   d S rQ  rR  )r8   Zi0klksr   r   rV     s    z1TestFixedPoint.test_no_acceleration.<locals>.func	iterationr  )r   r   )r   mZn0rV   r8   r   rs  r   test_no_acceleration  s    $z#TestFixedPoint.test_no_accelerationN)r    r!   r"   ra  rc  re  ri  ro  rp  rr  rx  r   r   r   r   r]    s   
	r]  ),r#   r:  rZ  Znumpy.testingr   r   r   r   r   r   r   r	   rS   Znumpyr   r
   r   Zmultiprocessing.poolr   Zscipyr   r   Zscipy.specialr   Zscipy.optimize._minpack_pyr   r   r   Zscipy.optimizer   Zscipy.optimize._minimizer   r   r$   r,   r5   r:   r;   r<   r   r   r   r   r]  r   r   r   r   <module>   s8   $	h     o