Skip to content

Commit 8694bb3

Browse files
authored
Make the Fun constructor type-inferrable (#103)
* parameterized dynamic function * Add tests
1 parent 3374258 commit 8694bb3

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

src/LinearAlgebra/helper.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,8 @@ end
640640

641641
## Dynamic functions
642642

643-
struct DFunction <: Function
644-
f
643+
struct DFunction{F} <: Function
644+
f :: F
645645
end
646646
(f::DFunction)(args...) = f.f(args...)
647647

src/constructors.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ Fun(c::Number,d::Space) = c==0 ? c*zeros(prectype(d),d) : c*ones(prectype(d),d)
9797

9898
## Adaptive constructors
9999
function default_Fun(f, d::Space)
100-
hasnumargs(f,1) || return Fun(xy->f(xy...),d)
100+
_default_Fun(hasnumargs(f, 1) ? f : Base.splat(f), d)
101+
end
102+
# In _default_Fun, we know that the function takes a single argument
103+
function _default_Fun(f, d::Space)
101104
isinf(dimension(d)) || return Fun(f,d,dimension(d)) # use exactly dimension number of sample points
102105

103106
#TODO: reuse function values?
@@ -108,11 +111,8 @@ function default_Fun(f, d::Space)
108111

109112
isa(f0,AbstractArray) && size(d) size(f0) && return Fun(f,Space(fill(d,size(f0))))
110113

111-
112-
113114
tol =T==Any ? 20eps() : 20eps(T)
114115

115-
116116
fr=map(f,r)
117117
maxabsfr=norm(fr,Inf)
118118

@@ -129,7 +129,7 @@ function default_Fun(f, d::Space)
129129

130130
# we allow for transformed coefficients being a different size
131131
##TODO: how to do scaling for unnormalized bases like Jacobi?
132-
if ncoefficients(cf) > 8 && maximum(abs,cf.coefficients[bs:end]) < 10tol*maxabsc &&
132+
if ncoefficients(cf) > 8 && maximum(abs, @view cf.coefficients[bs:end]) < 10tol*maxabsc &&
133133
all(k->norm(cf(r[k])-fr[k],1)<tol*length(cf.coefficients)*maxabsfr*1000,1:length(r))
134134
return chop!(cf,tol)
135135
end

test/SpacesTest.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import ApproxFunBase: PointSpace, HeavisideSpace, PiecewiseSegment, dimension, V
55
@testset "PointSpace" begin
66
@test eltype(domain(PointSpace([0,0.1,1])) ) == Float64
77

8-
f=Fun(x->(x-0.1),PointSpace([0,0.1,1]))
8+
f = @inferred Fun(x->(x-0.1),PointSpace([0,0.1,1]))
99
@test roots(f) == [0.1]
1010

11-
a=Fun(exp,space(f))
12-
@test f/a == Fun(x->(x-0.1)*exp(-x),space(f))
11+
a = @inferred Fun(exp, space(f))
12+
@test f/a == @inferred Fun(x->(x-0.1)*exp(-x),space(f))
1313

1414
f = Fun(space(f),[1.,2.,3.])
1515

0 commit comments

Comments
 (0)