From 46b53ef03d796b1caf6ced3c56ad7567e4f5674f Mon Sep 17 00:00:00 2001 From: mschauer Date: Fri, 30 Jun 2017 17:37:22 +0200 Subject: [PATCH 1/2] Define chol for UniformScaling's --- base/linalg/uniformscaling.jl | 24 ++++++++++++++++++++++++ test/linalg/cholesky.jl | 3 +++ 2 files changed, 27 insertions(+) diff --git a/base/linalg/uniformscaling.jl b/base/linalg/uniformscaling.jl index c85583106cce3..515b93ed07dd0 100644 --- a/base/linalg/uniformscaling.jl +++ b/base/linalg/uniformscaling.jl @@ -313,3 +313,27 @@ function hvcat(rows::Tuple{Vararg{Int}}, A::Union{AbstractVecOrMat,UniformScalin end return hvcat(rows, promote_to_arrays(n,1, promote_to_array_type(A), A...)...) end + + +## Cholesky +function _chol!(J::UniformScaling, uplo) + c, info = _chol!(J.λ, uplo) + UniformScaling(c), info +end + +chol!(J::UniformScaling, uplo) = ((J, info) = _chol!(J, uplo); @assertposdef J info) + +""" + chol(J::UniformScaling) -> C + +Compute the square root of a non-negative UniformScaling `J`. + +# Examples +```jldoctest +julia> chol(16I) +UniformScaling{Float64} +4.0*I +``` +""" +chol(J::UniformScaling, args...) = ((C, info) = _chol!(J, nothing); @assertposdef C info) + diff --git a/test/linalg/cholesky.jl b/test/linalg/cholesky.jl index dce15ce0d776c..948df0cfcedfe 100644 --- a/test/linalg/cholesky.jl +++ b/test/linalg/cholesky.jl @@ -59,6 +59,9 @@ using Base.LinAlg: BlasComplex, BlasFloat, BlasReal, QRPivoted, PosDefException @test all(x -> x ≈ √apos, cholfact(apos).factors) @test_throws PosDefException chol(-one(eltya)) + @test √apos*I ≈ chol(apos*I) + @test_throws PosDefException chol(-one(eltya)*I) + # Test cholfact with Symmetric/Hermitian upper/lower apds = Symmetric(apd) apdsL = Symmetric(apd, :L) From 6138c663051781ec34295b1b9d11a212cfa6bce1 Mon Sep 17 00:00:00 2001 From: mschauer Date: Fri, 7 Jul 2017 13:51:44 +0200 Subject: [PATCH 2/2] Move test for chol(lambda*I) to test/linalg/uniformscaling.jl --- test/linalg/cholesky.jl | 3 --- test/linalg/uniformscaling.jl | 8 ++++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/test/linalg/cholesky.jl b/test/linalg/cholesky.jl index 948df0cfcedfe..dce15ce0d776c 100644 --- a/test/linalg/cholesky.jl +++ b/test/linalg/cholesky.jl @@ -59,9 +59,6 @@ using Base.LinAlg: BlasComplex, BlasFloat, BlasReal, QRPivoted, PosDefException @test all(x -> x ≈ √apos, cholfact(apos).factors) @test_throws PosDefException chol(-one(eltya)) - @test √apos*I ≈ chol(apos*I) - @test_throws PosDefException chol(-one(eltya)*I) - # Test cholfact with Symmetric/Hermitian upper/lower apds = Symmetric(apd) apdsL = Symmetric(apd, :L) diff --git a/test/linalg/uniformscaling.jl b/test/linalg/uniformscaling.jl index 2eba997a031ad..9bdde7b7470e8 100644 --- a/test/linalg/uniformscaling.jl +++ b/test/linalg/uniformscaling.jl @@ -173,3 +173,11 @@ end hvcat((2,1,2),B,2eye(3,3),eye(6,6),3eye(3,3),4eye(3,3)) end end + +@testset "chol" begin + for T in (Float64, Complex64, BigFloat, Int) + λ = T(4) + @test chol(λ*I) ≈ √λ*I + @test_throws LinAlg.PosDefException chol(-λ*I) + end +end