Ë
    âQ(h@X  ã                   ó¦   — d Z ddlmZmZmZ ddlmZ ddlZddl	m
Z
 g d¢Zd„ Z	 dd	„Z	 dd
„Z	 	 dd„Zd„ Zd„ Zd„ Zej&                  ddddddfd„Zy)z3Equality-constrained quadratic programming solvers.é    )ÚlinalgÚbmatÚ
csc_matrix)ÚcopysignN)Únorm)Úeqp_kktfactÚsphere_intersectionsÚbox_intersectionsÚbox_sphere_intersectionsÚinside_box_boundariesÚmodified_doglegÚprojected_cgc                 óH  — t        j                  |«      \  }t        j                  |«      \  }t        t        | |j                  g|dgg«      «      }t        j
                  | | g«      }t        j                  |«      }|j                  |«      }	|	d| }
|	|||z     }|
|fS )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 matrix, shape (n, n)
        Hessian matrix of the EQP problem.
    c : array_like, shape (n,)
        Gradient of the quadratic objective function.
    A : sparse matrix
        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)	ÚnpÚshaper   r   ÚTÚhstackr   ÚspluÚsolve)ÚHÚcÚAÚbÚnÚmÚ
kkt_matrixÚkkt_vecÚluÚkkt_solÚxÚlagrange_multiplierss               ún/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/scipy/optimize/_trustregion_constr/qp_subproblem.pyr   r      s¡   € ô0 
�‰�!‹�B€AÜ	�‰�!‹�B€Aô
 œD 1 a§c¡c (¨Q°¨IÐ!6Ó7Ó8€Jä�i‰i˜!˜˜a˜R˜Ó!€Gô
 
�‰�ZÓ	 €BØ�h‰h�wÓ€GØ��ˆ€AØ# A a¨¡c˜N˜?ÐàÐ"Ð"Ð"ó    Fc                 óf  — t        |«      dk(  ryt        j                  |«      r/|r"t        j                   }t        j                  }nd}d}d}|||fS t        j                  ||«      }dt        j                  | |«      z  }t        j                  | | «      |dz  z
  }	||z  d|z  |	z  z
  }
|
dk  rd}dd|fS t        j
                  |
«      }|t        ||«      z   }| d|z  z  }d|	z  |z  }t        ||g«      \  }}|rd}n+|dk  s|dkD  rd}d}d}nd}t        d|«      }t        d|«      }|||fS )	aH  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   s\  € ôB ˆAƒw�!‚|Øä	‡x�x�ÔÙÜ—&‘&�ˆBÜ—‘‰BàˆBØˆBØˆ	Ø�2�yÐ Ð ä
�‰ˆq�!‹€AØ	ŒB�F‰F�1�a‹LÑ€AÜ
�‰ˆq�!‹�| Q‘Ñ&€AØ�Q‘3˜˜1™˜Q™‘;€LØ�aÒØˆ	Ø�!�YˆÐÜŸ™ Ó-Ðð ŒhÐ(¨!Ó,Ñ
,€CØ
ˆ��1‘‰€BØ	ˆA‰�‰€BÜ�R˜�HÓ�F€BˆáØ‰	ð �Š6�R˜!’VØˆIØˆBØ‰BàˆIô �Q˜“ˆBÜ�Q˜“ˆBàˆr�9ÐÐr#   c                 ó¦  — t        j                  | «      } t        j                  |«      }t        j                  |«      }t        j                  |«      }t        |«      dk(  ry|dk(  }| |   ||   k  j                  «       s| |   ||   kD  j                  «       rd}dd|fS t        j                  |«      }| |   } ||   }||   }||   }|| z
  |z  }|| z
  |z  }	t        t        j                  ||	«      «      }
t        t        j                  ||	«      «      }|
|k  rd}nd}|s)|dk  s|
dkD  rd}d}
d}nt        d|
«      }
t        d|«      }|
||fS )a5  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_notr/   Úminimumr0   Úmaximum)r1   r2   ÚlbÚubr4   Úzero_dr7   Ú
not_zero_dÚt_lbÚt_ubr5   r6   s               r"   r
   r
   —   se  € ôJ 	�
‰
�1‹€AÜ
�
‰
�1‹€AÜ	�‰�B‹€BÜ	�‰�B‹€BäˆAƒw�!‚|Øð �1‰f€Fð 	
ˆ&‰	�B�v‘JÑ×#Ñ#Ô%¨!¨F©)°b¸±jÑ*@×)EÑ)EÔ)GØˆ	Ø�!�YˆÐä—‘ Ó'€JØ	ˆ*‰€AØ	ˆ*‰€AØ	ˆJ‰€BØ	ˆJ‰€Bð ˆq‰D�A‰:€DØˆq‰D�A‰:€Dä	ŒR�Z‰Z˜˜dÓ#Ó	$€BÜ	ŒR�Z‰Z˜˜dÓ#Ó	$€Bð 
ˆR‚xØ‰	àˆ	áØ�Š6�R˜!’VØˆIØˆBØ‰Bô �Q˜“ˆBÜ�Q˜“ˆBàˆr�9ÐÐr#   c                 óô   — t        | ||||«      \  }}}	t        | |||«      \  }
}}t        j                  ||
«      }t        j                  ||«      }|	r
|r||k  rd}nd}|r|
||dœ}|||	dœ}|||||fS |||fS )aý  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)r5   r6   r7   )r
   r	   r   rA   r@   )r1   r2   rB   rC   r3   r4   Ú
extra_infoÚta_bÚtb_bÚintersect_bÚta_sÚtb_sÚintersect_sr5   r6   r7   Úsphere_infoÚbox_infos                     r"   r   r   ì   s­   € ôb 0°°1°b¸"Ø0;ó=Ñ€Dˆ$�ä2°1°aØ3?Ø3>ó@Ñ€Dˆ$�ô 
�‰�D˜$Ó	€BÜ	�‰�D˜$Ó	€BÙ‘{ r¨R¢xØ‰	àˆ	áØ!¨¸KÑHˆØ d¸ÑEˆØ�2�y +¨xÐ7Ð7à�2�yÐ Ð r#   c                 óR   — || k  j                  «       xr | |k  j                  «       S )zCheck if lb <= x <= ub.)Úall©r    rB   rC   s      r"   r   r   1  s!   € à�!‰G�=‰=‹?Ò.  R¡Ÿ}™}›Ð.r#   c                 óV   — t        j                  t        j                  | |«      |«      S )zReturn clipped value of x)r   r@   rA   rT   s      r"   Úreinforce_box_boundariesrV   6  s   € ä�:‰:”b—j‘j  BÓ'¨Ó,Ð,r#   c                 óŒ  — |j                  |«       }t        |||«      rt        |«      |k  r|}|S | j                  j                  |«      }| j                  |«      }	t	        j                   ||«       t	        j                   |	|	«      z  |z  }
t	        j
                  |
«      }|
}||
z
  }t        |||||«      \  }}}|r	|||z  z   }n|}|
}t        |||||«      \  }}}|||z  z   }|}|}t        |||||«      \  }}}|||z  z   }t        | j                  |«      |z   «      t        | j                  |«      |z   «      k  r|S |S )aA  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 matrix 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 matrix 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   r3   rB   rC   Únewton_pointr    ÚgÚA_gÚcauchy_pointÚorigin_pointr1   ÚpÚ_Úalphar7   Úx1Úx2s                      r"   r   r   ;  si  € ð` —E‘E˜!“H�9€Lä˜\¨2¨rÔ2Ü�Ó Ò-ØˆØˆð 	
�‰�‰�‹
€Að �%‰%�‹(€CÜ—F‘F˜1˜a“L�=¤2§6¡6¨#¨sÓ#3Ñ3°aÑ7€Lä—=‘= Ó.€Lð 	€AØ�|Ñ#€AÜ2°1°a¸¸RØ3?óAÑ€A€uˆiáØ��q‘‰[‰ð ˆØˆÜ.¨q°!°R¸Ø/;ó=‰ˆˆ5�!à��q‘‰[ˆð 	€AØ€AÜ*¨1¨a°°RØ+7ó9�K€A€uˆaà	
ˆU�1‰W‰€Bô ˆA�E‰E�"‹I˜‰MÓœT !§%¡%¨£)¨a¡-Ó0Ò0Øˆ	àˆ	r#   c           	      óØ  — d}t        j                  |«      \  }t        j                  |«      \  }|j                  | «      }|j                  | j                  |«      |z   «      }|j                  |«      }| }|r|g}| j                  |«      }t        |«      dz  }|t        |«      z
  }|dk  rt	        d«      ‚||k  r"ddddœ}|rj                  |«       ||d<   ||fS |€/t        t        dt        j                  |«      z  d	|z  «      |«      }|€%t        j                  |t         j                   «      }|€$t        j                  |t         j                  «      }|	€||z
  }	t        |	||z
  «      }	|
€||z
  }
d
}d}d}t        j                  |«      }d}t        |	«      D �]   }||k  rd} �n–|dz  }|j                  |«      }|dk  rSt        j                  |«      rt	        d«      ‚t        |||||d¬«      \  }} }!|!r|| |z  z   }t        |||«      }d}d} �n(||z  } || |z  z   }"t         j                   j                  |"«      |k\  r6t        || |z  |||«      \  }}#}!|!r||#| z  |z  z   }t        |||«      }d}d} nÃt#        |"||«      rd}n|dz  }|dkD  r2t        || |z  |||«      \  }}#}!|!r||#| z  |z  z   }t        |||«      }d}||
kD  r np|rj                  |"«       || |z  z   }$|j                  |$«      }%t        |%«      dz  }&|&|z  }'|% |'|z  z   }|"}|%}|%}t        |«      dz  }| j                  |«      }�Œ£ t#        |||«      s|}d}|||dœ}|r|d<   ||fS )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 matrix 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 matrix or ndarray), shape (n, n)
        Operator for projecting ``x`` into the null space of A.
    Y : LinearOperator,  sparse matrix, 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{®Gáz„?gš™™™™™¹?Fr&   r(   z9Negative curvature not allowed for unrestricted problems.)r4   é   )r   r   r,   r   Ú
ValueErrorÚappendr/   r0   r-   Úfullr+   rX   Úranger*   r   rV   r   r   )(r   r   ÚZrY   r   r3   rB   rC   ÚtolÚmax_iterÚmax_infeasible_iterÚ
return_allÚCLOSE_TO_ZEROr   r   r    Úrr[   r_   rh   ÚH_pÚrt_gÚtr_distanceÚinforg   rf   ÚcounterÚlast_feasible_xÚkÚiÚpt_H_pr`   ra   r7   Úx_nextÚthetaÚr_nextÚg_nextÚ	rt_g_nextÚbetas(                                           r"   r   r   ›  s  € ð` €Mä	�‰�!‹�B€AÜ	�‰�!‹�B€Að 	
�‰ˆqˆb‹	€AØ	�‰ˆa�e‰e�A‹h˜‰lÓ€AØ	�‰ˆa‹€AØ	
ˆ€Añ Ø�#ˆà
�%‰%�‹(€CÜ�‹7�A‰:€Dð ¤ a£Ñ(€KØ�Q‚ÜÐIÓJÐJð 
�}Ò	$Ø¨¸TÑBˆÙØ�N‰N˜1ÔØ%ˆD�‰OØ�$ˆwˆð €{Ü”#�dœRŸW™W T›]Ñ*¨C°$©JÓ7¸ÓGˆà	€zÜ�W‰W�QœŸ™˜Ó ˆØ	€zÜ�W‰W�QœŸ™ÓˆàÐØ�Q‘3ˆÜ�8˜Q˜q™SÓ!€HàÐ"Ø ™cÐà€MØ€IØ€GÜ—m‘m AÓ&€OØ	€AÜ�8‹_ó Tˆà�#Š:ØˆIÚØ	ˆQ‰ˆà—‘˜“ˆà�QŠ;Ü�x‰x˜Ô%Ü ð ">ó ?ð ?ô '?Ø�q˜"˜b ,¸Dô'BÑ#��5˜)ñ Ø˜E !™G™�Aô -¨Q°°BÓ7�à�	Ø $�Úð �v‘ˆØ�U˜1‘W‘ˆô �9‰9�>‰>˜&Ó! \Ò1ä":¸1¸eÀA¹gÀrÈ2Ø;Gó#IÑˆAˆu�iñ Ø˜˜e™ A™Ñ%�ô )¨¨B°Ó3ˆAàˆIØ ˆMÙô ! ¨¨RÔ0Ø‰Gà�q‰LˆGà�QŠ;Ü":¸1¸eÀA¹gÀrÈ2Ø;Gó#IÑˆAˆu�iáØ"# e¨E¡k°!¡mÑ"3�ô #;¸?Ø;=¸ró#C�à�àÐ(Ò(ÙáØ�N‰N˜6Ô"ð �U˜3‘Y‘ˆà—‘�v“ˆä˜“L !‘Oˆ	Ø˜4ÑˆØˆH�t˜A‘vÑˆàˆØˆØˆÜ�A‹w˜‰zˆØ�e‰e�A‹hŠðiTôl !  B¨Ô+ØˆØˆØ YØ*ñ,€DáØ!ˆˆY‰Øˆdˆ7€Nr#   )F)FF)Ú__doc__Úscipy.sparser   r   r   Úmathr   Únumpyr   Únumpy.linalgr   Ú__all__r   r	   r
   r   r   rV   r   r+   r   © r#   r"   ú<module>r‹      sz   ðÙ 9ç 3Ñ 3Ý Û Ý ò€ò*#ð\ &+óSðn #(óRðl */Ø(-óB!òJ/ò
-ò
]ð@ .0¯V©VØ˜T tØ°DØ!ôbr#   