-
Notifications
You must be signed in to change notification settings - Fork 31
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
Create constructor for creating allocated BlockArrays #39
Changes from 6 commits
9e1187c
a3302fe
f12e957
42d7a49
9e5ad98
526d846
6cdf3ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
julia 0.6 | ||
Compat 0.30.0 | ||
Compat 0.38.0 | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,28 @@ DocTestSetup = quote | |
end | ||
``` | ||
|
||
|
||
## Creating initialized `BlockArrays` | ||
|
||
A block array can be created with initialized blocks using the `BlockArray{T}(block_sizes)` | ||
function. The block_sizes are each an `AbstractVector{Int}` which determines the size of the blocks in that dimension. We here create a `[1,2]×[3,2]` block matrix of `Float32`s: | ||
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. Perhaps we should mention that the arrays themselves are initialized, the values in them are not? What do you think? 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. Is this behaviour going to change in 0.7 so that 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. I don't think it will, but not sure. 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. I'm a bit confused about the difference between I'm worried that there's a confusion between uninitialized blocks and uninitialized entries with using It might be better to add a
|
||
```julia | ||
julia> BlockArray{Float32}([1,2], [3,2]) | ||
2×2-blocked 3×5 BlockArrays.BlockArray{Float32,2,Array{Float32,2}}: | ||
9.39116f-26 1.4013f-45 3.34245f-21 │ 9.39064f-26 1.4013f-45 | ||
───────────────────────────────────────┼────────────────────────── | ||
3.28434f-21 9.37645f-26 3.28436f-21 │ 8.05301f-24 9.39077f-26 | ||
1.4013f-45 1.4013f-45 1.4013f-45 │ 1.4013f-45 1.4013f-45 | ||
``` | ||
We can also any other user defined array type that supports `similar`. | ||
|
||
## Creating uninitialized `BlockArrays`. | ||
|
||
A `BlockArray` can be created with the blocks left uninitialized using the `BlockArray(block_type, block_sizes...)` function. | ||
The `block_type` should be an array type, it could for example be `Matrix{Float64}`. The block sizes are each a `Vector{Int}` which determines the size of the blocks in that dimension. We here create a `[1,2]×[3,2]` block matrix of `Float32`s: | ||
A `BlockArray` can be created with the blocks left uninitialized using the `BlockArray(uninitialized, block_type, block_sizes...)` function. | ||
The `block_type` should be an array type, it could for example be `Matrix{Float64}`. The block sizes are each an `AbstractVector{Int}` which determines the size of the blocks in that dimension. We here create a `[1,2]×[3,2]` block matrix of `Float32`s: | ||
|
||
```jldoctest | ||
julia> BlockArray(Matrix{Float32}, [1,2], [3,2]) | ||
julia> BlockArray(uninitialized, Matrix{Float32}, [1,2], [3,2]) | ||
2×2-blocked 3×5 BlockArrays.BlockArray{Float32,2,Array{Float32,2}}: | ||
#undef #undef #undef │ #undef #undef | ||
------------------------┼---------------- | ||
|
@@ -23,13 +38,13 @@ julia> BlockArray(Matrix{Float32}, [1,2], [3,2]) | |
We can also use a `SparseVector` or any other user defined array type: | ||
|
||
```jl | ||
julia> BlockArray(SparseVector{Float64, Int}, [1,2]) | ||
julia> BlockArray(uninitialized, SparseVector{Float64, Int}, [1,2]) | ||
2-blocked 3-element BlockArrays.BlockArray{Float64,1,SparseVector{Float64,Int64}}: | ||
#undef | ||
------ | ||
#undef | ||
#undef | ||
julia> BlockArray(SparseVector{Float64, Int}, [1,2]) | ||
julia> BlockArray(uninitialized, SparseVector{Float64, Int}, [1,2]) | ||
``` | ||
|
||
Note that accessing an undefined block will throw an "access to undefined reference"-error. | ||
|
@@ -41,7 +56,7 @@ An alternative syntax for this is `block_array[Block(i...)] = v` or | |
`block_array[Block.(i)...]`. | ||
|
||
```jldoctest | ||
julia> block_array = BlockArray(Matrix{Float64}, [1,2], [2,2]) | ||
julia> block_array = BlockArrays._BlockArray(Matrix{Float64}, [1,2], [2,2]) | ||
2×2-blocked 3×4 BlockArrays.BlockArray{Float64,2,Array{Float64,2}}: | ||
#undef #undef │ #undef #undef | ||
----------------┼---------------- | ||
|
@@ -109,7 +124,7 @@ julia> view(A, Block(2)) .= [3,4]; A[Block(2)] | |
|
||
## Converting between `BlockArray` and normal arrays | ||
|
||
An array can be repacked into a `BlockArray` with`BlockArray(array, block_sizes...)`: | ||
An array can be repacked into a `BlockArray` with `BlockArray(array, block_sizes...)`: | ||
|
||
```jl | ||
julia> block_array_sparse = BlockArray(sprand(4, 5, 0.7), [1,3], [2,3]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@deprecate BlockArray(blocks::Array{R, N}, block_sizes::BlockSizes{N}) where {T, N, R <: AbstractArray{T, N}} BlockArrays._BlockArray(blocks, block_sizes) | ||
@deprecate BlockArray(blocks::Array{R, N}, block_sizes::Vararg{AbstractVector{Int}, N}) where {T, N, R <: AbstractArray{T, N}} BlockArrays._BlockArray(blocks, block_sizes...) | ||
@deprecate BlockArray(::Type{R}, block_sizes::BlockSizes{N}) where {T, N, R <: AbstractArray{T, N}} BlockArrays._BlockArray(R, block_sizes) | ||
@deprecate BlockArray(::Type{R}, block_sizes::Vararg{AbstractVector{Int}, N}) where {T, N, R <: AbstractArray{T, N}} BlockArrays._BlockArray(R, block_sizes...) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,32 @@ PseudoBlockArray(blocks::R, block_sizes::Vararg{AbstractVector{Int}, N}) where { | |
PseudoBlockArray(blocks, Vector{Int}.(block_sizes)...) | ||
|
||
|
||
|
||
@inline function PseudoBlockArray{T}(block_sizes::BlockSizes{N}) where {T, N} | ||
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. Presumably all these new 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. Yes I think you are right, even if the package supports 0.6. My only hesitation is that sometimes changes in Julia master are backtracked on, but in this case that’s unlikely to happen. |
||
PseudoBlockArray(similar(Array{T, N}, size(block_sizes)), block_sizes) | ||
end | ||
|
||
@inline function PseudoBlockArray{T, N}(block_sizes::BlockSizes{N}) where {T, N} | ||
PseudoBlockArray{T}(block_sizes) | ||
end | ||
|
||
@inline function PseudoBlockArray{T, N, R}(block_sizes::BlockSizes{N}) where {T, N, R <: AbstractArray{T, N}} | ||
PseudoBlockArray(similar(R, size(block_sizes)), block_sizes) | ||
end | ||
|
||
@inline function PseudoBlockArray{T}(block_sizes::Vararg{AbstractVector{Int}, N}) where {T, N} | ||
PseudoBlockArray{T}(BlockSizes(block_sizes...)) | ||
end | ||
|
||
@inline function PseudoBlockArray{T, N}(block_sizes::Vararg{AbstractVector{Int}, N}) where {T, N} | ||
PseudoBlockArray{T, N}(BlockSizes(block_sizes...)) | ||
end | ||
|
||
@inline function PseudoBlockArray{T, N, R}(block_sizes::Vararg{AbstractVector{Int}, N}) where {T, N, R <: AbstractArray{T, N}} | ||
PseudoBlockArray{T, N, R}(BlockSizes(block_sizes...)) | ||
end | ||
|
||
|
||
########################### | ||
# AbstractArray Interface # | ||
########################### | ||
|
@@ -68,11 +94,8 @@ function Base.similar(block_array::PseudoBlockArray{T,N}, ::Type{T2}) where {T,N | |
PseudoBlockArray(similar(block_array.blocks, T2), copy(block_array.block_sizes)) | ||
end | ||
|
||
@generated function Base.size(arr::PseudoBlockArray{T,N}) where {T,N} | ||
exp = Expr(:tuple, [:(arr.block_sizes[$i][end] - 1) for i in 1:N]...) | ||
return quote | ||
@inbounds return $exp | ||
end | ||
@inline function Base.size(arr::PseudoBlockArray{T,N}) where {T,N} | ||
size(arr.block_sizes) | ||
end | ||
|
||
@inline function Base.getindex(block_arr::PseudoBlockArray{T, N}, i::Vararg{Int, N}) where {T,N} | ||
|
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.
Compat 0.39
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.
It doesn't depend on anything in Compat 0.39 that's not in Compat 0.38, so I think this should be left.
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.
Compat 0.38 is broken. Please update to 0.39.
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.
I wrote this and then I merged 20 seconds later. Oh well, will update on master.