forked from flintlib/arb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht-inv_ldl_precomp.c
162 lines (136 loc) · 4.61 KB
/
t-inv_ldl_precomp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
Copyright (C) 2016 Arb authors
This file is part of Arb.
Arb is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version. See <http://www.gnu.org/licenses/>.
*/
#include "arb_mat.h"
void
_fmpq_mat_randtest_positive_semidefinite(fmpq_mat_t mat, flint_rand_t state, flint_bitcnt_t bits)
{
slong n;
fmpq_mat_t R, RT;
if (!fmpq_mat_is_square(mat)) flint_abort(); /* assert */
n = fmpq_mat_nrows(mat);
fmpq_mat_init(R, n, n);
fmpq_mat_init(RT, n, n);
fmpq_mat_randtest(R, state, bits);
fmpq_mat_transpose(RT, R);
fmpq_mat_mul(mat, R, RT);
fmpq_mat_clear(R);
fmpq_mat_clear(RT);
}
static int
_spd_inv(arb_mat_t X, const arb_mat_t A, slong prec)
{
slong n;
arb_mat_t L;
int result;
n = arb_mat_nrows(A);
arb_mat_init(L, n, n);
arb_mat_set(L, A);
if (_arb_mat_ldl_inplace(L, prec))
{
arb_mat_inv_ldl_precomp(X, L, prec);
result = 1;
}
else
{
result = 0;
}
arb_mat_clear(L);
return result;
}
int main()
{
slong iter;
flint_rand_t state;
flint_printf("inv_ldl_precomp....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++)
{
fmpq_mat_t Q, Qinv;
arb_mat_t A, Ainv;
slong n, qbits, prec;
int q_invertible, r_invertible, r_invertible2;
n = n_randint(state, 8);
qbits = 1 + n_randint(state, 30);
prec = 2 + n_randint(state, 200);
fmpq_mat_init(Q, n, n);
fmpq_mat_init(Qinv, n, n);
arb_mat_init(A, n, n);
arb_mat_init(Ainv, n, n);
_fmpq_mat_randtest_positive_semidefinite(Q, state, qbits);
q_invertible = fmpq_mat_inv(Qinv, Q);
if (!q_invertible)
{
arb_mat_set_fmpq_mat(A, Q, prec);
r_invertible = _spd_inv(Ainv, 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"); arb_mat_printd(A, 15); flint_printf("\n\n");
flint_printf("Ainv = \n"); arb_mat_printd(Ainv, 15); flint_printf("\n\n");
flint_abort();
}
}
else
{
/* now this must converge */
while (1)
{
arb_mat_set_fmpq_mat(A, Q, prec);
r_invertible = _spd_inv(Ainv, A, prec);
if (r_invertible)
{
break;
}
else
{
if (prec > 10000)
{
flint_printf("FAIL: failed to converge at 10000 bits\n");
flint_printf("Q = \n"); fmpq_mat_print(Q); flint_printf("\n\n");
flint_printf("A = \n"); arb_mat_printd(A, 15); flint_printf("\n\n");
flint_abort();
}
prec *= 2;
}
}
if (!arb_mat_contains_fmpq_mat(Ainv, Qinv))
{
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("Qinv = \n"); fmpq_mat_print(Qinv); flint_printf("\n\n");
flint_printf("A = \n"); arb_mat_printd(A, 15); flint_printf("\n\n");
flint_printf("Ainv = \n"); arb_mat_printd(Ainv, 15); flint_printf("\n\n");
flint_abort();
}
/* test aliasing */
r_invertible2 = _spd_inv(A, A, prec);
if (!arb_mat_equal(A, Ainv) || r_invertible != r_invertible2)
{
flint_printf("FAIL (aliasing)\n");
flint_printf("A = \n"); arb_mat_printd(A, 15); flint_printf("\n\n");
flint_printf("Ainv = \n"); arb_mat_printd(Ainv, 15); flint_printf("\n\n");
flint_abort();
}
}
fmpq_mat_clear(Q);
fmpq_mat_clear(Qinv);
arb_mat_clear(A);
arb_mat_clear(Ainv);
}
flint_randclear(state);
flint_cleanup();
flint_printf("PASS\n");
return EXIT_SUCCESS;
}