U
    /d*f                     @   s   d dl Z d dl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 d dlmZmZmZmZ d dlmZmZmZ d dlmZ G dd	 d	ZG d
d deZG dd dZG dd deZdd Zdd Zdd ZdS )    N)TestCaseassert_array_almost_equalassert_array_equalassert_assert_allcloseassert_equal)
csr_matrix)LinearOperator)ScalarFunctionVectorFunctionLinearVectorFunctionIdentityVectorFunction)rosen	rosen_der
rosen_hess)BFGSc                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
ExScalarFunctionc                 C   s   d| _ d| _d| _d S Nr   )nfevngevnhevself r   V/tmp/pip-unpacked-wheel-9gxwnfpp/scipy/optimize/tests/test_differentiable_functions.py__init__   s    zExScalarFunction.__init__c                 C   s6   |  j d7  _ d|d d |d d  d  |d  S )N      r   )r   r   xr   r   r   fun   s    zExScalarFunction.func                 C   s0   |  j d7  _ td|d  d d|d  gS )Nr      r   )r   nparrayr   r   r   r   grad   s    zExScalarFunction.gradc                 C   s   |  j d7  _ dtd S )Nr   r!   r   )r   r"   eyer   r   r   r   hess   s    zExScalarFunction.hessN)__name__
__module____qualname__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 )TestScalarFunctionc              	   C   s  t  }d}d}ddg}t|j|d|j|jd tj tjf}|d7 }|d7 }t|j| t|j| t|j	| t|j	| t|j|dd|jd tj tjf}|d7 }|d7 }t|j| t|j|j | t|j	|j	 | t|j
|j
 t|j|j dd	g}||}||}	|d7 }|d7 }t|j| t|j|j | t|j	|j	 | ||}
||}|d7 }|d7 }t|j| t|j|j | t|j	|j	 | t||
 t|	| d
dg}||}	|d7 }t|j| t|j|j | t|j	|j	 | ||}|d7 }|d7 }t|j| t|j|j | t|j	|j	 | t|	| dd	g}||}||}	|d7 }|d7 }t|j| t|j|j | t|j	|j	 | ||}
||}|d7 }|d7 }t|j| t|j|j | t|j	|j	 | t||
 t|	| dd	g}||}||}	|d7 }|d7 }t|j| t|j|j | t|j	|j	 | ||}
||}|d7 }|d7 }t|j| t|j|j | t|j	|j	 | t||
 t|	| d S )Nr         ?        r   r   2-point   
   333333?       @      @r   )r   r
   r    r$   r&   r"   infr   r   r   fr   g)r   exr   r   x0analitapproxr   f_analitZg_analitf_approxZg_approxr   r   r   test_finite_difference_grad&   s        



















z.TestScalarFunction.test_finite_difference_gradc              	   C   s4  t  }dd }ddg}t|j|d|j|jd tj tjf}||||f}|||| |jdkslt	d|d< ||||f}|||| ddg}t|j|dd|jd tj tjf}|jdkst	||||f}|||| |jdkst	d|d< ||||f}|||| d S )	Nc                 S   s(   t | d |d  t | d |d  d S )Nr   r   )r   )r   yr   r   r   fg_allclose   s    z9TestScalarFunction.test_fun_and_grad.<locals>.fg_allcloser1   r0   r   r   r+   3-point)
r   r
   r    r$   r&   r"   r3   Zfun_and_gradr   AssertionError)r   r6   r>   r7   r8   Zfgsfr   r   r   test_fun_and_grad   s6        z$TestScalarFunction.test_fun_and_gradc              	   C   sP  t  }d}d}d}ddg}t|j|d|j|jd tj tjf}|d7 }|d7 }|d7 }t|j| t|j| t|j	| t|j	| t|j
| t|j
| t|j|d|jdd tj tjf}tt|jt ddgdd	gd
dgfD ]:}t|j|j t|j|j t|j||j| q|d7 }|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | ddg}	||	}
|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | ||	}tt|t ddgdd	gd
dgfD ]}t|
||| q.|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | ddg}	||	}
|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | ||	}tt|t ddgdd	gd
dgfD ]}t|
||| qP|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | ddg}	||	}||	}
|d7 }|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | ||	}||	}tt|t ddgdd	gd
dgfD ]}t|
||| q|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | ddg}	||	}||	}
|d7 }|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | ||	}||	}tt|t ddgdd	gd
dgfD ]}t|
||| q|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | d S )Nr   r+   r,   r   r   r-   r1         @      @      @r!    @333333?r2   r0   @ffffff@)r   r
   r    r$   r&   r"   r3   r   r   r   r   r   
isinstanceHr	   r4   r   r5   dot)r   r6   r   r   r   r7   r8   r9   vr   H_analitH_approx_r   r   r   +test_finite_difference_hess_linear_operator   s        











z>TestScalarFunction.test_finite_difference_hess_linear_operatorc              	   C   s  dd }t dddg}t||dddd	 d t j t jf}||jk	sHtt||d
 ||jk	sftd|d< ||}t|d d|d< ||}t|d
 ||jk	stt }t dddg}t||dd|d t j t jf}||jk	stt||d
 ||jk	std|d< ||}t|d d|d< ||}t|d
 ||jk	sVtdd }t dddg}t||dddd	 d t j t jf}||jk	stt||d
 t|jt dddg ||jk	std S )Nc                 S   s   t t | d S )Nr   )r"   sumZasarrayr   r   r   r   r4   %  s    z4TestScalarFunction.test_x_storage_overlap.<locals>.fr+   r1   rC   r   r?   c                 S   s   | S Nr   rS   r   r   r   <lambda>)      z;TestScalarFunction.test_x_storage_overlap.<locals>.<lambda>g      ,@r,   r   g      *@r   c                 S   s   | | 9 } t | S rT   )r"   rR   rS   r   r   r   ffK  s    z5TestScalarFunction.test_x_storage_overlap.<locals>.ffc                 S   s   | S rT   r   rS   r   r   r   rU   Q  rV   )	r"   r#   r
   r3   r   r@   r   r    r   )r   r4   r   rA   f1f2r&   rW   r   r   r   test_x_storage_overlap   sR    "







      z)TestScalarFunction.test_x_storage_overlapc              	   C   s   t dddg}tt|dttd d }|dddg || |dddg |dddg t|j	d t|j
dddg tt|dd	td t j t jf}|dddg || |dddg |dddg t|j	d t|j
dddg d S )
Nr   r.   r!   r   r   g)\(?r+   r,   r-   )r"   r#   r
   r   r   r   r    r$   r   Z	_lowest_fZ	_lowest_xr3   )r   r7   rA   r   r   r   test_lowest_xX  s*     
 
z TestScalarFunction.test_lowest_xN)r'   r(   r)   r<   rB   rQ   rZ   r[   r   r   r   r   r*   $   s
   ]!|8r*   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
ExVectorialFunctionc                 C   s   d| _ d| _d| _d S r   )r   njevr   r   r   r   r   r   p  s    zExVectorialFunction.__init__c                 C   sh   |  j d7  _ td|d d |d d  d  |d  d|d d |d d  d  d|d   gS )Nr   r   r   r!   r.   )r   r"   r#   r   r   r   r   r    u  s    **zExVectorialFunction.func                 C   sP   |  j d7  _ td|d  d d|d  gd|d d  d d|d  ggS )Nr   r!   r      r   r.      )r]   r"   r#   r   r   r   r   jacz  s    zExVectorialFunction.jacc                 C   sJ   |  j d7  _ |d d td |d td|d  dgddgg  S )Nr   r   r!   r      r_   )r   r"   r%   r#   )r   r   rM   r   r   r   r&     s    ,zExVectorialFunction.hessN)r'   r(   r)   r   r    r`   r&   r   r   r   r   r\   n  s   r\   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestVectorialFunctionc              	   C   s2  t  }d}d}ddg}t|j||j|jd d tj tjfd }|d7 }|d7 }t|j| t|j| t|j	| t|j	| t|j|d|jd d tj tjfd }|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
|j
 t|j|j ddg}||}||}	|d7 }|d7 }t|j| t|j|j | t|j	| t|j	|j	 | ||}
||}|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t||
 t|	|d	d
 ddg}||}	|d7 }t|j| t|j|j | t|j	| t|j	|j	 | ||}|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t|	| ddg}||}||}	|d7 }|d7 }t|j| t|j|j | t|j	| t|j	|j	 | ||}
||}|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t||
 t|	| ddg}||}||}	|d7 }|d7 }t|j| t|j|j | t|j	| t|j	|j	 | ||}
||}|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t||
 t|	| d S )Nr   r+   r,   r   r-   r.   r/   r0   r!   decimalr1   r2   r   )r\   r   r    r`   r&   r"   r3   r   r   r]   r4   r   J)r   r6   r   r]   r7   r8   r9   r   r:   ZJ_analitr;   ZJ_approxr   r   r   test_finite_difference_jac  s      


















z0TestVectorialFunction.test_finite_difference_jacc              	   C   s  t  }d}d}d}ddg}ddg}t|j||j|jd d tj tjfd }|d7 }|d7 }|d7 }t|j| t|j| t|j	| t|j	| t|j
| t|j
| t|j||jdd d tj tjfd }tt|jt ddgddgd	dgfD ]:}	t|j|j t|j|j t|j|	|j|	 q|d7 }|d
7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | ddg}
||
|}|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | ||
|}tt|t ddgddgd	dgfD ] }	t||	||	dd q>|d
7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | ddg}
ddg}||
|}|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | ||
|}tt|t ddgddgd	dgfD ]}t|||| qp|d
7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | ddg}
||
}||
|}|d7 }|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | ||
}||
|}tt|t ddgddgd	dgfD ] }t||||d
d q|d
7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | ddg}
ddg}||
}||
|}|d7 }|d7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | ||
}||
|}tt|t ddgddgd	dgfD ] }t||||d
d q |d
7 }t|j| t|j|j | t|j	| t|j	|j	 | t|j
| t|j
|j
 | d S )Nr   r+   r,   r1   r   r-   rC   rD   rE   r!      rc   rF   rG   r2   r0   rH   rI   )r\   r   r    r`   r&   r"   r3   r   r   r]   r   r   rJ   rK   r	   r4   r   re   rL   )r   r6   r   r]   r   r7   Zv0r8   r9   pr   rN   rO   rM   rP   r   r   r   rQ     s      




zATestVectorialFunction.test_finite_difference_hess_linear_operatorc              	   C   s  t  }tddg}t|j|d|jd d tj tjfd }||jk	sFtt	|||| ||jk	sjtd|d< t	|||| ||jk	std|d< t	|||| ||jk	stt
 }tddg}t|j|d|d d tj tjfd }tt ||jk	stt	|||| ||jk	s:td|d< t	|||| ||jk	shtd|d< t	|||| ||jk	stW 5 Q R X d S )Nr+   r,   r?   r1   r   )r\   r"   r#   r   r    r&   r3   r   r@   r   r   pytestZwarnsUserWarning)r   r6   r7   Zvfr&   r   r   r   rZ   g  s>      z,TestVectorialFunction.test_x_storage_overlapN)r'   r(   r)   rf   rQ   rZ   r   r   r   r   rb     s   ` rb   c                  C   sJ  t dddgdddgg} t d}t| }t dddg}t ddg}t ddg}t| |d }t|j  t| |d	}t|j t| |d
}t|j  t||d }	t|	j t||d	}
t|
j t||d
}t|j  t||| t||| t|	||  t|	|
 |
  t|||
 t d d S )Nr   r   r!   r.   r   TFr.   r.   )r"   r#   zerosr   r   r   sparse_jacobianr   r    r`   toarrayr&   )ZA_denser7   ZA_sparser   rM   ZAxrX   rY   f3Zf4Zf5Zf6r   r   r   test_LinearVectorFunction  s4    



rs   c                  C   s   t dddgdddgg} t dddg}t| |d}t||j t| ||j t dddg}t| || t||j t| ||j t| ||| t| ||j d S )	Nrk   r   r   r!   r   Fr.   r/   )	r"   r#   r   r   r   rL   r4   r`   r    )Ar7   r    x1r   r   r   %test_LinearVectorFunction_memoization  s    rv   c                  C   s   t d} t| d }t| d}t| d}t|j t|j  t|j t dddg}t dddg}t||| t||| t||	 t 
d t||t 
d t|||	 t d	 d S )
Nr.   FTrk   r   r   r   rn   )r"   ro   r   r   rp   r#   r   r    r`   rq   r%   r&   )r7   rX   rY   rr   r   rM   r   r   r   test_IdentityVectorFunction  s    





rx   ) ri   Znumpyr"   Znumpy.testingr   r   r   r   r   r   Zscipy.sparser   Zscipy.sparse.linalgr	   Z(scipy.optimize._differentiable_functionsr
   r   r   r   Zscipy.optimizer   r   r   Z'scipy.optimize._hessian_update_strategyr   r   r*   r\   rb   rs   rv   rx   r   r   r   r   <module>   s$      L  $