def test_hs28(): # Hock-Schittkowski problem 28 (linearly constrained) def func(p): y = np.empty(3) t1 = p[0] + p[1] t2 = p[1] + p[2] y[:] = t1*t1 + t2*t2 return y def jacf(p): j = np.empty((3, 3)) t1 = p[0] + p[1] t2 = p[1] + p[2] j[:, 0] = 2.0 * t1 j[:, 1] = 2.0 * (t1 + t2) j[:, 2] = 2.0 * t2 return j y = np.zeros(3) p0 = [-4.0, 1.0, 1.0] pt = [ 0.5, -0.5, 0.5] A = np.asarray([1.0, 2.0, 3.0]) b = np.asarray([1.0]) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), jacf=jacf, **OPTS) assert_array_almost_equal(p, pt, decimal=5) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), **OPTS) assert_array_almost_equal(p, pt, decimal=4) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), cdiff=True, **OPTS) assert_array_almost_equal(p, pt, decimal=3)
def test_hs48(): # Hock-Schittkowski problem 48 (linearly constrained) def func(p): y = np.empty(5) t1 = p[0] - 1.0 t2 = p[1] - p[2] t3 = p[3] - p[4] y[:] = t1*t1 + t2*t2 + t3*t3 return y def jacf(p): j = np.empty((5, 5)) t1 = p[0] - 1.0 t2 = p[1] - p[2] t3 = p[3] - p[4] j[:, 0] = 2.0*t1 j[:, 1] = 2.0*t2 j[:, 2] = -2.0*t2 j[:, 3] = 2.0*t3 j[:, 4] = -2.0*t3 return j y = np.zeros(5, np.float64) p0 = [3.0, 5.0, -3.0, 2.0, -2.0] pt = [1.0, 1.0, 1.0, 1.0, 1.0] A = np.asarray([[1.0, 1.0, 1.0, 1.0, 1.0], [0.0, 0.0, 1.0, -2.0, -2.0]]) b = np.asarray([5.0, -3.0]) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), jacf=jacf, **OPTS) assert_array_almost_equal(p, pt, decimal=5) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), **OPTS) assert_array_almost_equal(p, pt, decimal=5) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), cdiff=True, **OPTS) assert_array_almost_equal(p, pt, decimal=5)
def test_hs48(): # Hock-Schittkowski problem 48 (linearly constrained) def func(p): y = np.empty(5) t1 = p[0] - 1.0 t2 = p[1] - p[2] t3 = p[3] - p[4] y[:] = t1 * t1 + t2 * t2 + t3 * t3 return y def jacf(p): j = np.empty((5, 5)) t1 = p[0] - 1.0 t2 = p[1] - p[2] t3 = p[3] - p[4] j[:, 0] = 2.0 * t1 j[:, 1] = 2.0 * t2 j[:, 2] = -2.0 * t2 j[:, 3] = 2.0 * t3 j[:, 4] = -2.0 * t3 return j y = np.zeros(5, np.float64) p0 = [3.0, 5.0, -3.0, 2.0, -2.0] pt = [1.0, 1.0, 1.0, 1.0, 1.0] A = np.asarray([[1.0, 1.0, 1.0, 1.0, 1.0], [0.0, 0.0, 1.0, -2.0, -2.0]]) b = np.asarray([5.0, -3.0]) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), jacf=jacf, **OPTS) assert_array_almost_equal(p, pt, decimal=5) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), **OPTS) assert_array_almost_equal(p, pt, decimal=5) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), cdiff=True, **OPTS) assert_array_almost_equal(p, pt, decimal=5)
def test_hs28(): # Hock-Schittkowski problem 28 (linearly constrained) def func(p): y = np.empty(3) t1 = p[0] + p[1] t2 = p[1] + p[2] y[:] = t1 * t1 + t2 * t2 return y def jacf(p): j = np.empty((3, 3)) t1 = p[0] + p[1] t2 = p[1] + p[2] j[:, 0] = 2.0 * t1 j[:, 1] = 2.0 * (t1 + t2) j[:, 2] = 2.0 * t2 return j y = np.zeros(3) p0 = [-4.0, 1.0, 1.0] pt = [0.5, -0.5, 0.5] A = np.asarray([1.0, 2.0, 3.0]) b = np.asarray([1.0]) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), jacf=jacf, **OPTS) assert_array_almost_equal(p, pt, decimal=5) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), **OPTS) assert_array_almost_equal(p, pt, decimal=4) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), cdiff=True, **OPTS) assert_array_almost_equal(p, pt, decimal=3)
def test_hs51(): # Hock-Schittkowski problem 51 (linearly constrained) def func(p): y = np.empty(5) t1 = p[0] - p[1] t2 = p[1] + p[2] - 2.0 t3 = p[3] - 1.0 t4 = p[4] - 1.0 y[:] = t1*t1 + t2*t2 + t3*t3 + t4*t4 return y def jacf(p): j = np.empty((5,5)) t1 = p[0] - p[1] t2 = p[1] + p[2] - 2.0 t3 = p[3] - 1.0 t4 = p[4] - 1.0 j[:,0] = 2.0*t1 j[:,1] = 2.0*(t2-t1) j[:,2] = 2.0*t2 j[:,3] = 2.0*t3 j[:,4] = 2.0*t4 return j y = np.zeros(5, np.float64) p0 = [2.5, 0.5, 2.0, -1.0, 0.5] pt = [1.0, 1.0, 1.0, 1.0, 1.0] A = np.asarray([[1.0, 3.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.0, -2.0], [0.0, 1.0, 0.0, 0.0, -1.0]]) b = np.asarray([4.0, 0.0, 0.0]) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), jacf=jacf, **OPTS) assert_array_almost_equal(p, pt, decimal=4) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), **OPTS) assert_array_almost_equal(p, pt, decimal=3) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), cdiff=True, **OPTS) assert_array_almost_equal(p, pt, decimal=3)
def test_bt3(): # Boggs-Tolle problem 3 (linearly constrained) def func(p): y = np.empty(5) t1 = p[0] - p[1] t2 = p[1] + p[2] - 2.0 t3 = p[3] - 1.0 t4 = p[4] - 1.0 y[:] = t1*t1 + t2*t2 + t3*t3 + t4*t4 return y def jacf(p): j = np.empty((5, 5)) t1 = p[0] - p[1] t2 = p[1] + p[2] - 2.0 t3 = p[3] - 1.0 t4 = p[4] - 1.0 j[:, 0] = 2.0 * t1 j[:, 1] = 2.0 * (t2 - t1) j[:, 2] = 2.0 * t2 j[:, 3] = 2.0 * t3 j[:, 4] = 2.0 * t4 return j y = np.zeros(5) p0 = [2.0, 2.0, 2.0, 2.0, 2.0] pt = [-0.76744, 0.25581, 0.62791, -0.11628, 0.25581] A = np.asarray([[1.0, 3.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.0, -2.0], [0.0, 1.0, 0.0, 0.0, -1.0]]) b = np.asarray([0.0, 0.0, 0.0]) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), jacf=jacf, **OPTS) assert_array_almost_equal(p, pt, decimal=4) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), **OPTS) assert_array_almost_equal(p, pt, decimal=4) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), cdiff=True, **OPTS) assert_array_almost_equal(p, pt, decimal=4)
def test_hs51(): # Hock-Schittkowski problem 51 (linearly constrained) def func(p): y = np.empty(5) t1 = p[0] - p[1] t2 = p[1] + p[2] - 2.0 t3 = p[3] - 1.0 t4 = p[4] - 1.0 y[:] = t1 * t1 + t2 * t2 + t3 * t3 + t4 * t4 return y def jacf(p): j = np.empty((5, 5)) t1 = p[0] - p[1] t2 = p[1] + p[2] - 2.0 t3 = p[3] - 1.0 t4 = p[4] - 1.0 j[:, 0] = 2.0 * t1 j[:, 1] = 2.0 * (t2 - t1) j[:, 2] = 2.0 * t2 j[:, 3] = 2.0 * t3 j[:, 4] = 2.0 * t4 return j y = np.zeros(5, np.float64) p0 = [2.5, 0.5, 2.0, -1.0, 0.5] pt = [1.0, 1.0, 1.0, 1.0, 1.0] A = np.asarray([[1.0, 3.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.0, -2.0], [0.0, 1.0, 0.0, 0.0, -1.0]]) b = np.asarray([4.0, 0.0, 0.0]) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), jacf=jacf, **OPTS) assert_array_almost_equal(p, pt, decimal=4) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), **OPTS) assert_array_almost_equal(p, pt, decimal=3) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), cdiff=True, **OPTS) assert_array_almost_equal(p, pt, decimal=3)
def test_bt3(): # Boggs-Tolle problem 3 (linearly constrained) def func(p): y = np.empty(5) t1 = p[0] - p[1] t2 = p[1] + p[2] - 2.0 t3 = p[3] - 1.0 t4 = p[4] - 1.0 y[:] = t1 * t1 + t2 * t2 + t3 * t3 + t4 * t4 return y def jacf(p): j = np.empty((5, 5)) t1 = p[0] - p[1] t2 = p[1] + p[2] - 2.0 t3 = p[3] - 1.0 t4 = p[4] - 1.0 j[:, 0] = 2.0 * t1 j[:, 1] = 2.0 * (t2 - t1) j[:, 2] = 2.0 * t2 j[:, 3] = 2.0 * t3 j[:, 4] = 2.0 * t4 return j y = np.zeros(5) p0 = [2.0, 2.0, 2.0, 2.0, 2.0] pt = [-0.76744, 0.25581, 0.62791, -0.11628, 0.25581] A = np.asarray([[1.0, 3.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.0, -2.0], [0.0, 1.0, 0.0, 0.0, -1.0]]) b = np.asarray([0.0, 0.0, 0.0]) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), jacf=jacf, **OPTS) assert_array_almost_equal(p, pt, decimal=4) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), **OPTS) assert_array_almost_equal(p, pt, decimal=4) p, pcov, info = levmar.levmar_lec(func, p0, y, (A, b), cdiff=True, **OPTS) assert_array_almost_equal(p, pt, decimal=4)