Skip to content

Commit 59390c6

Browse files
authored
Doctests for ApproxFun and ApproxFunBase (#829)
* Fetch docstrings from AFB * doctest for ApproxFunBase * import packages in meta block
1 parent 8bad4d2 commit 59390c6

File tree

7 files changed

+59
-47
lines changed

7 files changed

+59
-47
lines changed

docs/make.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ for (example, included) in [
2929
end
3030

3131
makedocs(
32-
doctest = false,
33-
clean = true,
3432
format = Documenter.HTML(),
3533
sitename = "ApproxFun.jl",
3634
authors = "Sheehan Olver",

docs/src/library.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,7 @@ Multiplication
290290
```@docs
291291
Neumann
292292
```
293+
294+
```@docs
295+
PartialInverseOperator
296+
```

docs/src/usage/constructors.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
```@setup using-pkgs
2-
using ApproxFun
1+
```@meta
2+
DocTestSetup = quote
3+
using ApproxFun, LinearAlgebra
4+
end
35
```
46

57
# Constructors
68

79
`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:
810

9-
```@repl using-pkgs
11+
```@repl
1012
x = Fun(identity,-1..1);
1113
f = exp(x);
1214
g = f/sqrt(1-x^2);
@@ -16,7 +18,7 @@ space(g) # Output is pretty version of JacobiWeight(-0.5,-0.5,Interval(-1.0,1.0
1618

1719
The absolute value is another case where the space of the output is inferred from the operation:
1820

19-
```@repl using-pkgs
21+
```@repl
2022
f = Fun(x->cospi(5x),-1..1);
2123
g = abs(f);
2224
space(f)
@@ -51,7 +53,7 @@ x = Fun()
5153

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

54-
```@repl using-pkgs
56+
```@repl
5557
f = Fun(Taylor(), [1,2,3]); # represents 1 + 2z + 3z^2
5658
f(0.1)
5759
1 + 2*0.1 + 3*0.1^2
@@ -77,7 +79,7 @@ For example, if we are in the two dimensional CosSpace space and we have coeffic
7779

7880
This is illustrated in the following code:
7981

80-
```@repl using-pkgs
82+
```@repl
8183
f = Fun(CosSpace()^2, [1,2,3])
8284
f(1,2)
8385
1cos(0*1)*cos(0*2) + 2cos(0*1)*cos(1*2) + 3cos(1*1)*cos(0*2)

docs/src/usage/equations.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
```@setup using-pkgs
2-
using ApproxFun, LinearAlgebra
1+
```@meta
2+
DocTestSetup = quote
3+
using ApproxFun, LinearAlgebra
4+
end
35
```
46

57
# Linear equations
@@ -12,7 +14,7 @@ Linear equations such as ordinary and partial differential equations, fractional
1214

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

15-
```@repl using-pkgs
17+
```@repl
1618
b = Fun(cos,Fourier());
1719
c = 0.1; u = (𝒟+c*I) \ b;
1820
u(0.6)
@@ -29,7 +31,7 @@ As another example, consider the Fredholm integral equation
2931

3032
We can solve this equation as follows:
3133

32-
```@repl using-pkgs
34+
```@repl
3335
Σ = DefiniteIntegral(Chebyshev()); x = Fun();
3436
u = (I+exp(x)*Σ[cos(x)]) \ cos(exp(x));
3537
u(0.1)
@@ -50,7 +52,7 @@ Incorporating boundary conditions into differential equations is important so th
5052

5153
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:
5254

53-
```@repl using-pkgs
55+
```@repl
5456
t = Fun(0..1);
5557
u = [Evaluation(0); 𝒟-t] \ [1;0];
5658
u(0)
@@ -70,7 +72,7 @@ A common usage is two-point boundary value problems. Consider the singularly per
7072

7173
This can be solved in ApproxFun via:
7274

73-
```@repl using-pkgs
75+
```@repl
7476
ϵ = 1/70; x = Fun();
7577
u = [Evaluation(-1);
7678
Evaluation(1);
@@ -82,7 +84,7 @@ Note in this case the space is inferred from the variable coefficient `x`.
8284

8385
This ODE can also be solved using the `Dirichlet` operator:
8486

85-
```@repl using-pkgs
87+
```@repl
8688
x = Fun();
8789
u = [Dirichlet();
8890
1/70*𝒟^2-x*𝒟+I] \ [[1,2],0];

docs/src/usage/operators.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
```@setup using-pkgs
2-
using ApproxFun, LinearAlgebra
1+
```@meta
2+
DocTestSetup = quote
3+
using ApproxFun, LinearAlgebra
4+
end
35
```
46

57
# Operators
@@ -12,7 +14,7 @@ Note that the size of an operator is specified by the dimension of the domain an
1214

1315
Differential and integral operators are perhaps the most useful type of operators in mathematics. Consider the derivative operator on `CosSpace`:
1416

15-
```@repl using-pkgs
17+
```@repl
1618
D = Derivative(CosSpace())
1719
f = Fun(θ->cos(cos(θ)), CosSpace());
1820
fp = D*f;
@@ -49,7 +51,7 @@ D[k,j] ≈ (D*ej).coefficients[k] ≈ -k
4951

5052
The `Chebyshev` space has the property that its derivatives are given by ultraspherical spaces:
5153

52-
```@repl using-pkgs
54+
```@repl
5355
Derivative(Chebyshev())
5456
```
5557

@@ -59,7 +61,7 @@ A particularly useful class of operators are _functionals_, which map from funct
5961

6062
As an example, the evaluation functional `f(0)` on `CosSpace` has the form:
6163

62-
```@repl using-pkgs
64+
```@repl
6365
B = Evaluation(CosSpace(),0)
6466
B*f ≈ f(0)
6567
```
@@ -68,7 +70,7 @@ As can be seen from the output, `rangespace(B)` is a `ConstantSpace(Point(0))`,
6870

6971
Closely related to functionals are operators with finite-dimensional range. For example, the `Dirichlet` operator represents the restriction of a space to its boundary. In the case, of `Chebyshev()`, this amounts to evaluation at the endpoints `±1`:
7072

71-
```@repl using-pkgs
73+
```@repl
7274
B = Dirichlet(Chebyshev())
7375
size(B)
7476
B*Fun(exp)
@@ -79,15 +81,15 @@ B*Fun(exp) ≈ Fun([exp(-1),exp(1)])
7981

8082
A `Multiplication` operator sends a `Fun` to a `Fun` in the corresponding space by multiplying a given function. The `Multiplication` operators are presented in matrix form in `ApproxFun`.
8183

82-
```@repl using-pkgs
84+
```@repl
8385
x = Fun();
8486
M = Multiplication(1 + 2x + x^2, Chebyshev())
8587
(M * x).coefficients == ((1 + 2x + x^2) * x).coefficients == M[1:4,1:2] * x.coefficients
8688
```
8789

8890
It is possible for domain space and range space to be different under `Mulitplication`.
8991

90-
```@repl using-pkgs
92+
```@repl
9193
c = Fun(θ -> cos(θ), CosSpace());
9294
Multiplication(c, SinSpace())
9395
```
@@ -116,14 +118,14 @@ where ``f_0 = 0``.
116118
Operators can be algebraically manipulated, provided that the domain and range spaces are compatible, or can be made compatible. As a simple example, we can add the second derivative of a Fourier space to the
117119
identity operator:
118120

119-
```@repl using-pkgs
121+
```@repl
120122
D2 = Derivative(Fourier(),2)
121123
D2 + I
122124
```
123125

124126
When the domain and range space are not the same, the identity operator becomes a conversion operator. That is, to represent `D+I` acting on the Chebyshev space, we would do the following:
125127

126-
```@repl using-pkgs
128+
```@repl
127129
D = Derivative(Chebyshev())
128130
C = Conversion(Chebyshev(),Ultraspherical(1))
129131
D + C
@@ -139,7 +141,7 @@ Now consider the Fredholm integral operator of the second kind:
139141

140142
We can construct this using
141143

142-
```@repl using-pkgs
144+
```@repl
143145
x = Fun();
144146
Σ = DefiniteIntegral(Chebyshev())
145147
L = I + exp(x)*Σ
@@ -167,7 +169,7 @@ Note that `Σ*exp(x)` applies the operator to a function. To construct the oper
167169

168170
It is often more convenient to not specify a space explicitly, but rather infer it when the operator is used. For example, we can construct `Derivative()`, which has the alias `𝒟`, and represents the first derivative on any space:
169171

170-
```@repl using-pkgs
172+
```@repl
171173
f = Fun(cos,Chebyshev(0..1)); (𝒟*f)(0.1)
172174
f = Fun(cos,Fourier()); (𝒟*f)(0.1)
173175
```
@@ -194,7 +196,7 @@ ConcreteDerivative : Chebyshev() → Ultraspherical(2)
194196
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋱
195197
```
196198

197-
Note that `rangespace(D) ≠ Chebyshev()`, hence the operators are not compatible. Therefore, it has thrown away its domain space, and thus this is equivalent to `Derivative(rangespace(D))*D`.
199+
Note that `rangespace(D) ≠ Chebyshev()`, hence the operators are not compatible. Therefore, it has thrown away its domain space, and thus this is equivalent to `Derivative(rangespace(D))*D`.
198200

199201
## Concatenating operators
200202

src/docs.jl

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ reverseorientation(::Fun)
385385
canonicalspace(s::Space)
386386
387387
Return a space that is used as a default to implement missing functionality,
388-
e.g., evaluation. Implement a `Conversion` operator or override `coefficients` to support this.
388+
e.g., evaluation. Implement a [`Conversion`](@ref) operator or override `coefficients` to support this.
389389
390390
# Examples
391391
```jldoctest
@@ -454,7 +454,7 @@ itransform(::Space, ::AbstractVector)
454454
"""
455455
evaluate(coefficients::AbstractVector, sp::Space, x)
456456
457-
Evaluates the expansion at a point `x` that lies in `domain(sp)`.
457+
Evaluate the expansion at a point `x` that lies in `domain(sp)`.
458458
If `x` is not in the domain, the returned value will depend on the space,
459459
and should not be relied upon. See [`extrapolate`](@ref) to evaluate a function
460460
at a value outside the domain.
@@ -473,7 +473,7 @@ spacescompatible(::Space,::Space)
473473
"""
474474
conversion_type(a::Space,b::Space)
475475
476-
Return a `Space` that has a banded conversion operator to both `a` and `b`.
476+
Return a `Space` that has a banded [`conversion`](@ref) operator to both `a` and `b`.
477477
Override `ApproxFun.conversion_rule` when adding new `Conversion` operators.
478478
"""
479479
conversion_type(::Space,::Space)
@@ -491,22 +491,22 @@ dimension(::Space)
491491
"""
492492
Operator{T}
493493
494-
is an abstract type to represent linear operators between spaces.
494+
Abstract type to represent linear operators between spaces.
495495
"""
496496
Operator
497497

498498
"""
499499
domainspace(op::Operator)
500500
501-
gives the domain space of `op`. That is, `op*f` will first convert `f` to
501+
Return the domain space of `op`. That is, `op*f` will first convert `f` to
502502
a `Fun` in the space `domainspace(op)` before applying the operator.
503503
"""
504504
domainspace(::Operator)
505505

506506
"""
507507
rangespace(op::Operator)
508508
509-
gives the range space of `op`. That is, `op*f` will return a `Fun` in the
509+
Return the range space of `op`. That is, `op*f` will return a `Fun` in the
510510
space `rangespace(op)`, provided `f` can be converted to a `Fun` in
511511
`domainspace(op)`.
512512
"""
@@ -546,17 +546,17 @@ choosedomainspace(::Operator,::Space)
546546

547547

548548
"""
549-
op[k,j]
549+
(op::Operator)[k,j]
550550
551551
Return the `k`th coefficient of `op*Fun([zeros(j-1);1],domainspace(op))`.
552552
"""
553553
getindex(::Operator,k,j)
554554

555555

556556
"""
557-
op[f::Fun]
557+
(op::Operator)[f::Fun]
558558
559-
constructs the operator `op * Multiplication(f)`, that is, it multiplies on the right
559+
Construct the operator `op * Multiplication(f)`, that is, it multiplies on the right
560560
by `f` first. Note that `op * f` is different: it applies `op` to `f`.
561561
562562
# Examples
@@ -567,10 +567,10 @@ Fun(Chebyshev(), [0.0, 1.0])
567567
julia> D = Derivative()
568568
ConcreteDerivative : ApproxFunBase.UnsetSpace() → ApproxFunBase.UnsetSpace()
569569
570-
julia> D2 = D[x]
570+
julia> Dx = D[x] # construct the operator y -> d/dx * (x * y)
571571
TimesOperator : ApproxFunBase.UnsetSpace() → ApproxFunBase.UnsetSpace()
572572
573-
julia> twox = D2 * x
573+
julia> twox = Dx * x # Evaluate d/dx * (x * x)
574574
Fun(Ultraspherical(1), [0.0, 1.0])
575575
576576
julia> twox(0.1) ≈ 2 * 0.1
@@ -584,5 +584,8 @@ getindex(::Operator,::Fun)
584584
Conversion(fromspace::Space,tospace::Space)
585585
586586
Represent a conversion operator between `fromspace` and `tospace`, when available.
587+
588+
See also [`PartialInverseOperator`](@ref) that might be able to represent the inverse,
589+
even if this isn't banded.
587590
"""
588591
Conversion(::Space,::Space)

test/runtests.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ using Aqua
77
end
88

99
using Documenter
10+
DocMeta.setdocmeta!(ApproxFunBase, :DocTestSetup, :(using ApproxFun); recursive=true)
1011
DocMeta.setdocmeta!(ApproxFun, :DocTestSetup, :(using ApproxFun); recursive=true)
1112

1213
@testset "doctests" begin
13-
doctest(ApproxFun, manual = false)
14+
doctest(ApproxFun)
15+
doctest(ApproxFunBase, manual=false)
1416
end
1517

1618
include("ReadmeTest.jl")
@@ -186,13 +188,12 @@ end
186188
@test minimum(r) -5
187189
end
188190

189-
f=Fun(exp)
190-
x=sample(f,100000)
191-
x=sample(f,100000)
192-
@time x=sample(f,100000)
193-
println("Sample: Time should be ~0.13")
194-
195-
191+
@testset "sample" begin
192+
f=Fun(exp)
193+
x=sample(f,100000)
194+
x=sample(f,100000)
195+
@time x=sample(f,100000)
196+
end
196197

197198
@testset "Null space" begin
198199
d=ChebyshevInterval()

0 commit comments

Comments
 (0)