Skip to content

Commit 50185a5

Browse files
authored
Constant propagation in Multiplication(f, ::Ultraspherical) (#151)
* Constant propagation in Multiplication(f, ::Ultraspherical) * Version bump to v0.5.17 * Update tests
1 parent 04057eb commit 50185a5

File tree

5 files changed

+68
-35
lines changed

5 files changed

+68
-35
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ApproxFunOrthogonalPolynomials"
22
uuid = "b70543e2-c0d9-56b8-a290-0d4d6d4de211"
3-
version = "0.5.16"
3+
version = "0.5.17"
44

55
[deps]
66
ApproxFunBase = "fbd15aa5-315a-5a7d-a8a4-24992e37be05"
@@ -19,7 +19,7 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1919
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
2020

2121
[compat]
22-
ApproxFunBase = "0.7.34"
22+
ApproxFunBase = "0.7.37"
2323
ApproxFunBaseTest = "0.1"
2424
Aqua = "0.5"
2525
BandedMatrices = "0.16, 0.17"

src/Spaces/Ultraspherical/UltrasphericalOperators.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,25 @@ Base.stride(M::ConcreteMultiplication{U,V}) where {U<:Ultraspherical,V<:Chebyshe
2626
Base.stride(M::ConcreteMultiplication{U,V}) where {U<:Ultraspherical,V<:Ultraspherical} =
2727
stride(M.f)
2828

29-
30-
function Multiplication(f::Fun{C},sp::Ultraspherical{Int}) where C<:Chebyshev
29+
@inline function _Multiplication(f::Fun{<:Chebyshev}, sp::Ultraspherical{Int})
3130
if order(sp) == 1
3231
cfs = f.coefficients
3332
MultiplicationWrapper(f,
3433
SpaceOperator(
35-
length(cfs) > 0 ?
36-
SymToeplitzOperator(cfs/2) +
37-
HankelOperator(view(cfs,3:length(cfs))/(-2)) :
34+
SymToeplitzOperator(cfs/2) +
3835
HankelOperator(view(cfs,3:length(cfs))/(-2)),
39-
sp,sp))
40-
36+
sp, sp)
37+
)
4138
else
4239
ConcreteMultiplication(f,sp)
4340
end
4441
end
42+
@static if VERSION >= v"1.8"
43+
Base.@constprop aggressive Multiplication(f::Fun{<:Chebyshev}, sp::Ultraspherical{Int}) =
44+
_Multiplication(f, sp)
45+
else
46+
Multiplication(f::Fun{<:Chebyshev}, sp::Ultraspherical{Int}) = _Multiplication(f, sp)
47+
end
4548

4649

4750
## Derivative

test/ChebyshevTest.jl

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -307,19 +307,41 @@ using ApproxFunOrthogonalPolynomials: forwardrecurrence
307307
@test a[1:nmin] b[1:nmin]
308308
end
309309

310-
@testset "constant propagation in Dirichlet" begin
311-
D = if VERSION >= v"1.8"
312-
@inferred (r -> Dirichlet(r))(Chebyshev(0..1))
313-
else
314-
Dirichlet(Chebyshev(0..1))
310+
@testset "Constant propagation" begin
311+
@testset "Dirichlet" begin
312+
D = if VERSION >= v"1.8"
313+
@inferred (r -> Dirichlet(r))(Chebyshev(0..1))
314+
else
315+
Dirichlet(Chebyshev(0..1))
316+
end
317+
# Dirichlet constraints don't depend on the domain
318+
D2 = Dirichlet(Chebyshev())
319+
@test Matrix(D[:, 1:4]) == Matrix(D2[:, 1:4])
320+
321+
D = @inferred (() -> Dirichlet(Chebyshev(), 2))()
322+
D2 = @inferred (() -> Dirichlet(Chebyshev(-1..1), 2))()
323+
@test Matrix(D[:, 1:4]) == Matrix(D2[:, 1:4])
324+
end
325+
326+
@testset "Multiplication" begin
327+
f = () -> Fun(0..1) * Derivative(Chebyshev(0..1))
328+
A = VERSION >= v"1.8" ? (@inferred f()) : f()
329+
@test (A * Fun(0..1))(0.5) 0.5
330+
331+
f = () -> Fun() * Derivative(Chebyshev())
332+
A = VERSION >= v"1.8" ? (@inferred f()) : f()
333+
@test (A * Fun())(0.5) 0.5
315334
end
316-
# Dirichlet constraints don't depend on the domain
317-
D2 = Dirichlet(Chebyshev())
318-
@test Matrix(D[:, 1:4]) == Matrix(D2[:, 1:4])
335+
end
336+
337+
@testset "Inference in PlusOperator" begin
338+
f = () -> Derivative(Chebyshev(0..1)) + Derivative(Chebyshev(0..1))
339+
A = VERSION >= v"1.8" ? (@inferred f()) : f()
340+
@test (A * Fun(0..1))(0.5) 2.0
319341

320-
D = @inferred (() -> Dirichlet(Chebyshev(), 2))()
321-
D2 = @inferred (() -> Dirichlet(Chebyshev(-1..1), 2))()
322-
@test Matrix(D[:, 1:4]) == Matrix(D2[:, 1:4])
342+
f = () -> Derivative(Chebyshev()) + Derivative(Chebyshev())
343+
A = VERSION >= v"1.8" ? (@inferred f()) : f()
344+
@test (A * Fun())(0.5) 2.0
323345
end
324346

325347
@testset "Evaluation" begin

test/UltrasphericalTest.jl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ using ApproxFunOrthogonalPolynomials: jacobip
3535
end
3636

3737
@testset "Normalized space" begin
38-
for f in Any[x -> 3x^3 + 5x^2 + 2, x->x, identity]
39-
for dt in Any[(), (0..1,)],
40-
S in Any[Ultraspherical(1, dt...),
38+
for f in (x -> 3x^3 + 5x^2 + 2, x->x, identity)
39+
for dt in ((), (0..1,)),
40+
S in (Ultraspherical(1, dt...),
4141
Ultraspherical(0.5,dt...),
42-
Ultraspherical(3, dt...)]
42+
Ultraspherical(3, dt...))
4343

4444
NS = NormalizedPolynomialSpace(S)
4545
fS = Fun(f, S)
@@ -63,16 +63,16 @@ using ApproxFunOrthogonalPolynomials: jacobip
6363
L = ultra2leg(ApproxFunBase.canonicalspace(U))
6464
NormalizedPolynomialSpace(L)
6565
end
66-
@testset for T in Any[Float32, Float64], ET in Any[T, complex(T)]
66+
@testset for T in (Float32, Float64), ET in (T, complex(T))
6767
v = Array{ET}(undef, 10)
6868
v2 = similar(v)
6969
M = Array{ET}(undef, 10, 10)
7070
M2 = similar(M)
7171
A = Array{ET}(undef, 10, 10, 10)
7272
A2 = similar(A)
73-
@testset for d in Any[(), (0..1,)], order in Any[0.5, 1, 3]
73+
@testset for d in ((), (0..1,)), order in (0.5, 1, 3)
7474
U = Ultraspherical(order, d...)
75-
Slist = Any[U, NormalizedPolynomialSpace(U)]
75+
Slist = (U, NormalizedPolynomialSpace(U))
7676
@testset for S in Slist
7777
if order == 0.5
7878
L = ultra2leg(S)
@@ -100,6 +100,7 @@ using ApproxFunOrthogonalPolynomials: jacobip
100100
test_transform!(A, A2, S)
101101
end
102102
end
103+
endend
103104
end
104105
end
105106

@@ -110,13 +111,13 @@ using ApproxFunOrthogonalPolynomials: jacobip
110111

111112
@testset "Evaluation" begin
112113
c = [i^2 for i in 1:4]
113-
@testset for d in Any[0..1, ChebyshevInterval()], order in Any[1, 2, 0.5]
114-
@testset for _sp in Any[Ultraspherical(order), Ultraspherical(order,d)],
115-
sp in Any[_sp, NormalizedPolynomialSpace(_sp)]
114+
@testset for d in (0..1, ChebyshevInterval()), order in (1, 2, 0.5)
115+
@testset for _sp in (Ultraspherical(order), Ultraspherical(order,d)),
116+
sp in (_sp, NormalizedPolynomialSpace(_sp))
116117
d = domain(sp)
117118
f = Fun(sp, c)
118-
for ep in [leftendpoint, rightendpoint],
119-
ev in [ApproxFunBase.ConcreteEvaluation, Evaluation]
119+
for ep in (leftendpoint, rightendpoint),
120+
ev in (ApproxFunBase.ConcreteEvaluation, Evaluation)
120121
E = @inferred ev(sp, ep, 0)
121122
@test E[2:4] E[1:4][2:end]
122123
@test E[1:2:5] E[1:5][1:2:5]

test/miscAFBase.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ Base.:(==)(a::UniqueInterval, b::UniqueInterval) = (@assert a.parentinterval ==
144144
end
145145

146146
@testset "inplace transform" begin
147-
@testset for sp_c in Any[Legendre(), Chebyshev(), Jacobi(1,2), Jacobi(0.3, 2.3),
148-
Ultraspherical(1), Ultraspherical(2)]
149-
@testset for sp in Any[sp_c, NormalizedPolynomialSpace(sp_c)]
147+
@testset for sp_c in (Legendre(), Chebyshev(), Jacobi(1,2), Jacobi(0.3, 2.3),
148+
Ultraspherical(1), Ultraspherical(2))
149+
@testset for sp in (sp_c, NormalizedPolynomialSpace(sp_c))
150150
v = rand(10)
151151
v2 = copy(v)
152152
@test itransform!(sp, transform!(sp, v)) v
@@ -371,6 +371,13 @@ Base.:(==)(a::UniqueInterval, b::UniqueInterval) = (@assert a.parentinterval ==
371371
@test (L * one(L))(x,y) L(x,y)
372372
@test (L + zero(L))(x,y) L(x,y)
373373
end
374+
375+
@testset "Multiplication" begin
376+
# empty coefficients
377+
f = () -> Multiplication(Fun(Chebyshev(), Float64[]), Ultraspherical(1))
378+
M = VERSION >= v"1.8" ? (@inferred f()) : f()
379+
@test all(iszero, coefficients(M * Fun()))
380+
end
374381
end
375382

376383
end # module

0 commit comments

Comments
 (0)