1 /*
2     Copyright (C) 2014 Alex J. Best
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_mat.h"
18 
19 int
main(void)20 main(void)
21 {
22     slong iter;
23     FLINT_TEST_INIT(state);
24 
25     flint_printf("hnf_xgcd....");
26     fflush(stdout);
27 
28     for (iter = 0; iter < 10000 * flint_test_multiplier(); iter++)
29     {
30         fmpz_mat_t A, H, H2, U;
31         slong m, n, b, d, r;
32         int equal;
33 
34         m = n_randint(state, 10);
35         n = n_randint(state, 10);
36         r = n_randint(state, FLINT_MIN(m, n) + 1);
37 
38         fmpz_mat_init(A, m, n);
39         fmpz_mat_init(H, m, n);
40         fmpz_mat_init(H2, m, n);
41         fmpz_mat_init(U, m, m);
42 
43         /* sparse */
44         b = 1 + n_randint(state, 10) * n_randint(state, 10);
45         d = n_randint(state, 2*m*n + 1);
46         fmpz_mat_randrank(A, state, r, b);
47 
48         /* dense */
49         if (n_randint(state, 2))
50             fmpz_mat_randops(A, state, d);
51 
52         fmpz_mat_hnf_xgcd(H, A);
53 
54         if (!fmpz_mat_is_in_hnf(H))
55         {
56             flint_printf("FAIL:\n");
57             flint_printf("matrix not in hnf!\n");
58             fmpz_mat_print_pretty(A); flint_printf("\n\n");
59             fmpz_mat_print_pretty(H); flint_printf("\n\n");
60             abort();
61         }
62 
63         fmpz_mat_hnf_classical(H2, A);
64         equal = fmpz_mat_equal(H, H2);
65 
66         if (!equal)
67         {
68             flint_printf("FAIL:\n");
69             flint_printf("hnfs produced by different methods should be the same!\n");
70             fmpz_mat_print_pretty(A); flint_printf("\n\n");
71             fmpz_mat_print_pretty(H); flint_printf("\n\n");
72             fmpz_mat_print_pretty(H2); flint_printf("\n\n");
73             abort();
74         }
75 
76         fmpz_mat_hnf_xgcd(H2, H);
77         equal = fmpz_mat_equal(H, H2);
78 
79         if (!equal)
80         {
81             flint_printf("FAIL:\n");
82             flint_printf("hnf of a matrix in hnf should be the same!\n");
83             fmpz_mat_print_pretty(A); flint_printf("\n\n");
84             fmpz_mat_print_pretty(H); flint_printf("\n\n");
85             fmpz_mat_print_pretty(H2); flint_printf("\n\n");
86             abort();
87         }
88 
89         fmpz_mat_clear(U);
90         fmpz_mat_clear(H2);
91         fmpz_mat_clear(H);
92         fmpz_mat_clear(A);
93     }
94 
95     FLINT_TEST_CLEANUP(state);
96 
97     flint_printf("PASS\n");
98     return 0;
99 }
100 
101