# 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
            self.qval += alpha * np.dot(r, p) + 0.5 * alpha**2 * pHp
            self.ds = alpha * p
            self.dr = alpha * Hp

            # Move to next iterate.
            s += self.ds
            r += self.dr
            y = prec(r)
            ry_next = np.dot(r, y)
            beta = ry_next / ry
            p *= beta
            p -= y  # p = -y + beta * p
            ry = ry_next

            # Transfer useful quantities for post iteration.
            self.pHp = pHp
            self.r = r
            self.y = y
            self.p = p
            self.step = s
            self.step_norm2 = snorm2
            self.ry = ry
            self.alpha = alpha
            self.beta = beta

            sqrtry = sqrt(ry)
            snorm2 = np.dot(s, s)

            try:
                self.post_iteration()

# 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

            self.qval += alpha * np.dot(r, p) + 0.5 * alpha**2 * pHp
            self.ds = alpha * p
            self.dr = alpha * Hp

            # Move to next iterate.
            s += self.ds
            r += self.dr
            y = prec(r)
            ry_next = np.dot(r, y)
            beta = ry_next / ry
            p *= beta
            p -= y  # p = -y + beta * p
            ry = ry_next

            # Transfer useful quantities for post iteration.
            self.pHp = pHp
            self.r = r
            self.y = y
            self.p = p
            self.step = s
            self.step_norm2 = snorm2
            self.ry = ry
            self.alpha = alpha
            self.beta = beta

            sqrtry = sqrt(ry)
            snorm2 = np.dot(s, s)

            try:
                self.post_iteration()

            except UserExitRequest: