Skip to content

Commit

Permalink
Improve broadcasting of PDMat and PDiagMat (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion authored Nov 22, 2023
1 parent e0cad7c commit cd28425
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "PDMats"
uuid = "90014a1f-27ba-587c-ab20-58faa44d9150"
version = "0.11.29"
version = "0.11.30"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
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 @@ -46,6 +46,9 @@ Base.Matrix(a::PDMat) = Matrix(a.mat)
LinearAlgebra.diag(a::PDMat) = diag(a.mat)
LinearAlgebra.cholesky(a::PDMat) = a.chol

### 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 @@ -256,4 +256,32 @@ using Test
@test_throws DimensionMismatch PDSparseMat(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

2 comments on commit cd28425

@devmotion
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/95783

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.11.30 -m "<description of version>" cd28425977ddaa44c3cae88d59ab3a75c787054c
git push origin v0.11.30

Please sign in to comment.