Exemple #1
0
int
main(void)
{
    int i;
    flint_rand_t state;
    flint_randinit(state);

    printf("dot_ptr....");
    fflush(stdout);

    for (i = 0; i < 10000; i++)
    {
        long len;
        nmod_t mod;
        mp_limb_t m, res, res2;
        mp_ptr x, y;
        mp_ptr * z;
        int limbs1;
        long j, offset;

        len = n_randint(state, 1000) + 1;
        m = n_randtest_not_zero(state);
        offset = n_randint(state, 10);

        nmod_init(&mod, m);

        x = _nmod_vec_init(len);
        y = _nmod_vec_init(len);
        z = flint_malloc(sizeof(mp_ptr) * len);

        _nmod_vec_randtest(x, state, len, mod);
        _nmod_vec_randtest(y, state, len, mod);

        for (j = 0; j < len; j++)
            z[j] = &y[j] + offset;

        limbs1 = _nmod_vec_dot_bound_limbs(len, mod);

        res = _nmod_vec_dot_ptr(x, z, -offset, len, mod, limbs1);
        res2 = _nmod_vec_dot(x, y, len, mod, limbs1);

        if (res != res2)
        {
            printf("FAIL:\n");
            printf("m = %lu\n", m);
            printf("len = %ld\n", len);
            printf("limbs1 = %d\n", limbs1);
            abort();
        }

        _nmod_vec_clear(x);
        _nmod_vec_clear(y);
        flint_free(z);
    }

    flint_randclear(state);

    printf("PASS\n");
    return 0;
}
void
_nmod_poly_revert_series_lagrange(mp_ptr Qinv, mp_srcptr Q, long n, nmod_t mod)
{
    long i;
    mp_ptr R, S, T, tmp;

    if (n >= 1) Qinv[0] = 0UL;
    if (n >= 2) Qinv[1] = n_invmod(Q[1], mod.n);
    if (n <= 2)
        return;

    R = _nmod_vec_init(n - 1);
    S = _nmod_vec_init(n - 1);
    T = _nmod_vec_init(n - 1);

    _nmod_poly_inv_series(R, Q + 1, n - 1, mod);
    _nmod_vec_set(S, R, n - 1);

    for (i = 2; i < n; i++)
    {
        _nmod_poly_mullow(T, S, n - 1, R, n - 1, n - 1, mod);
        Qinv[i] = nmod_div(T[i - 1], i, mod);
        tmp = S; S = T; T = tmp;
    }

    _nmod_vec_clear(R);
    _nmod_vec_clear(S);
    _nmod_vec_clear(T);
}
Exemple #3
0
void
_nmod_poly_exp_series(mp_ptr f, mp_srcptr h, long n, nmod_t mod)
{
    mp_ptr g, T, U, hprime;

    if (n < NMOD_NEWTON_EXP_CUTOFF2)
    {
        _nmod_poly_exp_series_basecase(f, h, n, n, mod);
        return;
    }

    g = _nmod_vec_init((n + 1) / 2);
    T = _nmod_vec_init(n);
    U = _nmod_vec_init(n);
    hprime = _nmod_vec_init(n);

    _nmod_poly_derivative(hprime, h, n, mod);
    hprime[n-1] = 0UL;

    __nmod_poly_exp_series_prealloc(f, g, h, hprime, T, U, n, mod, 0);

    _nmod_vec_free(hprime);
    _nmod_vec_free(g);
    _nmod_vec_free(T);
    _nmod_vec_free(U);
}
Exemple #4
0
void sample(void * arg, ulong count)
{
   mp_limb_t n;
   nmod_t mod;
   info_t * info = (info_t *) arg;
   mp_bitcnt_t bits = info->bits;
   mp_ptr vec = _nmod_vec_init(1000);
   mp_ptr vec2 = _nmod_vec_init(1000);
   mp_size_t j;
   long i;
   flint_rand_t state;
   flint_randinit(state);
    
   for (j = 0; j < 1000; j++)
      vec[j] = n_randlimb(state);

   prof_start();
   for (i = 0; i < count; i++)
   {
      n = n_randbits(state, bits);
      if (n == 0UL) n++;
      
	  nmod_init(&mod, n);
      _nmod_vec_reduce(vec2, vec, 1000, mod);
   }
   prof_stop();
 
   flint_randclear(state);
   _nmod_vec_clear(vec);
   _nmod_vec_clear(vec2);
}
Exemple #5
0
void nmod_poly_divrem_newton(nmod_poly_t Q, nmod_poly_t R, 
                             const nmod_poly_t A, const nmod_poly_t B)
{
    const long lenA = A->length, lenB = B->length;
    mp_ptr q, r;

    if (lenB == 0)
    {
        printf("Exception: division by zero in nmod_poly_divrem_newton\n");
        abort();
    }

    if (lenA < lenB)
    {
        nmod_poly_set(R, A);
        nmod_poly_zero(Q);
        return;
    }

    if (Q == A || Q == B)
    {
        q = _nmod_vec_init(lenA - lenB + 1);
    }
    else
    {
        nmod_poly_fit_length(Q, lenA - lenB + 1);
        q = Q->coeffs;
    }
    if (R == A || R == B)
    {
        r = _nmod_vec_init(lenB - 1);
    }
    else
    {
        nmod_poly_fit_length(R, lenB - 1);
        r = R->coeffs;
    }

    _nmod_poly_divrem_newton(q, r, A->coeffs, lenA,
                                   B->coeffs, lenB, B->mod);

    if (Q == A || Q == B)
    {
        _nmod_vec_clear(Q->coeffs);
        Q->coeffs = q;
        Q->alloc  = lenA - lenB + 1;
    }
    if (R == A || R == B)
    {
        _nmod_vec_clear(R->coeffs);
        R->coeffs = r;
        R->alloc  = lenB - 1;
    }
    Q->length = lenA - lenB + 1;
    R->length = lenB - 1;

    _nmod_poly_normalise(R);
}
void
_nmod_poly_taylor_shift_convolution(mp_ptr p, mp_limb_t c, slong len, nmod_t mod)
{
    slong i, n = len - 1;
    mp_limb_t f, d;
    mp_ptr t, u;

    if (c == 0 || len <= 1)
        return;

    t = _nmod_vec_init(len);
    u = _nmod_vec_init(len);

    f = 1;
    for (i = 2; i <= n; i++)
    {
        f = n_mulmod2_preinv(f, i, mod.n, mod.ninv);
        p[i] = n_mulmod2_preinv(p[i], f, mod.n, mod.ninv);
    }

    _nmod_poly_reverse(p, p, len, len);

    t[n] = 1;
    for (i = n; i > 0; i--)
        t[i - 1] = n_mulmod2_preinv(t[i], i, mod.n, mod.ninv);

    if (c == mod.n - 1)
    {
        for (i = 1; i <= n; i += 2)
            t[i] = nmod_neg(t[i], mod);
    }
    else if (c != 1)
    {
        d = c;

        for (i = 1; i <= n; i++)
        {
            t[i] = n_mulmod2_preinv(t[i], d, mod.n, mod.ninv);
            d = n_mulmod2_preinv(d, c, mod.n, mod.ninv);
        }
    }

    _nmod_poly_mullow(u, p, len, t, len, len, mod);

    f = n_mulmod2_preinv(f, f, mod.n, mod.ninv);
    f = n_invmod(f, mod.n);

    for (i = n; i >= 0; i--)
    {
        p[i] = n_mulmod2_preinv(u[n - i], f, mod.n, mod.ninv);
        f = n_mulmod2_preinv(f, (i == 0) ? 1 : i, mod.n, mod.ninv);
    }

    _nmod_vec_clear(t);
    _nmod_vec_clear(u);
}
int
main(void)
{
    int i, result = 1;
    flint_rand_t state;
    flint_randinit(state);
    
    printf("interpolate_nmod_vec_fast....");
    fflush(stdout);

    for (i = 0; i < 10000; i++)
    {
        nmod_poly_t P, Q;
        mp_ptr x, y;
        mp_limb_t mod;
        long j, n, npoints;

        mod = n_randtest_prime(state, 0);
        npoints = n_randint(state, FLINT_MIN(100, mod));
        n = n_randint(state, npoints + 1);

        nmod_poly_init(P, mod);
        nmod_poly_init(Q, mod);
        x = _nmod_vec_init(npoints);
        y = _nmod_vec_init(npoints);

        nmod_poly_randtest(P, state, n);

        for (j = 0; j < npoints; j++)
            x[j] = j;

        nmod_poly_evaluate_nmod_vec_fast(y, P, x, npoints);
        nmod_poly_interpolate_nmod_vec_fast(Q, x, y, npoints);

        result = nmod_poly_equal(P, Q);

        if (!result)
        {
            printf("FAIL:\n");
            printf("mod=%lu, n=%ld, npoints=%ld\n\n", mod, n, npoints);
            nmod_poly_print(P), printf("\n\n");
            nmod_poly_print(Q), printf("\n\n");
            abort();
        }

        nmod_poly_clear(P);
        nmod_poly_clear(Q);
        _nmod_vec_clear(x);
        _nmod_vec_clear(y);
    }

    flint_randclear(state);

    printf("PASS\n");
    return 0;
}
Exemple #8
0
void
nmod_mat_solve_tril_classical(nmod_mat_t X, const nmod_mat_t L,
                                                const nmod_mat_t B, int unit)
{
    int nlimbs;
    long i, j, n, m;
    nmod_t mod;
    mp_ptr inv, tmp;

    n = L->r;
    m = B->c;
    mod = L->mod;

    if (!unit)
    {
        inv = _nmod_vec_init(n);
        for (i = 0; i < n; i++)
            inv[i] = n_invmod(nmod_mat_entry(L, i, i), mod.n);
    }
    else
        inv = NULL;

    nlimbs = _nmod_vec_dot_bound_limbs(n, mod);
    tmp = _nmod_vec_init(n);

    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
            tmp[j] = nmod_mat_entry(X, j, i);

        for (j = 0; j < n; j++)
        {
            mp_limb_t s;
            s = _nmod_vec_dot(L->rows[j], tmp, j, mod, nlimbs);
            s = nmod_sub(nmod_mat_entry(B, j, i), s, mod);
            if (!unit)
                s = n_mulmod2_preinv(s, inv[j], mod.n, mod.ninv);
            tmp[j] = s;
        }

        for (j = 0; j < n; j++)
            nmod_mat_entry(X, j, i) = tmp[j];
    }

    _nmod_vec_clear(tmp);
    if (!unit)
        _nmod_vec_clear(inv);
}
Exemple #9
0
void _nmod_poly_div_newton(mp_ptr Q, mp_srcptr A, slong lenA, 
                                     mp_srcptr B, slong lenB, nmod_t mod)
{
    const slong lenQ = lenA - lenB + 1;
    mp_ptr Arev, Brev;

    Arev = _nmod_vec_init(2 * lenQ);
    Brev = Arev + lenQ;

    _nmod_poly_reverse(Arev, A + (lenA - lenQ), lenQ, lenQ);

    if (lenB >= lenQ)
    {
        _nmod_poly_reverse(Brev, B + (lenB - lenQ), lenQ, lenQ);
    }
    else
    {
        _nmod_poly_reverse(Brev, B, lenB, lenB);
        flint_mpn_zero(Brev + lenB, lenQ - lenB);
    }

    _nmod_poly_div_series(Q, Arev, Brev, lenQ, mod);

    _nmod_poly_reverse(Q, Q, lenQ, lenQ);

    _nmod_vec_clear(Arev);
}
void partition_function_nmod_vec(mp_ptr res, long len, nmod_t mod)
{
    mp_ptr tmp;
    mp_limb_t r;
    long k, n;

    r = mod.n - 1UL;

    if (len < 1)
        return;

    tmp = _nmod_vec_init(len);
    _nmod_vec_zero(tmp, len);

    tmp[0] = 1UL;

    for (n = k = 1; n + 4*k + 2 < len; k += 2)
    {
        tmp[n] = r;
        tmp[n + k] = r;
        tmp[n + 3*k + 1] = 1UL;
        tmp[n + 4*k + 2] = 1UL;
        n += 6*k + 5;
    }

    if (n < len) tmp[n] = r;
    if (n + k < len) tmp[n + k] = r;
    if (n + 3*k + 1 < len) tmp[n + 3*k + 1] = 1L;

    _nmod_poly_inv_series(res, tmp, len, mod);

    _nmod_vec_free(tmp);
}
Exemple #11
0
int
main(void)
{
    int i, result;
    flint_rand_t state;
    flint_randinit(state);

    printf("reduce....");
    fflush(stdout);

    for (i = 0; i < 10000; i++)
    {
        long j, len = n_randint(state, 100) + 1;
        mp_ptr vec = _nmod_vec_init(len);
        mp_ptr vec2 = _nmod_vec_init(len);

        mp_limb_t n = n_randtest_not_zero(state);
        nmod_t mod;
        nmod_init(&mod, n);

        for (j = 0; j < len; j++)
        {
            vec[j] = n_randtest(state);
            vec2[j] = vec[j];
        }

        _nmod_vec_reduce(vec, vec, len, mod);
        for (j = 0; j < len; j++)
            vec2[j] = n_mod2_preinv(vec2[j], mod.n, mod.ninv);

        result = _nmod_vec_equal(vec, vec2, len);
        if (!_nmod_vec_equal(vec, vec2, len))
        {
            printf("FAIL:\n");
            printf("len = %ld, n = %ld\n", len, n);
            abort();
        }

        _nmod_vec_clear(vec);
        _nmod_vec_clear(vec2);
    }

    flint_randclear(state);

    printf("PASS\n");
    return 0;
}
/*------------------------------------------------------------*/
void check(int opt){
  long i;
  flint_rand_t state;
  flint_randinit(state);
    mp_limb_t n = 65537;

  for (i = 1; i < 200; i+=20){

    mp_ptr powers_inverse_square_q = _nmod_vec_init(2*i-1);
    nmod_poly_t S;
    nmod_poly_init(S, n);

    mp_limb_t q = nmod_poly_find_root(2*i, S->mod);
 
    if (opt == 1){
      nmod_poly_eval_geom_prepare(powers_inverse_square_q, S, q, i);

      sage_output_init(S->mod);
      printf("q = k(%lu)\n", q);
      printf("i = %lu\n", i);
      sage_output_assign_poly(S, "S");
      sage_output_assign_vec(powers_inverse_square_q, 2*i-1, "powers_inverse_square_q");
      printf("powers_inverse_square_q == [1/q^(j^2) for j in range(2*i-1)]\n");
      printf("S == U([q^(j^2) for j in range(2*i-1)])\n");
    }
    else{
      double t, u;
      long j;
      t = util_gettime();
      for (j = 0; j < 10000; j++)
	nmod_poly_eval_geom_prepare(powers_inverse_square_q, S, q, i);
      t = util_gettime() - t;
      
      nmod_poly_t tmp1, tmp2, tmp3;
      nmod_poly_init(tmp1, n);
      nmod_poly_init(tmp2, n);
      nmod_poly_init(tmp3, n);

      nmod_poly_rand_dense(tmp1, state, i);
      nmod_poly_rand_dense(tmp2, state, i);

      u = util_gettime();
      for (j = 0; j < 10000; j++)
	nmod_poly_mul(tmp3, tmp1, tmp2);
      u = util_gettime() - u;

      nmod_poly_clear(tmp1);
      nmod_poly_clear(tmp2);
      nmod_poly_clear(tmp3);
      
      printf("%lu %f %f\n", i, t, u);
    }

    nmod_poly_clear(S);
    _nmod_vec_clear(powers_inverse_square_q);
  }

  flint_randclear(state);
}
Exemple #13
0
void
_nmod_poly_atan_series(mp_ptr g, mp_srcptr h, long n, nmod_t mod)
{
    mp_ptr t, u;

    t = _nmod_vec_init(n);
    u = _nmod_vec_init(n);

    /* atan(h(x)) = integral(h'(x)/(1+h(x)^2)) */
    _nmod_poly_mullow(u, h, n, h, n, n, mod); u[0] = 1UL;
    _nmod_poly_derivative(t, h, n, mod); t[n-1] = 0UL;
    _nmod_poly_div_series(g, t, u, n, mod);
    _nmod_poly_integral(g, g, n, mod);

    _nmod_vec_free(t);
    _nmod_vec_free(u);
}
void embeddings_isomorphism_inverse(mp_ptr F, const nmod_poly_t G, 
				    const embeddings_t FP, const embeddings_t FQ, const embeddings_t FR){

  long m = nmod_poly_degree(FP->P);
  long n = nmod_poly_degree(FQ->P);

  mp_ptr ell = _nmod_vec_init(m*n);
  nmod_poly_tmulmod(ell, FR->TP->coeffs, G, FR->P, FR->SP);

  mp_ptr Ftrace = _nmod_vec_init(m*n);
  embeddings_tisomorphism(Ftrace, ell, FP, FQ, FR);

  nmod_poly_convert_from_trace_bi(F, Ftrace, FP->P, FP->iP, FQ->P, FQ->iP);

  _nmod_vec_clear(Ftrace);
  _nmod_vec_clear(ell);
}
int main() {

	slong i;
	mp_limb_t p, w;
	nmod_poly_t f;
	mp_ptr res, res2, vect;

	// p premier
	// Le cardinal du groupe multiplicatif doit être une puissance de 2
	// Ne marche que pour p = 0, 1, 2, 3, 5, 17, 257, 65537.
	p = 17;

	nmod_poly_init(f, p);
	nmod_poly_set_coeff_ui(f, 0, 0);
	nmod_poly_set_coeff_ui(f, 1, 1);
	nmod_poly_set_coeff_ui(f, 2, 2);
	nmod_poly_set_coeff_ui(f, 3, 1);
	nmod_poly_set_coeff_ui(f, 4, 1);

	w = n_primitive_root_prime(p);

	res = _nmod_vec_init(p);
	nmod_poly_fft_pow2(res, f, w);

	flint_printf("w : %d\n", w);
	_fmpz_vec_print(res, p);
	flint_printf("\n");


	vect = _nmod_vec_init(p);
	for(i = 0 ; i < p ; i++) {
		vect[i] = i;
	}
	res2 = _nmod_vec_init(p);
	nmod_poly_evaluate_nmod_vec(res2, f, vect, p);

	_fmpz_vec_print(res2, p);
	flint_printf("\nBooléen d'égalité : %d\n", _nmod_vec_equal(res,res2,p));

	nmod_poly_clear(f);
	_nmod_vec_clear(res);
	_nmod_vec_clear(res2);
	_nmod_vec_clear(vect);

	return 0;
}
Exemple #16
0
void
nmod_poly_cosh_series(nmod_poly_t g, const nmod_poly_t h, long n)
{
    mp_ptr g_coeffs, h_coeffs;
    nmod_poly_t t1;
    long h_len;
    
    h_len = h->length;

    if (h_len > 0 && h->coeffs[0] != 0UL)
    {
        printf("Exception: nmod_poly_cosh_series: constant term != 0\n");
        abort();
    }

    if (h_len == 1 || n < 2)
    {
        nmod_poly_zero(g);
        if (n > 0)
            nmod_poly_set_coeff_ui(g, 0, 1UL);
        return;
    }

    if (h_len < n)
    {
        h_coeffs = _nmod_vec_init(n);
        mpn_copyi(h_coeffs, h->coeffs, h_len);
        mpn_zero(h_coeffs + h_len, n - h_len);
    }
    else
        h_coeffs = h->coeffs;

    if (h == g && h_len >= n)
    {
        nmod_poly_init2(t1, h->mod.n, n);
        g_coeffs = t1->coeffs;
    }
    else
    {
        nmod_poly_fit_length(g, n);
        g_coeffs = g->coeffs;
    }

    _nmod_poly_cosh_series(g_coeffs, h_coeffs, n, h->mod);

    if (h == g && h_len >= n)
    {
        nmod_poly_swap(g, t1);
        nmod_poly_clear(t1);
    }
    
    g->length = n;

    if (h_len < n)
        _nmod_vec_free(h_coeffs);

    _nmod_poly_normalise(g);
}
Exemple #17
0
void
_nmod_poly_asinh_series(mp_ptr g, mp_srcptr h, slong n, nmod_t mod)
{
    mp_ptr t, u;

    t = _nmod_vec_init(n);
    u = _nmod_vec_init(n);

    /* asinh(h(x)) = integral(h'(x)/sqrt(1+h(x)^2)) */
    _nmod_poly_mullow(u, h, n, h, n, n, mod); u[0] = UWORD(1);
    _nmod_poly_invsqrt_series(t, u, n, mod);
    _nmod_poly_derivative(u, h, n, mod); u[n-1] = UWORD(0);
    _nmod_poly_mullow(g, t, n, u, n, n, mod);
    _nmod_poly_integral(g, g, n, mod);

    _nmod_vec_clear(t);
    _nmod_vec_clear(u);
}
Exemple #18
0
void
_nmod_poly_tanh_series(mp_ptr f, mp_srcptr h, long n, nmod_t mod)
{
    mp_ptr t, u;

    t = _nmod_vec_init(n);
    u = _nmod_vec_init(n);

    _nmod_vec_add(t, h, h, n, mod);
    _nmod_poly_exp_series(u, t, n, mod);
    _nmod_vec_set(t, u, n);
    t[0] = 0UL;
    u[0] = 2UL;
    _nmod_poly_div_series(f, t, u, n, mod);

    _nmod_vec_free(t);
    _nmod_vec_free(u);
}
/*------------------------------------------------------------*/
void check(int opt){
  long i;
  flint_rand_t state;
  flint_randinit(state);
  mp_limb_t n = 65537;
  nmod_t mod;
  nmod_init(&mod, n);

  for (i = 1; i < 200; i+=1){

    mp_ptr val = _nmod_vec_init(i);
    mp_limb_t q = nmod_poly_find_root(i, mod);

    if (opt == 1){
      nmod_poly_eval_geom_derivative_fanin(val, q, i, mod);

      sage_output_init(mod);
      printf("q = k(%lu)\n", q);
      printf("i = %lu\n", i);
      sage_output_assign_vec(val, i, "val");
      printf("P = mul([x-q^j for j in range(i)])\n");
      printf("val == [P.derivative()(q^j) for j in range(i)]\n");
    }
    else{
      double t, u;
      long j;
      t = util_gettime();
      for (j = 0; j < 10000; j++)
	nmod_poly_eval_geom_derivative_fanin(val, q, i, mod);
      t = util_gettime() - t;
      
      nmod_poly_t tmp1, tmp2, tmp3;
      nmod_poly_init(tmp1, n);
      nmod_poly_init(tmp2, n);
      nmod_poly_init(tmp3, n);

      nmod_poly_rand_dense(tmp1, state, i);
      nmod_poly_rand_dense(tmp2, state, i);

      u = util_gettime();
      for (j = 0; j < 10000; j++)
	nmod_poly_mul(tmp3, tmp1, tmp2);
      u = util_gettime() - u;

      nmod_poly_clear(tmp1);
      nmod_poly_clear(tmp2);
      nmod_poly_clear(tmp3);
      
      printf("%lu %f %f\n", i, t, u);
    }

    _nmod_vec_clear(val);
  }

  flint_randclear(state);
}
void
_nmod_poly_interpolate_nmod_vec_barycentric(mp_ptr poly,
                            mp_srcptr xs, mp_srcptr ys, slong n, nmod_t mod)
{
    mp_ptr P, Q, w;
    slong i, j;

    if (n == 1)
    {
        poly[0] = ys[0];
        return;
    }

    P = _nmod_vec_init(n + 1);
    Q = _nmod_vec_init(n);
    w = _nmod_vec_init(n);

    _nmod_poly_product_roots_nmod_vec(P, xs, n, mod);

    for (i = 0; i < n; i++)
    {
        w[i] = UWORD(1);
        for (j = 0; j < n; j++)
        {
            if (i != j)
                w[i] = nmod_mul(w[i], nmod_sub(xs[i], xs[j], mod), mod);
        }
        w[i] = n_invmod(w[i], mod.n);
    }

    _nmod_vec_zero(poly, n);

    for (i = 0; i < n; i++)
    {
        _nmod_poly_div_root(Q, P, n + 1, xs[i], mod);
        _nmod_vec_scalar_addmul_nmod(poly, Q, n,
            nmod_mul(w[i], ys[i], mod), mod);
    }

    _nmod_vec_clear(P);
    _nmod_vec_clear(Q);
    _nmod_vec_clear(w);
}
Exemple #21
0
void
_nmod_poly_cosh_series(mp_ptr f, mp_srcptr h, long n, nmod_t mod)
{
    mp_ptr g, T, U, hprime;

    g = _nmod_vec_init(n);
    T = _nmod_vec_init(n);
    U = _nmod_vec_init(n);
    hprime = _nmod_vec_init(n);

    _nmod_poly_derivative(hprime, h, n, mod); hprime[n-1] = 0UL;
    __nmod_poly_exp_series_prealloc(f, g, h, hprime, T, U, n, mod, 1);
    _nmod_vec_add(f, f, g, n, mod);
    _nmod_vec_scalar_mul_nmod(f, f, n, n_invmod(2UL, mod.n), mod);

    _nmod_vec_free(hprime);
    _nmod_vec_free(g);
    _nmod_vec_free(T);
    _nmod_vec_free(U);
}
Exemple #22
0
void
nmod_poly_div_basecase(nmod_poly_t Q, const nmod_poly_t A,
                          const nmod_poly_t B)
{
    mp_ptr Q_coeffs, W;
    nmod_poly_t t1;
    long Alen, Blen;

    Blen = B->length;

    if (Blen == 0)
    {
        printf("Exception: division by zero in nmod_poly_div_basecase\n");
        abort();
    }

    Alen = A->length;

    if (Alen < Blen)
    {
        nmod_poly_zero(Q);

        return;
    }

    if (Q == A || Q == B)
    {
        nmod_poly_init2_preinv(t1, B->mod.n, B->mod.ninv,
                               Alen - Blen + 1);
        Q_coeffs = t1->coeffs;
    }
    else
    {
        nmod_poly_fit_length(Q, Alen - Blen + 1);
        Q_coeffs = Q->coeffs;
    }

    W = _nmod_vec_init(NMOD_DIV_BC_ITCH(Alen, Blen, A->mod));
    
    _nmod_poly_div_basecase(Q_coeffs, W, A->coeffs, Alen,
                               B->coeffs, Blen, B->mod);

    if (Q == A || Q == B)
    {
        nmod_poly_swap(Q, t1);
        nmod_poly_clear(t1);
    }
    
    Q->length = Alen - Blen + 1;

    _nmod_vec_clear(W);
    _nmod_poly_normalise(Q);
}
Exemple #23
0
void
nmod_poly_revert_series_lagrange(nmod_poly_t Qinv, 
                                 const nmod_poly_t Q, long n)
{
    mp_ptr Qinv_coeffs, Q_coeffs;
    nmod_poly_t t1;
    long Qlen;
    
    Qlen = Q->length;

    if (Qlen < 2 || Q->coeffs[0] != 0 || Q->coeffs[1] == 0)
    {
        printf("exception: nmod_poly_revert_series_lagrange: input must have "
            "zero constant and an invertible coefficient of x^1");
        abort();
    }

    if (Qlen < n)
    {
        Q_coeffs = _nmod_vec_init(n);
        mpn_copyi(Q_coeffs, Q->coeffs, Qlen);
        mpn_zero(Q_coeffs + Qlen, n - Qlen);
    }
    else
        Q_coeffs = Q->coeffs;

    if (Q == Qinv && Qlen >= n)
    {
        nmod_poly_init2(t1, Q->mod.n, n);
        Qinv_coeffs = t1->coeffs;
    }
    else
    {
        nmod_poly_fit_length(Qinv, n);
        Qinv_coeffs = Qinv->coeffs;
    }

    _nmod_poly_revert_series_lagrange(Qinv_coeffs, Q_coeffs, n, Q->mod);

    if (Q == Qinv && Qlen >= n)
    {
        nmod_poly_swap(Qinv, t1);
        nmod_poly_clear(t1);
    }
    
    Qinv->length = n;

    if (Qlen < n)
        _nmod_vec_clear(Q_coeffs);

    _nmod_poly_normalise(Qinv);
}
Exemple #24
0
void
nmod_poly_inv_series_basecase(nmod_poly_t Qinv,
                              const nmod_poly_t Q, long n)
{
    mp_ptr Qinv_coeffs, Q_coeffs;
    nmod_poly_t t1;
    long Qlen;

    Qlen = Q->length;

    if (n == 0 || Q->length == 0 || Q->coeffs[0] == 0)
    {
        printf("Exception: division by zero in nmod_poly_inv_series_basecase\n");
        abort();
    }

    if (Qlen < n)
    {
        Q_coeffs = _nmod_vec_init(n);
        mpn_copyi(Q_coeffs, Q->coeffs, Qlen);
        mpn_zero(Q_coeffs + Qlen, n - Qlen);
    }
    else
        Q_coeffs = Q->coeffs;

    if (Q == Qinv && Qlen >= n)
    {
        nmod_poly_init2(t1, Q->mod.n, n);
        Qinv_coeffs = t1->coeffs;
    }
    else
    {
        nmod_poly_fit_length(Qinv, n);
        Qinv_coeffs = Qinv->coeffs;
    }

    _nmod_poly_inv_series_basecase(Qinv_coeffs, Q_coeffs, n, Q->mod);

    if (Q == Qinv && Qlen >= n)
    {
        nmod_poly_swap(Qinv, t1);
        nmod_poly_clear(t1);
    }

    Qinv->length = n;

    if (Qlen < n)
        _nmod_vec_free(Q_coeffs);

    _nmod_poly_normalise(Qinv);
}
Exemple #25
0
void fq_nmod_pow(fq_nmod_t rop, const fq_nmod_t op, const fmpz_t e, const fq_nmod_ctx_t ctx)
{
    if (fmpz_sgn(e) < 0)
    {
        flint_printf("Exception (fq_nmod_pow).  e < 0.\n");
        abort();
    }

    if (fmpz_is_zero(e))
    {
        fq_nmod_one(rop, ctx);
    }
    else if (fq_nmod_is_zero(op, ctx))
    {
        fq_nmod_zero(rop, ctx);
    }
    else if (fmpz_is_one(e))
    {
        fq_nmod_set(rop, op, ctx);
    }
    else
    {
        const slong d = fq_nmod_ctx_degree(ctx);
        mp_limb_t *t;

        if (rop == op)
        {
            t = _nmod_vec_init(2 * d - 1);
        }
        else
        {
            nmod_poly_fit_length(rop, 2 * d - 1);
            t = rop->coeffs;
        }

        _fq_nmod_pow(t, op->coeffs, op->length, e, ctx);

        if (rop == op)
        {
            _nmod_vec_clear(rop->coeffs);
            rop->coeffs = t;
            rop->alloc  = 2 * d - 1;
            rop->length = d;
        }
        else
        {
            _nmod_poly_set_length(rop, d);
        }
        _nmod_poly_normalise(rop);
    }
}
Exemple #26
0
void
hmod_mat_randtest(hmod_mat_t mat, flint_rand_t state)
{
    long i, n;
    mp_ptr tmp; 

    n = mat->r * mat->c;

    tmp = _nmod_vec_init(n);

    _nmod_vec_randtest(tmp, state, n, mat->mod);

    for (i = 0; i < n; i++)
        mat->entries[i] = tmp[i];

    _nmod_vec_clear(tmp);
}
Exemple #27
0
void _fq_nmod_trace(fmpz_t rop2, const mp_limb_t *op, slong len, 
                    const fq_nmod_ctx_t ctx)
{
    const slong d = fq_nmod_ctx_degree(ctx);

    ulong i, l;
    mp_limb_t *t, rop;

    t = _nmod_vec_init(d);
    _nmod_vec_zero(t, d);

    t[0] = n_mod2_preinv(d, ctx->mod.n, ctx->mod.ninv);

    for (i = 1; i < d; i++)
    {
        for (l = ctx->len - 2; l >= 0 && ctx->j[l] >= d - (i - 1); l--)
        {
            t[i] = n_addmod(t[i],
                            n_mulmod2_preinv(t[ctx->j[l] + i - d], ctx->a[l], ctx->mod.n, ctx->mod.ninv), 
                            ctx->mod.n);
        }

        if (l >= 0 && ctx->j[l] == d - i)
        {
            t[i] = n_addmod(t[i],
                            n_mulmod2_preinv(ctx->a[l], i, ctx->mod.n, ctx->mod.ninv),
                            ctx->mod.n);
        }

        t[i] = n_negmod(t[i], ctx->mod.n);
    }

    
    rop = WORD(0);
    for (i = 0; i < d; i++)
    {
        rop = n_addmod(rop,
                       n_mulmod2_preinv(op[i], t[i], ctx->mod.n, ctx->mod.ninv),
                       ctx->mod.n);
    }

    _nmod_vec_clear(t);

    fmpz_set_ui(rop2, rop);
}
Exemple #28
0
void 
nmod_poly_rem_basecase(nmod_poly_t R, const nmod_poly_t A, const nmod_poly_t B)
{
    const long lenA = A->length, lenB = B->length;
    mp_ptr r, W;
    nmod_poly_t t;

    if (lenB == 0)
    {
        printf("Exception: division by zero in nmod_poly_rem_basecase\n");
        abort();
    }
    if (lenA < lenB)
    {
        nmod_poly_set(R, A);
        return;
    }

    if (R == A || R == B)
    {
        nmod_poly_init2_preinv(t, B->mod.n, B->mod.ninv, lenB - 1);
        r = t->coeffs;
    }
    else
    {
        nmod_poly_fit_length(R, lenB - 1);
        r = R->coeffs;
    }

    W = _nmod_vec_init(NMOD_DIVREM_BC_ITCH(lenA, lenB, A->mod));

    _nmod_poly_rem_basecase(r, W, A->coeffs, lenA,
                                  B->coeffs, lenB, B->mod);

    if (R == A || R == B)
    {
        nmod_poly_swap(R, t);
        nmod_poly_clear(t);
    }
    R->length = lenB - 1;

    _nmod_vec_clear(W);
    _nmod_poly_normalise(R);
}
Exemple #29
0
int main(void)
{
    flint_rand_t state;
    long i, j;

    printf("bell_number_nmod....");
    fflush(stdout);

    flint_randinit(state);

    for (i = 0; i < 10; i++)
    {
        mp_ptr b;
        long n;
        nmod_t mod;
        mp_limb_t p;

        n = n_randint(state, 1000);
        p = n_randtest_prime(state, 0);

        nmod_init(&mod, p);

        b = _nmod_vec_init(n + 1);
        bell_number_nmod_vec(b, n + 1, mod);

        for (j = 0; j <= n; j++)
        {
            mp_limb_t u = bell_number_nmod(j, mod);

            if (u != b[j])
            {
                printf("FAIL: p = %lu, i = %ld\n", p, j);
                abort();
            }
        }

        _nmod_vec_clear(b);
    }

    flint_randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return 0;
}
void
_nmod_poly_product_roots_nmod_vec(mp_ptr poly, mp_srcptr xs, slong n, nmod_t mod)
{
    if (n == 0)
    {
        poly[0] = UWORD(1);
    }
    else if (n < 20)
    {
        slong i, j;

        poly[n] = UWORD(1);
        poly[n - 1] = nmod_neg(xs[0], mod);

        for (i = 1; i < n; i++)
        {
            poly[n-i-1] = nmod_neg(n_mulmod2_preinv(poly[n-i], xs[i],
                mod.n, mod.ninv), mod);

            for (j = 0; j < i - 1; j++)
            {
                poly[n-i+j] = nmod_sub(poly[n-i+j],
                    n_mulmod2_preinv(poly[n-i+j+1], xs[i], mod.n, mod.ninv),
                        mod);
            }

            poly[n-1] = nmod_sub(poly[n-1], xs[i], mod);
        }
    }
    else
    {
        const slong m = (n + 1) / 2;
        mp_ptr tmp;

        tmp = _nmod_vec_init(n + 2);

        _nmod_poly_product_roots_nmod_vec(tmp, xs, m, mod);
        _nmod_poly_product_roots_nmod_vec(tmp + m + 1, xs + m, n - m, mod);
        _nmod_poly_mul(poly, tmp, m + 1, tmp + m + 1, n - m + 1, mod);

        _nmod_vec_clear(tmp);
    }
}