|
| 1 | +using LinearAlgebra, Jets, JetPack, Test |
| 2 | + |
| 3 | +n1,n2 = 33,44 |
| 4 | + |
| 5 | +@testset "JopTanh, correctness T=$(T)" for T in (Float64,Float32,Complex{Float64},Complex{Float32}) |
| 6 | + F = JopTanh(JetSpace(T,n1,n2)) |
| 7 | + x1 = rand(domain(F)) |
| 8 | + @test F*x1 ≈ tanh.(x1) |
| 9 | +end |
| 10 | + |
| 11 | +@testset "JopTanh, linearity test, T=$(T)" for T in (Float64,Float32,Complex{Float64},Complex{Float32}) |
| 12 | + F = JopTanh(JetSpace(T,n1,n2)) |
| 13 | + J = jacobian(F, rand(domain(F)) ) |
| 14 | + lhs,rhs = linearity_test(J) |
| 15 | + @test lhs ≈ rhs |
| 16 | +end |
| 17 | + |
| 18 | +@testset "JopTanh, dot product test, T=$(T)" for T in (Float64,Float32,Complex{Float64},Complex{Float32}) |
| 19 | + F = JopTanh(JetSpace(T,n1,n2)) |
| 20 | + J = jacobian!(F, rand(domain(F)) ) |
| 21 | + lhs, rhs = dot_product_test(J, -1 .+ 2 .* rand(domain(J)), -1 .+ 2 .* rand(range(J))) |
| 22 | + @test lhs ≈ rhs |
| 23 | +end |
| 24 | + |
| 25 | +# note the key here is to increase the size of the nonlinear vector |
| 26 | +@testset "JopTanh, linearization test, T=$(T)" for T in (Float64,Float32,Complex{Float64},Complex{Float32}) |
| 27 | + F = JopTanh(JetSpace(T,n1,n2)) |
| 28 | + m0 = -0.5 .* rand(domain(F)) |
| 29 | + μ = sqrt.([1/1,1/2,1/4,1/8,1/16,1/32,1/64,1/128,1/256,1/512,1/1024,1/2048]) |
| 30 | + δm = rand(domain(F)) |
| 31 | + observed, expected = linearization_test(F, m0, μ = μ, δm = δm) |
| 32 | + δ = minimum(abs, observed - expected) |
| 33 | + @test δ < 0.1 |
| 34 | +end |
| 35 | + |
| 36 | +@testset "JopTanh, odd funcion T=$(T)" for T in (Float64,Float32,Complex{Float64},Complex{Float32}) |
| 37 | + F = JopTanh(JetSpace(T,n1,n2)) |
| 38 | + x1 = 2 .* (-1 .+ 2 .* rand(domain(F))) |
| 39 | + @test F * (-1 .* x1) ≈ -(F * x1) |
| 40 | +end |
0 commit comments