
    Vpf                       d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	Z	ddl
mZ ddl	mZ ddlmZ  G d	 d
e          Z eej        ej        j                  Z eej        ej        j                  Zdej        ddfddZdS )z<The Broyden-Fletcher-Goldfarb-Shanno minimization algorithm.    )annotations)Callable)partial)
NamedTupleN)lax)line_searchc                      e Zd ZU dZded<   ded<   ded<   ded<   ded<   ded	<   d
ed<   d
ed<   d
ed<   d
ed<   d
ed<   ded<   ded<   dS )_BFGSResultsaE  Results from BFGS optimization.

  Parameters:
    converged: True if minimization converged.
    failed: True if line search failed.
    k: integer the number of iterations of the BFGS update.
    nfev: integer total number of objective evaluations performed.
    ngev: integer total number of jacobian evaluations
    nhev: integer total number of hessian evaluations
    x_k: array containing the last argument value found during the search. If
      the search converged, then this value is the argmin of the objective
      function.
    f_k: array containing the value of the objective function at `x_k`. If the
      search converged, then this is the (local) minimum of the objective
      function.
    g_k: array containing the gradient of the objective function at `x_k`. If
      the search converged the l2-norm of this tensor should be below the
      tolerance.
    H_k: array containing the inverse of the estimated Hessian.
    status: int describing end state.
    line_search_status: int describing line search end state (only means
      something if line search fails).
  zbool | jax.Array	convergedfailedzint | jax.Arrayknfevngevnhev	jax.Arrayx_kf_kg_kH_kold_old_fvalstatusline_search_statusN)__name__
__module____qualname____doc____annotations__     \/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/scipy/optimize/bfgs.pyr
   r
      s          . ............%%%%%%r   r
   )	precisiongh㈵>
   funr   x0r   maxiter
int | Nonegtolfloatline_search_maxiterintreturnc                    t          j        |          dz  |j        d         t          j        |j                  } t          j                   |          \  }}t          t           j        	                    |          k     ddddd|||||t           j        	                    |          dz  z   dd	          }	fd
}
 fd}t          j        |
||	          }	t          j        |	j        dt          j        |	j        k    dt          j        |	j        d|	j        z   d                              }|	                    |          }	|	S )al  Minimize a function using BFGS.

  Implements the BFGS algorithm from
    Algorithm 6.1 from Wright and Nocedal, 'Numerical Optimization', 1999, pg.
    136-143.

  Args:
    fun: function of the form f(x) where x is a flat ndarray and returns a real
      scalar. The function should be composed of operations with vjp defined.
    x0: initial guess.
    maxiter: maximum number of iterations.
    norm: order of norm for convergence check. Default inf.
    gtol: terminates minimization when |grad|_norm < g_tol.
    line_search_maxiter: maximum number of linesearch iterations.

  Returns:
    Optimization result.
  N   r   dtypeordF      )r   r   r   r   r   r   r   r   r   r   r   r   r   c                    t          j        | j                  t          j        | j                  z  | j        k     z  S )N)jnplogical_notr   r   r   )stater%   s    r    cond_funzminimize_bfgs.<locals>.cond_funy   s:    OEO,,oel++,w " #r   c           	        t          | j        | j                   }t          | j        || j        | j        | j                  }|                     | j        |j        z   | j	        |j	        z   |j
        |j                  } |j        |z  }| j        |z   }|j        }|j        }|| j        z
  }t          j        t          ||                    }|d d t          j        f         |t          j        d d f         z  }	t          j        |j                  ||	z  z
  }
t%          d|
| j        |
          ||d d t          j        f         z  |t          j        d d f         z  z   }t          j        t          j        |          || j                  }t          j                            |          k     }|                     || j        dz   ||||| j                  } | S )N)old_fvalr   gfkr%   )r   r   r   r   r.   zij,jk,lkr0   r2   )r   r   r   r   r   r   r   )_dotr   r   r   r   r   r   _replacer   r   r   r   a_kr5   
reciprocalnewaxiseyer/   _einsumwhereisfinitelinalgnormr   )r7   p_kline_search_resultss_kx_kp1f_kp1g_kp1y_krho_ksy_kwH_kp1r   dr#   r'   r)   rF   s                r    body_funzminimize_bfgs.<locals>.body_fun~   s   	59%%
%C%	'I#   NNZ-22Z-22").5	   E 
!C
'CIOE#E#E
%)
CN4S>>**Eqqq#+~S[!!!^!44D%%%4AZEIq11s111ck>**Saaa-@@AEIcl5))5%)<<E
40047INN
'A+Y   E Lr   )r   )r5   sizeshaperA   r/   jaxvalue_and_gradr
   rE   rF   r   
while_looprC   r   r   r   r   r=   )r#   r$   r%   rF   r'   r)   	initial_Hf_0g_0r7   r8   rS   r   rR   s   ` ````       @r    minimize_bfgsr]   G   s   6 _hrllS Ghqk!garx((()$S$$R(((#s

..5	



--11  % # # # # #
( ( ( ( ( ( ( ( (T .8U
3
3%9o	i
'W


)l%**   & ...
'
'%	,r   )r#   r   r$   r   r%   r&   r'   r(   r)   r*   r+   r
   )r   
__future__r   collections.abcr   	functoolsr   typingr   rW   	jax.numpynumpyr5   r   #jax._src.scipy.optimize.line_searchr   r
   dot	PrecisionHIGHESTr<   einsumrB   infr]   r   r   r    <module>rj      s2   C B " " " " " " $ $ $ $ $ $             



             ; ; ; ; ; ;$& $& $& $& $&: $& $& $&N wsw#-"7888
'#*(=
>
>
> 	!p p p p p p pr   