Example #1
0
 inline void subtractMultipleTo(DMatZZpFlint& C, 
                                const DMatZZpFlint& A, 
                                const DMatZZpFlint& B)
 {
   DMatZZpFlint D(C.ring(), A.numRows(), B.numColumns());
   nmod_mat_mul(D.nmod_mat(), A.nmod_mat(), B.nmod_mat());
   nmod_mat_sub(C.nmod_mat(), C.nmod_mat(), D.nmod_mat());
 }
Example #2
0
 inline void mult(const DMatZZpFlint& A, 
                  const DMatZZpFlint& B, 
                  DMatZZpFlint& result_product) 
 {
   //    DMatZZpFlint& A1 = const_cast<DMatZZpFlint&>(A); // needed because nmod_mat_mul doesn't declare params const
   //    DMatZZpFlint& B1 = const_cast<DMatZZpFlint&>(B);
   // The A1 and B1 on the next line are switched because the memory layout expected
   // is the transpose of what we have for DMat.
   nmod_mat_mul(result_product.nmod_mat(), A.nmod_mat(), B.nmod_mat());
 }
Example #3
0
int
main(void)
{
    long i;
    flint_rand_t state;
    flint_randinit(state);

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

    for (i = 0; i < 1000; i++)
    {
        nmod_mat_t A, X, B, Y;
        mp_limb_t m;
        long rows, cols;
        int unit;

        m = n_randtest_prime(state, 0);
        rows = n_randint(state, 100);
        cols = n_randint(state, 100);
        unit = n_randint(state, 2);

        nmod_mat_init(A, rows, rows, m);
        nmod_mat_init(B, rows, cols, m);
        nmod_mat_init(X, rows, cols, m);
        nmod_mat_init(Y, rows, cols, m);

        nmod_mat_randtriu(A, state, unit);
        nmod_mat_randtest(X, state);
        nmod_mat_mul(B, A, X);

        /* Check Y = A^(-1) * (A * X) = X */
        nmod_mat_solve_triu_recursive(Y, A, B, unit);
        if (!nmod_mat_equal(Y, X))
        {
            printf("FAIL!\n");
            printf("A:\n");
            nmod_mat_print_pretty(A);
            printf("X:\n");
            nmod_mat_print_pretty(X);
            printf("B:\n");
            nmod_mat_print_pretty(B);
            printf("Y:\n");
            nmod_mat_print_pretty(Y);
            abort();
        }

        /* Check aliasing */
        nmod_mat_solve_triu_recursive(B, A, B, unit);
        if (!nmod_mat_equal(B, X))
        {
            printf("FAIL!\n");
            printf("aliasing test failed");
            printf("A:\n");
            nmod_mat_print_pretty(A);
            printf("B:\n");
            nmod_mat_print_pretty(B);
            abort();
        }

        nmod_mat_clear(A);
        nmod_mat_clear(B);
        nmod_mat_clear(X);
        nmod_mat_clear(Y);
    }

    flint_randclear(state);

    printf("PASS\n");
    return 0;
}
Example #4
0
int
main(void)
{
    flint_rand_t state;
    long i;

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

    flint_randinit(state);

    /* Check evaluation homomorphism */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_mat_t A, B, C;
        nmod_mat_t a, b, c, d;
        mp_limb_t mod, x;
        long m, n, k, deg;

        mod = n_randtest_prime(state, 0);
        m = n_randint(state, 20);
        n = n_randint(state, 20);
        k = n_randint(state, 20);
        deg = 1 + n_randint(state, 10);

        nmod_poly_mat_init(A, m, n, mod);
        nmod_poly_mat_init(B, n, k, mod);
        nmod_poly_mat_init(C, m, k, mod);

        nmod_mat_init(a, m, n, mod);
        nmod_mat_init(b, n, k, mod);
        nmod_mat_init(c, m, k, mod);
        nmod_mat_init(d, m, k, mod);

        nmod_poly_mat_randtest(A, state, deg);
        nmod_poly_mat_randtest(B, state, deg);
        nmod_poly_mat_randtest(C, state, deg);  /* noise in output */

        if (nmod_poly_mat_max_length(A)
            + nmod_poly_mat_max_length(B) - 1 <= mod)
        {
            nmod_poly_mat_mul_interpolate(C, A, B);

            x = n_randint(state, mod);

            nmod_poly_mat_evaluate_nmod(a, A, x);
            nmod_poly_mat_evaluate_nmod(b, B, x);
            nmod_poly_mat_evaluate_nmod(d, C, x);
            nmod_mat_mul(c, a, b);

            if (!nmod_mat_equal(c, d))
            {
                printf("FAIL:\n");
                printf("A:\n");
                nmod_poly_mat_print(A, "x");
                printf("B:\n");
                nmod_poly_mat_print(B, "x");
                printf("C:\n");
                nmod_poly_mat_print(C, "x");
                printf("\n");
                abort();
            }
        }

        nmod_poly_mat_clear(A);
        nmod_poly_mat_clear(B);
        nmod_poly_mat_clear(C);

        nmod_mat_clear(a);
        nmod_mat_clear(b);
        nmod_mat_clear(c);
        nmod_mat_clear(d);
    }

    /* Check aliasing C and A */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_mat_t A, B, C;
        long m, n, deg;
        mp_limb_t mod;

        mod = n_randtest_prime(state, 0);
        m = n_randint(state, 20);
        n = n_randint(state, 20);
        deg = 1 + n_randint(state, 10);

        nmod_poly_mat_init(A, m, n, mod);
        nmod_poly_mat_init(B, n, n, mod);
        nmod_poly_mat_init(C, m, n, mod);

        nmod_poly_mat_randtest(A, state, deg);
        nmod_poly_mat_randtest(B, state, deg);
        nmod_poly_mat_randtest(C, state, deg);  /* noise in output */

        if (nmod_poly_mat_max_length(A)
            + nmod_poly_mat_max_length(B) - 1 <= mod)
        {

            nmod_poly_mat_mul_interpolate(C, A, B);
            nmod_poly_mat_mul_interpolate(A, A, B);

            if (!nmod_poly_mat_equal(C, A))
            {
                printf("FAIL:\n");
                printf("A:\n");
                nmod_poly_mat_print(A, "x");
                printf("B:\n");
                nmod_poly_mat_print(B, "x");
                printf("C:\n");
                nmod_poly_mat_print(C, "x");
                printf("\n");
                abort();
            }
        }

        nmod_poly_mat_clear(A);
        nmod_poly_mat_clear(B);
        nmod_poly_mat_clear(C);
    }

    /* Check aliasing C and B */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_mat_t A, B, C;
        long m, n, deg;
        mp_limb_t mod;

        mod = n_randtest_prime(state, 0);
        m = n_randint(state, 20);
        n = n_randint(state, 20);
        deg = 1 + n_randint(state, 10);

        nmod_poly_mat_init(A, m, m, mod);
        nmod_poly_mat_init(B, m, n, mod);
        nmod_poly_mat_init(C, m, n, mod);

        nmod_poly_mat_randtest(A, state, deg);
        nmod_poly_mat_randtest(B, state, deg);
        nmod_poly_mat_randtest(C, state, deg);  /* noise in output */

        if (nmod_poly_mat_max_length(A)
            + nmod_poly_mat_max_length(B) - 1 <= mod)
        {
            nmod_poly_mat_mul_interpolate(C, A, B);
            nmod_poly_mat_mul_interpolate(B, A, B);

            if (!nmod_poly_mat_equal(C, B))
            {
                printf("FAIL:\n");
                printf("A:\n");
                nmod_poly_mat_print(A, "x");
                printf("B:\n");
                nmod_poly_mat_print(B, "x");
                printf("C:\n");
                nmod_poly_mat_print(C, "x");
                printf("\n");
                abort();
            }
        }

        nmod_poly_mat_clear(A);
        nmod_poly_mat_clear(B);
        nmod_poly_mat_clear(C);
    }

    flint_randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return 0;
}
Example #5
0
File: t-sqr.c Project: goens/flint2
int
main(void)
{
    flint_rand_t state;
    long i;

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

    flint_randinit(state);

    /* Check evaluation homomorphism */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_mat_t A, C;
        nmod_mat_t a, c, d;
        mp_limb_t x, mod;
        long m, deg;

        mod = n_randtest_prime(state, 0);
        m = n_randint(state, 20);
        deg = 1 + n_randint(state, 10);

        nmod_poly_mat_init(A, m, m, mod);
        nmod_poly_mat_init(C, m, m, mod);

        nmod_mat_init(a, m, m, mod);
        nmod_mat_init(c, m, m, mod);
        nmod_mat_init(d, m, m, mod);

        nmod_poly_mat_randtest(A, state, deg);
        nmod_poly_mat_randtest(C, state, deg);  /* noise in output */

        nmod_poly_mat_sqr(C, A);

        x = n_randint(state, 0);

        nmod_poly_mat_evaluate_nmod(a, A, x);
        nmod_poly_mat_evaluate_nmod(d, C, x);
        nmod_mat_mul(c, a, a);

        if (!nmod_mat_equal(c, d))
        {
            printf("FAIL:\n");
            printf("A:\n");
            nmod_poly_mat_print(A, "x");
            printf("C:\n");
            nmod_poly_mat_print(C, "x");
            printf("\n");
            abort();
        }

        nmod_poly_mat_clear(A);
        nmod_poly_mat_clear(C);

        nmod_mat_clear(a);
        nmod_mat_clear(c);
        nmod_mat_clear(d);
    }

    /* Check aliasing B and A */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_mat_t A, B;
        long m, deg;
        mp_limb_t mod;

        mod = n_randtest_prime(state, 0);
        m = n_randint(state, 20);
        deg = 1 + n_randint(state, 10);

        nmod_poly_mat_init(A, m, m, mod);
        nmod_poly_mat_init(B, m, m, mod);

        nmod_poly_mat_randtest(A, state, deg);
        nmod_poly_mat_randtest(B, state, deg);  /* noise in output */

        nmod_poly_mat_sqr(B, A);
        nmod_poly_mat_sqr(A, A);

        if (!nmod_poly_mat_equal(B, A))
        {
            printf("FAIL (aliasing):\n");
            printf("A:\n");
            nmod_poly_mat_print(A, "x");
            printf("B:\n");
            nmod_poly_mat_print(B, "x");
            printf("\n");
            abort();
        }

        nmod_poly_mat_clear(A);
        nmod_poly_mat_clear(B);
    }

    flint_randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return 0;
}
Example #6
0
void
_nmod_poly_compose_mod_brent_kung(mp_ptr res, mp_srcptr poly1, long len1, 
                            mp_srcptr poly2,
                            mp_srcptr poly3, long len3, nmod_t mod)
{
    nmod_mat_t A, B, C;
    mp_ptr t, h;
    long i, n, m;

    n = len3 - 1;

    if (len3 == 1)
        return;

    if (len1 == 1)
    {
        res[0] = poly1[0];
        return;
    }

    if (len3 == 2)
    {
        res[0] = _nmod_poly_evaluate_nmod(poly1, len1, poly2[0], mod);
        return;
    }

    m = n_sqrt(n) + 1;

    nmod_mat_init(A, m, n, mod.n);
    nmod_mat_init(B, m, m, mod.n);
    nmod_mat_init(C, m, n, mod.n);

    h = _nmod_vec_init(n);
    t = _nmod_vec_init(n);

    /* Set rows of B to the segments of poly1 */
    for (i = 0; i < len1 / m; i++)
        _nmod_vec_set(B->rows[i], poly1 + i*m, m);

    _nmod_vec_set(B->rows[i], poly1 + i*m, len1 % m);

    /* Set rows of A to powers of poly2 */
    A->rows[0][0] = 1UL;
    _nmod_vec_set(A->rows[1], poly2, n);
    for (i = 2; i < m; i++)
        _nmod_poly_mulmod(A->rows[i], A->rows[i-1],
            n, poly2, n, poly3, len3, mod);

    nmod_mat_mul(C, B, A);

    /* Evaluate block composition using the Horner scheme */
    _nmod_vec_set(res, C->rows[m - 1], n);
    _nmod_poly_mulmod(h, A->rows[m - 1], n, poly2, n, poly3, len3, mod);

    for (i = m - 2; i >= 0; i--)
    {
        _nmod_poly_mulmod(t, res, n, h, n, poly3, len3, mod);
        _nmod_poly_add(res, t, n, C->rows[i], n, mod);
    }

    _nmod_vec_clear(h);
    _nmod_vec_clear(t);

    nmod_mat_clear(A);
    nmod_mat_clear(B);
    nmod_mat_clear(C);
}
Example #7
0
int
main(void)
{
    flint_rand_t state;
    long i;

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

    flint_randinit(state);

    for (i = 0; i < 10000; i++)
    {
        nmod_mat_t A, B, ker;
        mp_limb_t mod;
        long m, n, d, r, nullity, nulrank;

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

        for (r = 0; r <= FLINT_MIN(m,n); r++)
        {
            mod = n_randtest_prime(state, 0);
            d = n_randint(state, 2*m*n + 1);

            nmod_mat_init(A, m, n, mod);
            nmod_mat_init(ker, n, n, mod);
            nmod_mat_init(B, m, n, mod);

            nmod_mat_randrank(A, state, r);
            /* Densify */
            if (n_randlimb(state) % 2)
                nmod_mat_randops(A, d, state);

            nullity = nmod_mat_nullspace(ker, A);
            nulrank = nmod_mat_rank(ker);

            if (nullity != nulrank)
            {
                printf("FAIL:\n");
                printf("rank(ker) != nullity!\n");
                nmod_mat_print_pretty(A);
                printf("\n");
                abort();
            }

            if (nullity + r != n)
            {
                printf("FAIL:\n");
                printf("nullity + rank != n\n");
                nmod_mat_print_pretty(A);
                printf("\n");
                abort();
            }

            nmod_mat_mul(B, A, ker);

            if (nmod_mat_rank(B) != 0)
            {
                printf("FAIL:\n");
                printf("A * ker != 0\n");
                nmod_mat_print_pretty(A);
                printf("\n");
                abort();
            }

            nmod_mat_clear(A);
            nmod_mat_clear(ker);
            nmod_mat_clear(B);
        }
    }

    flint_randclear(state);
    printf("PASS\n");
    return 0;
}
Example #8
0
File: t-mul.c Project: goens/flint2
int
main(void)
{
    long i;
    flint_rand_t state;
    flint_randinit(state);

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

    for (i = 0; i < 10000; i++)
    {
        nmod_mat_t A, B, C, D;
        mp_limb_t mod;

        long m, k, n;

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

        /* We want to generate matrices with many entries close to half
           or full limbs with high probability, to stress overflow handling */
        switch (n_randint(state, 3))
        {
            case 0:
                mod = n_randtest_not_zero(state);
                break;
            case 1:
                mod = ULONG_MAX/2 + 1 - n_randbits(state, 4);
                break;
            case 2:
            default:
                mod = ULONG_MAX - n_randbits(state, 4);
                break;
        }

        nmod_mat_init(A, m, n, mod);
        nmod_mat_init(B, n, k, mod);
        nmod_mat_init(C, m, k, mod);
        nmod_mat_init(D, m, k, mod);

        if (n_randint(state, 2))
            nmod_mat_randtest(A, state);
        else
            nmod_mat_randfull(A, state);

        if (n_randint(state, 2))
            nmod_mat_randtest(B, state);
        else
            nmod_mat_randfull(B, state);

        nmod_mat_randtest(C, state);  /* make sure noise in the output is ok */

        nmod_mat_mul(C, A, B);
        nmod_mat_mul_check(D, A, B);

        if (!nmod_mat_equal(C, D))
        {
            printf("FAIL: results not equal\n");
            nmod_mat_print_pretty(A);
            nmod_mat_print_pretty(B);
            nmod_mat_print_pretty(C);
            nmod_mat_print_pretty(D);
            abort();
        }

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

    flint_randclear(state);

    printf("PASS\n");
    return 0;
}
Example #9
0
int
main(void)
{
    slong i;
    FLINT_TEST_INIT(state);
    

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

    for (i = 0; i < 10 * flint_test_multiplier(); i++)
    {
        nmod_mat_t A, X, B, Y;
        mp_limb_t m;
        slong rows, cols;
        int unit;

        m = n_randtest_prime(state, 0);
        rows = n_randint(state, 200);
        cols = n_randint(state, 200);
        unit = n_randint(state, 2);

        nmod_mat_init(A, rows, rows, m);
        nmod_mat_init(B, rows, cols, m);
        nmod_mat_init(X, rows, cols, m);
        nmod_mat_init(Y, rows, cols, m);

        nmod_mat_randtriu(A, state, unit);
        nmod_mat_randtest(X, state);
        nmod_mat_mul(B, A, X);

        /* Check Y = A^(-1) * (A * X) = X */
        nmod_mat_solve_triu(Y, A, B, unit);
        if (!nmod_mat_equal(Y, X))
        {
            flint_printf("FAIL!\n");
            flint_printf("A:\n");
            nmod_mat_print_pretty(A);
            flint_printf("X:\n");
            nmod_mat_print_pretty(X);
            flint_printf("B:\n");
            nmod_mat_print_pretty(B);
            flint_printf("Y:\n");
            nmod_mat_print_pretty(Y);
            abort();
        }

        /* Check aliasing */
        nmod_mat_solve_triu(B, A, B, unit);
        if (!nmod_mat_equal(B, X))
        {
            flint_printf("FAIL!\n");
            flint_printf("aliasing test failed");
            flint_printf("A:\n");
            nmod_mat_print_pretty(A);
            flint_printf("B:\n");
            nmod_mat_print_pretty(B);
            abort();
        }

        nmod_mat_clear(A);
        nmod_mat_clear(B);
        nmod_mat_clear(X);
        nmod_mat_clear(Y);
    }

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