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

Implement matrix logarithm #994

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions src/StaticArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ include("inv.jl")
include("solve.jl")
include("eigen.jl")
include("expm.jl")
include("logm.jl")
include("sqrtm.jl")
include("lyap.jl")
include("triangular.jl")
Expand Down
23 changes: 23 additions & 0 deletions src/logm.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@inline log(A::StaticMatrix) = _log(Size(A), A)

@inline function _log(::Size{(0,0)}, A::StaticMatrix)
T = typeof(log(one(eltype(A))))
SMT = similar_type(A,T)
return SMT()
end

@inline function _log(::Size{(1,1)}, A::StaticMatrix)
T = typeof(log(one(eltype(A))))
SMT = similar_type(A,T)
return SMT((log(A[1]), ))
end

function _log(::Size, A::StaticMatrix)
eigA = eigen(A)
T = typeof(log(one(eltype(A)) * one(eltype(eigA.values))))
VT = similar_type(typeof(eigA.values),T)
log_values = log.(VT(eigA.values))
log_eigA = Eigen(log_values, eigA.vectors)
logA = AbstractMatrix(log_eigA)
return logA
end
eschnett marked this conversation as resolved.
Show resolved Hide resolved
1 change: 0 additions & 1 deletion src/sqrtm.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

@inline sqrt(A::StaticMatrix) = _sqrt(Size(A),A)

@inline function _sqrt(::Size{(0,0)}, A::SA) where {SA<:StaticArray}
Expand Down
25 changes: 25 additions & 0 deletions test/logm.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using StaticArrays, Test, LinearAlgebra

@testset "Matrix logarithm" begin
@test log(SMatrix{0,0,Int}())::SMatrix === SMatrix{0,0,Float64}()
@test log(@SMatrix [2])::SMatrix ≈ SMatrix{1,1}(log(2))
@test log(@SMatrix [5 2; -2 1])::SMatrix ≈ log([5 2; -2 1])
@test log(@SMatrix [4 2; -2 1])::SMatrix ≈ log([4 2; -2 1])
@test log(@SMatrix [4 2; 2 1])::SMatrix ≈ log([4 2; 2 1])
# log(::Matrix) is inaccurate on at least Julia 1.1...1.2. We need
# to record the result explicitly.
# @test log(@SMatrix [-3+1im -1+2im; -4-5im 5-2im])::SMatrix ≈ log(Complex{Float64}[-3+1im -1+2im; -4-5im 5-2im])
@test log(@SMatrix [-3+1im -1+2im; -4-5im 5-2im])::SMatrix ≈
[1.6949262962402925 + 2.56908641247419im 0.4359384105167865 + 0.47910454835485583im
-1.7687979183427713 + 0.558514409317816im 1.7199705725159193 + 0.09415380973694587im]
# test for identity matrix
@test log(SMatrix{2,2}(I))::SMatrix ≈ zeros(2,2)
@test log(@SMatrix Complex{Float64}[1 2 0; 2 1 0; 0 0 1]) ≈ log([1 2 0; 2 1 0; 0 0 1])

for sz in 0:4, typ in (Float64, Complex{Float64})
A = rand(SMatrix{sz,sz,typ})
expA = exp(A)
logexpA = log(expA)
@test logexpA ≈ A
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ addtests("inv.jl")
addtests("solve.jl")
addtests("eigen.jl")
addtests("expm.jl")
addtests("logm.jl")
addtests("sqrtm.jl")
addtests("lyap.jl")
addtests("lu.jl")
Expand Down