ó
    Eñi‘/  ã                   ó¢   • S r SSKrSSKJrJr  SSKJrJrJ	r	  SSK
Jr  SSKJr  SSKJrJrJrJrJrJrJrJrJrJrJrJr  S	 rS
 rS r SS jrg)a	  
Dogleg algorithm with rectangular trust regions for least-squares minimization.

The description of the algorithm can be found in [Voglis]_. The algorithm does
trust-region iterations, but the shape of trust regions is rectangular as
opposed to conventional elliptical. The intersection of a trust region and
an initial feasible region is again some rectangle. Thus, on each iteration a
bound-constrained quadratic optimization problem is solved.

A quadratic problem is solved by well-known dogleg approach, where the
function is minimized along piecewise-linear "dogleg" path [NumOpt]_,
Chapter 4. If Jacobian is not rank-deficient then the function is decreasing
along this path, and optimization amounts to simply following along this
path as long as a point stays within the bounds. A constrained Cauchy step
(along the anti-gradient) is considered for safety in rank deficient cases,
in this situations the convergence might be slow.

If during iterations some variable hit the initial bound and the component
of anti-gradient points outside the feasible region, then a next dogleg step
won't make any progress. At this state such variables satisfy first-order
optimality conditions and they are excluded before computing a next dogleg
step.

Gauss-Newton step can be computed exactly by `numpy.linalg.lstsq` (for dense
Jacobian matrices) or by iterative procedure `scipy.sparse.linalg.lsmr` (for
dense and sparse matrices, or Jacobian being LinearOperator). The second
option allows to solve very large problems (up to couple of millions of
residuals on a regular PC), provided the Jacobian matrix is sufficiently
sparse. But note that dogbox is not very good for solving problems with
large number of constraints, because of variables exclusion-inclusion on each
iteration (a required number of function evaluations might be high or accuracy
of a solution will be poor), thus its large-scale usage is probably limited
to unconstrained problems.

References
----------
.. [Voglis] C. Voglis and I. E. Lagaris, "A Rectangular Trust Region Dogleg
            Approach for Unconstrained and Bound Constrained Nonlinear
            Optimization", WSEAS International Conference on Applied
            Mathematics, Corfu, Greece, 2004.
.. [NumOpt] J. Nocedal and S. J. Wright, "Numerical optimization, 2nd edition".
é    N)ÚlstsqÚnorm)ÚLinearOperatorÚaslinearoperatorÚlsmr)ÚOptimizeResult)Ú_call_callback_maybe_halté   )Ústep_size_to_boundÚ	in_boundsÚupdate_tr_radiusÚevaluate_quadraticÚbuild_quadratic_1dÚminimize_quadratic_1dÚcompute_gradÚcompute_jac_scaleÚcheck_terminationÚscale_for_robust_loss_functionÚprint_header_nonlinearÚprint_iteration_nonlinearc                 ód   ^ ^^• T R                   u  p4U UU4S jnU UU4S jn[        X44XV[        S9$ )z Compute LinearOperator to use in LSMR by dogbox algorithm.

`active_set` mask is used to excluded active variables from computations
of matrix-vector products.
c                 ór   >• U R                  5       R                  5       nSUT'   TR                  U T-  5      $ ©Nr   )ÚravelÚcopyÚmatvec)ÚxÚx_freeÚJopÚ
active_setÚds     €€€ÚW/home/mande/repo/quber/.venv/lib/python3.13/site-packages/scipy/optimize/_lsq/dogbox.pyr   Úlsmr_operator.<locals>.matvecB   s2   ø€ Ø—‘“—‘Ó!ˆØˆˆzÑØ�z‰z˜!˜a™%Ó Ð ó    c                 ó:   >• TTR                  U 5      -  nSUT'   U$ r   )Úrmatvec)r   Úrr   r    r!   s     €€€r"   r&   Úlsmr_operator.<locals>.rmatvecG   s#   ø€ Ø�—‘˜A“ÑˆØˆˆ*‰Øˆr$   )r   r&   Údtype)Úshaper   Úfloat)r   r!   r    ÚmÚnr   r&   s   ```    r"   Úlsmr_operatorr.   :   s-   ú€ ð �9‰9�D€A÷!÷
ô
 ˜1˜&¨ÌÑNÐNr$   c                 ó(  • X -
  nX0-
  n[         R                  " XA* 5      n[         R                  " XQ5      n[         R                  " Xd5      n[         R                  " Xu5      n	[         R                  " Xa* 5      n
[         R                  " Xq5      nXgX‰X«4$ )aã  Find intersection of trust-region bounds and initial bounds.

Returns
-------
lb_total, ub_total : ndarray with shape of x
    Lower and upper bounds of the intersection region.
orig_l, orig_u : ndarray of bool with shape of x
    True means that an original bound is taken as a corresponding bound
    in the intersection region.
tr_l, tr_u : ndarray of bool with shape of x
    True means that a trust-region bound is taken as a corresponding bound
    in the intersection region.
)ÚnpÚmaximumÚminimumÚequal)r   Ú	tr_boundsÚlbÚubÚlb_centeredÚub_centeredÚlb_totalÚub_totalÚorig_lÚorig_uÚtr_lÚtr_us               r"   Úfind_intersectionr?   O   sw   € ð ‘&€KØ‘&€Kä�zŠz˜+ zÓ2€HÜ�zŠz˜+Ó1€Hä�XŠX�hÓ,€FÜ�XŠX�hÓ,€Fä�8Š8�H˜jÓ)€DÜ�8Š8�HÓ(€Dà˜v¨tÐ9Ð9r$   c                 ó¦  • [        XXg5      u  p‰p«pÍ[        R                  " U [        S9n[	        XU	5      (       a  XS4$ [        [        R                  " U 5      U* X‰5      u  nn[        X4SU5      S   * U-  nUU-
  n[        UUX‰5      u  nnSUUS:  U
-  '   SUUS:„  U-  '   [        R                  " US:  U-  US:„  U-  -  5      nUUU-  -   UU4$ )aÆ  Find dogleg step in a rectangular region.

Returns
-------
step : ndarray, shape (n,)
    Computed dogleg step.
bound_hits : ndarray of int, shape (n,)
    Each component shows whether a corresponding variable hits the
    initial bound after the step is taken:
        *  0 - a variable doesn't hit the bound.
        * -1 - lower bound is hit.
        *  1 - upper bound is hit.
tr_hit : bool
    Whether the step hit the boundary of the trust-region.
©r)   Fr   éÿÿÿÿr
   )r?   r0   Ú
zeros_likeÚintr   r   r   Úany)r   Únewton_stepÚgÚaÚbr4   r5   r6   r9   r:   r;   r<   r=   r>   Ú
bound_hitsÚ	to_boundsÚ_Úcauchy_stepÚ	step_diffÚ	step_sizeÚhitsÚtr_hits                         r"   Údogleg_steprR   l   s   € ô  6GØ	�bó6Ñ2€H˜¨ô —’˜q¬Ñ,€Jä�¨×1Ñ1Ø¨Ð-Ð-ä%¤b§m¢m°AÓ&6¸¸¸HÓO�L€Iˆqô )¨¨q°)Ó<¸QÑ?Ð?À!ÑC€Kà˜kÑ)€IÜ(¨°iØ)1ó=�O€Iˆtà&(€J��q‘˜FÑ"Ñ#Ø&'€J��q‘˜FÑ"Ñ#Ü�VŠV�T˜A‘X Ñ%¨°©°TÑ(9Ñ9Ó:€Fà˜ YÑ.Ñ.°
¸FÐBÐBr$   c                 óò  • UnUR                  5       nSnUnSnUb5  U" U5      nS[        R                  " US   5      -  n[        UUU5      u  nnOS[        R                  " UU5      -  n[        UU5      n[        U[        5      =(       a    US:H  nU(       a  [        U5      u  nnOUSU-  nn[        UU-  [        R                  S9nUS:X  a  Sn[        R                  " U[        S9nSU[        R                  " X%5      '   SU[        R                  " X&5      '   Un[        R                  " U5      nU
c  UR                  S	-  n
S n Sn!S n"S n#US
:X  a
  [!        5          UU-  S:  n$U$) n%UU%   n&UR                  5       n'SUU$'   [        U[        R                  S9n(U(U	:  a  Sn US
:X  a  [#        U!UUU#U"U(5        U c  UU
:X  a  GO›UU%   n)UU%   n*UU%   n+UU%   n,US:X  a*  US S 2U%4   n-[%        U-U* SS9S   n.['        U-U&U&* 5      u  n/n0OHUS:X  aB  [)        U5      n1[+        U1UU$5      n2[-        U2U40 UD6S   U%   * n.U.U,-  n.['        U1UU* 5      u  n/n0Sn#U#S::  GaL  UU
:  GaE  UU,-  n3[/        U)W.U&W/W0U3U*U+5      u  n4n5n6UR1                  S5        U4UU%'   US:X  a  [3        W-U&U45      * n7OUS:X  a  [3        W1UU5      * n7[        R4                  " UU-   XV5      n8U " U85      n9US-  n[        UU-  [        R                  S9n:[        R6                  " [        R8                  " U95      5      (       d  SU:-  nMà  Ub  U" U9SS9n;OS[        R                  " U9U95      -  n;UU;-
  n#[;        UU#W7U:U65      u  nn<[        U5      n"[=        U#UU"[        U5      U<Xx5      n U b  OU#S::  a	  UU
:  a  GME  U#S:”  a€  W5UU%'   W8nUS:H  n=UU=   UU='   US:H  n=UU=   UU='   W9nUR                  5       nW;nU" U5      nUS-  nUb  U" U5      n[        UUU5      u  nn[        UU5      nU(       a  [        UU5      u  nnOSn"Sn#U!S-  n!Ub%  [?        UUU!US9n>W;U>S'   [A        UU>5      (       a  Sn OGM  U c  Sn [?        UUUUU'U(UUUU S9
$ )Nr
   g      à?r   Újac)Úordg      ð?rA   rB   éd   é   TÚexact)Úrcondr   g      ð¿g        g      Ð?)Ú	cost_only)r   ÚfunÚnitÚnfevÚcostéþÿÿÿ)
r   r^   r[   rT   ÚgradÚ
optimalityÚactive_maskr]   ÚnjevÚstatus)!r   r0   Úsumr   Údotr   Ú
isinstanceÚstrr   r   ÚinfrC   rD   r3   Ú
empty_likeÚsizer   r   r   r   r   r.   r   rR   Úfillr   ÚclipÚallÚisfiniter   r   r   r	   )?r[   rT   Úx0Úf0ÚJ0r5   r6   ÚftolÚxtolÚgtolÚmax_nfevÚx_scaleÚloss_functionÚ	tr_solverÚ
tr_optionsÚverboseÚcallbackÚfÚf_truer]   ÚJrc   Úrhor^   rG   Ú	jac_scaleÚscaleÚ	scale_invÚDeltaÚon_boundr   ÚstepÚtermination_statusÚ	iterationÚ	step_normÚactual_reductionr    Úfree_setÚg_freeÚg_fullÚg_normr   Úlb_freeÚub_freeÚ
scale_freeÚJ_freerF   rH   rI   r   Úlsmr_opr4   Ú	step_freeÚon_bound_freerQ   Úpredicted_reductionÚx_newÚf_newÚstep_h_normÚcost_newÚratioÚmaskÚintermediate_results?                                                                  r"   Údogboxrž   —   s_  € à
€AØ�V‰V‹X€FØ€Dà
€AØ€DàÑ Ù˜AÓˆØ”R—V’V˜C ™F“^Ñ#ˆÜ-¨a°°CÓ8‰ˆ‰1à”R—V’V˜A˜q“\Ñ!ˆä�Q˜Ó€Aä˜7¤CÓ(×=¨W¸Ñ-=€IÞÜ,¨QÓ/Ñˆ‰yà" A¨¡Kˆyˆä��i‘¤R§V¡VÑ,€EØ�ƒzØˆä�}Š}˜R¤sÑ+€HØ!#€HŒR�XŠX�bÓÑØ!"€HŒR�XŠX�bÓÑà
€AÜ�=Š=˜Ó€DàÑØ—7‘7˜S‘=ˆàÐØ€IØ€IØÐà�!ƒ|ÜÔ à
Ø ‘\ AÑ%ˆ
Ø�;ˆà�8‘ˆØ—‘“ˆØˆˆ*‰ä�aœRŸV™VÑ$ˆØ�D‹=Ø!"Ðà�a‹<Ü% i°°tÐ=MØ&/°ô9ð Ñ)¨T°XÓ-=Ùà�8‘ˆØ�X‘,ˆØ�X‘,ˆØ˜8‘_ˆ
ð ˜ÓØ’q˜(�{‘^ˆFÜ ¨¨°"Ñ5°aÑ8ˆKô & f¨f°v°gÓ>‰DˆA‰qØ˜&Ó Ü" 1Ó%ˆCô $ C¨°
Ó;ˆGÜ ¨Ñ9¨jÑ9¸!Ñ<¸XÑFÐFˆKØ˜:Ñ%ˆKô & c¨1¨q¨bÓ1‰DˆAˆqàÐØ !Ô#¨¨x¬Ø 
Ñ*ˆIä/:Ø˜ V¨Q°°9¸gÀwó0PÑ,ˆI�} fð �I‰I�cŒNØ&ˆD�‰Nà˜GÓ#Ü'9¸&À&Ø:Có(Eð 'EÑ#à˜fÓ$Ü'9¸#¸qÀ$Ó'GÐ&GÐ#ô —G’G˜A ™H bÓ-ˆEá˜“JˆEØ�A‰IˆDä˜t iÑ/´R·V±VÑ<ˆKä—6’6œ"Ÿ+š+ eÓ,×-Ñ-Ø˜{Ñ*�Ùð Ñ(Ù(¨¸$Ñ?‘à¤§¢¨¨uÓ!5Ñ5�Ø# h™Ðä+ØÐ'Ð)<Ø˜Vó‰LˆE�5ô
 ˜T›
ˆIÜ!2Ø  $¨	´4¸³7¸EÀ4ó"OÐð "Ñ-ØðY  !Ó#¨¨x®ð\ ˜aÓØ!.ˆH�XÑàˆAà˜r‘>ˆDØ˜‘hˆAˆd‰GØ˜q‘=ˆDØ˜‘hˆAˆd‰GàˆAØ—V‘V“XˆFàˆDá�A“ˆAØ�A‰IˆDàÑ(Ù# AÓ&�Ü5°a¸¸CÓ@‘��1ä˜Q Ó"ˆAæÜ#4°Q¸	Ó#BÑ ��yøàˆIØ Ðà�Q‰ˆ	ð ÑÜ"0Ø˜ 	°ñ#6Ðà*2Ð Ñ'ä(ØÐ-÷ñ ð &(Ð"Øò[ ð^ Ñ!ØÐäØ
�$˜F¨°À6Ø 4¨dÐ;MñOð Or$   )N)Ú__doc__Únumpyr0   Únumpy.linalgr   r   Úscipy.sparse.linalgr   r   r   Úscipy.optimizer   Úscipy._lib._utilr	   Úcommonr   r   r   r   r   r   r   r   r   r   r   r   r.   r?   rR   rž   © r$   r"   Ú<module>r§      sS   ðñ)óT ß $ç FÑ FÝ )Ý 6÷7÷ 7÷ 7ó 7òOò*:ò:(CðX DHõBOr$   