Skip to content

Commit 1858207

Browse files
Allow 0x0 matrices for exp, inv, sqrt (#991)
* Allow 0x0 matrices for exp, sqrt * Implement inv for 0x0 matrices * Update test/expm.jl Co-authored-by: Mateusz Baran <[email protected]> * Update test/inv.jl Co-authored-by: Mateusz Baran <[email protected]> * Update test/sqrtm.jl Co-authored-by: Mateusz Baran <[email protected]> * Bump patch version to 1.3.5 Co-authored-by: Mateusz Baran <[email protected]>
1 parent e1cbee2 commit 1858207

File tree

7 files changed

+17
-1
lines changed

7 files changed

+17
-1
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StaticArrays"
22
uuid = "90137ffa-7385-5640-81b9-e52037218182"
3-
version = "1.3.4"
3+
version = "1.3.5"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/expm.jl

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
@inline exp(A::StaticMatrix) = _exp(Size(A), A)
22

3+
@inline function _exp(::Size{(0,0)}, A::StaticMatrix)
4+
T = typeof(exp(zero(eltype(A))))
5+
newtype = similar_type(A,T)
6+
7+
(newtype)()
8+
end
9+
310
@inline function _exp(::Size{(1,1)}, A::StaticMatrix)
411
T = typeof(exp(zero(eltype(A))))
512
newtype = similar_type(A,T)

src/inv.jl

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
_inv(Size(A_S),A_S)
66
end
77

8+
@inline _inv(::Size{(0,0)}, A) = similar_type(A,typeof(inv(one(eltype(A)))))()
9+
810
@inline _inv(::Size{(1,1)}, A) = similar_type(A)(inv(A[1]))
911

1012
@inline function _inv(::Size{(2,2)}, A)

src/sqrtm.jl

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11

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

4+
@inline function _sqrt(::Size{(0,0)}, A::SA) where {SA<:StaticArray}
5+
similar_type(A,typeof(sqrt(zero(eltype(A)))))()
6+
end
7+
48
@inline function _sqrt(::Size{(1,1)}, A::SA) where {SA<:StaticArray}
59
s = sqrt(A[1,1])
610
similar_type(SA,typeof(s))(s)

test/expm.jl

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using StaticArrays, Test, LinearAlgebra
22

33
@testset "Matrix exponential" begin
4+
@test exp(SMatrix{0,0,Int}())::SMatrix === SMatrix{0,0,Float64}()
45
@test exp(@SMatrix [2])::SMatrix SMatrix{1,1}(exp(2))
56
@test exp(@SMatrix [5 2; -2 1])::SMatrix exp([5 2; -2 1])
67
@test exp(@SMatrix [4 2; -2 1])::SMatrix exp([4 2; -2 1])

test/inv.jl

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function almost_singular_matrix(N, rank, ϵ)
1919
end
2020

2121
@testset "Matrix inverse" begin
22+
@test inv(SMatrix{0,0,Int}())::SMatrix === SMatrix{0,0,Float64}()
2223
@test inv(@SMatrix [2])::SMatrix @SMatrix [0.5]
2324
@test inv(@SMatrix [1 2; 2 1])::SMatrix [-1/3 2/3; 2/3 -1/3]
2425
@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]

test/sqrtm.jl

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using StaticArrays, Test
22

33
@testset "Matrix square root" begin
4+
@test sqrt(SMatrix{0,0,Int}())::SMatrix === SMatrix{0,0,Float64}()
45
@test sqrt(@SMatrix [2])::SMatrix SMatrix{1,1}(sqrt(2))
56
@test sqrt(@SMatrix [5 2; -2 1])::SMatrix sqrt([5 2; -2 1])
67
@test sqrt(@SMatrix [4 2; -2 1])::SMatrix sqrt([4 2; -2 1])

0 commit comments

Comments
 (0)