예제 #1
0
/** Compute the column HNF with transformation matrix H = AU.
 *
 * TODO: Implement directly as in fmpz_mat_hnf_transform().
 */
void fmpz_mat_hnf_col_transform(fmpz_mat_t H, fmpz_mat_t U, \
        const fmpz_mat_t A)
{
    fmpz_mat_t tA, H_, U_;

    /* Compute row HNF for tA: H_ = U_*tA. */
    fmpz_mat_init(tA, fmpz_mat_ncols(A), fmpz_mat_nrows(A));
    fmpz_mat_transpose(tA, A);
    fmpz_mat_init(H_, fmpz_mat_nrows(tA), fmpz_mat_ncols(tA));
    fmpz_mat_init(U_, fmpz_mat_nrows(tA), fmpz_mat_nrows(tA));

    /* Argh!  Pernet-Stein algorithm can fail (with low probability.)  Why
     * doesn't fmpz_mat_hnf_transform() check this?!  Alex has this fixed, so
     * soon this should no longer be needed. */
    do {
        fmpz_mat_hnf_transform(H_, U_, tA);
    } while (!fmpz_mat_is_in_hnf(H_));

    fmpz_mat_clear(tA);

    /* Transpose H_, U_: H = A*U is column HNF. */
    fmpz_mat_transpose(H, H_);
    fmpz_mat_transpose(U, U_);
    fmpz_mat_clear(H_);
    fmpz_mat_clear(U_);
};
예제 #2
0
void
_fmpz_holonomic_forward_bsplit_fmpz(fmpz_mat_t M, fmpz_t Q,
    const fmpz_holonomic_t op, long start, long a, long b)
{
    long r = fmpz_holonomic_order(op);

    if (b - a == 1)
    {
        _fmpz_holonomic_eval_companion_matrix_fmpz(M, Q, op, start + a);
    }
    else
    {
        fmpz_mat_t L, R;
        fmpz_t Q2;
        long m = a + (b - a) / 2;

        fmpz_mat_init(L, r, r);
        fmpz_mat_init(R, r, r);
        fmpz_init(Q2);

        _fmpz_holonomic_forward_bsplit_fmpz(L, Q, op, start, a, m);
        _fmpz_holonomic_forward_bsplit_fmpz(R, Q2, op, start, m, b);

        fmpz_mat_mul(M, R, L);
        fmpz_mul(Q, Q, Q2);

        fmpz_mat_clear(L);
        fmpz_mat_clear(R);
        fmpz_clear(Q2);
    }
}
예제 #3
0
void
fmpq_mat_mul_fmpz_mat(fmpq_mat_t C, const fmpq_mat_t A, const fmpz_mat_t B)
{
    slong i, j;

    fmpz_mat_t Aclear;
    fmpz_mat_t Cclear;

    fmpz * Aden;

    fmpz_mat_init(Aclear, A->r, A->c);
    fmpz_mat_init(Cclear, A->r, B->c);

    Aden = _fmpz_vec_init(A->r);

    fmpq_mat_get_fmpz_mat_rowwise(Aclear, Aden, A);
    fmpz_mat_mul(Cclear, Aclear, B);

    for (i = 0; i < C->r; i++)
    {
        for (j = 0; j < C->c; j++)
        {
            fmpz_set(fmpq_mat_entry_num(C, i, j), fmpz_mat_entry(Cclear, i, j));
            fmpz_set(fmpq_mat_entry_den(C, i, j), Aden + i);
            fmpq_canonicalise(fmpq_mat_entry(C, i, j));
        }
    }

    fmpz_mat_clear(Aclear);
    fmpz_mat_clear(Cclear);

    _fmpz_vec_clear(Aden, A->r);
}
예제 #4
0
void
symplectic_basis(homol_t alpha, homol_t beta, const tree_t tree, sec_t c)
{
    slong i, len = (c.d-1)*(c.m-1);
    fmpz_mat_t m, p;
    fmpz_mat_init(m, len, len);
    fmpz_mat_init(p, len, len);
    /* compute big intersection matrix, size len = (d-1)*(m-1) */
    intersection_tree(m, tree, c.d, c.m);
    symplectic_reduction(p, m, c.g);
    fmpz_mat_clear(m);

    for (i = 0; i < c.g; i++)
    {
        slong n;
        fmpz * row;

        /* loops alpha = even coordinates */
        row = p->rows[2 * i];
        n = count_coeffs(row, len);
        loop_init(&alpha[i], n);
        set_loops(alpha[i].l, n, row, c.m - 1, len);

        /* loops beta = odd coordinates */
        row = p->rows[2 * i + 1];
        n = count_coeffs(row, len);
        loop_init(&beta[i], n);
        set_loops(beta[i].l, n, row, c.m - 1, len);

    }
    fmpz_mat_clear(p);
}
예제 #5
0
파일: t-pow.c 프로젝트: clear731/lattice
int main(void)
{
    slong i;
    FLINT_TEST_INIT(state);

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

    

    for (i = 0; i < 100 * flint_test_multiplier(); i++)
    {
        fmpz_mat_t A, B, C;
        slong i, n;
        ulong e;

        n = n_randint(state, 10);
        e = n_randint(state, 20);

        fmpz_mat_init(A, n, n);
        fmpz_mat_init(B, n, n);
        fmpz_mat_init(C, n, n);

        fmpz_mat_randtest(A, state, n_randint(state, 200) + 1);
        fmpz_mat_randtest(B, state, n_randint(state, 200) + 1);

        /* Make sure noise in the output is ok */
        fmpz_mat_randtest(B, state, n_randint(state, 200) + 1);

        fmpz_mat_pow(B, A, e);

        fmpz_mat_one(C);
        for (i = 0; i < e; i++)
            fmpz_mat_mul(C, C, A);

        if (!fmpz_mat_equal(C, B))
        {
            flint_printf("FAIL: results not equal\n");
            abort();
        }

        fmpz_mat_pow(A, A, e);

        if (!fmpz_mat_equal(A, B))
        {
            flint_printf("FAIL: aliasing failed\n");
            abort();
        }

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

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
예제 #6
0
파일: t-mul.c 프로젝트: clear731/lattice
int main(void)
{
    fmpz_mat_t A, B, C, D;
    slong i;
    FLINT_TEST_INIT(state);

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

    

    for (i = 0; i < 100 * flint_test_multiplier(); i++)
    {
        slong m, n, k;

        m = n_randint(state, 50);
        n = n_randint(state, 50);
        k = n_randint(state, 50);

        fmpz_mat_init(A, m, n);
        fmpz_mat_init(B, n, k);
        fmpz_mat_init(C, m, k);
        fmpz_mat_init(D, m, k);

        fmpz_mat_randtest(A, state, n_randint(state, 200) + 1);
        fmpz_mat_randtest(B, state, n_randint(state, 200) + 1);

        /* Make sure noise in the output is ok */
        fmpz_mat_randtest(C, state, n_randint(state, 200) + 1);

        fmpz_mat_mul(C, A, B);
        fmpz_mat_mul_classical_inline(D, A, B);

        if (!fmpz_mat_equal(C, D))
        {
            flint_printf("FAIL: results not equal\n");
            abort();
        }

        fmpz_mat_mul(A, A, B);

        if (!fmpz_mat_equal(A, C))
        {
            flint_printf("FAIL: aliasing failed\n");
            abort();
        }

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

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
예제 #7
0
파일: det_divisor.c 프로젝트: goens/flint2
void
fmpz_mat_det_divisor(fmpz_t d, const fmpz_mat_t A)
{
    fmpz_mat_t X, B;
    fmpz_t t, u, v, mod;
    long i, n;
    int success;

    n = A->r;

    fmpz_mat_init(B, n, 1);
    fmpz_mat_init(X, n, 1);
    fmpz_init(t);
    fmpz_init(u);
    fmpz_init(v);
    fmpz_init(mod);

    /* Create a "random" vector */
    for (i = 0; i < n; i++)
    {
        fmpz_set_si(fmpz_mat_entry(B, i, 0), 2*(i % 2) - 1);
    }

    success = fmpz_mat_solve_dixon(X, mod, A, B);

    if (success)
    {
        fmpz_one(d);
        for (i = 0; i < n; i++)
        {
            fmpz_mul(t, d, fmpz_mat_entry(X, i, 0));
            fmpz_fdiv_qr(u, t, t, mod);
            if (!_fmpq_reconstruct_fmpz(u, v, t, mod))
            {
                printf("Exception: fmpz_mat_det_divisor: "
                       "rational reconstruction failed!\n");
                abort();
            }

            fmpz_mul(d, v, d);
        }
    }
    else
    {
        fmpz_zero(d);
    }

    fmpz_mat_clear(B);
    fmpz_mat_clear(X);
    fmpz_clear(t);
    fmpz_clear(u);
    fmpz_clear(v);
    fmpz_clear(mod);
}
예제 #8
0
int
main(void)
{
    slong m, n, rep;
    FLINT_TEST_INIT(state);

    flint_printf("add/sub/neg....");
    fflush(stdout);

    

    for (rep = 0; rep < 1000 * flint_test_multiplier(); rep++)
    {
        fmpz_mat_t A;
        fmpz_mat_t B;
        fmpz_mat_t C;

        m = n_randint(state, 20);
        n = n_randint(state, 20);

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

        fmpz_mat_randtest(A, state, 100);
        fmpz_mat_randtest(B, state, 100);

        fmpz_mat_neg(C, A);
        fmpz_mat_add(A, A, B);
        fmpz_mat_sub(A, A, B);
        fmpz_mat_neg(A, A);

        if (!fmpz_mat_equal(A, C))
        {
            flint_printf("FAIL: matrices not equal!\n");
            abort();
        }

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

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
예제 #9
0
파일: t-add_sub.c 프로젝트: goens/flint2
int
main(void)
{
    long m, n, rep;
    flint_rand_t state;

    printf("add/sub/neg....");
    fflush(stdout);

    flint_randinit(state);

    for (rep = 0; rep < 10000; rep++)
    {
        fmpz_mat_t A;
        fmpz_mat_t B;
        fmpz_mat_t C;

        m = n_randint(state, 20);
        n = n_randint(state, 20);

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

        fmpz_mat_randtest(A, state, 100);
        fmpz_mat_randtest(B, state, 100);

        fmpz_mat_neg(C, A);
        fmpz_mat_add(A, A, B);
        fmpz_mat_sub(A, A, B);
        fmpz_mat_neg(A, A);

        if (!fmpz_mat_equal(A, C))
        {
            printf("FAIL: matrices not equal!\n");
            abort();
        }

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

    flint_randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return 0;
}
예제 #10
0
파일: inv.c 프로젝트: goens/flint2
int
fmpz_mat_inv(fmpz_mat_t B, fmpz_t den, const fmpz_mat_t A)
{
    long dim = A->r;

    if (dim == 0)
    {
        fmpz_one(den);
        return 1;
    }
    else if (dim == 1)
    {
        fmpz_set(den, A->entries);
        fmpz_one(B->entries);
        return !fmpz_is_zero(den);
    }
    else if (dim == 2)
    {
        _fmpz_mat_inv_2x2(B->rows, den, A->rows);
        return !fmpz_is_zero(den);
    }
    else
    {
        fmpz_mat_t I;
        long i;
        int success;

        fmpz_mat_init(I, dim, dim);
        for (i = 0; i < dim; i++)
            fmpz_one(fmpz_mat_entry(I, i, i));
        success = fmpz_mat_solve_fflu(B, den, A, I);
        fmpz_mat_clear(I);
        return success;
    }
}
예제 #11
0
int
main(void)
{
    int i;
    FLINT_TEST_INIT(state);
    

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

    for (i = 0; i < 1000 * flint_test_multiplier(); i++)
    {
        fmpz_mat_t A;
        slong rows = n_randint(state, 10);
        slong cols = n_randint(state, 10);

        fmpz_mat_init(A, rows, cols);

        if (fmpz_mat_is_square(A) != (rows == cols))
        {
            flint_printf("FAIL!\n");
            abort();
        }
        fmpz_mat_clear(A);
    }

    

    FLINT_TEST_CLEANUP(state);
    flint_printf("PASS\n");
    return 0;
}
예제 #12
0
파일: solve_fflu.c 프로젝트: goens/flint2
int
fmpz_mat_solve_fflu(fmpz_mat_t X, fmpz_t den,
                    const fmpz_mat_t A, const fmpz_mat_t B)
{
    fmpz_mat_t LU;
    long dim, *perm;
    int result;

    if (fmpz_mat_is_empty(A) || fmpz_mat_is_empty(B))
    {
        fmpz_one(den);
        return 1;
    }

    dim = fmpz_mat_nrows(A);
    perm = _perm_init(dim);
    fmpz_mat_init_set(LU, A);
    result = (fmpz_mat_fflu(LU, den, perm, LU, 1) == dim);

    if (result)
        fmpz_mat_solve_fflu_precomp(X, perm, LU, B);
    else
        fmpz_zero(den);

    _perm_clear(perm);
    fmpz_mat_clear(LU);
    return result;
}
예제 #13
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;
}
예제 #14
0
파일: mat-linalg.hpp 프로젝트: gblanco92/M2
 inline size_t nullSpace(const DMatQQFlint& A, 
                         DMatQQFlint& result_nullspace) 
 {
   fmpz_mat_t m1;
   fmpz_mat_t m2;
   fmpz_mat_init(m1, A.numRows(), A.numColumns());
   fmpz_mat_init(m2, A.numColumns(), A.numColumns());
   fmpq_mat_get_fmpz_mat_rowwise(m1, NULL, A.fmpq_mat());
   //fmpz_mat_print_pretty(m1);
   size_t nullity = fmpz_mat_nullspace(m2,m1);
   // now copy the first 'nullity' columns into result_nullspace
   result_nullspace.resize(A.numColumns(), nullity);
   for (size_t c = 0; c < nullity; c++)
     for (size_t r = 0; r < A.numColumns(); r++)
       fmpz_set(fmpq_numref(& result_nullspace.entry(r,c)), fmpz_mat_entry(m2,r,c));
   fmpz_mat_clear(m1);
   fmpz_mat_clear(m2);
   return nullity;
 }
예제 #15
0
파일: p-mul.c 프로젝트: goens/flint2
void sample(void * arg, ulong count)
{
    mat_mul_t * params = (mat_mul_t *) arg;
    long i, m = params->m, n = params->n, k = params->k;
    long bits = params->bits;
    int algorithm = params->algorithm;

    flint_rand_t rnd;
    fmpz_mat_t A, B, C;
    flint_rand_t state;
    flint_randinit(state);

    fmpz_mat_init(A, m, n);
    fmpz_mat_init(B, n, k);
    fmpz_mat_init(C, m, k);

    fmpz_mat_randbits(A, state, bits);
    fmpz_mat_randbits(B, state, bits);

    prof_start();

    if (algorithm == 0)
        for (i = 0; i < count; i++)
            fmpz_mat_mul(C, A, B);
    else if (algorithm == 1)
        for (i = 0; i < count; i++)
            fmpz_mat_mul_classical(C, A, B);
    else if (algorithm == 2)
        for (i = 0; i < count; i++)
            fmpz_mat_mul_classical_inline(C, A, B);
    else if (algorithm == 3)
        for (i = 0; i < count; i++)
            fmpz_mat_mul_multi_mod(C, A, B);

    prof_stop();

    fmpz_mat_clear(A);
    fmpz_mat_clear(B);
    fmpz_mat_clear(C);
    
    flint_randclear(state);
}
예제 #16
0
파일: mat-linalg.hpp 프로젝트: gblanco92/M2
 inline size_t rank(const DMatQQFlint& A) { 
   // fmpq_mat has no rank function.
   // So we clear denominators row-wise (or column-wise), and compute the rank of that matrix.
   fmpz_mat_t m1;
   fmpz_mat_init(m1, A.numRows(), A.numColumns());
   fmpq_mat_get_fmpz_mat_rowwise(m1, NULL, A.fmpq_mat());
   //fmpz_mat_print_pretty(m1);
   size_t rk = fmpz_mat_rank(m1);
   fmpz_mat_clear(m1);
   return rk;
 }
예제 #17
0
static void
obf_randomize_layer(obf_state_t *s, long nrows, long ncols,
                    encode_layer_randomization_flag_t rflag,
                    uint64_t n, fmpz_mat_t *mats)
{
    fmpz_t *fields;

    fields = s->vtable->sk->plaintext_fields(s->mmap);

    if (rflag & ENCODE_LAYER_RANDOMIZATION_TYPE_FIRST) {
        fmpz_mat_t first;
        _fmpz_mat_init_diagonal_rand(first, nrows, s->rand, fields[0]);
        fmpz_layer_mul_left(n, mats, first, fields[0]);
        fmpz_mat_clear(first);
    }
    if (rflag & ENCODE_LAYER_RANDOMIZATION_TYPE_LAST) {
        fmpz_mat_t last;
        _fmpz_mat_init_diagonal_rand(last, ncols, s->rand, fields[0]);
        fmpz_layer_mul_right(n, mats, last, fields[0]);
        fmpz_mat_clear(last);
    }

    if (rflag & ENCODE_LAYER_RANDOMIZATION_TYPE_FIRST
        && rflag & ENCODE_LAYER_RANDOMIZATION_TYPE_LAST) {
    } else if (rflag & ENCODE_LAYER_RANDOMIZATION_TYPE_FIRST) {
        fmpz_mat_init(*s->randomizer, ncols, ncols);
        fmpz_mat_init(*s->inverse, ncols, ncols);
        _fmpz_mat_init_square_rand(s, *s->randomizer, *s->inverse, ncols,
                                   s->rand, fields[0]);
        fmpz_layer_mul_right(n, mats, *s->randomizer, fields[0]);
    } else if (rflag & ENCODE_LAYER_RANDOMIZATION_TYPE_MIDDLE) {
        fmpz_layer_mul_left(n, mats, *s->inverse, fields[0]);
        fmpz_mat_clear(*s->randomizer);
        fmpz_mat_clear(*s->inverse);

        fmpz_mat_init(*s->randomizer, ncols, ncols);
        fmpz_mat_init(*s->inverse, ncols, ncols);
        _fmpz_mat_init_square_rand(s, *s->randomizer, *s->inverse, ncols,
                                   s->rand, fields[0]);
        fmpz_layer_mul_right(n, mats, *s->randomizer, fields[0]);
    } else if (rflag & ENCODE_LAYER_RANDOMIZATION_TYPE_LAST) {
        fmpz_layer_mul_left(n, mats, *s->inverse, fields[0]);
        fmpz_mat_clear(*s->randomizer);
        fmpz_mat_clear(*s->inverse);
    }

    {
        fmpz_t alpha;
        fmpz_init(alpha);
        for (uint64_t i = 0; i < n; ++i) {
            do {
                fmpz_randm_aes(alpha, s->rand, fields[0]);
            } while (fmpz_cmp_ui(alpha, 0) == 0);
            fmpz_mat_scalar_mul_fmpz(mats[i], mats[i], alpha);
        }
        fmpz_clear(alpha);
    }

    free(fields);
}
예제 #18
0
파일: det_bareiss.c 프로젝트: goens/flint2
void
fmpz_mat_det_bareiss(fmpz_t det, const fmpz_mat_t A)
{
    fmpz_mat_t tmp;

    if (A->r < 1)
    {
        fmpz_one(det);
        return;
    }

    fmpz_mat_init_set(tmp, A);
    _fmpz_mat_det_bareiss(det, tmp);
    fmpz_mat_clear(tmp);
}
예제 #19
0
파일: t-unit.c 프로젝트: hemmecke/flint2
int
main(void)
{
    long m, n, i, j, rep;
    flint_rand_t state;

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

    flint_randinit(state);

    for (rep = 0; rep < 1000; rep++)
    {
        fmpz_mat_t A;

        m = n_randint(state, 20);
        n = n_randint(state, 20);

        fmpz_mat_init(A, m, n);

        fmpz_mat_randtest(A, state, 100);
        fmpz_mat_unit(A);

        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                if (fmpz_cmp_ui(fmpz_mat_entry(A,i,j), i == j) != 0)
                {
                    printf("FAIL: nonzero entry\n");
                    abort();
                }
            }
        }

        fmpz_mat_clear(A);
    }

    flint_randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return 0;
}
예제 #20
0
파일: mul.c 프로젝트: hemmecke/flint2
void
fmpz_mat_mul(fmpz_mat_t C, const fmpz_mat_t A, const fmpz_mat_t B)
{
    long dim, m, n, k, ab, bb;

    m = A->r;
    n = A->c;
    k = B->c;

    if (C == A || C == B)
    {
        fmpz_mat_t t;
        fmpz_mat_init(t, m, k);
        fmpz_mat_mul(t, A, B);
        fmpz_mat_swap(C, t);
        fmpz_mat_clear(t);
        return;
    }

    dim = FLINT_MIN(FLINT_MIN(m, n), k);

    if (dim < 10)
    {
        fmpz_mat_mul_classical(C, A, B);
        return;
    }

    ab = fmpz_mat_max_bits(A);
    bb = fmpz_mat_max_bits(B);

    ab = FLINT_ABS(ab);
    bb = FLINT_ABS(bb);

    if (5*(ab + bb) > dim * dim)
    {
        fmpz_mat_mul_classical(C, A, B);
    }
    else
    {
        _fmpz_mat_mul_multi_mod(C, A, B, ab + bb + FLINT_BIT_COUNT(n) + 1);
    }
}
예제 #21
0
파일: dgsl.c 프로젝트: malb/gghlite-flint
void dgsl_mp_clear(dgsl_mp_t *self) {
  if (!self)
    return;
  _fmpz_vec_clear(self->c_z, fmpz_mat_ncols(self->B));
  _mpfr_vec_clear(self->c,   fmpz_mat_ncols(self->B));

  if (!fmpz_mat_is_empty(self->B)) {
    fmpz_mat_clear(self->B);
  }

  if (!mpfr_mat_is_empty(self->G)) {
    mpfr_mat_clear(self->G);
  }
  if(self->call == dgsl_mp_call_identity) {
    if(self->D) {
      dgs_disc_gauss_mp_clear(self->D[0]);
      free(self->D);
    }
  }
  mpfr_clear(self->sigma);
  free(self);
}
예제 #22
0
int
main(void)
{
    slong m, n, rep, res1, res2;
    FLINT_TEST_INIT(state);

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

    

    for (rep = 0; rep < 100 * flint_test_multiplier(); rep++)
    {
        fmpz_mat_t A;

        m = n_randint(state, 20);
        n = n_randint(state, 20);

        fmpz_mat_init(A, m, n);
        fmpz_mat_randtest(A, state, 1 + n_randint(state, 100));

        res1 = fmpz_mat_max_bits(A);
        res2 = _fmpz_vec_max_bits(A->entries, m*n);

        if (res1 != res2)
        {
            flint_printf("FAIL!\n");
            abort();
        }

        fmpz_mat_clear(A);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
예제 #23
0
파일: nullspace.c 프로젝트: goens/flint2
long
fmpz_mat_nullspace(fmpz_mat_t res, const fmpz_mat_t mat)
{
    long i, j, k, m, n, rank, nullity;
    long * pivots;
    long * nonpivots;
    fmpz_mat_t tmp;
    fmpz_t den;

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

    fmpz_mat_init_set(tmp, mat);
    fmpz_init(den);

    rank = fmpz_mat_rref(tmp, den, NULL, mat);
    nullity = n - rank;

    fmpz_mat_zero(res);
    if (rank == 0)
    {
        for (i = 0; i < nullity; i++)
            fmpz_one(res->rows[i] + i);
    }
    else if (nullity)
    {
        pivots = flint_malloc(rank * sizeof(long));
        nonpivots = flint_malloc(nullity * sizeof(long));

        for (i = j = k = 0; i < rank; i++)
        {
            while (fmpz_is_zero(tmp->rows[i] + j))
            {
                nonpivots[k] = j;
                k++;
                j++;
            }
            pivots[i] = j;
            j++;
        }
        while (k < nullity)
        {
            nonpivots[k] = j;
            k++;
            j++;
        }

        fmpz_set(den, tmp->rows[0] + pivots[0]);

        for (i = 0; i < nullity; i++)
        {
            for (j = 0; j < rank; j++)
                fmpz_set(res->rows[pivots[j]] + i, tmp->rows[j] + nonpivots[i]);
            fmpz_neg(res->rows[nonpivots[i]] + i, den);
        }

        flint_free(pivots);
        flint_free(nonpivots);
    }

    fmpz_clear(den);
    fmpz_mat_clear(tmp);

    return nullity;
}
예제 #24
0
int
main(void)
{
    fmpz_mat_t A;
    flint_rand_t state;
    long i, m;

    fmpz_t det1, det2;

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

    flint_randinit(state);

    for (i = 0; i < 10000; i++)
    {
        int proved = n_randlimb(state) % 2;
        m = n_randint(state, 10);

        fmpz_mat_init(A, m, m);

        fmpz_init(det1);
        fmpz_init(det2);

        fmpz_mat_randtest(A, state, 1+n_randint(state,200));

        fmpz_mat_det_bareiss(det1, A);
        fmpz_mat_det_multi_mod(det2, A, proved);

        if (!fmpz_equal(det1, det2))
        {
            printf("FAIL:\n");
            printf("different determinants!\n");
            fmpz_mat_print_pretty(A), printf("\n");
            printf("det1: "), fmpz_print(det1), printf("\n");
            printf("det2: "), fmpz_print(det2), printf("\n");
            abort();
        }

        fmpz_clear(det1);
        fmpz_clear(det2);
        fmpz_mat_clear(A);
    }

    for (i = 0; i < 10000; i++)
    {
        m = 2 + n_randint(state, 10);
        fmpz_mat_init(A, m, m);
        fmpz_init(det2);

        fmpz_mat_randrank(A, state, 1+n_randint(state, m - 1), 1+n_randint(state, 10));
        fmpz_mat_randops(A, state, n_randint(state, 2*m*m + 1));

        fmpz_mat_det_multi_mod(det2, A, 0);
        if (*det2)
        {
            printf("FAIL:\n");
            printf("expected zero determinant!\n");
            fmpz_mat_print_pretty(A), printf("\n");
            abort();
        }

        fmpz_mat_clear(A);
        fmpz_clear(det2);
    }

    flint_randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return 0;
}
예제 #25
0
파일: t-add.c 프로젝트: clear731/lattice
int
main(void)
{
    slong i;

    FLINT_TEST_INIT(state);

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

    /* Check evaluation homomorphism */
    for (i = 0; i < 100 * flint_test_multiplier(); i++)
    {
        fmpz_poly_mat_t A, B, C;
        fmpz_mat_t a, b, c, d;
        fmpz_t x;
        slong m, n, bits, deg;

        m = n_randint(state, 20);
        n = n_randint(state, 20);
        deg = 1 + n_randint(state, 10);
        bits = 1 + n_randint(state, 100);

        fmpz_poly_mat_init(A, m, n);
        fmpz_poly_mat_init(B, m, n);
        fmpz_poly_mat_init(C, m, n);

        fmpz_mat_init(a, m, n);
        fmpz_mat_init(b, m, n);
        fmpz_mat_init(c, m, n);
        fmpz_mat_init(d, m, n);

        fmpz_init(x);

        fmpz_poly_mat_randtest(A, state, deg, bits);
        fmpz_poly_mat_randtest(B, state, deg, bits);
        fmpz_poly_mat_add(C, A, B);

        fmpz_randtest(x, state, 1 + n_randint(state, 100));

        fmpz_poly_mat_evaluate_fmpz(a, A, x);
        fmpz_poly_mat_evaluate_fmpz(b, B, x);
        fmpz_poly_mat_evaluate_fmpz(d, C, x);
        fmpz_mat_add(c, a, b);

        if (!fmpz_mat_equal(c, d))
        {
            flint_printf("FAIL:\n");
            flint_printf("A:\n");
            fmpz_poly_mat_print(A, "x");
            flint_printf("B:\n");
            fmpz_poly_mat_print(B, "x");
            flint_printf("C:\n");
            fmpz_poly_mat_print(C, "x");
            flint_printf("\n");
            abort();
        }

        fmpz_poly_mat_clear(A);
        fmpz_poly_mat_clear(B);
        fmpz_poly_mat_clear(C);

        fmpz_mat_clear(a);
        fmpz_mat_clear(b);
        fmpz_mat_clear(c);
        fmpz_mat_clear(d);

        fmpz_clear(x);
    }

    /* Check aliasing C and A */
    for (i = 0; i < 100 * flint_test_multiplier(); i++)
    {
        fmpz_poly_mat_t A, B, C;
        slong m, n, bits, deg;

        m = n_randint(state, 20);
        n = n_randint(state, 20);
        deg = 1 + n_randint(state, 10);
        bits = 1 + n_randint(state, 100);

        fmpz_poly_mat_init(A, m, n);
        fmpz_poly_mat_init(B, m, n);
        fmpz_poly_mat_init(C, m, n);

        fmpz_poly_mat_randtest(A, state, deg, bits);
        fmpz_poly_mat_randtest(B, state, deg, bits);

        fmpz_poly_mat_add(C, A, B);
        fmpz_poly_mat_add(A, A, B);

        if (!fmpz_poly_mat_equal(C, A))
        {
            flint_printf("FAIL:\n");
            flint_printf("A:\n");
            fmpz_poly_mat_print(A, "x");
            flint_printf("B:\n");
            fmpz_poly_mat_print(B, "x");
            flint_printf("C:\n");
            fmpz_poly_mat_print(C, "x");
            flint_printf("\n");
            abort();
        }

        fmpz_poly_mat_clear(A);
        fmpz_poly_mat_clear(B);
        fmpz_poly_mat_clear(C);
    }

    /* Check aliasing C and B */
    for (i = 0; i < 100 * flint_test_multiplier(); i++)
    {
        fmpz_poly_mat_t A, B, C;
        slong m, n, bits, deg;

        m = n_randint(state, 20);
        n = n_randint(state, 20);
        deg = 1 + n_randint(state, 10);
        bits = 1 + n_randint(state, 100);

        fmpz_poly_mat_init(A, m, n);
        fmpz_poly_mat_init(B, m, n);
        fmpz_poly_mat_init(C, m, n);

        fmpz_poly_mat_randtest(A, state, deg, bits);
        fmpz_poly_mat_randtest(B, state, deg, bits);

        fmpz_poly_mat_add(C, A, B);
        fmpz_poly_mat_add(B, A, B);

        if (!fmpz_poly_mat_equal(C, B))
        {
            flint_printf("FAIL:\n");
            flint_printf("A:\n");
            fmpz_poly_mat_print(A, "x");
            flint_printf("B:\n");
            fmpz_poly_mat_print(B, "x");
            flint_printf("C:\n");
            fmpz_poly_mat_print(C, "x");
            flint_printf("\n");
            abort();
        }

        fmpz_poly_mat_clear(A);
        fmpz_poly_mat_clear(B);
        fmpz_poly_mat_clear(C);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
예제 #26
0
int
main(void)
{
    fmpz_mat_t A, X, B, AX;
    fmpz_t den;
    slong i, m, n, r;
    int success;

    FLINT_TEST_INIT(state);

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

    for (i = 0; i < 1000 * flint_test_multiplier(); i++)
    {
        m = n_randint(state, 4);
        n = n_randint(state, 10);

        fmpz_mat_init(A, m, m);
        fmpz_mat_init(B, m, n);
        fmpz_mat_init(X, m, n);
        fmpz_mat_init(AX, m, n);
        fmpz_init(den);

        fmpz_mat_randrank(A, state, m, 1+n_randint(state, 2)*n_randint(state, 100));
        fmpz_mat_randtest(B, state, 1+n_randint(state, 2)*n_randint(state, 100));

        /* Dense */
        if (n_randint(state, 2))
            fmpz_mat_randops(A, state, 1+n_randint(state, 1 + m*m));

        success = fmpz_mat_solve_cramer(X, den, A, B);

        fmpz_mat_mul(AX, A, X);
        fmpz_mat_scalar_divexact_fmpz(AX, AX, den);

        if (!fmpz_mat_equal(AX, B) || !success)
        {
            flint_printf("FAIL:\n");
            flint_printf("AX != B!\n");
            flint_printf("A:\n"),      fmpz_mat_print_pretty(A),  flint_printf("\n");
            flint_printf("B:\n"),      fmpz_mat_print_pretty(B),  flint_printf("\n");
            flint_printf("X:\n"),      fmpz_mat_print_pretty(X),  flint_printf("\n");
            flint_printf("den(X) = "), fmpz_print(den),           flint_printf("\n");
            flint_printf("AX:\n"),     fmpz_mat_print_pretty(AX), flint_printf("\n");
            abort();
        }

        fmpz_mat_clear(A);
        fmpz_mat_clear(B);
        fmpz_mat_clear(X);
        fmpz_mat_clear(AX);
        fmpz_clear(den);
    }

    /* Test singular systems */
    for (i = 0; i < 1000 * flint_test_multiplier(); i++)
    {
        m = 1 + n_randint(state, 3);
        n = 1 + n_randint(state, 10);
        r = n_randint(state, m);

        fmpz_mat_init(A, m, m);
        fmpz_mat_init(B, m, n);
        fmpz_mat_init(X, m, n);
        fmpz_mat_init(AX, m, n);
        fmpz_init(den);

        fmpz_mat_randrank(A, state, r, 1+n_randint(state, 2)*n_randint(state, 100));
        fmpz_mat_randtest(B, state, 1+n_randint(state, 2)*n_randint(state, 100));

        /* Dense */
        if (n_randint(state, 2))
            fmpz_mat_randops(A, state, 1+n_randint(state, 1 + m*m));

        success = fmpz_mat_solve_cramer(X, den, A, B);

        if (!fmpz_is_zero(den) || success)
        {
            flint_printf("FAIL:\n");
            flint_printf("singular system gave nonzero determinant\n");
            abort();
        }

        fmpz_mat_clear(A);
        fmpz_mat_clear(B);
        fmpz_mat_clear(X);
        fmpz_mat_clear(AX);
        fmpz_clear(den);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
예제 #27
0
 ~DMat() { fmpz_mat_clear(mArray); }
예제 #28
0
int
main(void)
{
    fmpz_mat_t A, X, B, AX, AXm, Bm;
    fmpz_t mod;
    slong i, m, n, r;
    int success;

    FLINT_TEST_INIT(state);

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

    for (i = 0; i < 100 * flint_test_multiplier(); i++)
    {
        m = n_randint(state, 20);
        n = n_randint(state, 20);

        fmpz_mat_init(A, m, m);
        fmpz_mat_init(B, m, n);
        fmpz_mat_init(Bm, m, n);
        fmpz_mat_init(X, m, n);
        fmpz_mat_init(AX, m, n);
        fmpz_mat_init(AXm, m, n);
        fmpz_init(mod);

        fmpz_mat_randrank(A, state, m, 1+n_randint(state, 2)*n_randint(state, 100));
        fmpz_mat_randtest(B, state, 1+n_randint(state, 2)*n_randint(state, 100));

        /* Dense */
        if (n_randint(state, 2))
            fmpz_mat_randops(A, state, 1+n_randint(state, 1 + m*m));

        success = fmpz_mat_solve_dixon(X, mod, A, B);

        fmpz_mat_set(AXm, X);

        fmpz_mat_mul(AX, A, AXm);

        fmpz_mat_scalar_mod_fmpz(AXm, AX, mod);
        fmpz_mat_scalar_mod_fmpz(Bm, B, mod);

        if (!fmpz_mat_equal(AXm, Bm) || !success)
        {
            flint_printf("FAIL:\n");
            flint_printf("AX != B!\n");
            flint_printf("A:\n"),      fmpz_mat_print_pretty(A),  flint_printf("\n");
            flint_printf("B:\n"),      fmpz_mat_print_pretty(B),  flint_printf("\n");
            flint_printf("X:\n"),      fmpz_mat_print_pretty(X),  flint_printf("\n");
            flint_printf("mod = "),    fmpz_print(mod),           flint_printf("\n");
            flint_printf("AX:\n"),     fmpz_mat_print_pretty(AX), flint_printf("\n");
            abort();
        }

        fmpz_mat_clear(A);
        fmpz_mat_clear(B);
        fmpz_mat_clear(Bm);
        fmpz_mat_clear(X);
        fmpz_mat_clear(AX);
        fmpz_mat_clear(AXm);
        fmpz_clear(mod);
    }

    /* Test singular systems */
    for (i = 0; i < 100 * flint_test_multiplier(); i++)
    {
        m = 1 + n_randint(state, 10);
        n = 1 + n_randint(state, 10);
        r = n_randint(state, m);

        fmpz_mat_init(A, m, m);
        fmpz_mat_init(B, m, n);
        fmpz_mat_init(X, m, n);
        fmpz_init(mod);

        fmpz_mat_randrank(A, state, r, 1+n_randint(state, 2)*n_randint(state, 100));
        fmpz_mat_randtest(B, state, 1+n_randint(state, 2)*n_randint(state, 100));

        /* Dense */
        if (n_randint(state, 2))
            fmpz_mat_randops(A, state, 1+n_randint(state, 1 + m*m));

        if (fmpz_mat_solve_dixon(X, mod, A, B) != 0)
        {
            flint_printf("FAIL:\n");
            flint_printf("singular system, returned nonzero\n");
            abort();
        }

        fmpz_mat_clear(A);
        fmpz_mat_clear(B);
        fmpz_mat_clear(X);
        fmpz_clear(mod);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
예제 #29
0
int
main(void)
{
    int i;
    FLINT_TEST_INIT(state);

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

    

    for (i = 0; i < 1000 * flint_test_multiplier(); i++)
    {
        slong bits, prime_bits, rows, cols, num_primes, j;
        fmpz_t mod;
        fmpz_mat_t A, B, C;
        nmod_mat_t Amod[1000];
        mp_limb_t primes[1000];

        bits = n_randint(state, 500) + 1;
        rows = n_randint(state, 10);
        cols = n_randint(state, 10);
        prime_bits = 1 + n_randint(state, FLINT_BITS - 1);

        fmpz_mat_init(A, rows, cols);
        fmpz_mat_init(B, rows, cols);
        fmpz_mat_init(C, rows, cols);

        fmpz_mat_randtest(A, state, bits);

        fmpz_init(mod);
        num_primes = 0;
        primes[0] = n_nextprime(UWORD(1) << prime_bits, 0);
        fmpz_set_ui(mod, primes[0]);

        /* + 1 for sign */
        while (fmpz_bits(mod) <= bits + 1)
        {
            primes[num_primes + 1] = n_nextprime(primes[num_primes], 0);
            fmpz_mul_ui(mod, mod, primes[num_primes + 1]);
            num_primes++;
        }

        num_primes++;

        for (j = 0; j < num_primes; j++)
            nmod_mat_init(Amod[j], rows, cols, primes[j]);

        fmpz_mat_multi_mod_ui(Amod, num_primes, A);
        fmpz_mat_multi_CRT_ui(B, Amod, num_primes, 1);

        if (!fmpz_mat_equal(B, A))
        {
            flint_printf("FAIL!\n");
            flint_printf("primes: ");
            for (j = 0; j < num_primes; j++)
                flint_printf("%wu ", primes[j]);
            flint_printf("\nA: \n");
            fmpz_mat_print_pretty(A);
            flint_printf("\nB: \n");
            fmpz_mat_print_pretty(B);
            flint_printf("\n");
            abort();
        }

        for (j = 0; j < num_primes; j++)
            nmod_mat_clear(Amod[j]);
        fmpz_mat_clear(A);
        fmpz_mat_clear(B);
        fmpz_mat_clear(C);
        fmpz_clear(mod);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
예제 #30
0
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;
    }
}