1 /*
2     Copyright (C) 2010-2012 Fredrik Johansson
3     Copyright (C) 2014 Alex J. Best
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 "fmpz_mat.h"
14 
fmpz_mat_is_in_rref_with_rank(const fmpz_mat_t A,const fmpz_t den,slong rank)15 int fmpz_mat_is_in_rref_with_rank(const fmpz_mat_t A, const fmpz_t den, slong rank)
16 {
17     slong i, j, k, prev_pivot;
18 
19     /* bottom should be zero */
20     for (i = rank; i < A->r; i++)
21         for (j = 0; j < A->c; j++)
22             if (!fmpz_is_zero(fmpz_mat_entry(A, i, j)))
23                 return 0;
24 
25     prev_pivot = -1;
26 
27     for (i = 0; i < rank; i++)
28     {
29         for (j = 0; j < A->c; j++)
30         {
31             if (!fmpz_is_zero(fmpz_mat_entry(A, i, j)))
32             {
33                 /* pivot should have a higher column index than previous */
34                 if (j <= prev_pivot)
35                     return 0;
36 
37                 /* column should be 0 ... 0 1 0 ... 0 */
38                 for (k = 0; k < rank; k++)
39                 {
40                     if (i == k && !fmpz_equal(fmpz_mat_entry(A, k, j), den))
41                         return 0;
42                     if (i != k && !fmpz_is_zero(fmpz_mat_entry(A, k, j)))
43                         return 0;
44                 }
45 
46                 prev_pivot = j;
47                 break;
48             }
49         }
50     }
51 
52     return 1;
53 }
54