o
    5ήcGl                     @   s   d dl Zd dlmZ d dlmZmZmZmZm	Z	m
Z
 d dlmZ d dlmZmZmZ d dlZG dd deZG dd	 d	eZG d
d deZG dd deZG dd deZG dd deZdS )    N)
csc_matrix)eqp_kktfactprojected_cgbox_intersectionssphere_intersectionsbox_sphere_intersectionsmodified_dogleg)projections)TestCaseassert_array_almost_equalassert_equalc                   @   s   e Zd Zdd ZdS )TestEQPDirectFactorizationc                 C   s~   t g dg dg dg}t g dg dg}tg d}tddg }t||||\}}t|g d	 t|dd
g d S )N         r      r   r   r      r   r   r   r   r   r   ir      r   r   r   )r   nparrayr   r   )selfHAcbxlagrange_multipliers r'   b/tmp/pip-target-vg8gfxp4/lib/python/scipy/optimize/_trustregion_constr/tests/test_qp_subproblem.pytest_nocedal_example   s   z/TestEQPDirectFactorization.test_nocedal_exampleN)__name__
__module____qualname__r)   r'   r'   r'   r(   r      s    r   c                   @      e Zd Zdd Zdd ZdS )$TestSphericalBoundariesIntersectionsc                 C   s   t ddgddgd\}}}t||gddg t|d t ddgddgd\}}}t|d t ddgddgd\}}}t|d t ddgddgd\}}}t||gddg t|d t ddgddgd\}}}t||gddg t|d d S )	Nr   r         ?Tr   Fr         ?r   r   r   r    tatb	intersectr'   r'   r(   test_2d_sphere_constraints#   s.   








z?TestSphericalBoundariesIntersections.test_2d_sphere_constraintsc                 C   s  t ddgddgddd\}}}t||gddg t|d t ddgddgddd\}}}t|d t ddgddgddd\}}}t||gd	d
g t|d t ddgd
dgddd\}}}t||gddg t|d t ddgddgddd\}}}t||gddg t|d d S )Nr   r   r/   Tentire_line      r   Fr   r   r0   g      @r1   r2   r'   r'   r(   -test_2d_sphere_constraints_line_intersections@   s:   



zRTestSphericalBoundariesIntersections.test_2d_sphere_constraints_line_intersectionsN)r*   r+   r,   r6   r;   r'   r'   r'   r(   r.   !   s    r.   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestBoxBoundariesIntersectionsc                 C   sf  t ddgddgddgddg\}}}t||gddg t|d t ddgddgddgddg\}}}t|d	 t ddgddgtj dgtjtjg\}}}t||gddg t|d t ddgddgddgddg\}}}t||gddg t|d t ddgd
d
gddgddg\}}}t||gddg t|d t ddgddgddgddg\}}}t|d	 t ddgddgddgddg\}}}t|d	 t ddgddgdtj gdtjg\}}}t|d	 t ddgddgddgddg\}}}t|d	 t ddgddgddgddg\}}}t|d	 t ddgddgddgddg\}}}t||gddg t|d d S )Nr   r   r   r   r/   Tr   r   Fr   r   d   Gz?r   r   r   r   infr2   r'   r'   r(   test_2d_box_constraintsf   sf   























z6TestBoxBoundariesIntersections.test_2d_box_constraintsc                 C   s  t ddgddgddgddgdd\}}}t||gddg t|d t ddgddgdd	gdd
gdd\}}}t||gddg t|d t ddgddgtj dgtjtjgdd\}}}t||gdtjg t|d t ddgddgddgddgdd\}}}t||gddg t|d t ddgddgdd	gddgdd\}}}t||gddg t|d t ddgddgd	d	gd
d
gdd\}}}t|d t ddgddgd	dgd
dgdd\}}}t|d t ddgddgd	tj gd
tjgdd\}}}t|d t ddgddgddgddgdd\}}}t|d t ddgddgddgddgdd\}}}t|d t ddgddgddgddgdd\}}}t||gddg t|d d S )Nr   r   r   r   Tr7   r/   r0   r   r   g      r9   r   r   Fr=   r>   r:   r?   r2   r'   r'   r(   #test_2d_box_constraints_entire_line   s~   












zBTestBoxBoundariesIntersections.test_2d_box_constraints_entire_linec                 C   s   t g dg dg dg d\}}}t||gddg t|d t g dg dg dg d\}}}t|d t g d	g d
g dg d\}}}t||gddg t|d d S )Nr   r   r   r   r   r   r   r   r   r   r   r   r   Tr   r   r   Fr   r   r   r   r   r   r   r   r   r   r2   r'   r'   r(   test_3d_box_constraints   s   




z6TestBoxBoundariesIntersections.test_3d_box_constraintsc                 C   s   t g dg dg dg ddd\}}}t||gddg t|d t g dg d	g dg ddd\}}}t||gd
dg t|d t g dg dg dg ddd\}}}t||gddg t|d d S )NrC   rD   rE   rF   Tr7   r   r   rG   r   r   rH   rI   rJ   r2   r'   r'   r(   #test_3d_box_constraints_entire_line   s$   

zBTestBoxBoundariesIntersections.test_3d_box_constraints_entire_lineN)r*   r+   r,   rA   rB   rK   rL   r'   r'   r'   r(   r<   d   s
    7Cr<   c                   @   r-   )$TestBoxSphereBoundariesIntersectionsc                 C   s  t ddgddgddgddgddd\}}}t||gddg t|d	 t ddgddgdd
gddgddd\}}}t||gddg t|d	 t ddgddgdd
gddgddd\}}}t||gddg t|d	 t ddgddgdd
gddgddd\}}}t||gddg t|d	 t ddgddgdd
gddgddd\}}}t|d t ddgddgddgddgddd\}}}t|d d S )Nr   r   r   r   Fr7   r   r/   Tr   r   
   r:   r         ?r   r   r   r2   r'   r'   r(   rA     D   




z<TestBoxSphereBoundariesIntersections.test_2d_box_constraintsc                 C   s  t ddgddgddgddgddd\}}}t||gddg t|d t ddgddgdd	gdd
gddd\}}}t||gddg t|d t ddgddgdd	gdd
gddd\}}}t||gddg t|d t ddgddgdd	gdd
gddd\}}}t||gddg t|d t ddgddgdd	gdd
gddd\}}}t|d t ddgddgddgddgddd\}}}t|d d S )Nr   r   r   r   Tr7   r   r/   r   r   rN   r:   r   rO   FrP   r2   r'   r'   r(   rB   2  rQ   zHTestBoxSphereBoundariesIntersections.test_2d_box_constraints_entire_lineN)r*   r+   r,   rA   rB   r'   r'   r'   r(   rM   	  s    'rM   c                   @   r-   )TestModifiedDoglegc                 C   s   t ddgg}t dg}t|\}}}t ddg}t|||dt j t j gt jt jg}t|| t|||dt j t j gt jt jg}t||t j|  t|||dt j t j gdt jg}t|||d  d  d S )	Nr      g(j'?gԶ?r   皙?r   )r   r   r	   r   r@   r   linalgnorm)r    r"   r$   _Ynewton_pointr%   r'   r'   r(   %test_cauchypoint_equalsto_newtonpoint\  s   &
&$z8TestModifiedDogleg.test_cauchypoint_equalsto_newtonpointc              	   C   s0  t g dg dg}t ddg}t|\}}}t g d}t g d}t |}t|||dt j t j t j gt jt jt jg}	t|	| t|||dt j t j t j gt jt jt jg}	|}
|| }|	|
 | }t|t dd tt j	|	d t|||d	d
t j t j gt jt jt jg}	|}
|| }|	|
 | }t|t dd t|	d d
 t|||dt j t j t j gt jt jt jg}	|}
|}|	|
 | }t|t dd tt j	|	d t|||dt j t j t j gt jdt jg}	|}
|}|	|
 | }t|t dd t|	d d d S )N)r   rS   r   )r   r   r   rT   r   )g
m^>gLl@gk߿)g\B6w?g?g6y%$-p?r   g#wy?r   r   gv?r   r   g|:]?gQZ?)
r   r   r	   
zeros_liker   r@   r   fullrV   rW   )r    r"   r$   ZLSrY   rZ   cauchy_pointoriginr%   zdtr'   r'   r(   test_3d_examplen  sX   


z"TestModifiedDogleg.test_3d_exampleN)r*   r+   r,   r[   re   r'   r'   r'   r(   rR   Z  s    rR   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d Zdd Zdd Zdd ZdS )TestProjectCGc           
      C   s   t g dg dg dg}t g dg dg}tg d}tddg }t|\}}}t|||||\}}	t|	d	 d
 t|	d d t|g d d S )Nr   r   r   r   r   r   r   r   	stop_condr   hits_boundaryFr   )r   r   r   r	   r   r   r   
r    r!   r"   r#   r$   r^   rX   rY   r%   infor'   r'   r(   r)     s   z"TestProjectCG.test_nocedal_examplec                 C   s   t g dg dg dg dg}t g dg dg}tg d}tdd	g }t|\}}}t|||||d	d
\}}	t||||\}
}t|	d d t|	d d 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   tolrg   r   rh   F)r   r   r   r	   r   r   r   r   r    r!   r"   r#   r$   r^   rX   rY   r%   rj   x_kktr'   r'   r(   test_compare_with_direct_fact  s    z+TestProjectCG.test_compare_with_direct_factc           	   	   C   s   t g dg dg dg dg}t g dg dg}tg d}tdd	g }d
}t|\}}}tt t||||||d W d    d S 1 sMw   Y  d S )Nrk   rl   rm   rn   rp   rq   rr   r   r   r   )trust_radiusr   r   r   r	   pytestraises
ValueErrorr   )	r    r!   r"   r#   r$   rx   r^   rX   rY   r'   r'   r(   test_trust_region_infeasible  s   "z*TestProjectCG.test_trust_region_infeasiblec              	   C   s   t g dg dg dg dg}t g dg dg}tg d}tdd	g }d
}t|\}}}t|||||d	|d\}	}
t|
d d t|
d d ttj|	| t|	|	|  d S )Nrk   rl   rm   rn   rp   rq   rr   r   r   g/7@rt   rx   rg   r   rh   T)
r   r   r   r	   r   r   r   rV   rW   dotr    r!   r"   r#   r$   rx   r^   rX   rY   r%   rj   r'   r'   r(   !test_trust_region_barely_feasible  s(   
z/TestProjectCG.test_trust_region_barely_feasiblec              	   C   s   t g dg dg dg dg}t g dg dg}tg d}tdd	g }d}t|\}}}t|||||d	|d
\}	}
t|
d d t|
d d ttj|	| d S )Nrk   rl   rm   rn   rp   rq   rr   r   r   r~   rg   r   rh   T	r   r   r   r	   r   r   r   rV   rW   r   r'   r'   r(   test_hits_boundary  s&   
z TestProjectCG.test_hits_boundaryc              	   C   s   t g dg dg dg dg}t g dg dg}tg d}tdd	g }t|\}}}tt t|||||d	d
 W d    d S 1 sKw   Y  d S )Nr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rp   r   r   r   r   rr   r   r   rs   ry   )r    r!   r"   r#   r$   r^   rX   rY   r'   r'   r(   %test_negative_curvature_unconstrained  s   "z3TestProjectCG.test_negative_curvature_unconstrainedc              	   C   s   t g dg dg dg dg}t g dg dg}tg d}tdd	g }t|\}}}d
}t|||||d	|d\}	}
t|
d d t|
d d ttj|	| d S )Nr   r   r   r   rp   r   rr   r   r     r~   rg   rh   Tr   r    r!   r"   r#   r$   r^   rX   rY   rx   r%   rj   r'   r'   r(   test_negative_curvature  s&   
z%TestProjectCG.test_negative_curvaturec                 C   s   t g dg dg dg dg}t g dg dg}tg d}tdd	g }t|\}}}t|||||d	d
tj tj tj gdd\}}	t||||\}
}t|	d d t|	d d t||
 d S )Nrk   rl   rm   rn   rp   rq   rr   r   r   r/   Trt   lb
return_allrg   r   rh   F)	r   r   r   r	   r   r@   r   r   r   ru   r'   r'   r(   test_inactive_box_constraints  s,   
z+TestProjectCG.test_inactive_box_constraintsc           
      C   s   t g dg dg dg dg}t g dg dg}tg d}tdd	g }t|\}}}t|||||d	d
tj tj tj gdd\}}	t|	d d t|	d d t|||  t|d	 d
 d S )Nrk   rl   rm   rn   rp   rq   rr   r   r   g?Tr   rg   r   rh   )	r   r   r   r	   r   r@   r   r   r   ri   r'   r'   r(   6test_active_box_constraints_maximum_iterations_reached2  s,   
zDTestProjectCG.test_active_box_constraints_maximum_iterations_reachedc                 C   s   t g dg dg dg dg}t g dg dg}tg d}tdd	g }d}t|\}}}t|||||d	tjtjd
tjg|dd	\}	}
t|
d d t|
d d t|	d d
 d S )Nrk   rl   rm   rn   rp   rq   rr   r   r   g?Trt   ubrx   r   rg   r   rh   r   r   r   r	   r   r@   r   r   r   r'   r'   r(   +test_active_box_constraints_hits_boundariesH  *   
z9TestProjectCG.test_active_box_constraints_hits_boundariesc                 C   s   t g dg dg dg dg}t g dg dg}tg d}tdd	g }d
}t|\}}}t|||||d	tjdtjtjg|dd	\}	}
t|
d d t|
d d t|	d d d S )Nrk   rl   rm   rn   rp   rq   rr   r   r   r   rU   Tr   rg   r   rh   r   r   r   r'   r'   r(   ;test_active_box_constraints_hits_boundaries_infeasible_iter^  r   zITestProjectCG.test_active_box_constraints_hits_boundaries_infeasible_iterc                 C   s   t g dg dg dg dg}t g dg dg}tg d}tdd	g }t|\}}}d
}t|||||d	tjtjdtjg|d\}	}
t|
d d t|
d d t|	d d d S )Nr   r   r   r   rp   r   rr   r   r   r   r=   )rt   r   rx   rg   rh   Tr   r   r   r'   r'   r(   .test_active_box_constraints_negative_curvaturet  s(   
z<TestProjectCG.test_active_box_constraints_negative_curvatureN)r*   r+   r,   r)   rw   r}   r   r   r   r   r   r   r   r   r   r'   r'   r'   r(   rf     s    rf   )numpyr   scipy.sparser   0scipy.optimize._trustregion_constr.qp_subproblemr   r   r   r   r   r   .scipy.optimize._trustregion_constr.projectionsr	   numpy.testingr
   r   r   rz   r   r.   r<   rM   rR   rf   r'   r'   r'   r(   <module>   s     C &QL