@@ -3,12 +3,13 @@ using LinearAlgebra, Test
33
44using Random
55Random. seed! (123 )
6-
7- const A = rand (300 , 300 )
6+ N = 300
7+ const A = rand (N, N )
88f (y, x) = mul! (y, A, x)
99f (x) = A * x
10- x = rand (300 )
11- v = rand (300 )
10+ x = rand (N)
11+ v = rand (N)
12+ a, b = rand (2 )
1213dy = similar (x)
1314g (x) = sum (abs2, x)
1415function h (x)
2021
2122cache1 = ForwardDiff. Dual{typeof (ForwardDiff. Tag (SparseDiffTools. DeivVecTag (), eltype (x))),
2223 eltype (x), 1 }. (x, ForwardDiff. Partials .(Tuple .(v)))
23- cache2 = ForwardDiff. Dual{typeof (ForwardDiff. Tag (SparseDiffTools. DeivVecTag (), eltype (x))),
24- eltype (x), 1 }. (x, ForwardDiff. Partials .(Tuple .(v)))
24+ cache2 = ForwardDiff. Dual {typeof(ForwardDiff.Tag(SparseDiffTools.DeivVecTag(), eltype(x))), eltype(x), 1} .(x, ForwardDiff. Partials .(Tuple .(v)))
2525@test num_jacvec! (dy, f, x, v)≈ ForwardDiff. jacobian (f, similar (x), x) * v rtol= 1e-6
2626@test num_jacvec! (dy, f, x, v, similar (v),
2727 similar (v))≈ ForwardDiff. jacobian (f, similar (x), x) * v rtol= 1e-6
@@ -65,61 +65,90 @@ cache4 = ForwardDiff.Dual{typeof(ForwardDiff.Tag(Nothing, eltype(x))), eltype(x)
6565@test auto_hesvecgrad! (dy, h, x, v, cache1, cache2)≈ ForwardDiff. hessian (g, x) * v rtol= 1e-2
6666@test auto_hesvecgrad (h, x, v)≈ ForwardDiff. hessian (g, x) * v rtol= 1e-2
6767
68+ # JacVec
69+
6870L = JacVec (f, x)
6971@test L * x ≈ auto_jacvec (f, x, x)
7072@test L * v ≈ auto_jacvec (f, x, v)
7173@test mul! (dy, L, v) ≈ auto_jacvec (f, x, v)
72- L. x .= v
74+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b) ≈ a* auto_jacvec (f,x,v) + b* _dy
75+ update_coefficients! (L, v, nothing , 0.0 )
7376@test mul! (dy, L, v) ≈ auto_jacvec (f, v, v)
77+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b) ≈ a* auto_jacvec (f,x,v) + b* _dy
7478
7579L = JacVec (f, x, autodiff = false )
7680@test L * x ≈ num_jacvec (f, x, x)
7781@test L * v ≈ num_jacvec (f, x, v)
78- L. x == x
7982@test mul! (dy, L, v)≈ num_jacvec (f, x, v) rtol= 1e-6
80- L. x .= v
83+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b) ≈ a* num_jacvec (f,x,v) + b* _dy rtol= 1e-6
84+ update_coefficients! (L, v, nothing , 0.0 )
8185@test mul! (dy, L, v)≈ num_jacvec (f, v, v) rtol= 1e-6
86+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b) ≈ a* num_jacvec (f,x,v) + b* _dy rtol= 1e-6
8287
83- # ## Integration test with IterativeSolvers
8488out = similar (v)
8589gmres! (out, L, v)
8690
87- x = rand (300 )
88- v = rand (300 )
91+ #=
92+ ff1 = ODEFunction(lorenz, jac_prototype = JacVec{Float64}(lorenz, u0))
93+ ff2 = ODEFunction(lorenz, jac_prototype = JacVec{Float64}(lorenz, u0, autodiff=false))
94+
95+ for ff in [ff1, ff2]
96+ prob = ODEProblem(ff, u0, tspan)
97+ @test solve(prob, TRBDF2()).retcode == :Success
98+ @test solve(prob, TRBDF2(linsolve = KrylovJL_GMRES())).retcode == :Success
99+ @test solve(prob, Exprb32()).retcode == :Success
100+ @test solve(prob, Rosenbrock23()).retcode == :Success
101+ @test solve(prob, Rosenbrock23(linsolve = KrylovJL_GMRES())).retcode == :Success
102+ end
103+ =#
104+
105+ # HesVec
106+
107+ x = rand (N)
108+ v = rand (N)
89109L = HesVec (g, x, autodiff = false )
90110@test L * x ≈ num_hesvec (g, x, x)
91111@test L * v ≈ num_hesvec (g, x, v)
92112@test mul! (dy, L, v)≈ num_hesvec (g, x, v) rtol= 1e-2
93- L. x .= v
113+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b) ≈ a* num_hesvec (g,x,v) + b* _dy rtol= 1e-2
114+ update_coefficients! (L, v, nothing , 0.0 )
94115@test mul! (dy, L, v)≈ num_hesvec (g, v, v) rtol= 1e-2
116+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b) ≈ a* num_hesvec (g,x,v) + b* _dy rtol= 1e-2
95117
96118L = HesVec (g, x)
97119@test L * x ≈ numauto_hesvec (g, x, x)
98120@test L * v ≈ numauto_hesvec (g, x, v)
99121@test mul! (dy, L, v)≈ numauto_hesvec (g, x, v) rtol= 1e-8
100- L. x .= v
122+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* numauto_hesvec (g,x,v)+ b* _dy rtol= 1e-8
123+ update_coefficients! (L, v, nothing , 0.0 )
101124@test mul! (dy, L, v)≈ numauto_hesvec (g, v, v) rtol= 1e-8
125+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* numauto_hesvec (g,x,v)+ b* _dy rtol= 1e-8
102126
103- # ## Integration test with IterativeSolvers
104127out = similar (v)
105128gmres! (out, L, v)
106129
107- x = rand (300 )
108- v = rand (300 )
130+ # HesVecGrad
131+
132+ x = rand (N)
133+ v = rand (N)
109134L = HesVecGrad (h, x, autodiff = false )
110135@test L * x ≈ num_hesvec (g, x, x)
111136@test L * v ≈ num_hesvec (g, x, v)
112137@test mul! (dy, L, v)≈ num_hesvec (g, x, v) rtol= 1e-2
113- L. x .= v
138+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* num_hesvec (g,x,v)+ b* _dy rtol= 1e-2
139+ update_coefficients! (L, v, nothing , 0.0 )
114140@test mul! (dy, L, v)≈ num_hesvec (g, v, v) rtol= 1e-2
141+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* num_hesvec (g,x,v)+ b* _dy rtol= 1e-2
115142
116143L = HesVecGrad (h, x, autodiff = true )
117144@test L * x ≈ autonum_hesvec (g, x, x)
118145@test L * v ≈ numauto_hesvec (g, x, v)
119146@test mul! (dy, L, v)≈ numauto_hesvec (g, x, v) rtol= 1e-8
120- L. x .= v
147+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* numauto_hesvec (g,x,v)+ b* _dy rtol= 1e-8
148+ update_coefficients! (L, v, nothing , 0.0 )
121149@test mul! (dy, L, v)≈ numauto_hesvec (g, v, v) rtol= 1e-8
150+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* numauto_hesvec (g,x,v)+ b* _dy rtol= 1e-8
122151
123- # ## Integration test with IterativeSolvers
124152out = similar (v)
125153gmres! (out, L, v)
154+ #
0 commit comments