Skip to content

Commit e359943

Browse files
authored
Test docstrings (#239)
* Test docstrings * simplify examples
1 parent e4e8204 commit e359943

File tree

7 files changed

+62
-67
lines changed

7 files changed

+62
-67
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1010
[compat]
1111
Aqua = "0.5, 0.6"
1212
ArrayLayouts = "1"
13+
Documenter = "0.27"
1314
FillArrays = "1"
1415
julia = "1.6"
1516

1617
[extras]
1718
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
1819
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
20+
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
1921
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
2022
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2123
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2224
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
2325
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2426

2527
[targets]
26-
test = ["Aqua", "Base64", "OffsetArrays", "SparseArrays", "StaticArrays", "Test", "Random"]
28+
test = ["Aqua", "Base64", "Documenter", "OffsetArrays", "SparseArrays", "StaticArrays", "Test", "Random"]

docs/src/man/blockarrays.md

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ julia> BlockArray(rand(4, 4), [2,2], [1,1,2])
2323

2424
julia> block_array_sparse = BlockArray(sprand(4, 5, 0.7), [1,3], [2,3])
2525
2×2-blocked 4×5 BlockMatrix{Float64, Matrix{SparseMatrixCSC{Float64, Int64}}, Tuple{BlockedUnitRange{Vector{Int64}}, BlockedUnitRange{Vector{Int64}}}}:
26-
0.0341601 0.3741870.0118196 0.299058 0.0
26+
0.0341601 0.3741870.0118196 0.299058 0.0
2727
----------------------------------------------------
28-
0.0945445 0.9311150.0460428 0.0 0.0
29-
0.314926 0.4389390.496169 0.0 0.0
28+
0.0945445 0.9311150.0460428 0.0 0.0
29+
0.314926 0.4389390.496169 0.0 0.0
3030
0.12781 0.2468620.732 0.449182 0.875096
3131
```
3232

@@ -77,17 +77,16 @@ julia> BlockArray(undef_blocks, SparseVector{Float64, Int}, [1,2])
7777
!!! warning
7878

7979
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-
80+
8181
```julia
8282
julia> fill!(BlockArray{Float32}(undef_blocks, [1,2], [3,2]), 0)
8383
ERROR: UndefRefError: access to undefined reference
8484
8585
```
86-
87-
86+
8887
## [Setting and getting blocks and values](@id setting_and_getting)
8988

90-
A block can be set by `block_array[Block(i...)] = v` or
89+
A block can be set by `block_array[Block(i...)] = v`. The indexing may equivalently be carried out as
9190
`block_array[Block.(i)...]`.
9291

9392
```jldoctest block_array
@@ -98,24 +97,21 @@ julia> block_array = BlockArray{Float64}(undef_blocks, [1,2], [2,2])
9897
#undef #undef │ #undef #undef
9998
#undef #undef │ #undef #undef
10099
101-
julia> block_array[Block(2,1)] = rand(2,2)
102-
2×2 Matrix{Float64}:
103-
0.325977 0.218587
104-
0.549051 0.894245
100+
julia> block_array[Block(2,1)] = reshape([1:4;], 2, 2);
105101
106102
julia> block_array[Block(1),Block(1)] = [1 2];
107103
108104
julia> block_array
109105
2×2-blocked 3×4 BlockMatrix{Float64}:
110-
1.0 2.0 │ #undef #undef
111-
────────────────────┼────────────────
112-
0.325977 0.218587 │ #undef #undef
113-
0.549051 0.894245 │ #undef #undef
106+
1.0 2.0 │ #undef #undef
107+
──────────┼────────────────
108+
1.0 3.0 │ #undef #undef
109+
2.0 4.0 │ #undef #undef
114110
```
115111

116112
Note that this will "take ownership" of the passed in array, that is, no copy is made.
117113

118-
A block can be retrieved with `view(block_array, Block(i...))`,
114+
A block can be retrieved with `view(block_array, Block(i...))`,
119115
or if a copy is desired, `block_array[Block(i...)]`:
120116

121117
```jldoctest block_array
@@ -171,10 +167,10 @@ An array can be repacked into a `BlockArray` with `BlockArray(array, block_sizes
171167
```jl
172168
julia> block_array_sparse = BlockArray(sprand(4, 5, 0.7), [1,3], [2,3])
173169
2×2-blocked 4×5 BlockArray{Float64, 2, Matrix{SparseMatrixCSC{Float64, Int64}}, Tuple{BlockedUnitRange{Vector{Int64}}, BlockedUnitRange{Vector{Int64}}}}:
174-
0.0341601 0.3741870.0118196 0.299058 0.0
170+
0.0341601 0.3741870.0118196 0.299058 0.0
175171
----------------------------------------------------
176-
0.0945445 0.9311150.0460428 0.0 0.0
177-
0.314926 0.4389390.496169 0.0 0.0
172+
0.0945445 0.9311150.0460428 0.0 0.0
173+
0.314926 0.4389390.496169 0.0 0.0
178174
0.12781 0.2468620.732 0.449182 0.875096
179175
```
180176

docs/src/man/pseudoblockarrays.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ end
1010

1111
A `PseudoBlockArray` is similar to a [`BlockArray`](@ref) except the full array is stored
1212
contiguously instead of block by block. This means that is not possible to insert and retrieve
13-
blocks without copying data. On the other hand, converting a ``PseudoBlockArray` to the "full" underlying array is instead instant since
13+
blocks without copying data. On the other hand, converting a `PseudoBlockArray` to the "full" underlying array is instead instant since
1414
it can just return the wrapped array.
1515

1616
When iteratively solving a set of equations with a gradient method the Jacobian typically has a block structure. It can be convenient
@@ -22,12 +22,12 @@ a direct solver using `Matrix`.
2222
Creating a `PseudoBlockArray` works in the same way as a `BlockArray`.
2323

2424
```jldoctest A
25-
julia> pseudo = PseudoBlockArray(rand(3,3), [1,2], [2,1])
26-
2×2-blocked 3×3 PseudoBlockMatrix{Float64}:
27-
0.579862 0.01490880.839622
28-
─────────────────────┼──────────
29-
0.411294 0.520355 0.967143
30-
0.972136 0.639562 0.131026
25+
julia> pseudo = PseudoBlockArray(reshape([1:9;], 3, 3), [1,2], [2,1])
26+
2×2-blocked 3×3 PseudoBlockMatrix{Int64}:
27+
1 47
28+
─────────
29+
2 5 8
30+
3 6 9
3131
```
3232

3333
This "takes ownership" of the passed in array so no copy of the array is made.
@@ -42,8 +42,8 @@ julia> PseudoBlockArray{Float32}(undef, [1,2], [3,2])
4242
2×2-blocked 3×5 PseudoBlockMatrix{Float32}:
4343
1.02295e-43 0.0 1.09301e-430.0 1.17709e-43
4444
───────────────────────────────────────┼──────────────────────────
45-
0.0 1.06499e-43 0.01.14906e-43 0.0
46-
1.05097e-43 0.0 1.13505e-430.0 1.1911e-43
45+
0.0 1.06499e-43 0.01.14906e-43 0.0
46+
1.05097e-43 0.0 1.13505e-430.0 1.1911e-43
4747
```
4848
We can also any other user defined array type that supports `similar`.
4949

@@ -63,15 +63,15 @@ julia> copyto!(A, view(pseudo, Block(2, 1)));
6363
6464
julia> A
6565
2×2 Matrix{Float64}:
66-
0.411294 0.520355
67-
0.972136 0.639562
66+
2.0 5.0
67+
3.0 6.0
6868
```
6969

7070
It is sometimes convenient to access an index in a certain block. We could of course write this as `A[Block(I,J)][i,j]` but the problem is that `A[Block(I,J)]` allocates its output so this type of indexing will be inefficient. Instead, it is possible to use the `A[BlockIndex((I,J), (i,j))]` indexing. Using the same block matrix `A` as above:
7171

7272
```jldoctest A
7373
julia> pseudo[BlockIndex((2,1), (2,2))]
74-
0.6395615996802734
74+
6
7575
```
7676

7777
The underlying array is accessed with `Array` just like for `BlockArray`.

src/abstractblockarray.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,6 @@ julia> A = BlockArray(v, [1,1], [2,1])
155155
──────┼───
156156
2 4 │ 6
157157
158-
julia> Matrix.(collect(eachblock(A)))
159-
2×2 Matrix{Matrix{Int64}}:
160-
[1 3] [5;;]
161-
[2 4] [6;;]
162-
163158
julia> sum.(eachblock(A))
164159
2×2 Matrix{Int64}:
165160
4 5

src/blockarray.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ array-constructor-caller would like an uninitialized block array. See also
1414
undef_blocks (@ref), an alias for UndefBlocksInitializer().
1515
1616
# Examples
17-
```julia
17+
```jldoctest; setup=:(using BlockArrays)
1818
julia> BlockArray(undef_blocks, Matrix{Float32}, [1,2], [3,2])
1919
2×2-blocked 3×5 BlockMatrix{Float32}:
2020
#undef #undef #undef │ #undef #undef
@@ -33,11 +33,11 @@ type UndefBlocksInitializer (@ref), used in block array initialization to indica
3333
array-constructor-caller would like an uninitialized block array.
3434
3535
# Examples
36-
```julia
36+
```jldoctest; setup=:(using BlockArrays)
3737
julia> BlockArray(undef_blocks, Matrix{Float32}, [1,2], [3,2])
3838
2×2-blocked 3×5 BlockMatrix{Float32}:
3939
#undef #undef #undef │ #undef #undef
40-
------------------------┼----------------
40+
────────────────────────┼────────────────
4141
#undef #undef #undef │ #undef #undef
4242
#undef #undef #undef │ #undef #undef
4343
```
@@ -220,23 +220,23 @@ This is an "inverse" of [`blocks`](@ref).
220220
# Examples
221221
```jldoctest; setup = quote using BlockArrays end
222222
julia> arrays = permutedims(reshape([
223-
1ones(1, 3), 2ones(1, 2),
224-
3ones(2, 3), 4ones(2, 2),
223+
fill(1.0, 1, 3), fill(2.0, 1, 2),
224+
fill(3.0, 2, 3), fill(4.0, 2, 2),
225225
], (2, 2)))
226226
2×2 Matrix{Matrix{Float64}}:
227227
[1.0 1.0 1.0] [2.0 2.0]
228228
[3.0 3.0 3.0; 3.0 3.0 3.0] [4.0 4.0; 4.0 4.0]
229229
230-
julia> mortar(arrays)
230+
julia> M = mortar(arrays)
231231
2×2-blocked 3×5 BlockMatrix{Float64}:
232232
1.0 1.0 1.0 │ 2.0 2.0
233233
───────────────┼──────────
234234
3.0 3.0 3.0 │ 4.0 4.0
235235
3.0 3.0 3.0 │ 4.0 4.0
236236
237-
julia> ans == mortar(
238-
(1ones(1, 3), 2ones(1, 2)),
239-
(3ones(2, 3), 4ones(2, 2)),
237+
julia> M == mortar(
238+
(fill(1.0, 1, 3), fill(2.0, 1, 2)),
239+
(fill(3.0, 2, 3), fill(4.0, 2, 2)),
240240
)
241241
true
242242
```

src/pseudo_blockarray.jl

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,24 @@ When iteratively solving a set of equations with a gradient method the Jacobian
1717
to use a `PseudoBlockArray` to build up the Jacobian block by block and then pass the resulting matrix to
1818
a direct solver using `Array`.
1919
20-
```jldoctest
21-
julia> using BlockArrays, Random, SparseArrays
22-
23-
julia> Random.seed!(12345);
24-
25-
julia> A = PseudoBlockArray(rand(2,3), [1,1], [2,1])
26-
2×2-blocked 2×3 PseudoBlockMatrix{Float64}:
27-
0.944791 0.339612 │ 0.322501
28-
────────────────────┼──────────
29-
0.866895 0.136117 │ 0.252549
30-
31-
julia> A = PseudoBlockArray(sprand(6, 0.5), [3,2,1])
32-
3-blocked 6-element PseudoBlockVector{Float64, SparseVector{Float64, Int64}, Tuple{BlockedUnitRange{Vector{Int64}}}}:
33-
0.0
34-
0.0
35-
0.26755021483368013
36-
───────────────────
37-
0.0
38-
0.11848853125656122
39-
───────────────────
40-
0.0
20+
# Examples
21+
```jldoctest; setup=:(using BlockArrays)
22+
julia> PseudoBlockArray(reshape([1:6;], 2, 3), [1,1], [2,1])
23+
2×2-blocked 2×3 PseudoBlockMatrix{Int64}:
24+
1 3 │ 5
25+
──────┼───
26+
2 4 │ 6
27+
28+
julia> PseudoBlockArray([1:6;], [3,2,1])
29+
3-blocked 6-element PseudoBlockVector{Int64}:
30+
1
31+
2
32+
3
33+
34+
4
35+
5
36+
37+
6
4138
```
4239
"""
4340
struct PseudoBlockArray{T, N, R<:AbstractArray{T,N}, BS<:NTuple{N,AbstractUnitRange{Int}}} <: AbstractBlockArray{T, N}

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ using Aqua
55
Aqua.test_all(BlockArrays, ambiguities=false)
66
end
77

8+
using Documenter
9+
@testset "docstrings" begin
10+
doctest(BlockArrays)
11+
end
12+
813
include("test_blockindices.jl")
914
include("test_blockarrays.jl")
1015
include("test_blockviews.jl")

0 commit comments

Comments
 (0)