Ejemplo n.º 1
0
void
acb_mat_pow_ui(acb_mat_t B, const acb_mat_t A, ulong exp, long prec)
{
    long d = acb_mat_nrows(A);

    if (exp <= 2 || d <= 1)
    {
        if (exp == 0 || d == 0)
        {
            acb_mat_one(B);
        }
        else if (d == 1)
        {
            acb_pow_ui(acb_mat_entry(B, 0, 0),
                 acb_mat_entry(A, 0, 0), exp, prec);
        }
        else if (exp == 1)
        {
            acb_mat_set(B, A);
        }
        else if (exp == 2)
        {
            acb_mat_mul(B, A, A, prec);   /* todo: sqr */
        }
    }
    else
    {
        acb_mat_t T, U;
        long i;

        acb_mat_init(T, d, d);
        acb_mat_set(T, A);
        acb_mat_init(U, d, d);

        for (i = ((long) FLINT_BIT_COUNT(exp)) - 2; i >= 0; i--)
        {
            acb_mat_mul(U, T, T, prec);   /* todo: sqr */

            if (exp & (1L << i))
                acb_mat_mul(T, U, A, prec);
            else
                acb_mat_swap(T, U);
        }

        acb_mat_swap(B, T);
        acb_mat_clear(T);
        acb_mat_clear(U);
    }
}
Ejemplo n.º 2
0
int
acb_mat_inv(acb_mat_t X, const acb_mat_t A, long prec)
{
    if (X == A)
    {
        int r;
        acb_mat_t T;
        acb_mat_init(T, acb_mat_nrows(A), acb_mat_ncols(A));
        r = acb_mat_inv(T, A, prec);
        acb_mat_swap(T, X);
        acb_mat_clear(T);
        return r;
    }

    acb_mat_one(X);
    return acb_mat_solve(X, A, X, prec);
}
Ejemplo n.º 3
0
Archivo: t-lu.c Proyecto: isuruf/arb
int main()
{
    slong iter;
    flint_rand_t state;

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

    flint_randinit(state);

    for (iter = 0; iter < 10000; iter++)
    {
        fmpq_mat_t Q;
        acb_mat_t A, LU, P, L, U, T;
        slong i, j, n, qbits, prec, *perm;
        int q_invertible, r_invertible;

        n = n_randint(state, 8);
        qbits = 1 + n_randint(state, 100);
        prec = 2 + n_randint(state, 202);

        fmpq_mat_init(Q, n, n);
        acb_mat_init(A, n, n);
        acb_mat_init(LU, n, n);
        acb_mat_init(P, n, n);
        acb_mat_init(L, n, n);
        acb_mat_init(U, n, n);
        acb_mat_init(T, n, n);
        perm = _perm_init(n);

        fmpq_mat_randtest(Q, state, qbits);
        q_invertible = fmpq_mat_is_invertible(Q);

        if (!q_invertible)
        {
            acb_mat_set_fmpq_mat(A, Q, prec);
            r_invertible = acb_mat_lu(perm, LU, A, prec);
            if (r_invertible)
            {
                flint_printf("FAIL: matrix is singular over Q but not over R\n");
                flint_printf("n = %wd, prec = %wd\n", n, prec);
                flint_printf("\n");

                flint_printf("Q = \n"); fmpq_mat_print(Q); flint_printf("\n\n");
                flint_printf("A = \n"); acb_mat_printd(A, 15); flint_printf("\n\n");
                flint_printf("LU = \n"); acb_mat_printd(LU, 15); flint_printf("\n\n");
            }
        }
        else
        {
            /* now this must converge */
            while (1)
            {
                acb_mat_set_fmpq_mat(A, Q, prec);
                r_invertible = acb_mat_lu(perm, LU, A, prec);
                if (r_invertible)
                {
                    break;
                }
                else
                {
                    if (prec > 10000)
                    {
                        flint_printf("FAIL: failed to converge at 10000 bits\n");
                        abort();
                    }
                    prec *= 2;
                }
            }

            acb_mat_one(L);
            for (i = 0; i < n; i++)
                for (j = 0; j < i; j++)
                    acb_set(acb_mat_entry(L, i, j),
                        acb_mat_entry(LU, i, j));

            for (i = 0; i < n; i++)
                for (j = i; j < n; j++)
                    acb_set(acb_mat_entry(U, i, j),
                        acb_mat_entry(LU, i, j));

            for (i = 0; i < n; i++)
                acb_one(acb_mat_entry(P, perm[i], i));

            acb_mat_mul(T, P, L, prec);
            acb_mat_mul(T, T, U, prec);

            if (!acb_mat_contains_fmpq_mat(T, Q))
            {
                flint_printf("FAIL (containment, iter = %wd)\n", iter);
                flint_printf("n = %wd, prec = %wd\n", n, prec);
                flint_printf("\n");

                flint_printf("Q = \n"); fmpq_mat_print(Q); flint_printf("\n\n");
                flint_printf("A = \n"); acb_mat_printd(A, 15); flint_printf("\n\n");
                flint_printf("LU = \n"); acb_mat_printd(LU, 15); flint_printf("\n\n");
                flint_printf("L = \n"); acb_mat_printd(L, 15); flint_printf("\n\n");
                flint_printf("U = \n"); acb_mat_printd(U, 15); flint_printf("\n\n");
                flint_printf("P*L*U = \n"); acb_mat_printd(T, 15); flint_printf("\n\n");

                abort();
            }
        }

        fmpq_mat_clear(Q);
        acb_mat_clear(A);
        acb_mat_clear(LU);
        acb_mat_clear(P);
        acb_mat_clear(L);
        acb_mat_clear(U);
        acb_mat_clear(T);
        _perm_clear(perm);
    }

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

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

    flint_randinit(state);

    for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++)
    {
        slong m, n, k, qbits1, qbits2, rbits1, rbits2, rbits3;
        fmpq_mat_t A, B, C;
        acb_mat_t a, b, c, d;

        qbits1 = 2 + n_randint(state, 200);
        qbits2 = 2 + n_randint(state, 200);
        rbits1 = 2 + n_randint(state, 200);
        rbits2 = 2 + n_randint(state, 200);
        rbits3 = 2 + n_randint(state, 200);

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

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

        acb_mat_init(a, m, n);
        acb_mat_init(b, n, k);
        acb_mat_init(c, m, k);
        acb_mat_init(d, m, k);

        fmpq_mat_randtest(A, state, qbits1);
        fmpq_mat_randtest(B, state, qbits2);
        fmpq_mat_mul(C, A, B);

        acb_mat_set_fmpq_mat(a, A, rbits1);
        acb_mat_set_fmpq_mat(b, B, rbits2);
        acb_mat_mul(c, a, b, rbits3);

        if (!acb_mat_contains_fmpq_mat(c, C))
        {
            flint_printf("FAIL\n\n");
            flint_printf("m = %wd, n = %wd, k = %wd, bits3 = %wd\n", m, n, k, rbits3);

            flint_printf("A = "); fmpq_mat_print(A); flint_printf("\n\n");
            flint_printf("B = "); fmpq_mat_print(B); flint_printf("\n\n");
            flint_printf("C = "); fmpq_mat_print(C); flint_printf("\n\n");

            flint_printf("a = "); acb_mat_printd(a, 15); flint_printf("\n\n");
            flint_printf("b = "); acb_mat_printd(b, 15); flint_printf("\n\n");
            flint_printf("c = "); acb_mat_printd(c, 15); flint_printf("\n\n");

            abort();
        }

        /* test aliasing with a */
        if (acb_mat_nrows(a) == acb_mat_nrows(c) &&
            acb_mat_ncols(a) == acb_mat_ncols(c))
        {
            acb_mat_set(d, a);
            acb_mat_mul(d, d, b, rbits3);
            if (!acb_mat_equal(d, c))
            {
                flint_printf("FAIL (aliasing 1)\n\n");
                abort();
            }
        }

        /* test aliasing with b */
        if (acb_mat_nrows(b) == acb_mat_nrows(c) &&
            acb_mat_ncols(b) == acb_mat_ncols(c))
        {
            acb_mat_set(d, b);
            acb_mat_mul(d, a, d, rbits3);
            if (!acb_mat_equal(d, c))
            {
                flint_printf("FAIL (aliasing 2)\n\n");
                abort();
            }
        }

        fmpq_mat_clear(A);
        fmpq_mat_clear(B);
        fmpq_mat_clear(C);

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

    /* check algebraic properties like associativity and distributivity */
    for (iter = 0; iter < 1000 * arb_test_multiplier(); iter++)
    {
        slong m, n, k, l;
        slong rbits;
        acb_mat_t a, b, c, d, ab, ac, bd, cd, s;

        rbits = 2 + n_randint(state, 200);

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

        _acb_mat_init_randtest(a, m, n, state);
        _acb_mat_init_randtest(b, n, k, state);
        _acb_mat_init_randtest(c, n, k, state);
        _acb_mat_init_randtest(d, k, l, state);

        acb_mat_init(ab, m, k);
        acb_mat_init(ac, m, k);
        acb_mat_init(bd, n, l);
        acb_mat_init(cd, n, l);
        acb_mat_init(s, n, k);

        acb_mat_mul(ab, a, b, rbits);
        acb_mat_mul(ac, a, c, rbits);
        acb_mat_mul(bd, b, d, rbits);
        acb_mat_mul(cd, c, d, rbits);
        acb_mat_add(s, b, c, rbits);

        /* check associativity of multiplication */
        /* (A*B)*D = A*(B*D) */
        {
            acb_mat_t lhs, rhs;

            acb_mat_init(lhs, m, l);
            acb_mat_init(rhs, m, l);

            acb_mat_mul(lhs, ab, d, rbits);
            acb_mat_mul(rhs, a, bd, rbits);

            if (!acb_mat_overlaps(lhs, rhs))
            {
                flint_printf("FAIL\n\n");
                flint_printf("m, n, k, l = %wd, %wd, %wd, %wd\n", m, n, k, l);
                flint_printf("rbits = %wd\n", rbits);

                _acb_mat_nprintd("a", a);
                _acb_mat_nprintd("b", b);
                _acb_mat_nprintd("d", d);
                _acb_mat_nprintd("(a*b)*d", lhs);
                _acb_mat_nprintd("a*(b*d)", rhs);

                abort();
            }

            acb_mat_clear(lhs);
            acb_mat_clear(rhs);
        }

        /* check left distributivity of multiplication over addition */
        /* A*(B + C) = A*B + A*C */
        {
            acb_mat_t lhs, rhs;

            acb_mat_init(lhs, m, k);
            acb_mat_init(rhs, m, k);

            acb_mat_mul(lhs, a, s, rbits);
            acb_mat_add(rhs, ab, ac, rbits);

            if (!acb_mat_overlaps(lhs, rhs))
            {
                flint_printf("FAIL\n\n");
                flint_printf("m, n, k, l = %wd, %wd, %wd, %wd\n", m, n, k, l);
                flint_printf("rbits = %wd\n", rbits);

                _acb_mat_nprintd("a", a);
                _acb_mat_nprintd("b", b);
                _acb_mat_nprintd("c", c);
                _acb_mat_nprintd("a*(b + c)", lhs);
                _acb_mat_nprintd("a*b + b*c", rhs);

                abort();
            }

            acb_mat_clear(lhs);
            acb_mat_clear(rhs);
        }

        /* check right distributivity of multiplication over addition */
        /* (B + C)*D = B*D + C*D */
        {
            acb_mat_t lhs, rhs;

            acb_mat_init(lhs, n, l);
            acb_mat_init(rhs, n, l);

            acb_mat_mul(lhs, s, d, rbits);
            acb_mat_add(rhs, bd, cd, rbits);

            if (!acb_mat_overlaps(lhs, rhs))
            {
                flint_printf("FAIL\n\n");
                flint_printf("m, n, k, l = %wd, %wd, %wd, %wd\n", m, n, k, l);
                flint_printf("rbits = %wd\n", rbits);

                _acb_mat_nprintd("b", b);
                _acb_mat_nprintd("c", c);
                _acb_mat_nprintd("d", d);
                _acb_mat_nprintd("(b + c)*d", lhs);
                _acb_mat_nprintd("b*d + c*d", rhs);

                abort();
            }

            acb_mat_clear(lhs);
            acb_mat_clear(rhs);
        }

        /* check left multiplicative identity I*D = D */
        {
            acb_mat_t one, lhs;

            acb_mat_init(one, k, k);
            acb_mat_init(lhs, k, l);

            acb_mat_one(one);
            acb_mat_mul(lhs, one, d, rbits);

            if (!acb_mat_contains(lhs, d))
            {
                flint_printf("FAIL\n\n");
                flint_printf("k = %wd, l = %wd\n", k, l);
                flint_printf("rbits = %wd\n", rbits);

                _acb_mat_nprintd("identity * d", lhs);
                _acb_mat_nprintd("d", d);

                abort();
            }

            acb_mat_clear(one);
            acb_mat_clear(lhs);
        }

        /* check right multiplicative identity A*I = A */
        {
            acb_mat_t one, lhs;

            acb_mat_init(one, n, n);
            acb_mat_init(lhs, m, n);

            acb_mat_one(one);
            acb_mat_mul(lhs, a, one, rbits);

            if (!acb_mat_contains(lhs, a))
            {
                flint_printf("FAIL\n\n");
                flint_printf("m = %wd, n = %wd\n", m, n);
                flint_printf("rbits = %wd\n", rbits);

                _acb_mat_nprintd("a * identity", lhs);
                _acb_mat_nprintd("a", a);

                abort();
            }

            acb_mat_clear(one);
            acb_mat_clear(lhs);
        }

        acb_mat_clear(a);
        acb_mat_clear(b);
        acb_mat_clear(c);
        acb_mat_clear(d);
        acb_mat_clear(ab);
        acb_mat_clear(ac);
        acb_mat_clear(bd);
        acb_mat_clear(cd);
        acb_mat_clear(s);
    }

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