<commit_before># Copyright 2014-2016 The ODL development group
#
# This file is part of ODL.
#
# ODL is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ODL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ODL.  If not, see <http://www.gnu.org/licenses/>.

"""Test for the smooth solvers."""

# Imports for common Python 2/3 codebase
from __future__ import print_function, division, absolute_import
from future import standard_library
standard_library.install_aliases()

import pytest
import odl
from odl.operator import OpNotImplementedError


@pytest.fixture(scope="module", params=['l2_squared', 'l2_squared_scaled',
                                        'rosenbrock'])
def functional(request):
    """functional with optimum 0 at 0."""
    name = request.param

    # TODO: quadratic (#606) functionals

    if name == 'l2_squared':
        space = odl.rn(3)
        return odl.solvers.L2NormSquared(space)
    elif name == 'l2_squared_scaled':
        space = odl.uniform_discr(0, 1, 3)
        scaling = odl.MultiplyOperator(space.element([1, 2, 3]),
                                       domain=space)
        return odl.solvers.L2NormSquared(space) * scaling
    elif name == 'rosenbrock':
        # Moderately ill-behaved rosenbrock functional.
        rosenbrock = odl.solvers.RosenbrockFunctional(odl.rn(2), scale=2)

        # Center at zero
        return rosenbrock.translated([-1, -1])
    else:
        assert False


@pytest.fixture(scope="module", params=['constant', 'backtracking'])
def functional_and_linesearch(request, functional):
    """Return functional with optimum 0 at 0 and a line search."""
    name = request.param

    if name == 'constant':
        return functional, 1.0
    else:
        return functional, odl.solvers.BacktrackingLineSearch(functional)


@pytest.fixture(scope="module", params=['first', 'second'])
def broyden_impl(request):
    return request.param


def test_newton_solver(functional_and_linesearch):
    """Test the Newton solver."""
    functional, line_search = functional_and_linesearch

    try:
        # Test if derivative exists
        functional.gradient.derivative(functional.domain.zero())
    except OpNotImplementedError:
        return

    # Solving the problem
    x = functional.domain.one()
    odl.solvers.newtons_method(functional, x, tol=1e-6,
                               line_search=line_search)

    # Assert x is close to the optimum at [1, 1]
    assert functional(x) < 1e-3


def test_bfgs_solver(functional_and_linesearch):
    """Test the BFGS quasi-Newton solver."""
    functional, line_search = functional_and_linesearch

    x = functional.domain.one()
    odl.solvers.bfgs_method(functional, x, tol=1e-6,
                            line_search=line_search)

    assert functional(x) < 1e-3


def test_lbfgs_solver(functional_and_linesearch):
    """Test limited memory BFGS quasi-Newton solver."""
    functional, line_search = functional_and_linesearch

    x = functional.domain.one()
    odl.solvers.bfgs_method(functional, x, tol=1e-6,
                            line_search=line_search, num_store=5)

    assert functional(x) < 1e-3


def test_broydens_method(broyden_impl, functional_and_linesearch):
    """Test the ``broydens_method`` quasi-Newton solver."""
    functional, line_search = functional_and_linesearch

    x = functional.domain.one()
    odl.solvers.broydens_method(functional, x, tol=1e-6,
                                line_search=line_search, impl=broyden_impl)

    assert functional(x) < 1e-3


def test_steepest_descent(functional):
    """Test the ``steepest_descent`` solver."""
    line_search = odl.solvers.BacktrackingLineSearch(functional)

    x = functional.domain.one()
    odl.solvers.steepest_descent(functional, x, tol=1e-6,
                                 line_search=line_search)

    assert functional(x) < 1e-3


if __name__ == '__main__':
    pytest.main([str(__file__.replace('\\', '/')), '-v'])<commit_msg>ENH: Add conjugate_gradient_nonlinear to smooth tests<commit_after># Copyright 2014-2016 The ODL development group
#
# This file is part of ODL.
#
# ODL is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ODL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ODL.  If not, see <http://www.gnu.org/licenses/>.

"""Test for the smooth solvers."""

# Imports for common Python 2/3 codebase
from __future__ import print_function, division, absolute_import
from future import standard_library
standard_library.install_aliases()

import pytest
import odl
from odl.operator import OpNotImplementedError


@pytest.fixture(scope="module", params=['l2_squared', 'l2_squared_scaled',
                                        'rosenbrock'])
def functional(request):
    """functional with optimum 0 at 0."""
    name = request.param

    # TODO: quadratic (#606) functionals

    if name == 'l2_squared':
        space = odl.rn(3)
        return odl.solvers.L2NormSquared(space)
    elif name == 'l2_squared_scaled':
        space = odl.uniform_discr(0, 1, 3)
        scaling = odl.MultiplyOperator(space.element([1, 2, 3]),
                                       domain=space)
        return odl.solvers.L2NormSquared(space) * scaling
    elif name == 'rosenbrock':
        # Moderately ill-behaved rosenbrock functional.
        rosenbrock = odl.solvers.RosenbrockFunctional(odl.rn(2), scale=2)

        # Center at zero
        return rosenbrock.translated([-1, -1])
    else:
        assert False


@pytest.fixture(scope="module", params=['constant', 'backtracking'])
def functional_and_linesearch(request, functional):
    """Return functional with optimum 0 at 0 and a line search."""
    name = request.param

    if name == 'constant':
        return functional, 1.0
    else:
        return functional, odl.solvers.BacktrackingLineSearch(functional)


@pytest.fixture(scope="module", params=['first', 'second'])
def broyden_impl(request):
    return request.param


def test_newton_solver(functional_and_linesearch):
    """Test the Newton solver."""
    functional, line_search = functional_and_linesearch

    try:
        # Test if derivative exists
        functional.gradient.derivative(functional.domain.zero())
    except OpNotImplementedError:
        return

    # Solving the problem
    x = functional.domain.one()
    odl.solvers.newtons_method(functional, x, tol=1e-6,
                               line_search=line_search)

    # Assert x is close to the optimum at [1, 1]
    assert functional(x) < 1e-3


def test_bfgs_solver(functional_and_linesearch):
    """Test the BFGS quasi-Newton solver."""
    functional, line_search = functional_and_linesearch

    x = functional.domain.one()
    odl.solvers.bfgs_method(functional, x, tol=1e-6,
                            line_search=line_search)

    assert functional(x) < 1e-3


def test_lbfgs_solver(functional_and_linesearch):
    """Test limited memory BFGS quasi-Newton solver."""
    functional, line_search = functional_and_linesearch

    x = functional.domain.one()
    odl.solvers.bfgs_method(functional, x, tol=1e-6,
                            line_search=line_search, num_store=5)

    assert functional(x) < 1e-3


def test_broydens_method(broyden_impl, functional_and_linesearch):
    """Test the ``broydens_method`` quasi-Newton solver."""
    functional, line_search = functional_and_linesearch

    x = functional.domain.one()
    odl.solvers.broydens_method(functional, x, tol=1e-6,
                                line_search=line_search, impl=broyden_impl)

    assert functional(x) < 1e-3


def test_steepest_descent(functional):
    """Test the ``steepest_descent`` solver."""
    line_search = odl.solvers.BacktrackingLineSearch(functional)

    x = functional.domain.one()
    odl.solvers.steepest_descent(functional, x, tol=1e-6,
                                 line_search=line_search)

    assert functional(x) < 1e-3


def test_conjguate_gradient_nonlinear(functional):
    """Test the ``conjugate_gradient_nonlinear`` solver."""
    line_search = odl.solvers.BacktrackingLineSearch(functional)

    x = functional.domain.one()
    odl.solvers.conjugate_gradient_nonlinear(functional, x, tol=1e-6,
                                             line_search=line_search)

    assert functional(x) < 1e-3


if __name__ == '__main__':
    pytest.main([str(__file__.replace('\\', '/')), '-v'])
{'fraction_non_alphanumeric': 0.10049869904596705, 'fraction_numerical': 0.013443191673894189, 'mean_word_length': 3.1554054054054053, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6945221', 'n_tokens_mistral': 2941, 'n_tokens_neox': 2731, 'n_words': 735}