Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_invertible_with_inverse for arb matrices #1525

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading