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/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