Skip to content

Commit

Permalink
rank-nullity fix
Browse files Browse the repository at this point in the history
  • Loading branch information
scheinerman committed Nov 29, 2023
1 parent b473c6e commit 0d31a6e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
18 changes: 15 additions & 3 deletions src/nullspacex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export nullspacex
"""
`nullspacex(A)` returns an exact basis for the nullspace of the matrix `A`
"""
nullspacex(A::AbstractMatrix{T}) where T = _nullspacex(A)
nullspacex(A::AbstractMatrix{T}) where {T} = _nullspacex(A)


function _nullspacex(A::AbstractMatrix{T}) where {T}
Expand Down Expand Up @@ -44,7 +44,19 @@ function nullspacex(A::AbstractMatrix{T}) where {T<:IntegerX}
return nullspacex(big.(A) // 1)
end

function nullspacex(A::AbstractMatrix{T}) where T<:AbstractAlgebraicFunction
function nullspacex(A::AbstractMatrix{T}) where {T<:AbstractAlgebraicFunction}
A = SimpleRationalFunction.(A)
return _nullspacex(A)
end
end

"""
nullityx(A::AbstractMatrix{T})::Int where {T}
Return the nullity of the matrix `A`.
"""
function nullityx(A::AbstractMatrix{T})::Int where {T}
NS = nullspacex(A)
r, c = size(NS)
return c
end
export nullityx
26 changes: 3 additions & 23 deletions src/rankx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,7 @@ export rankx
`rankx(A)` computes the rank of the exact matrix `A`
"""
function rankx(A::AbstractMatrix{T})::Int where {T}
r, c = size(A)

AA = deepcopy(A)
try
AA //= 1 # convert to rational if need be
try
rrefx!(AA)
catch
AA = big.(AA)
rrefx!(AA)
end
catch
rrefx!(AA)
end

count = 0
for i = 1:r
if any(AA[i, :] .!= 0)
count += 1
end
end
return count
function rankx(A::AbstractMatrix{T})::Int where T
r,c = size(A)
return c-nullityx(A)
end

0 comments on commit 0d31a6e

Please sign in to comment.