Skip to content

Commit 7151a98

Browse files
authored
Improve inference in norm (#194)
* improve inference in norm * version bump to v0.6.20 * add abs in norm * specialize norm for VectorFun * extend norm only for ScalarFun * fix default norm * add test
1 parent 936a633 commit 7151a98

File tree

4 files changed

+9
-5
lines changed

4 files changed

+9
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ApproxFunBase"
22
uuid = "fbd15aa5-315a-5a7d-a8a4-24992e37be05"
3-
version = "0.6.19"
3+
version = "0.6.20"
44

55
[deps]
66
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

src/Fun.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,10 @@ for (OP,SUM) in ((:(norm),:(sum)),(:linenorm,:linesum))
414414
@eval begin
415415
$OP(f::Fun) = $OP(f,2)
416416

417-
function $OP(f::Fun{<:Space{<:Any,<:Number}}, p::Real)
417+
# Specializing norm(::ScalarFun) helps with inference
418+
$OP(f::ScalarFun) = sqrt(abs($SUM(abs2(f))))
419+
420+
function $OP(f::ScalarFun, p::Real)
418421
if p < 1
419422
return error("p should be 1 ≤ p ≤ ∞")
420423
elseif 1 p < Inf
@@ -424,11 +427,12 @@ for (OP,SUM) in ((:(norm),:(sum)),(:linenorm,:linesum))
424427
end
425428
end
426429

427-
function $OP(f::Fun{<:Space{<:Any,<:Number}}, p::Int)
430+
function $OP(f::ScalarFun, p::Int)
428431
if 1 p < Inf
432+
p == 2 && return $OP(f)
429433
return iseven(p) ? abs($SUM(abs2(f)^(p÷2)))^(1/p) : abs($SUM(abs2(f)^(p/2)))^(1/p)
430434
else
431-
return error("p should be 1 ≤ p ≤ ∞")
435+
error("p should be 1 ≤ p ≤ ∞")
432436
end
433437
end
434438
end

src/Multivariate/VectorFun.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ norm(A::VectorFun, p::Real) = norm(norm.(Array(A)),p)
143143

144144

145145

146-
147146
## Vector of fun routines
148147

149148
function coefficientmatrix(::Type{N},f::AbstractVector{F},o...) where {N,F}

test/SpacesTest.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ using LinearAlgebra
252252

253253
@testset "ApproxFunOrthogonalPolynomials" begin
254254
@test (@inferred Fun()) == Fun(x->x, Chebyshev())
255+
@test (@inferred norm(Fun())) norm(Fun(), 2) (2/3) # √∫x^2 dx over -1..1
255256

256257
v = rand(4)
257258
v2 = transform(NormalizedChebyshev(), v)

0 commit comments

Comments
 (0)