From 79fc414ce3ca32d4a4daabdec023e50c79fe222f Mon Sep 17 00:00:00 2001 From: Josh Day Date: Thu, 2 Aug 2018 15:10:38 -0400 Subject: [PATCH] fix calling, add to README, add tests --- README.md | 4 ++++ src/PenaltyFunctions.jl | 2 +- test/runtests.jl | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7b48fe5..a1eb2c4 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,10 @@ value(p, x[1], s[1]) # evaluate on element, scaled by scalar value(p, x, s[1]) # evaluate on array, scaled by scalar value(p, x, s) # evaluate on array, element-wise scaling +# value via calling the Penalty object +p = L1Penalty() +p([1,2,3]) + # derivatives and gradients deriv(p, x[1]) # derivative deriv(p, x[1], s[1]) # scaled derivative diff --git a/src/PenaltyFunctions.jl b/src/PenaltyFunctions.jl index 221d42e..868942e 100644 --- a/src/PenaltyFunctions.jl +++ b/src/PenaltyFunctions.jl @@ -61,7 +61,7 @@ include("elementpenalty.jl") include("arraypenalty.jl") # Make Penalties Callable -for T in filter(isconcretetype, union(subtypes(ElementPenalty), +for T in filter(!isabstracttype, union(subtypes(ElementPenalty), subtypes(ProxableElementPenalty), subtypes(ArrayPenalty))) @eval (pen::$T)(args...) = value(pen, args...) diff --git a/test/runtests.jl b/test/runtests.jl index a5b086a..d34a89c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -37,6 +37,8 @@ end @test r(@inferred(deriv(p, θ))) ≈ r(v2) @test r.(value.(Ref(p), fill(θ, 5))) ≈ r.(fill(v1, 5)) @test r.(deriv.(Ref(p), fill(θ, 5))) ≈ r.(fill(v2, 5)) + @test value(p, θ) == p(θ) + @test value(p, θ, s) == p(θ, s) if isa(p, P.ProxableElementPenalty) @test r(@inferred(prox(p, θ, s))) ≈ r(v3) @test r.(prox.(Ref(p), fill(θ, 5), Ref(s))) ≈ r.(fill(v3, 5)) @@ -195,6 +197,7 @@ end s = .05 # FIXME: @inference broken. seems like a type instability @test value(p, Θ) ≈ sum(svd(Θ).S) + @test value(p, Θ) == p(Θ) @test value(p, Θ, s) ≈ s * sum(svd(Θ).S) prox!(p, Θ, s) end @@ -203,6 +206,7 @@ end Θ = randn(10) s = .05 @test @inferred(value(p, Θ)) ≈ norm(Θ) + @test value(p, Θ) == p(Θ) prox!(p, Θ, s) Θ = .01 * ones(10) @@ -212,6 +216,7 @@ end C = randn(5, 10) p = MahalanobisPenalty(C) θ = rand(10) + @test value(p, θ) == p(θ) s = .05 @test @inferred(value(p, θ)) ≈ 0.5 * dot(C * θ, C * θ) prox!(p, θ, s) @@ -220,6 +225,7 @@ end p = GroupLassoPenalty() s = scaled(p, .1) Θ = randn(10) + @test value(p, Θ) == p(Θ) @test @inferred(value(p, Θ, .1)) ≈ value(s, Θ) Θ2 = copy(Θ)