Skip to content

Commit

Permalink
add multiplication with vectors method (#32)
Browse files Browse the repository at this point in the history
* add multiplication method for BlockDiagonal*Vector

* allow FillArrays v0.8

* bump patch version

* reduce code duplication

* code style as per review
  • Loading branch information
dkarrasch authored and nickrobinson251 committed Dec 12, 2019
1 parent 891ae0d commit 234a99c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name = "BlockDiagonals"
uuid = "0a1fb500-61f7-11e9-3c65-f5ef3456f9f0"
authors = ["Invenia Technical Computing Corporation"]
version = "0.1.4"
version = "0.1.5"

[deps]
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[compat]
FillArrays = "0.6, 0.7"
FillArrays = "0.6, 0.7, 0.8"
julia = "1"

[extras]
Expand Down
18 changes: 13 additions & 5 deletions src/base_maths.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,31 @@ function Base.:*(B1::BlockDiagonal, B2::BlockDiagonal)
end
end

function _check_matmul_dims(A::AbstractMatrix, B::AbstractMatrix)
function _check_matmul_dims(A::AbstractMatrix, B::AbstractVecOrMat)
# match error message from LinearAlgebra
size(A, 2) == size(B, 1) || throw(DimensionMismatch(
"A has dimensions $(size(A)) but B has dimensions $(size(B))"
))
end

function Base.:*(B::BlockDiagonal{T}, M::AbstractMatrix) where T
_check_matmul_dims(B, M)
_mulblocksizes(bblocks, ::AbstractVector) = size.(bblocks, 1)
function _mulblocksizes(bblocks, M::AbstractMatrix)
return zip(size.(bblocks, 1), Base.Iterators.repeated(size(M, 2), length(bblocks)))
end

Base.:*(B::BlockDiagonal, x::AbstractVector) = _mul(B, x)
Base.:*(B::BlockDiagonal, X::AbstractMatrix) = _mul(B, X)

function _mul(B::BlockDiagonal{T}, x::AbstractVecOrMat) where {T}
_check_matmul_dims(B, x)
bblocks = blocks(B)
new_blocksizes = zip(size.(bblocks, 1), fill(size(M, 2), length(bblocks)))
new_blocksizes = _mulblocksizes(bblocks, x)
d = similar.(bblocks, T, new_blocksizes)
ed = 0
@inbounds @views for (p, block) in enumerate(bblocks)
st = ed + 1 # start
ed += size(block, 2) # end
mul!(d[p], block, M[st:ed, :])
mul!(d[p], block, selectdim(x, 1, st:ed))
end
return reduce(vcat, d)
end
Expand Down

2 comments on commit 234a99c

@nickrobinson251
Copy link
Contributor

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

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 Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.5 -m "<description of version>" 234a99c95e6dfd2f61e9f07e700501ff938ac84c
git push origin v0.1.5

Please sign in to comment.