o
    Ö­jå„  ã                   @   sÔ  d dl mZ d dlZd dlmZ d dlmZmZm	Z	m
Z
 d dl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 d d	lmZ d d
lmZmZmZ dBdd„ZdCd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%dCd d!„Z&d"d#„ Z'G d$d%„ d%ƒZ(G d&d'„ d'ƒZ)d(d)„ Z*e+e ,¡ ƒe*g Z-G d*d+„ d+ƒZ.G d,d-„ d-ƒZ/G d.d/„ d/ƒZ0G d0d1„ d1ƒZ1G d2d3„ d3e.e/e0e1ƒZ2G d4d5„ d5e.e/e0e1ƒZ3G d6d7„ d7e.ƒZ4d8d9„ Z5d:d;„ Z6d<d=„ Z7d>d?„ Z8d@dA„ Z9dS )Dé    )ÚproductN)Únorm)Úassert_Úassert_allcloseÚassert_equalÚsuppress_warnings)Úraises)ÚissparseÚ
lil_matrix)Úaslinearoperator)Úleast_squaresÚBounds)ÚIMPLEMENTED_LOSSES)ÚEPSÚmake_strictly_feasibleÚCL_scaling_vectorc                 C   s   | | d d S )Né   ç      @© ©ÚxÚar   r   úd/var/www/html/CropPilot/venv/lib/python3.10/site-packages/scipy/optimize/tests/test_least_squares.pyÚfun_trivial   s   r   ç        c                 C   s   d| |  S ©Nr   r   r   r   r   r   Újac_trivial   ó   r   c                 C   s   t  | d | d g¡S ©Nr   é   ©ÚnpÚarray©r   r   r   r   Úfun_2d_trivial   s   r$   c                 C   s
   t  d¡S r   )r!   Úidentityr#   r   r   r   Újac_2d_trivial   ó   
r&   c                 C   s*   t  d| d | d d   d| d  g¡S )Né
   r   r   r   r    r#   r   r   r   Úfun_rosenbrock!   s   *r)   c                 C   s   t  d| d  dgddgg¡S )Néìÿÿÿr   r(   éÿÿÿÿr    r#   r   r   r   Újac_rosenbrock%   s   þr,   c                 C   s$   t  d| d  dgddgddgg¡S )Nr*   r   r(   r+   r   r    r#   r   r   r   Újac_rosenbrock_bad_dim,   s
   ýr-   c                 C   ó   t | ƒd S ©Nr   )r)   r#   r   r   r   Úfun_rosenbrock_cropped4   r   r0   c                 C   r.   r/   )r,   r#   r   r   r   Újac_rosenbrock_cropped8   r   r1   c                 C   s   t  | | d | d g¡S )Nr   é   r    r#   r   r   r   Úfun_wrong_dimensions=   s   r3   c                 C   s   t  t| |d�¡S )N)r   )r!   Ú
atleast_3dr   r   r   r   r   Újac_wrong_dimensionsA   s   r5   c                 C   s´   t t | jd ¡ƒ}t |d |d f¡}|  ||f¡} | |dd…dd…f< |d d…dd…f |dd …dd…f  |dd…d d…f  |dd…dd …f  d|   | d  }| ¡ S )Nr   r   r   r+   éþÿÿÿé   r2   )Úintr!   ÚsqrtÚshapeÚzerosÚreshapeÚravel)r   ÚnÚuÚyr   r   r   Úfun_bvpE   s   `rA   c                   @   s&   e Zd Zd
dd„Zdd„ Zdd„ Zd	S )ÚBroydenTridiagonaléd   Úsparsec                    s^  t j d¡}|ˆ _t  |¡ ˆ _t  dd|¡ˆ _t  dd|¡ˆ _ˆ  jd| 	|¡ 7  _ˆ  jd| 	|¡ 7  _ˆ  jd| 	|¡ 7  _t
ˆ jˆ jˆ jƒˆ _|dkrŒt||ftd�ˆ _t  |¡}d	ˆ j||f< t  d	|¡}d	ˆ j||d	 f< t  |d	 ¡}d	ˆ j||d	 f< ˆ jˆ _d S |d
kr™‡ fdd„ˆ _d S |dkr©d ˆ _‡ fdd„ˆ _d S tdƒ d S )Nr   r6   ç      ø¿gš™™™™™é¿r   çš™™™™™¹?rD   )Údtyper   Úoperatorc                    s   t ˆ  | ¡ƒS ©N)r   Ú_jacr#   ©Úselfr   r   Ú<lambda>i   ó    z-BroydenTridiagonal.__init__.<locals>.<lambda>Údensec                    s   ˆ   | ¡ ¡ S rI   )rJ   Útoarrayr#   rK   r   r   rM   l   rN   F)r!   ÚrandomÚRandomStater>   ÚonesÚx0ÚlinspaceÚlbÚubÚrandnr   r
   r8   ÚsparsityÚarangerJ   Újacr   )rL   r>   ÚmodeÚrngÚir   rK   r   Ú__init__O   s0   
zBroydenTridiagonal.__init__c                 C   sP   d| | d }|dd …  |d d… 8  < |d d…  d|dd …  8  < |S )Nr2   r   r+   r   r   )rL   r   Úfr   r   r   Úfunp   s    zBroydenTridiagonal.func                 C   sr   t | j| jfƒ}t | j¡}dd|  |||f< t d| j¡}d|||d f< t | jd ¡}d|||d f< |S )Nr2   r   r   r+   r6   )r
   r>   r!   rZ   )rL   r   ÚJr^   r   r   r   rJ   v   s   zBroydenTridiagonal._jacN)rC   rD   )Ú__name__Ú
__module__Ú__qualname__r_   ra   rJ   r   r   r   r   rB   N   s    
!rB   c                   @   s.   e Zd ZdZ		ddd„Zdd	„ Zd
d„ ZdS )ÚExponentialFittingProblemz]Provide data and function for exponential fitting in the form
    y = a + exp(b * x) + noise.r   ©r+   r   é   Nc           
      C   s°   t j |¡}|| _d| _t  d¡| _t  |d |d |¡| _|t  	|| j ¡ | _
|  j
|| | j¡ 7  _
| d| j|¡}	| j
|	  d| | |¡ 7  < t  ||g¡| _d S )Nr   r   r   é2   )r!   rQ   rR   Úmr>   r;   Úp0rU   r   Úexpr@   rX   ÚrandintÚrandr"   Úp_opt)
rL   r   ÚbÚnoiseÚ
n_outliersÚx_rangeÚn_pointsÚrandom_seedr]   Úoutliersr   r   r   r_   …   s    z"ExponentialFittingProblem.__init__c                 C   s"   |d t  |d | j ¡ | j S r   )r!   rl   r   r@   ©rL   Úpr   r   r   ra   –   s   "zExponentialFittingProblem.func                 C   sL   t  | j| jf¡}d|d d …df< | jt  |d | j ¡ |d d …df< |S )Nr   r   )r!   Úemptyrj   r>   r   rl   )rL   rx   rb   r   r   r   r[   ™   s   &zExponentialFittingProblem.jac)r   rg   rh   N)rc   rd   re   Ú__doc__r_   ra   r[   r   r   r   r   rf   �   s    
ÿrf   c                 C   sL   t  d| jf¡}d|  }d|d d  |d< |d |d< d|d  |d< |S )Nr2   r   gUUUUUUÕ?r   gUUUUUUå¿g«ªªªªªú¿r   )r!   ry   Úsize)ÚzÚrhoÚtr   r   r   Úcubic_soft_l1    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'd(„ Zd)S )*Ú	BaseMixinc                 C   s6   t td| jd�}t|jddd� t|jt|jƒƒ d S )Nç       @©Úmethodr   ç-Cëâ6?©Úatol)r   r   rƒ   r   r   ra   ©rL   Úresr   r   r   Ú
test_basic¯   s   zBaseMixin.test_basicc              	   C   sÊ   d}dddt fD ]Z}tƒ �%}| td¡ ttd||f| jd�}ttd|d|i| jd	�}W d   ƒ n1 s5w   Y  t|j|d
d� t|j|d
d� t	t
ttdd| jd� t	t
ttdddi| jd	� qd S )Nç      @ú2-pointú3-pointÚcsúBjac='(3-point|cs)' works equivalently to '2-point' for method='lm'r�   )Úargsrƒ   r   )Úkwargsrƒ   r„   )Úrtol)r2   r7   Úkaboomr2   )r   r   ÚfilterÚUserWarningr   r   rƒ   r   r   Úassert_raisesÚ	TypeError)rL   r   r[   Úsuprˆ   Úres1r   r   r   Útest_args_kwargsµ   s.   þÿÿù

ÿ

ÿðzBaseMixin.test_args_kwargsc              	   C   s€   dddt fD ],}tƒ �}| td¡ ttd|| jd�}W d   ƒ n1 s%w   Y  t|jddd	� qt	t
ttdd
| jd� d S )Nr‹   rŒ   r�   rŽ   r�   r‚   r   r„   r…   Úoops©r[   rƒ   )r   r   r“   r”   r   r   rƒ   r   r   r•   Ú
ValueError)rL   r[   r—   rˆ   r   r   r   Útest_jac_optionsË   s   þû
ÿzBaseMixin.test_jac_optionsc                 C   s0   dD ]}t td|| jd�}t|jddd� qd S )N)Né   r�   )Úmax_nfevrƒ   r   r„   r…   )r   r   rƒ   r   r   )rL   rŸ   rˆ   r   r   r   Útest_nfev_optionsØ   s   ÿýzBaseMixin.test_nfev_optionsc                 C   sŽ   dt  dg¡dfD ]}ttd|d�}t|jdƒ q	ttttdd| jd� ttttdd	| jd� ttttdd | jd� ttttdd
| jd� d S )Nç      ð?ç      à?r[   r�   )Úx_scaler   Úauto)r£   rƒ   ç      ð¿y      ð?       @)	r!   r"   r   r   r   r   r•   rœ   rƒ   )rL   r£   rˆ   r   r   r   Útest_x_scale_optionsÞ   s   ÿÿÿ
ÿzBaseMixin.test_x_scale_optionsc                 C   s†   t tdd| jd�}t tdd| jd�}t tdd | jd�}t|jddd� t|jddd� t|jddd� t|j|jƒ t|j|jƒ d S )Nr�   rF   )Ú	diff_steprƒ   gš™™™™™¹¿r   r„   r…   )r   r   rƒ   r   r   r   Únfev)rL   r˜   Úres2Úres3r   r   r   Útest_diff_stepë   s   ÿÿÿzBaseMixin.test_diff_stepc                 C   s8   t tttd| jddid� t tttd| jddid� d S )Nr�   Úno_such_optionrC   )rƒ   ÚoptionsrŸ   )r•   r–   r   r   rƒ   rK   r   r   r   Útest_incorrect_options_usageú   s   

ÿ


ÿz&BaseMixin.test_incorrect_options_usagec                 C   sÔ   t td| jd�}t|jddd� t|jdƒ t|jdƒ t|jddd� t|jddd� t|j	ddd� t
|jdƒ | jd	krNt|jd
k ƒ t|jd u ƒ nt|jdk ƒ t|jdk ƒ t|jdkƒ t|jƒ d S )Nr�   r‚   r   r„   r…   g      )@é   ç{®Gáz„?Úlmé   r(   )r   r   rƒ   r   r   Úcostra   r[   ÚgradÚ
optimalityr   Úactive_maskr   r¨   ÚnjevÚstatusÚsuccessr‡   r   r   r   Útest_full_result   s   
zBaseMixin.test_full_resultc                 C   sÒ   | j dkrd S ttd| j dd�}t|jt dg¡ƒ t|jdƒ t|jt dg¡ƒ t|j	t dgg¡ƒ t|j
t d	g¡ƒ t|jd	ƒ t|jt d
g¡ƒ t|jdƒ t|jdƒ t|jd
ƒ t|jd
ƒ d S )Nr±   r�   r   )rƒ   rŸ   r   g     @D@é	   r7   é$   r   )rƒ   r   r   r   r   r!   r"   r³   ra   r[   r´   rµ   r¶   r¨   r·   r¸   r¹   r‡   r   r   r   Útest_full_result_single_fev  s    

ÿz%BaseMixin.test_full_result_single_fevc              
   C   sœ   ddg}ddg}t dddtgdt ddg¡dgd	d
gƒD ]/\}}}tƒ �}| td¡ tt||||| j	d�}W d   ƒ n1 s@w   Y  t
|j|ƒ qd S )Nr6   r   r‹   rŒ   r�   r¡   çš™™™™™É?r[   ÚexactÚlsmrrŽ   ©r£   Ú	tr_solverrƒ   )r   r,   r!   r"   r   r“   r”   r   r)   rƒ   r   r   )rL   rT   Úx_optr[   r£   rÂ   r—   rˆ   r   r   r   Útest_rosenbrock(  s$   
ýþ
ÿûõzBaseMixin.test_rosenbrockc              	   C   s†   ddg}| j dkrtttt|dd� d S tdddtgdt dd	g¡d
gddgƒD ]\}}}tt||||| j d�}t	|j
ddd� q(d S )Nr6   r   r±   r‚   r‹   rŒ   r�   r¡   r¾   r[   r¿   rÀ   rÁ   r   g›+¡†›„=r…   )rƒ   r•   rœ   r   r0   r   r1   r!   r"   r   r³   )rL   rT   r[   r£   rÂ   rˆ   r   r   r   Útest_rosenbrock_cropped8  s    

ÿ
ýþùz!BaseMixin.test_rosenbrock_croppedc                 C   s   t tttd| jd� d S ©Nr�   r‚   )r•   rœ   r   r3   rƒ   rK   r   r   r   Útest_fun_wrong_dimensionsG  ó   
ÿz#BaseMixin.test_fun_wrong_dimensionsc                 C   s   t tttdt| jd� d S rÆ   )r•   rœ   r   r   r5   rƒ   rK   r   r   r   Útest_jac_wrong_dimensionsK  s   
ÿz#BaseMixin.test_jac_wrong_dimensionsc                 C   s"   ddg}t ttt|t| jd� d S )Nr   r   r‚   )r•   rœ   r   r)   r-   rƒ   ©rL   rT   r   r   r   Ú(test_fun_and_jac_inconsistent_dimensionsO  s   

ÿz2BaseMixin.test_fun_and_jac_inconsistent_dimensionsc                 C   s*   t  d¡ dd¡}tttt|| jd� d S )Nr7   r   r‚   )r!   rS   r<   r•   rœ   r   r   rƒ   rÊ   r   r   r   Útest_x0_multidimensionalT  s   

ÿz"BaseMixin.test_x0_multidimensionalc                 C   s   d}t ttt|| jd� d S )Nù       @        r‚   ©r•   rœ   r   r   rƒ   rÊ   r   r   r   Útest_x0_complex_scalarY  s   

ÿz BaseMixin.test_x0_complex_scalarc                 C   s    ddg}t ttt|| jd� d S )Nr¡   rÍ   r‚   rÎ   rÊ   r   r   r   Útest_x0_complex_array^  s   

ÿzBaseMixin.test_x0_complex_arrayc                 C   sZ   d}t  |d ¡}| jdkrd}nd}tt|d| j|d�}t|j|k ƒ t|jdk ƒ d S )	Nr(   r   r±   iˆ  rC   r°   )Úftolrƒ   rŸ   r¢   )r!   rS   rƒ   r   rA   r   r¨   r³   )rL   r>   rT   rŸ   rˆ   r   r   r   Útest_bvpc  s   
ÿzBaseMixin.test_bvpc              
   C   s   t tttd| jd d d d� d S )Nr�   )rƒ   rÑ   ÚxtolÚgtolrÎ   rK   r   r   r   Ú/test_error_raised_when_all_tolerances_below_epst  s   


ÿz9BaseMixin.test_error_raised_when_all_tolerances_below_epsc              
   C   sV   | j dkrd S ddg}ddg}dD ]\}}}tt|t|||| j d�}t|j|ƒ qd S )Nr±   r6   r   ))ç:Œ0âŽyE>NN)NrÖ   N)NNrÖ   )r[   rÑ   rÔ   rÓ   rƒ   )rƒ   r   r)   r,   r   r   )rL   rT   rÃ   rÑ   rÓ   rÔ   rˆ   r   r   r   Ú0test_convergence_with_only_one_tolerance_enabledy  s   
þúz:BaseMixin.test_convergence_with_only_one_tolerance_enabledN)rc   rd   re   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                   @   sX   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 d¡dd„ ƒZdS )ÚBoundsMixinc                 C   ó   t tttdd| jd� d S )Nr�   )ç      $@r   ©Úboundsrƒ   rÎ   rK   r   r   r   Útest_inconsistentˆ  ó   

ÿzBoundsMixin.test_inconsistentc                 C   rÙ   )Nr�   )rŠ   r7   rÛ   rÎ   rK   r   r   r   Útest_infeasibleŒ  rÞ   zBoundsMixin.test_infeasiblec                 C   rÙ   )Nr�   )r¡   r   r2   rÛ   rÎ   rK   r   r   r   Útest_wrong_number�  rÞ   zBoundsMixin.test_wrong_numberc                 C   sF   t tttddddgf| jd� t tttddgdgddgf| jd� d S )Nr�   r¡   rŠ   rÛ   r   ç      @)r•   rœ   r   r   rƒ   r)   rK   r   r   r   Útest_inconsistent_shape”  s   
ÿ
ÿz#BoundsMixin.test_inconsistent_shapec                 C   s¶   dddt fD ]R}ttd|d| jd�}t|jddd	� t|jd
gƒ td|j  ko,dkn  ƒ ttd|d| jd�}t|jddd	� t|jdgƒ td|j  koTdkn  ƒ qd S )Nr‹   rŒ   r�   r�   )r¥   rŠ   ©r[   rÜ   rƒ   r   r„   r…   r   r+   r2   )r¢   rŠ   r¢   )	r   r   r   rƒ   r   r   r   r¶   r   )rL   r[   rˆ   r   r   r   Útest_in_bounds›  s   ÿÿ özBoundsMixin.test_in_boundsc              
   C   sì   dd„ }dd„ }dddt fD ]e}||fD ]^}ddg}tt||d	�}t|jd
d
gƒ tt|||dddgƒ| jd�}t|jddgƒ tt|||ddgdƒ| jd�}t|jddgƒ tt|||ddgddgƒ| jd�}t|jd
dgdd� qqd S )Nc                 S   s   | |fS rI   r   ©rV   rW   r   r   r   Úget_bounds_direct©  s   z8BoundsMixin.test_bounds_shape.<locals>.get_bounds_directc                 S   s
   t | |ƒS rI   )r   rå   r   r   r   Úget_bounds_instances¬  r'   z;BoundsMixin.test_bounds_shape.<locals>.get_bounds_instancesr‹   rŒ   r�   r¡   )r[   r   r¢   r�   rã   g333333Ó?r¾   rŠ   r+   çñhãˆµøä>r…   )r&   r   r$   r   r   rƒ   )rL   ræ   rç   r[   Úbounds_funcrT   rˆ   r   r   r   Útest_bounds_shape¨  s2   þþýðÿzBoundsMixin.test_bounds_shapec                 C   sô   t tdtƒ d�}t|jddd� t tdtdd�d�}t|jddd� t tdtd	dd
�d�}t|jddd� t tdtd	d�d�}t|jd	dd� t tddgtd	d	gdd
�d�}t|jddgdd� t tddgtddgd�d�}t|jddgdd� d S )Nr¢   )rÜ   r   r„   r…   rŠ   r¡   )rV   r¥   rå   ç      À)rW   rè   rF   )r   r   r   r   r   r$   r‡   r   r   r   Útest_bounds_instancesÂ  s    
ÿ
ÿz!BoundsMixin.test_bounds_instancesr(   c                 C   s*  t  ddg¡}t  ddg¡}t  ddg¡}t  ddg¡}t  ddg¡}|t j dgt jff|t j dgt jff|t j dgt jff|t j dgdt jgff|ddgddgff|d	dgd
dgffg}|D ]/\}}tdddtgddd
gdgddgƒD ]\}	}
}tt||	||
|| jd�}t|j	ddd� qxqcd S )Ng       Àr¡   r�   r   g333333ó¿rE   g      ø?rŠ   g      IÀr¢   rC   r‹   rŒ   r�   r[   r¿   rÀ   rÁ   rè   r…   )
r!   r"   Úinfr   r,   r   r)   rƒ   r   rµ   )rL   Úx0_1Úx0_2Úx0_3Úx0_4Úx0_5ÚproblemsrT   rÜ   r[   r£   rÂ   rˆ   r   r   r   Útest_rosenbrock_bounds×  s2   ú
ý
þùÿz"BoundsMixin.test_rosenbrock_boundsN)rc   rd   re   rÝ   rß   rà   râ   rä   rê   rì   ÚpytestÚmarkÚ	fail_slowrô   r   r   r   r   rØ   ‡  s    
rØ   c                   @   sh   e Z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„ ƒZdd„ Zdd„ Zdd„ ZdS )ÚSparseMixinc              	   C   sF   t ƒ }ttt|j|j|jd| jd� ttt|j|jd|j| jd� d S )Nr¿   ©rÂ   rƒ   )rÂ   Újac_sparsityrƒ   )	rB   r•   rœ   r   ra   rT   r[   rƒ   rY   rw   r   r   r   Útest_exact_tr_solverò  s   ÿ
þz SparseMixin.test_exact_tr_solverc                 C   sˆ   t dd�}t dd�}t|j|j|j| jd�}t|j|j|j| jd�}t|j|jƒ t|j	|j	dd� t|j
ddd� t|j
ddd� d S )NrD   ©r\   rO   r›   ç#B’¡œÇ;r…   r   )rB   r   ra   rT   r[   rƒ   r   r¨   r   r   r³   ©rL   rD   rO   Ú
res_sparseÚ	res_denser   r   r   Útest_equivalenceú  s   

þþzSparseMixin.test_equivalencec                 C   s8   t ƒ }t|j|j|j| jddid�}t|jddd� d S )NÚbtolç»½×Ùß|Û=©rƒ   Ú
tr_optionsr   rý   r…   )rB   r   ra   rT   r[   rƒ   r   r³   ©rL   rx   rˆ   r   r   r   Útest_tr_options  s
   ÿzSparseMixin.test_tr_optionsc              	   C   sH   t ƒ }ttt|j|j|jd| jd� ttt|j|j|jdddid� d S )NÚbestrù   rÀ   Útolr  )rÂ   r  )	rB   r•   rœ   r   ra   rT   r[   rƒ   r–   rw   r   r   r   Útest_wrong_parameters  s   ÿ
ÿz!SparseMixin.test_wrong_parametersc                 C   sˆ   t dd�}t dd�}t|j|j|j| jd�}t|j|j|j| jd�}t|jddd� t|jddd� tt	|jƒƒ tt
|jtjƒƒ d S )NrD   rü   rO   r›   r   rý   r…   )rB   r   ra   rT   r[   rƒ   r   r³   r   r	   Ú
isinstancer!   Úndarrayrþ   r   r   r   Útest_solver_selection  s   

ÿÿz!SparseMixin.test_solver_selectionc                 C   s„   t ƒ }dD ]:}t|j|j|| jd�}t|j|j|| j|jd�}t|j|jƒ t|j	|j	dd� t|j
ddd� t|j
ddd� qd S )N)r‹   rŒ   r�   r‚   )rƒ   rú   rý   r…   r   )rB   r   ra   rT   rƒ   rY   r   r¨   r   r   r³   )rL   rx   r[   r   rÿ   r   r   r   Útest_numerical_jac!  s   þøzSparseMixin.test_numerical_jacr(   c              	   C   sÆ   t ƒ }t|jdddgd |jgƒD ]P\}}t|j|j||jtj	f| j
|d�}t|j|j|tj	 |jf| j
|d�}t|j|j||j|jf| j
|d�}t|jddd� t|jddd� t|jddd� qd S )Nr‹   rŒ   r�   )rÜ   rƒ   rú   r   r  r…   )rB   r   r[   rY   r   ra   rT   rV   r!   rí   rƒ   rW   r   rµ   )rL   rx   r[   rú   Úres_1Úres_2Úres_3r   r   r   Útest_with_bounds-  s(   ÿþþþózSparseMixin.test_with_boundsc                 C   s2   t ƒ }|jd d… }ttt|j|j|| jd� d S )Nr+   ©rú   rƒ   )rB   rY   r•   rœ   r   ra   rT   rƒ   )rL   rx   rY   r   r   r   Útest_wrong_jac_sparsity?  s
   
ÿz#SparseMixin.test_wrong_jac_sparsityc              	   C   sT   t dd�}t|j|j|j| jd�}t|jddd� tt	t|j|j|j| jdd� d S )	NrH   rü   r‚   r   rý   r…   r¿   )rƒ   rÂ   ©
rB   r   ra   rT   r[   rƒ   r   r³   r•   rœ   r  r   r   r   Útest_linear_operatorE  s   

ÿz SparseMixin.test_linear_operatorc              	   C   s\   t ƒ }t|j|j|j| jdd�}t|jddd� t dd�}tt	t|j|j|j| jdd� d S )Nr[   )rƒ   r£   r   rý   r…   rH   rü   r  r  r   r   r   Útest_x_scale_jac_scaleL  s   ÿ

ÿz"SparseMixin.test_x_scale_jac_scaleN)rc   rd   re   rû   r  r  r
  r  r  rõ   rö   r÷   r  r  r  r  r   r   r   r   rø   ñ  s    

rø   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚLossFunctionMixinc                 C   sF   t D ]}ttd|| jd�}t|jddd� qttttdd| jd� d S )Nr�   ©Úlossrƒ   r   çVçž¯Ò<r…   Úhinge)ÚLOSSESr   r   rƒ   r   r   r•   rœ   ©rL   r  rˆ   r   r   r   Útest_optionsX  s   ÿ

ÿzLossFunctionMixin.test_optionsc                 C   s2   t D ]}ttd|| jd�}t|jt|jƒƒ qd S )Nr�   r  )r  r   r   rƒ   r   ra   r   r  r   r   r   Útest_funa  s   ÿýzLossFunctionMixin.test_func                 C   sˆ  t  dg¡}tt|tdd| jd�}t|jd| |d d  ƒ tt|tdd| jd�}t|jd| ƒ tt|tdd| jd�}t|jd| |d d  d|d d d  d	  ƒ tt|td
d| jd�}t|jd| |d d  d|d d d   ƒ tt|tdd| jd�}t|jd| |d d  d|d d d   ƒ tt|tt	d| jd�}t|jd| |d d  d|d d d  d  ƒ d S )Nr�   Úlinearr   ©r  rŸ   rƒ   r   r¯   ÚhuberÚsoft_l1r¢   ÚcauchyÚarctanr7   gUUUUUUå?)
r!   r"   r   r   r   rƒ   r   r´   r   r   )rL   r   rˆ   r   r   r   Ú	test_gradi  s:   
ÿ
ÿ
ÿ*ÿ
ÿ0
ÿ0
ÿ*ÿzLossFunctionMixin.test_gradc              	   C   s4  d}|d d }t t|tdd| jd�}t|jd| ƒ t t|tdd| jd�}t|jd| td  ƒ t t|tdd	dd
�}t|jd| ƒ t t|tdd| jd�}t|jd| d|d  d  ƒ t t|tdd| jd�}t|jd| td  ƒ t t|tdd	d| jd�}|d	 }t|jd| d|d  d  d|d   ƒ t t|tdd| jd�}t|jd| td  ƒ t t|tddd| jd�}|d }t|jd| dd|d   d  d|d   ƒ t t|ttdd�}t|jd| td  ƒ t t|ttddd
�}|d }t|jd| d|d d  d  d|d  d  ƒ d S )Nr�   r   r¯   r!  r   r"  r#  r¢   r(   )r  Úf_scalerŸ   r$  g      è¿r%  )r  r(  rŸ   rƒ   r&  g      4@rž   r2   r7   )r  rŸ   é   g«ªªªªªê¿)	r   r   r   rƒ   r   r[   r   r   r   )rL   r   r`   rˆ   Úfsr   r   r   Útest_jacˆ  s^   	
ÿ
ÿ
ÿ
ÿ 
ÿ
ÿ,
ÿ
ÿ0
ÿÿ*ÿzLossFunctionMixin.test_jacc                 C   sº   dD ]X}t dd|dd�}ddd|jfD ]F}t|j|j|| jd	�}t|jdd
d� tD ].}|dkr1q*t|j|j|||| jd�}t|jdd
d� t	t
|j|j ƒt
|j|j ƒk ƒ q*qqd S )N)rF   r¡   r   rF   r   )ru   r‹   rŒ   r�   r›   r°   r…   r!  )r[   r  r(  rƒ   )rf   r[   r   ra   rk   rƒ   r   rµ   r  r   r   r   ro   )rL   rq   rx   r[   Úres_lsqr  Ú
res_robustr   r   r   Útest_robustnessÌ  s*   ÿþÿùüýz!LossFunctionMixin.test_robustnessN)rc   rd   re   r  r   r'  r+  r.  r   r   r   r   r  W  s    	Dr  c                   @   s   e Zd ZdZdS )Ú
TestDogboxÚdogboxN)rc   rd   re   rƒ   r   r   r   r   r/  ß  s    r/  c                   @   s   e Zd ZdZdd„ ZdS )ÚTestTRFÚtrfc                 C   s@   t ƒ }dD ]}t|j|j|jdd|id�}t|jddd� qd S )N)TFr2  Ú
regularizer  r   rý   r…   )rB   r   ra   rT   r[   r   r³   )rL   rx   r3  rˆ   r   r   r   Útest_lsmr_regularizationæ  s   ÿýz TestTRF.test_lsmr_regularizationN)rc   rd   re   rƒ   r4  r   r   r   r   r1  ã  s    r1  c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚTestLMr±   c                 C   s   t tttdddd� d S )Nr�   )rë   rŠ   r±   rÛ   ©r•   rœ   r   r   rK   r   r   r   Útest_bounds_not_supportedñ  rÈ   z TestLM.test_bounds_not_supportedc                 C   s   ddg}t ttt|dd� d S )Nr6   r   r±   r‚   )r•   rœ   r   r0   rÊ   r   r   r   Útest_m_less_n_not_supportedõ  s   

ÿz"TestLM.test_m_less_n_not_supportedc                 C   s$   t ƒ }ttt|j|j|jdd� d S )Nr±   r‚   ©rB   r•   rœ   r   ra   rT   r[   rw   r   r   r   Útest_sparse_not_supportedú  s   
ÿz TestLM.test_sparse_not_supportedc                 C   s   t tttddgdd� d S )Nr�   r   r±   r  r6  rK   r   r   r   Útest_jac_sparsity_not_supportedÿ  rÞ   z&TestLM.test_jac_sparsity_not_supportedc                 C   s(   t dd�}ttt|j|j|jdd� d S )NrH   rü   r±   r‚   r9  rw   r   r   r   Ú!test_LinearOperator_not_supported  s   

ÿz(TestLM.test_LinearOperator_not_supportedc                 C   s8   t tdddd�}t|jddd� ttt tdddd	� d S )
Nr�   r!  r±   r  r   r„   r…   r#  )rƒ   r  )r   r   r   r   r•   rœ   r‡   r   r   r   Ú	test_loss  s
   

ÿzTestLM.test_lossN)
rc   rd   re   rƒ   r7  r8  r:  r;  r<  r=  r   r   r   r   r5  î  s    r5  c                  C   s   t tdƒ} t| jddd� d S )Nr�   r   r  r…   )r   r   r   r   )rˆ   r   r   r   r‰     s   
r‰   c                  C   s,   dD ]\} }}t tttd|| |dd� qd S )N))Nç‚vIhÂ%<=r>  )r>  Nr>  )r>  r>  Nr�   r±   )rÓ   rÑ   rÔ   rƒ   r6  )rÑ   rÓ   rÔ   r   r   r   Útest_small_tolerances_for_lm  s
   ÿýr?  c                     s†   t j d¡} t  ddd¡ d¡}|  d¡ d¡}dd„ ‰ ‡ fdd„}t|d	d	g||fd
�}|jdks4J ‚t|jt  	ddg¡dd� d S )Nr   r   rC   Úfloat32c                 S   s   | d | d |  S r   r   )rx   r   r   r   r   Úfunc&  s   ztest_fp32_gh12991.<locals>.funcc                    s   ˆ | |ƒ| S rI   r   )rx   r   r@   ©rA  r   r   Úerr)  s   ztest_fp32_gh12991.<locals>.errr¥   )r�   r   g ¼ùW Ú?gá	ÞáÃ?g-Cëâ6
?r…   )
r!   rQ   rR   rU   Úastyper   r¨   r   r   r"   )r]   r   r@   rC  rˆ   r   rB  r   Útest_fp32_gh12991  s   rE  c                     s„   d‰ d} ‡ fdd„}d}t || ddtjfd�}t|j|jt d¡t tj¡ƒ\}}|jdks0J ‚tjj	|j| tjd	�|k s@J ‚d S )
Ngê-�™—q=gy�²§õYs=c                    s   | ˆ  d S r   r   r#   ©Úanswerr   r   Úchi2;  r   z%test_gh_18793_and_19351.<locals>.chi2r  r   )rT   rÔ   rÜ   r   )Úord)
r   r!   rí   r   r   r´   Ú
atleast_1dr¸   Úlinalgr   )Úinitial_guessrH  rÔ   rˆ   ÚscalingÚ_r   rF  r   Útest_gh_18793_and_193517  s   
ÿ$rO  c                     sn   t  dgd g d¢ ¡‰t  dˆj¡d ‰ ‡ ‡fdd„} g d¢}d	t jd
dff}t| |d|d�}|js5J ‚d S )Nr   éB   )Sr¡   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á   rQ  ç      "@ç      @r�   rT  rR  r�   ç      *@rS  rR  ç      &@rÚ   rU  g      ,@ç      3@rV  g      .@g      2@g      :@rW  ç      @@g      =@g      <@ç      B@rX  g     €A@rY  g     €E@ç      J@rX  g      M@g      L@rZ  g     ÀP@g     €J@g      R@g      V@g     @S@g     ÀW@g     €W@g      U@g     €U@g     @Y@g     ÀZ@g      [@g     €]@g      X@g     À\@g     @a@g      a@r   rF   c                    s$   | \}}}|t  |ˆ |  ¡ ˆ S rI   )r!   rl   )ÚparamsÚAÚBrT   ©ÚxdataÚydatar   r   Úexponential_wrappedc  s   
z*test_gh_19103.<locals>.exponential_wrapped)r°   r¡   r   )r°   r   r   r(   gfffffæ4@r2  )rƒ   rÜ   )r!   r"   rZ   r{   rí   r   r¹   )ra  rT   rÜ   rˆ   r   r^  r   Útest_gh_19103Q  s   
rb  )r   )r   ):Ú	itertoolsr   Únumpyr!   Únumpy.linalgr   Únumpy.testingr   r   r   r   rõ   r   r•   Úscipy.sparser	   r
   Úscipy.sparse.linalgr   Úscipy.optimizer   r   Ú!scipy.optimize._lsq.least_squaresr   Úscipy.optimize._lsq.commonr   r   r   r   r   r$   r&   r)   r,   r-   r0   r1   r3   r5   rA   rB   rf   r   ÚlistÚkeysr  r€   rØ   rø   r  r/  r1  r5  r‰   r?  rE  rO  rb  r   r   r   r   Ú<module>   sR    


	3 Zjf 	"