§
    fŠtj‘/  ã                   ó¢   — d Z ddlZddlmZmZ ddlmZmZm	Z	 ddl
mZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZ d	„ Zd
„ Zd„ Z	 dd„ZdS )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                 ól   ‡ ‡‡— ‰ j         \  }}ˆ ˆˆfd„}ˆ ˆˆfd„}t          ||f||t          ¬¦  «        S )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                 óŠ   •— |                       ¦   «                              ¦   «         }d|‰<   ‰                     | ‰z  ¦  «        S ©Nr   )ÚravelÚcopyÚmatvec)ÚxÚx_freeÚJopÚ
active_setÚds     €€€úX/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/scipy/optimize/_lsq/dogbox.pyr   zlsmr_operator.<locals>.matvecB   s:   ø€ Ø—’‘”—’Ñ!Ô!ˆØˆˆzÑØ�zŠz˜!˜a™%Ñ Ô Ð ó    c                 óB   •— ‰‰                      | ¦  «        z  }d|‰<   |S r   )Úrmatvec)r   Úrr   r    r!   s     €€€r"   r%   zlsmr_operator.<locals>.rmatvecG   s%   ø€ Ø�—’˜A‘”ÑˆØˆˆ*‰Øˆr#   )r   r%   Údtype)Úshaper   Úfloat)r   r!   r    ÚmÚnr   r%   s   ```    r"   Úlsmr_operatorr,   :   su   øøø€ ð Œ9�D€A€qð!ð !ð !ð !ð !ð !ð !ð
ð ð ð ð ð ð õ
 ˜1˜a˜&¨¸ÍÐNÑNÔNÐNr#   c                 ó&  — || z
  }|| z
  }t          j        || ¦  «        }t          j        ||¦  «        }t          j        ||¦  «        }t          j        ||¦  «        }	t          j        || ¦  «        }
t          j        ||¦  «        }||||	|
|fS )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   s“   € ð �q‘&€KØ�q‘&€KåŒz˜+¨	 zÑ2Ô2€HÝŒz˜+ yÑ1Ô1€HåŒX�h Ñ,Ô,€FÝŒX�h Ñ,Ô,€FåŒ8�H˜y˜jÑ)Ô)€DÝŒ8�H˜iÑ(Ô(€Dà�X˜v v¨t°TÐ9Ð9r#   c                 óâ  — t          | |||¦  «        \  }}	}
}}}t          j        | t          ¬¦  «        }t	          |||	¦  «        r||dfS t          t          j        | ¦  «        | ||	¦  «        \  }}t          ||d|¦  «        d          |z  }||z
  }t          ||||	¦  «        \  }}d||dk     |
z  <   d||dk    |z  <   t          j        |dk     |z  |dk    |z  z  ¦  «        }|||z  z   ||fS )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=   r.   Ú
zeros_likeÚintr   r   r   Úany)r   Únewton_stepÚgÚaÚbr2   r3   r4   r7   r8   r9   r:   r;   r<   Ú
bound_hitsÚ	to_boundsÚ_Úcauchy_stepÚ	step_diffÚ	step_sizeÚhitsÚtr_hits                         r"   Údogleg_steprP   l   s/  € õ  6GØ	ˆ9�b˜"ñ6ô 6Ñ2€Hˆh˜ ¨¨dõ ”˜q­Ð,Ñ,Ô,€Jå�˜h¨Ñ1Ô1ð .Ø˜J¨Ð-Ð-å%¥b¤m°AÑ&6Ô&6¸¸¸HÀhÑOÔO�L€Iˆqõ )¨¨A¨q°)Ñ<Ô<¸QÔ?Ð?À!ÑC€Kà˜kÑ)€IÝ(¨°iØ)1°8ñ=ô =�O€Iˆtà&(€J��q’˜FÑ"Ñ#Ø&'€J��q’˜FÑ"Ñ#ÝŒV�T˜A’X Ñ%¨°ª°TÑ(9Ñ9Ñ:Ô:€Fà˜ YÑ.Ñ.°
¸FÐBÐBr#   c                 óþ	  — |}|                      ¦   «         }d}|}d}|�= ||¦  «        }dt          j        |d         ¦  «        z  }t          |||¦  «        \  }}ndt          j        ||¦  «        z  }t          ||¦  «        }t          |t          ¦  «        o|dk    }|rt          |¦  «        \  }}n|d|z  }}t          ||z  t          j
        ¬¦  «        }|dk    rd}t          j        |t          ¬¦  «        }d|t          j        ||¦  «        <   d|t          j        ||¦  «        <   |}t          j        |¦  «        }|
€
|j        d	z  }
d } d}!d }"d }#|d
k    rt!          ¦   «          	 ||z  dk     }$|$ }%||%         }&|                      ¦   «         }'d||$<   t          |t          j
        ¬¦  «        }(|(|	k     rd} |d
k    rt#          |!|||#|"|(¦  «         | €||
k    r�n||%         })||%         }*||%         }+||%         },|dk    r;|d d …|%f         }-t%          |-| d¬¦  «        d         }.t'          |-|&|& ¦  «        \  }/}0n[|dk    rUt)          |¦  «        }1t+          |1||$¦  «        }2t-          |2|fi |¤Žd         |%          }.|.|,z  }.t'          |1|| ¦  «        \  }/}0d}#|#dk    �rh||
k     �ra||,z  }3t/          |)|.|&|/|0|3|*|+¦  «        \  }4}5}6|                     d¦  «         |4||%<   |dk    rt3          |-|&|4¦  «         }7n|dk    rt3          |1||¦  «         }7t          j        ||z   ||¦  «        }8 | |8¦  «        }9|dz  }t          ||z  t          j
        ¬¦  «        }:t          j        t          j        |9¦  «        ¦  «        sd|:z  }Œë|� ||9d¬¦  «        };ndt          j        |9|9¦  «        z  };||;z
  }#t;          ||#|7|:|6¦  «        \  }}<t          |¦  «        }"t=          |#||"t          |¦  «        |<||¦  «        } | �n|#dk    r||
k     �°a|#dk    r˜|5||%<   |8}|dk    }=||=         ||=<   |dk    }=||=         ||=<   |9}|                      ¦   «         }|;} ||¦  «        }|dz  }|� ||¦  «        }t          |||¦  «        \  }}t          ||¦  «        }|rt          ||¦  «        \  }}nd}"d}#|!dz  }!|�+t?          |||!|¬¦  «        }>|;|>d<   tA          ||>¦  «        rd} n�Œw| €d} t?          |||||'|(|||| ¬¦
  «
        S )Nr
   g      à?r   Újac)Úordg      ð?r?   r@   éd   é   TÚexact)Úrcondr   g      ð¿g        g      Ð?)Ú	cost_only)r   ÚfunÚnitÚnfevÚcostéþÿÿÿ)
r   r\   rY   rR   ÚgradÚ
optimalityÚactive_maskr[   ÚnjevÚstatus)!r   r.   Úsumr   Údotr   Ú
isinstanceÚstrr   r   ÚinfrA   rB   r1   Ú
empty_likeÚsizer   r   r   r   r   r,   r   rP   Úfillr   ÚclipÚallÚisfiniter   r   r   r	   )?rY   rR   Úx0Úf0ÚJ0r3   r4   ÚftolÚxtolÚgtolÚmax_nfevÚx_scaleÚloss_functionÚ	tr_solverÚ
tr_optionsÚverboseÚcallbackÚfÚf_truer[   ÚJra   Úrhor\   rE   Ú	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_freerD   rF   rG   r   Úlsmr_opr2   Ú	step_freeÚon_bound_freerO   Úpredicted_reductionÚx_newÚf_newÚstep_h_normÚcost_newÚratioÚmaskÚintermediate_results?                                                                  r"   Údogboxrœ   —   sZ  € à
€AØ�VŠV‰XŒX€FØ€Dà
€AØ€DàÐ Øˆm˜AÑÔˆØ•R”V˜C œF‘^”^Ñ#ˆÝ-¨a°°CÑ8Ô8‰ˆˆ1ˆ1à•R”V˜A˜q‘\”\Ñ!ˆå�Q˜ÑÔ€Aå˜7¥CÑ(Ô(Ð=¨W¸Ò-=€IØð 0Ý,¨QÑ/Ô/Ñˆˆyˆyà" A¨¡Kˆyˆå��i‘¥R¤VÐ,Ñ,Ô,€EØ�‚z€zØˆåŒ}˜R¥sÐ+Ñ+Ô+€HØ!#€H�RŒX�b˜"ÑÔÑØ!"€H�RŒX�b˜"ÑÔÑà
€AÝŒ=˜ÑÔ€DàÐØ”7˜S‘=ˆàÐØ€IØ€IØÐà�!‚|€|ÝÑ Ô Ð ðMØ ‘\ AÒ%ˆ
Ø�;ˆà�8”ˆØ—’‘”ˆØˆˆ*‰å�a�RœVÐ$Ñ$Ô$ˆØ�DŠ=ˆ=Ø!"Ðà�aŠ<ˆ<Ý% i°°tÐ=MØ&/°ñ9ô 9ð 9ð Ð)¨T°XÒ-=Ð-=Ùà�8”ˆØ�X”,ˆØ�X”,ˆØ˜8”_ˆ
ð ˜ÒÐØ�q�q�q˜(�{”^ˆFÝ ¨¨°"Ð5Ñ5Ô5°aÔ8ˆKõ & f¨f°v°gÑ>Ô>‰DˆAˆqˆqØ˜&Ò Ð Ý" 1Ñ%Ô%ˆCõ $ C¨°
Ñ;Ô;ˆGÝ ¨Ð9Ð9¨jÐ9Ð9¸!Ô<¸XÔFÐFˆKØ˜:Ñ%ˆKõ & c¨1¨q¨bÑ1Ô1‰DˆAˆqàÐØ !Ò#Ñ#¨¨xª©Ø 
Ñ*ˆIå/:Ø˜ V¨Q°°9¸gÀwñ0Pô 0PÑ,ˆI�} fð �IŠI�c‰NŒNˆNØ&ˆD�‰Nà˜GÒ#Ð#Ý'9¸&À&Ø:Cñ(Eô (Eð 'EÐ#Ð#à˜fÒ$Ð$Ý'9¸#¸qÀ$Ñ'GÔ'GÐ&GÐ#õ ”G˜A ™H b¨"Ñ-Ô-ˆEà�C˜‘J”JˆEØ�A‰IˆDå˜t iÑ/µR´VÐ<Ñ<Ô<ˆKå”6�"œ+ eÑ,Ô,Ñ-Ô-ð Ø˜{Ñ*�Øð Ð(Ø(˜=¨¸$Ð?Ñ?Ô?��à¥¤¨¨uÑ!5Ô!5Ñ5�Ø# h™Ðå+ØÐ'Ð)<Ø˜Vñô ‰LˆE�5õ
 ˜T™
œ
ˆIÝ!2Ø  $¨	µ4¸±7´7¸EÀ4Èñ"Oô "OÐð "Ð-ØðY  !Ò#Ð#¨¨xª©ð\ ˜aÒÐØ!.ˆH�XÑàˆAà˜r’>ˆDØ˜”hˆAˆd‰GØ˜q’=ˆDØ˜”hˆAˆd‰GàˆAØ—V’V‘X”XˆFàˆDà��A‘”ˆAØ�A‰IˆDàÐ(Ø#�m AÑ&Ô&�Ý5°a¸¸CÑ@Ô@‘��1å˜Q Ñ"Ô"ˆAàð CÝ#4°Q¸	Ñ#BÔ#BÑ ��yøàˆIØ Ðà�Q‰ˆ	ð ÐÝ"0Ø˜ 	°ð#6ñ #6ô #6Ðà*2Ð Ñ'å(ØÐ-ñô ð ð &(Ð"Øñ[Mð^ Ð!ØÐåØ
�$˜F¨°À6Ø 4¨dÐ;MðOñ Oô Oð Or#   )N)Ú__doc__Únumpyr.   Ú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=   rP   rœ   © r#   r"   ú<module>r¥      si  ðð)ð )ðT Ð Ð Ð Ø $Ð $Ð $Ð $Ð $Ð $Ð $Ð $à FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FØ )Ð )Ð )Ð )Ð )Ð )Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6ð7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ðOð Oð Oð*:ð :ð :ð:(Cð (Cð (CðX DHðBOð BOð BOð BOð BOð BOr#   