@@ -4,15 +4,23 @@ using LinearAlgebra, Test
44using Random
55Random. seed! (123 )
66N = 300
7- const A = rand (N, N)
8-
9- _f (y, x) = mul! (y, A, x.^ 2 )
10- _f (x) = A * (x.^ 2 )
117
128x = rand (N)
139v = rand (N)
10+
11+ # Save original values of x and v to make sure they are not ever mutated
12+ x0 = copy (x)
13+ v0 = copy (v)
14+
1415a, b = rand (2 )
1516dy = similar (x)
17+
18+ # Define functions for testing
19+
20+ A = rand (N, N)
21+ _f (y, x) = mul! (y, A, x.^ 2 )
22+ _f (x) = A * (x.^ 2 )
23+
1624_g (x) = sum (abs2, x.^ 2 )
1725function _h (x)
1826 FiniteDiff. finite_difference_gradient (_g, x)
@@ -21,7 +29,7 @@ function _h(dy, x)
2129 FiniteDiff. finite_difference_gradient! (dy, _g, x)
2230end
2331
24- # Define state-dependent functions for operator tests
32+ # Make functions state-dependent for operator tests
2533
2634include (" update_coeffs_testutils.jl" )
2735f = WrapFunc (_f, 1.0 , 1.0 )
@@ -47,152 +55,147 @@ cache2 = ForwardDiff.Dual{typeof(ForwardDiff.Tag(SparseDiffTools.DeivVecTag(), e
4755 similar (v))≈ ForwardDiff. hessian (g, x) * v rtol= 1e-2
4856@test num_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol= 1e-2
4957
50- @test numauto_hesvec! (dy, g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-8
58+ @test numauto_hesvec! (dy, g, x, v)≈ ForwardDiff. hessian (g, x) * v
5159@test numauto_hesvec! (dy, g, x, v, ForwardDiff. GradientConfig (g, x), similar (v),
52- similar (v))≈ ForwardDiff. hessian (g, x) * v rtol = 1e-8
53- @test numauto_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-8
60+ similar (v))≈ ForwardDiff. hessian (g, x) * v
61+ @test numauto_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v
5462
55- @test autonum_hesvec! (dy, g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-2
63+ @test autonum_hesvec! (dy, g, x, v)≈ ForwardDiff. hessian (g, x) * v
5664@test autonum_hesvec! (dy, g, x, v, cache1, cache2)≈ ForwardDiff. hessian (g, x) * v rtol= 1e-2
57- @test autonum_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-8
65+ @test autonum_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v
5866
59- @test numback_hesvec! (dy, g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol= 1e-8
60- @test numback_hesvec! (dy, g, x, v, similar (v), similar (v))≈ ForwardDiff. hessian (g, x) * v rtol= 1e-8
61- @test numback_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol= 1e-8
67+ @test numback_hesvec! (dy, g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol= 1e-6
68+ @test numback_hesvec! (dy, g, x, v, similar (v), similar (v))≈ ForwardDiff. hessian (g, x) * v rtol= 1e-6
69+ @test numback_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol= 1e-6
6270
6371cache3 = ForwardDiff. Dual{typeof (ForwardDiff. Tag (Nothing, eltype (x))), eltype (x), 1
6472 }. (x, ForwardDiff. Partials .(tuple .(v)))
6573cache4 = ForwardDiff. Dual{typeof (ForwardDiff. Tag (Nothing, eltype (x))), eltype (x), 1
6674 }. (x, ForwardDiff. Partials .(tuple .(v)))
67- @test autoback_hesvec! (dy, g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-8
68- @test autoback_hesvec! (dy, g, x, v, cache3, cache4)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-8
69- @test autoback_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-8
75+ @test autoback_hesvec! (dy, g, x, v)≈ ForwardDiff. hessian (g, x) * v
76+ @test autoback_hesvec! (dy, g, x, v, cache3, cache4)≈ ForwardDiff. hessian (g, x) * v
77+ @test autoback_hesvec (g, x, v)≈ ForwardDiff. hessian (g, x) * v
7078
7179@test num_hesvecgrad! (dy, h, x, v)≈ ForwardDiff. hessian (g, x) * v rtol= 1e-2
7280@test num_hesvecgrad! (dy, h, x, v, similar (v), similar (v))≈ ForwardDiff. hessian (g, x) * v rtol= 1e-2
7381@test num_hesvecgrad (h, x, v)≈ ForwardDiff. hessian (g, x) * v rtol= 1e-2
7482
75- @test auto_hesvecgrad! (dy, h, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-2
76- @test auto_hesvecgrad! (dy, h, x, v, cache1, cache2)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-2
77- @test auto_hesvecgrad (h, x, v)≈ ForwardDiff. hessian (g, x) * v rtol = 1e-2
83+ @test auto_hesvecgrad! (dy, h, x, v)≈ ForwardDiff. hessian (g, x) * v
84+ @test auto_hesvecgrad! (dy, h, x, v, cache1, cache2)≈ ForwardDiff. hessian (g, x) * v
85+ @test auto_hesvecgrad (h, x, v)≈ ForwardDiff. hessian (g, x) * v
7886
7987@info " JacVec"
8088
81- L = JacVec (f, x , 1.0 , 1.0 )
89+ L = JacVec (f, copy (x) , 1.0 , 1.0 )
8290update_coefficients! (f, x, 1.0 , 1.0 )
8391@test L * x ≈ auto_jacvec (f, x, x)
8492@test L * v ≈ auto_jacvec (f, x, v)
8593@test mul! (dy, L, v) ≈ auto_jacvec (f, x, v)
8694dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b) ≈ a* auto_jacvec (f,x,v) + b* _dy
8795update_coefficients! (L, v, 3.0 , 4.0 )
8896update_coefficients! (f, v, 3.0 , 4.0 )
89- @test mul! (dy, L, v ) ≈ auto_jacvec (f, v, v )
90- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v ,a,b) ≈ a* auto_jacvec (f,x,v ) + b* _dy
97+ @test mul! (dy, L, x ) ≈ auto_jacvec (f, v, x )
98+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,x ,a,b) ≈ a* auto_jacvec (f,v,x ) + b* _dy
9199update_coefficients! (f, v, 5.0 , 6.0 )
92- @test L (dy, v, 5.0 , 6.0 ) ≈ auto_jacvec (f,x ,v)
100+ @test L (dy, v, 5.0 , 6.0 ) ≈ auto_jacvec (f,v ,v)
93101
94- L = JacVec (f, x , 1.0 , 1.0 ; autodiff = AutoFiniteDiff ())
102+ L = JacVec (f, copy (x) , 1.0 , 1.0 ; autodiff = AutoFiniteDiff ())
95103update_coefficients! (f, x, 1.0 , 1.0 )
96104@test L * x ≈ num_jacvec (f, x, x)
97105@test L * v ≈ num_jacvec (f, x, v)
98106@test mul! (dy, L, v)≈ num_jacvec (f, x, v) rtol= 1e-6
99107dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b) ≈ a* num_jacvec (f,x,v) + b* _dy rtol= 1e-6
100108update_coefficients! (L, v, 3.0 , 4.0 )
101109update_coefficients! (f, v, 3.0 , 4.0 )
102- @test mul! (dy, L, v )≈ num_jacvec (f, v, v ) rtol= 1e-6
103- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v ,a,b) ≈ a* num_jacvec (f,x,v ) + b* _dy rtol= 1e-6
110+ @test mul! (dy, L, x )≈ num_jacvec (f, v, x ) rtol= 1e-6
111+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,x ,a,b) ≈ a* num_jacvec (f,v,x ) + b* _dy rtol= 1e-6
104112update_coefficients! (f, v, 5.0 , 6.0 )
105- @test L (dy, v, 5.0 , 6.0 ) ≈ num_jacvec (f,x ,v) rtol= 1e-6
113+ @test L (dy, v, 5.0 , 6.0 ) ≈ num_jacvec (f,v ,v) rtol= 1e-6
106114
107115out = similar (v)
108116@test_nowarn gmres! (out, L, v)
109117
110118@info " HesVec"
111119
112- x = rand (N)
113- v = rand (N)
114- L = HesVec (g, x, 1.0 , 1.0 , autodiff = AutoFiniteDiff ())
120+ L = HesVec (g, copy (x), 1.0 , 1.0 , autodiff = AutoFiniteDiff ())
115121update_coefficients! (g, x, 1.0 , 1.0 )
116122@test L * x ≈ num_hesvec (g, x, x) rtol= 1e-2
117- num_hesvec (g, x, x)
118123@test L * v ≈ num_hesvec (g, x, v) rtol= 1e-2
119124@test mul! (dy, L, v)≈ num_hesvec (g, x, v) rtol= 1e-2
120125dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b) ≈ a* num_hesvec (g,x,v) + b* _dy rtol= 1e-2
121126update_coefficients! (L, v, 3.0 , 4.0 )
122127update_coefficients! (g, v, 3.0 , 4.0 )
123- @test mul! (dy, L, v )≈ num_hesvec (g, v, v ) rtol= 1e-2
124- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v ,a,b) ≈ a* num_hesvec (g,x,v ) + b* _dy rtol= 1e-2
128+ @test mul! (dy, L, x )≈ num_hesvec (g, v, x ) rtol= 1e-2
129+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,x ,a,b) ≈ a* num_hesvec (g,v,x ) + b* _dy rtol= 1e-2
125130update_coefficients! (g, v, 5.0 , 6.0 )
126- @test L (dy, v, 5.0 , 6.0 ) ≈ num_hesvec (g,x ,v) rtol= 1e-2
131+ @test L (dy, v, 5.0 , 6.0 ) ≈ num_hesvec (g,v ,v) rtol= 1e-2
127132
128- L = HesVec (g, x, 1.0 , 1.0 )
129- update_coefficients! (g, x, 1.0 , 1.0 )
130- numauto_hesvec (g, x, x)
131- num_hesvec (g, x, x)
133+ L = HesVec (g, copy (x), 1.0 , 1.0 )
132134@test L * x ≈ numauto_hesvec (g, x, x)
133135@test L * v ≈ numauto_hesvec (g, x, v)
134- @test mul! (dy, L, v)≈ numauto_hesvec (g, x, v) rtol = 1e-8
135- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b )≈ a* numauto_hesvec (g,x,v)+ b * _dy rtol = 1e-8
136+ @test mul! (dy, L, v)≈ numauto_hesvec (g, x, v)
137+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,0 )≈ a* numauto_hesvec (g,x,v)+ 0 * _dy
136138update_coefficients! (L, v, 3.0 , 4.0 )
137139update_coefficients! (g, v, 3.0 , 4.0 )
138- @test mul! (dy, L, v )≈ numauto_hesvec (g, v, v) rtol = 1e-8
139- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v ,a,b)≈ a* numauto_hesvec (g,x,v )+ b* _dy rtol = 1e-8
140+ @test mul! (dy, L, x )≈ numauto_hesvec (g, v, x)
141+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,x ,a,b)≈ a* numauto_hesvec (g,v,x )+ b* _dy
140142update_coefficients! (g, v, 5.0 , 6.0 )
141- @test L (dy, v, 5.0 , 6.0 ) ≈ numauto_hesvec (g,x ,v) rtol = 1e-2
143+ @test L (dy, v, 5.0 , 6.0 ) ≈ numauto_hesvec (g,v ,v)
142144
143145out = similar (v)
144146gmres! (out, L, v)
145147
146- x = rand (N)
147- v = rand (N)
148-
149- L = HesVec (g, x, 1.0 , 1.0 ; autodiff = AutoZygote ())
148+ L = HesVec (g, copy (x), 1.0 , 1.0 ; autodiff = AutoZygote ())
150149update_coefficients! (g, x, 1.0 , 1.0 )
151150@test L * x ≈ autoback_hesvec (g, x, x)
152151@test L * v ≈ autoback_hesvec (g, x, v)
153- @test mul! (dy, L, v)≈ autoback_hesvec (g, x, v) rtol = 1e-8
154- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* autoback_hesvec (g,x,v)+ b* _dy rtol = 1e-8
152+ @test mul! (dy, L, v)≈ autoback_hesvec (g, x, v)
153+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* autoback_hesvec (g,x,v)+ b* _dy
155154update_coefficients! (L, v, 3.0 , 4.0 )
156155update_coefficients! (g, v, 3.0 , 4.0 )
157- @test mul! (dy, L, v )≈ autoback_hesvec (g, v, v) rtol = 1e-8
158- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v ,a,b)≈ a* autoback_hesvec (g,x,v )+ b* _dy rtol = 1e-8
156+ @test mul! (dy, L, x )≈ autoback_hesvec (g, v, x)
157+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,x ,a,b)≈ a* autoback_hesvec (g,v,x )+ b* _dy
159158update_coefficients! (g, v, 5.0 , 6.0 )
160- @test L (dy, v, 5.0 , 6.0 ) ≈ autoback_hesvec (g,x ,v) rtol = 1e-2
159+ @test L (dy, v, 5.0 , 6.0 ) ≈ autoback_hesvec (g,v ,v)
161160
162161out = similar (v)
163162gmres! (out, L, v)
164163
165164@info " HesVecGrad"
166165
167- x = rand (N)
168- v = rand (N)
169- L = HesVecGrad (h, x, 1.0 , 1.0 ; autodiff = AutoFiniteDiff ())
166+ L = HesVecGrad (h, copy (x), 1.0 , 1.0 ; autodiff = AutoFiniteDiff ())
170167update_coefficients! (h, x, 1.0 , 1.0 )
171168update_coefficients! (g, x, 1.0 , 1.0 )
172169@test L * x ≈ num_hesvec (g, x, x) rtol= 1e-2
173170@test L * v ≈ num_hesvec (g, x, v) rtol= 1e-2
174171@test mul! (dy, L, v)≈ num_hesvec (g, x, v) rtol= 1e-2
175172dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* num_hesvec (g,x,v)+ b* _dy rtol= 1e-2
176173for op in (L, g, h) update_coefficients! (op, v, 3.0 , 4.0 ) end
177- @test mul! (dy, L, v )≈ num_hesvec (g, v, v ) rtol= 1e-2
178- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v ,a,b)≈ a* num_hesvec (g,x,v )+ b* _dy rtol= 1e-2
174+ @test mul! (dy, L, x )≈ num_hesvec (g, v, x ) rtol= 1e-2
175+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,x ,a,b)≈ a* num_hesvec (g,v,x )+ b* _dy rtol= 1e-2
179176update_coefficients! (g, v, 5.0 , 6.0 )
180- @test L (dy, v, 5.0 , 6.0 ) ≈ num_hesvec (g,x ,v) rtol= 1e-2
177+ @test L (dy, v, 5.0 , 6.0 ) ≈ num_hesvec (g,v ,v) rtol= 1e-2
181178
182- L = HesVecGrad (h, x , 1.0 , 1.0 )
179+ L = HesVecGrad (h, copy (x) , 1.0 , 1.0 )
183180update_coefficients! (g, x, 1.0 , 1.0 )
184181update_coefficients! (h, x, 1.0 , 1.0 )
185182@test L * x ≈ autonum_hesvec (g, x, x)
186183@test L * v ≈ numauto_hesvec (g, x, v)
187184@test mul! (dy, L, v)≈ numauto_hesvec (g, x, v) rtol= 1e-8
188- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* numauto_hesvec (g,x,v)+ b* _dy rtol = 1e-8
185+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v,a,b)≈ a* numauto_hesvec (g,x,v)+ b* _dy
189186for op in (L, g, h) update_coefficients! (op, v, 3.0 , 4.0 ) end
190- @test mul! (dy, L, v )≈ numauto_hesvec (g, v, v) rtol = 1e-8
191- dy= rand (N);_dy= copy (dy);@test mul! (dy,L,v ,a,b)≈ a* numauto_hesvec (g,x,v )+ b* _dy rtol = 1e-8
187+ @test mul! (dy, L, x )≈ numauto_hesvec (g, v, x)
188+ dy= rand (N);_dy= copy (dy);@test mul! (dy,L,x ,a,b)≈ a* numauto_hesvec (g,v,x )+ b* _dy
192189update_coefficients! (g, v, 5.0 , 6.0 )
193190update_coefficients! (h, v, 5.0 , 6.0 )
194- @test L (dy, v, 5.0 , 6.0 ) ≈ num_hesvec (g,x ,v) rtol = 1e-2
191+ @test L (dy, v, 5.0 , 6.0 ) ≈ numauto_hesvec (g,v ,v)
195192
196193out = similar (v)
197194gmres! (out, L, v)
195+
196+ # Test that x and v were not mutated
197+ # x's rtol can't be too large since it is mutated and then restored in some algorithms
198+ @test x ≈ x0
199+ @test v ≈ v0
200+
198201#
0 commit comments