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("mul....");
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, k, bits, deg;
37
38 m = n_randint(state, 20);
39 n = n_randint(state, 20);
40 k = n_randint(state, 20);
41 deg = 1 + n_randint(state, 10);
42 bits = 1 + n_randint(state, 100);
43
44 fmpz_poly_mat_init(A, m, n);
45 fmpz_poly_mat_init(B, n, k);
46 fmpz_poly_mat_init(C, m, k);
47
48 fmpz_mat_init(a, m, n);
49 fmpz_mat_init(b, n, k);
50 fmpz_mat_init(c, m, k);
51 fmpz_mat_init(d, m, k);
52
53 fmpz_init(x);
54
55 fmpz_poly_mat_randtest(A, state, deg, bits);
56 fmpz_poly_mat_randtest(B, state, deg, bits);
57 fmpz_poly_mat_randtest(C, state, deg, bits); /* noise in output */
58
59 fmpz_poly_mat_mul(C, A, B);
60
61 fmpz_randtest(x, state, 1 + n_randint(state, 100));
62
63 fmpz_poly_mat_evaluate_fmpz(a, A, x);
64 fmpz_poly_mat_evaluate_fmpz(b, B, x);
65 fmpz_poly_mat_evaluate_fmpz(d, C, x);
66 fmpz_mat_mul(c, a, b);
67
68 if (!fmpz_mat_equal(c, d))
69 {
70 flint_printf("FAIL:\n");
71 flint_printf("A:\n");
72 fmpz_poly_mat_print(A, "x");
73 flint_printf("B:\n");
74 fmpz_poly_mat_print(B, "x");
75 flint_printf("C:\n");
76 fmpz_poly_mat_print(C, "x");
77 flint_printf("\n");
78 abort();
79 }
80
81 fmpz_poly_mat_clear(A);
82 fmpz_poly_mat_clear(B);
83 fmpz_poly_mat_clear(C);
84
85 fmpz_mat_clear(a);
86 fmpz_mat_clear(b);
87 fmpz_mat_clear(c);
88 fmpz_mat_clear(d);
89
90 fmpz_clear(x);
91 }
92
93 /* Check aliasing C and A */
94 for (i = 0; i < 100 * flint_test_multiplier(); i++)
95 {
96 fmpz_poly_mat_t A, B, C;
97 slong m, n, bits, deg;
98
99 m = n_randint(state, 20);
100 n = n_randint(state, 20);
101 deg = 1 + n_randint(state, 10);
102 bits = 1 + n_randint(state, 100);
103
104 fmpz_poly_mat_init(A, m, n);
105 fmpz_poly_mat_init(B, n, n);
106 fmpz_poly_mat_init(C, m, n);
107
108 fmpz_poly_mat_randtest(A, state, deg, bits);
109 fmpz_poly_mat_randtest(B, state, deg, bits);
110 fmpz_poly_mat_randtest(C, state, deg, bits); /* noise in output */
111
112 fmpz_poly_mat_mul(C, A, B);
113 fmpz_poly_mat_mul(A, A, B);
114
115 if (!fmpz_poly_mat_equal(C, A))
116 {
117 flint_printf("FAIL:\n");
118 flint_printf("A:\n");
119 fmpz_poly_mat_print(A, "x");
120 flint_printf("B:\n");
121 fmpz_poly_mat_print(B, "x");
122 flint_printf("C:\n");
123 fmpz_poly_mat_print(C, "x");
124 flint_printf("\n");
125 abort();
126 }
127
128 fmpz_poly_mat_clear(A);
129 fmpz_poly_mat_clear(B);
130 fmpz_poly_mat_clear(C);
131 }
132
133 /* Check aliasing C and B */
134 for (i = 0; i < 100 * flint_test_multiplier(); i++)
135 {
136 fmpz_poly_mat_t A, B, C;
137 slong m, n, bits, deg;
138
139 m = n_randint(state, 20);
140 n = n_randint(state, 20);
141 deg = 1 + n_randint(state, 10);
142 bits = 1 + n_randint(state, 100);
143
144 fmpz_poly_mat_init(A, m, m);
145 fmpz_poly_mat_init(B, m, n);
146 fmpz_poly_mat_init(C, m, n);
147
148 fmpz_poly_mat_randtest(A, state, deg, bits);
149 fmpz_poly_mat_randtest(B, state, deg, bits);
150 fmpz_poly_mat_randtest(C, state, deg, bits); /* noise in output */
151
152 fmpz_poly_mat_mul(C, A, B);
153 fmpz_poly_mat_mul(B, A, B);
154
155 if (!fmpz_poly_mat_equal(C, B))
156 {
157 flint_printf("FAIL:\n");
158 flint_printf("A:\n");
159 fmpz_poly_mat_print(A, "x");
160 flint_printf("B:\n");
161 fmpz_poly_mat_print(B, "x");
162 flint_printf("C:\n");
163 fmpz_poly_mat_print(C, "x");
164 flint_printf("\n");
165 abort();
166 }
167
168 fmpz_poly_mat_clear(A);
169 fmpz_poly_mat_clear(B);
170 fmpz_poly_mat_clear(C);
171 }
172
173 FLINT_TEST_CLEANUP(state);
174
175 flint_printf("PASS\n");
176 return 0;
177 }
178