def evaluate_quadratic(J, g, s, diag=None):
    if s.ndim == 1:
        Js = J.dot(s)
        q = np.dot(Js, Js)
        if diag is not None:
            q += np.dot(s * diag, s)
    else:
        Js = J.dot(s.T)
        q = np.sum(Js**2, axis=0)
        if diag is not None:
            q += np.sum(diag * s**2, axis=1)

    l = np.dot(s, g)

    return 0.5 * q + l


# Utility functions to work with bound constraints.
Returns a bundle of data built by the related resource, usually via
dataset/ETHPy150Open scipy/scipy/scipy/optimize/_lsq/common.py/evaluate_quadratic--crossover with--dataset/ETHPy150Open hzlf/openbroadcast/website/apps/tastypie__/fields.py/RelatedField.build_related_resource