a
    hi                     @   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   n/var/www/html/assistant/venv/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                   @   s<   e Zd Zd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_xc              	   C   sZ   t g dt j}dd }t||ddtd t j t jf}||}|jt jksVJ d S )NrZ   c                 S   s   | j tjksJ t| S rS   )dtyper"   float32r   rR   r   r   r   rosen_r  s    z2TestScalarFunction.test_float_size.<locals>.rosen_r   r-   )	r"   r#   astyper]   r
   r   r3   r    r\   )r   r7   r^   r@   resr   r   r   test_float_sizem  s    
z"TestScalarFunction.test_float_sizeN)	r'   r(   r)   r<   rA   rP   rY   r[   ra   r   r   r   r   r*   $   s   ]!|8r*   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   ~  s    zExVectorialFunction.__init__c                 C   sn   |  j d7  _ tjd|d d |d d  d  |d  d|d d |d d  d  d|d   g|jdS )Nr   r   r   r!   r.   r\   )r   r"   r#   r\   r   r   r   r   r      s    **zExVectorialFunction.func                 C   sV   |  j d7  _ tjd|d  d d|d  gd|d d  d d|d  gg|jdS )	Nr   r!   r      r   r.      rd   )rc   r"   r#   r\   r   r   r   r   jac  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      rf   )r   r"   r%   r#   )r   r   rL   r   r   r   r&     s    ,zExVectorialFunction.hessN)r'   r(   r)   r   r    rg   r&   r   r   r   r   rb   |  s   rb   c                   @   s,   e Zd Zd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   )rb   r   r    rg   r&   r"   r3   r   r   rc   r4   r   J)r   r6   r   rc   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!      rj   rE   rF   r2   r0   rG   rH   )rb   r   r    rg   r&   r"   r3   r   r   rc   r   r   rI   rJ   r	   r4   r   rl   rK   )r   r6   r   rc   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   )rb   r"   r#   r   r    r&   r3   r   r   r   pytestZwarnsUserWarning)r   r6   r7   vfr&   r   r   r   rY   u  s:    z,TestVectorialFunction.test_x_storage_overlapc              	   C   sz   t  }tddgtj}t|j||j|jd d tj	 tj	fd }||}|j
tjks\J ||}|j
tjksvJ d S )Nr+   r,   )rb   r"   r#   r_   r]   r   r    rg   r&   r3   r\   )r   r6   r7   rr   r`   r   r   r   ra     s    

z%TestVectorialFunction.test_float_sizeN)r'   r(   r)   rm   rP   rY   ra   r   r   r   r   ri     s
   ` +ri   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   rt   r   rt   r   TFr.   r.   )r"   r#   zerosr   r   r   sparse_jacobianr   r    rg   toarrayr&   )ZA_denser7   ZA_sparser   rL   ZAxrW   rX   f3Zf4Zf5Zf6r   r   r   test_LinearVectorFunction  s4    



r}   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 )Nrs   ru   )r   r   rt   F)rt   r.   r/   )	r"   r#   r   r   r   rK   r4   rg   r    )Ar7   r    x1r   r   r   %test_LinearVectorFunction_memoization  s    r   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)rt   r   r   )r.   r   rx   )r"   ry   r   r   rz   r#   r   r    rg   r{   r%   r&   )r7   rW   rX   r|   r   rL   r   r   r   test_IdentityVectorFunction  s    





r   ) rp   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*   rb   ri   r}   r   r   r   r   r   r   <module>   s$      Z  $