1 /*
2     Copyright (C) 2007 William Hart and David Harvey
3     Copyright (C) 2011 Fredrik Johansson
4 
5     This file is part of FLINT.
6 
7     FLINT is free software: you can redistribute it and/or modify it under
8     the terms of the GNU Lesser General Public License (LGPL) as published
9     by the Free Software Foundation; either version 2.1 of the License, or
10     (at your option) any later version.  See <https://www.gnu.org/licenses/>.
11 */
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <gmp.h>
16 #include "flint.h"
17 #include "fmpz.h"
18 #include "fmpz_mat.h"
19 #include "nmod_mat.h"
20 
21 int
main(void)22 main(void)
23 {
24     int i;
25     FLINT_TEST_INIT(state);
26 
27     flint_printf("CRT_ui....");
28     fflush(stdout);
29 
30 
31 
32     for (i = 0; i < 1000 * flint_test_multiplier(); i++)
33     {
34         slong bits, prime_bits, rows, cols, num_primes, j;
35         fmpz_t mod;
36         fmpz_mat_t A, B, C;
37         nmod_mat_t Amod;
38         mp_limb_t primes[1000];
39 
40         bits = n_randint(state, 500) + 1;
41         rows = n_randint(state, 10);
42         cols = n_randint(state, 10);
43         prime_bits = 1 + n_randint(state, FLINT_BITS - 1);
44 
45         fmpz_mat_init(A, rows, cols);
46         fmpz_mat_init(B, rows, cols);
47         fmpz_mat_init(C, rows, cols);
48 
49         fmpz_mat_randtest(A, state, bits);
50 
51         fmpz_init(mod);
52         num_primes = 0;
53         primes[0] = n_nextprime(UWORD(1) << prime_bits, 0);
54         fmpz_set_ui(mod, primes[0]);
55 
56         /* + 1 for sign */
57         while (fmpz_bits(mod) <= bits + 1)
58         {
59             primes[num_primes + 1] = n_nextprime(primes[num_primes], 0);
60             fmpz_mul_ui(mod, mod, primes[num_primes + 1]);
61             num_primes++;
62         }
63 
64         num_primes++;
65 
66         nmod_mat_init(Amod, rows, cols, primes[0]);
67         fmpz_mat_get_nmod_mat(Amod, A);
68         fmpz_mat_set_nmod_mat(B, Amod);
69         fmpz_set_ui(mod, primes[0]);
70 
71         for (j = 1; j < num_primes; j++)
72         {
73             nmod_mat_clear(Amod);
74             nmod_mat_init(Amod, rows, cols, primes[j]);
75             fmpz_mat_get_nmod_mat(Amod, A);
76             fmpz_mat_CRT_ui(B, B, mod, Amod, 1);
77             fmpz_mul_ui(mod, mod, primes[j]);
78         }
79 
80         if (!fmpz_mat_equal(B, A))
81         {
82             flint_printf("FAIL!\n");
83             flint_printf("primes: ");
84             for (j = 0; j < num_primes; j++)
85                 flint_printf("%wu ", primes[j]);
86             flint_printf("\nA: \n");
87             fmpz_mat_print_pretty(A);
88             flint_printf("\nB: \n");
89             fmpz_mat_print_pretty(B);
90             flint_printf("\n");
91             abort();
92         }
93 
94         nmod_mat_clear(Amod);
95         fmpz_mat_clear(A);
96         fmpz_mat_clear(B);
97         fmpz_mat_clear(C);
98         fmpz_clear(mod);
99     }
100 
101     FLINT_TEST_CLEANUP(state);
102 
103     flint_printf("PASS\n");
104     return 0;
105 }
106