From 9c3a3011a889a0a3b77b08f7a87d57ff13d73ba0 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Fri, 10 Nov 2023 09:04:53 +0100 Subject: [PATCH] Ensure that `Matrix` constructor returns a `Matrix` for a `PDMat` (#196) * Ensure that `Matrix` constructor returns a `Matrix` for a `PDMat` * Add test for `Matrix` constructor * Check type of `Matrix` for static pd matrices --- src/pdmat.jl | 2 +- test/specialarrays.jl | 2 ++ test/testutils.jl | 5 +++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/pdmat.jl b/src/pdmat.jl index bc09e3e..0e44780 100644 --- a/src/pdmat.jl +++ b/src/pdmat.jl @@ -42,7 +42,7 @@ Base.convert(::Type{AbstractPDMat{T}}, a::PDMat) where {T<:Real} = convert(PDMat ### Basics Base.size(a::PDMat) = (a.dim, a.dim) -Base.Matrix(a::PDMat) = copy(a.mat) +Base.Matrix(a::PDMat) = Matrix(a.mat) LinearAlgebra.diag(a::PDMat) = diag(a.mat) LinearAlgebra.cholesky(a::PDMat) = a.chol diff --git a/test/specialarrays.jl b/test/specialarrays.jl index e896d72..75468ba 100644 --- a/test/specialarrays.jl +++ b/test/specialarrays.jl @@ -28,6 +28,8 @@ using StaticArrays Y = @SMatrix rand(4, 10) for A in (PDS, D, E, C) + @test Matrix(A) isa Matrix + if !(A isa Cholesky) # `*(::Cholesky, ::SArray)` is not defined @test A * x isa SVector{4, Float64} diff --git a/test/testutils.jl b/test/testutils.jl index 96843c9..32bd5a9 100644 --- a/test/testutils.jl +++ b/test/testutils.jl @@ -98,6 +98,11 @@ function pdtest_basics(C, Cmat::Matrix, d::Int, verbose::Int) if C isa AbstractPDMat @test M === C end + + _pdt(verbose, "Matrix") + M = Matrix(C) + @test M isa Matrix + @test M == Cmat end