Пример #1
0
def test_scipy(method: str, method_options: Options, compute_jacobian: bool, use_weights: bool) -> None:
    """Test that the solution to the example fixed point problem from scipy.optimize.fixed_point is reasonably close to
    the exact solution. Also verify that the configuration can be formatted.
    """
    def contraction(x: Array) -> ContractionResults:
        """Evaluate the contraction."""
        c1 = np.array([10, 12])
        c2 = np.array([3, 5])
        x0, x = x, np.sqrt(c1 / (x + c2))
        weights = np.ones_like(x) if use_weights else None
        jacobian = -0.5 * np.eye(2) * x / (x0 + c2) if compute_jacobian else None
        return x, weights, jacobian

    # simple methods do not accept an analytic Jacobian
    if compute_jacobian and method not in {'hybr', 'lm'}:
        return pytest.skip("This method does not accept an analytic Jacobian.")

    # initialize the configuration and test that it can be formatted
    iteration = Iteration(method, method_options, compute_jacobian)
    assert str(iteration)

    # define the exact solution
    exact_values = np.array([1.4920333, 1.37228132])

    # test that the solution is reasonably close (use the exact values if the iteration routine will just return them)
    start_values = exact_values if method == 'return' else np.ones_like(exact_values)
    computed_values, stats = iteration._iterate(start_values, contraction)
    assert stats.converged
    np.testing.assert_allclose(exact_values, computed_values, rtol=0, atol=1e-5)
Пример #2
0
def test_scipy(method: Union[str, Callable], method_options: Options, tol: float) -> None:
    """Test that the solution to the example fixed point problem from scipy.optimize.fixed_point is reasonably close to
    the exact solution. Also verify that the configuration can be formatted.
    """

    # test that the configuration can be formatted
    if method != 'return':
        method_options = method_options.copy()
        method_options['tol'] = tol
    iteration = Iteration(method, method_options)
    assert str(iteration)

    # test that the solution is reasonably close (use the exact values if the iteration routine will just return them)
    contraction = lambda x: np.sqrt(np.array([10, 12]) / (x + np.array([3, 5])))
    exact_values = np.array([1.4920333, 1.37228132])
    start_values = exact_values if method == 'return' else np.ones_like(exact_values)
    computed_values = iteration._iterate(start_values, contraction)[0]
    np.testing.assert_allclose(exact_values, computed_values, rtol=0, atol=10 * tol)