a
    h<                     @   sd   d dl Z d dlZd dlmZmZmZ d dlm  m	Z
 d dlmZ d dlmZmZ G dd dZdS )    N)assert_array_lessassert_allcloseassert_equal)stats)_differentiate_EERRORINCREASEc                	   @   sL  e Zd Zdd Zejddedddgdd	 Z	ejd
e
jjdd Zejdddgejde dddg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'ejdd(d(d)gfejd*ejejejfd+d, Zd-d. Zd/d0 Zejjejd
d1d2 dfd3d2 dffd4d5 Zd6S )7TestDifferentiatec                 C   s   t  |S N)r   normcdf)selfx r   c/var/www/html/assistant/venv/lib/python3.9/site-packages/scipy/optimize/tests/test_differentiate.pyf   s    zTestDifferentiate.fr   333333?皙?
   c                 C   sV   t | j|}t |}tj|j| t	t
|j| |j |jj|jksRJ d S r	   )differentiater   r   r
   pdfnpZtestingr   dfr   abserrorr   shape)r   r   resrefr   r   r   
test_basic   s
    zTestDifferentiate.test_basiccasec                 C   sL   |\}}t t|| }| d }t|j|}||}t|j|dd d S )Ng?绽|=atol)getattrr   Zmedianr   r   r   r   r   )r   r   distnameparamsdistr   r   r   r   r   r   test_accuracy   s    
zTestDifferentiate.test_accuracyorder      r   )   )      )r,      r.   c                    s<  |rt ddd|nd}t |t jfdd} fdd d	 _d
 _t |d}|| }dd |D }t	|j
 | t|j
j| dd |D }t	|j | t|jj| dd |D }	t	|j |	dd t|jj| dd |D }
t|j |
 t|jj| t |jjt js<J dd |D }t|j | t|jj| t |jjt jsJ dd |D }t|j | tt |j j t|jj|j
j t |jjt jsJ dd |D }t|j | tt |j j t|jj|j
j t |jjt js8J d S )Nr   r   r+   r   c                    s   t j|  dS )Nr(   )r   r   r   )r(   r   r   r   _differentiate_single,   s    zCTestDifferentiate.test_vectorization.<locals>._differentiate_singlec                    sR     j d7  _   j| jks(| jdkr,dn| jd 7  _j| g|R i |S )Nr)   )nitfevalsizendimr   r   )r   argskwargs)r   nr   r   r   r   0   s    ,z/TestDifferentiate.test_vectorization.<locals>.fr2   r   r/   c                 S   s   g | ]
}|j qS r   r0   .0r   r   r   r   
<listcomp>:       z8TestDifferentiate.test_vectorization.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r   )r   r:   r   r   r   r<   >   r=   c                 S   s   g | ]
}|j qS r   )r   r:   r   r   r   r<   B   r=   +<r!   c                 S   s   g | ]
}|j qS r   )successr:   r   r   r   r<   F   r=   c                 S   s   g | ]
}|j qS r   )statusr:   r   r   r   r<   K   r=   c                 S   s   g | ]
}|j qS r   )nfevr:   r   r   r   r<   P   r=   c                 S   s   g | ]
}|j qS r   )r3   r:   r   r   r   r<   V   r=   )r   linspacereshaper5   Z	vectorizer3   r4   r   ravelr   r   r   r   r   r   r?   
issubdtypedtypeZbool_r@   integerrA   max)r   r(   r   r   r1   r   refsZref_xZref_dfZ	ref_errorZref_successZref_flagZref_nfevZref_nitr   )r   r9   r(   r   r   test_vectorization$   sH    
z$TestDifferentiate.test_vectorizationc                    sr   t jd fdd d _t jdt jdf}t dgd dd	|d
}t tj	t
tjtjg}t|j| d S )N   ;O c                    sN    j d7  _ dd fdddd dd g  fddt| | D }|S )	Nr)   c                 S   s   | d S )N      @r   r0   r   r   r   <lambda>b   r=   z9TestDifferentiate.test_flags.<locals>.f.<locals>.<lambda>c                    s   t |    S r	   )r   exprandomr0   rngr   r   rM   c   r=   c                 S   s
   t | S r	   )r   rN   r0   r   r   r   rM   d   r=   c                 S   s   t | t jd S )Nr   )r   	full_likenanr0   r   r   r   rM   e   r=   c                    s   g | ]\}} | |qS r   r   )r;   r   jfuncsr   r   r<   f   r=   z;TestDifferentiate.test_flags.<locals>.f.<locals>.<listcomp>)r3   ziprD   )Zxsjsr   r   rQ   rU   r   r   `   s    
z'TestDifferentiate.test_flags.<locals>.fr   r-   rF   r)   +=r.   )rtolr(   r7   )r   rO   default_rngr3   ZarangeZint64r   arrayeim_ECONVERGEDr   	_ECONVERR
_EVALUEERRr   r@   )r   r7   r   	ref_flagsr   rY   r   
test_flags\   s    zTestDifferentiate.test_flagsc                    sR   t jd  fdd}t|ddddd}t tjttjtj	g}t
|j| d S )	NrK   c                    s2   | d t |    t | t | t jd gS )NrL   r   )r   rN   rO   rR   rS   r0   rP   r   r   r   v   s
    z6TestDifferentiate.test_flags_preserve_shape.<locals>.fr)   r[   r.   T)r\   r(   preserve_shape)r   rO   r]   r   r^   r_   r`   r   ra   rb   r   r@   )r   r   r   rc   r   rP   r   test_flags_preserve_shapes   s    z+TestDifferentiate.test_flags_preserve_shapec              	   C   s   dd }d}ddt d|  ddt d|   dt d|  |d d  dt d|  |d   g}t||d	d
}t|j| d S )Nc                 S   s:   | t d|  | t d|   t d|  | d d  gS )Nr,   r      r)   r.   )r   sinr0   r   r   r   r      s    z0TestDifferentiate.test_preserve_shape.<locals>.fr   r)   r,   r   rg   r.   Tre   )r   cosrh   r   r   r   )r   r   r   r   r   r   r   r   test_preserve_shape   s    &6z%TestDifferentiate.test_preserve_shapec           	      C   sF  t  }d}|j}||}tdddd}| }d|d< t||fi |}tt|j	| d d|d< t||fi |}tt|j	| d tt|j	| t|j	|  | }d|d< t||fi |}tt|j	| dt
|  d|d< t||fi |}tt|j	| dt
|  tt|j	| t|j	|  d S )	Nr)   r   r-   )r"   r\   r(   gMbP?r"   gư>r\   )r   r
   r   r   dictcopyr   r   r   r   r   )	r   r&   r   r   r   Zkwargs0r8   res1res2r   r   r   test_convergence   s*    
z"TestDifferentiate.test_convergencec           	      C   s  t  }d}|j}||}t||ddd}t||ddd}t|j| t|j| k s\J t||ddd}t||ddd}t|j| t|j| k sJ tddd	d
}t||fddd|}t||fddd|}t|j|jdd tdddd
}t||fddd|}t||fdt	
d dd|}t|j|jdd d|d< t||fddd|}t||fdt	
d dd|}t|j|jdd d S )Nr)   g      ?)initial_stepmaxiterg?r.   )step_factorrr   rg   r-   r   )r(   rr   step_direction)rq   rs   r>   r\   r2   rt   )r   r
   r   r   r   r   r   rl   r   r   sqrt)	r   r&   r   r   r   rn   ro   r8   r   r   r   r   test_step_parameters   s4    
  z&TestDifferentiate.test_step_parametersc                 C   sl   dd }t ddd}t |}d\||dk < ||dk< t|||d	}t|jt | t |jshJ d S )
Nc                 S   s$   t | }t j|| dk | dk < |S )Nr   r.   )r   rN   rS   )r   yr   r   r   r      s    
z0TestDifferentiate.test_step_direction.<locals>.fr   r.   r   )r)   r2   r   gffffff?)rt   )	r   rB   Z
zeros_liker   r   r   rN   allr?   )r   r   r   rt   r   r   r   r   test_step_direction   s    
z%TestDifferentiate.test_step_directionc                 C   s   dd }dd }t g dddd}t g dddd}t d	d
gddd}t||||fd}t ||||jj}t|j| d S )Nc                 S   s   | | S r	   r   r   pr   r   r   r      s    z@TestDifferentiate.test_vectorized_step_direction_args.<locals>.fc                 S   s   || |d   S Nr)   r   r{   r   r   r   r      s    zATestDifferentiate.test_vectorized_step_direction_args.<locals>.df)r)   r.   r,   r-   r2   r)   r2   r   r)   r.   r,   )rt   r7   )r   r^   rC   r   Zbroadcast_tor   r   r   )r   r   r   r   hdirr|   r   r   r   r   r   #test_vectorized_step_direction_args   s    z5TestDifferentiate.test_vectorized_step_direction_argsc                    s(  d}t  dfdd}d}t||dd}t|jr@J t|j|d d d	  ksdJ t|jksxJ  fd
d d _	d  _
t  _t|| dd}| D ]p}|dkr|| tjksJ  j
| tjksJ || tjksJ q||  j
|   kr|| ksn J qd S )Ng~,?r,   c                    s     | }|S r	   )r   )r   r   )r&   r   r   r      s    
z2TestDifferentiate.test_maxiter_callback.<locals>.f   V瞯<)rr   r\   r)   r.   c                    sb     j d7  _ |  _t| ds"J | j jvs2J  j| j | jtjksPJ  j kr^t	d S )Nr)   r   )
iterr   hasattrr   dfsaddr@   r_   _EINPROGRESSStopIterationr   )callbackrr   r   r   r      s    
z9TestDifferentiate.test_maxiter_callback.<locals>.callbackr2   )r   r\   r@   )r   r
   r   r   anyr?   ry   rA   r3   r   r   setr   keysr_   ra   r   Z
_ECALLBACK)r   r   r   Zdefault_orderr   ro   keyr   )r   r&   rr   r   test_maxiter_callback   s(    $	z'TestDifferentiate.test_maxiter_callbackr   r~   g?gffffff?rF   c                    s   t j| dd } fdd} fdd}t||d||d}|jj ksLJ |jj ks\J |jj kslJ t  j}t	|jt 
|jt |d	 d S )
NrZ   r   c                    s   | j  ksJ t| S r	   )rF   r   rN   r0   rZ   r   r   r     s    z'TestDifferentiate.test_dtype.<locals>.fc                    s4   | j j ksJ | jj ks J | jj ks0J d S r	   )r   rF   r   r   r   rZ   r   r   r     s    z.TestDifferentiate.test_dtype.<locals>.callbackr-   )r(   rt   r   ru   )r   Zasarrayr   r   rF   r   r   Zfinfoepsr   rN   rv   )r   r   r   rF   r   r   r   r   r   rZ   r   
test_dtype  s    
zTestDifferentiate.test_dtypec                 C   sl  d}t jt|d td d W d    n1 s20    Y  d}t jt|d tdd d W d    n1 sr0    Y  d}t jt|d" td	d d
dg W d    n1 s0    Y  d}t jt|d" tdd ddd W d    n1 s0    Y  t jt|d" tdd ddd W d    n1 s<0    Y  t jt|d" tdd dd d W d    n1 s~0    Y  t jt|d$ tdd dt d W d    n1 s0    Y  d}t jt|d" tdd ddd W d    n1 s0    Y  t jt|d" tdd ddd W d    n1 sJ0    Y  d}t jt|d" tdd ddd W d    n1 s0    Y  t jt|d" td d ddd W d    n1 s0    Y  d!}t jt|d" td"d dd#d$ W d    n1 s0    Y  d%}t jt|d" td&d dd'd( W d    n1 s^0    Y  d S ))Nz`func` must be callable.)matchr)   z3Abscissae and function output must be real numbers.c                 S   s   | S r	   r   r0   r   r   r   rM   1  r=   z9TestDifferentiate.test_input_validation.<locals>.<lambda>y            ?z6When `preserve_shape=False`, the shape of the array...c                 S   s   g dS )N)r)   r.   r,   r   r0   r   r   r   rM   5  r=   z6Tolerances and step parameters must be non-negative...c                 S   s   | S r	   r   r0   r   r   r   rM   9  r=   r2   r!   c                 S   s   | S r	   r   r0   r   r   r   rM   ;  r=   Zekkiru   c                 S   s   | S r	   r   r0   r   r   r   rM   =  r=   )rq   c                 S   s   | S r	   r   r0   r   r   r   rM   ?  r=   )rs   z%`maxiter` must be a positive integer.c                 S   s   | S r	   r   r0   r   r   r   rM   C  r=         ?)rr   c                 S   s   | S r	   r   r0   r   r   r   rM   E  r=   r   z"`order` must be a positive integerc                 S   s   | S r	   r   r0   r   r   r   rM   I  r=   r/   c                 S   s   | S r	   r   r0   r   r   r   rM   K  r=   z'`preserve_shape` must be True or False.c                 S   s   | S r	   r   r0   r   r   r   rM   O  r=   Zherringri   z`callback` must be callable.c                 S   s   | S r	   r   r0   r   r   r   rM   S  r=   Z	shrubbery)r   )pytestZraises
ValueErrorr   object)r   messager   r   r   test_input_validation(  sD    (,0022422222z'TestDifferentiate.test_input_validationc                    s   dd }t |ddd}|js J t|jd tdD ] d} fd	d}d
  | d   }t ||dtd d}t|j|dd t|jtj	 t ||td d}|jsJ |j
d
ksJ t|j|dd q4dd }t |d
dd}t|jd d S )Nc                 S   s    t | jt jsJ | d d S )Nc   r)   )r   rE   rF   Zfloatingr0   r   r   r   r   Z  s    z/TestDifferentiate.test_special_cases.<locals>.f   r    ru   gl)~IQr*   r   c                    s   d|    S )Nr.   r   r0   r9   r   r   r   k  s    r.   r)   )rr   r(   r   r/   c                 S   s   ||  d S r}   r   )r   cr   r   r   r   z  s    r,   )r7   )r   r?   r   r   rangerH   r   r   r   rS   r3   )r   r   r   r   r   r   r   r   test_special_casesU  s$    
	
z$TestDifferentiate.test_special_casesc                 C   s   | d d S )Nr)   r,   r   r0   r   r   r   rM     r=   zTestDifferentiate.<lambda>c                 C   s"   t | dk| d d | d d S )Nr)      r,   )r   wherer0   r   r   r   rM     r=   c                 C   s<   d}t |g d|d}t|js(J t|jd|d d S )NgؗҜ<r~   )rt   r"   r   r!   )r   r   ry   r?   r   r   )r   r   r"   r   r   r   r   test_saddle_gh18811  s    	z%TestDifferentiate.test_saddle_gh18811N) __name__
__module____qualname__r   r   markZparametrizer   rB   r   r   Z_distr_paramsZdistcontr'   tuplerJ   rd   rf   rk   rp   rw   rz   r   r   Zfloat16Zfloat32Zfloat64r   r   r   Zxfailr   r   r   r   r   r      s8   


6"(-+

r   )r   numpyr   Znumpy.testingr   r   r   Z(scipy._lib._elementwise_iterative_methodZ_libZ_elementwise_iterative_methodr_   Zscipyr   Zscipy.optimize._differentiater   r   r   r   r   r   r   r   <module>   s   