@@ -6,17 +6,22 @@ function auto_jacvec!(
66 f,
77 x,
88 v,
9- cache1 = Dual {DeivVecTag} .(x, reshape (v, size (x))),
9+ cache1 = Dual {typeof(ForwardDiff.Tag( DeivVecTag,eltype(x))),eltype(x),1 } .(x, ForwardDiff . Partials .( Tuple .( reshape (v, size (x)) ))),
1010 cache2 = similar (cache1),
1111)
12- cache1 .= Dual {DeivVecTag} .(x, reshape (v, size (x)))
12+ cache1 .= Dual {typeof(ForwardDiff.Tag( DeivVecTag,eltype(x))),eltype(x),1 } .(x, ForwardDiff . Partials .( Tuple .( reshape (v, size (x)) )))
1313 f (cache2, cache1)
14- dy .= partials .(cache2, 1 )
14+ vecdy = _vec (dy)
15+ vecdy .= partials .(_vec (cache2), 1 )
1516end
1617
18+ _vec (v) = vec (v)
19+ _vec (v:: AbstractVector ) = v
20+
1721function auto_jacvec (f, x, v)
1822 vv = reshape (v, axes (x))
19- vec (partials .(vec (f (ForwardDiff. Dual {DeivVecTag} .(x, vv))), 1 ))
23+ y = ForwardDiff. Dual {typeof(ForwardDiff.Tag(DeivVecTag,eltype(x))),eltype(x),1} .(x, ForwardDiff. Partials .(Tuple .(vv)))
24+ vec (partials .(vec (f (y)), 1 ))
2025end
2126
2227function num_jacvec! (
@@ -122,12 +127,12 @@ function autonum_hesvec!(
122127 f,
123128 x,
124129 v,
125- cache1 = ForwardDiff . Dual {DeivVecTag} .(x, v ),
126- cache2 = ForwardDiff . Dual {DeivVecTag} .(x, v ),
130+ cache1 = Dual {typeof(ForwardDiff.Tag( DeivVecTag,eltype(x))),eltype(x),1 } .(x, ForwardDiff . Partials .( Tuple .( reshape (v, size (x)))) ),
131+ cache2 = Dual {typeof(ForwardDiff.Tag( DeivVecTag,eltype(x))),eltype(x),1 } .(x, ForwardDiff . Partials .( Tuple .( reshape (v, size (x)))) ),
127132)
128133 cache = FiniteDiff. GradientCache (v[1 ], cache1, Val{:central })
129134 g = (dx, x) -> FiniteDiff. finite_difference_gradient! (dx, f, x, cache)
130- cache1 .= Dual {DeivVecTag} .(x, v )
135+ cache1 .= Dual {typeof(ForwardDiff.Tag( DeivVecTag,eltype(x))),eltype(x),1 } .(x, ForwardDiff . Partials .( Tuple .( reshape (v, size (x)))) )
131136 g (cache2, cache1)
132137 dy .= partials .(cache2, 1 )
133138end
@@ -164,16 +169,17 @@ function auto_hesvecgrad!(
164169 g,
165170 x,
166171 v,
167- cache2 = ForwardDiff . Dual {DeivVecTag} .(x, v ),
168- cache3 = ForwardDiff . Dual {DeivVecTag} .(x, v ),
172+ cache2 = Dual {typeof(ForwardDiff.Tag( DeivVecTag,eltype(x))),eltype(x),1 } .(x, ForwardDiff . Partials .( Tuple .( reshape (v, size (x)))) ),
173+ cache3 = Dual {typeof(ForwardDiff.Tag( DeivVecTag,eltype(x))),eltype(x),1 } .(x, ForwardDiff . Partials .( Tuple .( reshape (v, size (x)))) ),
169174)
170- cache2 .= Dual {DeivVecTag} .(x, v )
175+ cache2 .= Dual {typeof(ForwardDiff.Tag( DeivVecTag,eltype(x))),eltype(x),1 } .(x, ForwardDiff . Partials .( Tuple .( reshape (v, size (x)))) )
171176 g (cache3, cache2)
172177 dy .= partials .(cache3, 1 )
173178end
174179
175180function auto_hesvecgrad (g, x, v)
176- partials .(g (Dual {DeivVecTag} .(x, v)), 1 )
181+ y = Dual {typeof(ForwardDiff.Tag(DeivVecTag,eltype(x))),eltype(x),1} .(x, ForwardDiff. Partials .(Tuple .(reshape (v, size (x)))))
182+ partials .(g (y), 1 )
177183end
178184
179185# ## Operator Forms
@@ -188,15 +194,16 @@ end
188194
189195function JacVec (f, x:: AbstractArray ; autodiff = true )
190196 if autodiff
191- cache1 = ForwardDiff . Dual {DeivVecTag} .(x, x )
192- cache2 = ForwardDiff . Dual {DeivVecTag} .(x, x )
197+ cache1 = Dual {typeof(ForwardDiff.Tag( DeivVecTag,eltype(x))),eltype(x),1 } .(x, ForwardDiff . Partials .( Tuple .(x)) )
198+ cache2 = Dual {typeof(ForwardDiff.Tag( DeivVecTag,eltype(x))),eltype(x),1 } .(x, ForwardDiff . Partials .( Tuple .(x)) )
193199 else
194200 cache1 = similar (x)
195201 cache2 = similar (x)
196202 end
197203 JacVec (f, cache1, cache2, x, autodiff)
198204end
199205
206+ Base. eltype (L:: JacVec ) = eltype (L. x)
200207Base. size (L:: JacVec ) = (length (L. cache1), length (L. cache1))
201208Base. size (L:: JacVec , i:: Int ) = length (L. cache1)
202209Base.:* (L:: JacVec , v:: AbstractVector ) =
256263
257264function HesVecGrad (g, x:: AbstractArray ; autodiff = false )
258265 if autodiff
259- cache1 = ForwardDiff . Dual {DeivVecTag} .(x, x )
260- cache2 = ForwardDiff . Dual {DeivVecTag} .(x, x )
266+ cache1 = Dual {typeof(ForwardDiff.Tag( DeivVecTag,eltype(x))),eltype(x),1 } .(x, ForwardDiff . Partials .( Tuple .(x)) )
267+ cache2 = Dual {typeof(ForwardDiff.Tag( DeivVecTag,eltype(x))),eltype(x),1 } .(x, ForwardDiff . Partials .( Tuple .(x)) )
261268 else
262269 cache1 = similar (x)
263270 cache2 = similar (x)
0 commit comments