
    -i;X                         S r SSKJrJr  SSKJr  SSKrSSKJ	r	  / SQr
S r SS	 jr SS
 jr  SS jrS rS rS r\R$                  SSSSSS4S jrg)z3Equality-constrained quadratic programming solvers.    )linalgblock_array)copysignN)norm)eqp_kktfactsphere_intersectionsbox_intersectionsbox_sphere_intersectionsinside_box_boundariesmodified_doglegprojected_cgc                 4   [         R                  " U5      u  n[         R                  " U5      u  n[        XR                  /US//SS9n[         R                  " U* U* /5      n[
        R                  " U5      nUR                  U5      n	U	SU n
XXE-    * nX4$ )a  Solve equality-constrained quadratic programming (EQP) problem.

Solve ``min 1/2 x.T H x + x.t c`` subject to ``A x + b = 0``
using direct factorization of the KKT system.

Parameters
----------
H : sparse array, shape (n, n)
    Hessian matrix of the EQP problem.
c : array_like, shape (n,)
    Gradient of the quadratic objective function.
A : sparse array
    Jacobian matrix of the EQP problem.
b : array_like, shape (m,)
    Right-hand side of the constraint equation.

Returns
-------
x : array_like, shape (n,)
    Solution of the KKT problem.
lagrange_multipliers : ndarray, shape (m,)
    Lagrange multipliers of the KKT problem.
Ncsc)format)npshaper   Thstackr   splusolve)HcAbnm
kkt_matrixkkt_veclukkt_solxlagrange_multiplierss               c/var/www/html/venv/lib/python3.13/site-packages/scipy/optimize/_trustregion_constr/qp_subproblem.pyr   r      s    0 
!BA	!BA
 q##hD	25AJii!aR!G
 
Z	 BhhwGA#acN?""    Fc                    [        U5      S:X  a  g[        R                  " U5      (       a3  U(       a"  [        R                  * n[        R                  nOSnSnSnXEU4$ [        R                  " X5      nS[        R                  " X5      -  n[        R                  " X 5      US-  -
  n	X-  SU-  U	-  -
  n
U
S:  a  SnSSU4$ [        R
                  " U
5      nU[        X5      -   nU* SU-  -  nSU	-  U-  n[        XE/5      u  pEU(       a  SnO-US:  d  US:  a  SnSnSnOSn[        SU5      n[        SU5      nXEU4$ )	a  Find the intersection between segment (or line) and spherical constraints.

Find the intersection between the segment (or line) defined by the
parametric  equation ``x(t) = z + t*d`` and the ball
``||x|| <= trust_radius``.

Parameters
----------
z : array_like, shape (n,)
    Initial point.
d : array_like, shape (n,)
    Direction.
trust_radius : float
    Ball radius.
entire_line : bool, optional
    When ``True``, the function returns the intersection between the line
    ``x(t) = z + t*d`` (``t`` can assume any value) and the ball
    ``||x|| <= trust_radius``. When ``False``, the function returns the intersection
    between the segment ``x(t) = z + t*d``, ``0 <= t <= 1``, and the ball.

Returns
-------
ta, tb : float
    The line/segment ``x(t) = z + t*d`` is inside the ball for
    for ``ta <= t <= tb``.
intersect : bool
    When ``True``, there is a intersection between the line/segment
    and the sphere. On the other hand, when ``False``, there is no
    intersection.
r   r   r   F   T      F)
r   r   isinfinfdotsqrtr   sortedmaxmin)zdtrust_radiusentire_linetatb	intersectar   r   discriminantsqrt_discriminantauxs                r#   r   r   A   sM   B Aw!|	xx&&BBBB	y  
qA	BFF1LA
q|Q&A31Q;La	!Y- h(,
,C
1B	ABRHFB	 6R!VIBBI QBQB9r$   c                    [         R                  " U 5      n [         R                  " U5      n[         R                  " U5      n[         R                  " U5      n[        U5      S:X  a  gUS:H  nX   X%   :  R                  5       (       d  X   X5   :  R                  5       (       a  SnSSU4$ [         R                  " U5      nX   n X   nX'   nX7   nX -
  U-  nX0-
  U-  n	[        [         R                  " X5      5      n
[        [         R                  " X5      5      nX::  a  SnOSnU(       d+  US:  d  U
S:  a  SnSn
SnO[        SU
5      n
[        SU5      nXU4$ )a  Find the intersection between segment (or line) and box constraints.

Find the intersection between the segment (or line) defined by the
parametric  equation ``x(t) = z + t*d`` and the rectangular box
``lb <= x <= ub``.

Parameters
----------
z : array_like, shape (n,)
    Initial point.
d : array_like, shape (n,)
    Direction.
lb : array_like, shape (n,)
    Lower bounds to each one of the components of ``x``. Used
    to delimit the rectangular box.
ub : array_like, shape (n, )
    Upper bounds to each one of the components of ``x``. Used
    to delimit the rectangular box.
entire_line : bool, optional
    When ``True``, the function returns the intersection between the line
    ``x(t) = z + t*d`` (``t`` can assume any value) and the rectangular
    box. When ``False``, the function returns the intersection between the segment
    ``x(t) = z + t*d``, ``0 <= t <= 1``, and the rectangular box.

Returns
-------
ta, tb : float
    The line/segment ``x(t) = z + t*d`` is inside the box for
    for ``ta <= t <= tb``.
intersect : bool
    When ``True``, there is a intersection between the line (or segment)
    and the rectangular box. On the other hand, when ``False``, there is no
    intersection.
r   r&   FTr'   )	r   asarrayr   anylogical_notr0   minimumr1   maximum)r2   r3   lbubr5   zero_dr8   
not_zero_dt_lbt_ubr6   r7   s               r#   r	   r	      sO   J 	

1A


1A	BB	BBAw!| 1fF 	
	BJ##%%!)bj*@)E)E)G)G	!Y'J	A	A	B	B DA:DDA:D	RZZ#	$B	RZZ#	$B 
x		6R!VIBB QBQB9r$   c                     [        XX#U5      u  pxn	[        XUU5      u  pn[        R                  " Xz5      n[        R                  " X5      nU	(       a  U(       a  X::  a  SnOSnU(       a  XUS.nXxU	S.nXUUU4$ XU4$ )aU  Find the intersection between segment (or line) and box/sphere constraints.

Find the intersection between the segment (or line) defined by the
parametric  equation ``x(t) = z + t*d``, the rectangular box
``lb <= x <= ub`` and the ball ``||x|| <= trust_radius``.

Parameters
----------
z : array_like, shape (n,)
    Initial point.
d : array_like, shape (n,)
    Direction.
lb : array_like, shape (n,)
    Lower bounds to each one of the components of ``x``. Used
    to delimit the rectangular box.
ub : array_like, shape (n, )
    Upper bounds to each one of the components of ``x``. Used
    to delimit the rectangular box.
trust_radius : float
    Ball radius.
entire_line : bool, optional
    When ``True``, the function returns the intersection between the line
    ``x(t) = z + t*d`` (``t`` can assume any value) and the constraints.
    When ``False``, the function returns the intersection between the segment
    ``x(t) = z + t*d``, ``0 <= t <= 1`` and the constraints.
extra_info : bool, optional
    When ``True``, the function returns ``intersect_sphere`` and ``intersect_box``.

Returns
-------
ta, tb : float
    The line/segment ``x(t) = z + t*d`` is inside the rectangular box and
    inside the ball for ``ta <= t <= tb``.
intersect : bool
    When ``True``, there is a intersection between the line (or segment)
    and both constraints. On the other hand, when ``False``, there is no
    intersection.
sphere_info : dict, optional
    Dictionary ``{ta, tb, intersect}`` containing the interval ``[ta, tb]``
    for which the line intercepts the ball. And a boolean value indicating
    whether the sphere is intersected by the line.
box_info : dict, optional
    Dictionary ``{ta, tb, intersect}`` containing the interval ``[ta, tb]``
    for which the line intercepts the box. And a boolean value indicating
    whether the box is intersected by the line.
TF)r6   r7   r8   )r	   r   r   rB   rA   )r2   r3   rC   rD   r4   r5   
extra_infota_btb_bintersect_bta_stb_sintersect_sr6   r7   r8   sphere_infobox_infos                     r#   r
   r
      s    b 0b0;=D213?3>@D 
D	B	D	B{rx		!KHEy+x77y  r$   c                 X    X:*  R                  5       =(       a    X:*  R                  5       $ )zCheck if lb <= x <= ub.)allr!   rC   rD   s      r#   r   r   1  s    G==?.}}.r$   c                 X    [         R                  " [         R                  " X5      U5      $ )zReturn clipped value of x)r   rA   rB   rU   s      r#   reinforce_box_boundariesrW   6  s    ::bjj',,r$   c                    UR                  U5      * n[        XdU5      (       a  [        U5      U::  a  UnU$ U R                  R                  U5      nU R                  U5      n	[        R                   " X5      * [        R                   " X5      -  U-  n
[        R
                  " U
5      nU
nXj-
  n[        XXEU5      u  pnU(       a  XU-  -   nOUnU
n[        XXEU5      u  pnXU-  -   nUnUn[        XXEU5      u  pnXU-  -   n[        U R                  U5      U-   5      [        U R                  U5      U-   5      :  a  U$ U$ )a  Approximately  minimize ``1/2*|| A x + b ||^2`` inside trust-region.

Approximately solve the problem of minimizing ``1/2*|| A x + b ||^2``
subject to ``||x|| < Delta`` and ``lb <= x <= ub`` using a modification
of the classical dogleg approach.

Parameters
----------
A : LinearOperator (or sparse array or ndarray), shape (m, n)
    Matrix ``A`` in the minimization problem. It should have
    dimension ``(m, n)`` such that ``m < n``.
Y : LinearOperator (or sparse array or ndarray), shape (n, m)
    LinearOperator that apply the projection matrix
    ``Q = A.T inv(A A.T)`` to the vector. The obtained vector
    ``y = Q x`` being the minimum norm solution of ``A y = x``.
b : array_like, shape (m,)
    Vector ``b``in the minimization problem.
trust_radius: float
    Trust radius to be considered. Delimits a sphere boundary
    to the problem.
lb : array_like, shape (n,)
    Lower bounds to each one of the components of ``x``.
    It is expected that ``lb <= 0``, otherwise the algorithm
    may fail. If ``lb[i] = -Inf``, the lower
    bound for the ith component is just ignored.
ub : array_like, shape (n, )
    Upper bounds to each one of the components of ``x``.
    It is expected that ``ub >= 0``, otherwise the algorithm
    may fail. If ``ub[i] = Inf``, the upper bound for the ith
    component is just ignored.

Returns
-------
x : array_like, shape (n,)
    Solution to the problem.

Notes
-----
Based on implementations described in pp. 885-886 from [1]_.

References
----------
.. [1] Byrd, Richard H., Mary E. Hribar, and Jorge Nocedal.
       "An interior point algorithm for large-scale nonlinear
       programming." SIAM Journal on Optimization 9.4 (1999): 877-900.
)r-   r   r   r   r   
zeros_liker
   )r   Yr   r4   rC   rD   newton_pointr!   gA_gcauchy_pointorigin_pointr2   p_alphar8   x1x2s                      r#   r   r   ;  sL   ` EE!H9L\r22- 	

A %%(CFF1L=266##33a7L==.L 	A#A213?AAiq[ .qR/;=!q[ 	AA*1+79KAa	
1WB AEE"IMT!%%)a-00		r$   c           
      J   Sn[         R                  " U5      u  n[         R                  " U5      u  nUR                  U* 5      nUR                  U R                  U5      U-   5      nUR                  U5      nU* nU(       a  U/nU R                  U5      n[        U5      S-  nU[        U5      -
  nUS:  a  [	        S5      eUU:  a'  SSSS.nU(       a  WR                  U5        UUS'   UU4$ Uc0  [        [        S[         R                  " U5      -  S	U-  5      U5      nUc&  [         R                  " U[         R                  * 5      nUc%  [         R                  " U[         R                  5      nU	c  X-
  n	[        XU-
  5      n	U
c  X-
  n
S
nSnSn[         R                  " U5      nSn[        U	5       GH  nUU:  a  Sn  GOUS-  nUR                  U5      nUS::  aY  [         R                  " U5      (       a  [	        S5      e[        UUXgUSS9u  nn n!U!(       a  UU U-  -   n[        XU5      nSnSn  GO<UU-  n UU U-  -   n"[         R                   R                  U"5      U:  a9  [        UU U-  XgU5      u  nn#n!U!(       a  UU#U -  U-  -   n[        XU5      nSnSn  O[#        U"Xg5      (       a  SnOUS-  nUS:  a5  [        UU U-  XgU5      u  nn#n!U!(       a  UU#U -  U-  -   n[        UXg5      nSnUU
:  a    OwU(       a  WR                  U"5        UU U-  -   n$UR                  U$5      n%[        U%5      S-  n&U&U-  n'U%* U'U-  -   nU"nU%nU%n[        U5      S-  nU R                  U5      nGM     [#        XU5      (       d  UnSnUUUS.nU(       a  WUS'   UU4$ )a  Solve EQP problem with projected CG method.

Solve equality-constrained quadratic programming problem
``min 1/2 x.T H x + x.t c``  subject to ``A x + b = 0`` and,
possibly, to trust region constraints ``||x|| < trust_radius``
and box constraints ``lb <= x <= ub``.

Parameters
----------
H : LinearOperator (or sparse array or ndarray), shape (n, n)
    Operator for computing ``H v``.
c : array_like, shape (n,)
    Gradient of the quadratic objective function.
Z : LinearOperator (or sparse array or ndarray), shape (n, n)
    Operator for projecting ``x`` into the null space of A.
Y : LinearOperator,  sparse array, ndarray, shape (n, m)
    Operator that, for a given a vector ``b``, compute smallest
    norm solution of ``A x + b = 0``.
b : array_like, shape (m,)
    Right-hand side of the constraint equation.
trust_radius : float, optional
    Trust radius to be considered. By default, uses ``trust_radius=inf``,
    which means no trust radius at all.
lb : array_like, shape (n,), optional
    Lower bounds to each one of the components of ``x``.
    If ``lb[i] = -Inf`` the lower bound for the i-th
    component is just ignored (default).
ub : array_like, shape (n, ), optional
    Upper bounds to each one of the components of ``x``.
    If ``ub[i] = Inf`` the upper bound for the i-th
    component is just ignored (default).
tol : float, optional
    Tolerance used to interrupt the algorithm.
max_iter : int, optional
    Maximum algorithm iterations. Where ``max_inter <= n-m``.
    By default, uses ``max_iter = n-m``.
max_infeasible_iter : int, optional
    Maximum infeasible (regarding box constraints) iterations the
    algorithm is allowed to take.
    By default, uses ``max_infeasible_iter = n-m``.
return_all : bool, optional
    When ``true``, return the list of all vectors through the iterations.

Returns
-------
x : array_like, shape (n,)
    Solution of the EQP problem.
info : Dict
    Dictionary containing the following:

        - niter : Number of iterations.
        - stop_cond : Reason for algorithm termination:
            1. Iteration limit was reached;
            2. Reached the trust-region boundary;
            3. Negative curvature detected;
            4. Tolerance was satisfied.
        - allvecs : List containing all intermediary vectors (optional).
        - hits_boundary : True if the proposed step is on the boundary
          of the trust region.

Notes
-----
Implementation of Algorithm 6.2 on [1]_.

In the absence of spherical and box constraints, for sufficient
iterations, the method returns a truly optimal result.
In the presence of those constraints, the value returned is only
a inexpensive approximation of the optimal value.

References
----------
.. [1] Gould, Nicholas IM, Mary E. Hribar, and Jorge Nocedal.
       "On the solution of equality constrained quadratic
        programming problems arising in optimization."
        SIAM Journal on Scientific Computing 23.4 (2001): 1376-1395.
g}:r(   r   z.Trust region problem does not have a solution.T)niter	stop_condhits_boundaryallvecsg{Gz?g?Fr'   r)   z9Negative curvature not allowed for unrestricted problems.)r5      )r   r   r-   r   
ValueErrorappendr0   r1   r.   fullr,   rY   ranger+   r
   rW   r   r   )(r   r   ZrZ   r   r4   rC   rD   tolmax_itermax_infeasible_iter
return_allCLOSE_TO_ZEROr   r   r!   rr\   r`   ri   H_prt_gtr_distanceinforh   rg   counterlast_feasible_xkipt_H_pra   rb   r8   x_nextthetar_nextg_next	rt_g_nextbetas(                                           r#   r   r     s   ` M	!BA	!BA 	
qb	A	aeeAhlA	aA	
A #
%%(C7A:D a(KQIJJ 
}	$TBNN1%DO$w {#dRWWT]*C$J7G	zWWQ 	zWWQ38qS!H"cMIGmmA&O	A8_#:I	QQ;xx%%  "> ? ? '?q",D'B#5) E!GA -QB7	 $ vU1W 99>>&!\1":1eAgr;G#IAui eA% )3AI M !00GqLGQ;":1eAgr;G#IAui"#eEk!m"3 #;?;=#C((NN6" U3YvL!O	4HtAvAwzeeAhi l !++Y*,D!Yd7Nr$   )F)FF)__doc__scipy.sparser   r   mathr   numpyr   numpy.linalgr   __all__r   r   r	   r
   r   rW   r   r,   r    r$   r#   <module>r      sw    9 ,   *#\ &+Sn #(Rl */(-B!J/
-
]@ .0VVTtD!br$   