-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LogExpFunctions package extension (#184)
- Loading branch information
Showing
7 changed files
with
189 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
module SparseConnectivityTracerLogExpFunctionsExt | ||
|
||
if isdefined(Base, :get_extension) | ||
import SparseConnectivityTracer as SCT | ||
using LogExpFunctions | ||
else | ||
import ..SparseConnectivityTracer as SCT | ||
using ..LogExpFunctions | ||
end | ||
|
||
# 1-to-1 functions | ||
|
||
ops_1_to_1 = ( | ||
xlogx, | ||
xexpx, | ||
logistic, | ||
logit, | ||
logcosh, | ||
logabssinh, | ||
log1psq, | ||
log1pexp, | ||
log1mexp, | ||
log2mexp, | ||
logexpm1, | ||
softplus, | ||
invsoftplus, | ||
log1pmx, | ||
logmxp1, | ||
cloglog, | ||
cexpexp, | ||
loglogistic, | ||
logitexp, | ||
log1mlogistic, | ||
logit1mexp, | ||
) | ||
|
||
for op in ops_1_to_1 | ||
T = typeof(op) | ||
@eval SCT.is_der1_zero_global(::$T) = false | ||
@eval SCT.is_der2_zero_global(::$T) = false | ||
end | ||
|
||
# 2-to-1 functions | ||
|
||
ops_2_to_1 = (xlogy, xlog1py, xexpy, logaddexp, logsubexp) | ||
|
||
for op in ops_2_to_1 | ||
T = typeof(op) | ||
@eval SCT.is_der1_arg1_zero_global(::$T) = false | ||
@eval SCT.is_der2_arg1_zero_global(::$T) = false | ||
@eval SCT.is_der1_arg2_zero_global(::$T) = false | ||
@eval SCT.is_der2_arg2_zero_global(::$T) = false | ||
@eval SCT.is_der_cross_zero_global(::$T) = false | ||
end | ||
|
||
# Generate overloads | ||
eval(SCT.generate_code_1_to_1(:LogExpFunctions, ops_1_to_1)) | ||
eval(SCT.generate_code_2_to_1(:LogExpFunctions, ops_2_to_1)) | ||
|
||
# List operators for later testing | ||
SCT.test_operators_1_to_1(::Val{:LogExpFunctions}) = ops_1_to_1 | ||
SCT.test_operators_2_to_1(::Val{:LogExpFunctions}) = ops_2_to_1 | ||
SCT.test_operators_1_to_2(::Val{:LogExpFunctions}) = () | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using SparseConnectivityTracer | ||
using LogExpFunctions | ||
using Test | ||
|
||
# Load definitions of GRADIENT_TRACERS, GRADIENT_PATTERNS, HESSIAN_TRACERS and HESSIAN_PATTERNS | ||
include("../tracers_definitions.jl") | ||
|
||
lef_1_to_1_pos_input = ( | ||
xlogx, | ||
logistic, | ||
logit, | ||
log1psq, | ||
log1pexp, | ||
logexpm1, | ||
softplus, | ||
invsoftplus, | ||
log1pmx, | ||
logmxp1, | ||
logcosh, | ||
logabssinh, | ||
cloglog, | ||
cexpexp, | ||
loglogistic, | ||
log1mlogistic, | ||
) | ||
lef_1_to_1_neg_input = (log1mexp, log2mexp, logitexp, logit1mexp) | ||
lef_1_to_1 = union(lef_1_to_1_pos_input, lef_1_to_1_neg_input) | ||
lef_2_to_1 = (xlogy, xlog1py, xexpy, logaddexp, logsubexp) | ||
|
||
@testset "Jacobian Global" begin | ||
method = TracerSparsityDetector() | ||
J(f, x) = jacobian_sparsity(f, x, method) | ||
|
||
@testset "1-to-1 functions" begin | ||
@testset "$f" for f in lef_1_to_1 | ||
@test J(x -> f(x[1]), rand(2)) == [1 0] | ||
end | ||
end | ||
@testset "2-to-1 functions" begin | ||
@testset "$f" for f in lef_2_to_1 | ||
@test J(x -> f(x[1], x[2]), rand(3)) == [1 1 0] | ||
end | ||
end | ||
end | ||
|
||
@testset "Jacobian Local" begin | ||
method = TracerLocalSparsityDetector() | ||
J(f, x) = jacobian_sparsity(f, x, method) | ||
|
||
@testset "1-to-1 functions" begin | ||
@testset "$f" for f in lef_1_to_1_pos_input | ||
@test J(x -> f(x[1]), [0.5, 1.0]) == [1 0] | ||
end | ||
@testset "$f" for f in lef_1_to_1_neg_input | ||
@test J(x -> f(x[1]), [-0.5, 1.0]) == [1 0] | ||
end | ||
end | ||
@testset "2-to-1 functions" begin | ||
@testset "$f" for f in lef_2_to_1 | ||
@test J(x -> f(x[1], x[2]), [0.5, 1.0, 2.0]) == [1 1 0] | ||
end | ||
end | ||
end | ||
|
||
@testset "Hessian Global" begin | ||
method = TracerSparsityDetector() | ||
H(f, x) = hessian_sparsity(f, x, method) | ||
|
||
@testset "1-to-1 functions" begin | ||
@testset "$f" for f in lef_1_to_1 | ||
@test H(x -> f(x[1]), rand(2)) == [1 0; 0 0] | ||
end | ||
end | ||
@testset "2-to-1 functions" begin | ||
@testset "$f" for f in lef_2_to_1 | ||
@test H(x -> f(x[1], x[2]), rand(3)) == [1 1 0; 1 1 0; 0 0 0] | ||
end | ||
end | ||
end | ||
|
||
@testset "Hessian Local" begin | ||
method = TracerLocalSparsityDetector() | ||
H(f, x) = hessian_sparsity(f, x, method) | ||
|
||
@testset "1-to-1 functions" begin | ||
@testset "$f" for f in lef_1_to_1_pos_input | ||
@test H(x -> f(x[1]), [0.5, 1.0]) == [1 0; 0 0] | ||
end | ||
@testset "$f" for f in lef_1_to_1_neg_input | ||
@test H(x -> f(x[1]), [-0.5, 1.0]) == [1 0; 0 0] | ||
end | ||
end | ||
@testset "2-to-1 functions" begin | ||
@testset "$f" for f in lef_2_to_1 | ||
@test H(x -> f(x[1], x[2]), [0.5, 1.0, 2.0]) == [1 1 0; 1 1 0; 0 0 0] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters