Skip to content

Allow 0x0 matrices for exp, inv, sqrt #991

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

Merged
merged 6 commits into from
Feb 14, 2022
Merged
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StaticArrays"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.3.4"
version = "1.3.5"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
7 changes: 7 additions & 0 deletions src/expm.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
@inline exp(A::StaticMatrix) = _exp(Size(A), A)

@inline function _exp(::Size{(0,0)}, A::StaticMatrix)
T = typeof(exp(zero(eltype(A))))
newtype = similar_type(A,T)

(newtype)()
end

@inline function _exp(::Size{(1,1)}, A::StaticMatrix)
T = typeof(exp(zero(eltype(A))))
newtype = similar_type(A,T)
Expand Down
2 changes: 2 additions & 0 deletions src/inv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
_inv(Size(A_S),A_S)
end

@inline _inv(::Size{(0,0)}, A) = similar_type(A,typeof(inv(one(eltype(A)))))()

@inline _inv(::Size{(1,1)}, A) = similar_type(A)(inv(A[1]))

@inline function _inv(::Size{(2,2)}, A)
Expand Down
4 changes: 4 additions & 0 deletions src/sqrtm.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

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

@inline function _sqrt(::Size{(0,0)}, A::SA) where {SA<:StaticArray}
similar_type(A,typeof(sqrt(zero(eltype(A)))))()
end

@inline function _sqrt(::Size{(1,1)}, A::SA) where {SA<:StaticArray}
s = sqrt(A[1,1])
similar_type(SA,typeof(s))(s)
Expand Down
1 change: 1 addition & 0 deletions test/expm.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using StaticArrays, Test, LinearAlgebra

@testset "Matrix exponential" begin
@test exp(SMatrix{0,0,Int}())::SMatrix === SMatrix{0,0,Float64}()
@test exp(@SMatrix [2])::SMatrix ≈ SMatrix{1,1}(exp(2))
@test exp(@SMatrix [5 2; -2 1])::SMatrix ≈ exp([5 2; -2 1])
@test exp(@SMatrix [4 2; -2 1])::SMatrix ≈ exp([4 2; -2 1])
Expand Down
1 change: 1 addition & 0 deletions test/inv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function almost_singular_matrix(N, rank, ϵ)
end

@testset "Matrix inverse" begin
@test inv(SMatrix{0,0,Int}())::SMatrix === SMatrix{0,0,Float64}()
@test inv(@SMatrix [2])::SMatrix ≈ @SMatrix [0.5]
@test inv(@SMatrix [1 2; 2 1])::SMatrix ≈ [-1/3 2/3; 2/3 -1/3]
@test inv(@SMatrix [1 2 0; 2 1 0; 0 0 1])::SMatrix ≈ [-1/3 2/3 0; 2/3 -1/3 0; 0 0 1]
Expand Down
1 change: 1 addition & 0 deletions test/sqrtm.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using StaticArrays, Test

@testset "Matrix square root" begin
@test sqrt(SMatrix{0,0,Int}())::SMatrix === SMatrix{0,0,Float64}()
@test sqrt(@SMatrix [2])::SMatrix ≈ SMatrix{1,1}(sqrt(2))
@test sqrt(@SMatrix [5 2; -2 1])::SMatrix ≈ sqrt([5 2; -2 1])
@test sqrt(@SMatrix [4 2; -2 1])::SMatrix ≈ sqrt([4 2; -2 1])
Expand Down