Skip to content

Commit 89673a2

Browse files
authored
Change repl to jldoctest in docs (#840)
* Change repl to jldoctest in docs * floating point fixes + filter * filter in constructor * split abs space doctests
1 parent 3050390 commit 89673a2

File tree

4 files changed

+407
-147
lines changed

4 files changed

+407
-147
lines changed

docs/src/usage/constructors.md

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,51 @@ end
88

99
`Fun`s in ApproxFun are instances of Julia types with one field to store coefficients and another to describe the function space. Similarly, each function space has one field describing its domain, or another function space. Let's explore:
1010

11-
```@repl
12-
x = Fun(identity,-1..1);
13-
f = exp(x);
14-
g = f/sqrt(1-x^2);
15-
space(f) # Output is pretty version of Chebyshev(Interval(-1.0,1.0))
16-
space(g) # Output is pretty version of JacobiWeight(-0.5,-0.5,Interval(-1.0,1.0))
11+
```jldoctest
12+
julia> x = Fun(identity,-1..1);
13+
14+
julia> f = exp(x);
15+
16+
julia> g = f/sqrt(1-x^2);
17+
18+
julia> space(f) # Output is pretty version of Chebyshev(Interval(-1.0,1.0))
19+
Chebyshev(-1..1)
20+
21+
julia> space(g) # Output is pretty version of JacobiWeight(-0.5, -0.5, -1..1)
22+
(1-x^2)^-0.5[Chebyshev(-1..1)]
1723
```
1824

1925
The absolute value is another case where the space of the output is inferred from the operation:
2026

21-
```@repl
22-
f = Fun(x->cospi(5x),-1..1);
23-
g = abs(f);
24-
space(f)
25-
space(g)
27+
```@meta
28+
DocTestFilters = r"ContinuousSpace{Float64, Float64}\(PiecewiseSegment{Float64}\(\[[0-9,\.\s\-]+\]\)\)"
29+
```
30+
```jldoctest abs_space
31+
julia> f = Fun(x->cospi(5x),-1..1);
32+
33+
julia> g = abs(f);
34+
35+
julia> space(f)
36+
Chebyshev(-1..1)
37+
38+
julia> space(g)
39+
ContinuousSpace{Float64, Float64}(PiecewiseSegment{Float64}([-1.0, -0.9, -0.7000000000000014, -0.5000000000000008, -0.2999999999999996, -0.10000000000000027, 0.09999999999999978, 0.29999999999999993, 0.4999999999999999, 0.6999999999999998, 0.9, 1.0]))
40+
```
41+
```@meta
42+
DocTestFilters = nothing
43+
```
44+
We may check that the domain corresponds to segments separated by the roots of `f`, and the space is
45+
that of continuous functions over the piecewise domain:
46+
```jldoctest abs_space
47+
julia> p = [-1; roots(f); 1];
48+
49+
julia> segments = @views [Segment(x,y) for (x,y) in zip(p[1:end-1], p[2:end])];
50+
51+
julia> components(domain(g)) == segments
52+
true
53+
54+
julia> space(g) == ContinuousSpace(PiecewiseSegment(reverse(segments)))
55+
true
2656
```
2757

2858
## Convenience constructors
@@ -53,10 +83,11 @@ x = Fun()
5383

5484
It is sometimes necessary to specify coefficients explicitly. This is possible via specifying the space followed by a vector of coefficients:
5585

56-
```@repl
57-
f = Fun(Taylor(), [1,2,3]); # represents 1 + 2z + 3z^2
58-
f(0.1)
59-
1 + 2*0.1 + 3*0.1^2
86+
```jldoctest
87+
julia> f = Fun(Taylor(), [1,2,3]); # represents 1 + 2z + 3z^2
88+
89+
julia> f(0.1) ≈ 1 + 2*0.1 + 3*0.1^2
90+
true
6091
```
6192

6293
In higher dimensions, ApproxFun will sum products of the 1D basis functions. So if ``\mathop{T}_i(x)`` is the ``i``th basis function, then a 2D function can be approximated as the following:
@@ -79,10 +110,11 @@ For example, if we are in the two dimensional CosSpace space and we have coeffic
79110

80111
This is illustrated in the following code:
81112

82-
```@repl
83-
f = Fun(CosSpace()^2, [1,2,3])
84-
f(1,2)
85-
1cos(0*1)*cos(0*2) + 2cos(0*1)*cos(1*2) + 3cos(1*1)*cos(0*2)
113+
```jldoctest
114+
julia> f = Fun(CosSpace()^2, [1,2,3]);
115+
116+
julia> f(1,2) ≈ 1cos(0*1)*cos(0*2) + 2cos(0*1)*cos(1*2) + 3cos(1*1)*cos(0*2)
117+
true
86118
```
87119

88120
## Using ApproxFun for “manual” interpolation

docs/src/usage/equations.md

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ Linear equations such as ordinary and partial differential equations, fractional
1414

1515
where we want a solution that is periodic on ``[0,2π)``. This can be solved succinctly as follows:
1616

17-
```@repl
18-
b = Fun(cos,Fourier());
19-
c = 0.1; u = (𝒟+c*I) \ b;
20-
u(0.6)
21-
(c*cos(0.6)+sin(0.6)) / (1+c^2) # exact solution
17+
```jldoctest
18+
julia> b = Fun(cos, Fourier());
19+
20+
julia> c = 0.1;
21+
22+
julia> u = (𝒟 + c*I) \ b;
23+
24+
julia> t = 0.6; # choose a point to verify the solution
25+
26+
julia> u(t) ≈ (c*cos(t)+sin(t)) / (1+c^2) # exact solution
27+
true
2228
```
2329

2430
Recall that `𝒟` is an alias to `Derivative() == Derivative(UnsetSpace(),1)`.
@@ -31,13 +37,25 @@ As another example, consider the Fredholm integral equation
3137

3238
We can solve this equation as follows:
3339

34-
```@repl
35-
Σ = DefiniteIntegral(Chebyshev()); x = Fun();
36-
u = (I+exp(x)*Σ[cos(x)]) \ cos(exp(x));
37-
u(0.1)
40+
```@meta
41+
DocTestFilters = r"[0-9\.]+"
42+
```
43+
```jldoctest fredholm
44+
julia> Σ = DefiniteIntegral(Chebyshev());
45+
46+
julia> x = Fun();
47+
48+
julia> u = (I+exp(x)*Σ[cos(x)]) \ cos(exp(x));
49+
50+
julia> u(0.1)
51+
0.21864294855628819
52+
```
53+
```@meta
54+
DocTestFilters = nothing
3855
```
3956

40-
Note that we used the syntax `op[f::Fun]`, which is a shorthand for `op*Multiplication(f)`.
57+
!!! note
58+
We used the syntax `op[f::Fun]`, which is a shorthand for `op * Multiplication(f)`.
4159

4260
## Boundary conditions
4361

@@ -52,11 +70,16 @@ Incorporating boundary conditions into differential equations is important so th
5270

5371
To pose this in ApproxFun, we want to find a `u` such that `Evaluation(0)*u == 1` and `(𝒟 - t)*u == 0`. This is accomplished via:
5472

55-
```@repl
56-
t = Fun(0..1);
57-
u = [Evaluation(0); 𝒟-t] \ [1;0];
58-
u(0)
59-
norm(u'-t*u)
73+
```jldoctest
74+
julia> t = Fun(0..1);
75+
76+
julia> u = [Evaluation(0); 𝒟-t] \ [1;0];
77+
78+
julia> u(0) ≈ 1
79+
true
80+
81+
julia> norm(u'-t*u) < eps()
82+
true
6083
```
6184

6285
Behind the scenes, the `Vector{Operator{T}}` representing the functionals and operators are combined into a single `InterlaceOperator`.
@@ -72,23 +95,27 @@ A common usage is two-point boundary value problems. Consider the singularly per
7295

7396
This can be solved in ApproxFun via:
7497

75-
```@repl
76-
ϵ = 1/70; x = Fun();
77-
u = [Evaluation(-1);
78-
Evaluation(1);
79-
ϵ*𝒟^2-x*𝒟+I] \ [1,2,0];
80-
u(0.1)
98+
```jldoctest twopt
99+
julia> ϵ = 1/70;
100+
101+
julia> x = Fun();
102+
103+
julia> u = [Evaluation(-1); Evaluation(1); ϵ*𝒟^2-x*𝒟+I] \ [1,2,0];
104+
105+
julia> u(0.1) ≈ 0.05 # compare with the analytical solution
106+
true
81107
```
82108

83-
Note in this case the space is inferred from the variable coefficient `x`.
109+
!!! note
110+
In this case the space is inferred from the variable coefficient `x`.
111+
112+
This ODE can also be solved using the [`Dirichlet`](@ref) operator:
84113

85-
This ODE can also be solved using the `Dirichlet` operator:
114+
```jldoctest twopt
115+
julia> u = [Dirichlet(); ϵ*𝒟^2-x*𝒟+I] \ [[1,2],0];
86116
87-
```@repl
88-
x = Fun();
89-
u = [Dirichlet();
90-
1/70*𝒟^2-x*𝒟+I] \ [[1,2],0];
91-
u(0.1)
117+
julia> u(0.1) ≈ 0.05 # compare with the analytical solution
118+
true
92119
```
93120

94121
## QR Factorization

0 commit comments

Comments
 (0)