<commit_before>"""
SciPy-based function optimization
"""

import numpy as np
import scipy

from autodiff.symbolic import VectorArg
import autodiff.utils as utils


__all__ = ['fmin_cg', 'fmin_ncg', 'fmin_l_bfgs_b']


def fmin_cg(fn,
            init_args,
            **scipy_kwargs):
    """
    Minimize a scalar valued function using SciPy's nonlinear conjugate
    gradient algorithm. The initial parameter guess is 'init_args'.

    """

    init_args = utils.as_seq(init_args, tuple)
    f = VectorArg(fn, init_args=init_args, compile_fn=True)
    fprime = VectorArg(fn, init_args=init_args, compile_grad=True)
    x0 = f.vector_from_args(init_args)

    x_opt = scipy.optimize.fmin_cg(
        f=f,
        x0=x0,
        fprime=fprime,
        full_output=False,
        **scipy_kwargs)

    x_reshaped = f.args_from_vector(x_opt)
    if len(x_reshaped) == 1:
        x_reshaped = x_reshaped[0]

    return x_reshaped


def fmin_ncg(fn,
             init_args,
             **scipy_kwargs):
    """
    Minimize a scalar valued function using SciPy's Newton-CG algorithm. The
    initial parameter guess is 'init_args'.

    """

    init_args = utils.as_seq(init_args, tuple)
    f = VectorArg(fn, init_args=init_args, compile_fn=True)
    fprime = VectorArg(fn, init_args=init_args, compile_grad=True)
    fhess_p = VectorArg(fn, init_args=init_args, compile_hv=True)
    x0 = f.vector_from_args(init_args)

    x_opt = scipy.optimize.fmin_ncg(
        f=f,
        x0=x0,
        fprime=fprime,
        fhess_p=fhess_p,
        full_output=False,
        **scipy_kwargs)

    x_reshaped = f.args_from_vector(x_opt)
    if len(x_reshaped) == 1:
        x_reshaped = x_reshaped[0]

    return x_reshaped

<commit_msg>collapse whitespace<commit_after>"""
SciPy-based function optimization
"""

import numpy as np
import scipy

from autodiff.symbolic import VectorArg
import autodiff.utils as utils


__all__ = ['fmin_cg', 'fmin_ncg', 'fmin_l_bfgs_b']


def fmin_cg(fn, init_args, **scipy_kwargs):
    """
    Minimize a scalar valued function using SciPy's nonlinear conjugate
    gradient algorithm. The initial parameter guess is 'init_args'.

    """

    init_args = utils.as_seq(init_args, tuple)
    f = VectorArg(fn, init_args=init_args, compile_fn=True)
    fprime = VectorArg(fn, init_args=init_args, compile_grad=True)
    x0 = f.vector_from_args(init_args)

    x_opt = scipy.optimize.fmin_cg(
        f=f,
        x0=x0,
        fprime=fprime,
        full_output=False,
        **scipy_kwargs)

    x_reshaped = f.args_from_vector(x_opt)
    if len(x_reshaped) == 1:
        x_reshaped = x_reshaped[0]

    return x_reshaped


def fmin_ncg(fn, init_args, **scipy_kwargs):
    """
    Minimize a scalar valued function using SciPy's Newton-CG algorithm. The
    initial parameter guess is 'init_args'.

    """

    init_args = utils.as_seq(init_args, tuple)
    f = VectorArg(fn, init_args=init_args, compile_fn=True)
    fprime = VectorArg(fn, init_args=init_args, compile_grad=True)
    fhess_p = VectorArg(fn, init_args=init_args, compile_hv=True)
    x0 = f.vector_from_args(init_args)

    x_opt = scipy.optimize.fmin_ncg(
        f=f,
        x0=x0,
        fprime=fprime,
        fhess_p=fhess_p,
        full_output=False,
        **scipy_kwargs)

    x_reshaped = f.args_from_vector(x_opt)
    if len(x_reshaped) == 1:
        x_reshaped = x_reshaped[0]

    return x_reshaped


{'fraction_non_alphanumeric': 0.09917840375586855, 'fraction_numerical': 0.005868544600938967, 'mean_word_length': 3.1072289156626507, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6845864', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1257, 'n_words': 251}