Ejemplo n.º 1
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;
}
Ejemplo n.º 2
0
int main()
{
    slong iter;
    flint_rand_t state;

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

    flint_randinit(state);

    /* trace(empty) == 0 */
    {
        bool_mat_t A;
        bool_mat_init(A, 0, 0);
        bool_mat_one(A);
        if (bool_mat_trace(A) != 0)
        {
            flint_printf("FAIL (empty)\n");
            flint_printf("A = \n"); bool_mat_print(A); flint_printf("\n\n");
            abort();
        }
        bool_mat_clear(A);
    }

    /* trace(zero) == 0 */
    {
        slong n;
        bool_mat_t A;
        for (n = 1; n < 10; n++)
        {
            bool_mat_init(A, n, n);
            bool_mat_zero(A);
            if (bool_mat_trace(A) != 0)
            {
                flint_printf("FAIL (zero)\n");
                flint_printf("A = \n"); bool_mat_print(A); flint_printf("\n\n");
                abort();
            }
            bool_mat_clear(A);
        }
    }

    /* trace(one) == 1 */
    {
        slong n;
        bool_mat_t A;
        for (n = 1; n < 10; n++)
        {
            bool_mat_init(A, n, n);
            bool_mat_one(A);
            if (bool_mat_trace(A) != 1)
            {
                flint_printf("FAIL (one)\n");
                flint_printf("A = \n"); bool_mat_print(A); flint_printf("\n\n");
                abort();
            }
            bool_mat_clear(A);
        }
    }

    /* traces of random matrices with modified diagonal */
    for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++)
    {
        slong n, i;
        bool_mat_t A;

        n = n_randint(state, 10) + 1;
        bool_mat_init(A, n, n);
        bool_mat_randtest(A, state);

        i = (slong) n_randint(state, n);
        bool_mat_set_entry(A, i, i, 1);
        if (bool_mat_trace(A) != 1)
        {
            flint_printf("FAIL (diagonal has a non-zero entry)\n");
            flint_printf("A = \n"); bool_mat_print(A); flint_printf("\n\n");
            abort();
        }

        for (i = 0; i < n; i++)
        {
            bool_mat_set_entry(A, i, i, 0);
        }
        if (bool_mat_trace(A) != 0)
        {
            flint_printf("FAIL (diagonal is zero)\n");
            flint_printf("A = \n"); bool_mat_print(A); flint_printf("\n\n");
            abort();
        }

        bool_mat_clear(A);
    }

    /* trace(A + B) == trace(A) + trace(B) */
    for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++)
    {
        slong n;
        bool_mat_t A, B, C;

        n = n_randint(state, 10);

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

        bool_mat_randtest(A, state);
        bool_mat_randtest(B, state);
        bool_mat_add(C, A, B);
        
        if (bool_mat_trace(C) != (bool_mat_trace(A) | bool_mat_trace(B)))
        {
            flint_printf("FAIL (trace(A+B) == trace(A) | trace(B))\n");
            flint_printf("A = \n"); bool_mat_print(A); flint_printf("\n\n");
            flint_printf("B = \n"); bool_mat_print(B); flint_printf("\n\n");
            flint_printf("A+B = \n"); bool_mat_print(C); flint_printf("\n\n");
            abort();
        }

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

    /* trace(A*B) == trace(B*A) */
    for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++)
    {
        slong n, m;
        bool_mat_t A, B, AB, BA;

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

        bool_mat_init(A, n, m);
        bool_mat_init(B, m, n);
        bool_mat_init(AB, n, n);
        bool_mat_init(BA, m, m);

        bool_mat_randtest(A, state);
        bool_mat_randtest(B, state);
        bool_mat_mul(AB, A, B);
        bool_mat_mul(BA, B, A);
        
        if (bool_mat_trace(AB) != bool_mat_trace(BA))
        {
            flint_printf("FAIL (trace(AB) == trace(BA))\n");
            flint_printf("A = \n"); bool_mat_print(A); flint_printf("\n\n");
            flint_printf("B = \n"); bool_mat_print(B); flint_printf("\n\n");
            flint_printf("AB = \n"); bool_mat_print(AB); flint_printf("\n\n");
            flint_printf("BA = \n"); bool_mat_print(BA); flint_printf("\n\n");
            abort();
        }

        bool_mat_clear(A);
        bool_mat_clear(B);
        bool_mat_clear(AB);
        bool_mat_clear(BA);
    }

    flint_randclear(state);
    flint_cleanup();
    flint_printf("PASS\n");
    return EXIT_SUCCESS;
}
Ejemplo n.º 3
0
int main()
{
    slong iter;
    flint_rand_t state;

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

    flint_randinit(state);

    /* special matrices */
    {
        slong n;
        for (n = 0; n < 10; n++)
        {
            bool_mat_t A;
            bool_mat_init(A, n, n);

            /* identity matrices are transitive */
            bool_mat_one(A);
            if (!bool_mat_is_transitive(A))
            {
                flint_printf("FAIL (identity matrix)\n");
                flint_abort();
            }
            
            /* square zero matrices are transitive */
            bool_mat_zero(A);
            if (!bool_mat_is_transitive(A))
            {
                flint_printf("FAIL (zero matrix)\n");
                flint_abort();
            }

            bool_mat_clear(A);
        }
    }

    for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++)
    {
        slong n;
        bool_mat_t A;

        n = n_randint(state, 10);

        bool_mat_init(A, n, n);

        /* all square diagonal matrices are transitive */
        bool_mat_randtest_diagonal(A, state);
        if (!bool_mat_is_transitive(A))
        {
            flint_printf("FAIL (diagonal)\n");
            flint_printf("A:\n"); bool_mat_print(A); flint_printf("\n");
            flint_abort();
        }

        bool_mat_clear(A);
    }

    flint_randclear(state);
    flint_cleanup();
    flint_printf("PASS\n");
    return EXIT_SUCCESS;
}