1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
Copyright (C) 2011 Fredrik Johansson
This file is part of FLINT.
FLINT is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include "fmpq.h"
#include "fmpq_mat.h"
slong
fmpq_mat_rref_classical(fmpq_mat_t B, const fmpq_mat_t A)
{
slong m, n, i, j, pivot_row, pivot_col, rank;
m = A->r;
n = A->c;
if (m == 0 || n == 0)
return 0;
if (A != B)
fmpq_mat_set(B, A);
rank = 0;
pivot_row = 0;
pivot_col = 0;
while (pivot_row < m && pivot_col < n)
{
if (!fmpq_mat_pivot(NULL, B, pivot_row, pivot_col))
{
pivot_col++;
continue;
}
rank++;
/* Scale row to have 1 as leading entry */
for (j = pivot_col + 1; j < n; j++)
{
fmpq_div(fmpq_mat_entry(B, pivot_row, j),
fmpq_mat_entry(B, pivot_row, j),
fmpq_mat_entry(B, pivot_row, pivot_col));
}
/* Eliminate rows above and below */
for (i = 0; i < m; i++)
{
if (i == pivot_row ||
fmpq_is_zero(fmpq_mat_entry(B, i, pivot_col)))
continue;
for (j = pivot_col + 1; j < n; j++)
fmpq_submul(fmpq_mat_entry(B, i, j),
fmpq_mat_entry(B, pivot_row, j),
fmpq_mat_entry(B, i, pivot_col));
}
/* Clear pivot column */
for (i = 0; i < m; i++)
fmpq_set_si(fmpq_mat_entry(B, i, pivot_col), i == pivot_row, 1);
pivot_row++;
pivot_col++;
}
return rank;
}