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 <http://www.gnu.org/licenses/>.
10 */
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include "flint.h"
15 #include "fmpz.h"
16 #include "fmpz_mat.h"
17 #include "fmpz_poly.h"
18 #include "fmpz_poly_mat.h"
19 
20 int
main(void)21 main(void)
22 {
23     slong i;
24 
25     FLINT_TEST_INIT(state);
26 
27     flint_printf("sub....");
28     fflush(stdout);
29 
30     /* Check evaluation homomorphism */
31     for (i = 0; i < 100 * flint_test_multiplier(); i++)
32     {
33         fmpz_poly_mat_t A, B, C;
34         fmpz_mat_t a, b, c, d;
35         fmpz_t x;
36         slong m, n, bits, deg;
37 
38         m = n_randint(state, 20);
39         n = n_randint(state, 20);
40         deg = 1 + n_randint(state, 10);
41         bits = 1 + n_randint(state, 100);
42 
43         fmpz_poly_mat_init(A, m, n);
44         fmpz_poly_mat_init(B, m, n);
45         fmpz_poly_mat_init(C, m, n);
46 
47         fmpz_mat_init(a, m, n);
48         fmpz_mat_init(b, m, n);
49         fmpz_mat_init(c, m, n);
50         fmpz_mat_init(d, m, n);
51 
52         fmpz_init(x);
53 
54         fmpz_poly_mat_randtest(A, state, deg, bits);
55         fmpz_poly_mat_randtest(B, state, deg, bits);
56         fmpz_poly_mat_sub(C, A, B);
57 
58         fmpz_randtest(x, state, 1 + n_randint(state, 100));
59 
60         fmpz_poly_mat_evaluate_fmpz(a, A, x);
61         fmpz_poly_mat_evaluate_fmpz(b, B, x);
62         fmpz_poly_mat_evaluate_fmpz(d, C, x);
63         fmpz_mat_sub(c, a, b);
64 
65         if (!fmpz_mat_equal(c, d))
66         {
67             flint_printf("FAIL:\n");
68             flint_printf("A:\n");
69             fmpz_poly_mat_print(A, "x");
70             flint_printf("B:\n");
71             fmpz_poly_mat_print(B, "x");
72             flint_printf("C:\n");
73             fmpz_poly_mat_print(C, "x");
74             flint_printf("\n");
75             abort();
76         }
77 
78         fmpz_poly_mat_clear(A);
79         fmpz_poly_mat_clear(B);
80         fmpz_poly_mat_clear(C);
81 
82         fmpz_mat_clear(a);
83         fmpz_mat_clear(b);
84         fmpz_mat_clear(c);
85         fmpz_mat_clear(d);
86 
87         fmpz_clear(x);
88     }
89 
90     /* Check aliasing C and A */
91     for (i = 0; i < 100 * flint_test_multiplier(); i++)
92     {
93         fmpz_poly_mat_t A, B, C;
94         slong m, n, bits, deg;
95 
96         m = n_randint(state, 20);
97         n = n_randint(state, 20);
98         deg = 1 + n_randint(state, 10);
99         bits = 1 + n_randint(state, 100);
100 
101         fmpz_poly_mat_init(A, m, n);
102         fmpz_poly_mat_init(B, m, n);
103         fmpz_poly_mat_init(C, m, n);
104 
105         fmpz_poly_mat_randtest(A, state, deg, bits);
106         fmpz_poly_mat_randtest(B, state, deg, bits);
107 
108         fmpz_poly_mat_sub(C, A, B);
109         fmpz_poly_mat_sub(A, A, B);
110 
111         if (!fmpz_poly_mat_equal(C, A))
112         {
113             flint_printf("FAIL:\n");
114             flint_printf("A:\n");
115             fmpz_poly_mat_print(A, "x");
116             flint_printf("B:\n");
117             fmpz_poly_mat_print(B, "x");
118             flint_printf("C:\n");
119             fmpz_poly_mat_print(C, "x");
120             flint_printf("\n");
121             abort();
122         }
123 
124         fmpz_poly_mat_clear(A);
125         fmpz_poly_mat_clear(B);
126         fmpz_poly_mat_clear(C);
127     }
128 
129     /* Check aliasing C and B */
130     for (i = 0; i < 100 * flint_test_multiplier(); i++)
131     {
132         fmpz_poly_mat_t A, B, C;
133         slong m, n, bits, deg;
134 
135         m = n_randint(state, 20);
136         n = n_randint(state, 20);
137         deg = 1 + n_randint(state, 10);
138         bits = 1 + n_randint(state, 100);
139 
140         fmpz_poly_mat_init(A, m, n);
141         fmpz_poly_mat_init(B, m, n);
142         fmpz_poly_mat_init(C, m, n);
143 
144         fmpz_poly_mat_randtest(A, state, deg, bits);
145         fmpz_poly_mat_randtest(B, state, deg, bits);
146 
147         fmpz_poly_mat_sub(C, A, B);
148         fmpz_poly_mat_sub(B, A, B);
149 
150         if (!fmpz_poly_mat_equal(C, B))
151         {
152             flint_printf("FAIL:\n");
153             flint_printf("A:\n");
154             fmpz_poly_mat_print(A, "x");
155             flint_printf("B:\n");
156             fmpz_poly_mat_print(B, "x");
157             flint_printf("C:\n");
158             fmpz_poly_mat_print(C, "x");
159             flint_printf("\n");
160             abort();
161         }
162 
163         fmpz_poly_mat_clear(A);
164         fmpz_poly_mat_clear(B);
165         fmpz_poly_mat_clear(C);
166     }
167 
168     FLINT_TEST_CLEANUP(state);
169 
170     flint_printf("PASS\n");
171     return 0;
172 }
173