-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrrefx.jl
51 lines (40 loc) · 889 Bytes
/
rrefx.jl
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
export rrefx
"""
`rref(A)` returns the row-reduced echelon form of the exact
matrix `A`.
"""
function rrefx(A::AbstractMatrix{T}) where T<: IntegerX
AA = big.(A//1)
return rrefx(AA)
end
function rrefx(A::AbstractMatrix{T}) where T
AA = copy(A)
rrefx!(AA)
return AA
end
function _pivot!(A::AbstractMatrix{T}, i::Int, j::Int) where T
r,c = size(A)
s = A[i,j]
row_scale!(A,i,invx(s))
for k=1:r
if k != i
s = -A[k,j]
row_add_mult!(A,i,s,k)
end
end
end
function rrefx!(A::AbstractMatrix{T}) where T
rn,nc = size(A)
r = 1
for j=1:nc
# see if column j's first one is at level r or below
col = A[r:end,j]
if all(col .== 0)
continue
end
k = findfirst(col .!= 0)
row_swap!(A,r,k+r-1)
_pivot!(A,r,j)
r += 1
end
end