Skip to content

Commit af478d6

Browse files
mschauerandyferris
authored andcommitted
lyap function with 1x1 and 2x2 close form solutions (#246)
1 parent 2b9c38f commit af478d6

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

src/StaticArrays.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Base: getindex, setindex!, size, similar, vec, show,
88
length, convert, promote_op, promote_rule, map, map!, reduce, reducedim, mapreducedim,
99
mapreduce, broadcast, broadcast!, conj, transpose, ctranspose,
1010
hcat, vcat, ones, zeros, eye, one, cross, vecdot, reshape, fill,
11-
fill!, det, logdet, inv, eig, eigvals, expm, logm, sqrtm, trace, diag, vecnorm, norm, dot, diagm, diag,
11+
fill!, det, logdet, inv, eig, eigvals, expm, logm, sqrtm, lyap, trace, diag, vecnorm, norm, dot, diagm, diag,
1212
lu, svd, svdvals, svdfact, factorize, ishermitian, issymmetric, isposdef,
1313
sum, diff, prod, count, any, all, minimum,
1414
maximum, extrema, mean, copy, rand, randn, randexp, rand!, randn!,
@@ -95,6 +95,7 @@ include("solve.jl")
9595
include("eigen.jl")
9696
include("expm.jl")
9797
include("sqrtm.jl")
98+
include("lyap.jl")
9899
include("triangular.jl")
99100
include("cholesky.jl")
100101
include("svd.jl")

src/lyap.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
lyap(a::StaticMatrix, c::StaticMatrix) = _lyap(Size(a), Size(c), a, c)
2+
3+
_lyap(::Size{(1,1)}, ::Size{(1,1)}, a::StaticMatrix, c::StaticMatrix) = -c/(2a[1,1])
4+
5+
@inline function _lyap(::Size{(2,2)}, ::Size{(2,2)}, a::StaticMatrix, c::StaticMatrix)
6+
d = det(a)
7+
t = trace(a)
8+
-(d*c + (a - t*I)*c*(a-t*I)')/(2*d*t) # http://www.nber.org/papers/w8956.pdf
9+
end
10+
11+
@inline _lyap(sa::Size, sc::Size, a::StaticMatrix, c::StaticMatrix) = sc(Base.lyap(Array(a),Array(c)))
12+

test/lyap.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@testset "Lyapunov equation" begin
2+
@test lyap(@SMatrix([1]), @SMatrix [2]) === @SMatrix [-1.0]
3+
4+
@test lyap(@SMatrix([1 1; 0 1]), @SMatrix [2 1; 1 2]) == -eye(2)
5+
@test isa(lyap(@SMatrix([1 1; 0 1]), @SMatrix [2 1; 1 2]), SArray{Tuple{2,2},Float64,2,4})
6+
7+
@test lyap(@SMatrix([1 0 1; 0 1 0; 0 0 1]), @SMatrix [2 4 4; 0 4 20; 0 0 20]) == [-5.0 -2.0 3.0; 5.0 -2.0 -10.0; 5.0 0.0 -10.0]
8+
@test isa(lyap(@SMatrix([1 0 1; 0 1 0; 0 0 1]), @SMatrix [2 4 4; 0 4 20; 0 0 20]), SizedArray{Tuple{3,3},Float64,2,2})
9+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ include("solve.jl")
3030
include("eigen.jl")
3131
include("expm.jl")
3232
include("sqrtm.jl")
33+
include("lyap.jl")
3334
include("chol.jl")
3435
include("deque.jl")
3536
include("io.jl")

0 commit comments

Comments
 (0)