Skip to content

Commit 67c1103

Browse files
authored
restrict inv(::Factorization) to square matrices (#1397)
1 parent b7fd696 commit 67c1103

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

src/factorization.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Factorization{T}(A::AdjointFactorization) where {T} =
110110
adjoint(Factorization{T}(parent(A)))
111111
Factorization{T}(A::TransposeFactorization) where {T} =
112112
transpose(Factorization{T}(parent(A)))
113-
inv(F::Factorization{T}) where {T} = (n = size(F, 1); ldiv!(F, Matrix{T}(I, n, n)))
113+
inv(F::Factorization{T}) where {T} = (n = checksquare(F); ldiv!(F, Matrix{T}(I, n, n)))
114114

115115
Base.hash(F::Factorization, h::UInt) = mapreduce(f -> hash(getfield(F, f)), hash, 1:nfields(F); init=h)
116116
Base.:(==)( F::T, G::T) where {T<:Factorization} = all(f -> getfield(F, f) == getfield(G, f), 1:nfields(F))

src/svd.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ function pinv(F::SVD{T}; atol::Real=0, rtol::Real = (eps(real(float(oneunit(T)))
312312
end
313313

314314
function inv(F::SVD)
315-
# TODO: checksquare(F)
315+
checksquare(F)
316316
@inbounds for i in eachindex(F.S)
317317
iszero(F.S[i]) && throw(SingularException(i))
318318
end

test/svd.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ using LinearAlgebra: BlasComplex, BlasFloat, BlasReal, QRPivoted
5353
@test sf2.U*Diagonal(sf2.S)*sf2.Vt' m2
5454

5555
@test ldiv!([0., 0.], svd(Matrix(I, 2, 2)), [1., 1.]) [1., 1.]
56-
# @test_throws DimensionMismatch inv(svd(Matrix(I, 3, 2)))
56+
@test_throws DimensionMismatch inv(svd(Matrix(I, 3, 2)))
5757
@test inv(svd(Matrix(I, 2, 2))) I
5858
@test inv(svd([1 2; 3 4])) [-2.0 1.0; 1.5 -0.5]
59-
@test inv(svd([1 0 1; 0 1 0])) [0.5 0.0; 0.0 1.0; 0.5 0.0]
59+
@test pinv(svd([1 0 1; 0 1 0])) [0.5 0.0; 0.0 1.0; 0.5 0.0]
6060
@test_throws SingularException inv(svd([0 0; 0 0]))
6161
@test inv(svd([1+2im 3+4im; 5+6im 7+8im])) [-0.5 + 0.4375im 0.25 - 0.1875im; 0.375 - 0.3125im -0.125 + 0.0625im]
6262
end

0 commit comments

Comments
 (0)