a
    Re                     @   s  d dl Z d dlmZ d dlmZmZmZmZmZm	Z	 d dl
Zd dl
mZmZmZmZmZmZmZmZ d dlmZmZmZmZ d dlmZ d dlmZmZ  dee!j" Z#ee!j"Z$ej%ej&ej'ej(ej)gZ*ejgZ+e*e+ Z,d	d
 Z-dd Z.dd Z/dd Z0dd Z1dd Z2dd Z3edd Z4G dd dZ5G dd de5Z6G dd de5Z7dd  Z8d!d" Z9d#d$ Z:G d%d& d&Z;d'd( Z<d)d* Z=d+d, Z>d-d. Z?d/d0 Z@d1d2 ZAe jBCd3d4ejDfd5ejEfgd6d7 ZFd8d9 ZGe jBCd:g d;d<d= ZHe jBCd>g d;d?d@ ZIe jBCd:g d;e jBCdAdBdCgdDdE ZJdFdG ZKe jBCd:g d;e jBCdAdBdCgdHdI ZLdJdK ZMdLdM ZNe jBCd>e,dNdO ZOdS )P    N)	lru_cache)assert_warnsassert_assert_allcloseassert_equalassert_array_equalsuppress_warnings)finfopowernanisclosesqrtexpsincos)	_zeros_pynewtonroot_scalarOptimizeResult)getfullargspec_no_self)	get_tests	functions   c                 C   s   | d d|   d S N       xr   r   S/var/www/sunrise/env/lib/python3.9/site-packages/scipy/optimize/tests/test_zeros.pyf1!   s    r    c                 C   s   d|  d S Nr   r   r   r   r   r   f1_1%   s    r"   c                 C   s   dd|   S N       @r   r   r   r   r   r   f1_2)   s    r%   c                 C   s   t | t| t| fS N)r    r"   r%   r   r   r   r   f1_and_p_and_pp-   s    r'   c                 C   s   t | t|  S r&   r   r   r   r   r   r   f22   s    r)   c                 C   s   t | t|  S r&   )r   r   r   r   r   r   f2_16   s    r*   c                 C   s   t | t|  S r&   r(   r   r   r   r   f2_2:   s    r+   c                 C   s   | S r&   r   r   r   r   r   f_lrucached?   s    r,   c                   @   sJ   e Zd Zdeej Zdeej Zd	ddZ	d
ddZ
dddZdS )TestScalarRootFindersr   Nc              
   K   s   g }|p
g D ].}||vr,dddd ||}|||  qtf i |}|ddd |p`g D ]}|| ||< qb| d}	| d	d
}
z"||d	|
i|\}}|	||fW S  ty   |	ttddtj|f Y S 0 d S )Nx0x1f)abfuncTFfull_outputdisprootargsr   )	getappenddictupdate	ExceptionzerosRootResultsr   Z
_EVALUEERR)selftcmethodsig_args_keyssig_kwargs_keyskwargsZmethod_argskZmethod_kwargsr7   	func_argsrrrr   r   r   _run_one_testJ   s     
z#TestScalarRootFinders._run_one_testc                    s  t }t|j  t|j}t|j| }|jd| g |dv rv|dv rjd |dv rjd jd< njd< jd	< fd
d|D }	pg dd |	D }
fdd|
D }
dd |
D }t	t||gdg g jjd}|j
f i  |d	 |d|d  dd |	D }dd |D }dd |D } fddt|||D }dd |D }dd t||D }t	|t|gg dg dS )zRun test-cases using the specified method and the supplied signature.

        Extract the arguments for the method call from the test case
        dictionary using the supplied keys for the method's signature.N)secantr   halley)r   rM   fprime)rM   fprime2tolxtolrtolc                    s*   g | ]"}t j|fd  qS ))rD   rE   )listrK   ).0rB   )rF   rC   rA   rD   rE   r   r   
<listcomp>y   s   z3TestScalarRootFinders.run_tests.<locals>.<listcomp>c                 S   s   g | ]}|d  j s|qS r   	convergedrT   eltr   r   r   rU          c                    s    g | ]}|d  d  vr|qS r9   IDr   rY   
known_failr   r   rU      r[   c                 S   s   g | ]}|d  d qS r\   r   rY   r   r   r   rU      r[   r   rQ   rR   c                 S   s   g | ]}|d  j r|qS rV   rW   rY   r   r   r   rU      r[   c                 S   s   g | ]}|d  j qS rV   r7   rY   r   r   r   rU      r[   c                 S   s   g | ]}|d  qS r   r   rY   r   r   r   rU      r[   c                    s<   g | ]4\}}}t || d s|d d vr|g| qS ))rR   atolr9   r]   )r   )rT   r1   crZ   )rc   r_   rR   r   r   rU      s   c                 S   s2   g | ]*\}}}}|d  |g| dt R  qS )r0   r8   )r:   tuple)rT   Zarootrd   ZfulloutrB   r   r   r   rU      s   
c                 S   s"   g | ]\}}|d kr|g| qS rb   r   )rT   ZfvrZ   r   r   r   rU      r[   )_getfullargspecr   
kwonlyargslendefaultsr8   r;   rQ   rR   r   r=   r:   zip)rA   testsrC   namer_   rF   sigZ	nDefaultsZ	nRequiredresultsZnotcvgdZnotcvged_IDSZtolsZcvgdZapproxcorrectZnotcloseZfvsr   )rc   r_   rF   rC   rR   rA   rD   rE   r   	run_testsa   sF    




zTestScalarRootFinders.run_testsc                 K   s*   t ||d}| j|||fd|i| dS )zuRun a collection of tests using the specified method.

        The name is used to determine some optional arguments.
smoothnessr_   N)r   rp   )rA   
collectionrC   rl   rr   r_   rF   rk   r   r   r   run_collection   s    z$TestScalarRootFinders.run_collection)NN)N)NN)__name__
__module____qualname__npr	   floatepsrQ   rR   rK   rp   rt   r   r   r   r   r-   D   s     

6  r-   c                   @   s   e Zd Zejdeejdedd Zejdeejdedd Z	ejdedd Z
ejdejejejgd	d
 Zejdedd ZdS )TestBracketMethodsrC   functionc                 C   sP   dt d }}t||j||g|| j| jd}|js6J t|jd| j| jd d S )N      ?   )rC   bracketr.   rQ   rR         ?rc   rR   )r   r   ru   rQ   rR   rX   r   r7   )rA   rC   r|   r1   r2   rI   r   r   r   test_basic_root_scalar   s    
z)TestBracketMethods.test_basic_root_scalarc                 C   sL   dt d }}||||| j| jdd\}}|js4J t|d| j| jd d S )Nr}   r~   T)rQ   rR   r5   r   r   )r   rQ   rR   rX   r   )rA   rC   r|   r1   r2   r7   rI   r   r   r   test_basic_individual   s    

z(TestBracketMethods.test_basic_individualc                 C   s   | j d||jdd d S )Napsr   rq   )rt   ru   )rA   rC   r   r   r   test_aps_collection   s    z&TestBracketMethods.test_aps_collectionc                 C   s,   |t jkrdhni }| jd||j|d d S )Nzfun7.4Zchandrupatlar^   )r?   ridderrt   ru   )rA   rC   r_   r   r   r   test_chandrupatla_collection   s    z/TestBracketMethods.test_chandrupatla_collectionc                 C   s4   d\}}|t ||dd\}}|js&J t|d d S )N)r9   r   Tr5   r   )r,   rX   r   )rA   rC   r1   r2   r7   rI   r   r   r   test_lru_cached_individual   s    
z-TestBracketMethods.test_lru_cached_individualN)ru   rv   rw   pytestmarkparametrizebracket_methodststutils_functionsr   r   r   r?   bisectr   toms748r   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ejd#d$d%gd&d' Zd(S ))
TestNewtonc                 C   s6   dg}|ddg7 }dD ]}| j |tjdd|d qd S )N	aps.13.00z	aps.12.05	aps.12.17r   complexr   r   rr   r_   rt   r?   r   rA   r_   rs   r   r   r   test_newton_collections   s    z"TestNewton.test_newton_collectionsc                 C   s,   g d}dD ]}| j |tjdd|d qd S )N)z	aps.12.06z	aps.12.07z	aps.12.08z	aps.12.09z	aps.12.10z	aps.12.11z	aps.12.12z	aps.12.13z	aps.12.14z	aps.12.15z	aps.12.16r   z	aps.12.18r   r   rM   r   r   r   r   r   r   r   test_halley_collections   s
    z"TestNewton.test_halley_collectionsc                 C   s   t ttftttffD ]\}}}tj|ddd}t||ddd tj|dddd}t||ddd tj|d|dd}t||ddd tj|d||dd	}t||ddd qd S )
Nr~   ư>)rP   r   rc      )r/   rP   )rN   rP   )rN   rO   rP   )	r    r"   r%   r)   r*   r+   r?   r   r   )rA   r0   f_1f_2r   r   r   r   test_newton   s    zTestNewton.test_newtonc                 C   s   t ttftttffD ]0\}}}t|dd|dd}t||jddd qt ttftttffD ].\}}}t|dddd}t||jddd qZdS )	z#Invoke newton through root_scalar()r   r~   r   )rC   r.   rN   rQ   r   r   rC   r.   rQ   N	r    r"   r%   r)   r*   r+   r   r   r7   rA   r0   r   r   rI   r   r   r   test_newton_by_name   s    zTestNewton.test_newton_by_namec                 C   s   t ttftttffD ]V\}}}t|ddddd}t||jddd t|ddddd}t||jddd qt ttftttffD ].\}}}t|dddd	}t||jddd qd
S )z#Invoke secant through root_scalar()rL   r~   r   r   rC   r.   r/   rQ   r   r   r   r   Nr   r   r   r   r   test_secant_by_name   s    zTestNewton.test_secant_by_namec              	   C   sL   t ttftttffD ]2\}}}t|dd||dd}t||jddd qdS )z#Invoke halley through root_scalar()rM   r~   r   )rC   r.   rN   rO   rQ   r   r   Nr   r   r   r   r   test_halley_by_name   s
    zTestNewton.test_halley_by_namec                 C   s   d}t jt|d" ttdtddd W d    n1 s:0    Y  d}t jt|d" ttdtddd W d    n1 s~0    Y  d S )	Nz$fprime2 must be specified for halleymatchrM   r~   r   )rC   rN   r.   rQ   z#fprime must be specified for halley)rC   rO   r.   rQ   )r   raises
ValueErrorr   r    r"   r%   )rA   messager   r   r   test_root_scalar_fail  s    0z TestNewton.test_root_scalar_failc           
      C   s   dd }dd }dd }t g d}t tdd	 d
 }||ddddf}d
gd }t||||}d}	t||	 tj|||||d}t||	 tj|||d}t||	 dS )ztest newton with arrayc                 W   sJ   |d | |d   }|d |d t ||d  d   ||d   |  S )Nr   r~   r   r   r   r   r   rx   r   r   r1   r2   r   r   r   r      s    z(TestNewton.test_array_newton.<locals>.f1c                 W   sP   |d |d  }|d  t |d |d  | |   | |d |d   d S )Nr~   r   r   r   r   r   r   r   r   r   r   r"     s    z*TestNewton.test_array_newton.<locals>.f1_1c                 W   s@   |d |d  }|d  t |d |d  | |   |d  S )Nr~   r   r   r   r   r   r   r   r   r%     s    z*TestNewton.test_array_newton.<locals>.f1_2)
g4O@gNk@g]0J@g]Qݚt@g~EO5@g$J ?g~5,@gXCڭ@gͮ9@@gӍ@
   r   g      @g&.>gMbp?gn2d?)
gԩ˰@g9~4b'@gaq(@g0p@gk"z?g4se?gU+F@gwQu%@gd6)@g)i!@)rO   r8   N)rx   arrayr   ranger?   r   r   )
rA   r    r"   r%   Za0Za1r8   r.   r   Z
x_expectedr   r   r   test_array_newton  s    


zTestNewton.test_array_newtonc                 C   s   dd }dd }t dd}tj|||d}t||d t d}tj|||d}t||d t||}t||d d S )	Nc                 S   s   | d d S )Nr                 ?r   r   r   r   r   r0   7  s    z/TestNewton.test_array_newton_complex.<locals>.fc                 S   s   dS Nr   r   r   r   r   r   rN   :  s    z4TestNewton.test_array_newton_complex.<locals>.fprimer   r   )rN           )rx   fullr?   r   r   Zones)rA   r0   rN   tr   r   r   r   test_array_newton_complex6  s    
z$TestNewton.test_array_newton_complexc                 C   s2   t jdd ddgtddggd}t|d d	S )
z8test secant doesn't continue to iterate zero derivativesc                 W   s   | |  |d  S )Nr   r   r   r1   r   r   r   <lambda>K  r[   z>TestNewton.test_array_secant_active_zero_der.<locals>.<lambda>gˡE}@r         )r.   r8   )f~@g      @N)r?   r   rx   r   r   rA   r   r   r   r   !test_array_secant_active_zero_derI  s    z,TestNewton.test_array_secant_active_zero_derc                 C   sX   t jdd dgd ddgfd}t|d t jd	d d
gd ddgfd}t|d d S )Nc                 S   s   || d  S r!   r   yzr   r   r   r   Q  r[   z7TestNewton.test_array_newton_integers.<locals>.<lambda>      @r   g      .@g      1@r   )gNO@r   c                 S   s   || d  S r!   r   r   r   r   r   r   U  r[   r      r   )r?   r   r   r   r   r   r   test_array_newton_integersO  s    
 z%TestNewton.test_array_newton_integersc                 C   s   t ttjdd ddgdd  ttV tjdd ddgdd dd}t|jd	 |j	 sbJ |j
 rpJ W d    n1 s0    Y  d S )
Nc                 S   s   | d d S r!   r   r   r   r   r   r   [  r[   z@TestNewton.test_array_newton_zero_der_failures.<locals>.<lambda>r   c                 S   s   d|  S r!   r   r   r   r   r   r   [  r[   c                 S   s   | d d S r!   r   r   r   r   r   r   ^  r[   c                 S   s   d|  S r!   r   r   r   r   r   r   _  r[   Tr   r   )r   RuntimeWarningr?   r   r   warnsr   r7   Zzero_derallrX   any)rA   rn   r   r   r   #test_array_newton_zero_der_failuresX  s    z.TestNewton.test_array_newton_zero_der_failuresc                 C   s   dd }dd }dd }dd }t |d	d
|d}t |d	d
dd}t|j|jdd td|j |j t |dd
||d}t |dd
dd}t|j|jdd td
|j |j d S )Nc                 S   s   | d d|   d S r   r   r   r   r   r   r    e  s    z+TestNewton.test_newton_combined.<locals>.f1c                 S   s   d|  d S r!   r   r   r   r   r   r"   g  s    z-TestNewton.test_newton_combined.<locals>.f1_1c                 S   s   dd|   S r#   r   r   r   r   r   r%   i  s    z-TestNewton.test_newton_combined.<locals>.f1_2c                 S   s"   | d d|   d d|  d dfS )Nr   r   r$   r   r   r   r   r   r'   l  s    z8TestNewton.test_newton_combined.<locals>.f1_and_p_and_ppr   r~   )rC   r.   rN   T:0yE>r   r   rM   )rC   r.   rN   rO   )rC   r.   rO   )r   r   r7   r   function_calls)rA   r    r"   r%   r'   Zsol0Zsolr   r   r   test_newton_combinedd  s    zTestNewton.test_newton_combinedc           
   	   C   sv  d}g d}t dD ]Z}ddd}dtgdtggd | D ]\}}|||< q<tjt|fdd	i|\}}t|j t||j	 t|j
|jf||  |d
kr|j|j
d ksJ nt|j|d |j
  |j
d }	tjt|f|	d	d|\}}t|j  t||j	 t|j
|	 |dkrtjtd|	 d. tjt|f|	dd|\}}W d    q1 sf0    Y  qd S )Nr~   ))      )r   r   )r~   	   r   T)rP   r5   rN   rO   r6   Fr   r   )maxiterr6   z3Failed to converge after %d iterations, value is .*r   )r   r"   r%   r?   r   r    r   rX   r   r7   
iterationsr   r   r   RuntimeError)
rA   r.   Zexpected_countsZderivsrF   rG   vr   rI   Zitersr   r   r   test_newton_full_outputy  s0    
 


z"TestNewton.test_newton_full_outputc                 C   sf   dd }dd }t ttj|d|dd tjtdd	 t|d| W d    n1 sX0    Y  d S )
Nc                 S   s   | d d S )Nr   r$   r   r   r   r   r   r3     s    z0TestNewton.test_deriv_zero_warning.<locals>.funcc                 S   s   d|  S r!   r   r   r   r   r   dfunc  s    z1TestNewton.test_deriv_zero_warning.<locals>.dfuncr   Fr6   zDerivative was zeror   )r   r   r?   r   r   r   r   )rA   r3   r   r   r   r   test_deriv_zero_warning  s
    z"TestNewton.test_deriv_zero_warningc                 C   s4   t ddg}| }tt j|t j t|| d S )N皙?r~   )rx   r   copyr   r   r   r   )rA   r.   Zx0_copyr   r   r   test_newton_does_not_modify_x0  s    z)TestNewton.test_newton_does_not_modify_x0c                 C   s  t tdddd}t tddddd}ttddddd	d
 }tt|jddd |jjt ks^J tt|jddd |jjt ksJ tt|jddd |jjt ksJ |j|j  kr|jksn J |j|jd
   kr|j  kr|j  kr|jd ksn J d S )Nr   r~   r   r   rL   r   r   T)r.   r/   rP   r5   r   r   r   )	r   r    r   r   r7   shapere   r   r   )rA   Zres_newton_defaultZres_secant_defaultZ
res_secantr   r   r   test_gh17570_defaults  s2    



z TestNewton.test_gh17570_defaultsrC   rL   r   c                 C   sL   dd }t |d|d}|js J tt|jd |jjttjksHJ d S )Nc                 S   s   | d d S )Nr   r   r   r   r   r   r0     s    z)TestNewton.test_int_x0_gh19280.<locals>.fr   )r.   rC   g;f?)r   rX   r   absr7   dtyperx   float64)rA   rC   r0   resr   r   r   test_int_x0_gh19280  s
    
zTestNewton.test_int_x0_gh19280N)ru   rv   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s&   		'	%	r   c                     s\   d  fdd} t jt jg}t }}|D ].}|| dd||d}t |||d|j d q(d S )	Nr   c                    s   |   S r&   r   r   ra   r   r   r0     s    ztest_gh_5555.<locals>.fg    חg    cAr`   z	method %s)rc   rR   err_msg)r?   r   r   TOLr   ru   )r0   methodsrQ   rR   rC   r   r   ra   r   test_gh_5555  s    
r   c                  C   sP   dd } d}dt  }tjtjg}|D ]&}|| dd||d}td|||d	 q$d S )
Nc                 S   s   | dk rdS | d S d S )Nr}   g333333?r   r   r   r   r   r0     s    ztest_gh_5557.<locals>.fgRQ?r   r   r   r`   r   r   )
_FLOAT_EPSr?   brentqbrenthr   )r0   rc   rR   r   rC   r   r   r   r   test_gh_5557  s    
r   c                     sZ   d} d}| |fD ]D\}}}t | tjtjfD ]"}| fdd||}t|| q0qd S )N)g      |g     ug      y)g     u@g      |@g      y@c                    s   t |   S r&   r   r   rd   r   r   r     r[   z9test_brent_underflow_in_root_bracketing.<locals>.<lambda>)rx   r   r?   r   r   r   )Zunderflow_scenarioZoverflow_scenarior1   r2   r7   rC   r   r   r   r   'test_brent_underflow_in_root_bracketing   s    
r   c                   @   s.   e Zd ZejdddddZdd Zdd	 Zd
S )TestRootResultsr   ,   .   r   )r7   r   r   flagc                 C   s   d}t t| j| d S )Nzm      converged: True
           flag: converged
 function_calls: 46
     iterations: 44
           root: 1.0)r   reprrI   )rA   Zexpected_reprr   r   r   	test_repr  s    zTestRootResults.test_reprc                 C   s   t | jtsJ d S r&   )
isinstancerI   r   )rA   r   r   r   	test_type  s    zTestRootResults.test_typeN)ru   rv   rw   r?   r@   rI   r   r   r   r   r   r   r     s   r   c                  C   s   dd } dd }dd }t dd}d	}tj| ||||d
d}t| |g|R  dd
d |gd }d	}tj| ||||d
d}t| |g|R  dd
d dS )z&Test Halley's works with complex rootsc                 W   s$   |d | d  |d |   |d  S )Nr   r   r   r   r   r   r   r   r0     s    ztest_complex_halley.<locals>.fc                 W   s   d|d  |  |d  S )Nr   r   r   r   r   r   r   r   r   !  s    z test_complex_halley.<locals>.f_1c                 W   s>   d|d  }zt | }W n ty.   | Y S 0 |g| S d S )Nr   r   )rh   	TypeError)r   r1   retvalsizer   r   r   r   $  s    
z test_complex_halley.<locals>.f_2r   r$   )r$   g      @r   r   )r8   rN   rO   rP   r   r   r   N)r   r?   r   r   )r0   r   r   r   Zcoeffsr   r   r   r   test_complex_halley  s    	

r   c                  C   s  t tjd } d|  d|   }t 4}|td tjdd |gd d}W d	   n1 s`0    Y  t	|d
gd  d}t 0}|td tjdd |dd}W d	   n1 s0    Y  t	|d t
jtdd$ tjdd |dd}W d	   n1 s0    Y  d}t 0}|td tjdd |dd}W d	   n1 sT0    Y  t	|d t
jtdd$ tjdd |dd}W d	   n1 s0    Y  d	S )zBTest secant method with a non-zero dp, but an infinite newton stepgQ?g      i@r$   zRMS ofc                 S   s   | d d S )Ng      Y@r   r   r   r   r   r   r   E  r[   z%test_zero_der_nz_dp.<locals>.<lambda>r   r.   Nd   g.ЗK.?Tolerance ofc                 S   s   | d d S Nr   r   r   r   r   r   r   r   K  r[   F)r.   r6   r   r   c                 S   s   | d d S r  r   r   r   r   r   r   N  r[   Tg.ЗK.c                 S   s   | d d S r  r   r   r   r   r   r   R  r[   r9   c                 S   s   | d d S r  r   r   r   r   r   r   U  r[   )rx   r	   ry   rz   r   filterr   r?   r   r   r   r   r   )ZdxZp0supr   r   r   r   test_zero_der_nz_dp8  s(    62
44
r  c                     s   d} d d}d}d}|| |  | } fdd}t t: tj|g dd	|| gd
d}|j rfJ W d   n1 sz0    Y  t t. tj|dgd	 d	|| gd
d}W d   n1 s0    Y  dS )z(Test that array newton fails as expectedr   ga2U0*#?g@g\mJA?gCl@c              	      s6   dt |  dt  d | d| t |     S )Nr   r   g@gGz@)rx   r   log10)Zdarcy_frictionreZdiaZ	roughnessr   r   colebrook_eqne  s
    z1test_array_newton_failures.<locals>.colebrook_eqn){Gz?g?gvÖ?g333333?r   T)r.   r   r8   r5   Nr	  )	r   r   r   r?   r   rX   r   r   r   )ZdiameterrhomuuZreynolds_numberr  resultr   r  r   test_array_newton_failuresX  s$    
,r  c                  C   sT  dd } t j| dd}t|dt jt jd t j| dgd d}t|dt jt jd dd }d	d
 }t j| d|d}t|dt jt jd t j| d||d}t|dt jt jd t j| dgd |d}t|dt jt jd t j| dgd ||d}t|dt jt jd t j| d|d}t|dt jt jd t j| dgd |d}t|dt jt jd dS )z@Test that Newton or Halley don't warn if zero derivative at rootc                 S   s   | d | d  S Nr~   r   r   r   r   r   r   f_zeroder_root~  s    z9test_gh8904_zeroder_at_root_fails.<locals>.f_zeroder_rootr   r   r   r   c                 S   s   d| d  d|   S r  r   r   r   r   r   fder  s    z/test_gh8904_zeroder_at_root_fails.<locals>.fderc                 S   s   d|  d S )Nr   r   r   r   r   r   r   fder2  s    z0test_gh8904_zeroder_at_root_fails.<locals>.fder2)r.   rN   )r.   rN   rO   r}   N)r?   r   r   Z_xtolZ_rtol)r  rI   r  r  r   r   r   !test_gh8904_zeroder_at_root_failsz  s.    
r  c                     sn   d  fdd}  fdd} fdd}d}t | ||d	d
\}}|jsJJ t | |||d	d\}}|jsjJ dS )zzTest that Halley's method realizes that the 2nd order adjustment
    is too big and drops off to the 1st order adjustment.r   c                    s   t | d  t  d   S r   r
   r   nr   r   r0     s    ztest_gh_8881.<locals>.fc                    s   t | d      S r   r  r   r  r   r   fp  s    ztest_gh_8881.<locals>.fpc                    s*   t | dd     d   d     S r  r  r   r  r   r   fpp  s    ztest_gh_8881.<locals>.fppr   T)rN   r5   rN   rO   r5   N)r   rX   )r0   r  r  r.   rtrI   r   r  r   test_gh_8881  s    
r  c            	      C   s   dd } dd }dd }t jdgt jd}t| |||d	d
\}}|jsJJ t jddgt jd}tt$ tj| |||d	d
}W d   n1 s0    Y  dd }tj| |||d	d
}|j	 sJ dS )z_
    Test that shape is preserved for array inputs even if fprime or fprime2 is
    scalar
    c                 S   s   | d S r!   r   r   r   r   r   r0     s    z,test_gh_9608_preserve_array_shape.<locals>.fc                 S   s   d|  S r!   r   r   r   r   r   r    s    z-test_gh_9608_preserve_array_shape.<locals>.fpc                 S   s   dS r!   r   r   r   r   r   r    s    z.test_gh_9608_preserve_array_shape.<locals>.fppr   r   Tr  Nc                 S   s   t jt | dt jdS )Nr   r  )rx   r   r   float32r   r   r   r   	fpp_array  s    z4test_gh_9608_preserve_array_shape.<locals>.fpp_array)
rx   r   r  r   rX   r   r   
IndexErrorr?   r   )	r0   r  r  r.   r  rI   Zx0_arrayr  r  r   r   r   !test_gh_9608_preserve_array_shape  s     

$
r!  z maximum_iterations,flag_expectedr   r   c                 C   sp   t jdd ddddd| ddd		}|d
 j|ks2J |t jkrP|d
 j| kslJ n|t jkrl|d
 j| k slJ dS )z]
    Test that if the maximum iterations is exceeded that the flag is not
    converged.
    c                 S   s   d|  d |  d |  d S )Ng333333?gffffff@g333333@g      @r   r   r   r   r   r     r[   z6test_gh9254_flag_if_maxiter_exceeded.<locals>.<lambda>i   r   r   TFr4   r   N)r?   r   r   CONVERRr   	CONVERGED)Zmaximum_iterationsZflag_expectedr  r   r   r   $test_gh9254_flag_if_maxiter_exceeded  s    

r%  c                  C   s   dd } dd }t ttj| d|dd tjtdd	 t| d| W d
   n1 sX0    Y  t| tdd|}t|tdd d
S )zBTest that if disp is true then zero derivative raises RuntimeErrorc                 S   s   | |  d S Nr   r   r   r   r   r   r0     s    z/test_gh9551_raise_error_if_disp_true.<locals>.fc                 S   s   d|  S r!   r   r   r   r   r   f_p  s    z1test_gh9551_raise_error_if_disp_true.<locals>.f_pr   Fr   zY^Derivative was zero\. Failed to converge after \d+ iterations, value is [+-]?\d*\.\d+\.$r   Ng      $@r   )	r   r   r?   r   r   r   r   r   r   )r0   r'  r7   r   r   r   $test_gh9551_raise_error_if_disp_true  s    ,r(  solver_name)r   r   r   r   r   c                 C   sP   dd }t t| }tjtdd ||dd W d    n1 sB0    Y  d S )Nc                 S   s   t jS r&   )rx   r   r   r   r   r   r0     s    ztest_gh3089_8394.<locals>.fzThe function value at x...r   r   r   )getattrr?   r   r   r   )r)  r0   solverr   r   r   test_gh3089_8394  s    
r,  rC   c                    sf    fdd d _ t d| d}|jdu s.J |jds>J |j j ksNJ t|j|jv sbJ d S )Nc                    s     j d7  _ tjS r&  )_countrx   r   r   r0   r   r   r0      s    ztest_gh18171.<locals>.fr   )r   r   )r   rC   FzThe function value at x)r-  r   rX   r   
startswithr   strr7   )rC   r   r   r.  r   test_gh18171  s    r1  rs_interfaceTFc                    sf   |rdd nt t| } fdd d _| dddd}|rN|j jksbJ n|d	 j jksbJ d S )
Nc                 [   s   t | ||fdS N)r   r   r0   r1   r2   rF   r   r   r   r   2  r[   z%test_function_calls.<locals>.<lambda>c                    s     j d7  _ | d d S )Nr   r   )callsr   r.  r   r   r0   5  s    ztest_function_calls.<locals>.fr   r   Tr   r   )r*  r?   r6  r   )r)  r2  r+  r   r   r.  r   test_function_calls,  s    
r7  c                  C   s   dd } t jtdd  t| dddd}W d	   n1 s<0    Y  |jrPJ |jd
ks^J t jtdd& t| dddddd }W d	   n1 s0    Y  |jrJ |jd
ksJ d	S )zDTest that zero slope with secant method results in a converged=Falsec                 S   s   | t |  |   d S )NgQ?r   r   r   r   r   lhsD  s    z*test_gh_14486_converged_false.<locals>.lhsr   r   rL   g333333ÿr   )rC   r.   r/   Nzconvergence errorFT)r.   r/   r6   r5   r   )r   r   r   r   rX   r   r   )r8  r   r   r   r   test_gh_14486_converged_falseB  s    .
4
r9  c                 C   s   |rdd nt t| }dd }tjtdd  ||ddd	d
 W d    n1 sR0    Y  ||ddd	d
}|rt|n|d }|jsJ t|jddd ||dtdd	d
}|r|n|d }|jsJ t|jddd d S )Nc                 [   s   t | ||fdS r3  r4  r5  r   r   r   r   X  r[   ztest_gh5584.<locals>.<lambda>c                 S   s   d|  S )NgN~hr   r   r   r   r   r0   [  s    ztest_gh5584.<locals>.fz...must have different signsr   g      gٿTr   g?r   r   r   r   z-0.0)	r*  r?   r   r   r   rX   r   r7   ry   )r)  r2  r+  r0   r   r   r   r   test_gh5584R  s    
.

r:  c                  C   s   dd } d}t tj}tj| dd|d| d}| |}tj| dd|d| d}| |}||k sdJ d	|d
 dd|dd}tjt|d( tj| dd||d
 d W d    n1 s0    Y  d S )Nc                 S   s   | d d|   d S )Nr~   r   r   r   r   r   r   r   r0   u  s    ztest_gh13407.<locals>.fgYng|=g    _Br   r`   r   zrtol too small \(r   gz < z\)r   )	rx   r	   ry   rz   r?   r   r   r   r   )r0   rQ   rz   r/   r    Zx4Zf4r   r   r   r   test_gh13407o  s    r<  c                  C   sD   dd } t | d}t|ddd t| dddd	}t|jddd d S )
Nc                 S   s   | d S r&  r   )r   r   r   r   r0     s    z&test_newton_complex_gh10103.<locals>.fy      ?      ?r   g-q=r   y       @      ?rL   )r.   r/   rC   )r   r   r   r7   )r0   r   r   r   r   test_newton_complex_gh10103  s
    
r=  c                 C   sF   d}t jt|d  | tdddd W d    n1 s80    Y  d S )Nz2'float' object cannot be interpreted as an integerr   r   r   gR@)r   )r   r   r   r    )rC   r   r   r   r   test_maxiter_int_check_gh10236  s    r>  )Pr   	functoolsr   Znumpy.testingr   r   r   r   r   r   numpyrx   r	   r
   r   r   r   r   r   r   Zscipy.optimizer   r?   r   r   r   Zscipy._lib._utilr   rf   Zscipy.optimize._tstutilsr   r   r   ry   rz   r   r   r   r   r   r   r   r   Zgradient_methodsZall_methodsr    r"   r%   r'   r)   r*   r+   r,   r-   r{   r   r   r   r   r   r   r  r  r  r  r!  r   r   r#  r$  r%  r(  r,  r1  r7  r9  r:  r<  r=  r>  r   r   r   r   <module>   s    (

\0   "0"


