-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preserve block structure in broadcasting #61
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d0302dc
Preserve block structure
dlfivefifty 2e04ae7
remove currently unused linalg.jl
dlfivefifty 051a03c
Support Matrix .+ Vector
dlfivefifty c25631c
fix tests
dlfivefifty 096229f
Arrays and numbers implement BlockArray interface (#63)
dlfivefifty 778ae97
try fixing 0.7 tests
dlfivefifty 433b592
make test approx eq
dlfivefifty 7340610
Support StructuredMatrixStyle's
dlfivefifty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,56 @@ | ||
|
||
# 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)(_max(Val(M),Val(N))) | ||
BroadcastStyle(a::AbstractBlockStyle{N}, ::DefaultArrayStyle{M}) where {M,N} = typeof(a)(_max(Val(M),Val(N))) | ||
BroadcastStyle(::BlockStyle{M}, ::PseudoBlockStyle{N}) where {M,N} = BlockStyle(_max(Val(M),Val(N))) | ||
BroadcastStyle(::PseudoBlockStyle{M}, ::BlockStyle{N}) where {M,N} = BlockStyle(_max(Val(M),Val(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
using BlockArrays | ||
|
||
struct PartiallyImplementedBlockVector <: AbstractBlockArray{Float64,1} end | ||
|
||
@testset "partially implemented block array" begin | ||
|
@@ -25,6 +27,21 @@ struct PartiallyImplementedBlockVector <: AbstractBlockArray{Float64,1} end | |
end | ||
end | ||
|
||
@testset "Array block interface" begin | ||
@test blocksizes(1) == BlockArrays.BlockSizes{0}() | ||
@test 1[Block()] == 1 | ||
|
||
A = randn(5) | ||
@test blocksizes(A) == BlockArrays.BlockSizes([5]) | ||
A[Block(1)] == A | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
view(A,Block(1))[1] = 2 | ||
@test A[1] == 2 | ||
@test_throws BoundsError A[Block(2)] | ||
|
||
A = randn(5,5) | ||
@test A[Block(1,1)] == A | ||
end | ||
|
||
@testset "Triangular/Symmetric/Hermitian block arrays" begin | ||
A = PseudoBlockArray{ComplexF64}(undef, (1:4), (1:4)) | ||
A .= randn.() .+ randn.().*im | ||
|
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,87 @@ | ||
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) | ||
|
||
A .+ C | ||
|
||
|
||
@test A + C isa BlockVector{Float64} | ||
@test C + A isa BlockVector{Float64} | ||
@test B + C isa PseudoBlockVector{Float64} | ||
@test C + B isa PseudoBlockVector{Float64} | ||
|
||
blocksizes(A+C) == blocksizes(C+A) == blocksizes(A) | ||
blocksizes(B+C) == blocksizes(C+B) == blocksizes(B) | ||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you planning to "vendor"
_max
at some point? Otherwise it doesn't run on Julia master.