# Path: nlp/tools/utils.py
# def to_boundary(x, p, delta, xx=None):
#     u"""Compute a solution of the quadratic trust region equation.
# 
#     Return the largest (non-negative) solution of
#         ‖x + σ p‖ = Δ.
# 
#     The code is only guaranteed to produce a non-negative solution
#     if ‖x‖ ≤ Δ, and p != 0.
#     If the trust region equation has no solution, σ is set to 0.
# 
#     :keywords:
#         :xx: squared norm of argument `x`.
#     """
#     if delta is None:
#         raise ValueError('`delta` value must be a positive number.')
#     px = np.dot(p, x)
#     pp = np.dot(p, p)
#     if xx is None:
#         xx = np.dot(x, x)
#     d2 = delta**2
# 
#     # Guard against abnormal cases.
#     rad = px**2 + pp * (d2 - xx)
#     rad = sqrt(max(rad, 0.0))
# 
#     if px > 0:
#         sigma = (d2 - xx) / (px + rad)
#     elif rad > 0:
#         sigma = (rad - px) / pp
#     else:
#         sigma = 0
#     return sigma
# 
# Path: nlp/tools/exceptions.py
# class UserExitRequest(Exception):
#     """Exception that the caller can use to request clean exit."""
# 
#     pass


from nlp.tools.utils import to_boundary
from nlp.tools.exceptions import UserExitRequest
from math import sqrt
import numpy as np
import logging
        y = prec(r)
        ry = np.dot(r, y)
        sqrtry = sqrt(ry)

        stop_tol = max(abstol, reltol * sqrtry)
        k = 0

        exitOptimal = sqrtry <= stop_tol
        exitIter = k > maxiter
        exitUser = False

        p = -y

        onBoundary = False
        infDescent = False

        self.log.info(self.header)
        self.log.info('-' * len(self.header))

        while not (exitOptimal or exitIter or exitUser) and \
                not onBoundary and not infDescent:

            k += 1
            Hp = H * p
            pHp = np.dot(p, Hp)

            self.log.info(self.fmt % (k, ry, pHp))

            # Compute steplength to the boundary.
            if radius is not None:

# Path: nlp/tools/utils.py
# def to_boundary(x, p, delta, xx=None):
#     u"""Compute a solution of the quadratic trust region equation.
# 
#     Return the largest (non-negative) solution of
#         ‖x + σ p‖ = Δ.
# 
#     The code is only guaranteed to produce a non-negative solution
#     if ‖x‖ ≤ Δ, and p != 0.
#     If the trust region equation has no solution, σ is set to 0.
# 
#     :keywords:
#         :xx: squared norm of argument `x`.
#     """
#     if delta is None:
#         raise ValueError('`delta` value must be a positive number.')
#     px = np.dot(p, x)
#     pp = np.dot(p, p)
#     if xx is None:
#         xx = np.dot(x, x)
#     d2 = delta**2
# 
#     # Guard against abnormal cases.
#     rad = px**2 + pp * (d2 - xx)
#     rad = sqrt(max(rad, 0.0))
# 
#     if px > 0:
#         sigma = (d2 - xx) / (px + rad)
#     elif rad > 0:
#         sigma = (rad - px) / pp
#     else:
#         sigma = 0
#     return sigma
# 
# Path: nlp/tools/exceptions.py
# class UserExitRequest(Exception):
#     """Exception that the caller can use to request clean exit."""
# 
#     pass


# Path: nlp/optimize/pcg.py
from nlp.tools.utils import to_boundary
from nlp.tools.exceptions import UserExitRequest
from math import sqrt
import numpy as np
import logging

        y = prec(r)
        ry = np.dot(r, y)
        sqrtry = sqrt(ry)

        stop_tol = max(abstol, reltol * sqrtry)
        k = 0

        exitOptimal = sqrtry <= stop_tol
        exitIter = k > maxiter
        exitUser = False

        p = -y

        onBoundary = False
        infDescent = False

        self.log.info(self.header)
        self.log.info('-' * len(self.header))

        while not (exitOptimal or exitIter or exitUser) and \
                not onBoundary and not infDescent:

            k += 1
            Hp = H * p
            pHp = np.dot(p, Hp)

            self.log.info(self.fmt % (k, ry, pHp))

            # Compute steplength to the boundary.
            if radius is not None:

                sigma = to_boundary(s, p, radius, xx=snorm2)