a
    Re*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   f/var/www/sunrise/env/lib/python3.9/site-packages/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slJ d|d< ||||f}|||| ddg}t|j|dd|jd tj tjf}|jdksJ ||||f}|||| |jdksJ 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   )r   r6   r>   r7   r8   Zfgsfr   r   r   test_fun_and_grad   s.    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 g d}t||dddd d t j t jf}||jusFJ t||d ||jusdJ d	|d
< ||}t|d d|d
< ||}t|d ||jusJ t }t g d}t||dd|d t j t jf}||jusJ t||d ||jus
J d	|d
< ||}t|d d|d
< ||}t|d ||jusRJ dd }t g d}t||dddd d t j t jf}||jusJ t||d t|jt g d ||jusJ 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>.f)r+   r1   rB   r   r?   c                 S   s   | S Nr   rR   r   r   r   <lambda>)      z;TestScalarFunction.test_x_storage_overlap.<locals>.<lambda>g      ,@r,   r   g      *@r   c                 S   s   | | 9 } t | S rS   )r"   rQ   rR   r   r   r   ffK  s    z5TestScalarFunction.test_x_storage_overlap.<locals>.ffc                 S   s   | S rS   r   rR   r   r   r   rT   Q  rU   )r"   r#   r
   r3   r   r   r    r   )r   r4   r   r@   f1f2r&   rV   r   r   r   test_x_storage_overlap   sF    "







z)TestScalarFunction.test_x_storage_overlapc              	   C   s   t g d}tt|dttd d }|g d || |g d |g d t|j	d t|j
g d tt|ddtd t j t jf}|g d || |g d |g d t|j	d t|j
g d d S )N)r   r.   r!   r   )r   r   r   )g)\(?r   r+   r,   )r+   r+   r+   r-   )r"   r#   r
   r   r   r   r    r$   r   Z	_lowest_fZ	_lowest_xr3   )r   r7   r@   r   r   r   test_lowest_xX  s&    

z TestScalarFunction.test_lowest_xN)r'   r(   r)   r<   rA   rP   rY   rZ   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   rL   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-   rB   rC   rD   r!      rb   rE   rF   r2   r0   rG   rH   )r[   r   r    r_   r&   r"   r3   r   r   r\   r   r   rI   rJ   r	   r4   r   rd   rK   )r   r6   r   r\   r   r7   Zv0r8   r9   pr   rM   rN   rL   rO   r   r   r   rP     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usFJ t|||| ||jusjJ d|d< t|||| ||jusJ d|d< t|||| ||jusJ t	 }tddg}t|j|d|d d tj tjfd }t
t ||jusJ t|||| ||jus:J d|d< t|||| ||jushJ d|d< t|||| ||jusJ W d    n1 s0    Y  d S )Nr+   r,   r?   r1   r   )r[   r"   r#   r   r    r&   r3   r   r   r   pytestZwarnsUserWarning)r   r6   r7   Zvfr&   r   r   r   rY   g  s:    z,TestVectorialFunction.test_x_storage_overlapN)r'   r(   r)   re   rP   rY   r   r   r   r   ra     s   ` ra   c                  C   sD  t g dg dg} t d}t| }t g d}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 )Nr   r   r   r!   r   r.   )r   rk   r   rk   r   TFr.   r.   )r"   r#   zerosr   r   r   sparse_jacobianr   r    r_   toarrayr&   )ZA_denser7   ZA_sparser   rL   ZAxrW   rX   f3Zf4Zf5Zf6r   r   r   test_LinearVectorFunction  s4    



rt   c                  C   s   t g dg dg} t g d}t| |d}t||j t| ||j t g d}t| || t||j t| ||j t| ||| t| ||j d S )Nrj   rl   )r   r   rk   F)rk   r.   r/   )	r"   r#   r   r   r   rK   r4   r_   r    )Ar7   r    x1r   r   r   %test_LinearVectorFunction_memoization  s    rw   c                  C   s   t d} t| d }t| d}t| d}t|j t|j  t|j t g d}t g d}t||| t||| t||	 t 
d t||t 
d t|||	 t d d S )Nr.   FT)rk   r   r   )r.   r   ro   )r"   rp   r   r   rq   r#   r   r    r_   rr   r%   r&   )r7   rW   rX   rs   r   rL   r   r   r   test_IdentityVectorFunction  s    





ry   ) rh   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[   ra   rt   rw   ry   r   r   r   r   <module>   s$      L  $