1 /*
2 Copyright (C) 2011 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, B, ker;
26 slong i, m, n, b, d, r, nullity, nulrank;
27
28 FLINT_TEST_INIT(state);
29
30 flint_printf("nullspace....");
31 fflush(stdout);
32
33 /* small dimension */
34 for (i = 0; i < 1000 * flint_test_multiplier(); i++)
35 {
36 m = n_randint(state, 10);
37 n = n_randint(state, 10);
38
39 for (r = 0; r <= FLINT_MIN(m,n); r++)
40 {
41 b = 1 + n_randint(state, 10) * n_randint(state, 10);
42 d = n_randint(state, 2*m*n + 1);
43
44 fmpz_mat_init(A, m, n);
45 fmpz_mat_init(ker, n, n);
46 fmpz_mat_init(B, m, n);
47
48 fmpz_mat_randrank(A, state, r, b);
49 /* Densify */
50 if (n_randlimb(state) % 2)
51 fmpz_mat_randops(A, state, d);
52
53 nullity = fmpz_mat_nullspace(ker, A);
54 nulrank = fmpz_mat_rank(ker);
55
56 if (nullity != nulrank)
57 {
58 flint_printf("FAIL:\n");
59 flint_printf("rank(ker) != nullity!\n");
60 fmpz_mat_print_pretty(A);
61 flint_printf("\n");
62 abort();
63 }
64
65 if (nullity + r != n)
66 {
67 flint_printf("FAIL:\n");
68 flint_printf("nullity + rank != n\n");
69 fmpz_mat_print_pretty(A);
70 flint_printf("\n");
71 abort();
72 }
73
74 fmpz_mat_mul(B, A, ker);
75
76 if (fmpz_mat_rank(B) != 0)
77 {
78 flint_printf("FAIL:\n");
79 flint_printf("A * ker != 0\n");
80 fmpz_mat_print_pretty(A);
81 flint_printf("\n");
82 abort();
83 }
84
85 fmpz_mat_clear(A);
86 fmpz_mat_clear(ker);
87 fmpz_mat_clear(B);
88 }
89 }
90
91 /* larger dimension */
92 for (i = 0; i < 10 * flint_test_multiplier(); i++)
93 {
94 m = 25 + n_randint(state, 10);
95 n = 25 + n_randint(state, 10);
96
97 for (r = 0; r <= FLINT_MIN(m,n); r++)
98 {
99 b = 1 + n_randint(state, 10) * n_randint(state, 10);
100 d = n_randint(state, 2*m*n + 1);
101
102 fmpz_mat_init(A, m, n);
103 fmpz_mat_init(ker, n, n);
104 fmpz_mat_init(B, m, n);
105
106 fmpz_mat_randrank(A, state, r, b);
107 /* Densify */
108 if (n_randlimb(state) % 2)
109 fmpz_mat_randops(A, state, d);
110
111 nullity = fmpz_mat_nullspace(ker, A);
112 nulrank = fmpz_mat_rank(ker);
113
114 if (nullity != nulrank)
115 {
116 flint_printf("FAIL:\n");
117 flint_printf("rank(ker) != nullity!\n");
118 fmpz_mat_print_pretty(A);
119 flint_printf("\n");
120 abort();
121 }
122
123 if (nullity + r != n)
124 {
125 flint_printf("FAIL:\n");
126 flint_printf("nullity + rank != n\n");
127 fmpz_mat_print_pretty(A);
128 flint_printf("\n");
129 abort();
130 }
131
132 fmpz_mat_mul(B, A, ker);
133
134 if (fmpz_mat_rank(B) != 0)
135 {
136 flint_printf("FAIL:\n");
137 flint_printf("A * ker != 0\n");
138 fmpz_mat_print_pretty(A);
139 flint_printf("\n");
140 abort();
141 }
142
143 fmpz_mat_clear(A);
144 fmpz_mat_clear(ker);
145 fmpz_mat_clear(B);
146 }
147 }
148
149 FLINT_TEST_CLEANUP(state);
150
151 flint_printf("PASS\n");
152 return 0;
153 }
154