o
    ?Hh)                     @   sT  d Z ddl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,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   a/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/scipy/optimize/tests/test_minpack.py__init__      
zReturnShape.__init__c                 C   s   t | jS r   )nponesr   )r   xr   r   r   __call__!      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 sw   Y  d S )Nc                 S   s   |  S r   r   )fr   r   r   <lambda>.   s    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   hstacksum)
flow_ratesQtotkPFr   r   r   pressure_network5   s   &r7   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 parameters.

    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   )r2   r3   r4   npdiffjacr   r   r   pressure_network_jacobianT   s   6 r=   c                 C   s   t | ||t| ||fS r   )r7   r=   )r2   r3   r4   r   r   r   pressure_network_fun_and_gradk   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g d}tjt|||fdd\}}}}t|t d t|dk| d S )N         ?       @        rC   rD   Targsfull_outputr/   )	r   fullr
   r   fsolver7   r   r   r   )r   r4   r3   initial_guessfinal_flowsinfoiermesgr   r   r   !test_pressure_network_no_gradientq   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@   rA   rB   rF   fprime)	r   rH   r
   r   rI   r7   r=   r   r   r   r4   r3   rJ   rK   r   r   r   #test_pressure_network_with_gradient|   s   
z.TestFSolve.test_pressure_network_with_gradientc                 C   $   t d}ddg}tttj|| d S Nr/         ?rC   r   assert_raises	TypeErrorr   rI   r   funcx0r   r   r   test_wrong_shape_func_callable      z)TestFSolve.test_wrong_shape_func_callablec                 C       ddg}t ttjt|dd d S NrV   rC   )r/   rF   )rX   rY   r   rI   r&   r   r\   r   r   r   test_wrong_shape_func_function      z)TestFSolve.test_wrong_shape_func_functionc                 C   ,   t d}t d}tttj|ddg|d d S )Nr/   r.   r.   r   r\   rQ   rW   r   r[   
deriv_funcr   r   r    test_wrong_shape_fprime_callable      z+TestFSolve.test_wrong_shape_fprime_callablec                 C   ,   dd }dd }t ttj|ddg|d d S )Nc                 S   
   t | dS N)r.   r&   r   r   r   r   r[      r   z9TestFSolve.test_wrong_shape_fprime_function.<locals>.funcc                 S   rn   N)   rs   rp   rq   r   r   r   rj      r   z?TestFSolve.test_wrong_shape_fprime_function.<locals>.deriv_funcr   r/   rh   )rX   rY   r   rI   ri   r   r   r    test_wrong_shape_fprime_function      z+TestFSolve.test_wrong_shape_fprime_functionc                 C   J   dd }t tdd tj|dgd W d    d S 1 sw   Y  d S )Nc                  W      t dNI raised
ValueErrorrb   r   r   r   r[         z,TestFSolve.test_func_can_raise.<locals>.funcry   matchr   r\   rX   r{   r   rI   r   r[   r   r   r   test_func_can_raise      "zTestFSolve.test_func_can_raisec                 C   T   dd }dd }t tdd tj|dg|d W d    d S 1 s#w   Y  d S )	Nc                 S      | t dg S N
   r   r
   rq   r   r   r   r[         z,TestFSolve.test_Dfun_can_raise.<locals>.funcc                  W   rw   rx   rz   rb   r   r   r   rj      r|   z2TestFSolve.test_Dfun_can_raise.<locals>.deriv_funcry   r}   r   rh   r   ri   r   r   r   test_Dfun_can_raise   
   "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/     dtyper.   )r   r
   float32rq   r   r   r   r[      s   (z%TestFSolve.test_float32.<locals>.funcr/   r   MbP?atol)r   rI   r   r
   r   r   )r   r[   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                            t|  S r   )rO   r7   rb   r   r   r   r[         z,TestFSolve.test_reentrant_func.<locals>.funcr@   rA   rB   TrE   r/   )r   rH   r
   r   rI   r   r   r   )	r   r[   r4   r3   rJ   rK   rL   rM   rN   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                     r   r   )rS   r=   rb   r   r   r   rj      r   z3TestFSolve.test_reentrant_Dfunc.<locals>.deriv_funcr@   rA   rB   rP   )r   rH   r
   r   rI   r7   r   r   )r   rj   r4   r3   rJ   rK   r   r   r   test_reentrant_Dfunc   s   
zTestFSolve.test_reentrant_Dfuncc                 C   *   t | jgd }tdd |D sJ d S )Nr   c                 S      g | ]}|d u qS r   r   .0resultr   r   r   
<listcomp>       z:TestFSolve.test_concurrent_no_gradient.<locals>.<listcomp>)r-   rO   allr   vr   r   r   test_concurrent_no_gradient      z&TestFSolve.test_concurrent_no_gradientc                 C   r   )Nr   c                 S   r   r   r   r   r   r   r   r      r   z<TestFSolve.test_concurrent_with_gradient.<locals>.<listcomp>)r-   rS   r   r   r   r   r   test_concurrent_with_gradient   r   z(TestFSolve.test_concurrent_with_gradientN)r"   r#   r$   rO   rS   r]   rd   rk   rt   r   r   r   r   r   r   r   r   r   r   r   r?   p   s    

r?   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestRootHybrc                 C   H   t dd}d}tg d}tjt|d||fdj}t|t d d S )Nr@   rA   rB   hybrmethodrF   	r   rH   r
   r   rootr7   r   r   r   rR   r   r   r   rO         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@   rA   rB   r   rF   r   r<   )
r   rH   r
   r   r   r7   r=   r   r   r   rR   r   r   r   rS      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@   rA   rB   r   Tr   )	r   rH   r
   r   r   r>   r   r   r   rR   r   r   r   ,test_pressure_network_with_gradient_combined   s   z9TestRootHybr.test_pressure_network_with_gradient_combinedN)r"   r#   r$   rO   rS   r   r   r   r   r   r      s    	
r   c                   @   s   e Zd Zdd ZdS )
TestRootLMc                 C   r   )Nr@   rA   rB   lmr   r   rR   r   r   r   rO      r   z,TestRootLM.test_pressure_network_no_gradientN)r"   r#   r$   rO   r   r   r   r   r      s    r   c                   @   s>   e Zd Zdd Zdd Zejdg ddd Zd	d
 Z	dS )TestNfevc                 C   s   t  | _d S r   )	threadinglocalnfevr   r   r   r   setup_method     zTestNfev.setup_methodc                 C   s0   t | jds
d| j_| j jd7  _|d d S )Ncr   r/   r.   rs   )hasattrr   r   )r   yr   r   r   zero_f  s   zTestNfev.zero_fr   )
r   r   broyden1broyden2andersonlinearmixingdiagbroydenexcitingmixingkrylovzdf-sanec                 C   s0   d| j _tj| jd|d}|j | j jksJ d S )Nr   r   r   )r   r   r   r   r   )r   r   solutionr   r   r   test_root_nfev  s   zTestNfev.test_root_nfevc                 C   s:   d| j _tj| jddd\}}}}|d | j jksJ d S )Nr   r   T)rG   r   )r   r   r   rI   r   )r   r   rL   rM   rN   r   r   r   test_fsolve_nfev  s   zTestNfev.test_fsolve_nfevN)
r"   r#   r$   r   r   pytestmarkparametrizer   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'S )(TestLeastSqc                 C   sj   t ddd}d\}}}|| _|||f| _||d  ||  | }t jd |dt j|j  | _d S )Nr   r   (   )g@*   g33333sr.   g{Gz?)	r   linspacer   abcrandomseedstandard_normalr   y_meas)r   r   abr   y_truer   r   r   r   "  s   
zTestLeastSq.setup_methodc                 C   s*   |\}}}|||d  ||  |  }|S Nr.   r   )r   r   r   r   r   r   r   errr   r   r   	residuals+  s   
zTestLeastSq.residualsc                 C   s   t |d |t |gj S r   )r   vstack	ones_likeT)r   _p_yr   r   r   r   residuals_jacobian0  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   rb   r/   r.   rs   r@   solution not found (ier=%d)r.   decimalr
   r   r   r   r   r   r   r   r   p0
params_fitrM   r   r   r   
test_basic3  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   rF   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 g dg}t| j|| j| jfdd}|\}}}}}t|dv d|  d S )Nr   TrE   r   solution not found: )r
   r   r   r   r   r   )r   r   rG   r   cov_xinfodictrN   rM   r   r   r   test_full_outputD  s   
zTestLeastSq.test_full_outputc           	      C   sf   t g dtd}t |dd}t| j|| j| jfdd}|\}}}}}t|dv d|  t|| d S )Nr   r   T)copyrE   r   r   )r
   r   r   r   r   r   r   r   )	r   r   p0_copyrG   r   r   r   rN   rM   r   r   r   test_input_untouchedL  s   
z TestLeastSq.test_input_untouchedc                 C   rT   rU   r   rX   rY   r   r   rZ   r   r   r   r]   V  r^   z*TestLeastSq.test_wrong_shape_func_callablec                 C   r_   r`   )rX   rY   r   r   r&   rc   r   r   r   rd   ]  re   z*TestLeastSq.test_wrong_shape_func_functionc                 C   rf   )Nr/   rg   r   r\   r   r   ri   r   r   r   test_wrong_shape_Dfun_callablec  rl   z*TestLeastSq.test_wrong_shape_Dfun_callablec                 C   rm   )Nc                 S   rn   ro   rp   rq   r   r   r   r[   i  r   z8TestLeastSq.test_wrong_shape_Dfun_function.<locals>.funcc                 S   rn   rr   rp   rq   r   r   r   rj   k  r   z>TestLeastSq.test_wrong_shape_Dfun_function.<locals>.deriv_funcr   r/   r   rX   rY   r   r   ri   r   r   r   test_wrong_shape_Dfun_functionh  ru   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.   rC   rs   r   exp)r   r   r   qr   r   r   r[   q  s   8z&TestLeastSq.test_float32.<locals>.func)
g?gw/?g%C?gNbX9?gHzG?gM?gZd;O?gS?g~jt?gV-?r   )
gJ4?gN@aÓ?g^K=?gMO?g?g?W[?gl?g{Pk?g rh?gׁsF?)      ?r   r   r   rb   r   r.   g-C6?)r   r
   r   r   r   r   r1   )r   r[   r   r   r   p1successr   r   r   r   o  s   

4zTestLeastSq.test_float32c                 C   rv   )Nc                  W   rw   rx   rz   rb   r   r   r   r[     r|   z-TestLeastSq.test_func_can_raise.<locals>.funcry   r}   r   r   rX   r{   r   r   r   r   r   r   r     r   zTestLeastSq.test_func_can_raisec                 C   r   )	Nc                 S   r   r   r   rq   r   r   r   r[     r   z-TestLeastSq.test_Dfun_can_raise.<locals>.funcc                  W   rw   rx   rz   rb   r   r   r   rj     r|   z3TestLeastSq.test_Dfun_can_raise.<locals>.deriv_funcry   r}   r   r   r   ri   r   r   r   r     r   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                             j|  S r   )r   r   rb   r   r   r   r[        
z-TestLeastSq.test_reentrant_func.<locals>.funcr   rb   r   r   r.   r   )r
   r   r   r   r   r   r   )r   r[   r   r   rM   r   r   r   r     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                     r  r   )r   r   rb   r   r   r   rj     r  z3TestLeastSq.test_reentrant_Dfun.<locals>.deriv_funcr   r   r   r   r.   r   r   )r   rj   r   r   rM   r   r   r   test_reentrant_Dfun  s   

zTestLeastSq.test_reentrant_Dfunc                 C   r   )Nr   c                 S   r   r   r   r   r   r   r   r     r   z;TestLeastSq.test_concurrent_no_gradient.<locals>.<listcomp>)r-   r   r   r   r   r   r   r     r   z'TestLeastSq.test_concurrent_no_gradientc                 C   r   )Nr   c                 S   r   r   r   r   r   r   r   r     r   z=TestLeastSq.test_concurrent_with_gradient.<locals>.<listcomp>)r-   r   r   r   r   r   r   r     r   z)TestLeastSq.test_concurrent_with_gradientc                 C   sL   dd }t tdd tj|ddgd W d    d S 1 sw   Y  d S )Nc                 S   s   d| d d d  d S )Nr.   r   rs   r/   r   rq   r   r   r   r[        z=TestLeastSq.test_func_input_output_length_check.<locals>.funcz+Improper input: func input vector length N=r}   r   r/   r   r   r   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   r]   rd   r   r   r   r   r   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	e
jjdd Zdd Zedd Ze
jdg ddd Ze
jdg ddd Ze
jdddge
jdg d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d0d1 Ze
jd2g d3d4d5 Zd6d7 Zd8d9 Z e
jd:d;d<gd=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dGdH Z&e
j'dIdJdK Z(dLdM Z)dNdO Z*e
jddPdQgdRdS Z+dTS )UTestCurveFitc                 C   s    t g d| _t g d| _d S )N)r   g	@      #@gffffff+@)r   rC         @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 r   r   r   r   r   r   r   r[     r|   z,TestCurveFit.test_one_argument.<locals>.funcr/   )r/   r/   r   g3ı.n?r@   r   )r   r   g-C6Z?F)rG   check_finite)r   r   r   r   r)   r   r   r   )
r   r[   poptpcovrespopt2pcov2r   errmsgrM   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   r[     r!   z,TestCurveFit.test_two_argument.<locals>.funcr.   rg   c]K?-?r@   r   䃞ͪϵ?T㥛 b=y?)r   r   r   r   r)   r   r   )r   r[   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   r[     r!   z=TestCurveFit.test_func_is_classmethod.<locals>.test_self.funcN)r"   r#   r$   r%   r[   r   r   r   r   	test_self  s    r  rg   r  r  r@   r   r  r  r  )r   r[   r   r   r   r   r   )r   r  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9l?r  )g䃞j@gj@gT{7@g|'fɘ@g ƞ?g'ˊ@c                 S   sL   |t | | d  d|d    |t | | d  d|d     | S )Nr.   rC   r   )r   r\   x1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   guess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 qd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 sw   Y  t|jdk t|| t|jdk t|| d S )N)r   r/   r.   rs   r@      )r/   r/   r+           )r/   r.   r/   r.   r/   r.   c                 S      ||  | S r   r   r  r   r   r   r'     r!   z!TestCurveFit.test_pcov.<locals>.fr   trfdogboxr.   r   )r   r"  r   gN"q?g~uM/N?r   r&  rs   T)r   r"  absolute_sigmar   gO=N?g5=`6?g[u|?g-/i@c                 S      ||  S r   r   r  r   r   r   f_flat  r|   z&TestCurveFit.test_pcov.<locals>.f_flatr@   z3Covariance of the parameters could not be estimatedr   r"  r   rg   )r   r
   r   sqrtr8   r   infreshaper   filterr   r   r   r   )r   xdataydatar"  r'   r   r  r  perr_scaledperrr5  pcov_expectedsuppopt1pcov1r   r   r   	test_pcov  sL   



*
zTestCurveFit.test_pcovc                 C   s:   dd }g d}g d}t t|||d ddgdd	 d S )
Nc                 S   r/  r   r   r  r   r   r   f_linear/  r!   z.TestCurveFit.test_array_like.<locals>.f_linearr   )rs   r+  r,  	   r   r.   r/   g|=r   )r   r   )r   rE  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.   rs   r@   r+     )r/   r.   rs   r@   g      @rH  c                 S   r4  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_covariance6  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/   rs   r/   r.   rs   c                 S   r/  r   r   r  r   r   r   r(   F      z0TestCurveFit.test_NaN_handling.<locals>.<lambda>c                 S   r/  r   r   r  r   r   r   r(   H  rM  c                 S   r/  r   r   r  r   r   r   r(   J  rM  r  T)r   r
   nanrX   r{   r   rJ  r   r   r   test_NaN_handling>  s   


zTestCurveFit.test_NaN_handlingc                 C   s,  | |||dd}d}t t|d tdi |ddd W d    n1 s'w   Y  t tdd tdi |d	d
i W d    n1 sHw   Y  tdi |d	di\}}	||d< ||d< tdi |\}
}	t||
 d}t t|d tdi |d	di W d    d S 1 sw   Y  d S )NF)r'   r<  r=  r   r  z;`nan_policy='propagate'` is not supported by this function.r}   	propagatei  )
nan_policyr%  zThe input contains nanrQ  raiseomitr<  r=  zTnan_policy must be one of \{(?:'raise'|'omit'|None)(?:, ?(?:'raise'|'omit'|None))*\}hir   )rX   r{   r   r   )r'   xdata_with_nanxdata_without_nanydata_with_nanydata_without_nanr   kwargs	error_msgresult_with_nan_result_without_nanr   r   r   _check_nan_policyM  s&   
"zTestCurveFit._check_nan_policyr   r0  c                 C   sn   dd }t ddt jddt jg}t ddddt jdg}t g d	}t g d
}| |||||| d S )Nc                 S   r/  r   r   r  r   r   r   r'   l  r!   z*TestCurveFit.test_nan_policy_1d.<locals>.fr.   rs   r@   r/   r+  r,  )r.   rs   r@   rL  r   r
   rN  r^  r   r   r'   rU  rW  rV  rX  r   r   r   test_nan_policy_1dj  s   
zTestCurveFit.test_nan_policy_1dc              
   C   s   dd }t ddt jddt jdgddt jt jdt jdgg}t ddddt jdd	g}t g d
g dg}t g d}| |||||| d S )Nc                 S   s0   | dd d f }| dd d f }|| | | S Nr   r/   r   r   r   r   r  x2r   r   r   r'   y  s   z*TestCurveFit.test_nan_policy_2d.<locals>.fr.   rs   r@   r+  r,  r/   r   r.   rs   r+  r.   rs   r,  r/   r.   r   r_  r`  r   r   r   test_nan_policy_2dw  s   
zTestCurveFit.test_nan_policy_2dr:   r.   rs   c              
   C   s   dd }t ddt jddt jdgddt jt jdt jdggg}|dkr'| n|}t ddddt jdd	g}t g d
g dgg}t g d}| |||||| d S )Nc                 S   s<   | ddd d f   }| ddd d f   }|| | | S )N.r   r/   )squeezerc  r   r   r   r'     s   z,TestCurveFit.test_nan_policy_2_3d.<locals>.fr.   rs   r@   r+  r,  r/   r   re  rf  rg  )r   r
   rN  ri  r^  )r   r:   r   r'   rU  rW  rV  rX  r   r   r   test_nan_policy_2_3d  s   
z!TestCurveFit.test_nan_policy_2_3dc                 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   r4  r   r   r
  r   r   r   r(     rI  z0TestCurveFit.test_empty_inputs.<locals>.<lambda>c                 S   r4  r   r   r
  r   r   r   r(     rI  )r/   r.   )boundsc                 S   r4  r   r   r
  r   r   r   r(     rI  r/   c                 S   r4  r   r   r
  r   r   r   r(     rI  r.   rX   r{   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   rq   r   r   r   r(     s    z8TestCurveFit.test_function_zero_params.<locals>.<lambda>r/   r.   rs   r@   rl  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 r   )r   arange)r\  r   r   r   r   r(     s    z*TestCurveFit.test_None_x.<locals>.<lambda>r.   r   rC   )r   r   ro  r   )r   r  r  r   r   r   test_None_x  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      |t | |   S r   r   r  r   r   r   r'        z,TestCurveFit.test_method_argument.<locals>.fr   r/      rC   r1  r2  r   Nr   unknown)r   r   r   r   rX   r{   )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 ]J}t||||dd	\}}}}}	t|ddg d
|v s0J d|v s6J |dks>|d u rPd|v sDJ d|v sJJ d|v sPJ t|tsWJ |	dv s]J qd S )Nc                 S   rq  r   r   r  r   r   r   r'     rr  z(TestCurveFit.test_full_output.<locals>.fr   r/   rs  rC   rt  T)r   rG   r   fvecr   fjacipvtqtfr   )r   r   r   r   
isinstancestr)
r   r'   r<  r=  r   r  r  r   r  rM   r   r   r   r     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 ]$}t|||||d\}	}
t|	d d t|||||d\}}t||	 q$t|||dddgdt jgfd\}	}
t|	d d ttt||||dd d S )Nc                 S   rq  r   r   r  r   r   r   r'     rr  z#TestCurveFit.test_bounds.<locals>.fr   r/   rs  rC   r   rV   r  )Nr1  r2  )rk  r   r1  rD   g333333?)r   rk  r   )r   r   r   r   r   r9  rX   r{   )r   r'   r<  r=  lbubrk  bounds_classr   r  r  
popt_class
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 ]#}t|||d	t j d
\}}t|||d	t j ||d\}}t|| q"d S )Nc                 S   s   t | | S r   )r   sinr
  r   r   r   r'     r   z&TestCurveFit.test_bounds_p0.<locals>.fr.   r   rs   r1  r2  g @r7  r   rk  r   )r   r   pir  r   r   )	r   r'   r<  r=  rk  r   popt_1r\  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 ]}d
D ]}t|||||d\}}t|ddg qqdD ]}t|||||d\}}t|ddg q3d|d< t |jd }	d|	d< dD ]}t||||	||d\}}t|ddgdd qZd S )Nc                 S   rq  r   r   r  r   r   r   r'     rr  z TestCurveFit.test_jac.<locals>.fc                 S   *   t | |  }t || |  | fjS r   r   r   r   r   r   r   r   er   r   r   r<     r   z"TestCurveFit.test_jac.<locals>.jacr   r/   rs  rC   r  )z2-pointz3-pointcs)r<   r   r.   r0  r   r<   r   r+     )r"  r   r<   r   r&  )r   r   r   r   r   r   )
r   r'   r<   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   r   r.   c                 S   r4  r   r   r   r   r   r   r   r(   (  rI  z5TestCurveFit.test_maxfev_and_bounds.<locals>.<lambda>)r   rs   r   )rk  r%  c                 S   r4  r   r   r  r   r   r   r(   )  rI  )rk  max_nfev+=r   )r   ro  r   r   )r   r   r   rB  r\  r  r   r   r   test_maxfev_and_bounds#  s   z#TestCurveFit.test_maxfev_and_bounds	sigma_dim)r   r/   r.   c                 C   s  dd }t jd}d}t dd|}||dd}|d	kr)d
}||d	||7 }n4|dkr;|d
 }||d	||7 }n"|dkrY|d	d||f}||j }||t ||7 }nJ dddg}	|j|dd}
|j|dd}t j	||
< t j	||< t
||||	|dd\}}t t |
|f}t j||d	d}t j||d	d}t |}|jdkrt ||}n|jdkrt j||d	d}t j||dd}t
||||	|d\}}t||dd t||dd d S )Nc                 S   s   |t ||   S r   r   r  r   r   r   exponential0  s   z7TestCurveFit.test_curvefit_omitnan.<locals>.exponentiall   lt*G7 r   r/   r   皙?rA   r   g?r.   Fz1The sigma must be a scalar, 1D array or 2D array.g?r   r+  sizerS  )r   r"  rQ  )axisr6  r  r   )r   r   default_rngr   normalr   multivariate_normal
zeros_likeintegersrN  r   uniqueconcatenatedeleteasarrayndimr   )r   r  r  rngNr   r   r"  r   r   i_xi_yres_optres_covi_deleteref_optref_covr   r   r   test_curvefit_omitnan.  sH   






z"TestCurveFit.test_curvefit_omitnanc              
   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 ](}
t||||||
d\}}t|||||	|
d\}}t	||dd t	||dd qDq>d S )Nc                 S   rq  r   r   r  r   r   r   r[   g  rr  z9TestCurveFit.test_curvefit_simplecovariance.<locals>.funcc                 S   r  r   r  r  r   r   r   r<   j  r   z8TestCurveFit.test_curvefit_simplecovariance.<locals>.jacr   r@   2         @?r  r  r.   NNFTr"  r<   r3  r  r   )
r   r   r   r   r  r)   zerosr8   r   r   )r   r[   r<   r<  r   r=  r"  covarjac1jac2r3  rB  rC  r  r  r   r   r   test_curvefit_simplecovariancee  s(   



z+TestCurveFit.test_curvefit_simplecovariancec              
   C   sT  dd }dd }dd }dd }t jd	}t d
d}||dd}|d|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 ]1\}}dD ]*}t||||	||d\}}t||||||d\}}t||ddd t||ddd q|qvd 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
   r8  dotr   )r   r   r   rotnr   r   r   funcp  s
    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
   r8  r   r  r   r   )r   r   r   r  r  r   r   r   jacp  s     z3TestCurveFit.test_curvefit_covariance.<locals>.jacpc                 S   rq  r   r   r  r   r   r   r[     rr  z3TestCurveFit.test_curvefit_covariance.<locals>.funcc                 S   r  r   r  r  r   r   r   r<     r   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   RandomStatero  r  r)   r  r8   r
   r8  r  r   r   r   )r   r  r  r[   r<   r  r<  r   r=  r"  r  r  ydatapcovarpr  r  r3  rB  rC  r  r  r   r   r   test_curvefit_covariance  s8    




z%TestCurveFit.test_curvefit_covariancer3  FTc                 C   s`   dd }| j | j}}t|||d|d\}}t|||t|d|d\}}t||ks.J d S )Nc                 S   r/  r   r   r  r   r   r   r[     r!   z5TestCurveFit.test_curvefit_scalar_sigma.<locals>.funcr.   )r"  r3  )r   r   r   r   	full_liker   )r   r3  r[   r   r   r\  rC  r  r   r   r   test_curvefit_scalar_sigma  s   
z'TestCurveFit.test_curvefit_scalar_sigmac           	   
   C   s   t dd}d| d dt |  }dd }dD ]S}t jt jfD ]J}t jt jfD ]}||}||}q)t ) td	t	 t
||||d
\}}t | sTJ t |dr\J W d    n1 sfw   Y  q!qd S )Nr  r+  rV   r  rA   c                 S   r/  r   r   r  r   r   r   r[     r!   z&TestCurveFit.test_dtypes.<locals>.funcr0  errorr   r/   )r   ro  r  r   r   astypewarningscatch_warningssimplefilterr   r   isfiniter   allclose)	r   r   r   r[   r   dtxdtyr   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   r8  )r   s_1s_2o_xo_yr   b_2b_1r   r   r   	hyperbola  s   2z,TestCurveFit.test_dtypes2.<locals>.hyperbola)g      rD          g      $rD   )rD   r  r  rD   g      $@)ggUUUUUU?r   g      rA   )r  g?r  )iiir@   r@   r-         )r'   r<  r=  r   rk  gh㈵>r   )r   r
   r   r  r   r   )r   r  min_fitmax_fitr(  paramsr<  r=  popt_64r\  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 )
Nr   g@r.   g      @c                    s   || d  ||     S r   r   r  targetr   r   fit_func  r  z/TestCurveFit.test_broadcast_y.<locals>.fit_funcr0  )r<  r=  r   r   )r   ro  r   randr)   r   r  r   )r   r<  r  r   popt0pcov0rB  rC  r   r  r   test_broadcast_y  s    
$

zTestCurveFit.test_broadcast_yc                 C   sR   dd }t t t|g dg ddgdd W d    d S 1 s"w   Y  d S )Nc                 S   r/  r   r   r  r   r   r   r[     r!   z.TestCurveFit.test_args_in_kwargs.<locals>.funcr   r+  rF        r/   ra   )r<  r=  r   rF   rl  r   r   r   r   test_args_in_kwargs   s   
"z TestCurveFit.test_args_in_kwargsc                 C   sP   dd }t tdd t|g dg dd W d    d S 1 s!w   Y  d S )Nc                 S   s    |t | |   | | | S r   r   r   r   r   r   dr  r   r   r   r[     s    z<TestCurveFit.test_data_point_number_validation.<locals>.funczThe number of func parameters=r}   r   r  )r<  r=  )rX   rY   r   r   r   r   r   !test_data_point_number_validation  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s4J t	
|d	 }t |d
ksDJ t||j d S )Nc                 S   s0   |t | d |  |t | d |   | S Nr/   r   logr  r   r   r   r'     s   0z#TestCurveFit.test_gh4555.<locals>.f   MI9V$O-r   r.   r,  i r$  r   g{Gz)r   r   r  ro  r   r   r   r   r8   r   eighr   r   )	r   r'   r  r:   r   r   r   r  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|||\}}g dg dg dg}	t||	d d S )Nr  c                 S   s   |t | |   | S r   r   )r   r   r   r   r   r   r   r[   /  r  z'TestCurveFit.test_gh4555b.<locals>.funcr   r@   r  r  r  rA   r  r  )gt^G?d*.X|?T}H)r  g1x?u`v?)r  r  gon2f?gH׊>)r   r   r  r   r  r  r   r   )
r   r  r[   r<  r   y_noiser=  r\  r  refr   r   r   test_gh4555b*  s   zTestCurveFit.test_gh4555bc                    s   t jd}t ddd}d| d |jddd  }fd	d
 fdd d _d  _t ddg}t|||d d d S )Nl   FjGb r   rs   e   r.   r/   r  rA   c                    s.   t  j|kr
J | _| |d  |d  S rb  )r   r   last_pr  )liner   r   r  G  s   z'TestCurveFit.test_gh13670.<locals>.linec                    s0   t  j|kr
J | _t | t | gjS r   )r   r   r  r
   r   r   r  )r<   r   r   r<   L  s   z&TestCurveFit.test_gh13670.<locals>.jacr   g      @r   r  )r   r   r  r   r  r  r
   r   )r   r  r   r   r   r   )r<   r  r   test_gh13670>  s   zTestCurveFit.test_gh13670r1  r2  c              	   C   sZ   dd }d}t jt|d t|| j| jdgd|d W d    d S 1 s&w   Y  d S )Nc                 S   r	  r   r   r
  r   r   r   r[   Z  r|   z9TestCurveFit.test_gh20155_error_mentions_x0.<locals>.funcz+Initial guess is outside of provided boundsr}   r/   )r   i  r  )r   r	   r{   r   r   r   )r   r   r[   messager   r   r   test_gh20155_error_mentions_x0V  s   "z+TestCurveFit.test_gh20155_error_mentions_x0N),r"   r#   r$   r   r  r  r  r*  rD  rG  r   r   thread_unsaferK  rO  staticmethodr^  r   ra  rh  rj  rm  rn  rp  rv  r   r  r  r  r  r  r  r  r  r  r  r  r  r  filterwarningsr  r  r  r  r   r   r   r   r    s\    
0	



	"$
62

	
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   $   dd }d}t ||}t|d d S )Nc                 S      d|  S NrC   r   rq   r   r   r   r[   f  r|   z0TestFixedPoint.test_scalar_trivial.<locals>.funcr   rD   r   r   r   r[   r\   r   r   r   r   test_scalar_triviald     
z"TestFixedPoint.test_scalar_trivialc                 C   r  )Nc                 S      | d S r   r   rq   r   r   r   r[   n  r|   z/TestFixedPoint.test_scalar_basic1.<locals>.func?r   r  r  r   r   r   test_scalar_basic1l  r  z!TestFixedPoint.test_scalar_basic1c                 C   r  )Nc                 S   r  NrA   r   rq   r   r   r   r[   v  r|   z/TestFixedPoint.test_scalar_basic2.<locals>.funcr  r   r  r  r   r   r   test_scalar_basic2t  r  z!TestFixedPoint.test_scalar_basic2c                 C   sX   dd }ddg}t jdd t||}W d    n1 sw   Y  t|ddg d S )Nc                 S   r  r  r   rq   r   r   r   r[   }  r|   z/TestFixedPoint.test_array_trivial.<locals>.funcg333333?g333333?ignorer   rD   )r   errstater   r   r  r   r   r   test_array_trivial|  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 s'w   Y  t|d|  d S )	Nc                 S      || d  S r   r   r   r   r   r   r   r[     r!   z.TestFixedPoint.test_array_basic1.<locals>.funcg      ?r   g      ?)皙?gffffff?g?r  r  rb   r   )r
   r   r  r   r   r   r[   r   r\   r   r   r   r   test_array_basic1  s   z TestFixedPoint.test_array_basic1c                 C   s>   dd }t g d}g d}t|||fd}t||d  d S )Nc                 S   r  r  r   r  r   r   r   r[     r!   z.TestFixedPoint.test_array_basic2.<locals>.funcr  )g?r   r   rb   r.   )r
   r   r   r!  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 )Nr  rC   r   )xxr   r   r   r(     s    z.TestFixedPoint.test_lambertw.<locals>.<lambda>r   r   g-q=i  )rF   xtolmaxiterr  rC   r/   r.   )r   r   r   r   r   )r   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.   rH  r  gjt?r/   c                    s,   t  |  t  |  | d   d S r  r  )r:   i0klksr   r   r[     s   ,z1TestFixedPoint.test_no_acceleration.<locals>.func	iterationr   )r   r   )r   mn0r[   r:   r   r)  r   test_no_acceleration  s   $z#TestFixedPoint.test_no_accelerationN)r"   r#   r$   r  r  r  r  r"  r#  r(  r0  r   r   r   r   r  b  s    
	r  ).r%   r  r   r   numpy.testingr   r   r   r   r   r   r   r	   rX   numpyr   r
   r   multiprocessing.poolr   scipyr   r   scipy.specialr   scipy.optimize._minpack_pyr   r   r   scipy.optimizer   scipy.optimize._minimizer   r   r&   r-   r7   r=   r>   r?   r   r   r   r   r  r  r   r   r   r   <module>   sB    $	l       ,