Skip to content

Commit 4c8cdc2

Browse files
authored
type inference improvements in CosSpace/Fourier integrate (#83)
* type inference improvements in CosSpace/Fourier integrate * Test for empty case * Test against AFOP for CosSpace integrate
1 parent 13aeab6 commit 4c8cdc2

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1919
AbstractFFTs = "0.5, 1"
2020
ApproxFunBase = "0.8.6"
2121
ApproxFunBaseTest = "0.1"
22+
ApproxFunOrthogonalPolynomials = "0.6"
2223
Aqua = "0.5"
2324
BandedMatrices = "0.16, 0.17"
2425
DomainSets = "0.3, 0.4, 0.5, 0.6"
@@ -33,9 +34,10 @@ julia = "1.6"
3334

3435
[extras]
3536
ApproxFunBaseTest = "a931bfaf-0cfd-4a5c-b69c-bf2eed002b43"
37+
ApproxFunOrthogonalPolynomials = "b70543e2-c0d9-56b8-a290-0d4d6d4de211"
3638
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
3739
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
3840
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3941

4042
[targets]
41-
test = ["ApproxFunBaseTest", "Aqua", "Test", "SpecialFunctions"]
43+
test = ["ApproxFunBaseTest", "ApproxFunOrthogonalPolynomials", "Aqua", "Test", "SpecialFunctions"]

src/calculus.jl

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,25 @@ linesum(f::Fun{CosSpace{DD,RR}}) where {DD<:PeriodicSegment,RR} =
6868

6969

7070
function integrate(f::Fun{CS}) where CS<:CosSpace
71-
if isa(domain(f),Circle)
72-
error("Integrate not implemented for CosSpace on Circle")
73-
else # Probably periodic itnerval, drop constant term if zero
71+
d = domain(f)
72+
tinf = if d isa Circle
73+
throw(ArgumentError("Integrate not implemented for CosSpace on Circle"))
74+
else # Probably periodic interval, drop constant term if zero
7475
tol=1E-14 #TODO: smart tolerance. Here relative is a bit tricky
7576
# since this is called by Fourier integrate
76-
if abs(f.coefficients[1])<tol
77+
if isempty(f.coefficients) || abs(f.coefficients[1])<tol
7778
integrate(Fun(f,space(f)|(2:∞)))
78-
else
79-
d=domain(f)
80-
@assert isa(d,PeriodicSegment)
79+
elseif d isa PeriodicSegment
8180
x=Fun(identity, Interval(d))
82-
(f.coefficients[1]*x)integrate(Fun(f,space(f)|(2:∞)))
81+
fconstant = (f.coefficients[1]*x)
82+
fperiodic = integrate(Fun(f,space(f)|(2:∞)))
83+
SS = SumSpace(space(fconstant), space(fperiodic))
84+
Fun(SS, ApproxFunBase.interlace(coefficients(fconstant), coefficients(fperiodic)))
85+
else
86+
throw(ArgumentError("not implemented"))
8387
end
8488
end
89+
chop(tinf)
8590
end
8691

8792
function integrate(f::Fun{SS}) where SS<:SinSpace
@@ -98,9 +103,13 @@ for OP in (:differentiate,:integrate)
98103
@eval $OP(f::Fun{Fourier{D,R},T}) where {T,D<:Circle,R} = $OP(Fun(f,Laurent))
99104
end
100105

101-
integrate(f::Fun{Fourier{D,R},T}) where {T,D<:PeriodicSegment,R} =
102-
integrate(component(f,2))integrate(component(f,1))
103-
106+
function integrate(f::Fun{Fourier{D,R},T}) where {T,D<:PeriodicSegment,R}
107+
g1 = integrate(component(f,2))
108+
g2 = integrate(component(f,1))
109+
# g1 ⊕ g2, but we construct the space directly to improve type-stability
110+
SS = SumSpace(space(g1), space(g2))
111+
Fun(SS, ApproxFunBase.interlace(coefficients(g1), coefficients(g2)))
112+
end
104113

105114

106115

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ using ApproxFunBase: Block, EmptyDomain, UnionDomain
44
using ApproxFunBaseTest: testspace, testtransforms, testmultiplication, testraggedbelowoperator,
55
testbandedoperator, testblockbandedoperator, testbandedblockbandedoperator,
66
testcalculus, testfunctional
7+
using ApproxFunOrthogonalPolynomials
78
using LinearAlgebra
89
using SpecialFunctions
910
_factorial(n) = gamma(n+1)
@@ -299,6 +300,12 @@ end
299300
Fun->sin(sin(θ)),Fourier()),Fun->cos(θ)+cos(3θ),CosSpace()))
300301
@test norm(integrate(f)'-f)<10eps()
301302
end
303+
@test iszero(integrate(Fun(CosSpace(), Float64[])))
304+
f = Fun->1+cos(θ), CosSpace())
305+
g = integrate(f)
306+
θ = 0.4
307+
@test g(θ) θ + sin(θ)
308+
@test Fun(g', space(f)) f
302309
end
303310

304311

0 commit comments

Comments
 (0)