From 14bf424c27521894f5f17bdf1bce56a51a45ee59 Mon Sep 17 00:00:00 2001 From: Moritz Schauer Date: Wed, 19 Jul 2017 01:30:45 +0200 Subject: [PATCH] Define chol for UniformScaling's (#22633) * Define chol for UniformScaling's * Move test for chol(lambda*I) to test/linalg/uniformscaling.jl --- base/linalg/uniformscaling.jl | 24 ++++++++++++++++++++++++ test/linalg/uniformscaling.jl | 8 ++++++++ 2 files changed, 32 insertions(+) diff --git a/base/linalg/uniformscaling.jl b/base/linalg/uniformscaling.jl index c85583106cce35..515b93ed07dd0f 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/uniformscaling.jl b/test/linalg/uniformscaling.jl index 2eba997a031adc..9bdde7b7470e8b 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