Skip to content

Commit

Permalink
Add missing methods for Woodbury matrices (#117)
Browse files Browse the repository at this point in the history
* Add additional tests

* Update methods

* Increment patch number

* Remove scalars
  • Loading branch information
sethaxen authored Jan 5, 2023
1 parent 6b18635 commit a66ad91
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Pathfinder"
uuid = "b1d3bc72-d0e7-4279-b92f-7fa5d6d2d454"
authors = ["Seth Axen <[email protected]> and contributors"]
version = "0.6.0"
version = "0.6.1"

[deps]
AbstractDifferentiation = "c29ec348-61ec-40c8-8164-b8c60e9d9f3d"
Expand Down
25 changes: 17 additions & 8 deletions src/woodbury.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ function LinearAlgebra.mul!(
r::StridedVecOrMat{T}, R::WoodburyPDRightFactor{T}, x::StridedVecOrMat{T}
) where {T}
copyto!(r, x)
return lmul!(R, copyto!(r, x))
return lmul!(R, r)
end

function Base.:*(R::WoodburyPDRightFactor, x::StridedVecOrMat)
T = Base.promote_eltype(R, x)
y = copyto!(similar(x, T), x)
return lmul!(R, y)
function LinearAlgebra.mul!(
r::StridedVecOrMat{T}, L::WoodburyPDLeftFactor{T}, x::StridedVecOrMat{T}
) where {T}
copyto!(r, x)
return lmul!(L, r)
end

function LinearAlgebra.lmul!(R::WoodburyPDRightFactor, x::StridedVecOrMat)
Expand Down Expand Up @@ -329,8 +329,17 @@ function LinearAlgebra.lmul!(W::WoodburyPDMat, x::AbstractVecOrMat)
return lmul!(factorize(W), x)
end

function LinearAlgebra.mul!(y::AbstractVector, W::WoodburyPDMat, x::AbstractVecOrMat)
return lmul!(W, copyto!(y, x))
LinearAlgebra.ldiv!(W::WoodburyPDMat, x::AbstractVecOrMat) = ldiv!(factorize(W), x)

function LinearAlgebra.mul!(y::AbstractVecOrMat, W::WoodburyPDMat, x::AbstractVecOrMat)
copyto!(y, x)
return lmul!(W, y)
end

function Base.:\(W::WoodburyPDMat, x::AbstractVecOrMat)
y = similar(x, Base.promote_eltype(W, x))
copyto!(y, x)
return ldiv!(W, y)
end

function Base.:*(W::WoodburyPDMat, c::Real)
Expand Down
31 changes: 30 additions & 1 deletion test/woodbury.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ test_factorization(W::WoodburyPDMat) = test_factorization(W.A, W.B, W.D, W.F)
@test Z \ x Zmat \ x
@test Z' \ x Zmat' \ x
@test mul!(similar(x), Z, x) Zmat * x
@test mul!(similar(x), Z', x) Zmat' * x
@test lmul!(Z, copy(x)) Zmat * x
@test ldiv!(Z, copy(x)) Zmat \ x
end
Expand Down Expand Up @@ -230,6 +231,18 @@ end
@test X Y
end

@testset "ldiv!" begin
x = randn(T, n)
y = Wmat \ x
@test ldiv!(W, x) === x
@test x y

X = randn(T, n, 5)
Y = Wmat \ X
@test ldiv!(W, X) === X
@test X Y
end

@testset "mul!" begin
x = randn(T, n)
y = similar(x)
Expand All @@ -254,10 +267,26 @@ end
x = randn(T, n)
@test W * x Wmat * x

X = randn(T, n)
X = randn(T, n, 2)
@test W * X Wmat * X
end

@testset "\\" begin
x = randn(T, n)
@test W \ x Wmat \ x

X = randn(T, n, 2)
@test W \ X Wmat \ X
end

@testset "/" begin
x = randn(T, n)
@test x' / W x' / Wmat

X = randn(T, 2, n)
@test X / W X / Wmat
end

@testset "PDMats.dim" begin
@test PDMats.dim(W) == n
end
Expand Down

2 comments on commit a66ad91

@sethaxen
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/75213

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.6.1 -m "<description of version>" a66ad918ef3cd325393fe8bbb3e846e921da267a
git push origin v0.6.1

Please sign in to comment.