# Path: pybobyqa/util.py
# def sumsq(x):
#     # There are several ways to calculate sum of squares of a vector:
#     #   np.dot(x,x)
#     #   np.sum(x**2)
#     #   np.sum(np.square(x))
#     #   etc.
#     # Using the timeit routine, it seems like dot(x,x) is ~3-4x faster than other methods
#     return np.dot(x, x)


import numpy as np
import unittest
import pybobyqa
from pybobyqa.util import sumsq
        print(rosenbrock_hessian(soln.x))
        self.assertTrue(abs(soln.f - fmin) < 1e-4, "Wrong fmin")


class TestRosenbrockBounds2(unittest.TestCase):
    # Minimise the (2d) Rosenbrock function, where x[0] hits upper bound
    def runTest(self):
        # n, m = 2, 2
        x0 = np.array([-1.2, 0.7])  # standard start point too close to upper bounds
        lower = np.array([-2.0, -2.0])
        upper = np.array([0.9, 0.9])
        xmin = np.array([0.9, 0.81])  # approximate
        fmin = rosenbrock(xmin)
        np.random.seed(0)
        soln = pybobyqa.solve(rosenbrock, x0, bounds=(lower, upper))
        self.assertTrue(array_compare(soln.x, xmin, thresh=1e-2), "Wrong xmin")
        self.assertTrue(abs(soln.f - fmin) < 1e-4, "Wrong fmin")
        self.assertTrue(array_compare(soln.gradient, rosenbrock_gradient(soln.x), thresh=1e-2), "Wrong gradient")
        # Hessian entries are quite large, O(100-1000), so can have a fairly large tolerance (use relative terms)
        self.assertLessEqual(np.max(np.abs(rosenbrock_hessian(soln.x) / soln.hessian)) - 1, 1e-1, "Wrong Hessian")
        # self.assertTrue(array_compare(soln.hessian, rosenbrock_hessian(soln.x), thresh=1e-0), "Wrong Hessian")


class TestLinear(unittest.TestCase):
    # Solve min_x ||Ax-b||^2, for some random A and b
    def runTest(self):
        n, m = 2, 5
        np.random.seed(0)  # (fixing random seed)
        A = np.random.rand(m, n)
        b = np.random.rand(m)

# Path: pybobyqa/util.py
# def sumsq(x):
#     # There are several ways to calculate sum of squares of a vector:
#     #   np.dot(x,x)
#     #   np.sum(x**2)
#     #   np.sum(np.square(x))
#     #   etc.
#     # Using the timeit routine, it seems like dot(x,x) is ~3-4x faster than other methods
#     return np.dot(x, x)


# Path: pybobyqa/tests/test_solver.py
import numpy as np
import unittest
import pybobyqa
from pybobyqa.util import sumsq

        print(rosenbrock_hessian(soln.x))
        self.assertTrue(abs(soln.f - fmin) < 1e-4, "Wrong fmin")


class TestRosenbrockBounds2(unittest.TestCase):
    # Minimise the (2d) Rosenbrock function, where x[0] hits upper bound
    def runTest(self):
        # n, m = 2, 2
        x0 = np.array([-1.2, 0.7])  # standard start point too close to upper bounds
        lower = np.array([-2.0, -2.0])
        upper = np.array([0.9, 0.9])
        xmin = np.array([0.9, 0.81])  # approximate
        fmin = rosenbrock(xmin)
        np.random.seed(0)
        soln = pybobyqa.solve(rosenbrock, x0, bounds=(lower, upper))
        self.assertTrue(array_compare(soln.x, xmin, thresh=1e-2), "Wrong xmin")
        self.assertTrue(abs(soln.f - fmin) < 1e-4, "Wrong fmin")
        self.assertTrue(array_compare(soln.gradient, rosenbrock_gradient(soln.x), thresh=1e-2), "Wrong gradient")
        # Hessian entries are quite large, O(100-1000), so can have a fairly large tolerance (use relative terms)
        self.assertLessEqual(np.max(np.abs(rosenbrock_hessian(soln.x) / soln.hessian)) - 1, 1e-1, "Wrong Hessian")
        # self.assertTrue(array_compare(soln.hessian, rosenbrock_hessian(soln.x), thresh=1e-0), "Wrong Hessian")


class TestLinear(unittest.TestCase):
    # Solve min_x ||Ax-b||^2, for some random A and b
    def runTest(self):
        n, m = 2, 5
        np.random.seed(0)  # (fixing random seed)
        A = np.random.rand(m, n)
        b = np.random.rand(m)

        objfun = lambda x: sumsq(np.dot(A, x) - b)