Skip to content

Commit c14da85

Browse files
committed
manual doctests; closes #274
1 parent 6e61c75 commit c14da85

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

docs/src/iterators.md

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,42 @@ jacobi_iterable(x, A::SparseMatrixCSC, b; maxiter::Int = 10) =
2828

2929
Now if you apply Jacobi iteration multiple times with the same matrix for just a few iterations, it makes sense to initialize the iterable only once and reuse it afterwards:
3030

31-
```julia
32-
A = sprand(10_000, 10_000, 10 / 10_000) + 20I
33-
b1 = rand(10_000)
34-
b2 = rand(10_000)
35-
x = rand(10_000)
31+
```jldoctest
32+
julia> using LinearAlgebra, SparseArrays, IterativeSolvers
3633
37-
my_iterable = IterativeSolvers.jacobi_iterable(x, A, b1, maxiter = 4)
34+
julia> A = sprand(10_000, 10_000, 10 / 10_000) + 20I;
3835
39-
for item in my_iterable
40-
println("Iteration for rhs 1")
41-
end
36+
julia> b1 = rand(10_000);
4237
43-
@show norm(b1 - A * x) / norm(b1)
38+
julia> b2 = rand(10_000);
4439
45-
# Copy the next right-hand side into the iterable
46-
copyto!(my_iterable.b, b2)
40+
julia> x = rand(10_000);
4741
48-
for item in my_iterable
49-
println("Iteration for rhs 2")
50-
end
42+
julia> my_iterable = IterativeSolvers.jacobi_iterable(x, A, b1, maxiter = 4);
43+
44+
julia> for item in my_iterable
45+
println("Iteration for rhs 1")
46+
end
47+
Iteration for rhs 1
48+
Iteration for rhs 1
49+
Iteration for rhs 1
50+
Iteration for rhs 1
51+
52+
julia> relative_residual = norm(b1 - A * x) / norm(b1);
53+
54+
julia> # Copy the next right-hand side into the iterable
55+
copyto!(my_iterable.b, b2);
56+
57+
julia> for item in my_iterable
58+
println("Iteration for rhs 2")
59+
end
60+
Iteration for rhs 2
61+
Iteration for rhs 2
62+
Iteration for rhs 2
63+
Iteration for rhs 2
5164
52-
@show norm(b2 - A * x) / norm(b2)
65+
julia> relative_residual ≈ norm(b2 - A * x) / norm(b2)
66+
false
5367
```
5468

5569
This would output:

test/common.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ end
3030
end
3131

3232
DocMeta.setdocmeta!(IterativeSolvers, :DocTestSetup, :(using IterativeSolvers); recursive=true)
33-
doctest(IterativeSolvers, manual=false)
33+
doctest(IterativeSolvers, manual=true)
3434

3535
end # module

0 commit comments

Comments
 (0)