o
    gf(                     @   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 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_lineargS?gv/gJ+gX9vϿgsh|?ſgS㥫?)gl?gMbX9?gPnؿ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S )	BaseMixinc                 C   s   t jd| _d S )Nr   )nprandomRandomStaternd)self r   b/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/scipy/optimize/tests/test_lsq_linear.pysetup_method   s   zBaseMixin.setup_methodc                 C   sN   | j D ]!}ttt| j|d}t|jtttddd  t|j|jd  qd S )Nmethod
lsq_solver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   sd  t ddg}t ddg}tttddd }| jD ]#}ttt||f| j|d}t|j	tttddd  t|j
d | qt dt j g}| jD ]$}ttt|t jf| j|d}t|j	t ddgd	d
 t|j
d | qJt ddg}| jD ]$}ttt|t jf| j|d}t|j	t ddgdd
 t|j
d | qyt t jdg}| jD ]#}tttt j |f| j|d}t|j	t ddg t|j
d | qt dt jg}| jD ]#}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 ]"}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ư>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   sd   





zBaseMixin.test_dense_boundsc                 C   sf   t j }|t t g dg dg}W d    n1 s w   Y  t ddg}t||}d S )N)   r&   r         )
   r    r   r   r-      )r   testingsuppress_warningsfilterPendingDeprecationWarningmatrixr(   r	   )r   supr   ks_tr   r   r   test_np_matrixO   s   
zBaseMixin.test_np_matrixc                 C   s  t ddgg}t dg}ddg}ddg}| jD ]%}t||||f| j|d}t|jddg t|jd t||dd	d  qt d
dgddgddgg}t g d}ddg}ddg}| jD ]%}t||||f| j|d}t|j	ddd t|jd t||dd	d  q_d 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_deficientW   s2   
 
 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pk|jdk tt|jt t|j d S )Nr   r&   r    r   r'   r   r   g      ?r!   g-q=r#   r2   r/   )r   r(   r	   r   r   r   r   r   r   r   dotcostfunr=   r   active_maskr   nitstatus
isinstancemessagestrsuccess)r   r*   r+   r   rr   r   r   test_full_resultp   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   rA   )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.     )size      ?ic   )axisr    bvlsboundsr   trf绽|=)r   r   seedsortrandintchoicer	   r   sumr   abs)r   nmr   r   rU   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      3rf   g       )g      *g      $@g     @Q@)g     Dg     Vg     G@)g      ?@g      Fg      :@)g      K@rf   g      <@rS   rT   rV   r.   rW   )r   r(   r	   r   r\   r   r]   )r   r   r   rU   ra   rb   rc   rd   r   r   r   test_convergence_small_matrix   s   

z'BaseMixin.test_convergence_small_matrixN)__name__
__module____qualname__r   r   r,   r;   r>   rK   rM   re   rg   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  rN   r   random_stater"   r#   )r   r   randnr	   r   r=   r   )r   r_   r^   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 )Nrl   rN   r   rm   r    r!   r"   r#   gvIh%<=i  )lsmr_tollsmr_maxiterauto)rq   )r   r   ro   r	   r   r=   )r   r_   r^   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 sHw   Y  t|tddd}t	|j
|j
 d S )N)rP   rP   rP   gzo ?rP   )r   r   r    r.   r.   )r   r.   r    r   r.   )r/   r/   )shapeexact)r   lsmr matchr0   r   rr   )r   r(   r   r	   toarrayr   pytestraisesAssertionErrorr   r   )r   datarowcolr   	exact_soldefault_lsmr_sol	conv_lsmrr   r   r   test_sparse_ill_conditioned   s   z'SparseMixin.test_sparse_ill_conditionedN)rh   ri   rj   rp   rt   r   r   r   r   r   rk      s    rk   c                   @   s   e Zd ZdZddgZdS )TestTRFrV   rv   rw   Nrh   ri   rj   r   r   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZdgZdS )TestBVLSrS   rv   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 s4w   Y  tjt|d t ttddd}W d    n1 sSw   Y  tjt|d t ttdd	d}W d    d S 1 ssw   Y  d S )
Nrw   g{Gz?)r   rq   rs   z3`lsmr_tol` must be None, 'auto', or positive float.ry   r<   foor    r	   r   r   r}   r~   
ValueErrorr   _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 s,w   Y  tjt|d t ttddd}W d    d S 1 sLw   Y  d S )Nrw   r    r{   z0`lsmr_maxiter` must be None or positive integer.ry   r   r   r   r   r   r   r   test_option_lsmr_maxiter  s   "z*TestErrorChecking.test_option_lsmr_maxiterN)rh   ri   rj   r   r   r   r   r   r   r      s    r   )r}   numpyr   numpy.linalgr   numpy.testingr   r   r   scipy.sparser   r   scipy.sparse.linalgr   scipy.optimizer	   r(   r   r   r
   rk   r   r   r   r   r   r   r   <module>   s&     #2