o
    Ö­jg~  ã                   @   sÌ   d dl Z d dlmZ d dlZd dlmZmZmZ d dl	m
Z d dlmZmZmZ d dlmZmZmZmZmZm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dd„ Zdd„ ZdS )é    N)Úproduct)Úassert_allcloseÚassert_equalÚassert_)Úraises)Ú
csr_matrixÚ
csc_matrixÚ
lil_matrix)Ú_adjust_scheme_to_boundsÚapprox_derivativeÚcheck_derivativeÚgroup_columnsÚ_eps_for_methodÚ_compute_absolute_stepc                  C   s¾   g d¢g d¢g d¢g d¢g d¢g d¢g d¢g} t jtttfD ]1}|| ƒ}t  d¡}t  g d	¢¡}t||ƒ}t||ƒ g d
¢}t  g d¢¡}t||ƒ}t||ƒ qt|ƒ}t|ƒ}t||ƒ d S )N)é   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   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   r   )	ÚnpÚasarrayr   r   r	   ÚarangeÚarrayr   r   )Ú	structureÚ	transformÚAÚorderÚgroups_trueÚgroupsÚgroups_1Úgroups_2© r"   ú_/var/www/html/CropPilot/venv/lib/python3.10/site-packages/scipy/optimize/tests/test__numdiff.pyÚtest_group_columns   s*   ù	



r$   c                  C   sæ   t  t j¡j} | d | d | d dœ}dD ]}ttt jt j|ƒ|| ƒ ttt jt j|ƒ|| ƒ qt  t j¡j} | d | d | d dœ}dD ])}ttt jt j|ƒ|| ƒ ttt jt j|ƒ|| ƒ ttt jt j|ƒ|| ƒ qGd S )Nç      à?çUUUUUUÕ?©ú2-pointz3-pointÚcs)r   ÚfinfoÚfloat64Úepsr   r   Ú
complex128Úfloat32)ÚEPSÚrelative_stepÚmethodr"   r"   r#   Útest_correct_fp_eps+   sB   þþþþþþþ÷r2   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚTestAdjustSchemeToBoundsc                 C   s  t  d¡}t  dd¡}t  |¡}t  |¡}| t j ¡ | t j¡ t||dd||ƒ\}}t||ƒ tt  	|¡ƒ t||dd||ƒ\}}t||ƒ tt  	|¡ƒ t||dd||ƒ\}}t||ƒ tt  	| ¡ƒ t||dd||ƒ\}}t||ƒ tt  	| ¡ƒ d S )Nr   ç{®Gáz„?r   ú1-sidedr   ú2-sided)
r   ÚzerosÚfullÚ
empty_likeÚfillÚinfr
   r   r   Úall)ÚselfÚx0ÚhÚ	inf_lowerÚ	inf_upperÚ
h_adjustedÚ	one_sidedr"   r"   r#   Útest_no_boundsP   s4   


ÿ
ÿ
ÿ
ÿ
z'TestAdjustSchemeToBounds.test_no_boundsc                 C   s   t  g d¢¡}t  d¡ }t  d¡}t  g d¢¡d }t||dd||ƒ\}}t||ƒ t||dd||ƒ\}}t|t  g d¢¡d ƒ t||dd	||ƒ\}}t|t  |¡ƒ tt  | ¡ƒ t||dd	||ƒ\}}t|t  g d¢¡d ƒ t|t  g d
¢¡ƒ d S )N)ç        g333333ë?g333333ë¿r   )r   r   éÿÿÿÿçš™™™™™¹?r   r5   r   )r   rF   r   r6   )FTT)	r   r   Úonesr
   r   Úabsr   r<   r   )r=   r>   ÚlbÚubr?   rB   Ú_rC   r"   r"   r#   Útest_with_boundl   s$   

ÿÿz(TestAdjustSchemeToBounds.test_with_boundc                 C   s  t  ddg¡}t  ddg¡}t  ddg¡}t  ddg¡}t||dd||ƒ\}}t|t  ddg¡ƒ t||d	d||ƒ\}}t|t  d
dg¡ƒ t||dd||ƒ\}}t|t  ddg¡ƒ t|t  ddg¡ƒ t||d	d||ƒ\}}t|t  ddg¡ƒ t|t  ddg¡ƒ d S )Ng¸…ëQ¸ž¿gš™™™™™©?rE   g¸…ëQ¸ž?çš™™™™™¹¿r   r5   g¸…ëQ¸®¿r   gš™™™™™™?r6   FTg¸…ëQ¸Ž?g¸…ëQ¸Ž¿)r   r   r
   r   r   )r=   rJ   rK   r>   r?   rB   rL   rC   r"   r"   r#   Útest_tight_bounds‚   s$   ÿÿz*TestAdjustSchemeToBounds.test_tight_boundsN)Ú__name__Ú
__module__Ú__qualname__rD   rM   rO   r"   r"   r"   r#   r3   O   s    r3   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dJdd„ZdKdd„Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Zd;d<„ Zd=d>„ Z d?d@„ Z!dAdB„ Z"dCdD„ Z#dEdF„ Z$dGdH„ Z%dIS )LÚTestApproxDerivativesDensec                 C   ó
   t  |¡S ©N©r   Úsinh©r=   Úxr"   r"   r#   Úfun_scalar_scalarš   ó   
z,TestApproxDerivativesDense.fun_scalar_scalarc                 C   rT   rU   ©r   ÚcoshrX   r"   r"   r#   Újac_scalar_scalar�   r[   z,TestApproxDerivativesDense.jac_scalar_scalarc                 C   ó,   t  |d d t  |d ¡t  |d ¡g¡S ©Nr   r   ©r   r   ÚtanÚexprX   r"   r"   r#   Úfun_scalar_vector    ó   ,z,TestApproxDerivativesDense.fun_scalar_vectorc                 C   ó8   t  d|d  t  |d ¡d t  |d ¡g¡ dd¡S ©Nr   r   éþÿÿÿrF   r   ©r   r   Úcosrc   ÚreshaperX   r"   r"   r#   Újac_scalar_vector£   ó
   (ÿÿz,TestApproxDerivativesDense.jac_scalar_vectorc                 C   ó$   t  |d |d  ¡t  |d ¡ S ©Nr   r   ©r   ÚsinÚlogrX   r"   r"   r#   Úfun_vector_scalar§   ó   $z,TestApproxDerivativesDense.fun_vector_scalarc                 C   s    t  |d t  |¡t  |¡g¡S ©Nr   ra   rX   r"   r"   r#   Úwrong_dimensions_funª   s    z/TestApproxDerivativesDense.wrong_dimensions_func              	   C   ó|   t  |d t  |d |d  ¡ t  |d ¡ t  |d |d  ¡|d   |d t  |d |d  ¡ t  |d ¡ g¡S ©Nr   r   ©r   r   rj   rr   rq   rX   r"   r"   r#   Újac_vector_scalar­   ó   *ÿ*ýz,TestApproxDerivativesDense.jac_vector_scalarc                 C   óH   t  |d t  |d ¡ |d t  |d ¡ |d d |d d  g¡S ©Nr   r   r   ç      à¿©r   r   rq   rj   rX   r"   r"   r#   Úfun_vector_vector´   ó
   ýz,TestApproxDerivativesDense.fun_vector_vectorc                 C   sT   |dksJ ‚t  |d t  |d ¡ |d t  |d ¡ |d d |d d  g¡S )ú=Used to test passing custom arguments with check_derivative()é*   r   r   r   r~   r   ©r=   rY   Úargr"   r"   r#   Úfun_vector_vector_with_arg»   s   ýz5TestApproxDerivativesDense.fun_vector_vector_with_argc                 C   ó†   t  t  |d ¡|d t  |d ¡ g|d  t  |d ¡ t  |d ¡gd|d d  |d d  d|d d  |d d  gg¡S ©Nr   r   r   r   r~   ç      ø¿r   rX   r"   r"   r#   Újac_vector_vectorÄ   ó
   "$6ýz,TestApproxDerivativesDense.jac_vector_vectorc                 C   s’   |dksJ ‚t  t  |d ¡|d t  |d ¡ g|d  t  |d ¡ t  |d ¡gd|d d  |d d  d|d d  |d d  gg¡S )r‚   rƒ   r   r   r   r   r~   r‰   r   r„   r"   r"   r#   Újac_vector_vector_with_argË   s   "$6ýz5TestApproxDerivativesDense.jac_vector_vector_with_argç      ð?c                 C   s*   t  t  ||d  ¡t  ||d  ¡g¡S ro   ©r   r   rc   ©r=   rY   Úc0Úc1r"   r"   r#   Úfun_parametrizedÔ   s   *z+TestApproxDerivativesDense.fun_parametrizedrG   c              
   C   s:   t  |t  ||d  ¡ dgd|t  ||d  ¡ gg¡S ro   rŽ   r�   r"   r"   r#   Újac_parametrized×   s   þz+TestApproxDerivativesDense.jac_parametrizedc                 C   s   t  |¡dkr	|S t jS )Nç:Œ0âŽyE>©r   rI   ÚnanrX   r"   r"   r#   Úfun_with_nanÝ   ó   z'TestApproxDerivativesDense.fun_with_nanc                 C   s   t  |¡dkr	dS t jS )Nr”   r�   r•   rX   r"   r"   r#   Újac_with_nanà   r˜   z'TestApproxDerivativesDense.jac_with_nanc                 C   s,   t  |d |d  t  |d |d  ¡g¡S ro   )r   r   rj   rX   r"   r"   r#   Úfun_zero_jacobianã   re   z,TestApproxDerivativesDense.fun_zero_jacobianc              
   C   sV   t  |d |d g|d  t  |d |d  ¡ |d  t  |d |d  ¡ gg¡S rx   )r   r   rq   rX   r"   r"   r#   Újac_zero_jacobianæ   s   >þz,TestApproxDerivativesDense.jac_zero_jacobianc                 C   s   t  |¡ ¡ }t |¡S rU   )r   r   ÚitemÚmathrc   )r=   rY   Úxpr"   r"   r#   Újac_non_numpyì   s   
z(TestApproxDerivativesDense.jac_non_numpyc                 C   sh   d}t | j|dd�}t | j|ƒ}t | j|dd�}|  |¡}t||dd� t||dd� t||dd� d S )	Nr�   r(   ©r1   r)   ç�íµ ÷Æ°>©Úrtolç•Ö&è.>çê-�™—q=©r   rZ   r^   r   ©r=   r>   Ú
jac_diff_2Ú
jac_diff_3Ú
jac_diff_4Újac_truer"   r"   r#   Útest_scalar_scalarò   s   ÿÿ
z-TestApproxDerivativesDense.test_scalar_scalarc                 C   sp   d}t | j|ddd�}t | j|dd�}t | j|ddd�}|  |¡}t||dd� t||d	d� t||d
d� d S )Nr�   r(   çÛ"•“\ÿO>©r1   Úabs_step)r¯   r)   r¡   r¢   r¤   r¥   r¦   r§   r"   r"   r#   Útest_scalar_scalar_abs_stepþ   s   ÿÿÿ
z6TestApproxDerivativesDense.test_scalar_scalar_abs_stepc                 C   sn   d}t | j|dd�}t | j|ƒ}t | j|dd�}|  t |¡¡}t||dd� t||dd� t||dd� d S )	Nr%   r(   r    r)   r¡   r¢   r¤   r¥   )r   rd   rl   r   Ú
atleast_1dr   r§   r"   r"   r#   Útest_scalar_vector  s   ÿÿz-TestApproxDerivativesDense.test_scalar_vectorc                 C   ór   t  ddg¡}t| j|dd�}t| j|ƒ}t| j|dd�}|  |¡}t||dd� t||dd� t||d	d� d S )
Nç      Y@r~   r(   r    r)   r¡   r¢   çH¯¼šò×z>r¥   )r   r   r   rs   rz   r   r§   r"   r"   r#   Útest_vector_scalar  ó   ÿÿ
z-TestApproxDerivativesDense.test_vector_scalarc                 C   s~   t  ddg¡}t| j|ddd�}t| j|dt jd�}t| j|ddd�}|  |¡}t||dd	� t||d
d	� t||dd	� d S )Nr´   r~   r(   r­   r®   )r¯   Úrel_stepr)   r¡   r¢   çßA:ÜÅ)>r¥   )r   r   r   rs   r;   rz   r   r§   r"   r"   r#   Útest_vector_scalar_abs_step$  s   ÿÿÿ
z6TestApproxDerivativesDense.test_vector_scalar_abs_stepc                 C   r³   )
Nç      YÀçš™™™™™É?r(   r    r)   çñhãˆµøä>r¢   r¡   r¥   ©r   r   r   r€   rŠ   r   r§   r"   r"   r#   Útest_vector_vector2  r·   z-TestApproxDerivativesDense.test_vector_vectorc                 C   s<   d}t tt| j|ƒ |  t |¡¡}t tt| j||d� d S )Nr�   )Úf0)Úassert_raisesÚRuntimeErrorr   rv   r   r±   Ú
ValueError)r=   r>   rÀ   r"   r"   r#   Útest_wrong_dimensions>  s   ÿ
ÿz0TestApproxDerivativesDense.test_wrong_dimensionsc                 C   sZ   t  ddg¡}t| j|ddd�}t| j|dd�}|  |¡}t||dd� t||dd� d S )	NrN   rG   r(   g-Cëâ6?)r1   r¸   )r¸   r4   r¢   r¾   )r=   r>   r¨   r©   r«   r"   r"   r#   Útest_custom_rel_stepF  s   ÿÿ
z/TestApproxDerivativesDense.test_custom_rel_stepc              
   C   s´   t  ddg¡}d}d}d}d}| j|||d�}t  ddg¡}|  |||¡}t| j|d|||ft|d�||fd	�}	t| j||||ft|d�||fd
�}
t|	|dd� t|
|dd� d S )Nr�   ç      ð¿rE   ç       @)r‘   g�íµ ÷Æ°¾rµ   r(   )r1   r¸   rÀ   ÚargsÚkwargsÚbounds)r¸   rÀ   rÈ   rÉ   rÊ   r¡   r¢   r¤   )r   r   r’   r“   r   Údictr   )r=   r>   r�   r‘   rJ   rK   rÀ   r¸   r«   r¨   r©   r"   r"   r#   Útest_optionsP  s$   
þþz'TestApproxDerivativesDense.test_optionsc                 C   s|   t  d¡ }t  d¡}t  ddg¡}ttt| j|||fd� t  ddg¡}t| j|d||fd�}|  |¡}t||d	d
� d S )Nr   g       Àr¼   ©rÊ   rÆ   r�   r(   ©r1   rÊ   r¡   r¢   )	r   rH   r   rÁ   rÃ   r   r€   rŠ   r   )r=   rJ   rK   r>   Újac_diffr«   r"   r"   r#   Útest_with_bounds_2_pointb  s   
ÿÿ
z3TestApproxDerivativesDense.test_with_bounds_2_pointc                 C   s¾   t  ddg¡}t  ddg¡}t  ddg¡}|  |¡}t| j|ƒ}t||dd� t| j||t jfd�}t||dd� t| j|t j |fd�}t||dd� t| j|||fd�}t||dd� d S )Nr�   rÇ   r¤   r¢   rÍ   )r   r   rŠ   r   r€   r   r;   )r=   rJ   rK   r>   r«   rÏ   r"   r"   r#   Útest_with_bounds_3_pointp  s$   
ÿ
ÿÿz3TestApproxDerivativesDense.test_with_bounds_3_pointc                 C   s¼   t  ddg¡}|d }|d }|  |¡}t| j|d||fd�}t||dd� t| j|dd||fd�}t||dd� t| j|||fd	�}t||dd� t| j|d||fd
�}t||dd� d S )Ng      $@r¹   g•Ö&è.!>r(   rÎ   r¡   r¢   ©r1   r¸   rÊ   rÍ   ©r¸   rÊ   )r   r   rŠ   r   r€   r   )r=   r>   rJ   rK   r«   rÏ   r"   r"   r#   rO   †  s*   
ÿþÿÿz,TestApproxDerivativesDense.test_tight_boundsc                 C   s¼   d}d}d}|   |¡}t| j|dd||fd�}t| j|d||fd�}t||dd� t||d	d� d}|   |¡}t| j|dd||fd�}t| j|d||fd�}t||dd� t||d	d� d S )
Nç:Œ0âŽyE¾r”   rE   r(   r¡   rÒ   rÓ   r¢   r¤   )r™   r   r—   r   )r=   rJ   rK   r>   r«   r¨   r©   r"   r"   r#   Útest_bound_switchesš  s0   

þÿ

þÿz.TestApproxDerivativesDense.test_bound_switchesc                 C   sh   d}|   |¡}t| j |dd�}t| j |ƒ}t||dd� t||dd� ttt| j |fi tdd�¤Ž d S )Nr�   r(   r    r¡   r¢   r”   r)   )rŸ   r   r   rÁ   Ú	TypeErrorrË   )r=   r>   r«   r¨   r©   r"   r"   r#   Útest_non_numpy±  s   
ÿ
ÿz)TestApproxDerivativesDense.test_non_numpyc                    s€  t j d¡ dd„ ‰‡fdd„‰ t jdddt jd�‰t j d¡ t j¡‰t  d	d	g¡}tˆ |d
ˆˆfd�}tˆ | t j¡d
ˆˆfd�}ˆ |ˆˆƒj	t jksOJ ‚t
||dd� ‡ ‡‡fdd„}t|| t j¡d
d�}t
||dd� dd„ }dd„ }dd„ }dd„ }t jt jt jfD ]5}	t  |	¡j}
t  d¡ |	¡}td |||ƒd
ƒ}||||||
ƒ}t||d
|d�||ƒ ‰ tˆ ƒ|k s½J ‚qˆd S )Nr   c                 S   s   | d | d |  S ro   r"   )ÚprY   r"   r"   r#   ÚfuncÃ  s   z0TestApproxDerivativesDense.test_fp.<locals>.funcc                    s   ˆ | |ƒ| S rU   r"   )rØ   rY   Úy)rÙ   r"   r#   ÚerrÆ  ó   z/TestApproxDerivativesDense.test_fp.<locals>.errr   éd   ©ÚdtyperÆ   r(   )r1   rÈ   gü©ñÒMbP?)Úatolc                    s$   | j tjksJ ‚ˆ | ˆˆƒ tj¡S rU   )rß   r   r.   Úastype)rØ   )rÛ   rY   rÚ   r"   r#   Úerr_fp32Ö  s   z4TestApproxDerivativesDense.test_fp.<locals>.err_fp32r    c                 S   ó
   t  | ¡S rU   ©r   rq   ©rY   r"   r"   r#   Úfß  r[   z-TestApproxDerivativesDense.test_fp.<locals>.fc                 S   rã   rU   )r   rj   rå   r"   r"   r#   Úgá  r[   z-TestApproxDerivativesDense.test_fp.<locals>.gc                 S   s   t  | ¡ S rU   rä   rå   r"   r"   r#   Úhessã  s   z0TestApproxDerivativesDense.test_fp.<locals>.hessc                 S   s\   | d t t ||ƒ¡t |||  ƒ¡ƒ }||  t t ||ƒ¡t |||  ƒ¡ƒ }|| S ru   )Úmaxr   rI   )r?   r>   ræ   rè   r/   Út0Út1r"   r"   r#   Ú	calc_atolæ  s   **z5TestApproxDerivativesDense.test_fp.<locals>.calc_atolr�   r®   )r   ÚrandomÚseedÚlinspacer+   rá   r   r   r.   rß   r   Úfloat16r*   r,   r   rI   )r=   Úp0Újac_fp64Újac_fprâ   ræ   rç   rè   rì   rß   r/   r>   r?   rà   r"   )rÛ   rÙ   rY   rÚ   r#   Útest_fp¾  sB   ÿÿ	ÿÿùz"TestApproxDerivativesDense.test_fpc                 C   s�   t  ddg¡}t| j| j|ƒ}t|dk ƒ t| j| j|ƒ}t|dk ƒ t  ddg¡}t| j| j|ƒ}t|dkƒ t| j| j|ƒ}t|dkƒ d S )Nç      $Àé
   r¤   r¡   rE   r   )r   r   r   r€   rŠ   r   rš   r›   ©r=   r>   Úaccuracyr"   r"   r#   Útest_check_derivativeø  s$   ÿÿÿÿz0TestApproxDerivativesDense.test_check_derivativec                 C   s6   t  ddg¡}t| j| j|ddid�}t|dk ƒ d S )Nrõ   rö   r…   rƒ   )rÉ   r¤   )r   r   r   r†   rŒ   r   r÷   r"   r"   r#   Ú!test_check_derivative_with_kwargs	  s   ýz<TestApproxDerivativesDense.test_check_derivative_with_kwargsN)r�   )rG   )&rP   rQ   rR   rZ   r^   rd   rl   rs   rv   rz   r€   r†   rŠ   rŒ   r’   r“   r—   r™   rš   r›   rŸ   r¬   r°   r²   r¶   rº   r¿   rÄ   rÅ   rÌ   rÐ   rÑ   rO   rÕ   r×   rô   rù   rú   r"   r"   r"   r#   rS   ™   sH    	
	

:rS   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 )ÚTestApproxDerivativeSparsec                 C   sž   t j d¡ d| _ddt  | j¡  | _ddt  | j¡  | _t  | j¡| _d| jd d d…  | jd d d…< d| jdd d…  | jdd d…< |  	| j¡| _
d S )Nr   é2   rN   r   rG   gËPÊÿÿï?r   )r   rí   rî   Únr   rJ   rK   Úemptyr>   ÚjacÚJ_true©r=   r"   r"   r#   Úsetup_method  s     z'TestApproxDerivativeSparse.setup_methodc                 C   sD   |dd … d |d d… d  }t  dd| f¡t  d| df¡ S )Nr   r   rF   r   r   )r   Úhstack)r=   rY   Úer"   r"   r#   Úfun  s    $zTestApproxDerivativeSparse.func                 C   sÔ   |j }t ||f¡}d|d  |d< d|d d  |d< td|d ƒD ]2}d||d   |||d f< d	|| d  d
||   |||f< d||d  d  |||d f< q#d	|d d  |d< d|d  |d< |S )Néüÿÿÿr   ©r   r   r   r   r   ©r   r   iúÿÿÿé	   r   rF   ©rF   rF   rh   ©rF   rh   )Úsizer   r7   Úrange)r=   rY   rý   ÚJÚir"   r"   r#   rÿ   #  s   $"zTestApproxDerivativeSparse.jacc                 C   sb   t j||ftd�}d|d< d|d< td|d ƒD ]}d|||d |d …f< qd|d< d|d< |S )NrÞ   r   r  r  r   r
  r  )r   r7   Úintr  )r=   rý   r   r  r"   r"   r#   r   1  s   z$TestApproxDerivativeSparse.structurec                 C   s  |   | j¡}t | j¡}t||ƒ}tj |¡ t||ƒ}tg d¢||gtj | j	gtj| j
gƒD ]O\}}}}t| j| j|||f||fd�}	tt|	tƒƒ t|	 ¡ | jdd� t | jd¡}
|
d d d…  d9  < t| j| j||
||fd�}	t|	 ¡ | jd	d� q0d S )
Nr'   )r1   rÊ   Úsparsityr¡   r¢   r”   r   rF   )r1   r¸   r  r½   )r   rý   r   r   r   rí   Úshuffler   r;   rJ   rK   r   r  r>   r   Ú
isinstancer   r   Útoarrayr   Ú	full_like)r=   r   r   r    r!   r1   r   ÚlÚur  r¸   r"   r"   r#   Útest_all<  s*   

þÿÿôz#TestApproxDerivativeSparse.test_allc                 C   s6   |   | j¡}t| j| j|d�}t| ¡ | jdd� d S )N)r  r¡   r¢   )r   rý   r   r  r>   r   r  r   )r=   r   r  r"   r"   r#   Útest_no_precomputed_groupsQ  s   z5TestApproxDerivativeSparse.test_no_precomputed_groupsc                 C   sn   t j| j| jftd�}t  | j¡}dD ]!}t| j| j|d�}t| j| j||f|d�}t|| 	¡ ddd� qd S )NrÞ   r'   r    )r  r1   gVçž¯Â<g¦ÖVs†ÿ<)r£   rà   )
r   rH   rý   r  r   r   r  r>   r   r  )r=   r   r   r1   ÚJ_denseÚJ_sparser"   r"   r#   Útest_equivalenceV  s   ÿ
ÿüz+TestApproxDerivativeSparse.test_equivalencec                    s`   ‡ fdd„}t ˆ j|ˆ jˆ jˆ jfd�}t|dk ƒ t ˆ j|ˆ jˆ jˆ jfd�}t|dk ƒ d S )Nc                    s   t ˆ  | ¡ƒS rU   )r   rÿ   rå   r  r"   r#   rÿ   a  rÜ   z=TestApproxDerivativeSparse.test_check_derivative.<locals>.jacrÍ   r¤   )r   r  r>   rJ   rK   r   )r=   rÿ   rø   r"   r  r#   rù   `  s   
ÿ
ÿz0TestApproxDerivativeSparse.test_check_derivativeN)rP   rQ   rR   r  r  rÿ   r   r  r  r  rù   r"   r"   r"   r#   rû     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 )Ú"TestApproxDerivativeLinearOperatorc                 C   rT   rU   rV   rX   r"   r"   r#   rZ   o  r[   z4TestApproxDerivativeLinearOperator.fun_scalar_scalarc                 C   rT   rU   r\   rX   r"   r"   r#   r^   r  r[   z4TestApproxDerivativeLinearOperator.jac_scalar_scalarc                 C   r_   r`   ra   rX   r"   r"   r#   rd   u  re   z4TestApproxDerivativeLinearOperator.fun_scalar_vectorc                 C   rf   rg   ri   rX   r"   r"   r#   rl   x  rm   z4TestApproxDerivativeLinearOperator.jac_scalar_vectorc                 C   rn   ro   rp   rX   r"   r"   r#   rs   |  rt   z4TestApproxDerivativeLinearOperator.fun_vector_scalarc              	   C   rw   rx   ry   rX   r"   r"   r#   rz     r{   z4TestApproxDerivativeLinearOperator.jac_vector_scalarc                 C   r|   r}   r   rX   r"   r"   r#   r€   †  r�   z4TestApproxDerivativeLinearOperator.fun_vector_vectorc                 C   r‡   rˆ   r   rX   r"   r"   r#   rŠ   �  r‹   z4TestApproxDerivativeLinearOperator.jac_vector_vectorc                 C   sº   d}t | j|ddd�}t | j|dd�}t | j|ddd�}|  |¡}tj d¡ tdƒD ]/}tjjd	dd
d�}t| 	|¡|| dd� t| 	|¡|| dd� t| 	|¡|| dd� q+d S )Nr�   r(   T©r1   Úas_linear_operator©r  r)   r   rö   éöÿÿÿ©r   ©r  r½   r¢   çñhãˆµøÔ>)
r   rZ   r^   r   rí   rî   r  Úuniformr   Údot©r=   r>   r¨   r©   rª   r«   r  rØ   r"   r"   r#   r¬   ”  s4   þÿþ
ÿÿÿúz5TestApproxDerivativeLinearOperator.test_scalar_scalarc                 C   sÆ   d}t | j|ddd�}t | j|dd�}t | j|ddd�}|  t |¡¡}tj d¡ tdƒD ]2}tjjd	dd
d�}t	| 
|¡| 
|¡dd� t	| 
|¡| 
|¡dd� t	| 
|¡| 
|¡dd� q.d S )Nr%   r(   Tr  r   r)   r   rö   r!  r"  r#  r½   r¢   r$  )r   rd   rl   r   r±   rí   rî   r  r%  r   r&  r'  r"   r"   r#   r²   ©  s4   þÿþÿÿÿúz5TestApproxDerivativeLinearOperator.test_scalar_vectorc                 C   sÞ   t  ddg¡}t| j|ddd�}t| j|dd�}t| j|ddd�}|  |¡}t j d¡ td	ƒD ]<}t jjd
d	|j	d�}t
| |¡t  | |¡¡dd� t
| |¡t  | |¡¡dd� t
| |¡t  | |¡¡dd� q0d S )Nr´   r~   r(   Tr  r   r)   r   rö   r!  r#  r½   r¢   r$  rµ   )r   r   r   rs   rz   rí   rî   r  r%  Úshaper   r&  r±   r'  r"   r"   r#   r¶   ¾  s4   þÿþ
ÿÿÿúz5TestApproxDerivativeLinearOperator.test_vector_scalarc                 C   sÌ   t  ddg¡}t| j|ddd�}t| j|dd�}t| j|ddd�}|  |¡}t j d¡ td	ƒD ]3}t jjd
d	|j	d�}t
| |¡| |¡dd� t
| |¡| |¡dd� t
| |¡| |¡dd� q0d S )Nr»   r¼   r(   Tr  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¿   Ó  s(   þÿþ
üz5TestApproxDerivativeLinearOperator.test_vector_vectorc                 C   s.   t  ddg¡}ttt| j|ddt jfd� d S )Nr»   r¼   r(   r   rÎ   )r   r   rÁ   rÃ   r   r€   r;   )r=   r>   r"   r"   r#   Útest_exceptionå  s
   

þz1TestApproxDerivativeLinearOperator.test_exceptionN)rP   rQ   rR   rZ   r^   rd   rl   rs   rz   r€   rŠ   r¬   r²   r¶   r¿   r)  r"   r"   r"   r#   r  m  s    r  c                  C   s  dd„ } t | ddgddd�}t|ddgƒ t | ddgdd	d�}t|ddgƒ t | ddgdddgd�}t|ddgƒ t | ddgddd	gd�}t|ddgƒ t | ddgdd	dgd�}t|ddgƒ t | ddgddtj dfd
�}t|ddgƒ t | ddgdd	dtjfd
�}t|ddgƒ d S )Nc                 S   s&   t  | d d ¡ t  | d d ¡ S ro   )r   rI   rå   r"   r"   r#   ræ   õ  s   &z"test_absolute_step_sign.<locals>.frF   r(   r”   r®   rÆ   r�   rÔ   )r1   r¯   rÊ   )r   r   r   r;   )ræ   Úgradr"   r"   r#   Útest_absolute_step_signì  s4   	ÿÿÿ
þÿr+  c            
      C   sj  g d¢} t  g d¢¡}t  t j¡j}|d |d |d dœ}t  d¡}| D ]A}|| }t  ||d |d |t  |d ¡ g¡}td |||ƒ}t||ƒ | dk t	¡d d	 }	td | ||ƒ}t||	| ƒ q%t  g d
¢¡}t  |d |d  |d |d d |d t  |d ¡ g¡}t|||dƒ}t||ƒ | dk t	¡d d	 }	t|| |dƒ}t||	| ƒ d S )Nr'   )r½   r   r   g     jø@r%   r&   r�   r   r   r   r   )rG   r   rö   rÝ   r(   )
r   r   r*   r+   r,   rI   r   r   rá   Úfloat)
Úmethodsr>   r/   r0   rÀ   r1   r¸   Úcorrect_stepr¯   Úsign_x0r"   r"   r#   Útest__compute_absolute_step!  s>   ý
ý

ý
r0  )r�   Ú	itertoolsr   Únumpyr   Únumpy.testingr   r   r   Úpytestr   rÁ   Úscipy.sparser   r   r	   Úscipy.optimize._numdiffr
   r   r   r   r   r   r$   r2   r3   rS   rû   r  r+  r0  r"   r"   r"   r#   Ú<module>   s"     $J  {[5