§
    fŠtj P  ã                   ó6  — d Z ddlmZ ddlZddlmZ ddlmZm	Z	m
Z
 ddlmZmZ ddlmZ  ej        e¦  «        j        Zd„ Z	 	 d&d„Zd„ Zd„ Zd'd„Zd(d„Zd)d„Zd„ Zd„ Zd*d„Zd*d„Zd„ Zd„ Z d„ Z!d„ Z"d„ Z#d„ Z$d„ Z%d)d„Z&d„ Z'd„ Z(d „ Z)d+d"„Z*d+d#„Z+d$„ Z,d%„ Z-dS ),z+Functions used by least-squares algorithms.é    )ÚcopysignN)Únorm)Ú
cho_factorÚ	cho_solveÚLinAlgError)ÚLinearOperatorÚaslinearoperator)Úissparsec                 ór  — t          j        ||¦  «        }|dk    rt          d¦  «        ‚t          j        | |¦  «        }t          j        | | ¦  «        |dz  z
  }|dk    rt          d¦  «        ‚t          j        ||z  ||z  z
  ¦  «        }|t	          ||¦  «        z    }||z  }||z  }	||	k     r||	fS |	|fS )aq  Find the intersection of a line with the boundary of a trust region.

    This function solves the quadratic equation with respect to t
    ||(x + s*t)||**2 = Delta**2.

    Returns
    -------
    t_neg, t_pos : tuple of float
        Negative and positive roots.

    Raises
    ------
    ValueError
        If `s` is zero or `x` is not within the trust region.
    r   z`s` is zero.é   z#`x` is not within the trust region.)ÚnpÚdotÚ
ValueErrorÚsqrtr   )
ÚxÚsÚDeltaÚaÚbÚcÚdÚqÚt1Út2s
             úX/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/scipy/optimize/_lsq/common.pyÚintersect_trust_regionr      sÈ   € õ  	Œˆq�!‰Œ€AØˆA‚v€vÝ˜Ñ(Ô(Ð(å
Œˆq�!‰Œ€Aå
Œˆq�!‰Œ�u˜a‘xÑ€AØˆ1‚u€uÝÐ>Ñ?Ô?Ð?å
Œ��!‘�a˜‘c‘	ÑÔ€Að �h�q˜!‰nŒnÑ
Ð€AØ	
ˆQ‰€BØ	
ˆQ‰€Bà	ˆB‚w€wØ�2ˆvˆà�2ˆvˆó    ç{®Gáz„?é
   c	                 ó  — d„ }	||z  }
|| k    r t           |z  |d         z  }|d         |k    }nd}|r1|                     ||z  ¦  «         }t          |¦  «        |k    r|ddfS t          |
¦  «        |z  }|r |	d|
||¦  «        \  }}| |z  }nd}|�|s |dk    rt          d|z  ||z  dz  ¦  «        }n|}t	          |¦  «        D ]ƒ}||k     s||k    rt          d|z  ||z  dz  ¦  «        } |	||
||¦  «        \  }}|dk     r|}||z  }t          |||z
  ¦  «        }|||z   |z  |z  z  }t          j        |¦  «        ||z  k     r nŒ„|                     |
|d	z  |z   z  ¦  «         }||t          |¦  «        z  z  }|||d
z   fS )aÁ  Solve a trust-region problem arising in least-squares minimization.

    This function implements a method described by J. J. More [1]_ and used
    in MINPACK, but it relies on a single SVD of Jacobian instead of series
    of Cholesky decompositions. Before running this function, compute:
    ``U, s, VT = svd(J, full_matrices=False)``.

    Parameters
    ----------
    n : int
        Number of variables.
    m : int
        Number of residuals.
    uf : ndarray
        Computed as U.T.dot(f).
    s : ndarray
        Singular values of J.
    V : ndarray
        Transpose of VT.
    Delta : float
        Radius of a trust region.
    initial_alpha : float, optional
        Initial guess for alpha, which might be available from a previous
        iteration. If None, determined automatically.
    rtol : float, optional
        Stopping tolerance for the root-finding procedure. Namely, the
        solution ``p`` will satisfy ``abs(norm(p) - Delta) < rtol * Delta``.
    max_iter : int, optional
        Maximum allowed number of iterations for the root-finding procedure.

    Returns
    -------
    p : ndarray, shape (n,)
        Found solution of a trust-region problem.
    alpha : float
        Positive value such that (J.T*J + alpha*I)*p = -J.T*f.
        Sometimes called Levenberg-Marquardt parameter.
    n_iter : int
        Number of iterations made by root-finding procedure. Zero means
        that Gauss-Newton step was selected as the solution.

    References
    ----------
    .. [1] More, J. J., "The Levenberg-Marquardt Algorithm: Implementation
           and Theory," Numerical Analysis, ed. G. A. Watson, Lecture Notes
           in Mathematics 630, Springer Verlag, pp. 105-116, 1977.
    c                 óŠ   — |dz  | z   }t          ||z  ¦  «        }||z
  }t          j        |dz  |dz  z  ¦  «         |z  }||fS )z Function of which to find zero.

        It is defined as "norm of regularized (by alpha) least-squares
        solution minus `Delta`". Refer to [1]_.
        r   é   )r   r   Úsum)ÚalphaÚsufr   r   ÚdenomÚp_normÚphiÚ	phi_primes           r   Úphi_and_derivativez2solve_lsq_trust_region.<locals>.phi_and_derivativej   sY   € ð �1‘�u‘ˆÝ�c˜E‘kÑ"Ô"ˆØ�u‰nˆÝ”V˜C 1™H u¨a¡xÑ/Ñ0Ô0Ð0°6Ñ9ˆ	Ø�Iˆ~Ðr   r   éÿÿÿÿFg        Ngü©ñÒMbP?ç      à?r   é   )ÚEPSr   r   ÚmaxÚranger   Úabs)ÚnÚmÚufr   ÚVr   Úinitial_alphaÚrtolÚmax_iterr*   r%   Ú	thresholdÚ	full_rankÚpÚalpha_upperr(   r)   Úalpha_lowerr$   ÚitÚratios                        r   Úsolve_lsq_trust_regionr@   9   s  € ðb
ð 
ð 
ð ˆb‰&€Cð 	ˆA‚v€vÝ˜!‘G˜a œd‘Nˆ	Ø�b”E˜IÒ%ˆ	ˆ	àˆ	àð Ø�UŠU�2˜‘6‰]Œ]ˆNˆÝ�‰7Œ7�eÒÐØ�c˜1�9Ðå�s‘)”)˜eÑ#€Kàð Ø+Ð+¨C°°a¸Ñ?Ô?‰ˆˆYØ�d˜YÑ&ˆˆàˆàÐ IÐ°-À1Ò2DÐ2DÝ�E˜KÑ'¨+¸Ñ*CÀcÑ)IÑJÔJˆˆàˆå�H‰oŒoð ð ˆØ�;ÒÐ %¨+Ò"5Ð"5Ý˜ Ñ+¨k¸KÑ.GÈ#Ñ-MÑNÔNˆEà+Ð+¨E°3¸¸5ÑAÔA‰ˆˆYà�Š7ˆ7ØˆKà�i‘ˆÝ˜+ u¨u¡}Ñ5Ô5ˆØ�#˜‘+ Ñ&¨Ñ.Ñ.ˆåŒ6�#‰;Œ;˜ ™Ò%Ð%ØˆEð &ð 
�Šˆs�a˜‘d˜U‘lÑ#Ñ	$Ô	$Ð$€Að
 ˆ•�a‘”‰Ñ€Aàˆe�R˜!‘VÐÐr   c                 óX  — 	 t          | ¦  «        \  }}t          ||f|¦  «         }t          j        ||¦  «        |dz  k    r|dfS n# t          $ r Y nw xY w| d         |dz  z  }| d         |dz  z  }| d         |dz  z  }|d         |z  }	|d         |z  }
t          j        | |	z   d||z
  |
z   z  d|z  d| |z   |
z   z  | |	z
  g¦  «        }t          j        |¦  «        }t          j        |t          j        |¦  «                 ¦  «        }|t          j	        d|z  d|dz  z   z  d|dz  z
  d|dz  z   z  f¦  «        z  }d	t          j
        ||                      |¦  «        z  d¬
¦  «        z  t          j        ||¦  «        z   }t          j        |¦  «        }|dd…|f         }|dfS )az  Solve a general trust-region problem in 2 dimensions.

    The problem is reformulated as a 4th order algebraic equation,
    the solution of which is found by numpy.roots.

    Parameters
    ----------
    B : ndarray, shape (2, 2)
        Symmetric matrix, defines a quadratic term of the function.
    g : ndarray, shape (2,)
        Defines a linear term of the function.
    Delta : float
        Radius of a trust region.

    Returns
    -------
    p : ndarray, shape (2,)
        Found solution.
    newton_step : bool
        Whether the returned solution is the Newton step which lies within
        the trust region.
    r   T)r   r   )r   r-   )r-   r-   r   r-   é   r,   ©ÚaxisNF)r   r   r   r   r   ÚarrayÚrootsÚrealÚisrealÚvstackr#   Úargmin)ÚBÚgr   ÚRÚlowerr;   r   r   r   r   ÚfÚcoeffsÚtÚvalueÚis                  r   Úsolve_trust_region_2drT   «   sÙ  € ð.Ý˜a‘=”=‰ˆˆ5Ý˜˜5�z 1Ñ%Ô%Ð%ˆÝŒ6�!�Q‰<Œ<˜5 !™8Ò#Ð#Ø�d�7ˆNð $øåð ð ð Øˆðøøøð 	
ˆ$Œ�%˜‘(Ñ€AØ	ˆ$Œ�%˜‘(Ñ€AØ	ˆ$Œ�%˜‘(Ñ€Aà	ˆ!Œˆu‰€AØ	ˆ!Œˆu‰€AåŒXØ
ˆˆa‰��a˜!‘e˜a‘i‘ ! a¡%¨¨q¨b°1©f°q©jÑ)9¸A¸2À¹6ÐBñDô D€Få
Œ�ÑÔ€AÝ
Œ�•"”)˜A‘,”,”Ñ Ô €Aà•”	˜1˜q™5 A¨¨1©¡HÑ-°°A°q±D±¸QÀÀAÁ¹XÑ/FÐGÑHÔHÑH€AØ•"”&˜˜QŸUšU 1™XœX™¨AÐ.Ñ.Ô.Ñ.µ´¸¸1±´Ñ=€EÝ
Œ	�%ÑÔ€AØ	ˆ!ˆ!ˆ!ˆQˆ$Œ€Aàˆeˆ8€Os   ‚AA Á
AÁAc                 ó~   — |dk    r||z  }n||cxk    rdk    rn nd}nd}|dk     rd|z  } n|dk    r|r| dz  } | |fS )zÍUpdate the radius of a trust region based on the cost reduction.

    Returns
    -------
    Delta : float
        New radius.
    ratio : float
        Ratio between actual and predicted reductions.
    r   r-   ç      Ð?g      è?g       @© )r   Úactual_reductionÚpredicted_reductionÚ	step_normÚ	bound_hitr?   s         r   Úupdate_tr_radiusr\   Þ   s€   € ð ˜QÒÐØ Ð#6Ñ6ˆˆØ	Ð 0Ð	5Ð	5Ò	5Ð	5°AÒ	5Ð	5Ð	5Ð	5Ð	5Øˆˆàˆàˆt‚|€|Ø�yÑ ˆˆØ	�Šˆ˜)ˆØ�‰ˆà�%ˆ<Ðr   c                 ó  — |                       |¦  «        }t          j         ||¦  «        }|�|t          j         ||z  |¦  «        z  }|dz  }t          j         ||¦  «        }|�›|                       |¦  «        }|t          j         ||¦  «        z  }dt          j         ||¦  «        z  t          j         ||¦  «        z   }	|�9|t          j         ||z  |¦  «        z  }|	dt          j         ||z  |¦  «        z  z  }	|||	fS ||fS )a¬  Parameterize a multivariate quadratic function along a line.

    The resulting univariate quadratic function is given as follows::

        f(t) = 0.5 * (s0 + s*t).T * (J.T*J + diag) * (s0 + s*t) +
               g.T * (s0 + s*t)

    Parameters
    ----------
    J : ndarray, sparse array or LinearOperator shape (m, n)
        Jacobian matrix, affects the quadratic term.
    g : ndarray, shape (n,)
        Gradient, defines the linear term.
    s : ndarray, shape (n,)
        Direction vector of a line.
    diag : None or ndarray with shape (n,), optional
        Addition diagonal part, affects the quadratic term.
        If None, assumed to be 0.
    s0 : None or ndarray with shape (n,), optional
        Initial point. If None, assumed to be 0.

    Returns
    -------
    a : float
        Coefficient for t**2.
    b : float
        Coefficient for t.
    c : float
        Free term. Returned only if `s0` is provided.
    Nr,   )r   r   )
ÚJrL   r   ÚdiagÚs0Úvr   r   Úur   s
             r   Úbuild_quadratic_1drc   û   sø   € ð> 	
�Šˆa‰Œ€AÝ
Œˆq�!‰Œ€AØÐØ	�RŒV�A˜‘H˜aÑ Ô Ñ ˆØˆ�H€Aå
Œˆq�!‰Œ€Aà	€~Ø�EŠE�"‰IŒIˆØ	�RŒV�A�q‰\Œ\ÑˆØ•"”&˜˜A‘,”,Ñ¥¤¨¨2¡¤Ñ.ˆØÐØ•”˜˜T™	 1Ñ%Ô%Ñ%ˆAØ�•r”v˜b 4™i¨Ñ,Ô,Ñ,Ñ,ˆAØ�!�Qˆwˆà�!ˆtˆr   c                 óü   — ||g}| dk    r-d|z  | z  }||cxk     r|k     rn n|                      |¦  «         t          j        |¦  «        }|| |z  |z   z  |z   }t          j        |¦  «        }||         ||         fS )zÛMinimize a 1-D quadratic function subject to bounds.

    The free term `c` is 0 by default. Bounds must be finite.

    Returns
    -------
    t : float
        Minimum point.
    y : float
        Minimum value.
    r   g      à¿)Úappendr   ÚasarrayrJ   )	r   r   ÚlbÚubr   rQ   ÚextremumÚyÚ	min_indexs	            r   Úminimize_quadratic_1drl   .  sš   € ð 
ˆRˆ€AØˆA‚v€vØ˜!‘8˜a‘<ˆØ�ÐÐÒÐ˜2ÒÐÐÐÐØ�HŠH�XÑÔÐÝ
Œ
�1‰Œ€AØ	ˆQ�‰U�Q‰Y‰˜!Ñ€AÝ”	˜!‘”€IØˆYŒ<˜˜9œÐ%Ð%r   c                 óŠ  — |j         dk    rH|                      |¦  «        }t          j        ||¦  «        }|�|t          j        ||z  |¦  «        z  }nT|                      |j        ¦  «        }t          j        |dz  d¬¦  «        }|�|t          j        ||dz  z  d¬¦  «        z  }t          j        ||¦  «        }d|z  |z   S )aë  Compute values of a quadratic function arising in least squares.

    The function is 0.5 * s.T * (J.T * J + diag) * s + g.T * s.

    Parameters
    ----------
    J : ndarray, sparse array or LinearOperator, shape (m, n)
        Jacobian matrix, affects the quadratic term.
    g : ndarray, shape (n,)
        Gradient, defines the linear term.
    s : ndarray, shape (k, n) or (n,)
        Array containing steps as rows.
    diag : ndarray, shape (n,), optional
        Addition diagonal part, affects the quadratic term.
        If None, assumed to be 0.

    Returns
    -------
    values : ndarray with shape (k,) or float
        Values of the function. If `s` was 2-D, then ndarray is
        returned, otherwise, float is returned.
    r-   Nr   r   rC   r,   )Úndimr   r   ÚTr#   )r^   rL   r   r_   ÚJsr   Úls          r   Úevaluate_quadraticrr   E  s»   € ð. 	„v�‚{€{Ø�UŠU�1‰XŒXˆÝŒF�2�r‰NŒNˆØÐØ•”˜˜D™ !Ñ$Ô$Ñ$ˆAøà�UŠU�1”3‰ZŒZˆÝŒF�2�q‘5˜qÐ!Ñ!Ô!ˆØÐØ•”˜˜q !™t™¨!Ð,Ñ,Ô,Ñ,ˆAå
Œˆq�!‰Œ€Aà�‰7�Q‰;Ðr   c                 ó@   — t          j        | |k    | |k    z  ¦  «        S )z$Check if a point lies within bounds.)r   Úall)r   rg   rh   s      r   Ú	in_boundsru   o  s   € åŒ6�1˜’7˜q BšwÑ'Ñ(Ô(Ð(r   c                 ó
  — t          j        |¦  «        }||         }t          j        | ¦  «        }|                     t           j        ¦  «         t          j        d¬¦  «        5  t          j        || z
  |         |z  || z
  |         |z  ¦  «        ||<   ddd¦  «         n# 1 swxY w Y   t          j        |¦  «        }|t          j        ||¦  «        t          j	        |¦  «         
                    t          ¦  «        z  fS )að  Compute a min_step size required to reach a bound.

    The function computes a positive scalar t, such that x + s * t is on
    the bound.

    Returns
    -------
    step : float
        Computed step. Non-negative value.
    hits : ndarray of int with shape of x
        Each element indicates whether a corresponding variable reaches the
        bound:

             *  0 - the bound was not hit.
             * -1 - the lower bound was hit.
             *  1 - the upper bound was hit.
    Úignore)ÚoverN)r   ÚnonzeroÚ
empty_likeÚfillÚinfÚerrstateÚmaximumÚminÚequalÚsignÚastypeÚint)r   r   rg   rh   Únon_zeroÚ
s_non_zeroÚstepsÚmin_steps           r   Ústep_size_to_boundrˆ   t  s/  € õ$ Œz˜!‰}Œ}€HØ�8”€JÝŒM˜!ÑÔ€EØ	‡J‚J�rŒvÑÔÐÝ	Œ˜(Ð	#Ñ	#Ô	#ð Fð FÝœ* b¨1¡f¨hÔ%7¸*Ñ%DØ&(¨1¡f¨hÔ%7¸*Ñ%DñFô Fˆˆh‰ðFð Fð Fñ Fô Fð Fð Fð Fð Fð Fð Føøøð Fð Fð Fð Fõ Œv�e‰}Œ}€HØ•R”X˜e XÑ.Ô.µ´¸±´×1BÒ1BÅ3Ñ1GÔ1GÑGÐGÐGs   Á%1B"Â"B&Â)B&ç»½×Ùß|Û=c                 óø  — t          j        | t          ¬¦  «        }|dk    rd|| |k    <   d|| |k    <   |S | |z
  }|| z
  }|t          j        dt          j        |¦  «        ¦  «        z  }|t          j        dt          j        |¦  «        ¦  «        z  }t          j        |¦  «        |t          j        ||¦  «        k    z  }	d||	<   t          j        |¦  «        |t          j        ||¦  «        k    z  }
d||
<   |S )a·  Determine which constraints are active in a given point.

    The threshold is computed using `rtol` and the absolute value of the
    closest bound.

    Returns
    -------
    active : ndarray of int with shape of x
        Each component shows whether the corresponding constraint is active:

             *  0 - a constraint is not active.
             * -1 - a lower bound is active.
             *  1 - a upper bound is active.
    ©Údtyper   r+   r-   )r   Ú
zeros_likerƒ   r~   r1   ÚisfiniteÚminimum)r   rg   rh   r7   ÚactiveÚ
lower_distÚ
upper_distÚlower_thresholdÚupper_thresholdÚlower_activeÚupper_actives              r   Úfind_active_constraintsr—   ‘  sõ   € õ Œ]˜1¥CÐ(Ñ(Ô(€Fàˆq‚y€yØˆˆq�BŠw‰Øˆˆq�BŠw‰Øˆà�R‘€JØ�a‘€Jà�RœZ¨­2¬6°"©:¬:Ñ6Ô6Ñ6€OØ�RœZ¨­2¬6°"©:¬:Ñ6Ô6Ñ6€Oå”K ‘O”OØ¥2¤:¨j¸/Ñ#JÔ#JÒJñL€Là€Fˆ<Ñå”K ‘O”OØ¥2¤:¨j¸/Ñ#JÔ#JÒJñL€Là€Fˆ<Ñà€Mr   c           	      ó|  — |                       ¦   «         }t          | |||¦  «        }t          j        |d¦  «        }t          j        |d¦  «        }|dk    rIt          j        ||         ||         ¦  «        ||<   t          j        ||         ||         ¦  «        ||<   nx||         |t          j        dt          j        ||         ¦  «        ¦  «        z  z   ||<   ||         |t          j        dt          j        ||         ¦  «        ¦  «        z  z
  ||<   ||k     ||k    z  }d||         ||         z   z  ||<   |S )zÔShift a point to the interior of a feasible region.

    Each element of the returned vector is at least at a relative distance
    `rstep` from the closest bound. If ``rstep=0`` then `np.nextafter` is used.
    r+   r-   r   r,   )Úcopyr—   r   r€   Ú	nextafterr~   r1   )	r   rg   rh   ÚrstepÚx_newr�   Ú
lower_maskÚ
upper_maskÚtight_boundss	            r   Úmake_strictly_feasibler    ¸  s1  € ð �FŠF‰HŒH€Eå$ Q¨¨B°Ñ6Ô6€FÝ”˜& "Ñ%Ô%€JÝ”˜& !Ñ$Ô$€Jà�‚z€zÝœL¨¨J¬¸¸J¼ÑHÔHˆˆjÑÝœL¨¨J¬¸¸J¼ÑHÔHˆˆjÑÐà 
œ^Ø"¥R¤Z°µ2´6¸"¸Z¼.Ñ3IÔ3IÑ%JÔ%JÑJñKˆˆjÑà 
œ^Ø"¥R¤Z°µ2´6¸"¸Z¼.Ñ3IÔ3IÑ%JÔ%JÑJñKˆˆjÑð ˜B’J 5¨2¢:Ñ.€LØ  LÔ!1°B°|Ô4DÑ!DÑE€Eˆ,Ñà€Lr   c                 ó*  — t          j        | ¦  «        }t          j        | ¦  «        }|dk     t          j        |¦  «        z  }||         | |         z
  ||<   d||<   |dk    t          j        |¦  «        z  }| |         ||         z
  ||<   d||<   ||fS )a4  Compute Coleman-Li scaling vector and its derivatives.

    Components of a vector v are defined as follows::

               | ub[i] - x[i], if g[i] < 0 and ub[i] < np.inf
        v[i] = | x[i] - lb[i], if g[i] > 0 and lb[i] > -np.inf
               | 1,           otherwise

    According to this definition v[i] >= 0 for all i. It differs from the
    definition in paper [1]_ (eq. (2.2)), where the absolute value of v is
    used. Both definitions are equivalent down the line.
    Derivatives of v with respect to x take value 1, -1 or 0 depending on a
    case.

    Returns
    -------
    v : ndarray with shape of x
        Scaling vector.
    dv : ndarray with shape of x
        Derivatives of v[i] with respect to x[i], diagonal elements of v's
        Jacobian.

    References
    ----------
    .. [1] M.A. Branch, T.F. Coleman, and Y. Li, "A Subspace, Interior,
           and Conjugate Gradient Method for Large-Scale Bound-Constrained
           Minimization Problems," SIAM Journal on Scientific Computing,
           Vol. 21, Number 1, pp 1-23, 1999.
    r   r+   r-   )r   Ú	ones_liker�   rŽ   )r   rL   rg   rh   ra   ÚdvÚmasks          r   ÚCL_scaling_vectorr¥   Ó  s“   € õ< 	Œ�Q‰Œ€AÝ	Œ�qÑ	Ô	€Bà�ŠE•R”[ ‘_”_Ñ$€DØ�Œh˜˜4œÑ €A€d�GØ€B€t�Hà�ŠE•R”[ ‘_”_Ñ$€DØ�Œg˜˜4œÑ €A€d�GØ€B€t�Hàˆbˆ5€Lr   c                 óJ  — t          | ||¦  «        r| t          j        | ¦  «        fS t          j        |¦  «        }t          j        |¦  «        }|                      ¦   «         }t          j        | t          ¬¦  «        }|| z  }t          j        | |         d||         z  | |         z
  ¦  «        ||<   | |         ||         k     ||<   | |z  }t          j        | |         d||         z  | |         z
  ¦  «        ||<   | |         ||         k    ||<   ||z  }||z
  }t          j	        | |         ||         z
  d||         z  ¦  «        }	||         t          j        |	d||         z  |	z
  ¦  «        z   ||<   |	||         k    ||<   t          j        | ¦  «        }
d|
|<   ||
fS )z3Compute reflective transformation and its gradient.r‹   r   r+   )
ru   r   r¢   rŽ   r™   r�   Úboolr~   r�   Ú	remainder)rj   rg   rh   Ú	lb_finiteÚ	ub_finiter   Ú
g_negativer¤   r   rQ   rL   s              r   Úreflective_transformationr¬   ÿ  s’  € å��B˜ÑÔð "Ø•"”,˜q‘/”/Ð!Ð!å”˜B‘”€IÝ”˜B‘”€Ià	�Š‰Œ€AÝ”˜q­Ð-Ñ-Ô-€Jà˜	�zÑ!€DÝŒj˜˜4œ ! b¨¤h¡,°°4´Ñ"8Ñ9Ô9€A€d�GØ˜”w  D¤Ò)€JˆtÑàˆ:˜	Ñ!€DÝŒj˜˜4œ ! b¨¤h¡,°°4´Ñ"8Ñ9Ô9€A€d�GØ˜”w  D¤Ò)€JˆtÑà�yÑ €DØ
ˆR‰€AÝ
Œ�Q�t”W˜r $œxÑ'¨¨Q¨t¬W©Ñ5Ô5€AØ�Œh�œ A q¨1¨T¬7¡{°Q¡Ñ7Ô7Ñ7€A€d�GØ˜1˜Tœ7’{€JˆtÑå
Œ�Q‰Œ€AØ€A€j�Màˆaˆ4€Kr   c            
      óT   — t          d                     dddddd¦  «        ¦  «         d S )Nz${:^15}{:^15}{:^15}{:^15}{:^15}{:^15}Ú	Iterationz
Total nfevÚCostúCost reductionú	Step normÚ
Optimality©ÚprintÚformatrW   r   r   Úprint_header_nonlinearr¶   !  s>   € Ý	Ð
0ßŠ6�+˜|¨VÐ5EØ˜|ñ-ô -ñ.ô .ð .ð .ð .r   c           	      óh   — |€d}n|d›}|€d}n|d›}t          | d›|d›|d›|› |› |d›�¦  «         d S ©Nz               z^15.2ez^15z^15.4e©r´   )Ú	iterationÚnfevÚcostÚcost_reductionrZ   Ú
optimalitys         r   Úprint_iteration_nonlinearr¿   '  sq   € àÐØ!ˆˆà*Ð3Ð3ˆàÐØˆ	ˆ	à Ð)Ð)ˆ	å	ˆYÐ
aÐ
a˜DÐ
aÐ
a dÐ
aÐ
a°>Ð
aÀ9Ð
aÈjÐ
aÐ
aÐ
aÑbÔbÐbÐbÐbr   c            	      óR   — t          d                     ddddd¦  «        ¦  «         d S )Nz{:^15}{:^15}{:^15}{:^15}{:^15}r®   r¯   r°   r±   r²   r³   rW   r   r   Úprint_header_linearrÁ   6  s<   € Ý	Ð
*ßŠ6�+˜vÐ'7¸Øñ ô  ñ!ô !ð !ð !ð !r   c                 ób   — |€d}n|d›}|€d}n|d›}t          | d›|d›|› |› |d›�¦  «         d S r¸   r¹   )rº   r¼   r½   rZ   r¾   s        r   Úprint_iteration_linearrÃ   <  si   € àÐØ!ˆˆà*Ð3Ð3ˆàÐØˆ	ˆ	à Ð)Ð)ˆ	å	ˆYÐ
WÐ
W˜DÐ
WÐ
W¨Ð
W¸Ð
WÀJÐ
WÐ
WÐ
WÑXÔXÐXÐXÐXr   c                 óŠ   — t          | t          ¦  «        r|                      |¦  «        S | j                             |¦  «        S )z4Compute gradient of the least-squares cost function.)Ú
isinstancer   Úrmatvecro   r   )r^   rO   s     r   Úcompute_gradrÇ   N  s6   € å�!•^Ñ$Ô$ð Ø�yŠy˜‰|Œ|ÐàŒs�wŠw�q‰zŒzÐr   c                 óJ  — t          | ¦  «        rQt          j        |                      d¦  «                             d¬¦  «        ¦  «                             ¦   «         dz  }nt          j        | dz  d¬¦  «        dz  }|€
d||dk    <   nt          j        ||¦  «        }d|z  |fS )z5Compute variables scale based on the Jacobian matrix.r   r   rC   r,   Nr-   )r
   r   rf   Úpowerr#   Úravelr~   )r^   Úscale_inv_oldÚ	scale_invs      r   Úcompute_jac_scalerÍ   V  sž   € å��{„{ð .Ý”J˜qŸwšw q™zœzŸ~š~°1˜~Ñ5Ô5Ñ6Ô6×<Ò<Ñ>Ô>ÀÑCˆ	ˆ	å”F˜1˜a™4 aÐ(Ñ(Ô(¨#Ñ-ˆ	àÐØ$%ˆ	�)˜q’.Ñ!Ð!å”J˜y¨-Ñ8Ô8ˆ	àˆy‰=˜)Ð#Ð#r   c                 óx   ‡ ‡— t          ‰ ¦  «        Š ˆ ˆfd„}ˆ ˆfd„}ˆ ˆfd„}t          ‰ j        |||¬¦  «        S )z#Return diag(d) J as LinearOperator.c                 ó4   •— ‰‰                      | ¦  «        z  S ©N)Úmatvec©r   r^   r   s    €€r   rÑ   z(left_multiplied_operator.<locals>.matveci  s   ø€ Ø�1—8’8˜A‘;”;‰Ðr   c                 ó\   •— ‰d d …t           j        f         ‰                     | ¦  «        z  S rÐ   )r   ÚnewaxisÚmatmat©ÚXr^   r   s    €€r   rÕ   z(left_multiplied_operator.<locals>.matmatl  s'   ø€ Ø���•B”J�Ô !§(¢(¨1¡+¤+Ñ-Ð-r   c                 óX   •— ‰                      |                      ¦   «         ‰z  ¦  «        S rÐ   )rÆ   rÊ   rÒ   s    €€r   rÆ   z)left_multiplied_operator.<locals>.rmatveco  s!   ø€ Ø�yŠy˜Ÿš™œ Q™Ñ'Ô'Ð'r   ©rÑ   rÕ   rÆ   ©r	   r   Úshape©r^   r   rÑ   rÕ   rÆ   s   ``   r   Úleft_multiplied_operatorrÝ   e  s�   øø€ å˜ÑÔ€Aðð ð ð ð ð ð.ð .ð .ð .ð .ð .ð(ð (ð (ð (ð (ð (õ ˜!œ'¨&¸Ø")ð+ñ +ô +ð +r   c                 óx   ‡ ‡— t          ‰ ¦  «        Š ˆ ˆfd„}ˆ ˆfd„}ˆ ˆfd„}t          ‰ j        |||¬¦  «        S )z#Return J diag(d) as LinearOperator.c                 óX   •— ‰                      t          j        | ¦  «        ‰z  ¦  «        S rÐ   )rÑ   r   rÊ   rÒ   s    €€r   rÑ   z)right_multiplied_operator.<locals>.matvecz  s!   ø€ Ø�xŠx�œ ™œ a™Ñ(Ô(Ð(r   c                 ó\   •— ‰                      | ‰d d …t          j        f         z  ¦  «        S rÐ   )rÕ   r   rÔ   rÖ   s    €€r   rÕ   z)right_multiplied_operator.<locals>.matmat}  s)   ø€ Ø�xŠx˜˜A˜a˜a˜a¥¤˜mÔ,Ñ,Ñ-Ô-Ð-r   c                 ó4   •— ‰‰                      | ¦  «        z  S rÐ   ©rÆ   rÒ   s    €€r   rÆ   z*right_multiplied_operator.<locals>.rmatvec€  s   ø€ Ø�1—9’9˜Q‘<”<ÑÐr   rÙ   rÚ   rÜ   s   ``   r   Úright_multiplied_operatorrã   v  s�   øø€ å˜ÑÔ€Að)ð )ð )ð )ð )ð )ð.ð .ð .ð .ð .ð .ð ð  ð  ð  ð  ð  õ ˜!œ'¨&¸Ø")ð+ñ +ô +ð +r   c                 ó‚   ‡ ‡‡— t          ‰ ¦  «        Š ‰ j        \  Š}ˆ ˆfd„}ˆ ˆˆfd„}t          ‰|z   |f||¬¦  «        S )zµReturn a matrix arising in regularized least squares as LinearOperator.

    The matrix is
        [ J ]
        [ D ]
    where D is diagonal matrix with elements from `diag`.
    c                 ó\   •— t          j        ‰                     | ¦  «        ‰| z  f¦  «        S rÐ   )r   ÚhstackrÑ   )r   r^   r_   s    €€r   rÑ   z(regularized_lsq_operator.<locals>.matvec’  s&   ø€ ÝŒy˜!Ÿ(š( 1™+œ+ t¨a¡xÐ0Ñ1Ô1Ð1r   c                 ób   •— | d ‰…         }| ‰d …         }‰                      |¦  «        ‰|z  z   S rÐ   râ   )r   Úx1Úx2r^   r_   r3   s      €€€r   rÆ   z)regularized_lsq_operator.<locals>.rmatvec•  s6   ø€ Øˆr�ˆrŒUˆØˆqˆrˆrŒUˆØ�yŠy˜‰}Œ}˜t b™yÑ(Ð(r   )rÑ   rÆ   )r	   rÛ   r   )r^   r_   r2   rÑ   rÆ   r3   s   ``   @r   Úregularized_lsq_operatorrê   ‡  s   øøø€ õ 	˜ÑÔ€AØŒ7�D€A€qð2ð 2ð 2ð 2ð 2ð 2ð)ð )ð )ð )ð )ð )ð )õ
 ˜1˜q™5 !˜*¨V¸WÐEÑEÔEÐEr   Tc                 ó&  — |r)t          | t          ¦  «        s|                      ¦   «         } t          | ¦  «        r+| xj        |                     | j        d¬¦  «        z  c_        n+t          | t          ¦  «        rt          | |¦  «        } n| |z  } | S )zhCompute J diag(d).

    If `copy` is False, `J` is modified in place (unless being LinearOperator).
    Úclip)Úmode)rÅ   r   r™   r
   ÚdataÚtakeÚindicesrã   ©r^   r   r™   s      r   Úright_multiplyrò   �  s�   € ð
 ð •J˜q¥.Ñ1Ô1ð Ø�FŠF‰HŒHˆå��{„{ð Ø	ˆŒ�!—&’&˜œ¨�&Ñ0Ô0Ñ0ˆŒˆÝ	�A•~Ñ	&Ô	&ð Ý% a¨Ñ+Ô+ˆˆà	ˆQ‰ˆà€Hr   c                 ón  — |r)t          | t          ¦  «        s|                      ¦   «         } t          | ¦  «        r;| xj        t          j        |t          j        | j        ¦  «        ¦  «        z  c_        n?t          | t          ¦  «        rt          | |¦  «        } n| |dd…t
          j
        f         z  } | S )zhCompute diag(d) J.

    If `copy` is False, `J` is modified in place (unless being LinearOperator).
    N)rÅ   r   r™   r
   rî   r   ÚrepeatÚdiffÚindptrrÝ   rÔ   rñ   s      r   Úleft_multiplyr÷   ¯  s£   € ð
 ð •J˜q¥.Ñ1Ô1ð Ø�FŠF‰HŒHˆå��{„{ð Ø	ˆŒ•"”)˜A�rœw q¤xÑ0Ô0Ñ1Ô1Ñ1ˆŒˆÝ	�A•~Ñ	&Ô	&ð Ý$ Q¨Ñ*Ô*ˆˆà	ˆQˆqˆqˆq•"”*ˆ}ÔÑˆà€Hr   c                 óX   — | ||z  k     o|dk    }||||z   z  k     }|r|rdS |rdS |rdS dS )z8Check termination condition for nonlinear least squares.rV   é   r   r"   NrW   )	ÚdFÚFÚdx_normÚx_normr?   ÚftolÚxtolÚftol_satisfiedÚxtol_satisfieds	            r   Úcheck_terminationr  Á  s^   € à˜$ ™(’]Ð3 u¨t¢|€NØ˜t t¨f¡}Ñ5Ò5€Nàð ˜.ð ØˆqØ	ð ØˆqØ	ð Øˆqàˆtr   c                 óª   — |d         d|d         z  |dz  z  z   }t           ||t           k     <   |dz  }||d         |z  z  }t          | |d¬¦  «        |fS )z`Scale Jacobian and residuals for a robust loss function.

    Arrays are modified in place.
    r-   r   r,   F)r™   )r.   r÷   )r^   rO   ÚrhoÚJ_scales       r   Úscale_for_robust_loss_functionr  Ð  sg   € ð
 �!Œf�q˜3˜qœ6‘z A q¡DÑ(Ñ(€GÝ €GˆG•cŠMÑØ��O€GàˆˆQŒ�'Ñ	Ñ€Aå˜˜G¨%Ð0Ñ0Ô0°!Ð3Ð3r   )Nr   r   )NN)r   rÐ   )r‰   )T).Ú__doc__Úmathr   Únumpyr   Únumpy.linalgr   Úscipy.linalgr   r   r   Úscipy.sparse.linalgr   r	   Úscipy._lib._sparser
   ÚfinfoÚfloatÚepsr.   r   r@   rT   r\   rc   rl   rr   ru   rˆ   r—   r    r¥   r¬   r¶   r¿   rÁ   rÃ   rÇ   rÍ   rÝ   rã   rê   rò   r÷   r  r  rW   r   r   ú<module>r     sv  ðØ 1Ð 1Ø Ð Ð Ð Ð Ð à Ð Ð Ð Ø Ð Ð Ð Ð Ð à ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'ð €b„hˆu�o„oÔ€ð$ð $ð $ðN AEØ/1ðoð oð oð oðd0ð 0ð 0ðfð ð ð:0ð 0ð 0ð 0ðf&ð &ð &ð &ð.$ð $ð $ð $ðT)ð )ð )ð
Hð Hð Hð:$ð $ð $ð $ðNð ð ð ð6)ð )ð )ðXð ð ðD.ð .ð .ðcð cð cð!ð !ð !ðYð Yð Yð$ð ð ð$ð $ð $ð $ð+ð +ð +ð"+ð +ð +ð"Fð Fð Fð,ð ð ð ð$ð ð ð ð$ð ð ð4ð 4ð 4ð 4ð 4r   