From 0d31a6e90009cd885a6a82950e2c320dcf354841 Mon Sep 17 00:00:00 2001 From: Ed Scheinerman Date: Wed, 29 Nov 2023 17:10:07 -0500 Subject: [PATCH] rank-nullity fix --- src/nullspacex.jl | 18 +++++++++++++++--- src/rankx.jl | 26 +++----------------------- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/nullspacex.jl b/src/nullspacex.jl index 9322a0a..19a619f 100644 --- a/src/nullspacex.jl +++ b/src/nullspacex.jl @@ -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} @@ -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 \ No newline at end of file +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 diff --git a/src/rankx.jl b/src/rankx.jl index fe5c19e..2d75430 100644 --- a/src/rankx.jl +++ b/src/rankx.jl @@ -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