Skip to content

Commit

Permalink
Fix examples in manual (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub authored Mar 20, 2024
1 parent 2c6258d commit ea33af1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 48 deletions.
94 changes: 48 additions & 46 deletions docs/src/man/blockarrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@

```@meta
DocTestSetup = quote
using BlockArrays
using Random
Random.seed!(1234)
using BlockArrays, SparseArrays
end
```

## Creating `BlockArray`s from an array

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

```julia
julia> BlockArray(rand(4, 4), [2,2], [1,1,2])
2×3-blocked 4×4 BlockMatrix{Float64}:
0.703930.5687030.0137366 0.953038
0.249570.1459240.884324 0.134155
──────────┼────────────┼─────────────────────
0.4081330.7077230.467458 0.326718
0.8443140.7942790.0421491 0.683791

julia> block_array_sparse = BlockArray(sprand(4, 5, 0.7), [1,3], [2,3])
```jldoctest
julia> BlockArray(Array(reshape(1:16, 4, 4)), [2,2], [1,1,2])
2×3-blocked 4×4 BlockMatrix{Int64}:
1 │ 5 │ 9 13
2 │ 6 │ 10 14
───┼─────┼────────
3 │ 7 │ 11 15
4 │ 8 │ 12 16
julia> S = spzeros(4,5); S[1,2] = S[4,3] = 1;
julia> block_array_sparse = BlockArray(S, [1,3], [2,3])
2×2-blocked 4×5 BlockMatrix{Float64, Matrix{SparseMatrixCSC{Float64, Int64}}, Tuple{BlockedUnitRange{Vector{Int64}}, BlockedUnitRange{Vector{Int64}}}}:
0.0341601 0.3741870.0118196 0.299058 0.0
----------------------------------------------------
0.0945445 0.931115 0.0460428 0.0 0.0
0.314926 0.438939 0.496169 0.0 0.0
0.12781 0.246862 0.732 0.449182 0.875096
⋅ 1.0 ⋅ ⋅ ⋅
──────────┼───────────────
⋅ ⋅
⋅ ⋅ ⋅ ⋅
1.0
```


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

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:

```julia
```jldoctest
julia> BlockArray(undef_blocks, SparseVector{Float64, Int}, [1,2])
2-blocked 3-element BlockVector{Float64, Vector{SparseVector{Float64, Int64}}, Tuple{BlockedUnitRange{Vector{Int64}}}}:
#undef
------
──────
#undef
#undef
```
Expand All @@ -78,10 +78,9 @@ julia> BlockArray(undef_blocks, SparseVector{Float64, Int}, [1,2])

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:

```julia
```jldoctest
julia> fill!(BlockArray{Float32}(undef_blocks, [1,2], [3,2]), 0)
ERROR: UndefRefError: access to undefined reference
```

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

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

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

To get back the underlying array use `Array`:
To get back the underlying sparse array, use `sparse`:

```jl
```jldoctest repack
julia> sparse(block_array_sparse)
4×5 SparseMatrixCSC{Float64, Int64} with 2 stored entries:
⋅ 1.0 ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ 1.0 ⋅ ⋅
```

To get a dense array, use `Array`:
```jldoctest repack
julia> Array(block_array_sparse)
4×5 SparseMatrixCSC{Float64,Int64} with 13 stored entries:
[1, 1] = 0.30006
[2, 1] = 0.451742
[3, 1] = 0.243174
[4, 1] = 0.156468
[1, 2] = 0.94057
[3, 2] = 0.544175
[4, 2] = 0.598345
[3, 3] = 0.737486
[4, 3] = 0.929512
[1, 4] = 0.539601
[3, 4] = 0.757658
[4, 4] = 0.44709
[2, 5] = 0.514679
4×5 Matrix{Float64}:
0.0 1.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 1.0 0.0 0.0
```
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ end
using Documenter
@testset "docstrings" begin
# don't test docstrings on old versions to avoid failures due to changes in types
if VERSION >= v"1.9"
if v"1.10" <= VERSION < v"1.11.0-"
DocMeta.setdocmeta!(BlockArrays, :DocTestSetup, :(using BlockArrays); recursive=true)
doctest(BlockArrays)
doctest(BlockArrays, manual=false)
end
end

Expand Down

0 comments on commit ea33af1

Please sign in to comment.