| Server IP : 217.160.0.135 / Your IP : 216.73.217.85 Web Server : Apache System : Linux www 6.18.52-i1-ampere #1203 SMP Mon Sep 14 18:29:59 CEST 2026 aarch64 User : sws1074145052 ( 1074145052) PHP Version : 8.3.32 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /usr/lib/python3/dist-packages/scipy/optimize/_lsq/__pycache__/ |
Upload File : |
�
d�c~P � �6 � d Z ddlmZ ddlZddlmZ ddlmZm Z m
Z
ddlmZ ddl
mZmZ ej e� � j Zd� Z d&d�Zd� Zd
� Zd'd�Zd(d�Zd)d�Zd� Zd� Zd*d�Zd*d�Zd� Zd� 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-dS ),z+Functions used by least-squares algorithms.� )�copysignN)�norm)�
cho_factor� cho_solve�LinAlgError)�issparse)�LinearOperator�aslinearoperatorc �r � t j ||� � }|dk rt d� � �t j | |� � }t j | | � � |dz z
}|dk rt d� � �t j ||z ||z z
� � }|t ||� � z }||z }||z } || k r|| fS | |fS )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
�</usr/lib/python3/dist-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 }nd}|r1|� ||z � � }
t |
� � |k r|
ddfS t |
� � |z }|r | d|
||� � \ }}| |z }nd}|�|s |dk rt d|z ||z dz � � }n|}t |� � D ]�}||k s||k rt d|z ||z dz � � } | ||
||� � \ }}|dk r|}||z }t |||z
� � }|||z |z |z z }t j |� � ||z k r n��|� |
|d z |z z � � }
|
|t |
� � z z }
|
||d
z fS )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 � � |dz | z }t ||z � � }||z
}t j |dz |dz z � � |z }||fS )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 ||� � |dz k r|dfS n# t $ r Y nw xY w| d |dz z }| d |dz z }| d |dz 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|dz z z d|dz z
d|dz z z f� � z }d t j
|| � |� � z d�
� � z t j ||� � z }
t j |
� � }|dd�|f }|dfS )
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 �AA �
A�Ac �~ � |dk r||z }n||cxk rdk rn nd}nd}|dk rd|z } n
|dk r|r| dz } | |fS )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
}|dz }t j ||� � }|��| � |� � }|t j ||� � z
}dt j ||� � z t j ||� � z } |�9|t j ||z |� � z
}| dt j ||z |� � z z
} ||| fS ||fS )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 matrix 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� � �>