Exemplo n.º 1
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;
}
Exemplo n.º 2
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;
}
Exemplo n.º 3
0
Arquivo: t-sqr.c Projeto: 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;
}
Exemplo n.º 4
0
int
main(void)
{
    slong m, n, mod, mod2, rep;
    FLINT_TEST_INIT(state);
    

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

    /* Rectangular transpose, same modulus */
    for (rep = 0; rep < 100 * flint_test_multiplier(); rep++)
    {
        nmod_mat_t A, B, C;

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

        mod = n_randtest_not_zero(state);

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

        nmod_mat_randtest(A, state);
        nmod_mat_randtest(B, state);

        nmod_mat_transpose(B, A);
        nmod_mat_transpose(C, B);

        if (!nmod_mat_equal(C, A))
        {
            flint_printf("FAIL: C != A\n");
            abort();
        }

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

    /* Rectangular transpose, different modulus */
    for (rep = 0; rep < 100 * flint_test_multiplier(); rep++)
    {
        nmod_mat_t A, AT, B, BT, AT2;

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

        mod = n_randtest_not_zero(state);
        mod2 = n_randtest_not_zero(state);

        nmod_mat_init(A, m, n, mod);
        nmod_mat_init(AT, n, m, mod);
        nmod_mat_init(B, m, n, mod2);
        nmod_mat_init(BT, n, m, mod2);
        nmod_mat_init(AT2, n, m, mod2);

        nmod_mat_randtest(A, state);
        nmod_mat_set(B, A);

        nmod_mat_transpose(AT, A);
        nmod_mat_transpose(BT, B);

        nmod_mat_set(AT2, AT);

        if (!nmod_mat_equal(BT, AT2))
        {
            flint_printf("FAIL: AT != BT\n");
            abort();
        }

        nmod_mat_clear(A);
        nmod_mat_clear(AT);
        nmod_mat_clear(AT2);
        nmod_mat_clear(B);
        nmod_mat_clear(BT);
    }

    /* Self-transpose */
    for (rep = 0; rep < 100 * flint_test_multiplier(); rep++)
    {
        nmod_mat_t A, B;

        m = n_randint(state, 20);
        mod = n_randtest_not_zero(state);

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

        nmod_mat_randtest(A, state);
        nmod_mat_set(B, A);

        nmod_mat_transpose(B, B);
        nmod_mat_transpose(B, B);

        if (!nmod_mat_equal(B, A))
        {
            flint_printf("FAIL: B != A\n");
            abort();
        }

        nmod_mat_clear(A);
        nmod_mat_clear(B);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
Exemplo n.º 5
0
Arquivo: t-mul.c Projeto: 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;
}
Exemplo n.º 6
0
int main()
{
    long iter;
    flint_rand_t state;

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

    flint_randinit(state);

    for (iter = 0; iter < 10000; iter++)
    {
        fmpz_holonomic_t op;
        fmpz_mat_t M1;
        fmpz_t Q1;
        nmod_mat_t M2, L, R;
        mp_limb_t Q2;
        long start, n, r;
        mp_limb_t p;

        fmpz_holonomic_init(op);

        fmpz_holonomic_randtest(op, state, 4, 4, 10);

        r = fmpz_holonomic_order(op);
        start = n_randint(state, 10);
        n = n_randint(state, 100);
        p = n_randtest_prime(state, 0);

        fmpz_mat_init(M1, r, r);
        fmpz_init(Q1);
        nmod_mat_init(M2, r, r, p);
        nmod_mat_init(L, r, r, p);
        nmod_mat_init(R, r, r, p);

        fmpz_holonomic_forward_fmpz_mat(M1, Q1, op, start, n);
        fmpz_holonomic_forward_nmod_mat(M2, &Q2, op, start, n);

        fmpz_mat_get_nmod_mat(L, M1);
        nmod_mat_scalar_mul(L, L, Q2);

        nmod_mat_scalar_mul(R, M2, fmpz_fdiv_ui(Q1, p));

        /* check Q2 * M1 = Q1 * M2 */

        if (!nmod_mat_equal(L, R))
        {
            printf("FAIL\n");

            fmpz_holonomic_print(op, "n", "Sn"); printf("\n\n");
            printf("start = %lu, n = %lu\n", start, n);

            fmpz_mat_print_pretty(M1); printf("\n\n");
            fmpz_print(Q1); printf("\n\n");

            nmod_mat_print_pretty(M2); printf("\n\n");
            printf("%lu\n\n", Q2);

            abort();
        }

        fmpz_mat_clear(M1);
        fmpz_clear(Q1);
        nmod_mat_clear(M2);
        nmod_mat_clear(L);
        nmod_mat_clear(R);

        fmpz_holonomic_clear(op);
    }

    flint_randclear(state);
    flint_cleanup();
    printf("PASS\n");
    return EXIT_SUCCESS;
}
Exemplo n.º 7
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;
}