Skip to content

Constant propagation in Multiplication(f, ::Ultraspherical) #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ApproxFunOrthogonalPolynomials"
uuid = "b70543e2-c0d9-56b8-a290-0d4d6d4de211"
version = "0.5.16"
version = "0.5.17"

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

[compat]
ApproxFunBase = "0.7.34"
ApproxFunBase = "0.7.37"
ApproxFunBaseTest = "0.1"
Aqua = "0.5"
BandedMatrices = "0.16, 0.17"
Expand Down
17 changes: 10 additions & 7 deletions src/Spaces/Ultraspherical/UltrasphericalOperators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,25 @@ Base.stride(M::ConcreteMultiplication{U,V}) where {U<:Ultraspherical,V<:Chebyshe
Base.stride(M::ConcreteMultiplication{U,V}) where {U<:Ultraspherical,V<:Ultraspherical} =
stride(M.f)


function Multiplication(f::Fun{C},sp::Ultraspherical{Int}) where C<:Chebyshev
@inline function _Multiplication(f::Fun{<:Chebyshev}, sp::Ultraspherical{Int})
if order(sp) == 1
cfs = f.coefficients
MultiplicationWrapper(f,
SpaceOperator(
length(cfs) > 0 ?
SymToeplitzOperator(cfs/2) +
HankelOperator(view(cfs,3:length(cfs))/(-2)) :
SymToeplitzOperator(cfs/2) +
HankelOperator(view(cfs,3:length(cfs))/(-2)),
sp,sp))

sp, sp)
)
else
ConcreteMultiplication(f,sp)
end
end
@static if VERSION >= v"1.8"
Base.@constprop aggressive Multiplication(f::Fun{<:Chebyshev}, sp::Ultraspherical{Int}) =
_Multiplication(f, sp)
else
Multiplication(f::Fun{<:Chebyshev}, sp::Ultraspherical{Int}) = _Multiplication(f, sp)
end


## Derivative
Expand Down
44 changes: 33 additions & 11 deletions test/ChebyshevTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,19 +305,41 @@ using ApproxFunOrthogonalPolynomials: forwardrecurrence
@test a[1:nmin] ≈ b[1:nmin]
end

@testset "constant propagation in Dirichlet" begin
D = if VERSION >= v"1.8"
@inferred (r -> Dirichlet(r))(Chebyshev(0..1))
else
Dirichlet(Chebyshev(0..1))
@testset "Constant propagation" begin
@testset "Dirichlet" begin
D = if VERSION >= v"1.8"
@inferred (r -> Dirichlet(r))(Chebyshev(0..1))
else
Dirichlet(Chebyshev(0..1))
end
# Dirichlet constraints don't depend on the domain
D2 = Dirichlet(Chebyshev())
@test Matrix(D[:, 1:4]) == Matrix(D2[:, 1:4])

D = @inferred (() -> Dirichlet(Chebyshev(), 2))()
D2 = @inferred (() -> Dirichlet(Chebyshev(-1..1), 2))()
@test Matrix(D[:, 1:4]) == Matrix(D2[:, 1:4])
end

@testset "Multiplication" begin
f = () -> Fun(0..1) * Derivative(Chebyshev(0..1))
A = VERSION >= v"1.8" ? (@inferred f()) : f()
@test (A * Fun(0..1))(0.5) ≈ 0.5

f = () -> Fun() * Derivative(Chebyshev())
A = VERSION >= v"1.8" ? (@inferred f()) : f()
@test (A * Fun())(0.5) ≈ 0.5
end
# Dirichlet constraints don't depend on the domain
D2 = Dirichlet(Chebyshev())
@test Matrix(D[:, 1:4]) == Matrix(D2[:, 1:4])
end

@testset "Inference in PlusOperator" begin
f = () -> Derivative(Chebyshev(0..1)) + Derivative(Chebyshev(0..1))
A = VERSION >= v"1.8" ? (@inferred f()) : f()
@test (A * Fun(0..1))(0.5) ≈ 2.0

D = @inferred (() -> Dirichlet(Chebyshev(), 2))()
D2 = @inferred (() -> Dirichlet(Chebyshev(-1..1), 2))()
@test Matrix(D[:, 1:4]) == Matrix(D2[:, 1:4])
f = () -> Derivative(Chebyshev()) + Derivative(Chebyshev())
A = VERSION >= v"1.8" ? (@inferred f()) : f()
@test (A * Fun())(0.5) ≈ 2.0
end

@testset "Evaluation" begin
Expand Down
25 changes: 13 additions & 12 deletions test/UltrasphericalTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ using ApproxFunOrthogonalPolynomials: jacobip
end

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

NS = NormalizedPolynomialSpace(S)
fS = Fun(f, S)
Expand All @@ -63,16 +63,16 @@ using ApproxFunOrthogonalPolynomials: jacobip
L = ultra2leg(ApproxFunBase.canonicalspace(U))
NormalizedPolynomialSpace(L)
end
@testset for T in Any[Float32, Float64], ET in Any[T, complex(T)]
@testset for T in (Float32, Float64), ET in (T, complex(T))
v = Array{ET}(undef, 10)
v2 = similar(v)
M = Array{ET}(undef, 10, 10)
M2 = similar(M)
A = Array{ET}(undef, 10, 10, 10)
A2 = similar(A)
@testset for d in Any[(), (0..1,)], order in Any[0.5, 1, 3]
@testset for d in ((), (0..1,)), order in (0.5, 1, 3)
U = Ultraspherical(order, d...)
Slist = Any[U, NormalizedPolynomialSpace(U)]
Slist = (U, NormalizedPolynomialSpace(U))
@testset for S in Slist
if order == 0.5
L = ultra2leg(S)
Expand Down Expand Up @@ -100,6 +100,7 @@ using ApproxFunOrthogonalPolynomials: jacobip
test_transform!(A, A2, S)
end
end
endend
end
end

Expand All @@ -110,13 +111,13 @@ using ApproxFunOrthogonalPolynomials: jacobip

@testset "Evaluation" begin
c = [i^2 for i in 1:4]
@testset for d in Any[0..1, ChebyshevInterval()], order in Any[1, 2, 0.5]
@testset for _sp in Any[Ultraspherical(order), Ultraspherical(order,d)],
sp in Any[_sp, NormalizedPolynomialSpace(_sp)]
@testset for d in (0..1, ChebyshevInterval()), order in (1, 2, 0.5)
@testset for _sp in (Ultraspherical(order), Ultraspherical(order,d)),
sp in (_sp, NormalizedPolynomialSpace(_sp))
d = domain(sp)
f = Fun(sp, c)
for ep in [leftendpoint, rightendpoint],
ev in [ApproxFunBase.ConcreteEvaluation, Evaluation]
for ep in (leftendpoint, rightendpoint),
ev in (ApproxFunBase.ConcreteEvaluation, Evaluation)
E = @inferred ev(sp, ep, 0)
@test E[2:4] ≈ E[1:4][2:end]
@test E[1:2:5] ≈ E[1:5][1:2:5]
Expand Down
13 changes: 10 additions & 3 deletions test/miscAFBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ Base.:(==)(a::UniqueInterval, b::UniqueInterval) = (@assert a.parentinterval ==
end

@testset "inplace transform" begin
@testset for sp_c in Any[Legendre(), Chebyshev(), Jacobi(1,2), Jacobi(0.3, 2.3),
Ultraspherical(1), Ultraspherical(2)]
@testset for sp in Any[sp_c, NormalizedPolynomialSpace(sp_c)]
@testset for sp_c in (Legendre(), Chebyshev(), Jacobi(1,2), Jacobi(0.3, 2.3),
Ultraspherical(1), Ultraspherical(2))
@testset for sp in (sp_c, NormalizedPolynomialSpace(sp_c))
v = rand(10)
v2 = copy(v)
@test itransform!(sp, transform!(sp, v)) ≈ v
Expand Down Expand Up @@ -371,6 +371,13 @@ Base.:(==)(a::UniqueInterval, b::UniqueInterval) = (@assert a.parentinterval ==
@test (L * one(L))(x,y) ≈ L(x,y)
@test (L + zero(L))(x,y) ≈ L(x,y)
end

@testset "Multiplication" begin
# empty coefficients
f = () -> Multiplication(Fun(Chebyshev(), Float64[]), Ultraspherical(1))
M = VERSION >= v"1.8" ? (@inferred f()) : f()
@test all(iszero, coefficients(M * Fun()))
end
end

end # module