예제 #1
0
def test_gauss_newton_nonlinear_overdetermined_solve():
    """Test that gauss_newton solves nonlinear, overdetermined systems."""
    # The function being optimized is c0 + c1^2 * x^2 + c2^3 * x^3
    x0 = np.array([20., 2.5, 500.])
    result = nonlinear_solve(f3, x0, j3, method="gaussnewton")
    f_expt = np.array([0.] * 4)
    x_expt = np.array([2., 3., 5.])
    message_expt = "Convergence obtained."
    jac_expt = j3(x_expt)
    assert np.allclose(f_expt, result['f'], rtol=1e-5, atol=1e-5)
    assert np.allclose(jac_expt, result['J'], rtol=1e-5, atol=1e-5)
    assert np.allclose(x_expt, result['x'])
    assert result['success']
    assert result['message'] == message_expt
    # Because of symmetry the second coefficient of -3 should work
    x0 = np.array([1000., -200., 500.])
    result = nonlinear_solve(f3, x0, j3, method="gaussnewton")
    x_expt = np.array([2., -3., 5.])
    message_expt = "Convergence obtained."
    jac_expt = j3(x_expt)
    assert np.allclose(f_expt, result['f'], rtol=1e-5, atol=1e-5)
    assert np.allclose(jac_expt, result['J'], rtol=1e-5, atol=1e-5)
    assert np.allclose(x_expt, result['x'])
    assert result['success']
    assert result['message'] == message_expt
예제 #2
0
def test_gauss_newton_nonlinear_solve():
    """Test that gauss_newton solves nonlinear systems."""
    # The function being optimized is c0 + c1^2 * x^2 + c2^3 * x^3
    # The points are evaluated on [1., 2., 3.]
    x0 = np.array([20., 2.5, 500.])
    result = nonlinear_solve(f1, x0, j1, maxiter=5000, method="gaussnewton")
    f_expt = np.array([0.] * 3)
    x_expt = np.array([2., 3., 5.])
    message_expt = "Convergence obtained."
    jac_expt = j1(x_expt)
    assert np.allclose(f_expt, result['f'], rtol=1e-5, atol=1e-5)
    assert np.allclose(jac_expt, result['J'], rtol=1e-5, atol=1e-5)
    assert np.allclose(x_expt, result['x'])
    assert result['success']
    assert result['message'] == message_expt
    # Because of symmetry the second coefficient of -3 should work
    x0 = np.array([200., -200., 200.])
    result = nonlinear_solve(f1, x0, j1, maxiter=5000, method="gaussnewton")
    x_expt = np.array([2., -3., 5.])
    jac_expt = j1(x_expt)
    assert np.allclose(f_expt, result['f'], rtol=1e-5, atol=1e-5)
    assert np.allclose(jac_expt, result['J'], rtol=1e-5, atol=1e-5)
    assert np.allclose(x_expt, result['x'])
    assert result['success']
    assert result['message'] == message_expt
예제 #3
0
def test_nonlinear_singular_matrix_error():
    """Test that newton raises error when Jacobian is singular."""
    x0 = np.array([5., 5.])
    result = nonlinear_solve(f7, x0, j7)
    message_expt = "Singular Jacobian; no solution found."
    assert not result['success']
    assert result["message"] == message_expt
    assert result["eps"] == 1e-6
예제 #4
0
def test_nonlinear_no_solution1():
    """Test that no solution in nonlinear_solve raises error."""
    x_0 = np.array([1., 1.])
    result = nonlinear_solve(f6, x_0, j6, eps=1.0e-9, maxiter=100)
    assert not result["success"]
    assert not np.allclose(result["f"], [0., 0.], atol=1.0e-3)
    assert result["message"] == "Maximum number of iterations reached."
    assert result["niter"] == 100
예제 #5
0
def test_nonlinear_bfgs():
    """Test that newton solves system with bfgs inverse update."""
    x_0 = np.array([0.5, 0.5])
    result = nonlinear_solve(f5, x_0, j5, stepsize=0.5, eps=1.0e-5, maxiter=10000, method="bfgs")
    assert result["success"]
    assert result["niter"] < 10000
    assert np.allclose(result["f"], [0., 0.], rtol=1.0e-5, atol=1.0e-3)
    assert result["message"] == "Convergence obtained."
    assert np.allclose(result["x"], [1., 0.], rtol=1.0e-5, atol=1.0e-2)
예제 #6
0
def test_nonlinear_dfp():
    """Test that newton solves system with dfp update."""
    x_0 = np.array([3., 1.])
    result = nonlinear_solve(f5, x_0, j5, stepsize=1, eps=1.0e-5, maxiter=1000, method="dfp")
    assert result["success"]
    assert result["message"] == "Convergence obtained."
    assert np.allclose(result["f"], [0., 0.], atol=1.0e-3)
    assert np.allclose(result["x"], [1., 0.], atol=1.0e-3)
    assert result["niter"] < 1000
예제 #7
0
def test_nonlinear_badbroyden():
    """Test that newton solves system with bad broyden update."""
    x_0 = np.array([1.1, 0.5])
    result = nonlinear_solve(f5, x_0, stepsize=1, eps=1.0e-6, maxiter=100, method="badbroyden")
    assert result["success"]
    assert np.allclose(result["f"], [0., 0.], rtol=1.0e-5, atol=1.0e-5)
    assert result["message"] == "Convergence obtained."
    assert result["niter"] < 100
    assert np.allclose(result["x"], [1., 0.], rtol=1.0e-5, atol=1.0e-5)
    assert np.allclose(result["f"], [0., 0.], rtol=1.0e-5, atol=1.0e-5)
예제 #8
0
def test_gauss_newton_nonlinear_underdetermined_solve():
    """Test that gauss_newton solves nonlinear, underdetermined systems."""
    # Non-Exact Initial Guess. Here a really Good Initial guess is needed
    x_expt = np.array([2., 3., 5., 8.])
    f_expt = np.array([0.] * 3)
    x0 = np.array([2.001, 3.001, 5.001, 10.01])
    result = nonlinear_solve(f4, x0, j4, maxiter=12, method="gaussnewton")
    message_expt = "Maximum number of iterations reached."
    assert result['message'] == message_expt
    # Repeat with a higher number of iterations and a really good initial guess
    # Still it only converges to the first decimal place.
    x0 = np.array([2.00001, 3.00001, 5.00001, 8.00001])
    result = nonlinear_solve(f4, x0, j4, maxiter=10000, method="gaussnewton")
    jac_expt = j4(result["x"])
    assert np.allclose(f_expt, result['f'], rtol=1e-5, atol=1e-5)
    assert np.allclose(jac_expt, result['J'], rtol=1e-5, atol=1e-5)
    assert np.allclose(x_expt, result['x'], rtol=1e-1, atol=1e-1)
    assert result['message'] == "Convergence obtained."
    assert result['success']
예제 #9
0
def test_nonlinear_linear_solve():
    """Test that newton solves linear systems in 1 step."""
    x_0 = np.array([1.0, 1.0])
    result = nonlinear_solve(f4, x_0, j1, stepsize=np.array([1., 1.]), eps=1.0e-9, maxiter=1)
    assert result["success"]
    assert result["message"] == "Convergence obtained."
    assert result["niter"] == 1
    assert np.allclose(result["f"], [0., 0.], atol=1.0e-6)
    assert np.allclose(result["x"], [2., 1.], atol=1.0e-9)
    assert np.allclose(result["f"], [0., 0.], atol=1.0e-6)
    assert np.allclose(result["J"], [[1., 1.], [-1., 1]], atol=1.0e-6)
예제 #10
0
def test_nonlinear_nonlinear_solve():
    """Test that newton solves nonlinear systems."""
    x_0 = np.array([0.5, 0.5])
    result = nonlinear_solve(f5, x_0, j5, eps=1.0e-9, maxiter=100)
    assert result["success"]
    assert result["niter"] < 101
    assert np.allclose(result["f"], [0., 0.], atol=1.0e-6)
    assert result["message"] == "Convergence obtained."
    assert result["niter"] < 100
    assert np.allclose(result["x"], [1., 0.], atol=1.0e-6)
    assert np.allclose(result["f"], [0., 0.], atol=1.0e-6)
    assert np.allclose(result["J"], [[3., 1.], [-1., 0]], atol=1.0e-6)
예제 #11
0
def test_gauss_newton_approximation_overdetermined():
    """Test gauss newton using an approximation finite diff Jacobian."""
    x0 = np.array([20., 2.5, 500.])
    result = nonlinear_solve(f3, x0, method="gaussnewton")
    f_expt = np.array([0.] * 4)
    x_expt = np.array([2., 3., 5.])
    message_expt = "Convergence obtained."
    jac_expt = j3(x_expt)
    assert np.allclose(f_expt, result['f'], rtol=1e-5, atol=1e-5)
    assert np.allclose(jac_expt, result['J'], rtol=1e-5, atol=1e-5)
    assert np.allclose(x_expt, result['x'])
    assert result['success']
    assert result['message'] == message_expt
예제 #12
0
def test_gauss_newton_linear_solve():
    """Test that gauss_newton solves linear systems in 1 step."""
    # Obtained from pg 489 of Numerical Mathematics and Computing Sixth Edition.
    x0 = np.array([100., -200.])
    result = nonlinear_solve(f_lin, x0, j_lin, eps=1e-20, method="gaussnewton")
    x_expt = np.array([0.4864, -1.6589])
    f_expt = np.array([0., 0.])
    jac_expt = j_lin(1.)
    message_expt = "Convergence obtained."
    assert np.allclose(f_expt, result['f'], rtol=1e-5, atol=1e-5)
    assert np.allclose(jac_expt, result['J'], rtol=1e-5, atol=1e-5)
    assert np.allclose(x_expt, result['x'], rtol=1e-4, atol=1e-4)
    assert result['success']
    assert result['message'] == message_expt