-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic.jl
More file actions
69 lines (52 loc) · 1.42 KB
/
basic.jl
File metadata and controls
69 lines (52 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Test
using Ariadne
function F!(res, x, _)
res[1] = x[1]^2 + x[2]^2 - 2
return res[2] = exp(x[1] - 1) + x[2]^2 - 2
end
function F(x, p)
res = similar(x)
F!(res, x, p)
return res
end
let x₀ = [2.0, 0.5]
x, stats = newton_krylov!(F!, x₀)
@test stats.solved
end
let x₀ = [3.0, 5.0]
x, stats = newton_krylov(F, x₀)
@test stats.solved
end
import Ariadne: JacobianOperator
using Enzyme, LinearAlgebra
using ADTypes
@testset "Enzyme: JacobianOperator" begin
J_Enz = jacobian(Forward, x -> F(x, nothing), [3.0, 5.0]) |> only
J = JacobianOperator(F!, zeros(2), [3.0, 5.0], nothing)
@test size(J) == (2, 2)
@test length(J) == 4
@test eltype(J) == Float64
out = [NaN, NaN]
mul!(out, J, [1.0, 0.0])
@test out == [6.0, 7.38905609893065]
out = [NaN, NaN]
mul!(out, transpose(J), [1.0, 0.0])
@test out == [6.0, 10.0]
J_NK = collect(J)
@test J_NK == J_Enz
v = rand(2)
out = similar(v)
mul!(out, J, v)
@test out ≈ J_Enz * v
@test collect(transpose(J)) == transpose(collect(J))
end
@testset "DifferentiationInterface: JacobianOperator" begin
backend = ADTypes.AutoEnzyme()
J = Ariadne.DIJacobianOperator(backend, F!, zeros(2), [3.0, 5.0], nothing)
@test size(J) == (2, 2)
@test length(J) == 4
@test eltype(J) == Float64
out = [NaN, NaN]
mul!(out, J, [1.0, 0.0])
@test out == [6.0, 7.38905609893065]
end