コード例 #1
0
ファイル: skeygen.c プロジェクト: theSwan/LWE
int main(int argc, char *args[]) /*setup.txt:lev,d, {q, n, bign} */
{
        FILE *fp;
        
        if((fp = fopen(args[1], "r")) == NULL)
        {
                printf("file read error\n");
                exit(0);
        }
        
        char str[100];
        
        fmpz_t tmp;
        fmpz_init(tmp);
        
        fgets(str, 100, fp);
        fmpz_set_str(tmp, str, 10);
        
        long lev, d, i, j, row;
        lev = fmpz_get_si(tmp);
        fgets(str, 100, fp);
        fmpz_set_str(tmp, str, 10);
        d = fmpz_get_si(tmp);
        FILE *skf;
        
        char name[]="sk1.txt";
        for( i = lev ; i >= 0; i-- ) {
                param_node_t *h;
                fmpz_mat_t sk;
                h = param_node_init(h);
                fgets(str, 100, fp);
                fmpz_set_str(h->q, str, 10);
                fgets(str, 100, fp);
                fmpz_set_str(tmp, str, 10);
                h->n = fmpz_get_si(tmp);
                fgets(str, 100, fp);
                fmpz_set_str(tmp, str, 10);
                h->bign = fmpz_get_si(tmp);
                row = 1 + h->n;
                fmpz_mat_init(sk, row, 1);
                e_skeygen(sk, h);
                name[2] = '0' + i;
                if((skf = fopen(name, "w")) == NULL)
                {
                        printf("file open error\n");
                        exit(0);
                }
                int flag = fmpz_mat_fprint(skf, sk);
                if(flag < 0) {
                        printf("file write error\n");
                        exit(0);
                }
                fclose(skf);
                fmpz_mat_clear(sk);
        }
        fclose(fp);
        fmpz_clear(tmp);
        return 0;
}
コード例 #2
0
ファイル: log_balanced.c プロジェクト: goens/flint2
static void 
_padic_log_bsplit(fmpz_t z, const fmpz_t y, long v, const fmpz_t p, long N)
{
    fmpz_t P, B, T;
    long n;

    if (fmpz_fits_si(p))
        n = _padic_log_bound(v, N, fmpz_get_si(p));
    else
        n = (N - 1) / v;

    n = FLINT_MAX(n, 2);

    fmpz_init(P);
    fmpz_init(B);
    fmpz_init(T);

    _padic_log_bsplit_series(P, B, T, y, 1, n);

    n = fmpz_remove(B, B, p);
    fmpz_pow_ui(P, p, n);
    fmpz_divexact(T, T, P);

    _padic_inv(B, B, p, N);
    fmpz_mul(z, T, B);

    fmpz_clear(P);
    fmpz_clear(B);
    fmpz_clear(T);
}
コード例 #3
0
int
_is_superficially_ok_entrywise(const fmpz_mat_t A)
{
    slong n, i, j, d;

    if (!fmpz_mat_is_square(A))
        return 0;

    n = fmpz_mat_nrows(A);
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
            d = fmpz_get_si(fmpz_mat_entry(A, i, j));

            if (d < -2 || d > n-1)
                return 0;

            if (i == j && d == -1)
                return 0;

            if (i != j && d == 0)
                return 0;
        }
    }
    return 1;
}
コード例 #4
0
ファイル: precompute.c プロジェクト: argriffing/arb
slong
hypgeom_root_norm(const fmpz_poly_t P)
{
    slong res, i, p;
    fmpz_t t, A;

    fmpz_init(A);
    fmpz_init(t);

    p = fmpz_poly_degree(P);

    fmpz_zero(A);

    for (i = 1; i <= p; i++)
    {
        fmpz_cdiv_abs_q(t, P->coeffs + p - i, P->coeffs + p);
        fmpz_root(t, t, i);
        fmpz_add_ui(t, t, 1);

        if (fmpz_cmp(t, A) > 0)
            fmpz_swap(t, A);
    }

    if (!fmpz_fits_si(A))
        abort();

    res = fmpz_get_si(A);

    fmpz_clear(A);
    fmpz_clear(t);

    return res;
}
コード例 #5
0
void
_brute_force_all_pairs_longest_walk(fmpz_mat_t B, const bool_mat_t A)
{
    slong i, j, n;

    n = bool_mat_nrows(A);

    /* set entries of B according to the longest observed walk */
    {
        slong k;
        bool_mat_t T;
        bool_mat_init(T, n, n);
        bool_mat_one(T);
        fmpz_mat_zero(B);
        for (k = 0; k < 2*n+1; k++)
        {
            for (i = 0; i < n; i++)
            {
                for (j = 0; j < n; j++)
                {
                    if (bool_mat_get_entry(T, i, j))
                    {
                        fmpz_set_si(fmpz_mat_entry(B, i, j), k);
                    }
                }
            }
            bool_mat_mul(T, T, A);
        }
        bool_mat_clear(T);
    }

    /* set special values 0, -1, -2 */
    {
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < n; j++)
            {
                slong x;
                fmpz *p;
                p = fmpz_mat_entry(B, i, j);
                x = fmpz_get_si(p);
                if (x < 1)
                {
                    x = (i == j) ? 0 : -1;
                }
                else if (x > n-1)
                {
                    x = -2;
                }
                fmpz_set_si(p, x);
            }
        }
    }
}
コード例 #6
0
ファイル: get_si.c プロジェクト: argriffing/arb
slong
fmpr_get_si(const fmpr_t x, fmpr_rnd_t rnd)
{
    fmpz_t t;
    slong v;
    fmpz_init(t);
    fmpr_get_fmpz(t, x, rnd);
    if (!fmpz_fits_si(t))
    {
        flint_printf("fmpr_get_si: result does not fit in a signed slong\n");
        abort();
    }
    v = fmpz_get_si(t);
    fmpz_clear(t);
    return v;
}
コード例 #7
0
ファイル: scalar_div_si.c プロジェクト: goens/flint2
void _fmpq_poly_scalar_div_si(fmpz * rpoly, fmpz_t rden, const fmpz * poly,
                              const fmpz_t den, long len, long c)
{
    if (c == 1)
    {
        if (rpoly != poly)
        {
            _fmpz_vec_set(rpoly, poly, len);
            fmpz_set(rden, den);
        }
    }
    else if (c == -1)
    {
        _fmpz_vec_neg(rpoly, poly, len);
        fmpz_set(rden, den);
    }
    else
    {
        fmpz_t d, f;

        fmpz_init(d);
        fmpz_init(f);
        
        fmpz_set_si(f, c);
        _fmpz_vec_content(d, poly, len);
        fmpz_gcd(d, d, f);

        if (c > 0)
        {
            _fmpz_vec_scalar_divexact_fmpz(rpoly, poly, len, d);
            fmpz_mul_si(rden, den, c / fmpz_get_si(d));
        }
        else
        {
            ulong q = (- (ulong) c) / fmpz_get_ui(d);

            fmpz_neg(d, d);
            _fmpz_vec_scalar_divexact_fmpz(rpoly, poly, len, d);
            fmpz_mul_ui(rden, den, q);
        }

        fmpz_clear(d);
        fmpz_clear(f);
    }
}
コード例 #8
0
static void
set_loops(gamma_t * l, slong n, fmpz * row, slong m1, slong len)
{
    slong i, j;
    /* coeff k * (m-1) + s -> gamma_k^s */
    for (i = 0, j = 0; i < n; i++)
    {
        for (; fmpz_is_zero(row + j); j++);
        if (!fmpz_fits_si(row + j))
        {
            flint_printf("error: coefficient too large in basis\n");
            abort();
        }
        l[i].coeff = fmpz_get_si(row + j);
        l[i].shift = j % m1;
        l[i].index = j / m1;
    }
}
コード例 #9
0
ファイル: p-mullow.c プロジェクト: clear731/lattice
int
main(int argc, char** argv)
{
    double s[nalgs];

    int c, n, lenf, leng, len, ext, reps = 0;
    fmpz_t p, temp;
    TEMPLATE(T, poly_t) f, g, h;
    TEMPLATE(T, ctx_t) ctx;
    
    FLINT_TEST_INIT(state);
    
    fmpz_init(p);
    fmpz_set_str(p, argv[1], 10);

    fmpz_init(temp);
       
    fmpz_set_str(temp, argv[2], 10);
    ext = fmpz_get_si(temp);

    lenf = atol(argv[3]);
    leng = atol(argv[4]);
    len = atol(argv[5]);

    TEMPLATE(T, ctx_init)(ctx, p, ext, "a");

    TEMPLATE(T, poly_init)(f, ctx);
    TEMPLATE(T, poly_init)(g, ctx);
    TEMPLATE(T, poly_init)(h, ctx);

    for (c = 0; c < nalgs; c++)
    {
        s[c] = 0.0;
    }
       
    for (n = 0; n < ncases; n++)
    {
        double t[nalgs];
        int l, loops = 1;

        /*
           Construct random elements of fq
        */
        {
            TEMPLATE(T, poly_randtest_monic)(f, state, lenf, ctx);
            TEMPLATE(T, poly_randtest_monic)(g, state, leng, ctx);
        }
        
    loop:
        t[0] = 0.0;
        init_clock(0);
        prof_start();
        for (l = 0; l < loops; l++)
        {
            TEMPLATE(T, poly_mullow_classical)(h, f, g, len, ctx);
        }
        prof_stop();
        t[0] += get_clock(0);

        t[1] = 0.0;
        init_clock(0);
        prof_start();
        for (l = 0; l < loops; l++)
        {
            TEMPLATE(T, poly_mullow_KS)(h, f, g, len, ctx);
        }
        prof_stop();
        t[1] += get_clock(0);

        for (c = 0; c < nalgs; c++)
            if (t[c] * FLINT_CLOCK_SCALE_FACTOR <= cpumin)
            {
                loops *= 10;
                goto loop;
            }
        
        for (c = 0; c < nalgs; c++)
            s[c] += t[c];
        reps += loops;
    }
        
    for (c = 0; c < nalgs; c++)
    {
        flint_printf("%20f ", s[c] / (double) reps);
        fflush(stdout);
    }
    printf("\n");
        
    TEMPLATE(T, poly_clear)(h, ctx);
    TEMPLATE(T, poly_clear)(f, ctx);
    TEMPLATE(T, poly_clear)(g, ctx);
    TEMPLATE(T, ctx_clear)(ctx);
    fmpz_clear(p);
    fmpz_clear(temp);

    FLINT_TEST_CLEANUP(state);
    
    return 0;
}
コード例 #10
0
ファイル: nilpotency_degree.c プロジェクト: argriffing/arb
slong
bool_mat_nilpotency_degree(const bool_mat_t A)
{
    slong n;

    if (!bool_mat_is_square(A))
    {
        flint_printf("bool_mat_nilpotency_degree: a square matrix is required!\n");
        abort();
    }

    if (bool_mat_is_empty(A))
        return 0;

    n = bool_mat_nrows(A);

    if (n == 1)
    {
        return bool_mat_get_entry(A, 0, 0) ? -1 : 1;
    }
    else
    {
        _toposort_s s;
        slong i;
        int has_cycle;
        int result;

        _toposort_init(&s, n);

        for (has_cycle = 0, i = 0; !has_cycle && i < n; i++)
            if (!s.v[i])
                has_cycle = _toposort_visit(&s, A, i);

        if (has_cycle)
        {
            result = -1;
        }
        else
        {
            /* Find the length of the longest path within the DAG */
            /* http://stackoverflow.com/a/10737524/4072759 */

            slong x, y, z;
            slong max_overall;
            fmpz_mat_t E;

            fmpz_mat_init(E, n, n);
            fmpz_mat_zero(E);
            max_overall = 0;
            for (i = n - 1; i >= 0; i--)
            {
                slong max_in = 0;
                y = s.post[i];
                for (x = 0; x < n; x++)
                {
                    max_in = FLINT_MAX(max_in,
                                       fmpz_get_si(fmpz_mat_entry(E, x, y)));
                }
                for (z = 0; z < n; z++)
                {
                    if (bool_mat_get_entry(A, y, z))
                    {
                        fmpz_set_si(fmpz_mat_entry(E, y, z), max_in + 1);
                        max_overall = FLINT_MAX(max_overall, max_in + 1);
                    }
                }
            }
            fmpz_mat_clear(E);
            result = max_overall + 1;
        }
        _toposort_clear(&s);
        return result;
    }
}
コード例 #11
0
int main()
{
    slong iter;
    flint_rand_t state;

    flint_printf("all_pairs_longest_walk....");
    fflush(stdout);

    flint_randinit(state);

    for (iter = 0; iter < 1000 * arb_test_multiplier(); iter++)
    {
        slong m, degree;
        bool_mat_t A;
        fmpz_mat_t B, C, D;

        m = n_randint(state, 50);

        bool_mat_init(A, m, m);
        fmpz_mat_init(B, m, m);
        fmpz_mat_init(C, m, m);
        fmpz_mat_init(D, m, m);

        bool_mat_randtest(A, state);

        degree = bool_mat_all_pairs_longest_walk(B, A);

        /* entrywise reasonability */
        if (!_is_superficially_ok_entrywise(B))
        {
            flint_printf("FAIL (entrywise)\n");
            bool_mat_print(A); flint_printf("\n");
            fmpz_mat_print_pretty(B); flint_printf("\n");
            abort();
        }

        /* nilpotency degree */
        {
            slong nildegree = bool_mat_nilpotency_degree(A);
            if (nildegree != degree + 1)
            {
                flint_printf("FAIL (nilpotency degree)\n");
                bool_mat_print(A); flint_printf("\n");
                fmpz_mat_print_pretty(B); flint_printf("\n");
                flint_printf("nildegree=%wd degree=%wd\n", nildegree, degree);
                abort();
            }
        }

        /* transitive closure */
        {
            slong i, j;
            bool_mat_t U, V;

            bool_mat_init(U, m, m);
            bool_mat_transitive_closure(U, A);

            bool_mat_init(V, m, m);
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < m; j++)
                {
                    slong x = fmpz_get_si(fmpz_mat_entry(B, i, j));
                    bool_mat_set_entry(V, i, j, (x != -1 && x != 0));
                }
            }

            if (!bool_mat_equal(U, V))
            {
                flint_printf("FAIL (transitive closure)\n");
                bool_mat_print(A); flint_printf("\n");
                fmpz_mat_print_pretty(B); flint_printf("\n");
                bool_mat_print(U); flint_printf("\n");
                bool_mat_print(V); flint_printf("\n");
                abort();
            }

            bool_mat_clear(U);
            bool_mat_clear(V);
        }

        /* test commutativity of all-pairs-longest-walk with permutation */
        {
            bool_mat_t Ap;
            slong *perm;

            bool_mat_init(Ap, m, m);
            perm = flint_malloc(m * sizeof(slong));
            _perm_randtest(perm, m, state);

            /* C is the all-pairs-longest-walk of the permutation of A */
            _bool_mat_permute(Ap, A, perm);
            bool_mat_all_pairs_longest_walk(C, Ap);

            /* D is the permutation of the all-pairs-longest-walk of A */
            _fmpz_mat_permute(D, B, perm);

            if (!fmpz_mat_equal(C, D))
            {
                flint_printf("FAIL (permutation)\n");
                bool_mat_print(A); flint_printf("\n");
                fmpz_mat_print_pretty(B); flint_printf("\n");
                fmpz_mat_print_pretty(C); flint_printf("\n");
                fmpz_mat_print_pretty(D); flint_printf("\n");
                abort();
            }

            flint_free(perm);
            bool_mat_clear(Ap);
        }

        bool_mat_clear(A);
        fmpz_mat_clear(B);
        fmpz_mat_clear(C);
        fmpz_mat_clear(D);
    }

    /* use powering to check small random examples */
    for (iter = 0; iter < 1000 * arb_test_multiplier(); iter++)
    {
        slong m;
        bool_mat_t A;
        fmpz_mat_t B, C;

        m = n_randint(state, 10);

        bool_mat_init(A, m, m);
        fmpz_mat_init(B, m, m);
        fmpz_mat_init(C, m, m);

        bool_mat_randtest(A, state);
        bool_mat_all_pairs_longest_walk(B, A);

        _brute_force_all_pairs_longest_walk(C, A);

        if (!fmpz_mat_equal(B, C))
        {
            flint_printf("FAIL (powering)\n");
            bool_mat_print(A); flint_printf("\n");
            fmpz_mat_print_pretty(B); flint_printf("\n");
            fmpz_mat_print_pretty(C); flint_printf("\n");
            abort();
        }

        bool_mat_clear(A);
        fmpz_mat_clear(B);
        fmpz_mat_clear(C);
    }

    /* special matrices */
    {
        slong m;
        for (m = 1; m < 100; m++)
        {
            slong i, j, length;
            bool_mat_t A;
            fmpz_mat_t B;

            bool_mat_init(A, m, m);
            fmpz_mat_init(B, m, m);

            /* directed path */
            {
                bool_mat_directed_path(A);

                length = bool_mat_all_pairs_longest_walk(B, A);
                if (length != m-1)
                {
                    flint_printf("FAIL (directed path)\n");
                    bool_mat_print(A); flint_printf("\n");
                    fmpz_mat_print_pretty(B); flint_printf("\n");
                    flint_printf("m=%wd length=%wd\n", m, length);
                    abort();
                }

                for (i = 0; i < m; i++)
                {
                    for (j = 0; j < m; j++)
                    {
                        if (fmpz_get_si(fmpz_mat_entry(B, i, j)) !=
                            FLINT_MAX(-1, j - i))
                        {
                            flint_printf("FAIL (directed path)\n");
                            bool_mat_print(A); flint_printf("\n");
                            fmpz_mat_print_pretty(B); flint_printf("\n");
                            abort();
                        }
                    }
                }
            }

            /* directed cycle */
            {
                bool_mat_directed_cycle(A);

                length = bool_mat_all_pairs_longest_walk(B, A);
                if (length != -2)
                {
                    flint_printf("FAIL (directed cycle)\n");
                    bool_mat_print(A); flint_printf("\n");
                    fmpz_mat_print_pretty(B); flint_printf("\n");
                    flint_printf("m=%wd length=%wd\n", m, length);
                    abort();
                }

                for (i = 0; i < m; i++)
                {
                    for (j = 0; j < m; j++)
                    {
                        if (fmpz_get_si(fmpz_mat_entry(B, i, j)) != -2)
                        {
                            flint_printf("FAIL (directed cycle)\n");
                            bool_mat_print(A); flint_printf("\n");
                            fmpz_mat_print_pretty(B); flint_printf("\n");
                            abort();
                        }
                    }
                }
            }

            bool_mat_clear(A);
            fmpz_mat_clear(B);
        }
    }

    flint_randclear(state);
    flint_cleanup();
    flint_printf("PASS\n");
    return EXIT_SUCCESS;
}
コード例 #12
0
ファイル: decrypt.c プロジェクト: theSwan/RLWE
int main(int argc, char *args[])/* setup.txt, ct.txt{ct->lev, ct->row, ct->col, ct}, sk.txt{row, col, poly}*/
{
        FILE *fp;
        
        if((fp = fopen(args[1], "r")) == NULL)
        {
                printf("file read error\n");
                exit(0);
        }
        
        fmpz_t tmp;
        fmpz_init(tmp);
        fgets(str, 100, fp);
        fmpz_set_str(tmp, str, 10);
        
        long lev, d, i, j;
        lev = fmpz_get_si(tmp);
        fgets(str, 100, fp);
        fmpz_set_str(tmp, str, 10);
        d = fmpz_get_si(tmp);
        
        fmpz_poly_t fx;
        fmpz_poly_init(fx);
        fmpz_poly_set_coeff_si(fx, 0, 1);
        fmpz_poly_set_coeff_si(fx, d, 1);
        
        param_node_t *ph, *pr, *ps, *param;
        ph = param_node_init(ph);
        
        ps = ph;
        for( i = 0 ; i <= lev; i++ ) {
                pr = param_node_init(pr);
                fgets(str, 100, fp);
                fmpz_set_str(pr->q, str, 10);
                fgets(str, 100, fp);
                fmpz_set_str(tmp, str, 10);
                pr->n = fmpz_get_si(tmp);
                fgets(str, 100, fp);
                fmpz_set_str(tmp, str, 10);
                pr->bign = fmpz_get_si(tmp);
                ps->next = pr;
                ps = pr;
        }
        ps->next = NULL;
        fclose(fp);
        
        param = ph->next;
        
        long row, col, ctlev;
        
        if((fp = fopen(args[2], "r")) == NULL)
        {
                printf("file read error\n");
                exit(0);
        }
        
        fgets(str, 30, fp);
        fmpz_set_str(tmp, str,10);
        ctlev = fmpz_get_si(tmp);
        
        fgets(str, 30, fp);
        fmpz_set_str(tmp, str,10);
        row = fmpz_get_si(tmp);
        
        fgets(str, 30, fp);
        fmpz_set_str(tmp, str,10);
        col = fmpz_get_si(tmp);
        
        fmpz_poly_mat_t ct;
        fmpz_poly_mat_init(ct, row, col);
        for( i = 0 ; i < row ; i++) {
                for(j = 0; j < col ; j++) {
                        fgets(str, 100000, fp);
                        fmpz_poly_set_str(fmpz_poly_mat_entry(ct, i, j), str);
                }
        }
        
        fclose(fp);
        
        
        if((fp = fopen(args[3], "r")) == NULL)
        {
                printf("file read error\n");
                exit(0);
        }
        
        long l;
        sk_node_t *sh, *ss, *sr;
        sh = (sk_node_t *)malloc(sizeof(sk_node_t));
        
        ss = sh;
        for(l = 0; l <= lev ; l++){
                sr = (sk_node_t *)malloc(sizeof(sk_node_t));
                fgets(str, 30, fp);
                fmpz_set_str(tmp, str,10);
                row = fmpz_get_si(tmp);
                
                
                fgets(str, 30, fp);
                fmpz_set_str(tmp, str,10);
                col = fmpz_get_si(tmp);
                
                fmpz_poly_mat_init(sr->sk, row, col);
                
                for( i = 0 ; i < row ; i++) {
                        for(j = 0; j < col ; j++) {
                                fgets(str, 100000, fp);
                                fmpz_poly_set_str(fmpz_poly_mat_entry(sr->sk, i, j), str);
                        }
                }
                ss->next = sr;
                ss = sr;
        }
        ss->next = NULL;
        
        fclose(fp);
        
        sh = sh->next;
        while(lev > ctlev){
                sh = sh->next;
                param = param->next;
                lev--;
        }
        
        fmpz_poly_t ms;
        fmpz_poly_init(ms);
        e_decrypt(ms, param, sh->sk, ct, fx);
        fmpz_poly_print(ms);
        printf("\n");
        return 0;
        
}
コード例 #13
0
ファイル: diagfrob.c プロジェクト: SPancratz/deformation
void diagfrob(padic_mat_t F, const fmpz *a, long n, long d, long N, 
              const padic_ctx_t ctx, const int verbose)
{
    const fmpz *P = ctx->p;
    const long p  = fmpz_get_si(P);

    const long delta = diagfrob_delta(n, P);
    const long N2    = N - n + 2 * (padic_val_fac_ui(n - 1, P) + n + delta);
    const long M     = (p * p * (N2 + n_clog(N2 + 3, p) + 4) + (p - 2))
                       / (p - 1) - 1;

    mon_t *B;
    long *iB, lenB, lo, hi;

    long i, j, k, *u, *v;

    long **C, *lenC;

    fmpz *dinv, **mu;

    clock_t t0 = 0, t1 = 0;
    double t;

    gmc_basis_sets(&B, &iB, &lenB, &lo, &hi, n, d);

if (verbose)
{
    printf("Frobenius on the diagonal fibre\n");
    printf("N  = %ld\n", N);
    printf("N2 = %ld\n", N2);
    printf("M  = %ld\n", M);
}

if (verbose)
{
    printf("Basis for H_{dR}^%ld(U)\n", n);
    gmc_basis_print(B, iB, lenB, n, d);
    printf("\n");
}

    C    = malloc((n + 1) * sizeof(long *));
    C[0] = malloc((n + 1) * lenB * sizeof(long));
    for (i = 1; i <= n; i++)
    {
        C[i] = C[i-1] + lenB;
    }
    lenC = malloc((n + 1) * sizeof(long));

    for (i = 0; i <= n; i++)
    {
        _congruence_class(C[i], &lenC[i], i, B, lenB, n, d, p);
    }

    dinv  = _fmpz_vec_init(M/p + 1);
    mu    = malloc((n + 1) * sizeof(fmpz *));
    for (i = 0; i <= n; i++)
    {
        mu[i] = _fmpz_vec_init(((M + 1 + p - 1) / p) * lenC[i]);
    }

    u = malloc((n + 1) * sizeof(long));
    v = malloc((n + 1) * sizeof(long));

if (verbose)
{
    printf("Sequence d^{-r}\n");
    t0 = clock();
}

    precompute_dinv(dinv, M, d, p, N2);

if (verbose)
{
    t1 = clock();
    t  = (double) (t1 - t0) / CLOCKS_PER_SEC;
    printf("T = %f\n", t);
}

if (verbose)
{
    printf("Sequence mu_{m}\n");
    t0 = clock();
}

    precompute_muex(mu, M, (const long **) C, lenC, a, n, p, N2);  /* XXX */

if (verbose)
{
    t1 = clock();
    t  = (double) (t1 - t0) / CLOCKS_PER_SEC;
    printf("T = %f\n", t);
}

if (verbose)
{
    printf("Matrix F\n");
    t0 = clock();
}

    for (i = 0; i < lenB; i++)
        for (j = 0; j < lenB; j++)
        {
            for (k = 0; k <= n; k++)
            {
                u[k] = mon_get_exp(B[i], k);
                v[k] = mon_get_exp(B[j], k);
                if ((p * (u[k] + 1) - (v[k] + 1)) % d != 0)
                {
                     break;
                }
            }
            if (k <= n)
            {
                fmpz_zero(padic_mat_entry(F, i, j));
            }
            else
            {
                long o;

                entry(padic_mat_entry(F, i, j), &o, 
                      u, v, a, dinv, (const fmpz **) mu, M, (const long **) C, lenC, n, d, p, N, N2);

                if (o != - delta)
                {
                    fmpz_t w;
                    fmpz_init(w);
                    fmpz_pow_ui(w, P, o + delta);
                    fmpz_mul(padic_mat_entry(F, i, j), 
                             padic_mat_entry(F, i, j), w);
                    fmpz_clear(w);
                }
            }
        }

    padic_mat_val(F) = - delta;
    _padic_mat_canonicalise(F, ctx);

if (verbose)
{
    t1 = clock();
    t  = (double) (t1 - t0) / CLOCKS_PER_SEC;
    printf("T = %f\n", t);
}

    _fmpz_vec_clear(dinv, M/p + 1);
    for (i = 0; i <= n; i++)
    {
        _fmpz_vec_clear(mu[i], ((M + 1 + p - 1) / p) * lenC[i]);
    }
    free(mu);
    free(C[0]);
    free(C);
    free(lenC);
    free(u);
    free(v);
    free(B);
    free(iB);
}
コード例 #14
0
int
main(void)
{
    int i;
    FLINT_TEST_INIT(state);
    

    flint_printf("reconstruct_fmpz_2....");
    fflush(stdout);

    for (i = 0; i < 10000; i++)
    {
        int result;
        int modresult;
        int special_case;
        fmpq_t x, y;
        fmpz_t mod, res, N, D, t;
        mpz_t tmp;

        fmpq_init(x);
        fmpq_init(y);
        fmpz_init(mod);
        fmpz_init(res);
        fmpz_init(N);
        fmpz_init(D);
        fmpz_init(t);
        mpz_init(tmp);

        fmpq_randtest(x, state, 100);

        fmpz_abs(N, fmpq_numref(x));
        fmpz_set(D, fmpq_denref(x));

        /* Randomly generate larger bounds */
        if (n_randint(state, 2))
        {
            fmpz_randtest_not_zero(t, state, 100);
            fmpz_abs(t, t);
            fmpz_mul(N, N, t);
        }
        if (n_randint(state, 2))
        {
            fmpz_randtest_not_zero(t, state, 100);
            fmpz_abs(t, t);
            fmpz_mul(D, D, t);
        }

        fmpz_mul(mod, N, D);
        fmpz_mul_ui(mod, mod, UWORD(2));
        /* Next prime greater than or equal */
        fmpz_get_mpz(tmp, mod);
        flint_mpz_sub_ui(tmp, tmp, UWORD(1));
        mpz_nextprime(tmp, tmp);
        fmpz_set_mpz(mod, tmp);

        modresult = fmpq_mod_fmpz(res, x, mod);
        result = fmpq_reconstruct_fmpz_2(y, res, mod, N, D);

        /* Special case: both 1 and -1 have residue 1 mod 2.
           There's probably no particular reason to disallow this. */
        special_case = (fmpz_cmp_ui(mod, UWORD(2)) == 0 &&
                        fmpz_get_si(&x->num) == WORD(-1) &&
                        fmpz_cmp_ui(&x->den, UWORD(1)) == 0);

        if (special_case)
        {
            if (!modresult || !result ||
                !fmpz_is_one(&y->num) || !fmpz_is_one(&y->den))
            {
                flint_printf("FAIL: special case: -1 mod 2\n");
                abort();
            }
        }
        else if (!modresult || !result || !fmpq_equal(x, y))
        {
            flint_printf("FAIL: reconstruction failed\n");
            flint_printf("input = ");
            fmpq_print(x);
            flint_printf("\nmodulus = ");
            fmpz_print(mod);
            flint_printf("\nresidue = ");
            fmpz_print(res);
            flint_printf("\nreconstructed = ");
            fmpq_print(y);
            flint_printf("\nfmpq_mod_fmpz return value = %d", modresult);
            flint_printf("\nfmpq_reconstruct_fmpz return value = %d", result);
            flint_printf("\n");
            abort();
        }

        fmpq_clear(x);
        fmpq_clear(y);
        fmpz_clear(mod);
        fmpz_clear(res);
        fmpz_clear(N);
        fmpz_clear(D);
        fmpz_clear(t);
        mpz_clear(tmp);
    }

    

    FLINT_TEST_CLEANUP(state);
    flint_printf("PASS\n");
    return 0;
}