Exemplo n.º 1
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);
}
Exemplo n.º 2
0
 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;
 }
Exemplo n.º 3
0
 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;
 }
Exemplo n.º 4
0
Arquivo: inv.c Projeto: goens/flint2
int fmpq_mat_inv(fmpq_mat_t B, const fmpq_mat_t A)
{
    long n = A->r;

    if (n == 0)
    {
        return 1;
    }
    else if (n == 1)
    {
        if (fmpq_is_zero(fmpq_mat_entry(A, 0, 0)))
            return 0;
        fmpq_inv(fmpq_mat_entry(B, 0, 0), fmpq_mat_entry(A, 0, 0));
        return 1;
    }
    else if (n == 2)
    {
        fmpq_t d;
        int success;

        fmpq_init(d);

        fmpq_mul(d, fmpq_mat_entry(A, 0, 0), fmpq_mat_entry(A, 1, 1));
        fmpq_submul(d, fmpq_mat_entry(A, 0, 1), fmpq_mat_entry(A, 1, 0));
        success = !fmpq_is_zero(d);

        if (success)
        {
            fmpq_t t00, t01, t10, t11;
            fmpq_inv(d, d);

            fmpq_init(t00);
            fmpq_init(t01);
            fmpq_init(t10);
            fmpq_init(t11);

            fmpq_mul(t00, fmpq_mat_entry(A, 1, 1), d);
            fmpq_mul(t01, fmpq_mat_entry(A, 0, 1), d);
            fmpq_mul(t10, fmpq_mat_entry(A, 1, 0), d);
            fmpq_mul(t11, fmpq_mat_entry(A, 0, 0), d);

            fmpq_set(fmpq_mat_entry(B, 0, 0), t00);
            fmpq_neg(fmpq_mat_entry(B, 0, 1), t01);
            fmpq_neg(fmpq_mat_entry(B, 1, 0), t10);
            fmpq_set(fmpq_mat_entry(B, 1, 1), t11);

            fmpq_clear(t00);
            fmpq_clear(t01);
            fmpq_clear(t10);
            fmpq_clear(t11);
        }

        fmpq_clear(d);
        return success;
    }
    else
    {
        fmpz_mat_t Aclear, Bclear, I;
        fmpz * den;
        long i;
        int success;

        fmpz_mat_init(Aclear, n, n);
        fmpz_mat_init(Bclear, n, n);
        fmpz_mat_init(I, n, n);
        den = _fmpz_vec_init(n);

        fmpq_mat_get_fmpz_mat_rowwise(Aclear, den, A);
        for (i = 0; i < n; i++)
            fmpz_set(fmpz_mat_entry(I, i, i), den + i);

        success = fmpz_mat_solve(Bclear, den, Aclear, I);
        if (success)
            fmpq_mat_set_fmpz_mat_div_fmpz(B, Bclear, den);

        fmpz_mat_clear(Aclear);
        fmpz_mat_clear(Bclear);
        fmpz_mat_clear(I);
        _fmpz_vec_clear(den, A->r);

        return success;
    }
}