Skip to content

Commit 01e6fea

Browse files
committed
tests for sqrtm
1 parent b417357 commit 01e6fea

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/sqrtm.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
@inline sqrtm(A::StaticMatrix) = _sqrtm(Size(A),A)
22

3-
@inline function _sqrtm(::Size{(2,2)}, A::StaticMatrix)
3+
@inline function _sqrtm(::Size{(1,1)}, A::SA) where {SA<:StaticArray}
4+
s = sqrt(A[1,1])
5+
similar_type(SA,typeof(s))(s)
6+
end
7+
8+
@inline function _sqrtm(::Size{(2,2)}, A::SA) where {SA<:StaticArray}
49
a,b,c,d = A
510
s = sqrtm(a*d-b*c)
611
t = inv(sqrtm(a+d+2s))
7-
similar_type(typeof(A),typeof(t))(t*(a+s), t*b, t*c, t*(d+s))
12+
similar_type(SA,typeof(t))(t*(a+s), t*b, t*c, t*(d+s))
813
end
914

1015
@inline _sqrtm(s::Size, A::StaticArray) = s(Base.sqrtm(Array(A)))

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ using Base.Test
2727
include("solve.jl") # Strange inference / world-age error
2828
include("eigen.jl")
2929
include("expm.jl")
30+
include("sqrtm.jl")
3031
include("chol.jl")
3132
include("deque.jl")
3233
include("io.jl")

test/sqrtm.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@testset "Matrix square root" begin
2+
@test sqrtm(@SMatrix [2])::SMatrix SMatrix{1,1}(sqrtm(2))
3+
@test sqrtm(@SMatrix [5 2; -2 1])::SMatrix sqrtm([5 2; -2 1])
4+
@test sqrtm(@SMatrix [4 2; -2 1])::SMatrix sqrtm([4 2; -2 1])
5+
@test sqrtm(@SMatrix [4 2; 2 1])::SMatrix sqrtm([4 2; 2 1])
6+
@test sqrtm(@SMatrix [1 2 0; 2 1 0; 0 0 1])::SizedArray{Tuple{3,3}} sqrtm([1 2 0; 2 1 0; 0 0 1])
7+
end

0 commit comments

Comments
 (0)