Skip to content

Commit ed74c05

Browse files
stevengjdkarrasch
authored andcommitted
restrict inv(::Factorization) to square matrices (#1397)
1 parent 29a195c commit ed74c05

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ function ldiv!(A::SVD{T}, B::AbstractVecOrMat) where T
271271
return B
272272
end
273273

274-
function inv(F::SVD{T}) where T
274+
function inv(F::SVD)
275+
checksquare(F)
275276
@inbounds for i in eachindex(F.S)
276277
iszero(F.S[i]) && throw(SingularException(i))
277278
end

test/svd.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +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)))
5657
@test inv(svd(Matrix(I, 2, 2))) I
5758
@test inv(svd([1 2; 3 4])) [-2.0 1.0; 1.5 -0.5]
58-
@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]
5960
@test_throws SingularException inv(svd([0 0; 0 0]))
6061
@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]
6162
end

0 commit comments

Comments
 (0)