<commit_before>from __future__ import division, print_function, absolute_import
import warnings
import cython
cimport cpython
cimport c_zeros

cdef double TOL = 1.48e-8
cdef int MAXITER = 50


# the new standard callback function that uses the params struct instead of tuple
@staticmethod
cdef double scipy_zeros_functions_func(double x, c_zeros.scipy_zeros_parameters *params):
    cdef c_zeros.scipy_zeros_parameters *myparams
    cdef tuple args
    cdef callback_type_tup f
    cdef double retval

    myparams = params
    args = <object > <void *> myparams.args
    #cpython.Py_XINCREF(args)
    f = myparams.function

    return f(x, args)  # recall callback_type takes a double and a tuple


@cython.cdivision(True)
cdef double newton(callback_type_tup func, double p0, callback_type_tup fprime, tuple args):
    # Newton-Rapheson method
    cdef double fder, fval, p
    for iter in range(MAXITER):
        fder = fprime(p0, args)
        if fder == 0:
            msg = "derivative was zero."
            warnings.warn(msg, RuntimeWarning)
            return p0
        fval = func(p0, args)
        # Newton step
        p = p0 - fval / fder
        if abs(p - p0) < TOL:  # np_abs(p - p0).max() < tol:
            return p<commit_msg>use tuple instead of object, either increments automatically<commit_after>from __future__ import division, print_function, absolute_import
import warnings
import cython
cimport cpython
cimport c_zeros

cdef double TOL = 1.48e-8
cdef int MAXITER = 50


# the new standard callback function that uses the params struct instead of tuple
@staticmethod
cdef double scipy_zeros_functions_func(double x, c_zeros.scipy_zeros_parameters *params):
    cdef c_zeros.scipy_zeros_parameters *myparams
    cdef tuple args
    cdef callback_type_tup f

    myparams = params
    args = <tuple> myparams.args if myparams.args is not NULL else ()
    #cpython.Py_XINCREF(args)
    f = myparams.function

    return f(x, args)  # recall callback_type takes a double and a tuple


@cython.cdivision(True)
cdef double newton(callback_type_tup func, double p0, callback_type_tup fprime, tuple args):
    # Newton-Rapheson method
    cdef double fder, fval, p
    for iter in range(MAXITER):
        fder = fprime(p0, args)
        if fder == 0:
            msg = "derivative was zero."
            warnings.warn(msg, RuntimeWarning)
            return p0
        fval = func(p0, args)
        # Newton step
        p = p0 - fval / fder
        if abs(p - p0) < TOL:  # np_abs(p - p0).max() < tol:
            return p
{'fraction_non_alphanumeric': 0.07199055861526357, 'fraction_numerical': 0.011014948859166011, 'mean_word_length': 2.948757763975155, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 6, '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': '5887403', 'n_tokens_mistral': 850, 'n_tokens_neox': 831, 'n_words': 264}