Skip to content

Commit 406ba12

Browse files
authored
add handling of sqrt() function for empty matrices (#50270)
1 parent fdc50d8 commit 406ba12

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

stdlib/LinearAlgebra/src/dense.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,9 @@ julia> sqrt(A)
875875
sqrt(::AbstractMatrix)
876876

877877
function sqrt(A::AbstractMatrix{T}) where {T<:Union{Real,Complex}}
878-
if ishermitian(A)
878+
if checksquare(A) == 0
879+
return copy(A)
880+
elseif ishermitian(A)
879881
sqrtHermA = sqrt(Hermitian(A))
880882
return ishermitian(sqrtHermA) ? copytri!(parent(sqrtHermA), 'U', true) : parent(sqrtHermA)
881883
elseif istriu(A)

stdlib/LinearAlgebra/test/dense.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,11 @@ end
12131213
@test exp(log(A2)) A2
12141214
end
12151215

1216+
@testset "sqrt of empty Matrix of type $T" for T in [Int,Float32,Float64,ComplexF32,ComplexF64]
1217+
@test sqrt(Matrix{T}(undef, 0, 0)) == Matrix{T}(undef, 0, 0)
1218+
@test_throws DimensionMismatch sqrt(Matrix{T}(undef, 0, 3))
1219+
end
1220+
12161221
struct TypeWithoutZero end
12171222
Base.zero(::Type{TypeWithoutZero}) = TypeWithZero()
12181223
struct TypeWithZero end

0 commit comments

Comments
 (0)