TaylorTest/test/erf.jl
2025-02-10 12:08:26 +01:00

137 lines
3.4 KiB
Julia
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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