Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SpecialFunctions extension #82

Merged
merged 18 commits into from
May 22, 2024
Merged
Prev Previous commit
Next Next commit
Add tests
  • Loading branch information
gdalle committed May 22, 2024
commit b2dc872c251f49247fdf2f2a2470d37e52d0b804
9 changes: 9 additions & 0 deletions test/first_order.jl
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ using SparseConnectivityTracer:
using SparseConnectivityTracer: DuplicateVector, RecursiveSet, SortedVector
using ADTypes: jacobian_sparsity
using LinearAlgebra: det, dot, logdet
using SpecialFunctions: erf, beta
using Test

const FIRST_ORDER_SET_TYPES = (
@@ -34,6 +35,10 @@ const FIRST_ORDER_SET_TYPES = (
@test connectivity_pattern(x -> ℯ^x, 1, G) ≈ [1;;]
@test connectivity_pattern(x -> round(x, RoundNearestTiesUp), 1, G) ≈ [1;;]

# SpecialFunctions
@test connectivity_pattern(x -> erf(x), 1, G) == [1;;]
@test connectivity_pattern(x -> beta(x[1], x[2]), rand(3), G) == [1 1 0]

## Error handling when applying non-dual tracers to "local" functions with control flow
@test_throws MissingPrimalError connectivity_pattern(
x -> ifelse(x[2] < x[3], x[1] + x[2], x[3] * x[4]), [1 2 3 4], G
@@ -78,6 +83,10 @@ end
# Linear Algebra
@test jacobian_sparsity(x -> dot(x[1:2], x[4:5]), rand(5), method) == [1 1 0 1 1]

# SpecialFunctions
@test jacobian_sparsity(x -> erf(x), 1, method) == [1;;]
@test jacobian_sparsity(x -> beta(x[1], x[2]), rand(3), method) == [1 1 0]

## Error handling when applying non-dual tracers to "local" functions with control flow
@test_throws MissingPrimalError jacobian_sparsity(
x -> x[1] > x[2] ? x[3] : x[4], [1.0, 2.0, 3.0, 4.0], method
9 changes: 9 additions & 0 deletions test/second_order.jl
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ using SparseConnectivityTracer:
HessianTracer, MissingPrimalError, tracer, trace_input, empty
using SparseConnectivityTracer: DuplicateVector, RecursiveSet, SortedVector
using ADTypes: hessian_sparsity
using SpecialFunctions: erf, beta
using Test

const SECOND_ORDER_SET_TYPES = (
@@ -145,6 +146,14 @@ const SECOND_ORDER_SET_TYPES = (
0 1 0 0 1
]

# SpecialFunctions
@test hessian_sparsity(x -> erf(x), 1, method) == [1;;]
@test hessian_sparsity(x -> beta(x[1], x[2]), rand(3), method) == [
1 1 0
1 1 0
0 0 0
]

## Error handling when applying non-dual tracers to "local" functions with control flow
f2(x) = ifelse(x[2] < x[3], x[1] * x[2], x[3] * x[4])
@test_throws MissingPrimalError hessian_sparsity(f2, [1 3 2 4], method)
Loading