1 /*
2 Copyright (C) 2010 Fredrik Johansson
3
4 This file is part of FLINT.
5
6 FLINT is free software: you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version. See <https://www.gnu.org/licenses/>.
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <gmp.h>
15 #include "flint.h"
16 #include "fmpz.h"
17 #include "fmpz_vec.h"
18 #include "fmpz_mat.h"
19 #include "ulong_extras.h"
20
21
22 int
main(void)23 main(void)
24 {
25 fmpz_mat_t A, X, B, AX;
26 fmpz_t den;
27 slong i, m, n, r;
28 int success;
29
30 FLINT_TEST_INIT(state);
31
32 flint_printf("solve....");
33 fflush(stdout);
34
35 for (i = 0; i < 1000 * flint_test_multiplier(); i++)
36 {
37 m = n_randint(state, 10);
38 n = n_randint(state, 10);
39
40 fmpz_mat_init(A, m, m);
41 fmpz_mat_init(B, m, n);
42 fmpz_mat_init(X, m, n);
43 fmpz_mat_init(AX, m, n);
44 fmpz_init(den);
45
46 fmpz_mat_randrank(A, state, m, 1+n_randint(state, 2)*n_randint(state, 100));
47 fmpz_mat_randtest(B, state, 1+n_randint(state, 2)*n_randint(state, 100));
48
49 /* Dense */
50 if (n_randint(state, 2))
51 fmpz_mat_randops(A, state, 1+n_randint(state, 1 + m*m));
52
53 success = fmpz_mat_solve(X, den, A, B);
54
55 fmpz_mat_mul(AX, A, X);
56 fmpz_mat_scalar_divexact_fmpz(AX, AX, den);
57
58 if (!fmpz_mat_equal(AX, B) || !success)
59 {
60 flint_printf("FAIL:\n");
61 flint_printf("AX != B!\n");
62 flint_printf("A:\n"), fmpz_mat_print_pretty(A), flint_printf("\n");
63 flint_printf("B:\n"), fmpz_mat_print_pretty(B), flint_printf("\n");
64 flint_printf("X:\n"), fmpz_mat_print_pretty(X), flint_printf("\n");
65 flint_printf("den(X) = "), fmpz_print(den), flint_printf("\n");
66 flint_printf("AX:\n"), fmpz_mat_print_pretty(AX), flint_printf("\n");
67 abort();
68 }
69
70 fmpz_mat_clear(A);
71 fmpz_mat_clear(B);
72 fmpz_mat_clear(X);
73 fmpz_mat_clear(AX);
74 fmpz_clear(den);
75 }
76
77 /* Test singular systems */
78 for (i = 0; i < 1000 * flint_test_multiplier(); i++)
79 {
80 m = 1 + n_randint(state, 10);
81 n = 1 + n_randint(state, 10);
82 r = n_randint(state, m);
83
84 fmpz_mat_init(A, m, m);
85 fmpz_mat_init(B, m, n);
86 fmpz_mat_init(X, m, n);
87 fmpz_mat_init(AX, m, n);
88 fmpz_init(den);
89
90 fmpz_mat_randrank(A, state, r, 1+n_randint(state, 2)*n_randint(state, 100));
91 fmpz_mat_randtest(B, state, 1+n_randint(state, 2)*n_randint(state, 100));
92
93 /* Dense */
94 if (n_randint(state, 2))
95 fmpz_mat_randops(A, state, 1+n_randint(state, 1 + m*m));
96
97 success = fmpz_mat_solve(X, den, A, B);
98
99 if (!fmpz_is_zero(den) || success)
100 {
101 flint_printf("FAIL:\n");
102 flint_printf("singular system gave nonzero determinant\n");
103 abort();
104 }
105
106 fmpz_mat_clear(A);
107 fmpz_mat_clear(B);
108 fmpz_mat_clear(X);
109 fmpz_mat_clear(AX);
110 fmpz_clear(den);
111 }
112
113 FLINT_TEST_CLEANUP(state);
114
115 flint_printf("PASS\n");
116 return 0;
117 }
118