コード例 #1
0
ファイル: t-init_clear.c プロジェクト: goens/flint2
int
main(void)
{
    int i;
    flint_rand_t state;
    flint_randinit(state);

    printf("init/clear....");
    fflush(stdout);

    for (i = 0; i < 10000; i++)
    {
        nmod_poly_mat_t a;
        mp_limb_t mod;
        long j, k;
        long rows = n_randint(state, 100);
        long cols = n_randint(state, 100);
        mod = n_randtest_prime(state, 0);

        nmod_poly_mat_init(a, rows, cols, mod);

        for (j = 0; j < rows; j++)
            for (k = 0; k < cols; k++)
                nmod_poly_zero(nmod_poly_mat_entry(a, j, k));

        nmod_poly_mat_clear(a);
    }

    flint_randclear(state);
    printf("PASS\n");
    return 0;
}
コード例 #2
0
ファイル: set.c プロジェクト: clear731/lattice
void nmod_poly_factor_set(nmod_poly_factor_t res, const nmod_poly_factor_t fac)
{
    if (res != fac)
    {
        if (fac->num == 0)
        {
            nmod_poly_factor_clear(res);
            nmod_poly_factor_init(res);
        }
        else
        {
            slong i;

            nmod_poly_factor_fit_length(res, fac->num);
            for (i = 0; i < fac->num; i++)
            {
                nmod_poly_set(res->p + i, fac->p + i);
                (res->p + i)->mod = (fac->p + i)->mod;
                res->exp[i] = fac->exp[i];
            }
            for ( ; i < res->num; i++)
            {
                nmod_poly_zero(res->p + i);
                res->exp[i] = 0;
            }
            res->num = fac->num;
        }
    }
}
コード例 #3
0
ファイル: cosh_series.c プロジェクト: hemmecke/flint2
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);
}
コード例 #4
0
ファイル: divrem_newton.c プロジェクト: goens/flint2
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);
}
コード例 #5
0
ファイル: elements.c プロジェクト: defeo/ff_compositum
int pq_nmod_inv(pq_nmod_elt_t res, const pq_nmod_elt_t x, const pq_nmod_t A) {
  _pq_nmod_insure_mono(x, A);
  if (nmod_poly_invmod(res->mono, x->mono, A->M)) {
    nmod_poly_zero(res->dual);
    return 1;
  } else {
    return 0;
  }
}
コード例 #6
0
ファイル: compose_series_horner.c プロジェクト: goens/flint2
void
nmod_poly_compose_series_horner(nmod_poly_t res, 
                    const nmod_poly_t poly1, const nmod_poly_t poly2, long n)
{
    long len1 = poly1->length;
    long len2 = poly2->length;
    long lenr;

    if (len2 != 0 && poly2->coeffs[0] != 0)
    {
        printf("exception: nmod_poly_compose_series_horner: inner polynomial "
                "must have zero constant term\n");
        abort();
    }

    if (len1 == 0 || n == 0)
    {
        nmod_poly_zero(res);
        return;
    }

    if (len2 == 0 || len1 == 1)
    {
        nmod_poly_fit_length(res, 1);
        res->coeffs[0] = poly1->coeffs[0];
        res->length = 1;
        _nmod_poly_normalise(res);
        return;
    }

    lenr = FLINT_MIN((len1 - 1) * (len2 - 1) + 1, n);
    len1 = FLINT_MIN(len1, lenr);
    len2 = FLINT_MIN(len2, lenr);

    if ((res != poly1) && (res != poly2))
    {
        nmod_poly_fit_length(res, lenr);
        _nmod_poly_compose_series_horner(res->coeffs, poly1->coeffs, len1, 
                                        poly2->coeffs, len2, lenr, res->mod);
        res->length = lenr;
        _nmod_poly_normalise(res);
    }
    else
    {
        nmod_poly_t t;
        nmod_poly_init2_preinv(t, res->mod.n, res->mod.ninv, lenr);
        _nmod_poly_compose_series_horner(t->coeffs, poly1->coeffs, len1,
                                        poly2->coeffs, len2, lenr, res->mod);
        t->length = lenr;
        _nmod_poly_normalise(t);
        nmod_poly_swap(res, t);
        nmod_poly_clear(t);
    }
}
コード例 #7
0
ファイル: div_basecase.c プロジェクト: goens/flint2
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);
}
コード例 #8
0
void embeddings_isomorphism(nmod_poly_t G, mp_srcptr F, 
			    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);

  long i;
  nmod_poly_t tmpF, tmpG, S, X;

  nmod_t mod = FP->P->mod;
  nmod_poly_init(tmpF, mod.n);
  nmod_poly_init(tmpG, mod.n);
  nmod_poly_init(S, mod.n);
  nmod_poly_init(X, mod.n);

  nmod_poly_zero(G);
  nmod_poly_zero(X);
  nmod_poly_set_coeff_ui(X, 1, 1);
  embeddings_embed(S, X, FP, FQ, FR);
  
  for (i = m-1; i >= 0; i--){
    nmod_poly_fit_length(tmpF, n);
    long j;
    long offset = i*n;
    for (j = 0; j < n; j++)
      tmpF->coeffs[j] = F[offset+j];
    tmpF->length = n;
    _nmod_poly_normalise(tmpF);
    
    embeddings_embed(tmpG, tmpF, FQ, FP, FR);
    nmod_poly_mulmod(G, G, S, FR->P);
    nmod_poly_add(G, G, tmpG);
  }

  nmod_poly_clear(tmpF);
  nmod_poly_clear(tmpG);
  nmod_poly_clear(X);
  nmod_poly_clear(S);
}
コード例 #9
0
ファイル: pow.c プロジェクト: clear731/lattice
void
nmod_poly_pow(nmod_poly_t res, const nmod_poly_t poly, ulong e)
{
    const slong len = poly->length;
    slong rlen;

    if ((len < 2) | (e < UWORD(3)))
    {
        if (len == 0)
            nmod_poly_zero(res);
        else if (len == 1)
        {
            nmod_poly_fit_length(res, 1);
            res->coeffs[0] = n_powmod2_ui_preinv(poly->coeffs[0], e,
                poly->mod.n, poly->mod.ninv);
            res->length = 1;
            _nmod_poly_normalise(res);
        }
        else if (e == UWORD(0))
        {
            nmod_poly_set_coeff_ui(res, 0, UWORD(1));
            res->length = 1;
            _nmod_poly_normalise(res);
        }
        else if (e == UWORD(1))
            nmod_poly_set(res, poly);
        else  /* e == UWORD(2) */
            nmod_poly_mul(res, poly, poly);

        return;
    }

    rlen = (slong) e * (len - 1) + 1;

    if (res != poly)
    {
        nmod_poly_fit_length(res, rlen);
        _nmod_poly_pow(res->coeffs, poly->coeffs, len, e, poly->mod);
    }
    else
    {
        nmod_poly_t t;
        nmod_poly_init2(t, poly->mod.n, rlen);
        _nmod_poly_pow(t->coeffs, poly->coeffs, len, e, poly->mod);
        nmod_poly_swap(res, t);
        nmod_poly_clear(t);
    }

    res->length = rlen;
    _nmod_poly_normalise(res);
}
コード例 #10
0
ファイル: elements.c プロジェクト: defeo/ff_compositum
void pq_nmod_mul(pq_nmod_elt_t res, const pq_nmod_elt_t x,
		 const pq_nmod_elt_t y, const pq_nmod_t A) {
  switch (nmod_poly_is_zero(y->mono) | 
	  (nmod_poly_is_zero(y->dual) << 1) |
	  (nmod_poly_is_zero(x->mono) << 4) |
	  (nmod_poly_is_zero(x->dual) << 5)) {
    const pq_nmod_elt_struct* tmp;
  case 0x22:
    // Both have only dual -> add mono to x
    _pq_nmod_insure_mono(x, A);
  case 0x12:
  case 0x32:
    // x has mono, y has dual -> swap them
    tmp = x;
    x = y;
    y = tmp;
  case 0x21:
  case 0x23:
    // y has mono, x has dual
    nmod_poly_fit_length(res->dual, A->degree);
    nmod_poly_tmulmod(res->dual->coeffs, x->dual->coeffs, y->mono, A->M, A->S);
    nmod_poly_zero(res->mono);
    break;
  case 0x11:
  case 0x13:
  case 0x31:
  case 0x33:
    // both have mono
    nmod_poly_mulmod(res->mono, x->mono, y->mono, A->M);
    nmod_poly_zero(res->dual);
    break;
  default:
    // in any other case, result is 0
    nmod_poly_zero(res->mono);
    nmod_poly_zero(res->dual);
    break;
  }
}
コード例 #11
0
ファイル: mulmod.c プロジェクト: goens/flint2
void
nmod_poly_mulmod(nmod_poly_t res,
    const nmod_poly_t poly1, const nmod_poly_t poly2, const nmod_poly_t f)
{
    long len1, len2, lenf;
    mp_ptr fcoeffs;

    lenf = f->length;
    len1 = poly1->length;
    len2 = poly2->length;

    if (lenf == 0)
    {
        printf("Exception: nmod_poly_mulmod: divide by zero\n");
        abort();
    }

    if (lenf == 1 || len1 == 0 || len2 == 0)
    {
        nmod_poly_zero(res);
        return;
    }

    if (len1 + len2 - lenf > 0)
    {
        if (f == res)
        {
            fcoeffs = flint_malloc(sizeof(mp_limb_t) * lenf);
            _nmod_vec_set(fcoeffs, f->coeffs, lenf);
        }
        else
            fcoeffs = f->coeffs;

        nmod_poly_fit_length(res, lenf - 1);
        _nmod_poly_mulmod(res->coeffs, poly1->coeffs, len1,
                                       poly2->coeffs, len2,
                                       fcoeffs, lenf,
                                       res->mod);
        if (f == res)
            flint_free(fcoeffs);

        res->length = lenf - 1;
        _nmod_poly_normalise(res);
    }
    else
    {
        nmod_poly_mul(res, poly1, poly2);
    }
}
コード例 #12
0
ファイル: mulhigh.c プロジェクト: goens/flint2
void nmod_poly_mulhigh(nmod_poly_t res, 
                   const nmod_poly_t poly1, const nmod_poly_t poly2, long n)
{
    long len1, len2, len_out;
    
    len1 = poly1->length;
    len2 = poly2->length;

    len_out = poly1->length + poly2->length - 1;
    if (n > len_out)
        n = len_out;
    
    if (len1 == 0 || len2 == 0 || n == 0)
    {
        nmod_poly_zero(res);

        return;
    }

    if (res == poly1 || res == poly2)
    {
        nmod_poly_t temp;

        nmod_poly_init2(temp, poly1->mod.n, len_out);

        if (len1 >= len2)
            _nmod_poly_mulhigh(temp->coeffs, poly1->coeffs, len1,
                           poly2->coeffs, len2, n, poly1->mod);
        else
            _nmod_poly_mulhigh(temp->coeffs, poly2->coeffs, len2,
                           poly1->coeffs, len1, n, poly1->mod);
        
        nmod_poly_swap(temp, res);
        nmod_poly_clear(temp);
    } else
    {
        nmod_poly_fit_length(res, len_out);
        
        if (len1 >= len2)
            _nmod_poly_mulhigh(res->coeffs, poly1->coeffs, len1,
                           poly2->coeffs, len2, n, poly1->mod);
        else
            _nmod_poly_mulhigh(res->coeffs, poly2->coeffs, len2,
                           poly1->coeffs, len1, n, poly1->mod);
    }

    res->length = len_out;
    _nmod_poly_normalise(res);
}
コード例 #13
0
ファイル: mullow_classical.c プロジェクト: clear731/lattice
void
nmod_poly_mullow_classical(nmod_poly_t res,
                           const nmod_poly_t poly1, const nmod_poly_t poly2,
                           slong trunc)
{
    slong len_out;

    if (poly1->length == 0 || poly2->length == 0 || trunc == 0)
    {
        nmod_poly_zero(res);
        return;
    }

    len_out = poly1->length + poly2->length - 1;
    if (trunc > len_out)
        trunc = len_out;

    if (res == poly1 || res == poly2)
    {
        nmod_poly_t temp;
        nmod_poly_init2_preinv(temp, poly1->mod.n, poly1->mod.ninv, trunc);
        if (poly1->length >= poly2->length)
            _nmod_poly_mullow_classical(temp->coeffs, poly1->coeffs,
                                        poly1->length, poly2->coeffs,
                                        poly2->length, trunc, poly1->mod);
        else
            _nmod_poly_mullow_classical(temp->coeffs, poly2->coeffs,
                                        poly2->length, poly1->coeffs,
                                        poly1->length, trunc, poly1->mod);
        nmod_poly_swap(res, temp);
        nmod_poly_clear(temp);
    }
    else
    {
        nmod_poly_fit_length(res, trunc);
        if (poly1->length >= poly2->length)
            _nmod_poly_mullow_classical(res->coeffs, poly1->coeffs,
                                        poly1->length, poly2->coeffs,
                                        poly2->length, trunc, poly1->mod);
        else
            _nmod_poly_mullow_classical(res->coeffs, poly2->coeffs,
                                        poly2->length, poly1->coeffs,
                                        poly1->length, trunc, poly1->mod);
    }

    res->length = trunc;
    _nmod_poly_normalise(res);
}
コード例 #14
0
ファイル: mullow_KS.c プロジェクト: goens/flint2
void
nmod_poly_mullow_KS(nmod_poly_t res,
                 const nmod_poly_t poly1, const nmod_poly_t poly2,
                 mp_bitcnt_t bits, long n)
{
    long len_out;

    if ((poly1->length == 0) || (poly2->length == 0))
    {
        nmod_poly_zero(res);
        return;
    }

    len_out = poly1->length + poly2->length - 1;
    if (n > len_out)
        n = len_out;

    if (res == poly1 || res == poly2)
    {
        nmod_poly_t temp;
        nmod_poly_init2_preinv(temp, poly1->mod.n, poly1->mod.ninv, len_out);
        if (poly1->length >= poly2->length)
            _nmod_poly_mullow_KS(temp->coeffs, poly1->coeffs, poly1->length,
                              poly2->coeffs, poly2->length, bits,
                              n, poly1->mod);
        else
            _nmod_poly_mullow_KS(temp->coeffs, poly2->coeffs, poly2->length,
                              poly1->coeffs, poly1->length, bits,
                              n, poly1->mod);
        nmod_poly_swap(res, temp);
        nmod_poly_clear(temp);
    }
    else
    {
        nmod_poly_fit_length(res, len_out);
        if (poly1->length >= poly2->length)
            _nmod_poly_mullow_KS(res->coeffs, poly1->coeffs, poly1->length,
                              poly2->coeffs, poly2->length, bits,
                              n, poly1->mod);
        else
            _nmod_poly_mullow_KS(res->coeffs, poly2->coeffs, poly2->length,
                              poly1->coeffs, poly1->length, bits,
                              n, poly1->mod);
    }

    res->length = n;
    _nmod_poly_normalise(res);
}
コード例 #15
0
ファイル: get_nmod_poly.c プロジェクト: Axure/Hecke.jl
void
_fmpz_vec_get_nmod_poly(nmod_poly_t res, const fmpz * coeffs, slong len)
{
    if (len == 0)
    {
        nmod_poly_zero(res);
    }
    else
    {
        slong i;
        nmod_poly_fit_length(res, len);
        for (i = 0; i < len; i++)
            res->coeffs[i] = fmpz_fdiv_ui(coeffs + i, res->mod.n);
        _nmod_poly_set_length(res, len);
        _nmod_poly_normalise(res);
    }
}
コード例 #16
0
void
nmod_poly_interpolate_nmod_vec_barycentric(nmod_poly_t poly,
                                    mp_srcptr xs, mp_srcptr ys, slong n)
{
    if (n == 0)
    {
        nmod_poly_zero(poly);
    }
    else
    {
        nmod_poly_fit_length(poly, n);
        poly->length = n;
        _nmod_poly_interpolate_nmod_vec_barycentric(poly->coeffs,
            xs, ys, n, poly->mod);
        _nmod_poly_normalise(poly);
    }
}
コード例 #17
0
ファイル: div_divconquer.c プロジェクト: goens/flint2
void
nmod_poly_div_divconquer(nmod_poly_t Q,
                            const nmod_poly_t A, const nmod_poly_t B)
{
    nmod_poly_t tQ;
    mp_ptr q;
    long Alen, Blen;

    Blen = B->length;

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

    Alen = A->length;

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

    if (Q == A || Q == B)
    {
        nmod_poly_init2(tQ, A->mod.n, Alen - Blen + 1);
        q = tQ->coeffs;
    }
    else
    {
        nmod_poly_fit_length(Q, Alen - Blen + 1);
        q = Q->coeffs;
    }

    _nmod_poly_div_divconquer(q, A->coeffs, Alen,
                                       B->coeffs, Blen, A->mod);

    if (Q == A || Q == B)
    {
        nmod_poly_swap(tQ, Q);
        nmod_poly_clear(tQ);
    }
    
    Q->length = Alen - Blen + 1;
}
コード例 #18
0
ファイル: compose_divconquer.c プロジェクト: goens/flint2
void
nmod_poly_compose_divconquer(nmod_poly_t res, 
                             const nmod_poly_t poly1, const nmod_poly_t poly2)
{
    const long len1 = poly1->length;
    const long len2 = poly2->length;
    long lenr;
    
    if (len1 == 0)
    {
        nmod_poly_zero(res);
        return;
    }
    if (len1 == 1 || len2 == 0)
    {
        nmod_poly_set_coeff_ui(res, 0, poly1->coeffs[0]);
        nmod_poly_truncate(res, 1);
        return;
    }
    
    lenr = (len1 - 1) * (len2 - 1) + 1;
    
    if (res != poly1 && res != poly2)
    {
        nmod_poly_fit_length(res, lenr);
        _nmod_poly_compose_divconquer(res->coeffs, poly1->coeffs, len1, 
                                                   poly2->coeffs, len2, poly1->mod);
    }
    else
    {
        nmod_poly_t t;
        nmod_poly_init2(t, poly1->mod.n, lenr);
        _nmod_poly_compose_divconquer(t->coeffs, poly1->coeffs, len1,
                                                 poly2->coeffs, len2, poly1->mod);
        nmod_poly_swap(res, t);
        nmod_poly_clear(t);
    }

    res->length = lenr;
    _nmod_poly_normalise(res);
}
コード例 #19
0
void
nmod_poly_exp_series_monomial_ui(nmod_poly_t res, mp_limb_t coeff,
                                    ulong power, slong n)
{
    if (n == 0)
    {
        nmod_poly_zero(res);
        return;
    }

    if (coeff == UWORD(0))
    {
        nmod_poly_fit_length(res, 1);
        res->coeffs[0] = UWORD(1);
        res->length = 1;
        return;
    }

    if (power == 0)
    {
        flint_printf("Exception (nmod_poly_exp_series_monomial_ui). \n"
               "Constant term != 0.\n");
        abort();
    }

    if (coeff != UWORD(1))
        coeff = n_mod2_preinv(coeff, res->mod.n, res->mod.ninv);

    if (n == 1 || power >= n)
    {
        nmod_poly_fit_length(res, 1);
        res->coeffs[0] = UWORD(1);
        res->length = 1;
    }

    nmod_poly_fit_length(res, n);
    _nmod_poly_exp_series_monomial_ui(res->coeffs, coeff, power, n, res->mod);
    res->length = n;
    _nmod_poly_normalise(res);
}
コード例 #20
0
ファイル: div_newton.c プロジェクト: clear731/lattice
void nmod_poly_div_newton(nmod_poly_t Q, const nmod_poly_t A,
                                         const nmod_poly_t B)
{
    const slong lenA = A->length, lenB = B->length, lenQ = lenA - lenB + 1;

    mp_ptr q;

    if (lenB == 0)
    {
        flint_printf("Exception (nmod_poly_div_newton). Division by zero.\n");
        abort();
    }

    if (lenA < lenB)
    {
        nmod_poly_zero(Q);
        return;
    }

    if (Q == A || Q == B)
    {
        q = flint_malloc(lenQ * sizeof(mp_limb_t));
    }
    else
    {
        nmod_poly_fit_length(Q, lenQ);
        q = Q->coeffs;
    }

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

    if (Q == A || Q == B)
    {
        flint_free(Q->coeffs);
        Q->coeffs = q;
        Q->alloc  = lenQ;
    }
    Q->length = lenQ;
}
コード例 #21
0
ファイル: randtest_sparse.c プロジェクト: goens/flint2
void
nmod_poly_mat_randtest_sparse(nmod_poly_mat_t A, flint_rand_t state, long len,
    float density)
{
    long i, j;

    for (i = 0; i < A->r; i++)
    {
        for (j = 0; j < A->c; j++)
        {
            if (n_randint(state, 1000) < density * 1000)
            {
                long l = n_randint(state, len + 1);
                l = FLINT_MAX(l, 1);
                nmod_poly_randtest(nmod_poly_mat_entry(A, i, j), state, l);
            }
            else
            {
                nmod_poly_zero(nmod_poly_mat_entry(A, i, j));
            }
        }
    }
}
コード例 #22
0
ファイル: asin_series.c プロジェクト: clear731/lattice
void
nmod_poly_asin_series(nmod_poly_t g, const nmod_poly_t h, slong n)
{
    mp_ptr h_coeffs;
    slong h_len = h->length;

    if (h_len > 0 && h->coeffs[0] != UWORD(0))
    {
        flint_printf("Exception (nmod_poly_asin_series). Constant term != 0.\n");
        abort();
    }

    if (h_len == 1 || n < 2)
    {
        nmod_poly_zero(g);
        return;
    }

    nmod_poly_fit_length(g, n);

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

    _nmod_poly_asin_series(g->coeffs, h_coeffs, n, h->mod);

    if (h_len < n)
        _nmod_vec_clear(h_coeffs);

    g->length = n;
	_nmod_poly_normalise(g);
}
コード例 #23
0
ファイル: tanh_series.c プロジェクト: hemmecke/flint2
void
nmod_poly_tanh_series(nmod_poly_t g, const nmod_poly_t h, long n)
{
    mp_ptr h_coeffs;
    long h_len = h->length;

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

    if (h_len == 1 || n < 2)
    {
        nmod_poly_zero(g);
        return;
    }

    nmod_poly_fit_length(g, n);

    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;

    _nmod_poly_tanh_series(g->coeffs, h_coeffs, n, h->mod);

    if (h_len < n)
        _nmod_vec_free(h_coeffs);

    g->length = n;
	_nmod_poly_normalise(g);
}
コード例 #24
0
ファイル: test_print_poly.c プロジェクト: defeo/ff_compositum
/*------------------------------------------------------------*/
void check(int opt){

  mp_limb_t n = 12345;
  nmod_t Zn;
  nmod_init(&Zn, n);
  sage_output_init(Zn);

  nmod_poly_t a;
  nmod_poly_init2(a, n, 10);
  long i;
  for (i = 0; i < 10; i++)
    a->coeffs[i] = i;
  a->length = 10;
  _nmod_poly_normalise(a);

  sage_output_print_poly(a);
  printf("\n");

  nmod_poly_zero(a);
  sage_output_print_poly(a);
  printf("\n");

  nmod_poly_clear(a);
}
コード例 #25
0
ファイル: exp_series.c プロジェクト: hemmecke/flint2
void
nmod_poly_exp_series(nmod_poly_t f, const nmod_poly_t h, long n)
{
    mp_ptr f_coeffs, h_coeffs;
    nmod_poly_t t1;
    long hlen, k;

    nmod_poly_fit_length(f, n);
    hlen = h->length;

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

    if (n <= 1 || hlen == 0)
    {
        if (n == 0)
        {
            nmod_poly_zero(f);
        }
        else
        {
            f->coeffs[0] = 1UL;
            f->length = 1;
        }
        return;
    }

    /* Handle monomials */
    for (k = 0; h->coeffs[k] == 0UL && k < n - 1; k++);
    if (k == hlen - 1 || k == n - 1)
    {
        hlen = FLINT_MIN(hlen, n);
        _nmod_poly_exp_series_monomial_ui(f->coeffs,
            h->coeffs[hlen-1], hlen - 1, n, f->mod);
        f->length = n;
        _nmod_poly_normalise(f);
        return;
    }

    if (n < NMOD_NEWTON_EXP_CUTOFF2)
    {
        _nmod_poly_exp_series_basecase(f->coeffs, h->coeffs, hlen, n, f->mod);
        f->length = n;
        _nmod_poly_normalise(f);
        return;
    }

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

    if (h == f && hlen >= n)
    {
        nmod_poly_init2(t1, h->mod.n, n);
        f_coeffs = t1->coeffs;
    }
    else
    {
        nmod_poly_fit_length(f, n);
        f_coeffs = f->coeffs;
    }

    _nmod_poly_exp_series(f_coeffs, h_coeffs, n, f->mod);

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

    if (hlen < n)
        _nmod_vec_free(h_coeffs);

    _nmod_poly_normalise(f);
}
コード例 #26
0
ファイル: xgcd_hgcd.c プロジェクト: clear731/lattice
void
nmod_poly_xgcd_hgcd(nmod_poly_t G, nmod_poly_t S, nmod_poly_t T,
                         const nmod_poly_t A, const nmod_poly_t B)
{
    if (A->length < B->length)
    {
        nmod_poly_xgcd_hgcd(G, T, S, B, A);
    }
    else  /* lenA >= lenB >= 0 */
    {
        const slong lenA = A->length, lenB = B->length;
        mp_limb_t inv;

        if (lenA == 0)  /* lenA = lenB = 0 */
        {
            nmod_poly_zero(G);
            nmod_poly_zero(S);
            nmod_poly_zero(T);
        }
        else if (lenB == 0)  /* lenA > lenB = 0 */
        {
            inv = n_invmod(A->coeffs[lenA - 1], A->mod.n);
            nmod_poly_scalar_mul_nmod(G, A, inv);
            nmod_poly_zero(T);
            nmod_poly_set_coeff_ui(S, 0, inv);
            S->length = 1;
        }
        else if (lenB == 1)  /* lenA >= lenB = 1 */
        {
            nmod_poly_fit_length(T, 1);
            T->length = 1;
            T->coeffs[0] = n_invmod(B->coeffs[0], A->mod.n);
            nmod_poly_one(G);
            nmod_poly_zero(S);
        }
        else  /* lenA >= lenB >= 2 */
        {
            mp_ptr g, s, t;
            slong lenG;

            if (G == A || G == B)
            {
                g = _nmod_vec_init(FLINT_MIN(lenA, lenB));
            }
            else
            {
                nmod_poly_fit_length(G, FLINT_MIN(lenA, lenB));
                g = G->coeffs;
            }
            if (S == A || S == B)
            {
                s = _nmod_vec_init(FLINT_MAX(lenB - 1, 2));
            }
            else
            {
                nmod_poly_fit_length(S, FLINT_MAX(lenB - 1, 2));
                s = S->coeffs;
            }
            if (T == A || T == B)
            {
                t = _nmod_vec_init(FLINT_MAX(lenA - 1, 2));
            }
            else
            {
                nmod_poly_fit_length(T, FLINT_MAX(lenA - 1, 2));
                t = T->coeffs;
            }

            if (lenA >= lenB)
                lenG = _nmod_poly_xgcd_hgcd(g, s, t, A->coeffs, lenA,
                                                          B->coeffs, lenB, A->mod);
            else
                lenG = _nmod_poly_xgcd_hgcd(g, t, s, B->coeffs, lenB,
                                                          A->coeffs, lenA, A->mod);

            if (G == A || G == B)
            {
                flint_free(G->coeffs);
                G->coeffs = g;
                G->alloc  = FLINT_MIN(lenA, lenB);
            }
            if (S == A || S == B)
            {
                flint_free(S->coeffs);
                S->coeffs = s;
                S->alloc  = FLINT_MAX(lenB - 1, 2);
            }
            if (T == A || T == B)
            {
                flint_free(T->coeffs);
                T->coeffs = t;
                T->alloc  = FLINT_MAX(lenA - 1, 2);
            }

            G->length = lenG;
            S->length = FLINT_MAX(lenB - lenG, 1);
            T->length = FLINT_MAX(lenA - lenG, 1);
            MPN_NORM(S->coeffs, S->length);
            MPN_NORM(T->coeffs, T->length);

            if (G->coeffs[lenG - 1] != 1)
            {
                inv = n_invmod(G->coeffs[lenG - 1], A->mod.n);
                nmod_poly_scalar_mul_nmod(G, G, inv);
                nmod_poly_scalar_mul_nmod(S, S, inv);
                nmod_poly_scalar_mul_nmod(T, T, inv);
            }
        }
    }
}
コード例 #27
0
ファイル: inv.c プロジェクト: clear731/lattice
int
nmod_poly_mat_inv(nmod_poly_mat_t Ainv, nmod_poly_t den,
                    const nmod_poly_mat_t A)
{
    slong n = nmod_poly_mat_nrows(A);

    if (n == 0)
    {
        nmod_poly_one(den);
        return 1;
    }
    else if (n == 1)
    {
        nmod_poly_set(den, E(A, 0, 0));
        nmod_poly_one(E(Ainv, 0, 0));
        return !nmod_poly_is_zero(den);
    }
    else if (n == 2)
    {
        nmod_poly_mat_det(den, A);

        if (nmod_poly_is_zero(den))
        {
            return 0;
        }
        else if (Ainv == A)
        {
            nmod_poly_swap(E(A, 0, 0), E(A, 1, 1));
            nmod_poly_neg(E(A, 0, 1), E(A, 0, 1));
            nmod_poly_neg(E(A, 1, 0), E(A, 1, 0));
            return 1;
        }
        else
        {
            nmod_poly_set(E(Ainv, 0, 0), E(A, 1, 1));
            nmod_poly_set(E(Ainv, 1, 1), E(A, 0, 0));
            nmod_poly_neg(E(Ainv, 0, 1), E(A, 0, 1));
            nmod_poly_neg(E(Ainv, 1, 0), E(A, 1, 0));
            return 1;
        }
    }
    else
    {
        nmod_poly_mat_t LU, I;
        slong * perm;
        int result;

        perm = _perm_init(n);
        nmod_poly_mat_init_set(LU, A);
        result = (nmod_poly_mat_fflu(LU, den, perm, LU, 1) == n);

        if (result)
        {
            nmod_poly_mat_init(I, n, n, nmod_poly_mat_modulus(A));
            nmod_poly_mat_one(I);
            nmod_poly_mat_solve_fflu_precomp(Ainv, perm, LU, I);
            nmod_poly_mat_clear(I);
        }
        else
            nmod_poly_zero(den);

        if (_perm_parity(perm, n))
        {
            nmod_poly_mat_neg(Ainv, Ainv);
            nmod_poly_neg(den, den);
        }

        _perm_clear(perm);
        nmod_poly_mat_clear(LU);
        return result;
    }
}
コード例 #28
0
ファイル: divrem_basecase.c プロジェクト: clear731/lattice
void
nmod_poly_divrem_basecase(nmod_poly_t Q, nmod_poly_t R, const nmod_poly_t A,
                          const nmod_poly_t B)
{
    const slong lenA = A->length, lenB = B->length;
    mp_ptr Q_coeffs, R_coeffs, W;
    nmod_poly_t t1, t2;
    TMP_INIT;

    if (lenB == 0)
    {
        flint_printf("Exception (nmod_poly_divrem). Division by zero.\n");
        abort();
    }

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

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

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

    TMP_START;
    W = TMP_ALLOC(NMOD_DIVREM_BC_ITCH(lenA, lenB, A->mod)*sizeof(mp_limb_t));
    
    _nmod_poly_divrem_basecase(Q_coeffs, R_coeffs, W, A->coeffs, lenA,
                               B->coeffs, lenB, B->mod);

    if (Q == A || Q == B)
    {
        nmod_poly_swap(Q, t1);
        nmod_poly_clear(t1);
    }
    if (R == A || R == B)
    {
        nmod_poly_swap(R, t2);
        nmod_poly_clear(t2);
    }
    Q->length = lenA - lenB + 1;
    R->length = lenB - 1;

    TMP_END;
    _nmod_poly_normalise(R);
}
コード例 #29
0
ファイル: compose_mod_horner.c プロジェクト: goens/flint2
void
nmod_poly_compose_mod_horner(nmod_poly_t res, 
                    const nmod_poly_t poly1, const nmod_poly_t poly2,
                    const nmod_poly_t poly3)
{
    long len1 = poly1->length;
    long len2 = poly2->length;
    long len3 = poly3->length;
    long len = len3 - 1;

    mp_ptr ptr2;

    if (len3 == 0)
    {
        printf("exception: division by zero in nmod_poly_compose_mod_horner\n");
        abort();
    }

    if (len1 == 0 || len3 == 1)
    {
        nmod_poly_zero(res);
        return;
    }

    if (len1 == 1)
    {
        nmod_poly_set(res, poly1);
        return;
    }

    if (res == poly3 || res == poly1)
    {
        nmod_poly_t tmp;
        nmod_poly_init_preinv(tmp, res->mod.n, res->mod.ninv);
        nmod_poly_compose_mod_horner(tmp, poly1, poly2, poly3);
        nmod_poly_swap(tmp, res);
        nmod_poly_clear(tmp);
        return;
    }

    ptr2 = _nmod_vec_init(len);

    if (len2 <= len)
    {
        mpn_copyi(ptr2, poly2->coeffs, len2);
        mpn_zero(ptr2 + len2, len - len2);
    }
    else
    {
        _nmod_poly_rem(ptr2, poly2->coeffs, len2,
                             poly3->coeffs, len3, res->mod);
    }

    nmod_poly_fit_length(res, len);
    _nmod_poly_compose_mod_horner(res->coeffs,
        poly1->coeffs, len1, ptr2, poly3->coeffs, len3, res->mod);
    res->length = len;
    _nmod_poly_normalise(res);

    _nmod_vec_clear(ptr2);
}
コード例 #30
0
ファイル: t-factor.c プロジェクト: goens/flint2
int
main(void)
{
    int iter;
    flint_rand_t state;
    flint_randinit(state);

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

    /* Default algorithm */
    for (iter = 0; iter < 100; iter++)
    {
        int result = 1;
        nmod_poly_t pol1, poly, quot, rem, product;
        nmod_poly_factor_t res;
        mp_limb_t modulus, lead = 1;
        long length, num, i, j;
        ulong exp[5], prod1;

        modulus = n_randtest_prime(state, 0);

        nmod_poly_init(pol1, modulus);
        nmod_poly_init(poly, modulus);
        nmod_poly_init(quot, modulus);
        nmod_poly_init(rem, modulus);

        nmod_poly_zero(pol1);
        nmod_poly_set_coeff_ui(pol1, 0, 1);

        length = n_randint(state, 7) + 2;
        do 
        {
            nmod_poly_randtest(poly, state, length); 
            if (poly->length)
                nmod_poly_make_monic(poly, poly);
        }
        while ((!nmod_poly_is_irreducible(poly)) || (poly->length < 2));

        exp[0] = n_randint(state, 30) + 1;
        prod1 = exp[0];
        for (i = 0; i < exp[0]; i++)
            nmod_poly_mul(pol1, pol1, poly);

        num = n_randint(state, 5) + 1;
        for (i = 1; i < num; i++)
        {
            do 
            {
                length = n_randint(state, 7) + 2;
                nmod_poly_randtest(poly, state, length); 
                if (poly->length)
                {
                    nmod_poly_make_monic(poly, poly);
                    nmod_poly_divrem(quot, rem, pol1, poly);
                }
            }
            while ((!nmod_poly_is_irreducible(poly)) ||
                (poly->length < 2) || (rem->length == 0));
            exp[i] = n_randint(state, 30) + 1;
            prod1 *= exp[i];

            for (j = 0; j < exp[i]; j++)
                nmod_poly_mul(pol1, pol1, poly);
        }

        nmod_poly_factor_init(res);

        switch (n_randint(state, 3))
        {
            case 0:
                lead = nmod_poly_factor(res, pol1);
                break;
            case 1:
                lead = nmod_poly_factor_with_berlekamp(res, pol1);
                break;
            case 2:
                if (modulus == 2)
                    lead = nmod_poly_factor(res, pol1);
                else
                    lead = nmod_poly_factor_with_cantor_zassenhaus(res, pol1);
                break;
        }

        result &= (res->num == num);
        if (!result)
        {
            printf("Error: number of factors incorrect, %ld, %ld\n",
                res->num, num);
            abort();
        }

        nmod_poly_init(product, pol1->mod.n);
        nmod_poly_set_coeff_ui(product, 0, 1);
        for (i = 0; i < res->num; i++)
            for (j = 0; j < res->exp[i]; j++)
                nmod_poly_mul(product, product, res->p + i);
        nmod_poly_scalar_mul_nmod(product, product, lead);
        result &= nmod_poly_equal(pol1, product);
        if (!result)
        {
            printf("Error: product of factors does not equal original polynomial\n");
            nmod_poly_print(pol1); printf("\n");
            nmod_poly_print(product); printf("\n");
            abort();
        }
        nmod_poly_clear(product);

        nmod_poly_clear(quot);
        nmod_poly_clear(rem);
        nmod_poly_clear(pol1);
        nmod_poly_clear(poly);
        nmod_poly_factor_clear(res);
    }

    /* Test deflation trick */
    for (iter = 0; iter < 100; iter++)
    {
        nmod_poly_t pol1, poly, quot, rem;
        nmod_poly_factor_t res, res2;
        mp_limb_t modulus;
        long length, num, i, j;
        long exp[5], prod1;
        ulong inflation;
        int found;

        do {
            modulus = n_randtest_prime(state, 0);
        } while (modulus == 2); /* To compare with CZ */

        nmod_poly_init(pol1, modulus);
        nmod_poly_init(poly, modulus);
        nmod_poly_init(quot, modulus);
        nmod_poly_init(rem, modulus);

        nmod_poly_zero(pol1);
        nmod_poly_set_coeff_ui(pol1, 0, 1);

        inflation = n_randint(state, 7) + 1;

        length = n_randint(state, 7) + 2;
        do 
        {
            nmod_poly_randtest(poly, state, length); 
            if (poly->length)
                nmod_poly_make_monic(poly, poly);
        }
        while ((!nmod_poly_is_irreducible(poly)) || (poly->length < 2));
        nmod_poly_inflate(poly, poly, inflation);

        exp[0] = n_randint(state, 6) + 1;
        prod1 = exp[0];
        for (i = 0; i < exp[0]; i++)
            nmod_poly_mul(pol1, pol1, poly);

        num = n_randint(state, 5) + 1;
        for (i = 1; i < num; i++)
        {
            do
            {
                length = n_randint(state, 6) + 2;
                nmod_poly_randtest(poly, state, length); 
                if (poly->length)
                {
                    nmod_poly_make_monic(poly, poly);
                    nmod_poly_divrem(quot, rem, pol1, poly);
                }
            }
            while ((!nmod_poly_is_irreducible(poly)) ||
                (poly->length < 2) || (rem->length == 0));
            exp[i] = n_randint(state, 6) + 1;
            prod1 *= exp[i];
            nmod_poly_inflate(poly, poly, inflation);

            for (j = 0; j < exp[i]; j++)
                nmod_poly_mul(pol1, pol1, poly);
        }

        nmod_poly_factor_init(res);
        nmod_poly_factor_init(res2);

        switch (n_randint(state, 3))
        {
            case 0:
                nmod_poly_factor(res, pol1);
                break;
            case 1:
                nmod_poly_factor_with_berlekamp(res, pol1);
                break;
            case 2:
                nmod_poly_factor_with_cantor_zassenhaus(res, pol1);
                break;
        }

        nmod_poly_factor_cantor_zassenhaus(res2, pol1);

        if (res->num != res2->num)
        {
            printf("FAIL: different number of factors found\n");
            abort();
        }

        for (i = 0; i < res->num; i++)
        {
            found = 0;
            for (j = 0; j < res2->num; j++)
            {
                if (nmod_poly_equal(res->p + i, res2->p + j) &&
                        res->exp[i] == res2->exp[j])
                {
                    found = 1;
                    break;
                }
            }

            if (!found)
            {
                printf("FAIL: factor not found\n");
                abort();
            }
        }

        nmod_poly_clear(quot);
        nmod_poly_clear(rem);
        nmod_poly_clear(pol1);
        nmod_poly_clear(poly);
        nmod_poly_factor_clear(res);
        nmod_poly_factor_clear(res2);
    }

    flint_randclear(state);
    printf("PASS\n");
    return 0;
}