This commit is contained in:
Gaspard Jankowiak 2025-02-10 12:08:26 +01:00
commit 76136dbc05
7 changed files with 303 additions and 0 deletions

137
test/erf.jl Normal file
View file

@ -0,0 +1,137 @@
function compute_f1(p::Vector{Float64}; x::Float64=0.0)
L, μ, σ = p
sq2σ = sqrt(2) * σ
f1 = (x - (μ - 0.5L)) / sq2σ
return f1
end
function compute_J_f1(p::Vector{Float64}; x::Float64=0.0)
L, μ, σ = p
sq2σ = sqrt(2) * σ
J_f1 = [0.5 -1 -(x - (μ - 0.5L)) / σ] / sq2σ
return J_f1
end
function compute_H_f1(p::Vector{Float64}; x::Float64=0.0)
L, μ, σ = p
sq2σ = sqrt(2) * σ
J_f1 = [0.5 -1 -(x - (μ - 0.5L)) / σ] / sq2σ
H_f1 = zeros(3, 3)
H_f1[1, 3] = H_f1[3, 1] = -0.5 / (σ * sq2σ)
H_f1[2, 3] = H_f1[3, 2] = 1 / (σ * sq2σ)
H_f1[3, 3] = -2 * J_f1[3] / σ
return H_f1
end
function compute_f2(p::Vector{Float64}; x::Float64=0.0)
L, μ, σ = p
sq2σ = sqrt(2) * σ
f2 = (x - (μ + 0.5L)) / sq2σ
return f2
end
function compute_J_f2(p::Vector{Float64}; x::Float64=0.0)
L, μ, σ = p
sq2σ = sqrt(2) * σ
J_f2 = [-0.5 -1 -(x - (μ + 0.5L)) / σ] / sq2σ
return J_f2
end
function compute_H_f2(p::Vector{Float64}; x::Float64=0.0)
L, μ, σ = p
sq2σ = sqrt(2) * σ
J_f2 = [-0.5 -1 -(x - (μ + 0.5L)) / σ] / sq2σ
H_f2 = zeros(3, 3)
H_f2[1, 3] = H_f2[3, 1] = 0.5 / (σ * sq2σ)
H_f2[2, 3] = H_f2[3, 2] = 1 / (σ * sq2σ)
H_f2[3, 3] = -2 * J_f2[3] / σ
return H_f2
end
@testset "Erf aux functions" begin
L = 10 * (rand() - 0.5)
μ = 10 * (rand() - 0.5)
σ = 10rand()
x = σ * (2 * rand() - 0.5) + μ
p = [L, μ, σ]
@test TaylorTest.check(compute_f1, compute_J_f1, p; x=x)
@test TaylorTest.check(compute_J_f1, compute_H_f1, p; x=x)
@test TaylorTest.check(compute_f2, compute_J_f2, p; x=x)
@test TaylorTest.check(compute_J_f2, compute_H_f2, p; x=x)
end
@testset "Difference of error functions" begin
function diff_of_erf(p::Vector{Float64}; x::Float64=0.0)
L, μ, σ = p
f1 = compute_f1(p; x=x)
f2 = compute_f2(p; x=x)
return 0.5 * erf(f2, f1)
end
function J_diff_of_erf(p::Vector{Float64}; x::Float64=0.0)
# d/dx erf(x)/2 = gauss([sqrt(2), 0, 1]; x=x)
L, μ, σ = p
sq2σ = sqrt(2) * σ
# d/dξ ( erf( f1(x) ) - erf( f2(x) ) )/2 = d/dξ f1(x) gauss([sqrt(2), 0, 1]; x=f1(x)) - d/dξ f2(x) gauss([sqrt(2), 0, 1]; x=f2(x))
f1 = compute_f1(p; x=x)
f2 = compute_f2(p; x=x)
J_f1 = compute_J_f1(p; x=x)
J_f2 = compute_J_f2(p; x=x)
return J_f1 .* gauss([sqrt(2), 0, 1]; x=f1) - J_f2 .* gauss([sqrt(2), 0, 1]; x=f2)
end
function H_diff_of_erf(p::Vector{Float64}; x::Float64=0.0)
# d/dx erf(x)/2 = gauss([sqrt(2), 0, 1]; x=x)
L, μ, σ = p
sq2σ = sqrt(2) * σ
# d²/dξ² ( erf( f1(x) ) - erf( f2(x) ) )/2 = d²/dξ² f1(x) gauss([sqrt(2), 0, 1]; x=f1(x)) - d²/dξ² f2(x) gauss([sqrt(2), 0, 1]; x=f2(x))
# + (d/dξ f1(x))^2 gauss([sqrt(2), 0, 1]; x=f1(x)) - d²/dξ² f2(x) gauss([sqrt(2), 0, 1]; x=f2(x))
f1 = compute_f1(p; x=x)
f2 = compute_f2(p; x=x)
J_f1 = compute_J_f1(p; x=x)
J_f2 = compute_J_f2(p; x=x)
H_f1 = compute_H_f1(p; x=x)
H_f2 = compute_H_f2(p; x=x)
return (H_f1 .* gauss([sqrt(2), 0, 1]; x=f1) - H_f2 .* gauss([sqrt(2), 0, 1]; x=f2)
-
J_f1' * J_f1 .* f1 .* gauss([2 * sqrt(2), 0, 1]; x=f1) + J_f2' * J_f2 .* f2 .* gauss([2 * sqrt(2), 0, 1]; x=f2))
end
L = 10 * (rand() - 0.5)
μ = 10 * (rand() - 0.5)
σ = 10rand()
x = σ * (2 * rand() - 0.5) + μ
p = [L, μ, σ]
@test TaylorTest.check(diff_of_erf, J_diff_of_erf, p; x=x)
@test TaylorTest.check(J_diff_of_erf, H_diff_of_erf, p, [2, 3]; x=x)
end

50
test/gauss.jl Normal file
View file

@ -0,0 +1,50 @@
function J_gauss(p::Vector{Float64}; x::Float64=0.0)
λ, μ, σ = p
c = [1 / λ (2 * (x - μ) / σ^2) (-1 + 2 * ((x - μ) / σ)^2) / σ]
return c .* gauss(p; x=x)
end
function H_gauss(p::Vector{Float64}; x::Float64=0.0)
λ, μ, σ = p
c = zeros(3, 3)
# c[1,1] = 0
c[1, 2] = c[2, 1] = (2x) / (λ * σ^2) - (2 * μ) / (λ * σ^2)
c[1, 3] = c[3, 1] = (2 * (x - μ)^2 - σ^2) / (λ * σ^3)
c[2, 2] = (4 * (x - μ)^2 - 2 * σ^2) / σ^4
c[2, 3] = c[3, 2] = (2λ * (x - μ) * (2μ^2 - 3σ^2 + 2x^2 - 4μ * x)) / (λ * σ^5)
c[3, 3] = (2 * (σ^4 - 5σ^2 * (x - μ)^2 + 2 * (x - μ)^4)) / σ^6
return c .* gauss(p; x=x)
end
@testset "Gauss function" begin
λ = 10 * (rand() - 0.5)
μ = 10 * (rand() - 0.5)
σ = 10rand()
x = σ * (2 * rand() - 0.5) + μ
p = [λ, μ, σ]
@test TaylorTest.check(gauss, J_gauss, p; x=x)
@test TaylorTest.check(J_gauss, H_gauss, p; x=x)
end
@testset "Composed Gauss function" begin
p = 1 .+ 5 * rand(6)
x = p[3] + p[4] + 2p[5] + sqrt(p[6]) * (2rand() - 1)
p_aux = _p -> [_p[1]/_p[2], _p[3] + _p[4] + 2_p[5], _p[6]]
J_p_aux = _p -> [1/_p[2] -_p[1]/_p[2]^2 0 0 0 0;
0 0 1 1 2 0;
0 0 0 0 0 1]
g = (_p; x=x) -> gauss(p_aux(_p); x=x)
Jg = (_p; x=x) -> J_gauss(p_aux(_p); x=x) * J_p_aux(p)
Hg = (_p; x=x) -> J_gauss(p_aux(_p); x=x) * J_p_aux(p)
@test TaylorTest.check(g, Jg, p; x=x)
end

15
test/runtests.jl Normal file
View file

@ -0,0 +1,15 @@
import Test: @test, @testset
import TaylorTest
import SpecialFunctions: erf
function gauss(p::Vector{Float64}; x::Float64=0.0)
λ, μ, σ = p
return λ / sqrt(2π * σ^2) * exp(-((x - μ) / σ)^2)
end
include("trig_functions.jl")
include("gauss.jl")
include("erf.jl")
include("tensors.jl")

28
test/tensors.jl Normal file
View file

@ -0,0 +1,28 @@
compute_f = x -> [x[1]^2; x[2]^2 + x[3]; -x[3]^2 + x[4] + x[5]^3]
compute_Jf = x -> [
2x[1] 0 0 0 0;
0 2x[2] 1 0 0;
0 0 -2x[3] 1 3*x[5]^2
]
function compute_Hf(x)
Hf = zeros(3, 5, 5)
Hf[1, 1, 1] = 2
Hf[2, 2, 2] = 2
Hf[3, 3, 3] = -2
Hf[3, 5, 5] = 6 * x[5]
return Hf
end
@testset "Tensors" begin
m, n = 3, 5
x = 2rand(n) .- 0.5
@test TaylorTest.check(compute_f, compute_Jf, x)
@test TaylorTest.check(compute_Jf, compute_Hf, x)
end

4
test/trig_functions.jl Normal file
View file

@ -0,0 +1,4 @@
@testset "trigonometric functions" begin
@test TaylorTest.check(x -> cos(x), x -> -sin(x), 2π * rand())
@test TaylorTest.check(x -> sin(x), x -> cos(x), 2π * rand())
end