Skip to content

Commit

Permalink
Merge branch 'master' into dw/unify_pdmat_pdsparsemat
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion authored Nov 27, 2023
2 parents 33a80f3 + cd28425 commit 60df321
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/pdiagmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Base.Matrix(a::PDiagMat) = Matrix(Diagonal(a.diag))
LinearAlgebra.diag(a::PDiagMat) = copy(a.diag)
LinearAlgebra.cholesky(a::PDiagMat) = Cholesky(Diagonal(map(sqrt, a.diag)), 'U', 0)

### Treat as a `Diagonal` matrix in broadcasting since that is better supported
Base.broadcastable(a::PDiagMat) = Base.broadcastable(Diagonal(a.diag))

### Inheriting from AbstractMatrix

function Base.getindex(a::PDiagMat, i::Integer)
Expand Down
3 changes: 3 additions & 0 deletions src/pdmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Base.Matrix(a::PDMat) = Matrix(a.mat)
LinearAlgebra.diag(a::PDMat) = diag(a.mat)
LinearAlgebra.cholesky(a::PDMatCholesky) = a.fact

### Work with the underlying matrix in broadcasting
Base.broadcastable(a::PDMat) = Base.broadcastable(a.mat)

### Inheriting from AbstractMatrix

Base.getindex(a::PDMat, i::Int) = getindex(a.mat, i)
Expand Down
28 changes: 28 additions & 0 deletions test/pdmtypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,32 @@ using Test
@test_throws DimensionMismatch PDMat(A[1:(end - 1), 1:(end - 1)], C)
end
end

@testset "Subtraction" begin
# This falls back to the generic method in Julia based on broadcasting
dim = 4
x = rand(dim, dim)
A = PDMat(x' * x + I)
@test Base.broadcastable(A) == A.mat

B = PDiagMat(rand(dim))
@test Base.broadcastable(B) == Diagonal(B.diag)

for X in (A, B), Y in (A, B)
@test X - Y isa (X === Y === B ? Diagonal{Float64, Vector{Float64}} : Matrix{Float64})
@test X - Y Matrix(X) - Matrix(Y)
end

C = ScalMat(dim, rand())
@test A - C isa Matrix{Float64}
@test A - C Matrix(A) - Matrix(C)
@test C - A isa Matrix{Float64}
@test C - A Matrix(C) - Matrix(A)

# ScalMat does not behave nicely with PDiagMat
@test_broken B - C isa Diagonal{Float64, Vector{Float64}}
@test B - C Matrix(B) - Matrix(C)
@test_broken C - B isa Diagonal{Float64, Vector{Float64}}
@test C - B Matrix(C) - Matrix(B)
end
end
16 changes: 16 additions & 0 deletions test/specialarrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ using StaticArrays
@test Xt_invA_X(A, Y) isa Symmetric{Float64,<:SMatrix{10, 10, Float64}}
@test Xt_invA_X(A, Y) Matrix(Y)' * (Matrix(A) \ Matrix(Y))
end

# Subtraction falls back to the generic method in Base which is based on broadcasting
@test Base.broadcastable(PDS) == PDS.mat
@test Base.broadcastable(D) == Diagonal(D.diag)
for A in (PDS, D), B in (PDS, D)
@test A - B isa SMatrix{4, 4, Float64}
@test A - B Matrix(A) - Matrix(B)
end

# ScalMat does not behave nicely with broadcasting currently
for A in (PDS, D)
@test_broken A - E isa SMatrix{4, 4, Float64}
@test_broken E - A isa SMatrix{4, 4, Float64}
@test A - E Matrix(A) - Matrix(E)
@test E - A Matrix(E) - Matrix(A)
end
end

@testset "BandedMatrices" begin
Expand Down

0 comments on commit 60df321

Please sign in to comment.