
    xf                      j    d dl Z d dlZd dlmZmZmZ d dlmZm	Z	m
Z
  G d d      Zd Z G d d      Zy)	    N)assert_allcloseassert_assert_array_equal)fmin_cobylaminimizeBoundsc                   6    e Zd Zd Zd Zd Zd Zd Zd Zd Z	y)	
TestCobylac                 f    ddg| _         t        j                  d      dg| _        ddddd	| _        y )
Ng@gQ?g888@gUUUUUU?F   h㈵>d   )disprhobegtolmaxiter)x0mathsqrtsolutionoptsselfs    B/usr/lib/python3/dist-packages/scipy/optimize/tests/test_cobyla.pysetup_methodzTestCobyla.setup_method
   s5    ,?3U;"a #%	    c                 6    |d   dz  t        |d         dz  z   S )Nr      r      )absr   xs     r   funzTestCobyla.fun   s!    tQwQqTA%%r   c                 *    |d   dz  |d   dz  z   dz
  S )Nr   r   r       r!   s     r   con1zTestCobyla.con1   s"    tQw1q 2%%r   c                 &    | j                  |       S N)r'   r!   s     r   con2zTestCobyla.con2   s    		!}r   c           	          t        | j                  | j                  | j                  | j                  gdddd      }t        || j                  d       y )Nr   r   r   T)r   rhoendmaxfunr   -C6?atol)r   r#   r   r'   r*   r   r   r!   s     r   test_simplezTestCobyla.test_simple   s@    $''DIItyy+A!#Cd<4==t4r   c                     G d d      } |       }d| j                   dd| j                  df}t        | j                  | j                  d||| j
                        }t        |j                  | j                  d       t        |j                  |j                         t        |j                  d	k  |       t        |j                  d
k  |       t        |j                  | j                  | j                        dz   k  |       t        |j                  |j                  k(  d       t        |j                  |j                   d       y )Nc                       e Zd Zd Zd Zy)1TestCobyla.test_minimize_simple.<locals>.Callbackc                      d| _         d | _        y )Nr   n_callslast_xr   s    r   __init__z:TestCobyla.test_minimize_simple.<locals>.Callback.__init__!   s     "r   c                 <    | xj                   dz  c_         || _        y Nr   r6   r!   s     r   __call__z:TestCobyla.test_minimize_simple.<locals>.Callback.__call__%   s    !r   N)__name__
__module____qualname__r9   r<   r&   r   r   Callbackr4       s    # r   r@   ineqtyper#   cobyla)methodconstraintscallbackoptionsr.   r/   r   F   MbP?z<Callback is not called exactly once for every function eval.zGLast design vector sent to the callback is not equal to returned value.)r'   r*   r   r#   r   r   r   r"   r   r   successmessagemaxcvnfevr7   r   r8   )r   r@   rG   conssols        r   test_minimize_simplezTestCobyla.test_minimize_simple   s    	  	  :  		2		24txxt ($))=t}}48S[[)		D #&2s#$((4==1D88#>H,,,N	P355(//d	fr   c                   	
 t         j                  j                  d       t         j                  j                  dd      	t         j                  j                  d      
	fd
fd}fd}fd}fd}d|d	d|d	d|d	f}t        j                  d
      }t        ||d|ddi      }t        |j                  dkD         t        |j                          y )Ni  
   c                 &    j                  |       S r)   )dot)wpbs    r   pz8TestCobyla.test_minimize_constraint_violation.<locals>.p?   s    66!9r   c                 ,    | z  j                          S r)   )sum)rV   spreads    r   fz8TestCobyla.test_minimize_constraint_violation.<locals>.fB   s    Z$$&&&r   c                 H    dt         |             j                         z
  S )Ni  r    rZ   rV   rX   s    r   c1z9TestCobyla.test_minimize_constraint_violation.<locals>.c1E   s    QqT((r   c                 H    dt         |       j                               z
  S N   r^   r_   s    r   c2z9TestCobyla.test_minimize_constraint_violation.<locals>.c2H   s    s1Q488:&&r   c                 H    dt         |             j                         z
  S rb   )r    maxr_   s    r   c3z9TestCobyla.test_minimize_constraint_violation.<locals>.c3K   s    s1Q4y}}&&r   rA   rB   )rS   rD   catolgư>)rE   rF   rH   )	nprandomseedrandzerosr   r   rM   rK   )r   r\   r`   rd   rg   rO   w0rP   rX   rW   r[   s           @@@r   "test_minimize_constraint_violationz-TestCobyla.test_minimize_constraint_violation:   s    
		tYY^^B##		'	)	'	'  +++- XXe_q"X4 '0		D !CKK r   N)
r=   r>   r?   r   r#   r'   r*   r1   rQ   ro   r&   r   r   r
   r
   	   s&    %&&5f6!r   r
   c                  *  
 d 

fd} d }d }t        j                  ddg      }
||g}ddg}d	}t        
||d
      }t        ||d       t        
|| d
      }t         
|      dd       |D cg c]  }d|d	 }	}t	        
||	d
      }t        |j
                  |d       t        |j                  |j                         t        |j                  |d       d| d}	t	        
||	d
      }t        |j                  dd       y c c}w )Nc                 0    | d   dz
  dz  | d   dz
  dz  z   S )Nr   r   r   g      @r&   r"   s    r   r#   z$test_vector_constraints.<locals>.fun[   s%    !q1}!s
Q..r   c                      |       dz
  S r;   r&   )r"   r#   s    r   fminz%test_vector_constraints.<locals>.fmin^   s    1vzr   c           	          t        j                  g dg dg dg      }t        j                  t        t        |            D cg c]%  }||df   | d   z  ||df   | d   z  z   ||df   z   ' c}      S c c}w )N)r   r   )rv      )rw   r   r   r   r   r   )ri   arrayrangelen)r"   ais      r   cons1z&test_vector_constraints.<locals>.cons1a   s    HHj+z:;xx*/A-9%& 1a41Q4!AqD'AaD.81a4! 9 : 	: 9s   *A5c                     | S r)   r&   rr   s    r   cons2z&test_vector_constraints.<locals>.cons2f   s    r   r   r   gffffff?g333333?g?r   )r,   r.   r/   r   rA   rB   )rF   r   )
ri   ry   r   r   r   r"   r   rK   rL   r#   )rt   r~   r   r   	cons_listxsolfsolrP   rO   rF   r#   s             @r   test_vector_constraintsr   X   s   /:
 
1a&	BeU#I:DD c2y
6CCD)
c2tD
1CCHad+ >GGTF40GKG
3
>CCEE4d+CKK%CGGT-!$/K
3
>CCGGQT* Hs   6Dc                       e Zd Zd Zd Zy)
TestBoundsc                     d }g d}g d}t        ||      D cg c]	  \  }}||f }}}t        |g dd|      }g d}|j                  sJ t        |j                  |d	       y c c}}w )
Nc                 2    t        j                  | dz        S Nr   ri   rZ   rr   s    r   r\   z TestBounds.test_basic.<locals>.f       66!Q$<r   )rw   Nr   N      )r   r   NNr   )r   r   r      rc   rD   r   rE   bounds)r   r   r   r   r   rJ   r/   )zipr   rK   r   r"   )	r   r\   lbubr|   br   resrefs	            r   
test_basiczTestBounds.test_basic   sl    	  '+%(R[1TQ1a&11 q_XfM&{{{s. 2s   A(c                    d }t        t        j                   t        j                   gt        j                  t        j                  g      }t        |ddgd|      }|j                  sJ t        |j                  dd       t        dt        j                   gt        j                  t        j                  g      }t        |ddgd|      }|j                  sJ t        |j                  ddgd       y )	Nc                 2    t        j                  | dz        S r   r   rr   s    r   r\   z$TestBounds.test_unbounded.<locals>.f   r   r   r   r   rD   r   r   rJ   r/   )r   ri   infr   rK   r   r"   )r   r\   r   r   s       r   test_unboundedzTestBounds.test_unbounded   s    	  "&&266'*RVVRVV,<=qaVHVD{{{qt,RVVGrvvrvv&67qaVHVD{{{1vD1r   N)r=   r>   r?   r   r   r&   r   r   r   r      s    
/2r   r   )r   numpyri   numpy.testingr   r   r   scipy.optimizer   r   r   r
   r   r   r&   r   r   <module>r      s4      F F 8 8L! L!^'+T2 2r   