Skip to content

Commit ea33af1

Browse files
authored
Fix examples in manual (#341)
1 parent 2c6258d commit ea33af1

File tree

2 files changed

+50
-48
lines changed

2 files changed

+50
-48
lines changed

docs/src/man/blockarrays.md

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@
22

33
```@meta
44
DocTestSetup = quote
5-
using BlockArrays
6-
using Random
7-
Random.seed!(1234)
5+
using BlockArrays, SparseArrays
86
end
97
```
108

119
## Creating `BlockArray`s from an array
1210

1311
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).
1412

15-
```julia
16-
julia> BlockArray(rand(4, 4), [2,2], [1,1,2])
17-
2×3-blocked 4×4 BlockMatrix{Float64}:
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])
13+
```jldoctest
14+
julia> BlockArray(Array(reshape(1:16, 4, 4)), [2,2], [1,1,2])
15+
2×3-blocked 4×4 BlockMatrix{Int64}:
16+
1 │ 5 │ 9 13
17+
2 │ 6 │ 10 14
18+
───┼─────┼────────
19+
3 │ 7 │ 11 15
20+
4 │ 8 │ 12 16
21+
22+
julia> S = spzeros(4,5); S[1,2] = S[4,3] = 1;
23+
24+
julia> block_array_sparse = BlockArray(S, [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
27-
----------------------------------------------------
28-
0.0945445 0.931115 0.0460428 0.0 0.0
29-
0.314926 0.438939 0.496169 0.0 0.0
30-
0.12781 0.246862 0.732 0.449182 0.875096
26+
⋅ 1.0 ⋅ ⋅ ⋅
27+
──────────┼───────────────
28+
⋅ ⋅
29+
⋅ ⋅ ⋅ ⋅
30+
1.0
3131
```
3232

3333

@@ -65,11 +65,11 @@ julia> BlockArray{Float32}(undef_blocks, [1,2], [3,2])
6565

6666
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:
6767

68-
```julia
68+
```jldoctest
6969
julia> BlockArray(undef_blocks, SparseVector{Float64, Int}, [1,2])
7070
2-blocked 3-element BlockVector{Float64, Vector{SparseVector{Float64, Int64}}, Tuple{BlockedUnitRange{Vector{Int64}}}}:
7171
#undef
72-
------
72+
──────
7373
#undef
7474
#undef
7575
```
@@ -78,10 +78,9 @@ julia> BlockArray(undef_blocks, SparseVector{Float64, Int}, [1,2])
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:
8080

81-
```julia
81+
```jldoctest
8282
julia> fill!(BlockArray{Float32}(undef_blocks, [1,2], [3,2]), 0)
8383
ERROR: UndefRefError: access to undefined reference
84-
8584
```
8685

8786
## [Setting and getting blocks and values](@id setting_and_getting)
@@ -164,32 +163,35 @@ julia> view(A, Block.(1:2))
164163

165164
An array can be repacked into a `BlockArray` with `BlockArray(array, block_sizes...)`:
166165

167-
```jl
168-
julia> block_array_sparse = BlockArray(sprand(4, 5, 0.7), [1,3], [2,3])
169-
2×2-blocked 4×5 BlockArray{Float64, 2, Matrix{SparseMatrixCSC{Float64, Int64}}, Tuple{BlockedUnitRange{Vector{Int64}}, BlockedUnitRange{Vector{Int64}}}}:
170-
0.0341601 0.3741870.0118196 0.299058 0.0
171-
----------------------------------------------------
172-
0.0945445 0.9311150.0460428 0.0 0.0
173-
0.314926 0.4389390.496169 0.0 0.0
174-
0.12781 0.2468620.732 0.449182 0.875096
166+
```jldoctest repack
167+
julia> S = spzeros(4,5); S[1,2] = S[4,3] = 1;
168+
169+
julia> block_array_sparse = BlockArray(S, [1,3], [2,3])
170+
2×2-blocked 4×5 BlockMatrix{Float64, Matrix{SparseMatrixCSC{Float64, Int64}}, Tuple{BlockedUnitRange{Vector{Int64}}, BlockedUnitRange{Vector{Int64}}}}:
171+
⋅ 1.0 │ ⋅ ⋅ ⋅
172+
──────────┼───────────────
173+
⋅ ⋅ │ ⋅ ⋅ ⋅
174+
⋅ ⋅ │ ⋅ ⋅ ⋅
175+
⋅ ⋅ │ 1.0 ⋅ ⋅
175176
```
176177

177-
To get back the underlying array use `Array`:
178+
To get back the underlying sparse array, use `sparse`:
178179

179-
```jl
180+
```jldoctest repack
181+
julia> sparse(block_array_sparse)
182+
4×5 SparseMatrixCSC{Float64, Int64} with 2 stored entries:
183+
⋅ 1.0 ⋅ ⋅ ⋅
184+
⋅ ⋅ ⋅ ⋅ ⋅
185+
⋅ ⋅ ⋅ ⋅ ⋅
186+
⋅ ⋅ 1.0 ⋅ ⋅
187+
```
188+
189+
To get a dense array, use `Array`:
190+
```jldoctest repack
180191
julia> Array(block_array_sparse)
181-
4×5 SparseMatrixCSC{Float64,Int64} with 13 stored entries:
182-
[1, 1] = 0.30006
183-
[2, 1] = 0.451742
184-
[3, 1] = 0.243174
185-
[4, 1] = 0.156468
186-
[1, 2] = 0.94057
187-
[3, 2] = 0.544175
188-
[4, 2] = 0.598345
189-
[3, 3] = 0.737486
190-
[4, 3] = 0.929512
191-
[1, 4] = 0.539601
192-
[3, 4] = 0.757658
193-
[4, 4] = 0.44709
194-
[2, 5] = 0.514679
192+
4×5 Matrix{Float64}:
193+
0.0 1.0 0.0 0.0 0.0
194+
0.0 0.0 0.0 0.0 0.0
195+
0.0 0.0 0.0 0.0 0.0
196+
0.0 0.0 1.0 0.0 0.0
195197
```

test/runtests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ end
88
using Documenter
99
@testset "docstrings" begin
1010
# don't test docstrings on old versions to avoid failures due to changes in types
11-
if VERSION >= v"1.9"
11+
if v"1.10" <= VERSION < v"1.11.0-"
1212
DocMeta.setdocmeta!(BlockArrays, :DocTestSetup, :(using BlockArrays); recursive=true)
13-
doctest(BlockArrays)
13+
doctest(BlockArrays, manual=false)
1414
end
1515
end
1616

0 commit comments

Comments
 (0)