Skip to content

Commit 8305d11

Browse files
authored
Improve type-inference in evaluate (#276)
* improve type-inference in evaluate * Test for BigInt * Bump ApproxFunBase version * Call clenshaw in each branch in evaluate
1 parent dc3d51c commit 8305d11

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

Project.toml

Lines changed: 5 additions & 3 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.6.37"
3+
version = "0.6.38"
44

55
[deps]
66
ApproxFunBase = "fbd15aa5-315a-5a7d-a8a4-24992e37be05"
@@ -28,13 +28,14 @@ Static = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
2828
ApproxFunOrthogonalPolynomialsStaticExt = "Static"
2929

3030
[compat]
31-
ApproxFunBase = "0.8.30"
31+
ApproxFunBase = "0.8.39"
3232
ApproxFunBaseTest = "0.1"
3333
Aqua = "0.6"
3434
BandedMatrices = "0.16, 0.17"
3535
BlockArrays = "0.14, 0.15, 0.16"
3636
BlockBandedMatrices = "0.10, 0.11, 0.12"
3737
DomainSets = "0.5, 0.6"
38+
DualNumbers = "0.6.2"
3839
FastGaussQuadrature = "0.4, 0.5"
3940
FastTransforms = "0.12, 0.13, 0.14, 0.15.1"
4041
FillArrays = "0.11, 0.12, 0.13, 1"
@@ -51,10 +52,11 @@ julia = "1.6"
5152
[extras]
5253
ApproxFunBaseTest = "a931bfaf-0cfd-4a5c-b69c-bf2eed002b43"
5354
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
55+
DualNumbers = "fa6b7ba4-c1ee-5f82-b5fc-ecf0adba8f74"
5456
LazyArrays = "5078a376-72f3-5289-bfd5-ec5146d43c02"
5557
Static = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
5658
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
5759
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
5860

5961
[targets]
60-
test = ["ApproxFunBaseTest", "Aqua", "Test", "Static", "StaticArrays", "LazyArrays"]
62+
test = ["ApproxFunBaseTest", "Aqua", "DualNumbers", "Test", "Static", "StaticArrays", "LazyArrays"]

src/Spaces/PolynomialSpace.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@ rangespace(M::ConcreteMultiplication{U,V}) where {U<:PolynomialSpace,V<:Polynomi
1717
## Evaluation
1818

1919
function evaluate(f::AbstractVector,S::PolynomialSpace,x)
20+
# We call clenshaw in each branch to obtain type-stability
21+
y = tocanonical(S,x)
2022
if x in domain(S)
21-
clenshaw(S,f,tocanonical(S,x))
23+
clenshaw(S,f,y)
2224
elseif isambiguous(domain(S))
23-
length(f) == 0 && return zero(eltype(f))
25+
length(f) == 0 && return clenshaw(S, SVector{0,eltype(f)}(),y)
2426
for k = 2:length(f)
2527
iszero(f[k]) || throw(ArgumentError("Ambiguous domains only work with constants"))
2628
end
27-
return first(f)
29+
# type-stable way to evaluate the first element of the series
30+
return clenshaw(S, SVector{1}(f[1]),y)
2831
else
29-
zero(eltype(f))
32+
# type-stable way to obtain the zero element
33+
clenshaw(S, SVector{0,eltype(f)}(),y)
3034
end
3135
end
3236

test/ChebyshevTest.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module ChebyshevTest
22

33
using ApproxFunOrthogonalPolynomials
44
using ApproxFunBase
5+
using DualNumbers
56
using LinearAlgebra
67
using Test
78
using ApproxFunBase: transform!, itransform!
@@ -24,6 +25,12 @@ include("testutils.jl")
2425
@test Fun(x->4).coefficients == [4.0]
2526
@test @inferred(Fun(4)).coefficients == [4.0]
2627

28+
@test @inferred(Fun(Chebyshev(), [1])(1)) == 1.0
29+
@test @inferred(Fun(Chebyshev(), Int[])(Dual(1,1))) == Dual(0,0)
30+
@test @inferred(Fun(Chebyshev(), Int[1,2])(Dual(1,1))) == Dual(3,2)
31+
32+
@test @inferred(Fun(Chebyshev(), BigInt[1])(1)) == 1
33+
2734
f = @inferred Fun(ChebyshevInterval(), [1])
2835
@test f(0.1) == 1
2936

test/MultivariateTest.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module MultivariateTest
22

33
using ApproxFunBase
44
using ApproxFunOrthogonalPolynomials
5+
using DualNumbers
56
using LinearAlgebra
67
using SpecialFunctions
78
using BlockBandedMatrices
@@ -235,6 +236,8 @@ include("testutils.jl")
235236
f=Fun(xy->exp(-xy[1]-2cos(xy[2])),d)
236237
@test f(0.5,0.5) exp(-0.5-2cos(0.5))
237238
@test f(SVector(0.5,0.5)) exp(-0.5-2cos(0.5))
239+
@test @inferred(first(f)) f(0,0)
240+
@test @inferred(last(f)) f(1,1)
238241

239242
f=Fun(xy->exp(-xy[1]-2cos(xy[2])),d,20)
240243
@test f(0.5,0.5) exp(-0.5-2cos(0.5))
@@ -405,8 +408,9 @@ include("testutils.jl")
405408

406409
@testset "off domain evaluate" begin
407410
g = Fun(1, Segment(SVector(0,-1) , SVector(π,-1)))
408-
@test g(0.1,-1) 1
409-
@test g(0.1,1) 0
411+
@test @inferred(g(0.1,-1)) 1
412+
@test @inferred(g(Dual(1,1),-1)) == Dual(1,0)
413+
@test @inferred(g(0.1,1)) 0
410414
end
411415

412416
@testset "Dirichlet" begin

0 commit comments

Comments
 (0)