Skip to content

Commit 7aa7188

Browse files
committed
Make Cholesky factorization compatible with 0.7
Note that the error handling is still a bit different as `_chol!` still throws an error instead of returning a non-zero `info`.
1 parent 7a907c8 commit 7aa7188

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

src/SDiagonal.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ else
9494
log(D::SDiagonal) = SDiagonal(log.(D.diag))
9595
sqrt(D::SDiagonal) = SDiagonal(sqrt.(D.diag))
9696
end
97-
LinearAlgebra.chol(D::SDiagonal) = SDiagonal(Base.chol.(D.diag))
97+
LinearAlgebra.chol(D::SDiagonal) = SDiagonal(chol.(D.diag))
9898
LinearAlgebra._chol!(D::SDiagonal, ::Type{UpperTriangular}) = chol(D)
9999

100100
\(D::SDiagonal, B::StaticMatrix) = scalem(1 ./ D.diag, B)
@@ -112,4 +112,3 @@ function inv(D::SDiagonal)
112112
check_singular(D)
113113
SDiagonal(inv.(D.diag))
114114
end
115-

src/cholesky.jl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
# Generic Cholesky decomposition for fixed-size matrices, mostly unrolled
2+
if isdefined(Compat.LinearAlgebra, :non_hermitian_error)
3+
non_hermitian_error() = Compat.LinearAlgebra.non_hermitian_error("chol")
4+
else
5+
non_hermitian_error() = throw(Compat.LinearAlgebra.PosDefException(-1))
6+
end
27
@inline function LinearAlgebra.chol(A::StaticMatrix)
3-
ishermitian(A) || LinearAlgebra.non_hermitian_error("chol")
8+
ishermitian(A) || non_hermitian_error()
49
_chol(Size(A), A)
510
end
611

712
@inline function LinearAlgebra.chol(A::LinearAlgebra.RealHermSymComplexHerm{<:Real, <:StaticMatrix})
813
_chol(Size(A), A.data)
914
end
10-
@inline LinearAlgebra._chol!(A::StaticMatrix, ::Type{UpperTriangular}) = chol(A)
15+
if VERSION < v"0.7.0-DEV.393"
16+
@inline LinearAlgebra._chol!(A::StaticMatrix, ::Type{UpperTriangular}) = chol(A)
17+
else
18+
# TODO should return non-zero info instead of throwing on errors
19+
@inline LinearAlgebra._chol!(A::StaticMatrix, ::Type{UpperTriangular}) = (chol(A), 0)
20+
end
1121

1222

1323
@generated function _chol(::Size{(1,1)}, A::StaticMatrix)
@@ -50,4 +60,4 @@ end
5060
end
5161

5262
# Otherwise default algorithm returning wrapped SizedArray
53-
@inline _chol(s::Size, A::StaticArray) = s(full(chol(Hermitian(Array(A)))))
63+
@inline _chol(s::Size, A::StaticArray) = s(Matrix(chol(Hermitian(Array(A)))))

test/SDiagonal.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
using Compat.LinearAlgebra: chol
2+
13
@testset "SDiagonal" begin
24
@testset "Constructors" begin
35
@test SDiagonal{1,Int64}((1,)).diag === SVector{1,Int64}((1,))
46
@test SDiagonal{1,Float64}((1,)).diag === SVector{1,Float64}((1,))
5-
7+
68
@test SDiagonal{4,Float64}((1, 1.0, 1, 1)).diag.data === (1.0, 1.0, 1.0, 1.0)
79
@test SDiagonal{4}((1, 1.0, 1, 1)).diag.data === (1.0, 1.0, 1.0, 1.0)
810
@test SDiagonal((1, 1.0, 1, 1)).diag.data === (1.0, 1.0, 1.0, 1.0)

test/chol.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
using StaticArrays, Compat, Compat.Test, Compat.LinearAlgebra
2+
13
@testset "Cholesky decomposition" begin
4+
if isdefined(Compat.LinearAlgebra, :PosDefException)
5+
PosDefException = Compat.LinearAlgebra.PosDefException
6+
else
7+
PosDefException = ArgumentError
8+
end
29
@testset "1×1" begin
310
m = @SMatrix [4.0]
411
(c,) = chol(m)
@@ -8,7 +15,7 @@
815
@testset "2×2" for i = 1:100
916
m_a = randn(2,2)
1017
#non hermitian
11-
@test_throws ArgumentError chol(SMatrix{2,2}(m_a))
18+
@test_throws PosDefException chol(SMatrix{2,2}(m_a))
1219
m_a = m_a*m_a'
1320
m = SMatrix{2,2}(m_a)
1421
@test chol(Hermitian(m)) chol(m_a)
@@ -17,7 +24,7 @@
1724
@testset "3×3" for i = 1:100
1825
m_a = randn(3,3)
1926
#non hermitian
20-
@test_throws ArgumentError chol(SMatrix{3,3}(m_a))
27+
@test_throws PosDefException chol(SMatrix{3,3}(m_a))
2128
m_a = m_a*m_a'
2229
m = SMatrix{3,3}(m_a)
2330
@test chol(m) chol(m_a)
@@ -26,7 +33,7 @@
2633
@testset "4×4" for i = 1:100
2734
m_a = randn(4,4)
2835
#non hermitian
29-
@test_throws ArgumentError chol(SMatrix{4,4}(m_a))
36+
@test_throws PosDefException chol(SMatrix{4,4}(m_a))
3037
m_a = m_a*m_a'
3138
m = SMatrix{4,4}(m_a)
3239
@test chol(m) chol(m_a)
@@ -36,7 +43,7 @@
3643
m_a = randn(3,3)
3744
m_a = m_a*m_a'
3845
m = SMatrix{3,3}(m_a)
39-
@test chol(reshape([m, 0m, 0m, m], 2, 2)) ==
46+
@test chol(reshape([m, 0m, 0m, m], 2, 2)) ==
4047
reshape([chol(m), 0m, 0m, chol(m)], 2, 2)
4148
end
4249
end

0 commit comments

Comments
 (0)