-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preserve block structure in broadcasting (#61)
* Preserve block structure * remove currently unused linalg.jl * Support Matrix .+ Vector * fix tests * Arrays and numbers implement BlockArray interface (#63) * try fixing 0.7 tests * make test approx eq * Support StructuredMatrixStyle's
- Loading branch information
1 parent
d92f0b4
commit 9024edf
Showing
10 changed files
with
186 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
|
||
# Here we override broadcasting for banded matrices. | ||
# The design is to to exploit the broadcast machinery so that | ||
# banded matrices that conform to the banded matrix interface but are not | ||
# <: AbstractBandedMatrix can get access to fast copyto!, lmul!, rmul!, axpy!, etc. | ||
# using broadcast variants (B .= A, B .= 2.0 .* A, etc.) | ||
|
||
|
||
abstract type AbstractBlockStyle{N} <: AbstractArrayStyle{N} end | ||
struct BlockStyle{N} <: AbstractBlockStyle{N} end | ||
struct PseudoBlockStyle{N} <: AbstractBlockStyle{N} end | ||
|
||
|
||
BlockStyle(::Val{N}) where {N} = BlockStyle{N}() | ||
PseudoBlockStyle(::Val{N}) where {N} = PseudoBlockStyle{N}() | ||
BlockStyle{M}(::Val{N}) where {N,M} = BlockStyle{N}() | ||
PseudoBlockStyle{M}(::Val{N}) where {N,M} = PseudoBlockStyle{N}() | ||
BroadcastStyle(::Type{<:BlockArray{<:Any,N}}) where N = BlockStyle{N}() | ||
BroadcastStyle(::Type{<:PseudoBlockArray{<:Any,N}}) where N = PseudoBlockStyle{N}() | ||
BroadcastStyle(::DefaultArrayStyle{N}, b::AbstractBlockStyle{M}) where {M,N} = typeof(b)(Val(max(M,N))) | ||
BroadcastStyle(a::AbstractBlockStyle{N}, ::DefaultArrayStyle{M}) where {M,N} = typeof(a)(Val(max(M,N))) | ||
BroadcastStyle(::StructuredMatrixStyle, b::AbstractBlockStyle{M}) where {M} = typeof(b)(Val(max(M,2))) | ||
BroadcastStyle(a::AbstractBlockStyle{M}, ::StructuredMatrixStyle) where {M} = typeof(a)(Val(max(M,2))) | ||
BroadcastStyle(::BlockStyle{M}, ::PseudoBlockStyle{N}) where {M,N} = BlockStyle(Val(max(M,N))) | ||
BroadcastStyle(::PseudoBlockStyle{M}, ::BlockStyle{N}) where {M,N} = BlockStyle(Val(max(M,N))) | ||
|
||
|
||
#### | ||
# Default to standard Array broadcast | ||
#### | ||
|
||
|
||
# following code modified from julia/base/broadcast.jl | ||
broadcast_cumulsizes(::Number) = () | ||
broadcast_cumulsizes(A::AbstractArray) = cumulsizes(blocksizes(A)) | ||
broadcast_cumulsizes(A::Broadcasted) = cumulsizes(blocksizes(A)) | ||
|
||
combine_cumulsizes(A) = A | ||
combine_cumulsizes(A, B, C...) = combine_cumulsizes(_cms(A,B), C...) | ||
|
||
_cms(::Tuple{}, ::Tuple{}) = () | ||
_cms(::Tuple{}, newshape::Tuple) = (newshape[1], _cms((), tail(newshape))...) | ||
_cms(shape::Tuple, ::Tuple{}) = (shape[1], _cms(tail(shape), ())...) | ||
_cms(shape::Tuple, newshape::Tuple) = (sort!(union(shape[1], newshape[1])), _cms(tail(shape), tail(newshape))...) | ||
|
||
|
||
blocksizes(A::Broadcasted{<:AbstractArrayStyle{N}}) where N = | ||
BlockSizes(combine_cumulsizes(broadcast_cumulsizes.(A.args)...)) | ||
|
||
|
||
copyto!(dest::AbstractArray, bc::Broadcasted{<:AbstractBlockStyle}) = | ||
copyto!(dest, Broadcasted{DefaultArrayStyle{2}}(bc.f, bc.args, bc.axes)) | ||
|
||
similar(bc::Broadcasted{<:AbstractBlockStyle{N}}, ::Type{T}) where {T,N} = | ||
BlockArray{T,N}(undef, blocksizes(bc)) | ||
|
||
similar(bc::Broadcasted{PseudoBlockStyle{N}}, ::Type{T}) where {T,N} = | ||
PseudoBlockArray{T,N}(undef, blocksizes(bc)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +0,0 @@ | ||
@deprecate getindex(block_sizes::BlockSizes, i) cumulsizes(block_sizes, i) | ||
@deprecate getindex(block_sizes::BlockSizes, i, j) cumulsizes(block_sizes, i, j) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using BlockArrays, Test | ||
|
||
@testset "broadcast" begin | ||
@testset "BlockArray" begin | ||
A = BlockArray(randn(6), 1:3) | ||
|
||
@test BlockArrays.BroadcastStyle(typeof(A)) == BlockArrays.BlockStyle{1}() | ||
|
||
@test exp.(A) == exp.(Vector(A)) | ||
@test blocksizes(A) == blocksizes(exp.(A)) | ||
|
||
@test A+A isa BlockArray | ||
@test blocksizes(A + A) == blocksizes(A .+ A) == blocksizes(A) | ||
@test blocksizes(A .+ 1) == blocksizes(A) | ||
|
||
A = BlockArray(randn(6,6), 1:3,1:3) | ||
|
||
@test BlockArrays.BroadcastStyle(typeof(A)) == BlockArrays.BlockStyle{2}() | ||
|
||
@test exp.(A) == exp.(Matrix(A)) | ||
@test blocksizes(A) == blocksizes(exp.(A)) | ||
|
||
|
||
@test blocksizes(A + A) == blocksizes(A .+ A) == blocksizes(A) | ||
@test blocksizes(A .+ 1) == blocksizes(A) | ||
end | ||
|
||
@testset "PseudoBlockArray" begin | ||
A = PseudoBlockArray(randn(6), 1:3) | ||
|
||
@test BlockArrays.BroadcastStyle(typeof(A)) == BlockArrays.PseudoBlockStyle{1}() | ||
|
||
|
||
@test exp.(A) == exp.(Vector(A)) | ||
@test blocksizes(A) == blocksizes(exp.(A)) | ||
|
||
@test A+A isa PseudoBlockArray | ||
@test blocksizes(A + A) == blocksizes(A .+ A) == blocksizes(A) | ||
@test blocksizes(A .+ 1) == blocksizes(A) | ||
|
||
B = PseudoBlockArray(randn(6,6), 1:3,1:3) | ||
|
||
@test BlockArrays.BroadcastStyle(typeof(B)) == BlockArrays.PseudoBlockStyle{2}() | ||
|
||
@test exp.(B) == exp.(Matrix(B)) | ||
@test blocksizes(B) == blocksizes(exp.(B)) | ||
|
||
@test blocksizes(B + B) == blocksizes(B .+ B) == blocksizes(B) | ||
@test blocksizes(B .+ 1) == blocksizes(B) | ||
@test blocksizes(A .+ 1 .+ B) == blocksizes(B) | ||
@test A .+ 1 .+ B == Vector(A) .+ 1 .+ B == Vector(A) .+ 1 .+ Matrix(B) | ||
end | ||
|
||
@testset "Mixed" begin | ||
A = BlockArray(randn(6), 1:3) | ||
B = PseudoBlockArray(randn(6), 1:3) | ||
|
||
@test A + B isa BlockArray | ||
@test B + A isa BlockArray | ||
|
||
@test blocksizes(A + B) == blocksizes(A) | ||
|
||
C = randn(6) | ||
|
||
@test A + C isa BlockVector{Float64} | ||
@test C + A isa BlockVector{Float64} | ||
@test B + C isa PseudoBlockVector{Float64} | ||
@test C + B isa PseudoBlockVector{Float64} | ||
|
||
@test blocksizes(A+C) == blocksizes(C+A) == blocksizes(A) | ||
@test blocksizes(B+C) == blocksizes(C+B) == blocksizes(B) | ||
|
||
A = BlockArray(randn(6,6), 1:3, 1:3) | ||
D = Diagonal(ones(6)) | ||
@test blocksizes(A + D) == blocksizes(A) | ||
@test blocksizes(B .+ D) == BlockArrays.BlockSizes([1,2,3],[6]) | ||
end | ||
|
||
@testset "Mixed block sizes" begin | ||
A = BlockArray(randn(6), 1:3) | ||
B = BlockArray(randn(6), fill(2,3)) | ||
@test blocksizes(A+B) == BlockArrays.BlockSizes([1,1,1,1,2]) | ||
|
||
A = BlockArray(randn(6,6), 1:3, 1:3) | ||
B = BlockArray(randn(6,6), fill(2,3), fill(3,2)) | ||
|
||
@test blocksizes(A+B) == BlockArrays.BlockSizes([1,1,1,1,2], 1:3) | ||
end | ||
end |