a
    ReV*                     @   s   d dl Z d dlZd dlmZ d dlmZmZmZ d dl	m
Z
mZ d dlmZ d dlmZ d dlmZ edd	gd
dgddggZeg dZG dd dZG dd dZG dd deeZG dd deZG dd dZdS )    N)lstsq)assert_allcloseassert_equalassert_)rand
coo_matrix)aslinearoperator)
lsq_linear)BoundsgS?gv/gJ+gX9vϿgsh|?ſgS㥫?)gl?gMbX9?gPnؿ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S )	BaseMixinc                 C   s   t jd| _d S )Nr   )nprandomZRandomStaternd)self r   X/var/www/sunrise/env/lib/python3.9/site-packages/scipy/optimize/tests/test_lsq_linear.pysetup_method   s    zBaseMixin.setup_methodc                 C   sN   | j D ]B}ttt| j|d}t|jtttddd  t|j|jd  qd S )Nmethod
lsq_solverZrcondr   )	lsq_solversr	   Abr   r   xr   unbounded_sol)r   r   resr   r   r   test_dense_no_bounds   s    
zBaseMixin.test_dense_no_boundsc                 C   sh  t ddg}t ddg}tttddd }| jD ]F}ttt||f| j|d}t|j	tttddd  t|j
d | q4t dt j g}| jD ]H}ttt|t jf| j|d}t|j	t ddgd	d
 t|j
d | qt ddg}| jD ]H}ttt|t jf| j|d}t|j	t ddgdd
 t|j
d | qt t jdg}| jD ]H}tttt j |f| j|d}t|j	t ddg t|j
d | qRt dt jg}| jD ]H}tttt j |f| j|d}t|j	t ddg t|j
d | qt ddg}t ddg}| jD ]D}ttt||f| j|d}t|j	t ddg t|j
d | qd S )Nr   i   r   r   r           g1Vư>Zatolgԡw?gV瞯<ggUw-r 
su?)r   arrayr   r   r   r   r	   r   r   r   r   inf)r   lbubr   r   r   r   r   r   test_dense_bounds   sb    





zBaseMixin.test_dense_boundsc           
      C   s   t ddg}| jjdd}|| }t ddg}t ddg}||f}t||}t|||}t|||}	t |	j|	jd rJ t	|j|	j d S )Nr      )   r,   sizer,   r   )
r   r&   r   uniformr
   r	   Zallcloser   r   r   )
r   r   r   r   r(   r)   Z
bounds_oldZ
bounds_newZres_oldZres_newr   r   r   test_bounds_variantsP   s    
zBaseMixin.test_bounds_variantsc                 C   sf   t j 0}|t t g dg dg}W d    n1 s@0    Y  t ddg}t|| d S )N)   r$   r   r,   r+   )
   r   r   r   r1      )r   ZtestingZsuppress_warningsfilterPendingDeprecationWarningZmatrixr&   r	   )r   supr   kr   r   r   test_np_matrix]   s
    
4zBaseMixin.test_np_matrixc                 C   s  t ddgg}t dg}ddg}ddg}| jD ]J}t||||f| j|d}t|jddg t|jd t||dd	d  q2t d
dgddgddgg}t g d}ddg}ddg}| jD ]J}t||||f| j|d}t|j	ddd t|jd t||dd	d  qd S )NgSӿgZd;Oǿg#~j?皙g?r   r   r   r   gK7A`?gK7A`?gPngPng~jt?g~jt?)g`"gHzG?gJ+?r   g      gdy=r"   )
r   r&   r   r	   r   r   r   r   r   
optimality)r   r   r   r(   r)   r   r   r   r   r   test_dense_rank_deficiente   s0    
 
z#BaseMixin.test_dense_rank_deficientc                 C   s   t ddg}t ddg}ttt||f| jd}t|jddg t|jd t	ttddd  t
|jt }t|jdt 
||  t|j| t|jd	d
d t|jddg t|jdk  t|jdkp|jdk tt|jt t|j d S )Nr   r$   r   r   r%   r   r   g      ?r    g-q=r"   r4   r+   )r   r&   r	   r   r   r   r   r   r   r   dotcostZfunr;   r   Zactive_maskr   Znitstatus
isinstancemessagestrsuccess)r   r(   r)   r   rr   r   r   test_full_result~   s    zBaseMixin.test_full_resultc                 C   sL   t g dg dg dg}t g d}t||| jd}t|jdk  d S )N)gSQcU?gn-(8?g(8?)gڌ?g,?gk,?)g\s`?gAʨY?g Y?)gt\O?v?g"He?gFH{?r=   gg5_PG>)r   r&   r	   r   r   r?   )r   r   r   resultr   r   r   test_almost_singular   s    zBaseMixin.test_almost_singularc                 C   s(  t jd t t jjdddd\}}|d9 }dt jjdd||gd }dt jjdd|gd }dt jt jjddd|fddd }|d	d d f  d7  < t j||}|d d |f }t|||d
dj}t|||ddj}t || | d }	t || | d }
t	t
|	|
 |
d k  d S )Nr   r,     r-         ?ic   )Zaxisr   bvlsboundsr   trf绽|=)r   r   seedsortrandintchoicer	   r   sumr   abs)r   nmr   r   rN   wx_bvlsx_trf	cost_bvlscost_trfr   r   r   test_large_rank_deficient   s    $z#BaseMixin.test_large_rank_deficientc                 C   s   t g dg dg dg}t g d}t g dg dg}t|||ddj}t|||d	dj}t || | d
 }t || | d
 }tt|| |d k  d S )N)g     H@g     D@      @)g      3r_   g       )g      *g      $@g     @Q@)g     Dg     Vg     G@)g      ?@g      Fg      :@)g      K@r_   g      <@rL   rM   rO   r,   rP   )r   r&   r	   r   rU   r   rV   )r   r   r   rN   rZ   r[   r\   r]   r   r   r   test_convergence_small_matrix   s    

z'BaseMixin.test_convergence_small_matrixN)__name__
__module____qualname__r   r   r*   r0   r9   r<   rF   rH   r^   r`   r   r   r   r   r      s   1r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )SparseMixinc                 C   sb   d}d}t ||dd}| j|}t||}t|jddd t|}t||}t|jddd d S )N  rI   r   Zrandom_stater!   r"   )r   r   randnr	   r   r;   r   )r   rX   rW   r   r   r   r   r   r   test_sparse_and_LinearOperator   s    

z*SparseMixin.test_sparse_and_LinearOperatorc                 C   s   d}d}t ||dd}| j|}| j|}|d }t||||f}t|jddd t||||fd	d
d}t|jddd t||||fdd}t|jddd d S )Nre   rI   r   rf   r   r    r!   r"   gvIh%<=i  )lsmr_tollsmr_maxiterauto)ri   )r   r   rg   r	   r   r;   )r   rX   rW   r   r   r(   r)   r   r   r   r   test_sparse_bounds   s    zSparseMixin.test_sparse_boundsc                 C   s   t g d}t g d}t g d}t|||ffdd}t| tdd}t|tdd}tjtd	d
 t	|j
|j
 W d    n1 s0    Y  t|tddd}t	|j
|j
 d S )N)rJ   rJ   rJ   gzo ?rJ   )r   r   r   r,   r,   )r   r,   r   r   r,   )r+   r+   )shapeexact)r   lsmr matchr2   r   rj   )r   r&   r   r	   Ztoarrayr   pytestraisesAssertionErrorr   r   )r   datarowcolr   Z	exact_solZdefault_lsmr_solZ	conv_lsmrr   r   r   test_sparse_ill_conditioned   s    ,z'SparseMixin.test_sparse_ill_conditionedN)ra   rb   rc   rh   rl   rz   r   r   r   r   rd      s   rd   c                   @   s   e Zd ZdZddgZdS )TestTRFrO   rn   ro   Nra   rb   rc   r   r   r   r   r   r   r{      s   r{   c                   @   s   e Zd ZdZdgZdS )TestBVLSrL   rn   Nr|   r   r   r   r   r}      s   r}   c                   @   s   e Zd Zdd Zdd ZdS )TestErrorCheckingc                 C   s   t ttddd}t ttddd}t ttdd d}d}tjt|d  t ttddd}W d    n1 sh0    Y  tjt|d  t ttddd}W d    n1 s0    Y  tjt|d  t ttdd	d}W d    n1 s0    Y  d S )
Nro   g{Gz?)r   ri   rk   z3`lsmr_tol` must be None, 'auto', or positive float.rq   r:   Zfoor   r	   r   r   rt   ru   
ValueErrorr   _Zerr_messager   r   r   test_option_lsmr_tol  s    ..z&TestErrorChecking.test_option_lsmr_tolc                 C   s   t ttddd}t ttdd d}d}tjt|d  t ttddd}W d    n1 sX0    Y  tjt|d  t ttddd}W d    n1 s0    Y  d S )Nro   r   rs   z0`lsmr_maxiter` must be None or positive integer.rq   r   r   r   r   r   r   r   test_option_lsmr_maxiter  s    .z*TestErrorChecking.test_option_lsmr_maxiterN)ra   rb   rc   r   r   r   r   r   r   r~      s   r~   )rt   numpyr   Znumpy.linalgr   Znumpy.testingr   r   r   Zscipy.sparser   r   Zscipy.sparse.linalgr   Zscipy.optimizer	   Zscipy.optimize._minimizer
   r&   r   r   r   rd   r{   r}   r~   r   r   r   r   <module>   s&    02