
    ukie                       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 dd
lmZ ddlmZ ddlmZ ddlmZ  G d de      Z eej2                  ej4                  j6                        Z eej                   ej4                  j6                        Zde
j<                  ddf	 	 	 	 	 	 	 	 	 	 	 ddZy)z<The Broyden-Fletcher-Goldfarb-Shanno minimization algorithm.    )annotations)Callable)partial)
NamedTupleN)api)lax)numpy)einsum)linalg)line_search)Arrayc                      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<   y)_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 | Array	convergedfailedzint | Arrayknfevngevnhevr   x_kf_kg_kH_kold_old_fvalstatusline_search_statusN)__name__
__module____qualname____doc____annotations__     W/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/scipy/optimize/bfgs.pyr   r   !   sR    . 
.****
!!r#   r   )	precisiongh㈵>
   c                    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                  }|	j%                  |      }	|	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.
     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   )statemaxiters    r$   cond_funzminimize_bfgs.<locals>.cond_fun~   s>    OOEOO,ooell+,ww " #r#   c           	     p   t        | j                  | j                         }t        | j                  || 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                  || j0                  dz   ||||| j
                        } | S )N)old_fvalr   gfkr3   )r   r   r   r   r)   zij,jk,lkr+   r-   )r   r   r   r   r   r   r   )_dotr   r   r   r   r   r   _replacer   r   r   r   a_kr0   
reciprocalnpnewaxiseyer*   _einsumwhereisfinite
jnp_linalgnormr   )r2   p_kline_search_resultss_kx_kp1f_kp1g_kp1y_krho_ksy_kwH_kp1r   dfungtolline_search_maxiterrC   s                r$   body_funzminimize_bfgs.<locals>.body_fun   s   		599%
%C%		''II# NNZZ-222ZZ-222")).55	  E 
!
!C
'CIIOE##E##E
%))
CNN4S>*Eq"**}BJJM 22D%4AZEIIq1s1bjj=))C

A,>>?EIIcll5)5%))<E4047INN
''A+YY  E Lr#   )r   )r<   sizeshaper0   r>   r*   r   value_and_gradr   rB   rC   r   
while_loopr@   r   r   r   r   r9   )rP   x0r3   rC   rQ   rR   	initial_Hf_0g_0r2   r4   rS   r   rO   s   ` ````       @r$   minimize_bfgsr]   L   s>   6 _ggbkCGhhqk!ggarxx()$S$R((#s
.5	



-11% #
( (T ..8U
3%99oo	ii
''W


))ll%***& ...
'%	,r#   )rP   r   rY   r   r3   z
int | NonerQ   floatrR   intreturnr   ) r    
__future__r   collections.abcr   	functoolsr   typingr   r	   r<   jax._srcr   r   r0   jax._src.numpyr
   
jnp_einsumr   rB   #jax._src.scipy.optimize.line_searchr   jax._src.typingr   r   dot	PrecisionHIGHESTr8   r?   infr]   r"   r#   r$   <module>rn      s    C " $      ! / / ; !$": $"N sww#--"7"78
*##s}}/D/D
E 	!p	pp p
 p p pr#   