Skip to content

Commit 45bd192

Browse files
authored
move docstrings from ApproxFun (#221)
1 parent 9447c21 commit 45bd192

File tree

7 files changed

+480
-4
lines changed

7 files changed

+480
-4
lines changed

src/Fun.jl

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,57 @@ end
2222

2323
const VFun{S,T} = Fun{S,T,Vector{T}}
2424

25+
"""
26+
Fun(s::Space, coefficients::AbstractVector)
27+
28+
Return a `Fun` with the specified `coefficients` in the space `s`
29+
30+
# Examples
31+
```jldoctest
32+
julia> f = Fun(Fourier(), [1,1]);
33+
34+
julia> f(0.1) == 1 + sin(0.1)
35+
true
36+
37+
julia> f = Fun(Chebyshev(), [1,1]);
38+
39+
julia> f(0.1) == 1 + 0.1
40+
true
41+
```
42+
"""
2543
Fun(sp::Space,coeff::AbstractVector) = Fun{typeof(sp),eltype(coeff),typeof(coeff)}(sp,coeff)
44+
45+
"""
46+
Fun()
47+
48+
Return `Fun(identity, Chebyshev())`, which represents the identity function in `-1..1`.
49+
50+
# Examples
51+
```jldoctest
52+
julia> f = Fun(Chebyshev())
53+
Fun(Chebyshev(), [0.0, 1.0])
54+
55+
julia> f(0.1)
56+
0.1
57+
```
58+
"""
2659
Fun() = Fun(identity, ChebyshevInterval())
2760
Fun(d::Domain) = Fun(identity,d)
61+
62+
"""
63+
Fun(s::Space)
64+
65+
Return `Fun(identity, s)`
66+
67+
# Examples
68+
```jldoctest
69+
julia> x = Fun(Chebyshev())
70+
Fun(Chebyshev(), [0.0, 1.0])
71+
72+
julia> x(0.1)
73+
0.1
74+
```
75+
"""
2876
Fun(d::Space) = Fun(identity,d)
2977

3078

@@ -44,6 +92,20 @@ hasnumargs(f::Fun,k) = k == 1 || domaindimension(f) == k # all funs take a sing
4492
##Coefficient routines
4593
#TODO: domainscompatible?
4694

95+
"""
96+
coefficients(f::Fun, s::Space) -> Vector
97+
98+
Return the coefficients of `f` in the space `s`, which
99+
may not be the same as `space(f)`.
100+
101+
# Examples
102+
```jldoctest
103+
julia> f = Fun(x->(3x^2-1)/2);
104+
105+
julia> coefficients(f, Legendre()) ≈ [0, 0, 1]
106+
true
107+
```
108+
"""
47109
function coefficients(f::Fun,msp::Space)
48110
#zero can always be converted
49111
fc = f.coefficients
@@ -54,6 +116,24 @@ function coefficients(f::Fun,msp::Space)
54116
end
55117
end
56118
coefficients(f::Fun,::Type{T}) where {T<:Space} = coefficients(f,T(domain(f)))
119+
120+
"""
121+
coefficients(f::Fun) -> Vector
122+
123+
Return the coefficients of `f`, corresponding to the space `space(f)`.
124+
125+
# Examples
126+
```jldoctest
127+
julia> f = Fun(x->x^2)
128+
Fun(Chebyshev(), [0.5, 0.0, 0.5])
129+
130+
julia> coefficients(f)
131+
3-element Vector{Float64}:
132+
0.5
133+
0.0
134+
0.5
135+
```
136+
"""
57137
coefficients(f::Fun) = f.coefficients
58138
coefficients(c::Number,sp::Space) = Fun(c,sp).coefficients
59139

@@ -178,11 +258,45 @@ setspace(f::Fun,s::Space) = Fun(s,f.coefficients)
178258

179259
## General routines
180260

261+
"""
262+
domain(f::Fun)
181263
264+
Return the domain that `f` is defined on.
265+
266+
# Examples
267+
```jldoctest
268+
julia> f = Fun(x->x^2);
269+
270+
julia> domain(f)
271+
-1.0..1.0 (Chebyshev)
272+
273+
julia> f = Fun(x->x^2, 0..1);
274+
275+
julia> domain(f)
276+
0..1
277+
```
278+
"""
182279
domain(f::Fun) = domain(f.space)
183280
domain(v::AbstractMatrix{T}) where {T<:Fun} = map(domain,v)
184281
domaindimension(f::Fun) = domaindimension(f.space)
185282

283+
"""
284+
setdomain(f::Fun, d::Domain)
285+
286+
Return `f` projected onto `domain`.
287+
288+
!!! note
289+
The new function may differ from the original one, as the coefficients are left unchanged.
290+
291+
# Examples
292+
```jldoctest
293+
julia> f = Fun(x->x^2)
294+
Fun(Chebyshev(), [0.5, 0.0, 0.5])
295+
296+
julia> setdomain(f, 0..1)
297+
Fun(Chebyshev(0..1), [0.5, 0.0, 0.5])
298+
```
299+
"""
186300
setdomain(f::Fun,d::Domain) = Fun(setdomain(space(f),d),f.coefficients)
187301

188302
for op in (:tocanonical,:tocanonicalD,:fromcanonical,:fromcanonicalD,:invfromcanonicalD)
@@ -197,6 +311,20 @@ for op in (:fromcanonical,:fromcanonicalD,:invfromcanonicalD)
197311
end
198312

199313

314+
"""
315+
space(f::Fun)
316+
317+
Return the space of `f`.
318+
319+
# Examples
320+
```jldoctest
321+
julia> f = Fun(x->x^2)
322+
Fun(Chebyshev(), [0.5, 0.0, 0.5])
323+
324+
julia> space(f)
325+
Chebyshev()
326+
```
327+
"""
200328
space(f::Fun) = f.space
201329
spacescompatible(f::Fun,g::Fun) = spacescompatible(space(f),space(g))
202330
pointscompatible(f::Fun,g::Fun) = pointscompatible(space(f),space(g))
@@ -206,6 +334,14 @@ canonicaldomain(f::Fun) = canonicaldomain(space(f))
206334

207335
##Evaluation
208336

337+
"""
338+
evaluate(coefficients::AbstractVector, sp::Space, x)
339+
340+
Evaluate the expansion at a point `x` that lies in `domain(sp)`.
341+
If `x` is not in the domain, the returned value will depend on the space,
342+
and should not be relied upon. See [`extrapolate`](@ref) to evaluate a function
343+
at a value outside the domain.
344+
"""
209345
function evaluate(f::AbstractVector,S::Space,x...)
210346
csp=canonicalspace(S)
211347
if spacescompatible(csp,S)
@@ -236,21 +372,94 @@ end
236372
extrapolate(f::AbstractVector,S::Space,x...) = evaluate(f,S,x...)
237373

238374
# Do not override these
375+
"""
376+
extrapolate(f::Fun,x)
377+
378+
Return an extrapolation of `f` from its domain to `x`.
379+
380+
# Examples
381+
```jldoctest
382+
julia> f = Fun(x->x^2)
383+
Fun(Chebyshev(), [0.5, 0.0, 0.5])
384+
385+
julia> domain(f)
386+
-1.0..1.0 (Chebyshev)
387+
388+
julia> extrapolate(f, 2)
389+
4.0
390+
```
391+
"""
239392
extrapolate(f::Fun,x) = extrapolate(f.coefficients,f.space,x)
240393
extrapolate(f::Fun,x,y,z...) = extrapolate(f.coefficients,f.space,Vec(x,y,z...))
241394

242395

243396
##Data routines
244397

398+
"""
399+
values(f::Fun)
400+
401+
Return `f` evaluated at `points(f)`.
402+
403+
# Examples
404+
```jldoctest
405+
julia> f = Fun(x->x^2)
406+
Fun(Chebyshev(), [0.5, 0.0, 0.5])
407+
408+
julia> values(f)
409+
3-element Vector{Float64}:
410+
0.75
411+
0.0
412+
0.75
245413
414+
julia> map(x->x^2, points(f)) ≈ values(f)
415+
true
416+
```
417+
"""
246418
values(f::Fun,dat...) = _values(f.space, f.coefficients, dat...)
247419
_values(sp, v, dat...) = itransform(sp, v, dat...)
248420
_values(sp::UnivariateSpace, v::Vector{T}, dat...) where {T<:Number} =
249421
itransform(sp, v, dat...)::Vector{float(T)}
422+
"""
423+
points(f::Fun)
424+
425+
Return a grid of points that `f` can be transformed into values
426+
and back.
427+
428+
# Examples
429+
```jldoctest
430+
julia> f = Fun(x->x^2);
431+
432+
julia> chebypts(n) = [cos((2i+1)pi/2n) for i in 0:n-1];
433+
434+
julia> points(f) ≈ chebypts(ncoefficients(f))
435+
true
436+
```
437+
"""
250438
points(f::Fun) = points(f.space,ncoefficients(f))
439+
440+
"""
441+
ncoefficients(f::Fun) -> Integer
442+
443+
Return the number of coefficients of a fun
444+
445+
# Examples
446+
```jldoctest
447+
julia> f = Fun(x->x^2)
448+
Fun(Chebyshev(), [0.5, 0.0, 0.5])
449+
450+
julia> ncoefficients(f)
451+
3
452+
```
453+
"""
251454
ncoefficients(f::Fun)::Int = length(f.coefficients)
455+
252456
blocksize(f::Fun) = (block(space(f),ncoefficients(f)).n[1],)
253457

458+
"""
459+
stride(f::Fun)
460+
461+
Return the stride of the coefficients, checked numerically
462+
"""
254463
function stride(f::Fun)
255464
# Check only for stride 2 at the moment
256465
# as higher stride is very rare anyways
@@ -286,6 +495,20 @@ function chop!(f::Fun,tol...)
286495
f
287496
end
288497

498+
"""
499+
chop(f::Fun[, tol = 10eps()]) -> Fun
500+
501+
Reduce the number of coefficients by dropping the tail that is below the specified tolerance.
502+
503+
# Examples
504+
```jldoctest
505+
julia> f = Fun(Chebyshev(), [1,2,3,0,0,0])
506+
Fun(Chebyshev(), [1, 2, 3, 0, 0, 0])
507+
508+
julia> chop(f)
509+
Fun(Chebyshev(), [1, 2, 3])
510+
```
511+
"""
289512
chop(f::Fun,tol...) = chop!(Fun(f.space,Vector(f.coefficients)),tol...)
290513

291514
copy(f::Fun) = Fun(space(f),copy(f.coefficients))
@@ -548,7 +771,11 @@ iszero(f::Fun) = all(iszero,f.coefficients)
548771

549772
# sum, integrate, and idfferentiate are in CalculusOperator
550773

774+
"""
775+
reverseorientation(f::Fun)
551776
777+
Return `f` on a reversed orientated contour.
778+
"""
552779
function reverseorientation(f::Fun)
553780
csp=canonicalspace(f)
554781
if spacescompatible(csp,space(f))

0 commit comments

Comments
 (0)