o
    `^h                     @   sR   d Z ddlmZmZ ddlZddlmZmZ ddl	Z
ddlmZ G dd dZdS )z5
Unit tests for optimization routines from _root.py.
    )assert_assert_equalN)raiseswarnsrootc                   @   sV   e Zd Zdd Zdd Zdd Zdd Zejj	d	d
 Z
ejdg ddd ZdS )TestRootc                 C   s   dd }dd }dD ]W}|dv rq
|dv r|}nd }t |ddg|d	|d
}t |ddg|d|d
}| d||j d||j }t|j| t|j| tt||j t||j k | q
d S )Nc                 S   s&   | \}}t |d d |d d gS N      nparrayzxy r   ]/home/air/shanriGPT/back/venv/lib/python3.10/site-packages/scipy/optimize/tests/test__root.pyfunc      z)TestRoot.test_tol_parameter.<locals>.funcc                 S   s.   | \}}t d|d  dgdd|d  ggS )Nr
      r   r   r   r   r   r   dfunc   s   &z*TestRoot.test_tol_parameter.<locals>.dfunc)hybrlmbroyden1broyden2andersondiagbroydenkrylov)linearmixingexcitingmixing)r   r   皙?g-C6?)jactolmethod      ?z: z vs. )r   r   r   successabsmax)selfr   r   r%   r#   sol1sol2msgr   r   r   test_tol_parameter   s"    $zTestRoot.test_tol_parameterc                 C   s2   dd }dD ]}t tjtd|d|id qd S )Nc                 S   s   t | d S Nr   )r(   r   r   r   r   norm,   s   z$TestRoot.test_tol_norm.<locals>.norm)r!   r   r    r   r   r   r   r   tol_norm)r%   options)r   r   
zeros_likezeros)r*   r1   r%   r   r   r   test_tol_norm*   s   zTestRoot.test_tol_normc                 C   s    ddd}t |ddgdd d S )Nr   c                 S   s&   | \}}t |d d |d | gS r	   r   )r   fr   r   r   r   r   r   <   r   z=TestRoot.test_minimize_scalar_coerce_args_param.<locals>.funcr"   g      ?)argsr   r   )r*   r   r   r   r   &test_minimize_scalar_coerce_args_param:   s   
z/TestRoot.test_minimize_scalar_coerce_args_paramc                 C   sT   G dd d}| }t t t|ddgdd W d    d S 1 s#w   Y  d S )Nc                   @   s   e Zd Zdd Zdd ZdS )z!TestRoot.test_f_size.<locals>.func                 S   s
   d| _ d S r/   count)r*   r   r   r   __init__F   s   
z*TestRoot.test_f_size.<locals>.fun.__init__c                 S   s   |  j d7  _ | j d s |d d|d |d  d   d }|S |d d|d |d  d   d d|d |d  d  |d  g}|S )Nr      r   r&   r
   g      ?r;   )r*   r   retr   r   r   __call__I   s   
$"z*TestRoot.test_f_size.<locals>.fun.__call__N)__name__
__module____qualname__r=   r@   r   r   r   r   funE   s    rD   皙?g        r   )r%   )assert_raises
ValueErrorr   )r*   rD   Fr   r   r   test_f_sizeA   s
   
"zTestRoot.test_f_sizec                    s  dd  dd  fdd}t d}t |dd	d
}d}tt|d t |dd	d}W d    n1 s8w   Y  tt|d t||dd	dd}W d    n1 sWw   Y  t|j|j t|j|j |j|j  u r}|j  u r}du sJ  J d S )Nc                 S   s@   d| d  d| d d   d d| d d  d| d   d gS )	Nr
   r   g      ?r   r   
   rE   r>   r   r   ignoredr   r   r   rD   ]   s   @z#TestRoot.test_gh_10370.<locals>.func                 S   s    dd| d  gd| d  dggS )Nr
   r&   r   g?r   r>   r   rK   r   r   r   grad`   s    z$TestRoot.test_gh_10370.<locals>.gradc                    s    | || |fS )Nr   rK   rD   rM   r   r   fun_gradc   s   z(TestRoot.test_gh_10370.<locals>.fun_gradr   r9   r   )r8   r%   z'Method krylov does not use the jacobian)match)r8   r%   r#   T)r   r5   r   assert_warnsRuntimeWarningr   r   r'   )r*   rO   x0refmessageres1res2r   rN   r   test_gh_10370X   s   
0zTestRoot.test_gh_10370r%   )
r   r   r   r   r   r    r   r!   r   zdf-sanec                 C   s*   dd }t |dg|d}|j|ksJ d S )Nc                 S   s   | d S )Nr   r   r0   r   r   r   r   x   s   z,TestRoot.test_method_in_result.<locals>.funcr   )rS   r%   )r   r%   )r*   r%   r   resr   r   r   test_method_in_results   s   zTestRoot.test_method_in_resultN)rA   rB   rC   r.   r6   r:   rI   pytestmarkthread_unsaferX   parametrizerZ   r   r   r   r   r      s    
r   )__doc__numpy.testingr   r   r[   r   rF   r   rQ   numpyr   scipy.optimizer   r   r   r   r   r   <module>   s    