Skip to content

Commit aaa2c1d

Browse files
phipsgablerdlfivefifty
authored andcommitted
Add more examples with different block structures + some minor improvements (#81)
* Add more examples with different block structures, and improve some minor things. Fixes #80 and #79. * Try to fix reference warning * Apply feedback, add clarification about block sizes
1 parent 65d3ebc commit aaa2c1d

File tree

2 files changed

+81
-19
lines changed

2 files changed

+81
-19
lines changed

docs/src/index.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,37 @@
44

55
[![Build Status](https://travis-ci.org/JuliaArrays/BlockArrays.jl.svg?branch=master)](https://travis-ci.org/JuliaArrays/BlockArrays.jl) [![codecov](https://codecov.io/gh/JuliaArrays/BlockArrays.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/JuliaArrays/BlockArrays.jl)
66

7-
A block array is a partition of an array into blocks or subarrays, see [wikipedia](https://en.wikipedia.org/wiki/Block_matrix) for a more extensive description. This package has two purposes. Firstly, it defines an interface for an `AbstractBlockArray` block arrays that can be shared among types representing different types of block arrays. The advantage to this is that it provides a consistent API for block arrays.
7+
A block array is a partition of an array into multiple blocks or subarrays, see [wikipedia](https://en.wikipedia.org/wiki/Block_matrix) for a more extensive description. This package has two purposes. Firstly, it defines an interface for an `AbstractBlockArray` block arrays that can be shared among types representing different types of block arrays. The advantage to this is that it provides a consistent API for block arrays.
8+
9+
Secondly, it also implements two concrete types of block arrays that follow the `AbstractBlockArray` interface. The type `BlockArray` stores each single block contiguously, by wrapping an `AbstractArray{<:AbstractArray{T,N},N}` to concatenate all blocks – the complete array is thus not stored contiguously. Conversely, a `PseudoBlockArray` stores the full matrix contiguously (by wrapping only one `AbstractArray{T, N}`) and only superimposes a block structure. This means that `BlockArray` supports fast non copying extraction and insertion of blocks, while `PseudoBlockArray` supports fast access to the full matrix to use in, for example, a linear solver.
10+
11+
12+
## Terminology
13+
14+
We talk about an “a×b-blocked m×n block array”, if we have ``m \times n`` values arranged in ``a \times b`` blocks, like in the following example:
15+
16+
```julia
17+
2×3-blocked 4×4 BlockArray{Float64,2}:
18+
0.566090.954290.0688403 0.980771
19+
0.2038290.1386670.0200418 0.0515364
20+
──────────┼────────────┼──────────────────────
21+
0.9638320.3911760.925799 0.148993
22+
0.186930.8385290.801236 0.793251
23+
```
24+
25+
The dimension of arrays works the same as with standard Julia arrays; for example the following is a ``2 \times 2`` block vector:
26+
27+
```julia
28+
2-blocked 4-element BlockArray{Float64,1}:
29+
0.35609231970760424
30+
0.7732179994849591
31+
───────────────────
32+
0.8455294223894625
33+
0.04250653797187476
34+
```
35+
36+
A block array layout is specified its _block sizes_ – a tuple of `AbstractArray{Int}`. The length of the tuple is equal to the dimension, the length of each block size array is the number of blocks in the corresponding dimension, and the sum of each block size is the scalar size in that dimension. For example, `BlockArray{Int}(undef, [2,2,2], [2,2,2], [2,2,2])` will produce a blocked cube (an `AbstractArray{Int, 3}`, i.e., 3 dimensions), consisting of 27 2×2×2 blocks (3 in each dimension) and 216 values (6 in each dimension).
837

9-
Secondly, it also implements two different type of block arrays that follow the `AbstractBlockArray` interface. The type `BlockArray` stores each block contiguously while the type `PseudoBlockArray` stores the full matrix contiguously. This means that `BlockArray` supports fast non copying extraction and insertion of blocks while `PseudoBlockArray` supports fast access to the full matrix to use in in for example a linear solver.
1038

1139
## Manual Outline
1240

docs/src/man/blockarrays.md

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,51 @@ DocTestSetup = quote
88
end
99
```
1010

11+
## Creating `BlockArray`s from an array
1112

12-
## Creating uninitialized `BlockArrays`
13+
An `AbstractArray` can be repacked into a `BlockArray` with `BlockArray(array, block_sizes...)`. The block sizes are each an `AbstractVector{Int}` which determines the size of the blocks in that dimension (so the sum of `block_sizes` in every dimension must match the size of `array` in that dimension).
1314

14-
A block array can be created with initialized blocks using the `BlockArray{T}(block_sizes)`
15-
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:
1615
```julia
17-
julia> BlockArray{Float32}(undef, [1,2], [3,2])
18-
2×2-blocked 3×5 BlockArray{Float32,2}:
19-
9.39116f-26 1.4013f-45 3.34245f-219.39064f-26 1.4013f-45
20-
───────────────────────────────────────┼──────────────────────────
21-
3.28434f-21 9.37645f-26 3.28436f-218.05301f-24 9.39077f-26
22-
1.4013f-45 1.4013f-45 1.4013f-451.4013f-45 1.4013f-45
16+
julia> BlockArray(rand(4, 4), [2,2], [1,1,2])
17+
2×3-blocked 4×4 BlockArray{Float64,2}:
18+
0.703930.5687030.0137366 0.953038
19+
0.249570.1459240.884324 0.134155
20+
──────────┼────────────┼─────────────────────
21+
0.4081330.7077230.467458 0.326718
22+
0.8443140.7942790.0421491 0.683791
23+
24+
julia> block_array_sparse = BlockArray(sprand(4, 5, 0.7), [1,3], [2,3])
25+
2×2-blocked 4×5 BlockArray{Float64,2,Array{SparseMatrixCSC{Float64,Int64},2},BlockArrays.BlockSizes{2,Array{Int64,1}}}:
26+
0.0341601 0.3741870.0118196 0.299058 0.0
27+
----------------------------------------------------
28+
0.0945445 0.9311150.0460428 0.0 0.0
29+
0.314926 0.4389390.496169 0.0 0.0
30+
0.12781 0.2468620.732 0.449182 0.875096
31+
```
32+
33+
34+
## Creating uninitialized `BlockArray`s
35+
36+
A block array can be created with uninitialized values (but initialized blocks) using the
37+
`BlockArray{T}(undef, 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 block matrix of `Float32`s:
38+
39+
```julia
40+
julia> BlockArray{Float32}(undef, [1,2,1], [1,1,1])
41+
3×3-blocked 4×3 BlockArray{Float32,2}:
42+
-2.15145e-351.4013e-45-1.77199e-35
43+
──────────────┼────────────────┼──────────────
44+
1.4013e-45-1.77199e-35-1.72473e-34
45+
1.4013e-454.57202e-414.57202e-41
46+
──────────────┼────────────────┼──────────────
47+
0.0-1.36568e-33-1.72473e-34
2348
```
49+
2450
We can also any other user defined array type that supports `similar`.
2551

52+
2653
## Creating `BlockArrays` with uninitialized blocks.
2754

28-
A `BlockArray` can be created with the blocks left uninitialized using the `BlockArray(undef, block_type, block_sizes...)` function.
29-
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:
55+
A `BlockArray` can be created with the blocks left uninitialized using the `BlockArray(undef_blocks[, block_type], block_sizes...)` function. We here create a `[1,2]×[3,2]` block matrix of `Float32`s:
3056

3157
```jldoctest
3258
julia> BlockArray{Float32}(undef_blocks, [1,2], [3,2])
@@ -37,10 +63,9 @@ julia> BlockArray{Float32}(undef_blocks, [1,2], [3,2])
3763
#undef #undef #undef │ #undef #undef
3864
```
3965

40-
We can also use a `SparseVector` or any other user defined array type by
41-
specifying it as the second argument:
66+
The `block_type` should be an array type. It specifies the internal block type, which defaults to an `Array` of the according dimension. We can also use a `SparseVector` or any other user defined array type:
4267

43-
```jl
68+
```julia
4469
julia> BlockArray(undef_blocks, SparseVector{Float64, Int}, [1,2])
4570
2-blocked 3-element BlockArray{Float64,1,Array{SparseVector{Float64,Int64},1},BlockArrays.BlockSizes{1,Array{Int64,1}}}:
4671
#undef
@@ -49,9 +74,18 @@ julia> BlockArray(undef_blocks, SparseVector{Float64, Int}, [1,2])
4974
#undef
5075
```
5176

52-
Note that accessing an undefined block will throw an "access to undefined reference"-error.
53-
54-
## Setting and getting blocks and values
77+
!!! warning
78+
79+
Note that accessing an undefined block will throw an "access to undefined reference"-error! If you create an array with undefined blocks, you _have_ to [initialize it block-wise](@ref setting_and_getting)); whole-array functions like `fill!` will not work:
80+
81+
```julia
82+
julia> fill!(BlockArray{Float32}(undef_blocks, [1,2], [3,2]), 0)
83+
ERROR: UndefRefError: access to undefined reference
84+
85+
```
86+
87+
88+
## [Setting and getting blocks and values](@id setting_and_getting)
5589

5690
A block can be set by `setblock!(block_array, v, i...)` where `v` is the array to set and `i` is the block index.
5791
An alternative syntax for this is `block_array[Block(i...)] = v` or

0 commit comments

Comments
 (0)