o
    g*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                   @   ,   e Zd Zdd Zdd Zdd Zdd Zd	S )
ExScalarFunctionc                 C      d| _ d| _d| _d S Nr   )nfevngevnhevself r   p/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/scipy/optimize/tests/test_differentiable_functions.py__init__      
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_analitg_analitf_approxg_approxr   r   r   test_finite_difference_grad&   s   



















z.TestScalarFunction.test_finite_difference_gradc              	   C   s2  t  }dd }ddg}t|j|d|j|jd tj tjf}||||f}|||| |jdks6J d|d< ||||f}|||| ddg}t|j|dd|jd tj tjf}|jdkshJ ||||f}|||| |jdksJ 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_allcloser4   r3   r   r   r.   3-point)	r   r
   r#   r'   r)   r%   r6   fun_and_gradr   )r   r9   rC   r:   r;   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| qs|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|
||| 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|
||| qG|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   r0   r4         @      @      @r$    @333333?r5   r3   @ffffff@)r   r
   r#   r'   r)   r%   r6   r   r   r   r   r   
isinstanceHr	   r7   r   r8   dot)r   r9   r   r   r   r:   r;   r<   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s#J t||d ||jus2J d	|d
< ||}t|d d|d
< ||}t|d ||jusUJ t }t g d}t||dd|d t j t jf}||jusuJ t||d ||jusJ d	|d
< ||}t|d d|d
< ||}t|d ||jusJ 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%   sumasarrayr"   r   r   r   r7   %  s   z4TestScalarFunction.test_x_storage_overlap.<locals>.f)r.   r4   rI   r   rD   c                 S      | S Nr   rZ   r   r   r   <lambda>)      z;TestScalarFunction.test_x_storage_overlap.<locals>.<lambda>g      ,@r/   r   g      *@r   c                 S   s   | | 9 } t | S r\   )r%   rX   rZ   r   r   r   ffK  s   
z5TestScalarFunction.test_x_storage_overlap.<locals>.ffc                 S   r[   r\   r   rZ   r   r   r   r]   Q  r^   )r%   r&   r
   r6   r"   r   r#   r   )r   r7   r"   rG   f1f2r)   r_   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    r1   r$   r   )r   r   r   )g)\(?r   r.   r/   )r.   r.   r.   r0   )r%   r&   r
   r   r   r   r#   r'   r   	_lowest_f	_lowest_xr6   )r   r:   rG   r   r   r   test_lowest_xX  s&   

z TestScalarFunction.test_lowest_xN)r*   r+   r,   rA   rH   rW   rb   re   r   r   r   r   r-   $   s    ]!|8r-   c                   @   r   )
ExVectorialFunctionc                 C   r   r   )r   njevr   r   r   r   r   r   p  r   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$   r1   )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    r1      )rg   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       ri   )r   r%   r(   r&   )r   r"   rS   r   r   r   r)     s   ,
zExVectorialFunction.hessN)r*   r+   r,   r   r#   rj   r)   r   r   r   r   rf   n  s
    rf   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   r0   r1   r2   r3   r$   decimalr4   r5   r    )rf   r   r#   rj   r)   r%   r6   r   r   rg   r7   r   J)r   r9   r   rg   r:   r;   r<   r"   r=   J_analitr?   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|	 qy|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|||| 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||||d
d qY|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/   r4   r   r0   rI   rJ   rK   r$      rm   rL   rM   r5   r3   rN   rO   )rf   r   r#   rj   r)   r%   r6   r   r   rg   r   r   rP   rQ   r	   r7   r   ro   rR   )r   r9   r   rg   r   r:   v0r;   r<   pr"   rT   rU   rS   rV   r   r   r   rW     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s#J t|||| ||jus5J d|d< t|||| ||jusKJ d|d< t|||| ||jusaJ t	 }tddg}t|j|d|d d tj tjfd }t
tN ||jusJ t|||| ||jusJ d|d< t|||| ||jusJ d|d< t|||| ||jusJ W d    d S 1 sw   Y  d S )Nr.   r/   rD   r4   r   )rf   r%   r&   r   r#   r)   r6   r"   r   r   pytestwarnsUserWarning)r   r9   r:   vfr)   r   r   r   rb   g  s<   "z,TestVectorialFunction.test_x_storage_overlapN)r*   r+   r,   rr   rW   rb   r   r   r   r   rl     s
    ` rl   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    r1   )r   r{   r   r{   r   TFr1   r1   )r%   r&   zerosr   r   r   sparse_jacobianr   r#   rj   toarrayr)   )A_denser:   A_sparser"   rS   Axr`   ra   f3f4f5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 )Nrz   r|   )r   r    r{   F)r{   r1   r2   )	r%   r&   r   r   r"   rR   r7   rj   r#   )Ar:   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 )Nr1   FT)r{   r    r   )r1   r   r   )r%   r   r   r   r   r&   r   r#   rj   r   r(   r)   )r:   r`   ra   r   r"   rS   r   r   r   test_IdentityVectorFunction  s   





 r   ) rv   numpyr%   numpy.testingr   r   r   r   r   r   scipy.sparser   scipy.sparse.linalgr	   (scipy.optimize._differentiable_functionsr
   r   r   r   scipy.optimizer   r   r   'scipy.optimize._hessian_update_strategyr   r   r-   rf   rl   r   r   r   r   r   r   r   <module>   s&       L  $