Skip to content

Commit 846ac8d

Browse files
committed
docs + interface (#6)
1 parent e60c428 commit 846ac8d

19 files changed

+743
-229
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
*.jl.cov
22
*.jl.*.cov
33
*.jl.mem
4+
docs/build/
5+
docs/site/

.travis.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ notifications:
99
email: false
1010
# uncomment the following lines to override the default test script
1111
script:
12-
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
13-
- julia -e 'Pkg.clone(pwd()); Pkg.test("BlockArrays"; coverage=true)'
12+
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
13+
- julia -e 'Pkg.clone(pwd()); Pkg.test("BlockArrays"; coverage=true)'
1414

1515
after_success:
16-
- julia -e 'cd(Pkg.dir("BlockArrays")); Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())'
16+
- julia -e 'cd(Pkg.dir("BlockArrays")); Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())'
17+
- julia -e 'Pkg.clone("https://github.com/MichaelHatherly/Documenter.jl")'
18+
- julia -e 'cd(Pkg.dir("BlockArrays")); include(joinpath("docs", "make.jl"))'

README.md

Lines changed: 5 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -2,155 +2,17 @@
22

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

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.
66

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.
88

99
**Note:** Currently, a quite new build of julia master is needed to use this package.
1010

11-
### Creating uninitialized `BlockArrays`
11+
## Documentation
1212

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.
1514

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.2843380.0 0.52346 0.403969
95-
----------------------------------------------------
96-
0.909193 0.00.0 0.3401 0.922003
97-
0.0 0.7367930.00840872 0.804832 0.441806
98-
0.0 0.00.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.5601070.540811
127-
------------------------------
128-
0.46358 0.114230.520826
129-
0.250737 0.8090220.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://img.shields.io/badge/docs-stable-blue.svg)](https://KristofferC.github.io/BlockArrays.jl/stable) [![](https://img.shields.io/badge/docs-latest-blue.svg)](https://KristofferC.github.io/BlockArrays.jl/latest)
15416

15517
## Author
15618

docs/make.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Documenter, BlockArrays
2+
3+
# Build documentation.
4+
# ====================
5+
6+
makedocs(
7+
# options
8+
modules = [BlockArrays],
9+
clean = true,
10+
doctest = false,
11+
)
12+
13+
# Deploy built documentation from Travis.
14+
# =======================================
15+
16+
# Needs to install an additional dep, mkdocs-material, so provide a custom `deps`.
17+
custom_deps() = run(`pip install --user pygments mkdocs mkdocs-material`)
18+
19+
deploydocs(
20+
# options
21+
deps = custom_deps,
22+
repo = "github.com/KristofferC/BlockArrays.jl.git"
23+
)

docs/mkdocs.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
site_name: BlockArrays.jl
2+
repo_url: https://github.com/KristofferC/BlockArrays.jl
3+
site_description: Description...
4+
site_author: Kristoffer Carlsson
5+
6+
theme: readthedocs
7+
8+
extra_css:
9+
- assets/Documenter.css
10+
11+
extra_javascript:
12+
- https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML
13+
- assets/mathjaxhelper.js
14+
15+
markdown_extensions:
16+
- extra
17+
- tables
18+
- fenced_code
19+
- mdx_math
20+
21+
docs_dir: 'build'
22+
23+
pages:
24+
- Home: index.md
25+
- Manual:
26+
- AbstractBlockArray interface: man/abstractblockarrayinterface.md
27+
- BlockArrays: man/blockarrayss.md
28+
- PseudoBlockArrays: man/pseudoblockarrays.md
29+
- Library:
30+
- Public: lib/public.md
31+
- Internals: lib/internals.md

docs/src/index.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# BlockArrays.jl
2+
3+
*Block arrays in Julia*
4+
5+
[![Build Status](https://travis-ci.org/KristofferC/BlockArrays.jl.svg?branch=master)](https://travis-ci.org/KristofferC/BlockArrays.jl) [![codecov](https://codecov.io/gh/KristofferC/BlockArrays.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/KristofferC/BlockArrays.jl)
6+
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.
8+
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.
10+
11+
**Note:** Currently, a quite new build of julia master is needed to use this package.
12+
13+
14+
## Manual Outline
15+
16+
{contents}
17+
Pages = ["man/abstractblockarrayinterface.md", "man/blockarrayss.md", "man/pseudoblockarrays.md"]
18+
Depth = 2
19+
20+
## Library Outline
21+
22+
{contents}
23+
Pages = ["lib/public.md", "lib/internals.md"]
24+
Depth = 2
25+
26+
## [Index]({ref#main-index})
27+
28+
{index}
29+
Pages = ["lib/public.md", "lib/internals.md"]

docs/src/lib/internals.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{meta}
2+
CurrentModule = BlockArrays
3+
4+
# Internal Documentation
5+
6+
## Contents
7+
8+
{contents}
9+
Pages = ["internals.md"]
10+
11+
## Index
12+
13+
{index}
14+
Pages = ["internals.md"]
15+
16+
## Internals
17+
18+
{docs}
19+
BlockIndex
20+
blockindex2global
21+
global2blockindex

docs/src/lib/public.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{meta}
2+
CurrentModule = BlockArrays
3+
4+
# Public Documentation
5+
6+
Documentation for `BlockArrays.jl`'s public interface.
7+
8+
See [Internal Documentation]({ref}) for internal package docs covering all submodules.
9+
10+
11+
## Contents
12+
13+
{contents}
14+
Pages = ["public.md"]
15+
16+
## Index
17+
18+
{index}
19+
Pages = ["public.md"]
20+
21+
## AbstractBlockArray interface
22+
23+
This sections defines the functions a subtype of `AbstractBlockArray` should define to be a part of the `AbstractBlockArray` interface. An `AbstractBlockArray{T, N}` is a subtype of `AbstractArray{T,N}` and should therefore also fulfill the [`AbstractArray` interface](http://docs.julialang.org/en/latest/manual/interfaces/#abstract-arrays).
24+
25+
{docs}
26+
AbstractBlockArray
27+
BlockBoundsError
28+
Block
29+
nblocks
30+
blocksize
31+
getblock
32+
getblock!
33+
setblock!
34+
full
35+
blockcheckbounds
36+
37+
## BlockArray
38+
39+
{docs}
40+
BlockArray
41+
42+
43+
44+
## PseudoBlockArray
45+
46+
{docs}
47+
PseudoBlockArray
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# The AbstractBlockArray interface
2+
3+
4+
| Methods to implement | Brief description |
5+
| :---------------------- | :---------------- |
6+
| `nblocks(A)` | Tuple of number of blocks in each dimension |
7+
| `nblocks(A, i)` | Number of blocks in dimension `i` |
8+
| `blocksize(A, i...)` | Size of the block at block index `i...` |
9+
| `getblock(A, i...)` | `X[Block(i...)]`, blocked indexing |
10+
| `setblock!(A, v, i...)` | `X[Block(i...)] = v`, blocked index assignment |
11+
| `full(A)` | The non blocked array |
12+
| **Optional methods** | |
13+
| `getblock!(x, A, i)` | `X[i]`, blocked index assignment with in place storage in `x` |
14+
15+
16+
With the methods above implemented the following are automatically provided:
17+
18+
* A pretty printing `show` function that uses unicode lines to split up the blocks:
19+
20+
julia> BlockArray(rand(4, 5), [1,3], [2,3])
21+
2×2-blocked 4×5 BlockArrays.BlockArray{Float64,2,Array{Float64,2}}:
22+
0.28346 0.234328 │ 0.10266 0.0670817 0.941958
23+
--------------------┼-------------------------------
24+
0.881618 0.152164 │ 0.938311 0.819992 0.860623
25+
0.74367 0.16049 │ 0.704886 0.950269 0.601036
26+
0.502035 0.259069 │ 0.857453 0.197673 0.962873
27+
28+
29+
* A bounds index checking function for indexing with blocks:
30+
julia> A = BlockArray(rand(4, 5), [1,3], [2,3]);
31+
32+
julia> blockcheckbounds(A, 5, 3)
33+
ERROR: BlockBoundsError: attempt to access 2×2-blocked 4×5 BlockArrays.BlockArray{Float64,2,Array{Float64,2}} at block index [5,3]
34+
in blockcheckbounds(::BlockArrays.BlockArray{Float64,2,Array{Float64,2}}, ::Int64, ::Int64) at .julia/v0.5/BlockArrays/src/abstractblockarray.jl:190
35+
in eval(::Module, ::Any) at ./boot.jl:226
36+
37+
* Happy users who know how to use your new block array :)
38+

0 commit comments

Comments
 (0)