# Path: nlp/tools/norms.py
# def norm2(x):
#     """Compute 2-norm of `x`."""
#     if len(x) > 0:
#         return norm(x)
#     return 0.0


import numpy as np
import logging
from math import copysign, sqrt
from nlp.tools.norms import norm2
    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


def projected_gradient_norm2(x, g, l, u):
    """Compute the Euclidean norm of the projected gradient at x."""
    lower = where(x == l)
    upper = where(x == u)

    pg = g.copy()
    pg[lower] = np.minimum(g[lower], 0)
    pg[upper] = np.maximum(g[upper], 0)


# Path: nlp/tools/norms.py
# def norm2(x):
#     """Compute 2-norm of `x`."""
#     if len(x) > 0:
#         return norm(x)
#     return 0.0


# Path: nlp/tools/utils.py
import numpy as np
import logging
from math import copysign, sqrt
from nlp.tools.norms import norm2

    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


def projected_gradient_norm2(x, g, l, u):
    """Compute the Euclidean norm of the projected gradient at x."""
    lower = where(x == l)
    upper = where(x == u)

    pg = g.copy()
    pg[lower] = np.minimum(g[lower], 0)
    pg[upper] = np.maximum(g[upper], 0)


    return norm2(pg[where(l != u)])