Skip to content

Commit

Permalink
Add is_invertible_with_inverse for arb matrices (#1525)
Browse files Browse the repository at this point in the history
  • Loading branch information
thofma authored Aug 16, 2023
1 parent ae8f01c commit 407b0e0
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 10 deletions.
14 changes: 10 additions & 4 deletions src/arb/ComplexMat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -440,12 +440,18 @@ Given a $n\times n$ matrix of type `acb_mat`, return an
$n\times n$ matrix $X$ such that $AX$ contains the
identity matrix. If $A$ cannot be inverted numerically an exception is raised.
"""
function inv(x::ComplexMat, prec::Int = precision(Balls))
ncols(x) != nrows(x) && error("Matrix must be square")
function inv(x::ComplexMat)
fl, z = is_invertible_with_inverse(x)
fl && return z
error("Matrix singular or cannot be inverted numerically")
end

function is_invertible_with_inverse(x::ComplexMat)
ncols(x) != nrows(x) && return false, x
z = similar(x)
r = ccall((:acb_mat_inv, libarb), Cint,
(Ref{ComplexMat}, Ref{ComplexMat}, Int), z, x, prec)
Bool(r) ? (return z) : error("Matrix cannot be inverted numerically")
(Ref{ComplexMat}, Ref{ComplexMat}, Int), z, x, precision(Balls))
return Bool(r), z
end

###############################################################################
Expand Down
10 changes: 8 additions & 2 deletions src/arb/RealMat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,17 @@ $n\times n$ matrix $X$ such that $AX$ contains the
identity matrix. If $A$ cannot be inverted numerically an exception is raised.
"""
function inv(x::RealMat)
ncols(x) != nrows(x) && error("Matrix must be square")
fl, z = is_invertible_with_inverse(x)
fl && return z
error("Matrix singular or cannot be inverted numerically")
end

function is_invertible_with_inverse(x::RealMat)
ncols(x) != nrows(x) && return false, x
z = similar(x)
r = ccall((:arb_mat_inv, libarb), Cint,
(Ref{RealMat}, Ref{RealMat}, Int), z, x, precision(Balls))
Bool(r) ? (return z) : error("Matrix cannot be inverted numerically")
return Bool(r), z
end

###############################################################################
Expand Down
10 changes: 8 additions & 2 deletions src/arb/acb_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,17 @@ $n\times n$ matrix $X$ such that $AX$ contains the
identity matrix. If $A$ cannot be inverted numerically an exception is raised.
"""
function inv(x::acb_mat)
ncols(x) != nrows(x) && error("Matrix must be square")
fl, z = is_invertible_with_inverse(x)
fl && return z
error("Matrix singular or cannot be inverted numerically")
end

function is_invertible_with_inverse(x::acb_mat)
ncols(x) != nrows(x) && return false, x
z = similar(x)
r = ccall((:acb_mat_inv, libarb), Cint,
(Ref{acb_mat}, Ref{acb_mat}, Int), z, x, precision(base_ring(x)))
Bool(r) ? (return z) : error("Matrix cannot be inverted numerically")
return Bool(r), z
end

###############################################################################
Expand Down
10 changes: 8 additions & 2 deletions src/arb/arb_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,17 @@ $n\times n$ matrix $X$ such that $AX$ contains the
identity matrix. If $A$ cannot be inverted numerically an exception is raised.
"""
function inv(x::arb_mat)
ncols(x) != nrows(x) && error("Matrix must be square")
fl, z = is_invertible_with_inverse(x)
fl && return z
error("Matrix singular or cannot be inverted numerically")
end

function is_invertible_with_inverse(x::arb_mat)
ncols(x) != nrows(x) && return false, x
z = similar(x)
r = ccall((:arb_mat_inv, libarb), Cint,
(Ref{arb_mat}, Ref{arb_mat}, Int), z, x, precision(base_ring(x)))
Bool(r) ? (return z) : error("Matrix cannot be inverted numerically")
return Bool(r), z
end

###############################################################################
Expand Down
8 changes: 8 additions & 0 deletions test/arb/ComplexMat-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,14 @@ end

@test overlaps(A*C, one(S))
@test contains(C, B)

fl, C = is_invertible_with_inverse(A)
@test fl && contains(C, B)

A = CC[1 1; 1 1]
fl, C = is_invertible_with_inverse(A)
@test !fl
@test_throws ErrorException inv(A)
end

@testset "ComplexMat.divexact" begin
Expand Down
8 changes: 8 additions & 0 deletions test/arb/RealMat-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,14 @@ end

@test overlaps(A*C, one(S))
@test contains(C, B)

fl, C = is_invertible_with_inverse(A)
@test fl && contains(C, B)

A = RR[1 1; 1 1]
fl, C = is_invertible_with_inverse(A)
@test !fl
@test_throws ErrorException inv(A)
end

@testset "RealMat.divexact" begin
Expand Down
8 changes: 8 additions & 0 deletions test/arb/acb_mat-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,14 @@ end

@test overlaps(A*C, one(S))
@test contains(C, B)

fl, C = is_invertible_with_inverse(A)
@test fl && contains(C, B)

A = CC[1 1; 1 1]
fl, C = is_invertible_with_inverse(A)
@test !fl
@test_throws ErrorException inv(A)
end

@testset "acb_mat.divexact" begin
Expand Down
8 changes: 8 additions & 0 deletions test/arb/arb_mat-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,14 @@ end

@test overlaps(A*C, one(S))
@test contains(C, B)

fl, C = is_invertible_with_inverse(A)
@test fl && contains(C, B)

A = RR[1 1; 1 1]
fl, C = is_invertible_with_inverse(A)
@test !fl
@test_throws ErrorException inv(A)
end

@testset "arb_mat.divexact" begin
Expand Down

0 comments on commit 407b0e0

Please sign in to comment.