Skip to content

Commit 76d6c3f

Browse files
authored
Add docstrings to LowRankFun and PartialInverseOperator (#219)
* docstrings for LowRankFun and PartialInverseOperator * condense PartialInverseOperator methods * fix off-by-one in basisfunction * error checks in basisfunction * undo basisfunction off-by-one
1 parent d26f787 commit 76d6c3f

File tree

5 files changed

+96
-8
lines changed

5 files changed

+96
-8
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ApproxFunBase"
22
uuid = "fbd15aa5-315a-5a7d-a8a4-24992e37be05"
3-
version = "0.7.13"
3+
version = "0.7.14"
44

55
[deps]
66
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

src/Multivariate/LowRankFun.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,19 @@
55
export LowRankFun
66

77
"""
8-
`LowRankFun` gives an approximation to a bivariate function in low rank form.
8+
LowRankFun
9+
10+
Return an approximation to a bivariate function in low rank form.
11+
12+
# Examples
13+
```jldoctest
14+
julia> f = (x,y) -> x^2 * y^3;
15+
16+
julia> L = LowRankFun(f, Chebyshev() ⊗ Chebyshev());
17+
18+
julia> L(0.1, 0.2) ≈ f(0.1, 0.2)
19+
true
20+
```
921
"""
1022
mutable struct LowRankFun{S<:Space,M<:Space,SS<:AbstractProductSpace,T<:Number} <: BivariateFun{T}
1123
A::Vector{VFun{S,T}}
@@ -30,6 +42,7 @@ LowRankFun(A::Vector{VFun{S,T}},B::Vector{VFun{M,V}}) where {S,M,T,V} =
3042
LowRankFun(strictconvert(Vector{VFun{S,promote_type(T,V)}},A),
3143
strictconvert(Vector{VFun{M,promote_type(T,V)}},B),
3244
space(first(A))space(first(B)))
45+
3346
rank(f::LowRankFun) = length(f.A)
3447
size(f::LowRankFun,k::Integer) = k==1 ? mapreduce(length,max,f.A) : mapreduce(length,max,f.B)
3548
size(f::LowRankFun) = size(f,1),size(f,2)

src/Operators/general/PartialInverseOperator.jl

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,63 @@
11
export PartialInverseOperator
22

3+
"""
4+
PartialInverseOperator(O::Operator[, bandwidths = bandwidths(O)])
35
4-
struct PartialInverseOperator{T<:Number,CO<:CachedOperator,BI} <: Operator{T}
6+
Return an approximate estimate for `inv(O)`, such that `PartialInverseOperator(O) * O` is banded, and
7+
is approximately `I` up to a bandwidth that is one less than the sum of the bandwidths
8+
of `O` and `PartialInverseOperator(O)`.
9+
10+
!!! note
11+
Only upper triangular operators are supported as of now.
12+
13+
# Examples
14+
15+
```jldoctest
16+
julia> C = Conversion(Chebyshev(), Ultraspherical(1));
17+
18+
julia> bandwidths(C)
19+
(0, 2)
20+
21+
julia> P = PartialInverseOperator(C);
22+
23+
julia> bandwidths(P)
24+
(0, 2)
25+
26+
julia> P * C
27+
TimesOperator : Chebyshev() → Chebyshev()
28+
1.0 0.0 0.0 0.0 -0.5 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
29+
⋅ 1.0 0.0 0.0 0.0 -1.0 ⋅ ⋅ ⋅ ⋅ ⋅
30+
⋅ ⋅ 1.0 0.0 0.0 0.0 -1.0 ⋅ ⋅ ⋅ ⋅
31+
⋅ ⋅ ⋅ 1.0 0.0 0.0 0.0 -1.0 ⋅ ⋅ ⋅
32+
⋅ ⋅ ⋅ ⋅ 1.0 0.0 0.0 0.0 -1.0 ⋅ ⋅
33+
⋅ ⋅ ⋅ ⋅ ⋅ 1.0 0.0 0.0 0.0 -1.0 ⋅
34+
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 0.0 0.0 0.0 ⋱
35+
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 0.0 0.0 ⋱
36+
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 0.0 ⋱
37+
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋱
38+
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋱
39+
40+
julia> P = PartialInverseOperator(C, (0, 4));
41+
42+
julia> bandwidths(P)
43+
(0, 4)
44+
45+
julia> P * C
46+
TimesOperator : Chebyshev() → Chebyshev()
47+
1.0 0.0 0.0 0.0 0.0 0.0 -0.5 ⋅ ⋅ ⋅ ⋅
48+
⋅ 1.0 0.0 0.0 0.0 0.0 0.0 -1.0 ⋅ ⋅ ⋅
49+
⋅ ⋅ 1.0 0.0 0.0 0.0 0.0 0.0 -1.0 ⋅ ⋅
50+
⋅ ⋅ ⋅ 1.0 0.0 0.0 0.0 0.0 0.0 -1.0 ⋅
51+
⋅ ⋅ ⋅ ⋅ 1.0 0.0 0.0 0.0 0.0 0.0 ⋱
52+
⋅ ⋅ ⋅ ⋅ ⋅ 1.0 0.0 0.0 0.0 0.0 ⋱
53+
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 0.0 0.0 0.0 ⋱
54+
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 0.0 0.0 ⋱
55+
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 0.0 ⋱
56+
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋱
57+
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋱
58+
```
59+
"""
60+
struct PartialInverseOperator{T<:Number,CO<:CachedOperator,BI<:Tuple{Any,Any}} <: Operator{T}
561
cache::CO
662
bandwidths::BI
763
end
@@ -11,10 +67,12 @@ function PartialInverseOperator(CO::CachedOperator{T},bandwidths) where T<:Numbe
1167
return PartialInverseOperator{T,typeof(CO),typeof(bandwidths)}(CO,bandwidths)
1268
end
1369

14-
PartialInverseOperator(B::Operator, bandwidths) = PartialInverseOperator(cache(B), bandwidths)
15-
PartialInverseOperator(B::Operator) = PartialInverseOperator(B, bandwidths(B))
70+
function PartialInverseOperator(B::Operator, bandwidths = bandwidths(B))
71+
PartialInverseOperator(cache(B), bandwidths)
72+
end
1673

17-
convert(::Type{Operator{T}},A::PartialInverseOperator) where {T}=PartialInverseOperator(strictconvert(Operator{T},A.cache), A.bandwidths)
74+
convert(::Type{Operator{T}},A::PartialInverseOperator) where {T} =
75+
PartialInverseOperator(strictconvert(Operator{T},A.cache), A.bandwidths)
1876

1977
domainspace(P::PartialInverseOperator)=rangespace(P.cache)
2078
rangespace(P::PartialInverseOperator)=domainspace(P.cache)
@@ -60,7 +118,7 @@ end
60118

61119
## These are both hacks that apparently work
62120

63-
function BandedMatrix(S::SubOperator{T,PP,Tuple{UnitRange{Int},UnitRange{Int}}}) where {T,PP<:PartialInverseOperator}
121+
function BandedMatrix(S::SubOperator{T,<:PartialInverseOperator,Tuple{UnitRange{Int},UnitRange{Int}}}) where {T}
64122
kr,jr = parentindices(S)
65123
P = parent(S)
66124
#ret = BandedMatrix{eltype(S)}(undef, size(S), bandwidths(S))

src/onehotvector.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
struct OneHotVector{T} <: AbstractVector{T}
22
n :: Int
33
len :: Int
4+
5+
function OneHotVector{T}(n, len) where {T}
6+
len >= 0 || throw(ArgumentError("length must be non-negative"))
7+
0 <= n <= len || throw(ArgumentError("index must be <= length"))
8+
new{T}(n, len)
9+
end
410
end
511
OneHotVector(n, len = n) = OneHotVector{Float64}(n, len)
612
Base.size(v::OneHotVector) = (v.len,)
713
Base.length(v::OneHotVector) = v.len
814
function Base.getindex(v::OneHotVector{T}, i::Int) where {T}
915
i == v.n ? one(T) : zero(T)
1016
end
11-
basisfunction(sp, k) = Fun(sp, OneHotVector(k))
17+
# assume that the basis label starts at zero
18+
function basisfunction(sp, k)
19+
k >= 0 || throw(ArgumentError("basis label must be non-negative, received $k"))
20+
Fun(sp, OneHotVector(k))
21+
end

test/SpacesTest.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ using LinearAlgebra
163163
@test^f == exp(f)
164164
@test values(ℯ^f) == exp.(values(f))
165165
end
166+
167+
# trivial sanity tests for PartialInverseOperator
168+
@testset "PartialInverseOperator" begin
169+
C = Conversion(PointSpace(1:3), PointSpace(1:3))
170+
P = PartialInverseOperator(C)
171+
@test AbstractMatrix(P * C) == I(size(C,1))
172+
end
166173
end
167174

168175
@testset "DiracSpace" begin

0 commit comments

Comments
 (0)