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_poly.h"
16 #include "fmpz_poly_mat.h"
17
18
19 int
main(void)20 main(void)
21 {
22 slong i;
23
24 FLINT_TEST_INIT(state);
25
26 flint_printf("inv....");
27 fflush(stdout);
28
29 /* Test aliasing */
30 for (i = 0; i < 40 * flint_test_multiplier(); i++)
31 {
32 fmpz_poly_mat_t A, Ainv;
33 fmpz_poly_t den1, den2;
34 slong n, bits, deg;
35 float density;
36 int ns1, ns2;
37 int result;
38
39 n = n_randint(state, 8);
40 deg = 1 + n_randint(state, 5);
41 bits = 1 + n_randint(state, 100);
42 density = n_randint(state, 100) * 0.01;
43
44 fmpz_poly_mat_init(A, n, n);
45 fmpz_poly_mat_init(Ainv, n, n);
46 fmpz_poly_init(den1);
47 fmpz_poly_init(den2);
48
49 fmpz_poly_mat_randtest_sparse(A, state, deg, bits, density);
50
51 ns1 = fmpz_poly_mat_inv(Ainv, den1, A);
52 ns2 = fmpz_poly_mat_inv(A, den2, A);
53
54 result = ns1 == ns2;
55
56 if (result && ns1 != 0)
57 {
58 result = fmpz_poly_equal(den1, den2) &&
59 fmpz_poly_mat_equal(A, Ainv);
60 }
61
62 if (!result)
63 {
64 flint_printf("FAIL (aliasing)!\n");
65 fmpz_poly_mat_print(A, "x"); flint_printf("\n");
66 fmpz_poly_mat_print(Ainv, "x"); flint_printf("\n");
67 abort();
68 }
69
70 fmpz_poly_mat_clear(A);
71 fmpz_poly_mat_clear(Ainv);
72 fmpz_poly_clear(den1);
73 fmpz_poly_clear(den2);
74 }
75
76 /* Check A^(-1) = A = 1 */
77 for (i = 0; i < 100 * flint_test_multiplier(); i++)
78 {
79 fmpz_poly_mat_t A, Ainv, B, Iden;
80 fmpz_poly_t den, det;
81 slong n, bits, deg;
82 float density;
83 int nonsingular;
84
85 n = n_randint(state, 10);
86 deg = 1 + n_randint(state, 5);
87 bits = 1 + n_randint(state, 100);
88 density = n_randint(state, 100) * 0.01;
89
90 fmpz_poly_mat_init(A, n, n);
91 fmpz_poly_mat_init(Ainv, n, n);
92 fmpz_poly_mat_init(B, n, n);
93 fmpz_poly_mat_init(Iden, n, n);
94 fmpz_poly_init(den);
95 fmpz_poly_init(det);
96
97 fmpz_poly_mat_randtest_sparse(A, state, deg, bits, density);
98 nonsingular = fmpz_poly_mat_inv(Ainv, den, A);
99 fmpz_poly_mat_det_interpolate(det, A);
100
101 if (n == 0)
102 {
103 if (nonsingular == 0 || !fmpz_poly_is_one(den))
104 {
105 flint_printf("FAIL: expected empty matrix to pass\n");
106 abort();
107 }
108 }
109 else
110 {
111 if (!fmpz_poly_equal(den, det))
112 {
113 fmpz_poly_neg(det, det);
114 flint_printf("FAIL: den != det(A)\n");
115 abort();
116 }
117
118 fmpz_poly_mat_mul(B, Ainv, A);
119 fmpz_poly_mat_one(Iden);
120 fmpz_poly_mat_scalar_mul_fmpz_poly(Iden, Iden, den);
121
122 if (!fmpz_poly_mat_equal(B, Iden))
123 {
124 flint_printf("FAIL:\n");
125 flint_printf("A:\n");
126 fmpz_poly_mat_print(A, "x");
127 flint_printf("Ainv:\n");
128 fmpz_poly_mat_print(Ainv, "x");
129 flint_printf("B:\n");
130 fmpz_poly_mat_print(B, "x");
131 flint_printf("den:\n");
132 fmpz_poly_print_pretty(den, "x");
133 abort();
134 }
135 }
136
137 fmpz_poly_clear(den);
138 fmpz_poly_clear(det);
139 fmpz_poly_mat_clear(A);
140 fmpz_poly_mat_clear(Ainv);
141 fmpz_poly_mat_clear(B);
142 fmpz_poly_mat_clear(Iden);
143 }
144
145 FLINT_TEST_CLEANUP(state);
146
147 flint_printf("PASS\n");
148 return 0;
149 }
150