Skip to content

Commit 1ac6aef

Browse files
authored
Add constructors to BlockArray docstring (#360)
* Add constructors to BlockArray docstring * Fix assigned value in docstring
1 parent d406642 commit 1ac6aef

File tree

2 files changed

+92
-5
lines changed

2 files changed

+92
-5
lines changed

docs/src/lib/public.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ blockcheckbounds
4646

4747
```@docs
4848
BlockArray
49+
BlockArray(::UndefBlocksInitializer, ::Type{R}, block_sizes::Vararg{AbstractVector{<:Integer}, N}) where {T, N, R<:AbstractArray{T,N}}
50+
BlockArray{T}(::UndefBlocksInitializer, block_sizes::Vararg{AbstractVector{<:Integer}, N}) where {T, N}
51+
BlockArray{T}(::UndefInitializer, block_sizes::Vararg{AbstractVector{<:Integer}, N}) where {T, N}
4952
undef_blocks
5053
UndefBlocksInitializer
5154
mortar

src/blockarray.jl

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
1212
Singleton type used in block array initialization, indicating the
1313
array-constructor-caller would like an uninitialized block array. See also
14-
undef_blocks (@ref), an alias for UndefBlocksInitializer().
14+
[`undef_blocks`](@ref), an alias for `UndefBlocksInitializer()`.
1515
1616
# Examples
1717
```jldoctest
@@ -28,8 +28,8 @@ struct UndefBlocksInitializer end
2828
"""
2929
undef_blocks
3030
31-
Alias for UndefBlocksInitializer(), which constructs an instance of the singleton
32-
type UndefBlocksInitializer (@ref), used in block array initialization to indicate the
31+
Alias for `UndefBlocksInitializer()`, which constructs an instance of the singleton
32+
type [`UndefBlocksInitializer`](@ref), used in block array initialization to indicate the
3333
array-constructor-caller would like an uninitialized block array.
3434
3535
# Examples
@@ -97,21 +97,83 @@ end
9797
_BlockArray(R, block_sizes...)
9898

9999
"""
100-
Constructs a `BlockArray` with uninitialized blocks from a block type `R` with sizes defined by `block_sizes`.
100+
BlockArray(::UndefBlocksInitializer, ::Type{R}, block_sizes::Vararg{AbstractVector{<:Integer}, N}) where {N,R<:AbstractArray{<:Any,N}}
101101
102+
Construct a `N`-dim `BlockArray` with uninitialized blocks from a block type `R`, with sizes defined by `block_sizes`.
103+
Each block **must** be allocated before being accessed.
104+
105+
# Examples
102106
```jldoctest
103-
julia> BlockArray(undef_blocks, Matrix{Float64}, [1,3], [2,2])
107+
julia> B = BlockArray(undef_blocks, Matrix{Float64}, [1,3], [2,2])
104108
2×2-blocked 4×4 BlockMatrix{Float64}:
105109
#undef #undef │ #undef #undef
106110
────────────────┼────────────────
107111
#undef #undef │ #undef #undef
108112
#undef #undef │ #undef #undef
109113
#undef #undef │ #undef #undef
114+
115+
julia> typeof(blocks(B))
116+
Matrix{Matrix{Float64}} (alias for Array{Array{Float64, 2}, 2})
117+
118+
julia> using SparseArrays
119+
120+
julia> B = BlockArray(undef_blocks, SparseMatrixCSC{Float64,Int}, [1,3], [2,2]);
121+
122+
julia> typeof(blocks(B))
123+
Matrix{SparseMatrixCSC{Float64, Int64}} (alias for Array{SparseMatrixCSC{Float64, Int64}, 2})
110124
```
125+
126+
See also [`undef_blocks`](@ref), [`UndefBlocksInitializer`](@ref)
111127
"""
112128
@inline BlockArray(::UndefBlocksInitializer, ::Type{R}, block_sizes::Vararg{AbstractVector{<:Integer}, N}) where {T, N, R<:AbstractArray{T,N}} =
113129
undef_blocks_BlockArray(Array{R,N}, block_sizes...)
114130

131+
"""
132+
BlockArray{T}(::UndefBlocksInitializer, block_sizes::Vararg{AbstractVector{<:Integer}, N}) where {T,N}
133+
134+
Construct a `N`-dim `BlockArray` with uninitialized blocks of type `Array{T,N}`, with sizes defined by `block_sizes`.
135+
Each block **must** be allocated before being accessed.
136+
137+
# Examples
138+
```jldoctest
139+
julia> B = BlockArray{Float64}(undef_blocks, [1,2], [1,2])
140+
2×2-blocked 3×3 BlockMatrix{Float64}:
141+
#undef │ #undef #undef
142+
────────┼────────────────
143+
#undef │ #undef #undef
144+
#undef │ #undef #undef
145+
146+
julia> typeof(blocks(B))
147+
Matrix{Matrix{Float64}} (alias for Array{Array{Float64, 2}, 2})
148+
149+
julia> B = BlockArray{Int8}(undef_blocks, [1,2])
150+
2-blocked 3-element BlockVector{Int8}:
151+
#undef
152+
──────
153+
#undef
154+
#undef
155+
156+
julia> typeof(blocks(B))
157+
Vector{Vector{Int8}} (alias for Array{Array{Int8, 1}, 1})
158+
159+
julia> B[Block(1)] .= 2 # errors, as the block is not allocated yet
160+
ERROR: UndefRefError: access to undefined reference
161+
[...]
162+
163+
julia> B[Block(1)] = [1]; # assign an array to the block
164+
165+
julia> B[Block(2)] = [2,3];
166+
167+
julia> B
168+
2-blocked 3-element BlockVector{Int8}:
169+
1
170+
171+
2
172+
3
173+
```
174+
175+
See also [`undef_blocks`](@ref), [`UndefBlocksInitializer`](@ref)
176+
"""
115177
@inline BlockArray{T}(::UndefBlocksInitializer, block_sizes::Vararg{AbstractVector{<:Integer}, N}) where {T, N} =
116178
BlockArray(undef_blocks, Array{T,N}, block_sizes...)
117179

@@ -149,6 +211,28 @@ initialized_blocks_BlockArray(::Type{R}, block_sizes::Vararg{AbstractVector{<:In
149211
@inline BlockArray{T,N,R,BS}(::UndefInitializer, baxes::BS) where {T, N, R<:AbstractArray{<:AbstractArray{T,N},N}, BS<:NTuple{N,AbstractUnitRange{Int}}} =
150212
initialized_blocks_BlockArray(R, baxes)
151213

214+
"""
215+
BlockArray{T}(::UndefInitializer, block_sizes::Vararg{AbstractVector{<:Integer}, N}) where {T, N}
216+
217+
Construct a `N`-dim `BlockArray` with blocks of type `Array{T,N}`, with sizes defined by `block_sizes`.
218+
The blocks are allocated using `similar`, and the elements in each block are therefore unitialized.
219+
220+
# Examples
221+
```jldoctest
222+
julia> B = BlockArray{Int8}(undef, [1,2]);
223+
224+
julia> B[Block(1)] .= 2;
225+
226+
julia> B[Block(2)] .= 3;
227+
228+
julia> B
229+
2-blocked 3-element BlockVector{Int8}:
230+
2
231+
232+
3
233+
3
234+
```
235+
"""
152236
@inline BlockArray{T}(::UndefInitializer, block_sizes::Vararg{AbstractVector{<:Integer}, N}) where {T, N} =
153237
initialized_blocks_BlockArray(Array{Array{T,N},N}, block_sizes...)
154238

0 commit comments

Comments
 (0)