<commit_before>/*=============================================================================

    This file is part of FLINT.

    FLINT 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 2 of the License, or
    (at your option) any later version.

    FLINT 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 FLINT; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

=============================================================================*/
/******************************************************************************

    Copyright (C) 2011 <NAME>

******************************************************************************/

#include <mpir.h>
#include "flint.h"
#include "ulong_extras.h"
#include "nmod_vec.h"
#include "nmod_poly.h"


static __inline__ void
__nmod_poly_exp_series_prealloc(mp_ptr f, mp_ptr g, mp_srcptr h,
    mp_srcptr hprime, mp_ptr T, mp_ptr U, long n, nmod_t mod)
{
    long m, m2, l;

    if (n <= 2)
    {
        if (n >= 1)
            f[0] = g[0] = 1UL;
        if (n == 2)
        {
            f[1] = h[1];
            g[1] = n_negmod(h[1], mod.n);
        }
        return;
    }

    m = (n + 1) / 2;
    m2 = (m + 1) / 2;
    l = m - 1;  /* shifted for derivative */

    /* f := exp(h) + O(x^m),  g := exp(-h) + O(x^m2) */
    __nmod_poly_exp_series_prealloc(f, g, h, hprime, T, U, m, mod);

    /* g := exp(-h) + O(x^m) */
    _nmod_poly_mullow(T, f, n, g, m2, m, mod);
    _nmod_poly_mullow(U + m2, g, m2, T + m2, m - m2, m - m2, mod);
    _nmod_vec_neg(g + m2, U + m2, m - m2, mod);

    /* U := h' + g (f' - f h') + O(x^(n-1))
       Note: should replace h' by h' mod x^(m-1) */
    _nmod_vec_zero(f + m, n - m);
    _nmod_poly_mullow(T, f, n, hprime, n, n, mod);  /* should be mulmid */
    _nmod_poly_derivative(U, f, n, mod);            /* should skip low terms */
    _nmod_vec_sub(U + l, U + l, T + l, n - l, mod);
    _nmod_poly_mullow(T + l, g, n, U + l, n - l, n - l, mod);
    _nmod_vec_add(U + l, hprime + l, T + l, n - l, mod);

    /* f := f + f * (h - int U) + O(x^n) = exp(h) + O(x^n) */
    _nmod_poly_integral(U, U, n, mod);  /* should skip low terms */
    _nmod_vec_sub(U + m, h + m, U + m, n - m, mod);
    _nmod_poly_mullow(T + m, f, n - m, U + m, n - m, n - m, mod);
    _nmod_vec_set(f + m, T + m, n - m);
}

void
_nmod_poly_exp_series(mp_ptr f, mp_srcptr h, long n, nmod_t mod)
{
    mp_ptr g, T, U, hprime;

    g = _nmod_vec_init((n + 1) / 2);<commit_msg>Avoid some redundant operations in nmod_poly_exp_series<commit_after>/*=============================================================================

    This file is part of FLINT.

    FLINT 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 2 of the License, or
    (at your option) any later version.

    FLINT 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 FLINT; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

=============================================================================*/
/******************************************************************************

    Copyright (C) 2011 <NAME>

******************************************************************************/

#include <mpir.h>
#include "flint.h"
#include "ulong_extras.h"
#include "nmod_vec.h"
#include "nmod_poly.h"


static void
__nmod_poly_exp_series_prealloc(mp_ptr f, mp_ptr g, mp_srcptr h,
    mp_srcptr hprime, mp_ptr T, mp_ptr U, long n, nmod_t mod)
{
    long m, m2, l;

    if (n <= 2)
    {
        if (n >= 1)
            f[0] = g[0] = 1UL;
        if (n == 2)
        {
            f[1] = h[1];
            g[1] = n_negmod(h[1], mod.n);
        }
        return;
    }

    m = (n + 1) / 2;
    m2 = (m + 1) / 2;
    l = m - 1;  /* shifted for derivative */

    /* f := exp(h) + O(x^m),  g := exp(-h) + O(x^m2) */
    __nmod_poly_exp_series_prealloc(f, g, h, hprime, T, U, m, mod);

    /* g := exp(-h) + O(x^m) */
    _nmod_poly_mullow(T, f, n, g, m2, m, mod);
    _nmod_poly_mullow(g + m2, g, m2, T + m2, m - m2, m - m2, mod);
    _nmod_vec_neg(g + m2, g + m2, m - m2, mod);

    /* U := h' + g (f' - f h') + O(x^(n-1))
       Note: should replace h' by h' mod x^(m-1) */
    _nmod_vec_zero(f + m, n - m);
    _nmod_poly_mullow(T, f, n, hprime, n, n, mod);  /* should be mulmid */
    _nmod_poly_derivative(U, f, n, mod);            /* should skip low terms */
    _nmod_vec_sub(U + l, U + l, T + l, n - l, mod);
    _nmod_poly_mullow(T + l, g, n, U + l, n - l, n - l, mod);
    _nmod_vec_add(U + l, hprime + l, T + l, n - l, mod);

    /* f := f + f * (h - int U) + O(x^n) = exp(h) + O(x^n) */
    _nmod_poly_integral(U, U, n, mod);  /* should skip low terms */
    _nmod_vec_sub(U + m, h + m, U + m, n - m, mod);
    _nmod_poly_mullow(f + m, f, n - m, U + m, n - m, n - m, mod);
}

void
_nmod_poly_exp_series(mp_ptr f, mp_srcptr h, long n, nmod_t mod)
{
    mp_ptr g, T, U, hprime;

    g = _nmod_vec_init((n + 1) / 2);
{'fraction_non_alphanumeric': 0.23645490739137962, 'fraction_numerical': 0.0162714211528475, 'mean_word_length': 2.692012779552716, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '5411508', 'n_tokens_mistral': 2225, 'n_tokens_neox': 2106, 'n_words': 709}