|
2 | 2 |
|
3 | 3 | [](https://travis-ci.org/KristofferC/BlockArrays.jl) [](https://codecov.io/gh/KristofferC/BlockArrays.jl)
|
4 | 4 |
|
5 |
| -A block array is a partition of an array into blocks or subarrays, see [wikipedia](https://en.wikipedia.org/wiki/Block_matrix) for a good description. This package introduces the abstract type `AbstractBlockArray` for arrays that exhibit this block structure. Currently, two concrete types are implemented that have very similar API but differs in how the blocks are stored in memory. The type `BlockArray` stores each block contiguously while the type `PseudoBlockArray` stores the full matrix contiguously. Which one to use depends on your use case, `BlockArray` supports fast non copying extraction and insertion of blocks while `PseudoBlockArray` supports directly using the underlying full matrix in for example a linear solver. Both these types follow the `AbstractArray` interface and should work in arbitrary dimensions for arbitrary block types as long as the block type itself satisfies the `AbstractArray` interface. |
| 5 | +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. |
6 | 6 |
|
7 |
| -This README will first provide an overview over the `BlockArray` type and then later discuss the few differences between `BlockArrays` and `PseudoBlockArrays`. |
| 7 | +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. |
8 | 8 |
|
9 | 9 | **Note:** Currently, a quite new build of julia master is needed to use this package.
|
10 | 10 |
|
11 |
| -### Creating uninitialized `BlockArrays` |
| 11 | +## Documentation |
12 | 12 |
|
13 |
| -A `BlockArray` can be created with the blocks left uninitialized using the `BlockArray(block_type, block_sizes...)` function. |
14 |
| -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: |
| 13 | +Documentation is built automatically with help from MkDocs on Travis CI and hosted by GitHub Pages. |
15 | 14 |
|
16 |
| -```jl |
17 |
| -julia> BlockArray(Matrix{Float32}, [1,2], [3,2]) |
18 |
| -3×5 BlockArrays.BlockArray{Float32,2,Array{Float32,2}}: |
19 |
| - #undef #undef #undef │ #undef #undef |
20 |
| - ------------------------┼---------------- |
21 |
| - #undef #undef #undef │ #undef #undef |
22 |
| - #undef #undef #undef │ #undef #undef |
23 |
| -``` |
24 |
| - |
25 |
| -We can also use a `SparseVector` or any other user defined array type: |
26 |
| - |
27 |
| -```jl |
28 |
| -julia> BlockArray(SparseVector{Float64, Int}, [1,2]) |
29 |
| -3-element BlockArrays.BlockArray{Float64,1,SparseVector{Float64,Int64}}: |
30 |
| - #undef |
31 |
| - ------ |
32 |
| - #undef |
33 |
| - #undef |
34 |
| -``` |
35 |
| - |
36 |
| -Note that accessing an undefined block will throw an "access to undefined reference"-error. |
37 |
| - |
38 |
| -### Setting and getting blocks and values |
39 |
| - |
40 |
| -A block can be set by `setblock!(block_array, v, i...)` where `v` is the array to set and `i` is the block index. |
41 |
| -An alternative syntax for this is `block_array[Block(i...)] = v`. |
42 |
| - |
43 |
| -```jl |
44 |
| -julia> block_array = BlockArray(Matrix{Float64}, [1,2], [2,2]) |
45 |
| -3×4 BlockArrays.BlockArray{Float64,2,Array{Float64,2}}: |
46 |
| - #undef #undef │ #undef #undef |
47 |
| - ----------------┼---------------- |
48 |
| - #undef #undef │ #undef #undef |
49 |
| - #undef #undef │ #undef #undef |
50 |
| - |
51 |
| -julia> setblock!(block_array, rand(2,2), 2, 1) |
52 |
| -3×4 BlockArrays.BlockArray{Float64,2,Array{Float64,2}}: |
53 |
| - #undef #undef │ #undef #undef |
54 |
| - ------------------------┼---------------- |
55 |
| - 0.314407 0.298761 │ #undef #undef |
56 |
| - 0.91585 0.644499 │ #undef #undef |
57 |
| - |
58 |
| -julia> block_array[Block(1, 1)] = [1 2]; |
59 |
| - |
60 |
| -julia> block_array |
61 |
| -3×4 BlockArrays.BlockArray{Float64,2,Array{Float64,2}}: |
62 |
| - 1.0 2.0 │ #undef #undef |
63 |
| - --------------------┼---------------- |
64 |
| - 0.314407 0.298761 │ #undef #undef |
65 |
| - 0.91585 0.644499 │ #undef #undef |
66 |
| -``` |
67 |
| - |
68 |
| -Note that this will "take ownership" of the passed in array, that is, no copy is made. |
69 |
| - |
70 |
| -A block can be retrieved with `getblock(block_array, i...)` or `block_array[Block(i...)]`: |
71 |
| - |
72 |
| -```jl |
73 |
| -julia> block_array[Block(1, 1)] |
74 |
| -1×2 Array{Float64,2}: |
75 |
| - 1.0 2.0 |
76 |
| -``` |
77 |
| - |
78 |
| -Similarly to `setblock!` this does not copy the returned array. |
79 |
| - |
80 |
| -For setting and getting a single scalar element, the usual `setindex!` and `getindex` are available. |
81 |
| - |
82 |
| -```jl |
83 |
| -julia> block_array[1, 2] |
84 |
| -2.0 |
85 |
| -``` |
86 |
| - |
87 |
| -### Converting between `BlockArray` and normal arrays |
88 |
| - |
89 |
| -An array can be repacked into a `BlockArray` with`BlockArray(array, block_sizes...)`: |
90 |
| - |
91 |
| -```jl |
92 |
| -julia> block_array_sparse = BlockArray(sprand(4, 5, 0.7), [1,3], [2,3]) |
93 |
| -4×5 BlockArrays.BlockArray{Float64,2,SparseMatrixCSC{Float64,Int64}}: |
94 |
| - 0.0 0.284338 │ 0.0 0.52346 0.403969 |
95 |
| - --------------------┼-------------------------------- |
96 |
| - 0.909193 0.0 │ 0.0 0.3401 0.922003 |
97 |
| - 0.0 0.736793 │ 0.00840872 0.804832 0.441806 |
98 |
| - 0.0 0.0 │ 0.553519 0.757454 0.575238 |
99 |
| -``` |
100 |
| - |
101 |
| -To get back the full array use `full`: |
102 |
| - |
103 |
| -```jl |
104 |
| -julia> full(block_array_sparse) |
105 |
| -4×5 sparse matrix with 13 Float64 nonzero entries: |
106 |
| - [2, 1] = 0.909193 |
107 |
| - [1, 2] = 0.284338 |
108 |
| - ⋮ |
109 |
| - [3, 5] = 0.441806 |
110 |
| - [4, 5] = 0.575238 |
111 |
| -``` |
112 |
| - |
113 |
| -### Operations on `BlockArrays`. |
114 |
| - |
115 |
| -Simple unary/binary functions and reductions are available, for an overview, see the `operations.jl` file. |
116 |
| - |
117 |
| -## `PseudoBlockArrays` |
118 |
| - |
119 |
| -`PseudoBlockArrays` are similar to `BlockArrays` except the full matrix is stored contiguously. This means that it is no longer possible to set and get blocks by simply changing a reference. However, the wrapped array can be directly used in for example linear solvers. |
120 |
| - |
121 |
| -Creating a `PseudoBlockArray` works in the same way as a `BlockArray`. |
122 |
| - |
123 |
| -```julia |
124 |
| -julia> pseudo = PseudoBlockArray(rand(3,3), [1,2], [2,1]) |
125 |
| -3×3 BlockArrays.PseudoBlockArray{Float64,2,Array{Float64,2}}: |
126 |
| - 0.282059 0.560107 │ 0.540811 |
127 |
| - --------------------┼---------- |
128 |
| - 0.46358 0.11423 │ 0.520826 |
129 |
| - 0.250737 0.809022 │ 0.905993 |
130 |
| -``` |
131 |
| - |
132 |
| -This "takes ownership" of the passed in array so no copy of the array is made. |
133 |
| - |
134 |
| -Setting and getting blocks uses the same API as `BlockArrays`. The difference here is that setting a block will update the block in place and getting a block |
135 |
| -will extract a copy of the block and return it. For `PseudoBlockArrays` there is a mutating block getter called `getblock!` which updates a passed in array to avoid a copy: |
136 |
| - |
137 |
| -```julia |
138 |
| -julia> A = zeros(2,2) |
139 |
| - |
140 |
| -julia> getblock!(A, pseudo, 2, 1); |
141 |
| - |
142 |
| -julia> A |
143 |
| -2×2 Array{Float64,2}: |
144 |
| - 0.46358 0.11423 |
145 |
| - 0.250737 0.809022 |
146 |
| -``` |
147 |
| - |
148 |
| -The underlying array is accessed with `full` just like for `BlockArray`. |
149 |
| - |
150 |
| -## TODO |
151 |
| - |
152 |
| -- Linear algebra stuff |
153 |
| -- Investigate performance (for example that bounds check removal work properly) |
| 15 | +[](https://KristofferC.github.io/BlockArrays.jl/stable) [](https://KristofferC.github.io/BlockArrays.jl/latest) |
154 | 16 |
|
155 | 17 | ## Author
|
156 | 18 |
|
|
0 commit comments